Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp59244ybp; Thu, 3 Oct 2019 10:09:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqzSBllaAXX+WUIIHDnYr24omoC67XglmjRC1B8izXFeHq1CQNSH6Rx1G3fCDjFn8WdwoH3E X-Received: by 2002:aa7:c657:: with SMTP id z23mr10679466edr.234.1570122562898; Thu, 03 Oct 2019 10:09:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570122562; cv=none; d=google.com; s=arc-20160816; b=AYsoBvVymjbxhrmvv+tn65tdq6Y0RJ5vfe9swUrId2aL6ciV4uOv0o+nI5HC+XKh2R bdV/blXk3T5xHvBLty2Uu8P6nnjw+QHNLrr+/FW8Z+NNnkx5hfd5QYOf+xlUZ6zrpcdU scTTXreakDTJpC1o1JG1iFMbI81NY0813Reo6LaRwkvz2JguM0fZdQaKqAK3zDinMNVv cnAicm+MGybtvb1FTmL3WzZncgFsJ8zY/oGcFrQClTMySQ03YW+/lVxZLDURAOgZ5Bd6 VhkgsfzRObaMakO4mFLKrq3iuvlxOnceixSvPCrm44xJmdM1p5qN5HJHN7AoNV1/D99Z CfxA== 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=t50GiurGtfndq9Z7QizhI6Xxfi++e6lpRkxpXhDTEh8=; b=WguBgc95XcECE+sPUj6kGcTMpPRPekA+uYTiidVHoTDPw7jEYWJ6n8juAm3aHLT/nu YWMKyvg6y4w1hjNOBlP2SI019quAthycm/g9hJGTsOLPPY6PIull9qB8+CW21HgBoozr G9XfzhC4DxSyZiCcJGmQs71AXKQ79gfsrU33+N6Unc7EA8UOLe1kEPhyPs1B2FQZaIWZ 4XvTN3UZLjMDTsQREUzbA9g088x+NGN+NUqvXUxjA4uDyp7IrX+9Uwtq3jY62YNmyoB8 Fof/Xwt+iASSP43vzHf2vquJB8aw+4cXzN6/SeIbcey0RHYfD6FTHlZFnIF2m2e/5ThG dXsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SnGBzfM3; 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 v14si378421ejw.436.2019.10.03.10.08.58; Thu, 03 Oct 2019 10:09:22 -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=SnGBzfM3; 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 S2404441AbfJCQgg (ORCPT + 99 others); Thu, 3 Oct 2019 12:36:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:45758 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403786AbfJCQga (ORCPT ); Thu, 3 Oct 2019 12:36:30 -0400 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 7DF282070B; Thu, 3 Oct 2019 16:36:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570120589; bh=7O4GyeaIeGFExRzrSwzaWAeA3pgTN+zw+yySvrhX3+s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SnGBzfM340qYwbpCrBwL/pNJ2GbVcTbg0fvUQ9s5+TUG4z3fjkoW8r3EQwinBLr4y rCHpx7iL9b9g85f433769G+go1Y5FqCR/eX0Cd2Y2U1A8F4eDw42yfEu6l4hwzZ6ug zPKellf4PFU8QSMgNik3pOwUrdruH0CKbthCxNWA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christoph Hellwig , Keith Busch , Bart Van Assche , Ming Lei , Bob Liu , Yufen Yu , Jens Axboe Subject: [PATCH 5.2 277/313] block: fix null pointer dereference in blk_mq_rq_timed_out() Date: Thu, 3 Oct 2019 17:54:15 +0200 Message-Id: <20191003154600.332260425@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191003154533.590915454@linuxfoundation.org> References: <20191003154533.590915454@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yufen Yu commit 8d6996630c03d7ceeabe2611378fea5ca1c3f1b3 upstream. We got a null pointer deference BUG_ON in blk_mq_rq_timed_out() as following: [ 108.825472] BUG: kernel NULL pointer dereference, address: 0000000000000040 [ 108.827059] PGD 0 P4D 0 [ 108.827313] Oops: 0000 [#1] SMP PTI [ 108.827657] CPU: 6 PID: 198 Comm: kworker/6:1H Not tainted 5.3.0-rc8+ #431 [ 108.829503] Workqueue: kblockd blk_mq_timeout_work [ 108.829913] RIP: 0010:blk_mq_check_expired+0x258/0x330 [ 108.838191] Call Trace: [ 108.838406] bt_iter+0x74/0x80 [ 108.838665] blk_mq_queue_tag_busy_iter+0x204/0x450 [ 108.839074] ? __switch_to_asm+0x34/0x70 [ 108.839405] ? blk_mq_stop_hw_queue+0x40/0x40 [ 108.839823] ? blk_mq_stop_hw_queue+0x40/0x40 [ 108.840273] ? syscall_return_via_sysret+0xf/0x7f [ 108.840732] blk_mq_timeout_work+0x74/0x200 [ 108.841151] process_one_work+0x297/0x680 [ 108.841550] worker_thread+0x29c/0x6f0 [ 108.841926] ? rescuer_thread+0x580/0x580 [ 108.842344] kthread+0x16a/0x1a0 [ 108.842666] ? kthread_flush_work+0x170/0x170 [ 108.843100] ret_from_fork+0x35/0x40 The bug is caused by the race between timeout handle and completion for flush request. When timeout handle function blk_mq_rq_timed_out() try to read 'req->q->mq_ops', the 'req' have completed and reinitiated by next flush request, which would call blk_rq_init() to clear 'req' as 0. After commit 12f5b93145 ("blk-mq: Remove generation seqeunce"), normal requests lifetime are protected by refcount. Until 'rq->ref' drop to zero, the request can really be free. Thus, these requests cannot been reused before timeout handle finish. However, flush request has defined .end_io and rq->end_io() is still called even if 'rq->ref' doesn't drop to zero. After that, the 'flush_rq' can be reused by the next flush request handle, resulting in null pointer deference BUG ON. We fix this problem by covering flush request with 'rq->ref'. If the refcount is not zero, flush_end_io() return and wait the last holder recall it. To record the request status, we add a new entry 'rq_status', which will be used in flush_end_io(). Cc: Christoph Hellwig Cc: Keith Busch Cc: Bart Van Assche Cc: stable@vger.kernel.org # v4.18+ Reviewed-by: Ming Lei Reviewed-by: Bob Liu Signed-off-by: Yufen Yu Signed-off-by: Greg Kroah-Hartman ------- v2: - move rq_status from struct request to struct blk_flush_queue v3: - remove unnecessary '{}' pair. v4: - let spinlock to protect 'fq->rq_status' v5: - move rq_status after flush_running_idx member of struct blk_flush_queue Signed-off-by: Jens Axboe --- block/blk-flush.c | 10 ++++++++++ block/blk-mq.c | 5 ++++- block/blk.h | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) --- a/block/blk-flush.c +++ b/block/blk-flush.c @@ -214,6 +214,16 @@ static void flush_end_io(struct request /* release the tag's ownership to the req cloned from */ spin_lock_irqsave(&fq->mq_flush_lock, flags); + + if (!refcount_dec_and_test(&flush_rq->ref)) { + fq->rq_status = error; + spin_unlock_irqrestore(&fq->mq_flush_lock, flags); + return; + } + + if (fq->rq_status != BLK_STS_OK) + error = fq->rq_status; + hctx = flush_rq->mq_hctx; if (!q->elevator) { blk_mq_tag_set_rq(hctx, flush_rq->tag, fq->orig_rq); --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -910,7 +910,10 @@ static bool blk_mq_check_expired(struct */ if (blk_mq_req_expired(rq, next)) blk_mq_rq_timed_out(rq, reserved); - if (refcount_dec_and_test(&rq->ref)) + + if (is_flush_rq(rq, hctx)) + rq->end_io(rq, 0); + else if (refcount_dec_and_test(&rq->ref)) __blk_mq_free_request(rq); return true; --- a/block/blk.h +++ b/block/blk.h @@ -19,6 +19,7 @@ struct blk_flush_queue { unsigned int flush_queue_delayed:1; unsigned int flush_pending_idx:1; unsigned int flush_running_idx:1; + blk_status_t rq_status; unsigned long flush_pending_since; struct list_head flush_queue[2]; struct list_head flush_data_in_flight; @@ -47,6 +48,12 @@ static inline void __blk_get_queue(struc kobject_get(&q->kobj); } +static inline bool +is_flush_rq(struct request *req, struct blk_mq_hw_ctx *hctx) +{ + return hctx->fq->flush_rq == req; +} + struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q, int node, int cmd_size, gfp_t flags); void blk_free_flush_queue(struct blk_flush_queue *q);