Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3638593rdh; Mon, 27 Nov 2023 22:23:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwSW4deFZBY8XYXxXKo6cSwk6qjhnFJZFqsbUTGGZr7lLRc0oI3oV/H0/18oxLvRr0NApQ X-Received: by 2002:a05:6358:52c3:b0:168:dd37:172d with SMTP id z3-20020a05635852c300b00168dd37172dmr12784172rwz.20.1701152586687; Mon, 27 Nov 2023 22:23:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701152586; cv=none; d=google.com; s=arc-20160816; b=p9FGlpIj5At7VsBvqLXOwS2X84BpvjNyTA3azesfuvCo60fvTmk2EgtNRwAPFdtAXt c9KoC2I1dAEsgYZrq5GM3N0ujpYRVI0V1AYvP4yIVEFrUazfxxpb/5u/xWrxy05uFLmy MfKq2bD3VniGUk/C1iMyR8pVIFYr/VvkfP1z1dBSnoTwtcsAcC1HYVNYqUMpjUJ0Lfkh ZpUG72fE+Kr8TEjW2O/cV6zx9nceikxiijsi+cdaT13XxCYnhhhHIzkVZAIXxWnMUyIk xuL44DCgmI5zc1sgog2I6zBp/vLByK3nOAuHZ2F2OEQtJ/1v0HI/T3mlAAH/Wv5A9fOT jphg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=lw5uVCgD9Taaun1taHIguxczes1SBV0WvdTM3jUNVGA=; fh=4jNXCa21TLDTrBQxwm4s4U6s7N1oIeMyGsbrzCPTXVk=; b=nyLMf98K/drbaSSDwC01aL6RgyiXiQiztrIuFMvybgp4aTIFrhHat4+rN9ZOYCgwsr G3PTFZjL32LOwGkaInspGPS998f2EU9CebWu9o2NuTIRCwNkYqNBhYp1MHfpIu6D+RlB Rw1i/HQE9eoBJIdmm1VE3QHWgfP3+JiDUKpoo/68GeLA5Y0kyfV0fIoZyopQI9MGMFSZ dB+iV3l9rGH0Y0zZtqjxiFkPxfgBvt7aN/cpkhssHj5xDhgX0k58PR2bPgXZX+U4f4/u H8gSakc0TmSilS1I0QxoT4Z3cfSMgPeUOf75Ky5O23JAaeE9RGuoN20ylXVLqL8Dfw2D 63gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lKjAGk9P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id bx23-20020a056a02051700b0059ccb99a2eesi13363445pgb.173.2023.11.27.22.23.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 22:23:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lKjAGk9P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id E673D8199704; Mon, 27 Nov 2023 22:23:03 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343633AbjK1GWk (ORCPT + 99 others); Tue, 28 Nov 2023 01:22:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234627AbjK1GWi (ORCPT ); Tue, 28 Nov 2023 01:22:38 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D75201A5 for ; Mon, 27 Nov 2023 22:22:43 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FBD0C433C8; Tue, 28 Nov 2023 06:22:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701152563; bh=t5sDrTG1EJKhTRMw8UnnW33SR0iqgn47M/6dfufOzwM=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=lKjAGk9Pubq8lOo9wuUlnV+fERNEnYW7irgo1HOlLwHCGNdaRkXmR4mGUOBpMUWDB mdCfyeEMDnsfXMWER7S7Zdb+DiIbiq10RO/fPrw0KFiMbvfO5RzBFR7ota7S/BOHU/ 0LhtXEN/PCppyv0XTV2LBMMRqx/mjp4aS/MLSSCXUpImVnNHHn9mE79h//8xTe4tZL LWfeEromkH0sXx1iw4Jcn67sMmRXgCsXuEAMAG2gtimXHkc+nxAIpB9qGUEQWaXFtw nR3jmm8Ugo2HWgNUlI6mP226+eBxVTkoDQV6UJfWNI6TtjK8kXZAJnFf2AkR1da6jg gG323YQhfrdDA== Message-ID: <5d8726fc-e912-6954-3820-862eecff07b0@kernel.org> Date: Tue, 28 Nov 2023 14:22:37 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: [PATCH 1/1] f2fs: fix fallocate failed under pinned block situation Content-Language: en-US To: Wu Bo , Wu Bo , Jaegeuk Kim Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org References: <20231030094024.263707-1-bo.wu@vivo.com> <670ce4a6-f00c-dbe9-86e2-366311221cf3@gmail.com> From: Chao Yu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 27 Nov 2023 22:23:04 -0800 (PST) On 2023/11/17 7:34, Wu Bo wrote: > On 2023/11/11 12:49, Chao Yu wrote: >> On 2023/11/8 21:48, Wu Bo wrote: >>> On 2023/11/7 22:39, Chao Yu wrote: >>>> On 2023/10/30 17:40, Wu Bo wrote: >>>>> If GC victim has pinned block, it can't be recycled. >>>>> And if GC is foreground running, after many failure try, the pinned file >>>>> is expected to be clear pin flag. To enable the section be recycled. >>>>> >>>>> But when fallocate trigger FG_GC, GC can never recycle the pinned >>>>> section. Because GC will go to stop before the failure try meet the >>>>> threshold: >>>>>      if (has_enough_free_secs(sbi, sec_freed, 0)) { >>>>>          if (!gc_control->no_bg_gc && >>>>>              total_sec_freed < gc_control->nr_free_secs) >>>>>              goto go_gc_more; >>>>>          goto stop; >>>>>      } >>>>> >>>>> So when fallocate trigger FG_GC, at least recycle one. >>>> >>>> Hmm... it may break pinfile's semantics at least on one pinned file? >>>> In this case, I prefer to fail fallocate() rather than unpinning file, >>>> in order to avoid leaving invalid LBA references of unpinned file held >>>> by userspace. >>> >>> As f2fs designed now, FG_GC is able to unpin the pinned file. >>> >>> fallocate() triggered FG_GC, but can't recycle space.  It breaks the >>> design logic of FG_GC. >> >> Yes, contradictoriness exists. >> >> IMO, unpin file by GC looks more dangerous, it may cause potential data >> corruption w/ below case: >> 1. app pins file & holds LBAs of data blocks. >> 2. GC unpins file and migrates its data to new LBAs. >> 3. other file reuses previous LBAs. >> 4. app read/write data via previous LBAs. >> >> So I suggest to normalize use of pinfile and do not add more unpin cases >> in filesystem inner processes. >> >>> >>> This issue is happened in Android OTA scenario.  fallocate() always >>> return failure cause OTA fail. >> >> Can you please check why other pinned files were so fragmented that f2fs_gc() >> can not recycle one free section? >> > Not because pinned files were fragmented, but if the GC victim section has one block is pinned will cause this issue. > > If the section don't unpin the block, it can't be recycled. But there is high chance that the pinned section will be chosen next time under f2fs current victim selection strategy. > > So if we want to avoid unpin files, I think change victim selection to considering pinned blocks can fix this issue. Oh, I get it. How about this? diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 325dab01a29d..3fb52dec5df8 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -1730,7 +1730,10 @@ next_alloc: f2fs_down_write(&sbi->gc_lock); stat_inc_gc_call_count(sbi, FOREGROUND); err = f2fs_gc(sbi, &gc_control); - if (err && err != -ENODATA) + + if (err == -EAGAIN) + f2fs_balance_fs(sbi, true); + else if (err && err != -ENODATA) goto out_err; } However, the code won't fix contradictoriness issue, because the root cause is we left fragmented pinned data in filesystem, which should be avoided in GC-reliance LFS filesyetem as much as possible. Thanks, > >> Thanks, >> >>> >>>    And this commit changed previous behavior of fallocate(): >>> >>> Commit 2e42b7f817ac ("f2fs: stop allocating pinned sections if EAGAIN >>> happens") >>> >>> Before this commit, if fallocate() meet this situation, it will trigger >>> FG_GC to recycle pinned space finally. >>> >>> FG_GC is expected to recycle pinned space when there is no more free >>> space.  And this is the right time to do it when fallocate() need free >>> space. >>> >>> It is weird when f2fs shows enough spare space but can't fallocate(). So >>> I think it should be fixed. >>> >>>> >>>> Thoughts? >>>> >>>> Thanks, >>>> >>>>> >>>>> This issue can be reproduced by filling f2fs space as following layout. >>>>> Every segment has one block is pinned: >>>>> +-+-+-+-+-+-+-----+-+ >>>>> | | |p| | | | ... | | seg_n >>>>> +-+-+-+-+-+-+-----+-+ >>>>> +-+-+-+-+-+-+-----+-+ >>>>> | | |p| | | | ... | | seg_n+1 >>>>> +-+-+-+-+-+-+-----+-+ >>>>> ... >>>>> +-+-+-+-+-+-+-----+-+ >>>>> | | |p| | | | ... | | seg_n+k >>>>> +-+-+-+-+-+-+-----+-+ >>>>> >>>>> And following are steps to reproduce this issue: >>>>> dd if=/dev/zero of=./f2fs_pin.img bs=2M count=1024 >>>>> mkfs.f2fs f2fs_pin.img >>>>> mkdir f2fs >>>>> mount f2fs_pin.img ./f2fs >>>>> cd f2fs >>>>> dd if=/dev/zero of=./large_padding bs=1M count=1760 >>>>> ./pin_filling.sh >>>>> rm padding* >>>>> sync >>>>> touch fallocate_40m >>>>> f2fs_io pinfile set fallocate_40m >>>>> fallocate -l 41943040 fallocate_40m >>>>> >>>>> fallocate always fail with EAGAIN even there has enough free space. >>>>> >>>>> 'pin_filling.sh' is: >>>>> count=1 >>>>> while : >>>>> do >>>>>       # filling the seg space >>>>>       for i in {1..511}: >>>>>       do >>>>>           name=padding_$count-$i >>>>>           echo write $name >>>>>           dd if=/dev/zero of=./$name bs=4K count=1 > /dev/null 2>&1 >>>>>           if [ $? -ne 0 ]; then >>>>>                   exit 0 >>>>>           fi >>>>>       done >>>>>       sync >>>>> >>>>>       # pin one block in a segment >>>>>       name=pin_file$count >>>>>       dd if=/dev/zero of=./$name bs=4K count=1 > /dev/null 2>&1 >>>>>       sync >>>>>       f2fs_io pinfile set $name >>>>>       count=$(($count + 1)) >>>>> done >>>>> >>>>> Signed-off-by: Wu Bo >>>>> --- >>>>>    fs/f2fs/file.c | 2 +- >>>>>    1 file changed, 1 insertion(+), 1 deletion(-) >>>>> >>>>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c >>>>> index ca5904129b16..e8a13616543f 100644 >>>>> --- a/fs/f2fs/file.c >>>>> +++ b/fs/f2fs/file.c >>>>> @@ -1690,7 +1690,7 @@ static int f2fs_expand_inode_data(struct inode >>>>> *inode, loff_t offset, >>>>>                .init_gc_type = FG_GC, >>>>>                .should_migrate_blocks = false, >>>>>                .err_gc_skipped = true, >>>>> -            .nr_free_secs = 0 }; >>>>> +            .nr_free_secs = 1 }; >>>>>        pgoff_t pg_start, pg_end; >>>>>        loff_t new_size; >>>>>        loff_t off_end;