Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5643789img; Wed, 27 Mar 2019 12:18:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqwuLLH8bJd8In8lP5pMiuSe1DsFXebzv1uvhNYhQLwMfVCoezKMeAVJXjemB9alE+2O15Ul X-Received: by 2002:a17:902:7206:: with SMTP id ba6mr1418520plb.301.1553714322039; Wed, 27 Mar 2019 12:18:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553714322; cv=none; d=google.com; s=arc-20160816; b=GnY4BIGlYuVqiSAZYddAabKwXXr8Vg3VPYD5BgLlYfD8xKeI9iDduQupAhWeMQ7jyg Pkh2m5Dmg2+R1SBqz8+5vwPbN80RWgRmPQJmu4AtJ74HvaPc5wzYHc/GGKH+6DhJbUcG Bo0KrUJnTXj44yO25Jc1kK+IOcq9NNl7oFLKqiXt0rWwhRPwTjrt+SErL8qVR0t4aJyg VkDSeQkPW7GZOxp3dnYcyvK5V9o2DpRq6ViwlmHEbQGhNY9a/LIniBMNC7Wf2kGoWNZe rB9PZARgc9Xpx+ysoPF+J/NcFz1OGeMgSQy/9uFZnD2f+oaCdkqKyUrfSJv2JIyUzkZo rntA== 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=TO6iOwRHpXhGq0KsWkts3RXzQMHCXlVHtDMRrkHt1ew=; b=Vh903CsVls02xydRZLEq5/42PaxdBr0+v+YpYE2Dmnqb/3qukBMkG2Oe8hZTJijK2O TY1mn8GHvTVHH6a4ZQFaCEASfsWsOjx/mrjmNdvKa/BilSAlj3HrHVG+JV7CD7Qkjbd1 01C3qABcY1vlrV1bXOZxnYz/iDzeUv6hT2ue4nuFXSbrHQz5FYx/Tq3H631b4nV+OVff IHpTWxFYO1NGgg+ap8YFs3SWPaykF0az+Jsr5/GQyYMbekSSzs//24Gtn/B6YkxkYbCS BGGXyujkq3fXAaxqgBywDj9a276oR4MAtX9Jo9Ehgj5gsZmZjcifFME/WR6ObSzSKoRD G2Pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=f545AhMQ; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t18si18826063pgu.369.2019.03.27.12.18.26; Wed, 27 Mar 2019 12:18:42 -0700 (PDT) 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=@kernel.org header.s=default header.b=f545AhMQ; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387628AbfC0SIh (ORCPT + 99 others); Wed, 27 Mar 2019 14:08:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:50320 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731234AbfC0SIa (ORCPT ); Wed, 27 Mar 2019 14:08:30 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DCED021738; Wed, 27 Mar 2019 18:08:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710109; bh=J/++bZj6EMAxb1LPDclSEOFVVjmZFr85edmg43Bb2Ho=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f545AhMQYS1XqY0wKTBtBkikWxYKcJL92oAN4ZELDPnpNh5+ihJhyi1jNqsBFPl6i QtAGmHoWCedXqe0u6pYqggqXnsvwIycz+H8oOg/ilEzIk7UtcUd7ETUYl4nx9AXxWd Z8GybNezfR71IqrfnE6ynqSxuE9jnNAWEzjys34g= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Paolo Valente , Jens Axboe , Sasha Levin , linux-block@vger.kernel.org Subject: [PATCH AUTOSEL 5.0 199/262] block, bfq: fix queue removal from weights tree Date: Wed, 27 Mar 2019 14:00:54 -0400 Message-Id: <20190327180158.10245-199-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paolo Valente [ Upstream commit 9dee8b3b057e1da26f85f1842f2aaf3bb200fb94 ] 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 Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/bfq-iosched.c | 17 +++++++++++++---- block/bfq-wf2q.c | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index f010810c095f..e5ed28629271 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -747,6 +747,7 @@ void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq, inc_counter: bfqq->weight_counter->num_active++; + bfqq->ref++; } /* @@ -771,6 +772,7 @@ void __bfq_weights_tree_remove(struct bfq_data *bfqd, reset_entity_pointer: bfqq->weight_counter = NULL; + bfq_put_queue(bfqq); } /* @@ -782,9 +784,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; @@ -818,6 +817,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); } /* @@ -1011,7 +1019,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 72adbbe975d5..4aab1a8191f0 100644 --- a/block/bfq-wf2q.c +++ b/block/bfq-wf2q.c @@ -1667,15 +1667,15 @@ void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq, bfqd->busy_queues--; - 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); } /* -- 2.19.1