Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6267476imu; Mon, 21 Jan 2019 06:07:05 -0800 (PST) X-Google-Smtp-Source: ALg8bN4IAY7Jxrxj8BKUmpQU2hqSkW3YaZcc5RbbRqX7gqUytQywLyiWCvKIa8T2hAB33DbejEk6 X-Received: by 2002:a62:1f9d:: with SMTP id l29mr29820028pfj.14.1548079625873; Mon, 21 Jan 2019 06:07:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548079625; cv=none; d=google.com; s=arc-20160816; b=XDnjJRvzSxy92M8vK/uvyE71h38XIXZ0Fhi8xAvboxuQeVr3I8nL9KfPLLk0wwhGjR /rbWPd8DP7os16dGlBncGl25jJeykP9lkEggsKCqUDsPIXbEtMSvTt5COomH31o2TnjY 1EFWlNMPqFR2F1W4GDYp0xWS8MOrDBQ2h0vXsROEyj80lsaIwbi78gEUTbM1zeBODGZM x40sj60KknbPZuwenId8J0oXYY8ssNVjgNdb46qB7gRvHeKwhKJ3Cz99w6T7AFI9g4Vf MDrygecvMmVcZ04bghO6lHX+4dpE/AWmdXq8upse68Wr4IeI9bJnuawXaBCC01viMlnL uGoA== 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=69VhNdjic7Hepl+I9E9LDa9FCg81+Tn28mozkUDkGJQ=; b=Gzi4VJeLyZUwUKtqYASx3WP6xuVpVYZ445bgviUAz29n9ge127ucAZhmkzafhZGtf1 1uzAy+8OH9sOIgYziCxHmUVb8epH7vA7N50eiQqQ7+D8npt/ihYOZZ+qGiQbT1zSrANg VVkjGRTNm6Smqc4GnHuvWiW4RzzP4tav5UiuMDb21nUbRgoTntW3SLTE6DQz3RUlVSr7 NW6U5J2ULHHUTfYElh/wtLX0N/s9q/Gw8QEGX1AFs3fM0rpcnzC+J3dzg0uR4QPP6OnM nb5aT+rzk3iw99cLIsYOLxV7M6DsHGYsHihNbY+YO7G93TIVmQxySJGaAfGCVKfmNbrd WaKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MyDVR0HK; 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 m3si12863628pld.331.2019.01.21.06.06.50; Mon, 21 Jan 2019 06:07:05 -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=MyDVR0HK; 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 S1732185AbfAUOBc (ORCPT + 99 others); Mon, 21 Jan 2019 09:01:32 -0500 Received: from mail.kernel.org ([198.145.29.99]:48594 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729579AbfAUOBb (ORCPT ); Mon, 21 Jan 2019 09:01:31 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 D1C8B21019; Mon, 21 Jan 2019 14:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548079290; bh=Ipa1bCBlV1JZQqjfHk5wS3yVDDxUYDSzqVlzbf88n4g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MyDVR0HKS1KpfEtmDLYxeXIs1TFpPCYdk4Q/49qO1oI3/wmLeYLNIvZii5XON6Czi OCtQj3rm5qh+Dk90y+zE4JXrk/eGsjxj6ElAGkQX4URjpuwnS6iP9ezPvBhpUBNYnD 6zhA906K2VMkI8RskK01tSidO2Jj8hn6MtqipJ5c= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+9933e4476f365f5d5a1b@syzkaller.appspotmail.com, Jan Kara , Jens Axboe Subject: [PATCH 4.19 72/99] blockdev: Fix livelocks on loop device Date: Mon, 21 Jan 2019 14:49:04 +0100 Message-Id: <20190121134916.736211742@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190121134913.924726465@linuxfoundation.org> References: <20190121134913.924726465@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jan Kara commit 04906b2f542c23626b0ef6219b808406f8dddbe9 upstream. bd_set_size() updates also block device's block size. This is somewhat unexpected from its name and at this point, only blkdev_open() uses this functionality. Furthermore, this can result in changing block size under a filesystem mounted on a loop device which leads to livelocks inside __getblk_gfp() like: Sending NMI from CPU 0 to CPUs 1: NMI backtrace for cpu 1 CPU: 1 PID: 10863 Comm: syz-executor0 Not tainted 4.18.0-rc5+ #151 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 RIP: 0010:__sanitizer_cov_trace_pc+0x3f/0x50 kernel/kcov.c:106 ... Call Trace: init_page_buffers+0x3e2/0x530 fs/buffer.c:904 grow_dev_page fs/buffer.c:947 [inline] grow_buffers fs/buffer.c:1009 [inline] __getblk_slow fs/buffer.c:1036 [inline] __getblk_gfp+0x906/0xb10 fs/buffer.c:1313 __bread_gfp+0x2d/0x310 fs/buffer.c:1347 sb_bread include/linux/buffer_head.h:307 [inline] fat12_ent_bread+0x14e/0x3d0 fs/fat/fatent.c:75 fat_ent_read_block fs/fat/fatent.c:441 [inline] fat_alloc_clusters+0x8ce/0x16e0 fs/fat/fatent.c:489 fat_add_cluster+0x7a/0x150 fs/fat/inode.c:101 __fat_get_block fs/fat/inode.c:148 [inline] ... Trivial reproducer for the problem looks like: truncate -s 1G /tmp/image losetup /dev/loop0 /tmp/image mkfs.ext4 -b 1024 /dev/loop0 mount -t ext4 /dev/loop0 /mnt losetup -c /dev/loop0 l /mnt Fix the problem by moving initialization of a block device block size into a separate function and call it when needed. Thanks to Tetsuo Handa for help with debugging the problem. Reported-by: syzbot+9933e4476f365f5d5a1b@syzkaller.appspotmail.com Signed-off-by: Jan Kara Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- fs/block_dev.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -104,6 +104,20 @@ void invalidate_bdev(struct block_device } EXPORT_SYMBOL(invalidate_bdev); +static void set_init_blocksize(struct block_device *bdev) +{ + unsigned bsize = bdev_logical_block_size(bdev); + loff_t size = i_size_read(bdev->bd_inode); + + while (bsize < PAGE_SIZE) { + if (size & bsize) + break; + bsize <<= 1; + } + bdev->bd_block_size = bsize; + bdev->bd_inode->i_blkbits = blksize_bits(bsize); +} + int set_blocksize(struct block_device *bdev, int size) { /* Size must be a power of two, and between 512 and PAGE_SIZE */ @@ -1408,18 +1422,9 @@ EXPORT_SYMBOL(check_disk_change); void bd_set_size(struct block_device *bdev, loff_t size) { - unsigned bsize = bdev_logical_block_size(bdev); - inode_lock(bdev->bd_inode); i_size_write(bdev->bd_inode, size); inode_unlock(bdev->bd_inode); - while (bsize < PAGE_SIZE) { - if (size & bsize) - break; - bsize <<= 1; - } - bdev->bd_block_size = bsize; - bdev->bd_inode->i_blkbits = blksize_bits(bsize); } EXPORT_SYMBOL(bd_set_size); @@ -1496,8 +1501,10 @@ static int __blkdev_get(struct block_dev } } - if (!ret) + if (!ret) { bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); + set_init_blocksize(bdev); + } /* * If the device is invalidated, rescan partition @@ -1532,6 +1539,7 @@ static int __blkdev_get(struct block_dev goto out_clear; } bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9); + set_init_blocksize(bdev); } if (bdev->bd_bdi == &noop_backing_dev_info)