Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp3185837lqo; Wed, 15 May 2024 02:13:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUW3H+fhdS4weXtkSlwK3WsyuBrp7+EfgUv58qjscMRR1fpQ2++jExriE+Gen/Pz2LpEnL9bVYvpeFoue9mFQsVTNWT3VhFyWoIrq0nVA== X-Google-Smtp-Source: AGHT+IHstjqcErW+QHAiLqjeTD3XuNm11k7uH2jh9NwB1+WKB3ANuytCrunXBb83nX//77ogMNoI X-Received: by 2002:a17:907:86a8:b0:a59:a282:5db7 with SMTP id a640c23a62f3a-a5a2d67e024mr1219812266b.65.1715764405525; Wed, 15 May 2024 02:13:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715764405; cv=pass; d=google.com; s=arc-20160816; b=ux7jawLhah6M7Cq2zV5PcXaz/3rKgRc44G4310laKgwXueRLZgT1SlLXRXOtoo203B tqlibW+gQFNd05DOntiI9fGoZrizHUbZknFzR7BsEP1h5cFZGr2pQcL6I/AthMPcA1Fh A8rplsZSp7HHHKBOJSYt++IW9oR+x7HGo8c5g9M7/TuCK4yPTWmoIOdpFhnthsFuT/VG oEMg8OxOB0Q2OTiGAxXZL7d4qI+7TMoxdCtZq4Z4q9hIzUtAkm4grtf3keU3DkYjFfIo rLVv7w7k0nOoZf9lZtUWHxmNzr3r0gT/aPoeVNtEedGntxRnLhxssWL2SxhWQS0+qtAX 2XLA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :message-id:date:references:in-reply-to:subject:cc:to:from :dkim-signature; bh=zL4qr8VhiZbn65nWQOPKdeChi7hbUaBPNiog1Y1OVjM=; fh=4c63iedcdAfstJ128KKNTkxClp9RLr61/BnmBy+Iwks=; b=LDUD5bjf/fyVwZGVIDsReM+s7D/W3EJAxmcgQQlZSDwDJBVkoBjbG8MIptZDN1/SRg 3UCm0bK3lZYuAZjdph/DSInekA50k/lSyr8pqUJG+v7yxzgUvvJZXXbDjoXx3+gWLNTA YmuHj4LEoybntC4W8onOBPW+2lokLF5ug3V5Nj9dIyZcfPWNhX31NwO45BGvMQfK7PR4 rsLJDtkDKu4IFjv5dkMRdmVlCYZ1A7S+lcqjGMbSM1F3hyp9wRjl33/wTEwd9M4kt0VD dwJkeM0caoWwFtSGDXxONZQWbnsYA8Crr+2U/chghUboEH+MgZQWd7uKKOOSAizcAsaY kxeA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=DQHFLjvz; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-179692-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179692-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17ba3b22si766210966b.520.2024.05.15.02.13.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 02:13:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179692-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=DQHFLjvz; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-179692-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179692-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 1BC811F228B2 for ; Wed, 15 May 2024 09:13:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0409A57CA1; Wed, 15 May 2024 09:13:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="DQHFLjvz" Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) (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 23DCC56458 for ; Wed, 15 May 2024 09:13:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715764397; cv=none; b=jlKPJvXAd21G7bVq3qG6lwgf/bm/8//JW0FjXMbVQXxYZ/voVu6q2ebCfrojdEtL/bVKUoOvj4ZVvmKhC+H2eKQSpwbH0uqOOObE/eI9a0HQAEFPhrK8rB4ksPWK+PoS8F5YtHSiH27CGVazP/svakJs/aEe6E13Q0at/Y7kqjY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715764397; c=relaxed/simple; bh=sX4zTxtZoxVRcgDVDf6LPuGX1WeRgHeCta+TeRvTYxM=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=PH7k2i81sDjishD0pci8by3kvuhZsKXwEf+njx+4RNhwnqc9vqJMr6nqzHO9t2+IZfaPZJNiVgujpOw2EC0vDIggNC5G6nOzDlsyaI7cD79DrmoNmLuvjS49Ts3F5pKD5OczN82E1s1ej6thdTA8nvbXWI2fwU9Iq4YHMPmrdOI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=DQHFLjvz; arc=none smtp.client-ip=95.215.58.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715764392; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=zL4qr8VhiZbn65nWQOPKdeChi7hbUaBPNiog1Y1OVjM=; b=DQHFLjvzmB8eAtjGkVbT3o2RLeNlAV1VS08LhcOoBVJniztgiaEGAOMpwFtSwiQQHs9TCZ +rlFaiQrU2avTpkMAzgD3tz/DDElasfH3ymFQnZrZ5cniRtHhsqJR84sO6BTXG6/KnWxGS ET8QLGOxHL9zBccLkzC/3gVwM6WaaKE= From: Luis Henriques To: Zhang Yi Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, Theodore Ts'o , Andreas Dilger , Harshad Shirwadkar Subject: Re: [PATCH] ext4: fix infinite loop when replaying fast_commit In-Reply-To: <326db1c7-1064-d19c-0028-d2149c61f6f5@huaweicloud.com> (Zhang Yi's message of "Wed, 15 May 2024 16:52:54 +0800") References: <20240510115252.11850-1-luis.henriques@linux.dev> <2ee78957-b0a6-f346-5957-c4b2ebcea4ce@huaweicloud.com> <87o798a6k5.fsf@brahms.olymp> <87pltniimq.fsf@brahms.olymp> <326db1c7-1064-d19c-0028-d2149c61f6f5@huaweicloud.com> Date: Wed, 15 May 2024 10:13:06 +0100 Message-ID: <87bk57phel.fsf@brahms.olymp> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain X-Migadu-Flow: FLOW_OUT On Wed 15 May 2024 04:52:54 PM +08, Zhang Yi wrote; > On 2024/5/15 16:28, Luis Henriques wrote: >> On Wed 15 May 2024 12:59:26 PM +08, Zhang Yi wrote; >> >>> On 2024/5/14 21:04, Luis Henriques wrote: >>>> On Sat 11 May 2024 02:24:17 PM +08, Zhang Yi wrote; >>>> >>>>> On 2024/5/10 19:52, Luis Henriques (SUSE) wrote: >>>>>> When doing fast_commit replay an infinite loop may occur due to an >>>>>> uninitialized extent_status struct. ext4_ext_determine_insert_hole() does >>>>>> not detect the replay and calls ext4_es_find_extent_range(), which will >>>>>> return immediately without initializing the 'es' variable. >>>>>> >>>>>> Because 'es' contains garbage, an integer overflow may happen causing an >>>>>> infinite loop in this function, easily reproducible using fstest generic/039. >>>>>> >>>>>> This commit fixes this issue by detecting the replay in function >>>>>> ext4_ext_determine_insert_hole(). It also adds initialization code to the >>>>>> error path in function ext4_es_find_extent_range(). >>>>>> >>>>>> Thanks to Zhang Yi, for figuring out the real problem! >>>>>> >>>>>> Fixes: 8016e29f4362 ("ext4: fast commit recovery path") >>>>>> Signed-off-by: Luis Henriques (SUSE) >>>>>> --- >>>>>> Hi! >>>>>> >>>>>> Two comments: >>>>>> 1) The change in ext4_ext_map_blocks() could probably use the min_not_zero >>>>>> macro instead. I decided not to do so simply because I wasn't sure if >>>>>> that would be safe, but I'm fine changing that if you think it is. >>>>>> >>>>>> 2) I thought about returning 'EXT_MAX_BLOCKS' instead of '0' in >>>>>> ext4_lblk_t ext4_ext_determine_insert_hole(), which would then avoid >>>>>> the extra change to ext4_ext_map_blocks(). '0' sounds like the right >>>>>> value to return, but I'm also OK using 'EXT_MAX_BLOCKS' instead. >>>>>> >>>>>> And again thanks to Zhang Yi for pointing me the *real* problem! >>>>>> >>>>>> fs/ext4/extents.c | 6 +++++- >>>>>> fs/ext4/extents_status.c | 5 ++++- >>>>>> 2 files changed, 9 insertions(+), 2 deletions(-) >>>>>> >>>>>> diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c >>>>>> index e57054bdc5fd..b5bfcb6c18a0 100644 >>>>>> --- a/fs/ext4/extents.c >>>>>> +++ b/fs/ext4/extents.c >>>>>> @@ -4052,6 +4052,9 @@ static ext4_lblk_t ext4_ext_determine_insert_hole(struct inode *inode, >>>>>> ext4_lblk_t hole_start, len; >>>>>> struct extent_status es; >>>>>> >>>>>> + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) >>>>>> + return 0; >>>>>> + >>>>> >>>>> Sorry, I think it's may not correct. When replaying the jouranl, although >>>>> we don't use the extent statue tree, we still need to query the accurate >>>>> hole length, e.g. please see skip_hole(). If you do this, the hole length >>>>> becomes incorrect, right? >>>> >>>> Thank you for your review (and sorry for my delay replying). >>>> >>>> So, I see three different options to follow your suggestion: >>>> >>>> 1) Initialize 'es' immediately when declaring it in function >>>> ext4_ext_determine_insert_hole(): >>>> >>>> es.es_lblk = es.es_len = es.es_pblk = 0; >>>> >>>> 2) Initialize 'es' only in ext4_es_find_extent_range() when checking if an >>>> fc replay is in progress (my patch was already doing something like >>>> that): >>>> >>>> if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) { >>>> /* Initialize extent to zero */ >>>> es->es_lblk = es->es_len = es->es_pblk = 0; >>>> return; >>>> } >>>> >>>> 3) Remove the check for fc replay in function ext4_es_find_extent_range(), >>>> which will then unconditionally call __es_find_extent_range(). This >>>> will effectively also initialize the 'es' fields to '0' and, because >>>> __es_tree_search() will return NULL (at least in generic/039 test!), >>>> nothing else will be done. >>>> >>>> Since all these 3 options seem to have the same result, I believe option >>>> 1) is probably the best as it initializes the structure shortly after it's >>>> declaration. Would you agree? Or did I misunderstood you? >>>> >>> >>> Both 1 and 2 are looks fine to me, but I would prefer to initialize it >>> unconditionally in ext4_es_find_extent_range(). >>> >>> @@ -310,6 +310,8 @@ void ext4_es_find_extent_range(struct inode *inode, >>> ext4_lblk_t lblk, ext4_lblk_t end, >>> struct extent_status *es) >>> { >>> + es->es_lblk = es->es_len = es->es_pblk = 0; >>> + >>> if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) >>> return; >> >> Thank you, Yi. I'll send out v2 shortly. Although, to be fair, the real >> patch author shouldn't be me. :-) >> > > Never mind, I just give a suggestion and also I didn't do a full test on > this change. Oh, talking about testing, I forgot to mention that I see the same behaviour with generic/311. I.e. this test also enters an infinite loop, but fixed with this patch. Cheers, -- Luis