From patchwork Tue Nov 12 07:48:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valente X-Patchwork-Id: 179172 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp7758145ilf; Mon, 11 Nov 2019 23:49:26 -0800 (PST) X-Google-Smtp-Source: APXvYqyas8i7HwiOmPGVPYYmVE/Lmk28N3VrZpm8T1cNIt/+DxgzfIk3zEHNvmpxzjlx6Xo5LHDn X-Received: by 2002:a50:9b10:: with SMTP id o16mr30456645edi.117.1573544966359; Mon, 11 Nov 2019 23:49:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573544966; cv=none; d=google.com; s=arc-20160816; b=pQbo0rajRUkP30e/SVpCjjLjtOIy+9FG/gmQpn8EEQ6cl7L/ukqlp9DARu6jk3J2Il lQ4HviYj7b97SO8CynyaKLyIxoJV49W6kPQR7/bKgY14ZEdKcdOefY5x9Txo+vzyv22c LgBo2PE6TWe5u3dklK0q2lYqjqIDRl06yg89Yq0vuE7jbq7A6STJ4JWtSCesiBjojGvS oGvMHApd5ALZanmYQ/KjIHmqB2IhWhGZKi5+pKlbAEqoSAUGmLrSUxuFSP+sKlpztMSg h7X5dtythMik6SItvHVW392sTNA2N+cwqYEcUPtEHx3e5fXr6sAIOOfc8RqkPjyZkcE2 IGjw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=h+vr6PNUoMDGum6dWz+gtk31tHP3FZ8Tbn4zljQ9/yo=; b=CB6XFfyodmcZf84+0CjcaRx6wpDhO1sa7Va55teO6EwhqawKj7u+AVBNFiPVDooaSj noePmJAhhA+/TLHXepAxWInGTyFa1OLg84uaO/1uAzuUovJ2HxWBOVH+E3jL4r7jvXhe 1DEDO4eZEe2WKGWf9FRLppijH+DnYB+kUMHjfT4eDBowJPWXJ2aX5APaUN3vmT89UukC lhOJpKoX5pr/gvmAGUJqFmCocRmPTdL8drf/3oIdE7iDgAWXzMC4B8qlikr4BZGi3CwF eeCLhbMhJiYFGlMRmHkZzVLVcn0OpS5b+jjQvowB7PtOy3bxRmkMJND5UoyWhTTdCOcf dUIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=q61JAj3N; 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 p22si11057295eja.204.2019.11.11.23.49.26; Mon, 11 Nov 2019 23:49:26 -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=q61JAj3N; 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 S1725899AbfKLHtN (ORCPT + 26 others); Tue, 12 Nov 2019 02:49:13 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:36422 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725775AbfKLHtM (ORCPT ); Tue, 12 Nov 2019 02:49:12 -0500 Received: by mail-wm1-f66.google.com with SMTP id c22so1823710wmd.1 for ; Mon, 11 Nov 2019 23:49:11 -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:mime-version :content-transfer-encoding; bh=h+vr6PNUoMDGum6dWz+gtk31tHP3FZ8Tbn4zljQ9/yo=; b=q61JAj3Nv+2bmyX2BElQcE/CdF63fy8UBh9vOX4+5wtt1qU0l+At2fLHtC9V44RrvJ S6gH5dPypoWemgb4AWOp4ne7x9iRQKrBy+64JAkHzq3hw0IrIuXNPeLCM/HqucVOgJyf gGIf1QmA++75IxCNB6yH1fOwcM21UH3GVLy6T9eh/I0vVWwKu6T2GcGPgIGXtm4LERzW ssi8crq8fPSBCgIeaS1/YuVnHf4r45ZuezRy6R9+Q9eH/+rxlhnv+7RW0JZRoHDChMD2 msU6IFnEnoKk0zavHf23UlaOAZd+g3SawOh+uP8cF+7sbAPxEF00AAvhW4PZ8TRZoKo5 cYSw== 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:mime-version :content-transfer-encoding; bh=h+vr6PNUoMDGum6dWz+gtk31tHP3FZ8Tbn4zljQ9/yo=; b=eR6wlvvrA41CRr9wxYFuebqH2M0BNsJ5rtt12va7qjGmRDObB/58tGnmumA3wicsfl v67txu2CgoV7Ht12K+4bsB+cTiB7PyGurULE3JKESRjgx4Y9AqGqM5iw3FhTmxKQBETK a7qM9xR8aXWvsS2ezJzCJ7R0/2FKWIEfs4MwNp4VgtR9tk4Z4R//OljtGNCjnFjk+3ot pWY0JnfUYHvmSn81K/+jprvec1ENt2BCgysEj46wLQtURATAxuQ9CJJfuBrbhwAwOnrM NORfO+u2TWMi99lRxlmhgVaarhe4oSPQfYNXy9urKOHt6a+utmDXOzp/26UliwAGLOI9 llow== X-Gm-Message-State: APjAAAUbld/ZKuQYEqy7XVtGjgFkCs+DBQ8wU5IOrqzNM6ku0ugCZrS9 ZxLM8sjKM24RJ81w1IZjpMEqdQ== X-Received: by 2002:a1c:39c2:: with SMTP id g185mr2519283wma.88.1573544950426; Mon, 11 Nov 2019 23:49:10 -0800 (PST) Received: from localhost.localdomain (88-147-76-3.dyn.eolo.it. [88.147.76.3]) by smtp.gmail.com with ESMTPSA id f67sm2819974wme.16.2019.11.11.23.49.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Nov 2019 23:49: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, bfq-iosched@googlegroups.com, oleksandr@natalenko.name, Paolo Valente , Chris Evich , Patrick Dung , Thorsten Schubert Subject: [PATCH BUGFIX] block, bfq: deschedule empty bfq_queues not referred by any process Date: Tue, 12 Nov 2019 08:48:56 +0100 Message-Id: <20191112074856.40433-1-paolo.valente@linaro.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: Paolo Valente --- block/bfq-iosched.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) -- 2.20.1 diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index 0319d6339822..ba68627f7740 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -2713,6 +2713,27 @@ static void bfq_bfqq_save_state(struct bfq_queue *bfqq) } } + +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)) + 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) @@ -2783,8 +2804,7 @@ bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic, */ 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, @@ -4899,7 +4919,7 @@ static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq) 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) @@ -5001,8 +5021,7 @@ static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio) 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); } @@ -5963,7 +5982,7 @@ bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq) bfq_put_cooperator(bfqq); - bfq_put_queue(bfqq); + bfq_release_process_ref(bfqq->bfqd, bfqq); return NULL; }