From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D348F397690 for ; Sun, 3 May 2026 04:58:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777784315; cv=none; b=c/bwbj7TU+f1pBSt8DLhksbZfi8AEH/TVRh/KaLeJkDJj/IQmTdcQhwP08HrXV3APHY34I13u0xbv4gcscQOaOCe6VgzCDBBLqmk9VI8yIJ+J53dQFdY7jP6scVzXZ+U3MLO/Bssv4fF5T5OAnM+yZyWW/w/ih5bW4m3oCM67sc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777784315; c=relaxed/simple; bh=7VVYY4xEBIvqMjO9dnJGnVC9P9kUyK/SnFikHPyHpxE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=X+OXx+T8Var40lsGhzwdPnBQbOlWjZA+e9scWxPmFEWOqmYisLE1Nt598xridG4Tfn/anoq1BSX+qgAfZuSiIBLPBBtWzE78rd9HWaxb4AqgHC7HeOV+v2owKhTZ2BbAT7s2LZhLXEMFaDX1OzYacjhHDjODtoM4pyjLsnDK7lE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JS9HKc64; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JS9HKc64" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2ad617d5b80so18277825ad.1 for ; Sat, 02 May 2026 21:58:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777784312; x=1778389112; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=bx/V9bGyVRo8uv3ZHvZhi/z5CN9+7hIS1jPr7RlO1vw=; b=JS9HKc64ZwSHeJotmMjhLVY1A+66fV2EdJhZ1zaCH5NK1wcBcCLorwXn5alqldkRfC t1br2J6g0KkT+EqIYUz+iTg/WDTWIeEWyj84Kk0pQd24gDoiNm7SWkKHcOohWW6r4e3p Xxbpo+qbmId6kB1fLQNmy0NfF5P8shmkHaOpjZEdqHeOzexwbgO/qTWHZwi24F+H7T/i RQ8jCGkYEhFGCR+3KqBcZ0PuCtMC1QyNx1s4AFhJz3K9k0W466q0RRr4aHWBL/EOqjjr Q17WFuX44E5+8o1Z63RK5jjJ2f1jCm+LdBfnJiZMUwcJj6ppYTPLfVhPd/DDMsfFo0fS 9pXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777784312; x=1778389112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=bx/V9bGyVRo8uv3ZHvZhi/z5CN9+7hIS1jPr7RlO1vw=; b=VvhmULFqrcp/owFC2L7RitflXLTqYBcFsKIipisgB8w7FrUTB8eCtCgnL7P1S5Vf7n uZ2+LmFPF6Y37KnWq4HoHqGuY2eDwTqWyP4K3KS3oLoF7iIIFNXzJ/bhKRs4SkBMeFMm tK0ALI4jw6q4NmKsUc8zYyHE+jcnZ6PBkHbYydowvFMolAZwABy6EOaG93Mh3fr4+36S X40/Nu5DMjZ508IzoMaOu0Yj+hTBqZx2kgVXKEhORTMTK95plRt2JpoEGrPsbcbxQZg/ ecL9+zR1aMUaaE90hkHEwzXgjaWkGo+s1twODyQ5htn/fByCzWYg3+11G6t6WBs5jXEQ 8Abg== X-Forwarded-Encrypted: i=1; AFNElJ8oJSNS6DYZtN7j9kREMtV8f9LBcmyKGXk/0IjAy14rbushDBoYnUs1s/ykvKq4LUsRfPGPzIznoRGunck=@vger.kernel.org X-Gm-Message-State: AOJu0Ywfwn0KF1MRbDATRo/Wa734tkok/sZikkm9B5R0wjZukCVXsbQN g9X4O2ihqPiz/Bkpqefa++UnuQMeK4BXGH4TfXCewu9x3TjRpC/0nDL+ X-Gm-Gg: AeBDiev1aZrZMTCeB9eGBVpYgnNmbLyENPRoQBp/FU3YMbnjb5ZUAefOgJ0P6p+rrIL 1X1MqTP2mVcSxcYCrYbvadOnSMDW+l14mzSSRAcuEdoQyNwmofcz7qxp4SHiBnIPAOHln1P3WKr T1Ax2p76eB4ArAjmNCFw7LzCmArYuYxZxNwn9qHMELOIqzvLbImWxDBc7SVbjySMeQK0fcKbW4N cbIOm6KtHFgh6eau5AnApi9oTIXSwlpjfA134/DRPQL2RECG6JV/gaTaQupXHIxzqalSgsYhl8f wgRAj1GQ1huWsv+67J8sOiXPi4S7buYH8jWNehKKx3Xu0Oha9+2JBB9TjIFQ6PIKnszyjrpz+n4 OL2ojiMZY8xJJST4KdXfmhbGDmyjl+HAGAfF36QmXwGDvgkeKrK+mMF/Qp3PkBy4t8EGJZG4H0B hyMw1xAy4hXb+ICtR7psA1zio9SxEM X-Received: by 2002:a17:903:2885:b0:2b0:a957:304 with SMTP id d9443c01a7336-2b9f2566d07mr28812215ad.6.1777784311964; Sat, 02 May 2026 21:58:31 -0700 (PDT) Received: from localhost ([111.228.63.84]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b9caaadf8dsm66729695ad.23.2026.05.02.21.58.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2026 21:58:31 -0700 (PDT) From: Cen Zhang To: jaegeuk@kernel.org, chao@kernel.org Cc: quic_stummala@quicinc.com, linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, baijiaju1990@gmail.com, Cen Zhang Subject: [PATCH] f2fs: start discard thread after mount recovery Date: Sun, 3 May 2026 12:58:20 +0800 Message-Id: <20260503045820.973718-1-zzzccc427@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Keep create_discard_cmd_control() limited to allocating and publishing the command-control object, which recovery can use to queue discard commands. Start the discard thread later in f2fs_fill_super(), after POR, recovery, checkpoint option handling, and discard tuning have completed. If starting the thread fails, unwind the shrinker and any GC thread that may already have been started before continuing through the existing mount-failure cleanup path. Fixes: d6d2b491a82e1e411a6766fbfb87c697d8701554 ("f2fs: allow to change discard policy based on cached discard cmds") Signed-off-by: Cen Zhang --- fs/f2fs/segment.c | 17 ++++------------- fs/f2fs/super.c | 12 ++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index 8390994a8..deb98f564 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -2302,12 +2302,10 @@ int f2fs_start_discard_thread(struct f2fs_sb_info *sbi) static int create_discard_cmd_control(struct f2fs_sb_info *sbi) { struct discard_cmd_control *dcc; - int err = 0, i; + int i; - if (SM_I(sbi)->dcc_info) { - dcc = SM_I(sbi)->dcc_info; - goto init_thread; - } + if (SM_I(sbi)->dcc_info) + return 0; dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL); if (!dcc) @@ -2344,14 +2342,7 @@ static int create_discard_cmd_control(struct f2fs_sb_info *sbi) init_waitqueue_head(&dcc->discard_wait_queue); SM_I(sbi)->dcc_info = dcc; -init_thread: - err = f2fs_start_discard_thread(sbi); - if (err) { - kfree(dcc); - SM_I(sbi)->dcc_info = NULL; - } - - return err; + return 0; } static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi) diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index 40079fd78..8228be53d 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -5340,6 +5340,15 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) f2fs_tuning_parameters(sbi); + /* + * After POR and mount-time recovery, we can run the discard thread. It + * reads node-manager memory thresholds and the superblock read-only + * state, so keep it out of the fill_super() initialization window. + */ + err = f2fs_start_discard_thread(sbi); + if (err) + goto leave_shrinker; + f2fs_notice(sbi, "Mounted with checkpoint version = %llx", cur_cp_version(F2FS_CKPT(sbi))); f2fs_update_time(sbi, CP_TIME); @@ -5349,6 +5358,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) sbi->umount_lock_holder = NULL; return 0; +leave_shrinker: + f2fs_leave_shrinker(sbi); + f2fs_stop_gc_thread(sbi); sync_free_meta: /* safe to flush all the data */ sync_filesystem(sbi->sb); -- 2.43.0