From patchwork Tue Jan 29 11:06:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valente X-Patchwork-Id: 156966 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp4523200jaa; Tue, 29 Jan 2019 03:07:44 -0800 (PST) X-Google-Smtp-Source: ALg8bN5hA/yC7n3qc8QCTqgXPrjyjoGHURNOP7gdjEScg1Mg5ZfF9lKfpiUiloivJUSPfM560neB X-Received: by 2002:a17:902:b48b:: with SMTP id y11mr24570197plr.200.1548760064209; Tue, 29 Jan 2019 03:07:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548760064; cv=none; d=google.com; s=arc-20160816; b=lUspsQomGqwzwzkXsXbc/B9GjaeiJt2Nefb3gy+/9T835EDoHALJCCJFomNVmrlOdR tAWfNEPK/TZ6OqxdGHpIhZC23fOGmQAXwbSKpIk89eVSWqdEy7qitMLbRsvAjTiui1Dw +4y9n6RE9o63pOLb/FnTBbaBicTStuLAGric1DNNJ+/7dASsmUdbi8wySiyKKBh1VDFA QLO4pS0fmWjJXL81iuPgQB/JyW4IFJ1WvUV5CsXjLjBbT6vs8LBRx1Wzrf3H6ZAlAbGW I2CQwDljPkXGxPQCZ4kIF+7vKWn9qcMfaUX4k9qqP7/iwzTEfsfJF2sWivQDe66dHeq6 7glg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ui0RozgVj782eZenovCwcdQYstEPpPQMeO8MT0fjT5o=; b=J3XWo+la9ehanyrB1I8RCY3lxMOUweBpcaXfLsaaHGVBjHdEXX4BHvjC3TeONLaKm0 HK/YgBVtoYpW2wKw9rK0PsFOQTcMt20qHXcoeFMGo/xRylfXTOnzn9suFJr5xMV3hbNQ 5r+vebmDMecFe56Eril4tta2yIP11We9dEJejSzvO4KZrC6jy79hmNjtR56JWNpNp2WE 5ZNMHt61kI4yXy1PZeNeiD93zlwzGya7uAGUtB5WL7bwqmFrAUDJXyHPvUsI8E/e4dE0 wE7Km4trIiw0LxSnN1+uOCFV/RLcjoOn4zXwAO4VEf0RZ2oCrDfB/VZtdZ4DQ6JrHzz/ fG6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="L/8rgA1a"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o3si35074388pgq.139.2019.01.29.03.07.43; Tue, 29 Jan 2019 03:07:44 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="L/8rgA1a"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728681AbfA2LHm (ORCPT + 31 others); Tue, 29 Jan 2019 06:07:42 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:45596 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727728AbfA2LHL (ORCPT ); Tue, 29 Jan 2019 06:07:11 -0500 Received: by mail-wr1-f66.google.com with SMTP id t6so21506279wrr.12 for ; Tue, 29 Jan 2019 03:07:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ui0RozgVj782eZenovCwcdQYstEPpPQMeO8MT0fjT5o=; b=L/8rgA1a0X/Tzr6jFJ7LPCtro61Yhi/kU9AXGI8zscrdDCQQoDzxOAgtOlxQEJmkqQ Im3eeKa7fN2xw9GQceKjWdHYXTC11GBPY67zB3gdzRXMhhAjAD6CISk+xyKiBEO5cAtj amn9Kk1fJqkf9621GzFX8GpON9gGM72ruLoC0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ui0RozgVj782eZenovCwcdQYstEPpPQMeO8MT0fjT5o=; b=EbYvGDpai0a6O1sH7u2t3Q/OXeKlRlcjpTQjya8P7AGde2s0gSwc5CATx3uSLJZncu nGFj/lRNlTOJFb57+U7Sy/QOSfHZaFvAoro8od7g4Ax4RZhAPpIvDMTjeDLWz4e/xoQ0 uK3OyKK4VAKs2scNogN+w5xYjd7mjkoEE6d0nZcc06e15gg4Ic6VlQE5VgsaKyn1aNuK WSG4usSoNExrGYbrYHhnSZd01RSjhO+9AIIikNTfDk0t+XugnXM3J8U7B6M50bJpQlEs mH0VNOcdEC8lLhtdgfA6JyZcd9JJUC2X4lXbAkRMvjxhtyLFnds2hxgYMCYrprEvl1p8 k1nA== X-Gm-Message-State: AJcUukea3OhXGo/58R1pftbd5wxn2EQgZyWzqK1ORSqXbxfE9muDb5Eu osNPlMA1zkIf9mcerjC+LOew3Q== X-Received: by 2002:adf:8068:: with SMTP id 95mr26094476wrk.181.1548760030187; Tue, 29 Jan 2019 03:07:10 -0800 (PST) Received: from localhost.localdomain ([88.147.67.218]) by smtp.gmail.com with ESMTPSA id s132sm2066112wmf.28.2019.01.29.03.07.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Jan 2019 03:07:09 -0800 (PST) From: Paolo Valente To: Jens Axboe Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, ulf.hansson@linaro.org, linus.walleij@linaro.org, broonie@kernel.org, bfq-iosched@googlegroups.com, oleksandr@natalenko.name, mancha@tower-research.com, Paolo Valente Subject: [PATCH BUGFIX IMPROVEMENT 10/14] block, bfq: fix queue removal from weights tree Date: Tue, 29 Jan 2019 12:06:34 +0100 Message-Id: <20190129110638.12652-11-paolo.valente@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190129110638.12652-1-paolo.valente@linaro.org> References: <20190129110638.12652-1-paolo.valente@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org bfq maintains an ordered list, through a red-black tree, of unique weights of active bfq_queues. This list is used to detect whether there are active queues with differentiated weights. The weight of a queue is removed from the list when both the following two conditions become true: (1) the bfq_queue is flagged as inactive (2) the has no in-flight request any longer; Unfortunately, in the rare cases where condition (2) becomes true before condition (1), the removal fails, because the function to remove the weight of the queue (bfq_weights_tree_remove) is rightly invoked in the path that deactivates the bfq_queue, but mistakenly invoked *before* the function that actually performs the deactivation (bfq_deactivate_bfqq). This commits moves the invocation of bfq_weights_tree_remove for condition (1) to after bfq_deactivate_bfqq. As a consequence of this move, it is necessary to add a further reference to the queue when the weight of a queue is added, because the queue might otherwise be freed before bfq_weights_tree_remove is invoked. This commit adds this reference and makes all related modifications. Signed-off-by: Paolo Valente --- block/bfq-iosched.c | 17 +++++++++++++---- block/bfq-wf2q.c | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) -- 2.20.1 diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index 12228af16198..bf585ad29bb5 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -754,6 +754,7 @@ void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq, inc_counter: bfqq->weight_counter->num_active++; + bfqq->ref++; } /* @@ -778,6 +779,7 @@ void __bfq_weights_tree_remove(struct bfq_data *bfqd, reset_entity_pointer: bfqq->weight_counter = NULL; + bfq_put_queue(bfqq); } /* @@ -789,9 +791,6 @@ void bfq_weights_tree_remove(struct bfq_data *bfqd, { struct bfq_entity *entity = bfqq->entity.parent; - __bfq_weights_tree_remove(bfqd, bfqq, - &bfqd->queue_weights_tree); - for_each_entity(entity) { struct bfq_sched_data *sd = entity->my_sched_data; @@ -825,6 +824,15 @@ void bfq_weights_tree_remove(struct bfq_data *bfqd, bfqd->num_groups_with_pending_reqs--; } } + + /* + * Next function is invoked last, because it causes bfqq to be + * freed if the following holds: bfqq is not in service and + * has no dispatched request. DO NOT use bfqq after the next + * function invocation. + */ + __bfq_weights_tree_remove(bfqd, bfqq, + &bfqd->queue_weights_tree); } /* @@ -1020,7 +1028,8 @@ bfq_bfqq_resume_state(struct bfq_queue *bfqq, struct bfq_data *bfqd, static int bfqq_process_refs(struct bfq_queue *bfqq) { - return bfqq->ref - bfqq->allocated - bfqq->entity.on_st; + return bfqq->ref - bfqq->allocated - bfqq->entity.on_st - + (bfqq->weight_counter != NULL); } /* Empty burst list and add just bfqq (see comments on bfq_handle_burst) */ diff --git a/block/bfq-wf2q.c b/block/bfq-wf2q.c index ce37d709a34f..63311d1ff1ed 100644 --- a/block/bfq-wf2q.c +++ b/block/bfq-wf2q.c @@ -1673,15 +1673,15 @@ void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq, bfqd->busy_queues[bfqq->ioprio_class - 1]--; - if (!bfqq->dispatched) - bfq_weights_tree_remove(bfqd, bfqq); - if (bfqq->wr_coeff > 1) bfqd->wr_busy_queues--; bfqg_stats_update_dequeue(bfqq_group(bfqq)); bfq_deactivate_bfqq(bfqd, bfqq, true, expiration); + + if (!bfqq->dispatched) + bfq_weights_tree_remove(bfqd, bfqq); } /*