From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 ADB443D7D81 for ; Mon, 4 May 2026 12:48:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777898935; cv=none; b=mx7Hmwae18apXMq4/9/gVSj1csO7ZKgUcFb69MPiFMdmSLJNgtOV6B6H5e3fAkX6jp3wtjXE3cXF53EdOLKS0l52i0oigyFyOUfZrTeWp/t/sfKye1R2e1Yxcb3Cxy42mVHxEIPhFF7CN/SCqy/PhKP6e/F4qRvO7vmgbuxlIQY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777898935; c=relaxed/simple; bh=gRpYRmrtNS3hGt3FeZ4hVvrZZ5AWY+e8dbVSL7kGQkM=; h=Message-ID:Date:MIME-Version:Cc:Subject:To:References:From: In-Reply-To:Content-Type; b=qVRkKQAs/PHEs8AhM1sPNGDNvzlcTH5+fGyP+OO4bKHY2YyVBg55BVdvm/V+lA49RUBsdBB9CHpv8UwwiH0cFEKxxKT+WICiKcUWEzlWaP/iHt9ezBE6iBlt3RQ8foYeF2MRH7bqUqW3XdcMPdnJIO1Wt7L29phMcJsS+GkSj4k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i5GkrMsy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="i5GkrMsy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E467FC2BCF6; Mon, 4 May 2026 12:48:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777898935; bh=gRpYRmrtNS3hGt3FeZ4hVvrZZ5AWY+e8dbVSL7kGQkM=; h=Date:Cc:Subject:To:References:From:In-Reply-To:From; b=i5GkrMsyGX12CNuMTkslHNYX7ZhvGXPJPYnuv7V5tmKvTBZPFAfS9bh2aykpGSYHB 7WYn+pF14DHstecjIkAZDo7WmL0SkBlYQXB1trM7hWv7NoBViW1CS2/lDHXnQkj2al C0wFI7ff2uAqrCtUocC8wLgsmhNPTYr8+LA/NjerSbKI6e+Msx+UCuAo5VrdHW4JhY LoJu+uL6tjoz3GUlE3Z+FWizQ759YFslxuBFqUnD4yd3Vpi+C/QlLzMRCN+7YJC689 6QzeJ7sDNEy7v/psme65llsuqPNWxVW2Q/hDsEVX5iY7WpafZeuzE+8bCkWEsIXPgk Ee8vswOOEACTw== Message-ID: <87bdc398-9387-49bf-ace1-7e7101661767@kernel.org> Date: Mon, 4 May 2026 20:48:48 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: chao@kernel.org, jaegeuk@kernel.org, quic_stummala@quicinc.com, linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, baijiaju1990@gmail.com Subject: Re: [PATCH] f2fs: start discard thread after mount recovery To: Cen Zhang References: <20260503045820.973718-1-zzzccc427@gmail.com> <2ba0b3ad-615e-43c8-a165-af631607a4c3@kernel.org> Content-Language: en-US From: Chao Yu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 5/3/26 18:16, Cen Zhang wrote: > Dear Chao Yu > > Thanks for taking a look, and sorry for the confusion. > >> >> On 5/3/26 12:58, Cen Zhang wrote: >>> The discard command control is built while the segment manager is >>> constructed, before the node manager is built and before mount recovery >>> has completed. Starting the discard thread from that constructor lets the >>> background thread run while f2fs_fill_super() is still publishing and >>> initializing mount-time state. >>> >>> After commit d6d2b491a82e ("f2fs: allow to change discard policy based >>> on cached discard cmds"), issue_discard_thread() may consult node-manager >>> memory thresholds through f2fs_available_free_memory(). It can therefore >>> observe sbi->nm_info while f2fs_build_node_manager() is publishing and >>> initializing it. The same early-start window also lets the thread test >>> the superblock read-only state while recovery paths still make temporary >>> SB_RDONLY transitions. >> >> Not sure I understood you correctly, do you mean this? >> > No, I did not mean the old UAF-style failure path. That path should > already be covered by the existing cleanup ordering: > > free_nm: > f2fs_stop_discard_thread(sbi); > f2fs_destroy_node_manager(sbi); > > What I was trying to describe is an earlier window during mount > initialization, when the discard thread is actually started: > > - f2fs_fill_super() > - f2fs_build_segment_manager() > - create_discard_cmd_control() > - f2fs_start_discard_thread() > - f2fs_build_node_manager() > - sbi->nm_info = f2fs_kzalloc(...) > - init_node_manager() initializes fields such as ram_thresh, counters, > locks/lists/bitmaps, etc. > > At this point issue_discard_thread() may run concurrently and call > f2fs_available_free_memory(). That helper reads NM_I(sbi), returns true > only if it is NULL, and otherwise uses node-manager fields such as > nm_i->ram_thresh. So my concern is that the thread may observe a newly > published but still being initialized node manager, rather than a freed > one. Cen, I see, thanks for the explanation. Can you please update commit message w/ the explanation? That will be helpful for reviewer and git blame. Thanks, > > The data race report maps to the following paths in v7.0.3: > > issue_discard_thread() fs/f2fs/segment.c:1921 > f2fs_available_free_memory() fs/f2fs/node.c:50 > NM_I() fs/f2fs/f2fs.h:2228 > > racing with mount-time node-manager initialization: > > f2fs_fill_super() fs/f2fs/super.c:5151 > f2fs_build_node_manager() fs/f2fs/node.c:3420 > > There is also a similar early-start window for the SB_RDONLY check in > issue_discard_thread(), since mount recovery can still make temporary > superblock flag transitions before f2fs_fill_super() reaches the stable > mounted state. The second report maps to: > > issue_discard_thread() fs/f2fs/segment.c:1935 > f2fs_readonly() fs/f2fs/f2fs.h:3665 > > racing with mount recovery restoring the superblock flags: > > f2fs_fill_super() fs/f2fs/super.c:5267 > f2fs_recover_fsync_data() fs/f2fs/recovery.c:953 > > So the patch is intended to fix this lifecycle ordering: keep the discard > command control available early, but defer starting the background > discard thread until mount recovery and node-manager initialization have > completed. > > Please let me know if I missed anything here. > > Best regards, > Cen