From patchwork Sun Jan 19 14:08:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chunhai Guo X-Patchwork-Id: 13944464 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3C078C02183 for ; Sun, 19 Jan 2025 13:51:13 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1tZVho-0004NR-Pg; Sun, 19 Jan 2025 13:51:12 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1tZVhn-0004ND-A5 for linux-f2fs-devel@lists.sourceforge.net; Sun, 19 Jan 2025 13:51:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=MIME-Version:Content-Type:Content-Transfer-Encoding :Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=uomzGJNUj1qirrzt+vLSWu2hlqJtxtk7N1b6UpF8aPg=; b=RglTtKTaGj87+JCrSuBjxlT6YX D3WmgyxwWY8WMAgZyz4kD65ZSXPJ1QTSqBNZHu3mqrTb4qeBe/3lx0naSVdtrTwSocMBC6IIcuQoL JEsFyBFamwhon0tDBptf1TMT3uZ7MyxaHy6IfRZ1EeSUO1TstFge0QpERNOf0r9LLePk=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:Content-ID:Content-Description:Resent-Date :Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=uomzGJNUj1qirrzt+vLSWu2hlqJtxtk7N1b6UpF8aPg=; b=B 5Emp5/vJxbfuMYw236enOksCwtk3UjVRn19WQsEleuyo53BZMTIc/UetDcVmqVJ5MLurr4UC0+/4X yBy941fZ97oD2z/8pKaeL8C9jHNEiAG0eNgyYmQ40NpnA4ssjn+CDbrbelp/50YO3cSTE6nEM62hr j4O+8e2Pw4cRZaF8=; Received: from mail-sg2apc01on2081.outbound.protection.outlook.com ([40.107.215.81] helo=APC01-SG2-obe.outbound.protection.outlook.com) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1tZVhm-0003Ji-Kz for linux-f2fs-devel@lists.sourceforge.net; Sun, 19 Jan 2025 13:51:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=btipi7DwerreJ3yX6EEbHgCWzzdpf3IGiSxm3DH+K8/MTyx3/qvvWz7QV10DRvDxv2uAD8W84vBDAYzL6IdUmI1FMLsagyKmwEddFCH4JPymvF6ybLjXdgVINId/WXuYCshmtP5vxNhqyIEI2POfltEIn1fc6m2e5NsTn6JpPiBh387cI1Rg6fzue8acW7yHPBoHb1LWVgImSwuwF3LbbJypdpOEIG+pcYzWq4gvJVvS8/A+jIEfCqvrDrYLJfu41kMNYYH9FJCDNNz0bX5no/gC3/Rh3BdOs3MrwWy7xUI9aiw+9ZsusZJbw+ljq8Gq3e9YBjYz488DJJZaOCNGlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uomzGJNUj1qirrzt+vLSWu2hlqJtxtk7N1b6UpF8aPg=; b=Pp9KzyGz5/Z6chU5PSI/kCnimY2iavsQq1qs4vsI9nrJWdMZyGrhbsNhMeZimIcynpxLAZnyXFzfYtUExTBsYa6kEth+1gdfkoLONM6Ex5gsBfEMQexugsq6fNPU/MQhfX9rFZIJmK/JzX/DBV6I27JhYppC1cCHFVr0xkZh0/7tiIegd0jDLfd3AebXCYTuQdYWsoDwB0IqKZM+eaieXOiCvMnALJQ3ojoriRAS6sortU8Z197JB8Di/BqJ4xz65wTYbhcI6cSkHTYpHdTGD7YrUxBTSePKMGQjfoRkhN5sJXtJW+Lk+70Qu2lahuGI8QlL+P7Q0ALht1dy/0ga/A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uomzGJNUj1qirrzt+vLSWu2hlqJtxtk7N1b6UpF8aPg=; b=EoTopP4CHl36d96vyXh47a1NP4w0Oo6b2XAGb5Opa0YSn3aosuO67NPVQh/AkkOfBqEQVnqgRfXGNQSxHAMUhzsidYYKvOSrffhMdKEh4K7zsU7H0cNmzkmVLrsJrQEghTEV/HnyuFa+gy0E9EUqVs9RNBp/QkeHY5B1S4NdCI2dFJsQfP3WR4XhAQ9R8hAIHOiIZjjjlzcKzCaRmVKfrZesgpSvencuxfxBoqrIn4gw4HlXvpnw4uAWLDkiW4mD+dYxPeOZ75K8VDjQdjr3r8luQSYi3+yNBfiw0QzJVXl3oL4lVP4tKKXAmagbO+dnQv3bokr2THWIaEvUvRrmAA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from SI6PR06MB7103.apcprd06.prod.outlook.com (2603:1096:4:24e::14) by TYUPR06MB5928.apcprd06.prod.outlook.com (2603:1096:400:35f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8377.11; Sun, 19 Jan 2025 13:51:00 +0000 Received: from SI6PR06MB7103.apcprd06.prod.outlook.com ([fe80::b34a:c5ea:54cb:7bf9]) by SI6PR06MB7103.apcprd06.prod.outlook.com ([fe80::b34a:c5ea:54cb:7bf9%4]) with mapi id 15.20.8377.004; Sun, 19 Jan 2025 13:51:00 +0000 To: chao@kernel.org, jaegeuk@kernel.org Date: Sun, 19 Jan 2025 07:08:34 -0700 Message-Id: <20250119140834.1061145-1-guochunhai@vivo.com> X-Mailer: git-send-email 2.34.1 X-ClientProxiedBy: SGXP274CA0021.SGPP274.PROD.OUTLOOK.COM (2603:1096:4:b8::33) To SI6PR06MB7103.apcprd06.prod.outlook.com (2603:1096:4:24e::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SI6PR06MB7103:EE_|TYUPR06MB5928:EE_ X-MS-Office365-Filtering-Correlation-Id: 1a5cea6b-e8a3-49a2-def1-08dd38904ed5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|1800799024|376014|52116014|38350700014; X-Microsoft-Antispam-Message-Info: hvU1n7WOQY8gww1UdJYhij3+UIlCiUVtgWAN0IpWkoBkPTx5cZZ5Uvz/7hYzUfkqzszED+FoUVfsFNruyQqcWPZIkxtRtv5UWtvENUeiuaww9g5m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gwbR8PiDN/a8x1UPD/yFNl/D77wavbLFKIsrMPCFRyrogMyB916dvr16aO35L02w+r+pOCR0dz2fIqaJfFKdUDoXWI0EY0elYkvUwBczOmlDt6/0bnmhZbBtACY2jyp6Hkg4n4Br/XXH0Xv3pZxBrA27dr7XIKCqrwJwDYuX4xVufyNWAI/60Fq7vqY3kCZVUoo1xhg8aKHZfBrz6RlolZDS6zVVYyslEyZTrmB7kkJ/jMd0sQ4l0S155SgaOb0zAga9O144ARA6XDrvVSuKfjHoo6 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SI6PR06MB7103.apcprd06.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(1800799024)(376014)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 36pfM3ygNhphO1bsMADL3lyLDtYq4QgNXF6kA8Dx6PUEJNZRhdZHEwIMKKAbrw5U8Ekdd5bQbaeDiqKVuJv8Yabm8GBYYpCGK6zwLYbe50Wu/zbvyzdd1NJFIFCexNZQCCO9h6czlBLk8rjcP4181hoq0gxZBAyHHkCyB68Vd1/La6Cw3d78oQzg0K8zC0Xb4VoGtRcJBU71zqYUf8GQQHCNayrnSs6pCP2V3v171TlshAQRIkkvs+7CimwPGMrZJovH6eXM5L4rDjtHi0AaRnlZG22FQJXizsKWvplXcJ49eC6Z7eLMRZY0QkQ7tcsGphLaaXTKPTisbf2QQ17lEwzXzUXXB8mmEa4XSOiYT0DVNJs+6xk5eAGiShr+K/PiI2pSeGtHzlirOjk73ULwiQ0CrxmGIXrEfthVj3qXzYHgJCdaASc2lDxeEAlGqfU+hX1PFhCO1a21rTd0cj+2L1qvfkkb6Mckdtuq+fwfUogsGXZMIE6CnKUzyO9mDvjuVW4m8iW9tfDVsKlymddeh8+V9wDVsz3hnkppc8DhBlFm6NTYoElxlqquB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cea6b-e8a3-49a2-def1-08dd38904ed5 X-MS-Exchange-CrossTenant-AuthSource: SI6PR06MB7103.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jan 2025 13:51:00.7121 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: xr/zBOefd7uOATlw3BJXWU4O3sHsmXQt+UkjHQsV0Kc1HZjT94nHcPCzh57kYQiHx/o3j2C+LAdyyYV+yKxAIA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYUPR06MB5928 X-Headers-End: 1tZVhm-0003Ji-Kz Subject: [f2fs-dev] [PATCH v2] f2fs: fix missing discard candidates in fstrim X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Chunhai Guo via Linux-f2fs-devel From: Chunhai Guo Reply-To: Chunhai Guo Cc: Chunhai Guo , linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net fstrim may miss candidates that need to be discarded, as shown in the examples below. The root cause is that when cpc->reason is set with CP_DISCARD, add_discard_addrs() expects that ckpt_valid_map and cur_valid_map have been synced by seg_info_to_raw_sit() [1], and it tries to find the candidates based on ckpt_valid_map and discard_map. However, seg_info_to_raw_sit() does not actually run before f2fs_exist_trim_candidates(), resulting in the failure. The code logic can be simplified for all cases by finding all the discard blocks based only on discard_map. This might result in more discard blocks being sent for the segment during the first checkpoint after mounting, which were originally expected to be sent only in fstrim. Regardless, these discard blocks should eventually be sent, and the simplified code makes sense in this context. root# cp testfile /f2fs_mountpoint root# f2fs_io fiemap 0 1 /f2fs_mountpoint/testfile Fiemap: offset = 0 len = 1 logical addr. physical addr. length flags 0 0000000000000000 0000000406a00000 000000003d800000 00001000 root# rm /f2fs_mountpoint/testfile root# fstrim -v -o 0x406a00000 -l 1024M /f2fs_mountpoint -- no candidate is found /f2fs_mountpoint: 0 B (0 bytes) trimmed Relevant code process of the root cause: f2fs_trim_fs() f2fs_write_checkpoint() ... if (cpc->reason & CP_DISCARD) { if (!f2fs_exist_trim_candidates(sbi, cpc)) { unblock_operations(sbi); goto out; // No candidates are found here, and it exits. } ... } [1] Please refer to commit d7bc2484b8d4 ("f2fs: fix small discards not to issue redundantly") for the relationship between seg_info_to_raw_sit() and add_discard_addrs(). Fixes: 25290fa5591d ("f2fs: return fs_trim if there is no candidate") Signed-off-by: Chunhai Guo --- v1: https://lore.kernel.org/linux-f2fs-devel/20250102101310.580277-1-guochunhai@vivo.com/ v1->v2: Find all the discard blocks based only on discard_map in add_discard_addrs(). --- fs/f2fs/segment.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index 13ee73a3c481..25ea892a42dd 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -2074,8 +2074,6 @@ static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc, { int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); - unsigned long *cur_map = (unsigned long *)se->cur_valid_map; - unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; unsigned long *discard_map = (unsigned long *)se->discard_map; unsigned long *dmap = SIT_I(sbi)->tmp_map; unsigned int start = 0, end = -1; @@ -2100,8 +2098,7 @@ static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc, /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ for (i = 0; i < entries; i++) - dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] : - (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; + dmap[i] = ~discard_map[i]; while (force || SM_I(sbi)->dcc_info->nr_discards <= SM_I(sbi)->dcc_info->max_discards) {