From patchwork Fri Nov 22 10:30:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 180016 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp53822ilf; Fri, 22 Nov 2019 03:06:51 -0800 (PST) X-Google-Smtp-Source: APXvYqxAUM7VJ2T7FQD3A/5G7neG0LZzVsjQnoY92fbz9omTYMZIAP6sobAuZf8xpH9AIndmOJrc X-Received: by 2002:a05:6402:324:: with SMTP id q4mr324969edw.108.1574420811435; Fri, 22 Nov 2019 03:06:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574420811; cv=none; d=google.com; s=arc-20160816; b=eSMvU/EssdwZ1gxYLU68jluwS64RgCznyqWA/whvA8X/aGlxxHWgeYn8Fca3M8EDU5 lyRtbdKUQ7i9XMMXppEN4vro2vvUWcU5wodKH5RYmBZizv6IE+sS2wtRaoSq5xV/RoDp kq8rKfwtH5rgsmt0Pdo9EEoX24zxcFUzOitvLWg8FEUJptOo9u4HDj5rfWDicbB7ASnd kjAVeUB21wyh3IJ5KP46Dnc6EPeUHt36QqNWX71FbSz64kMgT6/FbIgUCotkr0ewLGGu ClheXpljyzP6TF7xODsRpBjg2CNePzYWo6X+vCeRqLgAidY9Q6cyFLotmArwgAQ7avX3 fu0Q== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2FntFZ4tGWG7dSr4UUp5i3xU2jWM7bsqE0SGR2XoPL4=; b=pccGWvq88LuaXdetMnCwAOb7k++JauLWLW9gHRoXS2QXYUrPAMQyIJC3davWRbQTQ1 y6iN4oQOazUWOfJ/1iS3nWwZH938OgYkDOrKsV0IcMbcMpE2euf6Gkt3MjvQBlzAo8b6 bWFdH40GpN/fSMh3AKSMaIiu60KQFHmd6bAvCyFthveDjl6M3AUh7v8QMDhfIBhR6QVa cWNHj2oEt/zvqdZVBvrRho6PFT24ETci/WtHRLq7RoNtg6xBZZBZHbEsSQtItTiEBoGb fVAlOx4957O5jTyLAHhd5/tDgpHEQqgzThSTGWLCHLryYidPipGtiFxSbEKn7LP3BGl3 +jNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wRrhsAdu; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i47si4763503eda.91.2019.11.22.03.06.51; Fri, 22 Nov 2019 03:06:51 -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=@kernel.org header.s=default header.b=wRrhsAdu; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731339AbfKVLGt (ORCPT + 26 others); Fri, 22 Nov 2019 06:06:49 -0500 Received: from mail.kernel.org ([198.145.29.99]:35094 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731013AbfKVLGk (ORCPT ); Fri, 22 Nov 2019 06:06:40 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6228E20885; Fri, 22 Nov 2019 11:06:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574420799; bh=rl0J2kUgD8Q7v5xJ3ktPrZJwJ0UTuHJGEvm0mn5/a2o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wRrhsAduIY8H87L7UEAIcC7ASXirk7MclJdUxtB9j/SqaaKKg/fc8NbvqXg8/TUWC tEi3b3JDXeVeSfgM5FLeRZDfeICy3/0FkruACP7JM9uE1fV9GrQNvYaOUlPmCOwIZn NrmbuB0/dsUDMOfu9629YMDNgKAl4reErSMiaJLc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chris Evich , Patrick Dung , Thorsten Schubert , Oleksandr Natalenko , Paolo Valente , Jens Axboe , Laura Abbott Subject: [PATCH 5.3 2/6] block, bfq: deschedule empty bfq_queues not referred by any process Date: Fri, 22 Nov 2019 11:30:04 +0100 Message-Id: <20191122100322.673010728@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191122100320.878809004@linuxfoundation.org> References: <20191122100320.878809004@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paolo Valente commit 478de3380c1c7dbb0f65f545ee0185848413f3fe upstream. Since commit 3726112ec731 ("block, bfq: re-schedule empty queues if they deserve I/O plugging"), to prevent the service guarantees of a bfq_queue from being violated, the bfq_queue may be left busy, i.e., scheduled for service, even if empty (see comments in __bfq_bfqq_expire() for details). But, if no process will send requests to the bfq_queue any longer, then there is no point in keeping the bfq_queue scheduled for service. In addition, keeping the bfq_queue scheduled for service, but with no process reference any longer, may cause the bfq_queue to be freed when descheduled from service. But this is assumed to never happen, and causes a UAF if it happens. This, in turn, caused crashes [1, 2]. This commit fixes this issue by descheduling an empty bfq_queue when it remains with not process reference. [1] https://bugzilla.redhat.com/show_bug.cgi?id=1767539 [2] https://bugzilla.kernel.org/show_bug.cgi?id=205447 Fixes: 3726112ec731 ("block, bfq: re-schedule empty queues if they deserve I/O plugging") Reported-by: Chris Evich Reported-by: Patrick Dung Reported-by: Thorsten Schubert Tested-by: Thorsten Schubert Tested-by: Oleksandr Natalenko Signed-off-by: Paolo Valente Signed-off-by: Jens Axboe Cc: Laura Abbott Signed-off-by: Greg Kroah-Hartman --- block/bfq-iosched.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -2699,6 +2699,28 @@ static void bfq_bfqq_save_state(struct b } } + +static +void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq) +{ + /* + * To prevent bfqq's service guarantees from being violated, + * bfqq may be left busy, i.e., queued for service, even if + * empty (see comments in __bfq_bfqq_expire() for + * details). But, if no process will send requests to bfqq any + * longer, then there is no point in keeping bfqq queued for + * service. In addition, keeping bfqq queued for service, but + * with no process ref any longer, may have caused bfqq to be + * freed when dequeued from service. But this is assumed to + * never happen. + */ + if (bfq_bfqq_busy(bfqq) && RB_EMPTY_ROOT(&bfqq->sort_list) && + bfqq != bfqd->in_service_queue) + bfq_del_bfqq_busy(bfqd, bfqq, false); + + bfq_put_queue(bfqq); +} + static void bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic, struct bfq_queue *bfqq, struct bfq_queue *new_bfqq) @@ -2769,8 +2791,7 @@ bfq_merge_bfqqs(struct bfq_data *bfqd, s */ new_bfqq->pid = -1; bfqq->bic = NULL; - /* release process reference to bfqq */ - bfq_put_queue(bfqq); + bfq_release_process_ref(bfqd, bfqq); } static bool bfq_allow_bio_merge(struct request_queue *q, struct request *rq, @@ -4885,7 +4906,7 @@ static void bfq_exit_bfqq(struct bfq_dat bfq_put_cooperator(bfqq); - bfq_put_queue(bfqq); /* release process reference */ + bfq_release_process_ref(bfqd, bfqq); } static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync) @@ -4987,8 +5008,7 @@ static void bfq_check_ioprio_change(stru bfqq = bic_to_bfqq(bic, false); if (bfqq) { - /* release process reference on this queue */ - bfq_put_queue(bfqq); + bfq_release_process_ref(bfqd, bfqq); bfqq = bfq_get_queue(bfqd, bio, BLK_RW_ASYNC, bic); bic_set_bfqq(bic, bfqq, false); } @@ -5948,7 +5968,7 @@ bfq_split_bfqq(struct bfq_io_cq *bic, st bfq_put_cooperator(bfqq); - bfq_put_queue(bfqq); + bfq_release_process_ref(bfqq->bfqd, bfqq); return NULL; }