Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8015383ybi; Tue, 23 Jul 2019 01:18:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqwstpJKlenJ2QYTLQoqsi8D2HfhoaA7+7NkKe4uEDNJ2Tu8vJUfXm/MzzqQmKl2+WZAcvgP X-Received: by 2002:a17:90a:b00b:: with SMTP id x11mr81720109pjq.120.1563869897561; Tue, 23 Jul 2019 01:18:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563869897; cv=none; d=google.com; s=arc-20160816; b=zegzA/A53b/go+OPknxDuAseBZY65nLt/w/ISXRsdTU1DdwdNwUtp5ToBoN49owW7F O/hvg/UP/UfON1ReqSUTzMdOMY9yHkx7v5LNCe2gedmytkVDJOFgn80SmHVLYMiPCc4r FgtMlC5WU2cU5S8RqBKeJZOvOhLAipAIwMXAqZQL8ywZs0m9LonVXGtK9DmFd3MBKuIW z8Y6dOc6niEW6YYams5coTeHBCGlL5CgFxlwKtYK8G2AtSyPCOoeRi7SABUuT1hxP/8k QAFlcpylMbNu16FikA/uVDNhr192mPtw0C74ssoU0/7AkGoaNehqZswcBhIPLUlhxEHW VZ0Q== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=egqqW/mZD3nk8a5ohX29bdMEufJeiEnpm1IdWpCZRK0=; b=J0EVNNn9cz8S5oLp5aqLMZxFDaD0nLWqniYhhADDnctrYxY1mmd/rUUhjTJFswT4Gr 4JNJhF3izAzT3Uwt8eijxAIOAAikkkVPW0w8j+t+gOmRWCzf7jm1lNSJMS4rtHgDH4tw SLDxSZXb+TbttLojLh/6tJD476MavY0x9QYVl+HKzpzOd8GiRNljUEybVHOh0fysv4Cr y2oHMXcI5m8YzjBBWjZs/nIU3oRoKIcMP/jtm2eeDVX3tW640JEuerle2SKoiiRooVgb lvat/yanegkdQbXtbzi5NAVzbgtYdoJy+15jyRmZ1ytr7YE8LBnv1FLyRpxzwuTMSZyZ 1TOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=JTKVwsYP; 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=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t185si1959871pgb.329.2019.07.23.01.18.00; Tue, 23 Jul 2019 01:18:17 -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=@oracle.com header.s=corp-2018-07-02 header.b=JTKVwsYP; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730211AbfGVXgw (ORCPT + 99 others); Mon, 22 Jul 2019 19:36:52 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:51568 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727157AbfGVXgw (ORCPT ); Mon, 22 Jul 2019 19:36:52 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x6MNXqjV001985; Mon, 22 Jul 2019 23:36:25 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : to : cc : references : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=egqqW/mZD3nk8a5ohX29bdMEufJeiEnpm1IdWpCZRK0=; b=JTKVwsYPWPsu3AU08JPRXmOo/VtI5VqrdAtfGHg9VkamStKFb3mdvMwn4SkSdRQt6Um9 w/ahqK9ioeMad5i4KNM9oaUIttmsdZ8ZynR9NnVzp7WZbgmvDoJlTs+MwH0kcGthzE+D gfgQewbXzP5bSVzNyuV3CYuxm8O+PQU9vWqp+c2slPWvJS2W1FqZwDisHWIFcxCtn+Q8 GDbsSeV04Iz/k3k6RV+gUjg0ttX8x1/EzePJ0SYK5VEiKtZsdlSxB+tw7gmVDrSEycxU TMp4qotn+NZaQ0amJKm79LrCTkXeARDGkdlkxEyndOIHbclT2WNq6dCW+UmpjkVlN+9l hw== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by aserp2120.oracle.com with ESMTP id 2tutwpadq8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 22 Jul 2019 23:36:25 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x6MNWYeR080720; Mon, 22 Jul 2019 23:36:24 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userp3030.oracle.com with ESMTP id 2tur2u2x3d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 22 Jul 2019 23:36:24 +0000 Received: from abhmp0017.oracle.com (abhmp0017.oracle.com [141.146.116.23]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x6MNaMkC003855; Mon, 22 Jul 2019 23:36:22 GMT Received: from [192.168.1.14] (/180.165.87.209) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 22 Jul 2019 16:36:22 -0700 Subject: Re: [PATCH] bfq: Check if bfqq is NULL in bfq_insert_request To: Guenter Roeck , Paolo Valente , Jens Axboe Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Hsin-Yi Wang , Nicolas Boichat , Doug Anderson References: <1563816648-12057-1-git-send-email-linux@roeck-us.net> <20190722202920.GA18431@roeck-us.net> From: Bob Liu Message-ID: Date: Tue, 23 Jul 2019 07:36:16 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: <20190722202920.GA18431@roeck-us.net> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9326 signatures=668684 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1907220249 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9326 signatures=668684 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1907220249 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 7/23/19 4:29 AM, Guenter Roeck wrote: > On Mon, Jul 22, 2019 at 10:30:48AM -0700, Guenter Roeck wrote: >> In bfq_insert_request(), bfqq is initialized with: >> bfqq = bfq_init_rq(rq); >> In bfq_init_rq(), we find: >> if (unlikely(!rq->elv.icq)) >> return NULL; >> Indeed, rq->elv.icq can be NULL if the memory allocation in >> create_task_io_context() failed. >> > > The above function should have been ioc_create_icq(), sorry. > > Guenter > >> A comment in bfq_insert_request() suggests that bfqq is supposed to be >> non-NULL if 'at_head || blk_rq_is_passthrough(rq)' is false. Yet, as >> debugging and practical experience shows, this is not the case in the >> above situation. >> >> This results in the following crash. >> >> Unable to handle kernel NULL pointer dereference >> at virtual address 00000000000001b0 >> ... >> Call trace: >> bfq_setup_cooperator+0x44/0x134 >> bfq_insert_requests+0x10c/0x630 >> blk_mq_sched_insert_requests+0x60/0xb4 >> blk_mq_flush_plug_list+0x290/0x2d4 >> blk_flush_plug_list+0xe0/0x230 >> blk_finish_plug+0x30/0x40 >> generic_writepages+0x60/0x94 >> blkdev_writepages+0x24/0x30 >> do_writepages+0x74/0xac >> __filemap_fdatawrite_range+0x94/0xc8 >> file_write_and_wait_range+0x44/0xa0 >> blkdev_fsync+0x38/0x68 >> vfs_fsync_range+0x68/0x80 >> do_fsync+0x44/0x80 >> >> The problem is relatively easy to reproduce by running an image with >> failslab enabled, such as with: >> >> cd /sys/kernel/debug/failslab >> echo 10 > probability >> echo 300 > times >> >> Avoid the problem by checking if bfqq is NULL before using it. With the >> NULL check in place, requests with missing io context are queued >> immediately, and the crash is no longer seen. >> What about other place use blk_init_rq()? E.g in bfq_request_merged(): 1897 struct bfq_queue *bfqq = bfq_init_rq(req); 1898 struct bfq_data *bfqd = bfqq->bfqd; Which may have same Null-pointer bug? -Bob >> Fixes: 18e5a57d79878 ("block, bfq: postpone rq preparation to insert or merge") >> Reported-by: Hsin-Yi Wang >> Cc: Hsin-Yi Wang >> Cc: Nicolas Boichat >> Cc: Doug Anderson >> Signed-off-by: Guenter Roeck >> --- >> block/bfq-iosched.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c >> index 72860325245a..56f3f4227010 100644 >> --- a/block/bfq-iosched.c >> +++ b/block/bfq-iosched.c >> @@ -5417,7 +5417,7 @@ static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, >> >> spin_lock_irq(&bfqd->lock); >> bfqq = bfq_init_rq(rq); >> - if (at_head || blk_rq_is_passthrough(rq)) { >> + if (!bfqq || at_head || blk_rq_is_passthrough(rq)) { >> if (at_head) >> list_add(&rq->queuelist, &bfqd->dispatch); >> else >> -- >> 2.7.4 >>