From patchwork Fri Sep 27 06:36:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Baokun Li X-Patchwork-Id: 1990143 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=ozlabs.org (client-ip=2404:9400:2221:ea00::3; helo=mail.ozlabs.org; envelope-from=srs0=tuks=qz=vger.kernel.org=linux-ext4+bounces-4353-patchwork-incoming=ozlabs.org@ozlabs.org; receiver=patchwork.ozlabs.org) Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XFLRh5hfJz1xst for ; Fri, 27 Sep 2024 16:39:44 +1000 (AEST) Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4XFLRc3dMSz4xRB for ; Fri, 27 Sep 2024 16:39:40 +1000 (AEST) Received: by gandalf.ozlabs.org (Postfix) id 4XFLRc3b47z4xRj; Fri, 27 Sep 2024 16:39:40 +1000 (AEST) Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: gandalf.ozlabs.org; arc=pass smtp.remote-ip="2604:1380:40f1:3f00::1" arc.chain=subspace.kernel.org ARC-Seal: i=2; a=rsa-sha256; d=ozlabs.org; s=201707; t=1727419180; cv=pass; b=udpOXqxt8RnR48LQJqmdR6fthLOVCFDNltxQhfTTt2gA3MQHH56xOAqXbefeB4e0j3bgrcc0NN9kKyRcKPb29Uo4lJbov0ZVzBDi0veRMHFNnjtxS8dTzIj/MKFVoq5UNcGeglfbQgcpsS6ckcivafsv8oOw3smsIQ0cAud6vmHt/6asowm5wkU9bJY9EACPBtBAZEm0WFUfkAbg/0wDbbG5Q7CsE9eD4PywMJKRJ19bY1HeR6XBSuEyDmkDGc+Xt/OHziusM2NKACiehsFY7f3IIKgZ+9h2MZ17yu3noiHNa2E5AoH2aGTHv7SpzlMC93P/3F6iq4wk8Q/LWDJ6tw== ARC-Message-Signature: i=2; a=rsa-sha256; d=ozlabs.org; s=201707; t=1727419180; c=relaxed/relaxed; bh=d5AjJC7S7oLHaRBa/Gd3EonMTbKRVN4w1UUepkzuEFI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=DjOV+vMg8nrhI/8JgZO3fvsk0jtYcb/O7A5yjrAXUXixKm0FMhulSuWy2xe2yh5CxtpcLvbrSUiNskk2wVMfc242Ig1fM78Nh9TKDTlPle11vPBYkCnuF4s2Gcl6+Otsbvitwm6DbJe1wJbyOXLSIOuVmS1slixysFbulhh7liScKZTWqp92yrpFTTSMk00VqKol06CI72DSE311XJBlQ2OZDxV3WtKlIH+i200JFhPRTupPQdZVLXkuiRnGJrI8iiK0jGdMS/kvwfzQeLbQktYfKA8lrWNES67PrWqWYDAd8IkH41WhDos4ajLfmTATavcqH7znb+8hxqntTDmA9g== ARC-Authentication-Results: i=2; gandalf.ozlabs.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass (client-ip=2604:1380:40f1:3f00::1; helo=sy.mirrors.kernel.org; envelope-from=linux-ext4+bounces-4353-patchwork-incoming=ozlabs.org@vger.kernel.org; receiver=ozlabs.org) smtp.mailfrom=vger.kernel.org Authentication-Results: gandalf.ozlabs.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: gandalf.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:40f1:3f00::1; helo=sy.mirrors.kernel.org; envelope-from=linux-ext4+bounces-4353-patchwork-incoming=ozlabs.org@vger.kernel.org; receiver=ozlabs.org) Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org [IPv6:2604:1380:40f1:3f00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by gandalf.ozlabs.org (Postfix) with ESMTPS id 4XFLRc3SK0z4xRB for ; Fri, 27 Sep 2024 16:39:40 +1000 (AEST) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 4F70FB23C96 for ; Fri, 27 Sep 2024 06:39:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0BD45176ACB; Fri, 27 Sep 2024 06:39:33 +0000 (UTC) X-Original-To: linux-ext4@vger.kernel.org Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39F98158557; Fri, 27 Sep 2024 06:39:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727419172; cv=none; b=Ux2Ctv4Nrr7VgD0HWxtElTdsT8Z3GGOzbZEDDHztx56+s3AbwN4X2Wt2Tj5So1CbzSaLHOzcoWl9BOgJ8rv7uCqZcX/S9liyOIENh319DC9LjuFG5fDrGPbi5xhuJWV9SPhK2PWrznMGzNHZBmzu5mbaTuGwbqEGsxmDT1zpaHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727419172; c=relaxed/simple; bh=uiMMQvxopeaDfVxhERXqaMFelDtteEisin7Ss13WtfA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=Fr9oIEZIVuBwCOkEvTPJLoyP18sZcW3DW0OsDFBkwpkW7O9ECm98otN5jl+DlmoLileN5ocqE26vqPdpKCLmZook3xGe4w1XwgxglKhXHPAK4NVXLDVrs50AG1hyCznE3Oh2j3AAI8r+oMX8edeHPjMePulgsIS3q+iZfz9FdCs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4XFLR06Y4Bz4f3jXP; Fri, 27 Sep 2024 14:39:08 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 3A6F31A018D; Fri, 27 Sep 2024 14:39:25 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgDHR8QWU_ZmwM_lCQ--.36079S4; Fri, 27 Sep 2024 14:39:20 +0800 (CST) From: libaokun@huaweicloud.com To: linux-ext4@vger.kernel.org Cc: tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, yangerkun@huawei.com, libaokun@huaweicloud.com, Baokun Li , Wesley Hershberger , =?utf-8?q?St=C3=A9ph?= =?utf-8?q?ane_Graber?= , Alexander Mikhalitsyn , Eric Sandeen , stable@vger.kernel.org Subject: [PATCH] ext4: fix off by one issue in alloc_flex_gd() Date: Fri, 27 Sep 2024 14:36:20 +0800 Message-Id: <20240927063620.2630898-1-libaokun@huaweicloud.com> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-ext4@vger.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r0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267 AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbknY7UUUUU== X-CM-SenderInfo: 5olet0hnxqqx5xdzvxpfor3voofrz/1tbiAgAPBWbv1ZsktAABse From: Baokun Li Wesley reported an issue: ================================================================== EXT4-fs (dm-5): resizing filesystem from 7168 to 786432 blocks ------------[ cut here ]------------ kernel BUG at fs/ext4/resize.c:324! CPU: 9 UID: 0 PID: 3576 Comm: resize2fs Not tainted 6.11.0+ #27 RIP: 0010:ext4_resize_fs+0x1212/0x12d0 Call Trace: __ext4_ioctl+0x4e0/0x1800 ext4_ioctl+0x12/0x20 __x64_sys_ioctl+0x99/0xd0 x64_sys_call+0x1206/0x20d0 do_syscall_64+0x72/0x110 entry_SYSCALL_64_after_hwframe+0x76/0x7e ================================================================== While reviewing the patch, Honza found that when adjusting resize_bg in alloc_flex_gd(), it was possible for flex_gd->resize_bg to be bigger than flexbg_size. The reproduction of the problem requires the following: o_group = flexbg_size * 2 * n; o_size = (o_group + 1) * group_size; n_group: [o_group + flexbg_size, o_group + flexbg_size * 2) o_size = (n_group + 1) * group_size; Take n=0,flexbg_size=16 as an example: last:15 |o---------------|--------------n-| o_group:0 resize to n_group:30 The corresponding reproducer is: img=test.img truncate -s 600M $img mkfs.ext4 -F $img -b 1024 -G 16 8M dev=`losetup -f --show $img` mkdir -p /tmp/test mount $dev /tmp/test resize2fs $dev 248M Delete the problematic plus 1 to fix the issue, and add a WARN_ON_ONCE() to prevent the issue from happening again. Reported-by: Wesley Hershberger Closes: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/2081231 Reported-by: Stéphane Graber Closes: https://lore.kernel.org/all/20240925143325.518508-1-aleksandr.mikhalitsyn@canonical.com/ Tested-by: Alexander Mikhalitsyn Tested-by: Eric Sandeen Fixes: 665d3e0af4d3 ("ext4: reduce unnecessary memory allocation in alloc_flex_gd()") Cc: stable@vger.kernel.org Signed-off-by: Baokun Li Reviewed-by: Jan Kara --- fs/ext4/resize.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c index e04eb08b9060..397970121d43 100644 --- a/fs/ext4/resize.c +++ b/fs/ext4/resize.c @@ -253,9 +253,9 @@ static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned int flexbg_size, /* Avoid allocating large 'groups' array if not needed */ last_group = o_group | (flex_gd->resize_bg - 1); if (n_group <= last_group) - flex_gd->resize_bg = 1 << fls(n_group - o_group + 1); + flex_gd->resize_bg = 1 << fls(n_group - o_group); else if (n_group - last_group < flex_gd->resize_bg) - flex_gd->resize_bg = 1 << max(fls(last_group - o_group + 1), + flex_gd->resize_bg = 1 << max(fls(last_group - o_group), fls(n_group - last_group)); flex_gd->groups = kmalloc_array(flex_gd->resize_bg,