mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: DaeMyung Kang <charsyam@gmail.com>
To: Namjae Jeon <linkinjeon@kernel.org>, Hyunchul Lee <hyc.lee@gmail.com>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	DaeMyung Kang <charsyam@gmail.com>
Subject: [PATCH v3 2/3] ntfs: avoid heap allocation for free-cluster readahead state
Date: Thu, 21 May 2026 22:00:16 +0900	[thread overview]
Message-ID: <20260521130017.713848-3-charsyam@gmail.com> (raw)
In-Reply-To: <20260521130017.713848-1-charsyam@gmail.com>

get_nr_free_clusters() is run from the precalc_free_clusters() worker
queued at the end of ntfs_fill_super().  It is also the only place that
publishes the result by atomic64_set(&vol->free_clusters, ...), sets
NVolSetFreeClusterKnown(), and wakes vol->free_waitq.

The function currently allocates a temporary file_ra_state with
kzalloc() before that publication happens.  If the allocation fails,
get_nr_free_clusters() returns 0 without setting NVolFreeClusterKnown()
or waking vol->free_waitq, so callers that wait for the free count can
block forever.

The readahead state is only used synchronously while scanning the bitmap
and struct file_ra_state is small.  Keep it on the stack and pass it to
the readahead helper by address, eliminating the early allocation
failure path.  Zero-initialize the on-stack state because
file_ra_state_init() only sets ra_pages and prev_pos.

Signed-off-by: DaeMyung Kang <charsyam@gmail.com>
---
 fs/ntfs/super.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index 7e3561265b47..d2c4fb6ea0d4 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -1954,7 +1954,7 @@ s64 get_nr_free_clusters(struct ntfs_volume *vol)
 	struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
 	struct folio *folio;
 	pgoff_t index, max_index;
-	struct file_ra_state *ra;
+	struct file_ra_state ra = { 0 };
 
 	ntfs_debug("Entering.");
 	/* Serialize accesses to the cluster bitmap. */
@@ -1962,11 +1962,7 @@ s64 get_nr_free_clusters(struct ntfs_volume *vol)
 	if (NVolFreeClusterKnown(vol))
 		return atomic64_read(&vol->free_clusters);
 
-	ra = kzalloc(sizeof(*ra), GFP_NOFS);
-	if (!ra)
-		return 0;
-
-	file_ra_state_init(ra, mapping);
+	file_ra_state_init(&ra, mapping);
 
 	/*
 	 * Convert the number of bits into bytes rounded up, then convert into
@@ -1985,7 +1981,7 @@ s64 get_nr_free_clusters(struct ntfs_volume *vol)
 		 * Get folio from page cache, getting it from backing store
 		 * if necessary, and increment the use count.
 		 */
-		folio = ntfs_get_locked_folio(mapping, index, max_index, ra);
+		folio = ntfs_get_locked_folio(mapping, index, max_index, &ra);
 
 		/* Ignore pages which errored synchronously. */
 		if (IS_ERR(folio)) {
@@ -2024,7 +2020,6 @@ s64 get_nr_free_clusters(struct ntfs_volume *vol)
 	else
 		atomic64_set(&vol->free_clusters, nr_free);
 
-	kfree(ra);
 	NVolSetFreeClusterKnown(vol);
 	wake_up_all(&vol->free_waitq);
 	ntfs_debug("Exiting.");
-- 
2.43.0


  parent reply	other threads:[~2026-05-21 13:00 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-20 17:01 [PATCH 0/3] ntfs: fix mount failure cleanup and free-count setup DaeMyung Kang
2026-05-20 17:01 ` [PATCH 1/3] ntfs: free volume-wide resources on fill_super failure DaeMyung Kang
2026-05-21  1:03   ` Hyunchul Lee
2026-05-20 17:01 ` [PATCH 2/3] ntfs: wake free-cluster waiters when precalc allocation fails DaeMyung Kang
2026-05-21  1:03   ` Hyunchul Lee
2026-05-20 17:01 ` [PATCH 3/3] ntfs: only alias volume $UpCase to default on exact match DaeMyung Kang
2026-05-21  1:04   ` Hyunchul Lee
2026-05-21  1:02 ` [PATCH 0/3] ntfs: fix mount failure cleanup and free-count setup Hyunchul Lee
2026-05-21 10:17 ` [PATCH v2 " DaeMyung Kang
2026-05-21 10:17   ` [PATCH v2 1/3] ntfs: free volume-wide resources on fill_super failure DaeMyung Kang
2026-05-21 10:17   ` [PATCH v2 2/3] ntfs: avoid heap allocation for free-cluster readahead state DaeMyung Kang
2026-05-21 12:19     ` Namjae Jeon
2026-05-21 12:48       ` CharSyam
2026-05-22  0:00     ` Hyunchul Lee
2026-05-22  0:28       ` Namjae Jeon
2026-05-22  1:22         ` CharSyam
2026-05-22  4:31           ` Namjae Jeon
2026-05-21 10:17   ` [PATCH v2 3/3] ntfs: only alias volume $UpCase to default on exact match DaeMyung Kang
2026-05-21 13:00 ` [PATCH v3 0/3] ntfs: fix mount failure cleanup and free-count setup DaeMyung Kang
2026-05-21 13:00   ` [PATCH v3 1/3] ntfs: free volume-wide resources on fill_super failure DaeMyung Kang
2026-05-21 13:00   ` DaeMyung Kang [this message]
2026-05-21 13:00   ` [PATCH v3 3/3] ntfs: only alias volume $UpCase to default on exact match DaeMyung Kang
2026-05-23 14:11   ` [PATCH v3 0/3] ntfs: fix mount failure cleanup and free-count setup Namjae Jeon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260521130017.713848-3-charsyam@gmail.com \
    --to=charsyam@gmail.com \
    --cc=hyc.lee@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox