Skip to contentSkip to conversation

ntfs: serialize attribute-list replacement with lookups

PATCH Superseded Cen Zhang 2 participants 5 messages Jul 2 – Jul 6

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Cen Zhang

author
#0 lore raw

PATCH v4 1/1 ntfs: serialize attribute-list replacement with lookups

ntfs_external_attr_find() walks base_ni->attr_list with raw attr_list_entry cursors stored in ctx->al_entry and local variables. Other metadata paths can replace base_ni->attr_list and free the old buffer while a lookup still carries one of those cursors.

The buggy scenario involves two paths, with each column showing the order within that path:

  attribute lookup path:              attribute-list mutation path:
  1. enters ntfs_attr_lookup()        1. enters an attr-list add/remove
  2. reads base_ni->attr_list         2. builds a replacement buffer
  3. keeps ctx->al_entry/al_entry     3. publishes base_ni->attr_list
     pointing into that buffer        4. frees the previous buffer

4. continues the attribute search
5. dereferences the saved cursor

Validation reproduced this kernel report:

BUG: KASAN: slab-use-after-free in ntfs_attr_lookup+0x24b2/0x2bf0

Call Trace:
 <TASK>
 dump_stack_lvl+0x66/0xa0
 print_report+0xce/0x630
 ? ntfs_attr_lookup+0x24b2/0x2bf0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __virt_addr_valid+0x20d/0x410
 ? ntfs_attr_lookup+0x24b2/0x2bf0
 kasan_report+0xe0/0x110
 ? ntfs_attr_lookup+0x24b2/0x2bf0
 ntfs_attr_lookup+0x24b2/0x2bf0
 ? ntfs_attr_lookup+0x9/0x2bf0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? 0xffffffffc00000da
 ? __pfx_ntfs_attr_lookup+0x10/0x10
 ntfs_attr_map_whole_runlist+0x488/0x8b0
 ? __pfx_ntfs_attr_map_whole_runlist+0x10/0x10
 ? __pfx_do_raw_spin_trylock+0x10/0x10
 ntfs_attr_vcn_to_rl+0x13a/0x1d0
 ? filemap_fault+0xf03/0x2080
 ntfs_read_iomap_begin_non_resident+0x142/0x920
 ? __pfx_ntfs_read_iomap_begin_non_resident+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? __pfx_ntfs_read_iomap_begin+0x10/0x10
 iomap_iter+0x6de/0x11e0
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? prepare_alloc_pages+0x1c4/0x4d0
 iomap_readahead+0x239/0x910
 ? folios_put_refs+0x1ed/0x4f0
 ? __pfx_iomap_readahead+0x10/0x10
 ? __pfx_folios_put_refs+0x10/0x10
 ? lock_release+0x1e0/0x280
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? xas_store+0x871/0x1650
 ? xas_find_conflict+0x64b/0xb90
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? lock_release+0x1e0/0x280
 ntfs_readahead+0x193/0x1d0
 ? __pfx_ntfs_readahead+0x10/0x10
 ? __filemap_add_folio+0x869/0xa60
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? lock_acquire+0x2b8/0x2f0
 ? __pfx___filemap_add_folio+0x10/0x10
 read_pages+0x18f/0x890
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? folio_alloc_noprof+0x69/0xb0
 ? __pfx_read_pages+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 page_cache_ra_unbounded+0x389/0x6c0
 do_page_cache_ra+0x109/0x170
 filemap_fault+0xf03/0x2080
 ? __pfx_filemap_fault+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? lock_acquire+0x2b8/0x2f0
 ? lockdep_init_map_type+0x5c/0x230
 __do_fault+0xf0/0x260
 __handle_mm_fault+0xdcf/0x1ca0
 ? __pfx___handle_mm_fault+0x10/0x10
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? srso_alias_return_thunk+0x5/0xfbef5
 ? lock_release+0x1e0/0x280
 handle_mm_fault+0x19c/0x470
 do_user_addr_fault+0x23b/0x9c0
 exc_page_fault+0x5c/0xc0
 asm_exc_page_fault+0x26/0x30

Allocated by task 511:
 kasan_save_stack+0x33/0x60
 kasan_save_track+0x14/0x30
 __kasan_kmalloc+0xaa/0xb0
 __kvmalloc_node_noprof+0x353/0x920
 ntfs_attrlist_entry_rm+0x28d/0x510
 ntfs_attr_record_rm+0x4dd/0xda0
 ntfs_delete+0x50a/0xf90
 ntfs_unlink+0x212/0x4d0
 vfs_unlink+0x259/0xb00
 filename_unlinkat+0x2db/0x5f0
 __x64_sys_unlink+0x46/0x70
 do_syscall_64+0x115/0x6a0
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 511:
 kasan_save_stack+0x33/0x60
 kasan_save_track+0x14/0x30
 kasan_save_free_info+0x3b/0x60
 __kasan_slab_free+0x5f/0x80
 kfree+0x307/0x580
 ntfs_attrlist_entry_add+0xc1b/0x1110
 ntfs_resident_attr_record_add+0x6a8/0xa10
 ntfs_attr_add+0x682/0xcd0
 __ntfs_link+0x815/0xde0
 ntfs_link+0x223/0x3d0
 vfs_link+0x465/0xcf0
 filename_linkat+0x316/0x540
 __x64_sys_link+0x81/0xb0
 do_syscall_64+0x115/0x6a0
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Add a per-ntfs_inode attr_list_lock and couple it to ntfs_attr_search_ctx. Search contexts that may carry attr-list cursors take the read side for their full lifetime, so ctx->al_entry remains protected until ntfs_attr_put_search_ctx(). Attribute-list mutation paths use the write side while deriving offsets from the old list and publishing the replacement list. Contexts used under runlist.lock use an explicit NONE mode so the ordering stays:

mrec_lock => attr_list_lock => runlist.lock

Fixes 495e90fa3348 ("ntfs: update attrib operations")
Assisted-by Codex:gpt-5.5

---
v4:
- Rebase onto ntfs-for-7.2-rc1 and carry attr_list_lock through the rebased nolock runlist mapping paths.
- Make LOCK_NONE assert that attr_list_lock is already held.
- Remove the lock_mode argument from __ntfs_attr_update_mapping_pairs().
- Avoid recursive attr_list_lock acquisition when resident resize calls ntfs_attr_make_non_resident().

v3:
- Keep attr_list_lock tied to ntfs_attr_search_ctx lifetime instead of taking it only around ntfs_attr_lookup().
- Add nolock helpers for paths that already hold attr_list_lock before runlist.lock, preserving the mrec_lock => attr_list_lock => runlist.lock order.
- Protect the remaining direct attr_list walker in ntfs_inode_attach_all_extents().

---
 fs/ntfs/aops.c     |   3 +
 fs/ntfs/attrib.c   | 315 ++++++++++++++++++++++++++++++++++++---------
 fs/ntfs/attrib.h   |  27 +++-
 fs/ntfs/attrlist.c |  30 +++--
 fs/ntfs/compress.c |   6 +-
 fs/ntfs/dir.c      |   8 +-
 fs/ntfs/ea.c       |   2 +-
 fs/ntfs/file.c     |  13 +-
 fs/ntfs/index.c    |   5 +-
 fs/ntfs/inode.c    |  88 ++++++++++---
 fs/ntfs/inode.h    |   7 +
 fs/ntfs/iomap.c    |  23 +++-
 fs/ntfs/logfile.c  |   4 +
 fs/ntfs/mft.c      |  63 ++++++---
 fs/ntfs/namei.c    |   6 +-
 fs/ntfs/super.c    |   6 +-
 16 files changed, 481 insertions(+), 125 deletions(-)
fs/ntfs/aops.c +3 -0
index 1fbf832ad1654..981777ff24279 100644
@@ -131,6 +131,7 @@ static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
 	s64 lcn;
 	unsigned long blocksize, flags;
 	struct ntfs_inode *ni = NTFS_I(mapping->host);
+	struct ntfs_inode *attr_lock_ni;
 	struct ntfs_volume *vol = ni->vol;
 	unsigned int delta;
 	unsigned char blocksize_bits;
@@ -160,10 +161,12 @@ static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
 	 */
 	if (unlikely(ofs >= size || (ofs + blocksize > size && size < i_size)))
 		goto hole;
+	attr_lock_ni = ntfs_attr_list_lock(ni, NTFS_ATTR_CTX_LOCK_READ);
 	down_read(&ni->runlist.lock);
 	lcn = ntfs_attr_vcn_to_lcn_nolock(ni, ntfs_bytes_to_cluster(vol, ofs),
 			false);
 	up_read(&ni->runlist.lock);
+	ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 	if (unlikely(lcn < LCN_HOLE)) {
 		/*
 		 * Step down to an integer to avoid gcc doing a long long
fs/ntfs/attrib.c +254 -61

776 lines not on this page · raw message

fs/ntfs/attrib.h +25 -2
index e2224fbfaabe9..6f167194afb89 100644
@@ -26,6 +26,8 @@ extern __le16 AT_UNNAMED[];
  * @base_ntfs_ino: Base inode
  * @mapped_base_mrec: true if @base_mrec was mapped by the search
  * @base_attr: Base attribute record pointer
+ * @attr_list_lock_ni: Base inode whose attr_list_lock is held
+ * @attr_list_lock_mode: attr_list_lock mode held by this context
  *
  * Structure must be initialized to zero before the first call to one of the
  * attribute search functions. Initialize @mrec to point to the mft record to
@@ -39,6 +41,12 @@ extern __le16 AT_UNNAMED[];
  * any modification of the search context, to automagically get the next
  * matching attribute.
  */
+enum ntfs_attr_search_ctx_lock_mode {
+	NTFS_ATTR_CTX_LOCK_NONE,
+	NTFS_ATTR_CTX_LOCK_READ,
+	NTFS_ATTR_CTX_LOCK_WRITE,
+};
+
 struct ntfs_attr_search_ctx {
 	struct mft_record *mrec;
 	bool mapped_mrec;
@@ -50,6 +58,8 @@ struct ntfs_attr_search_ctx {
 	struct mft_record *base_mrec;
 	bool mapped_base_mrec;
 	struct attr_record *base_attr;
+	struct ntfs_inode *attr_list_lock_ni;
+	enum ntfs_attr_search_ctx_lock_mode attr_list_lock_mode;
 };
 
 enum {                  /* ways of processing holes when expanding */
@@ -66,6 +76,7 @@ struct runlist_element *ntfs_attr_find_vcn_nolock(struct ntfs_inode *ni,
 		const s64 vcn, struct ntfs_attr_search_ctx *ctx);
 struct runlist_element *__ntfs_attr_find_vcn_nolock(struct runlist *runlist,
 		const s64 vcn);
+int ntfs_attr_map_whole_runlist_nolock(struct ntfs_inode *ni);
 int ntfs_attr_map_whole_runlist(struct ntfs_inode *ni);
 int ntfs_attr_lookup(const __le32 type, const __le16 *name,
 		const u32 name_len, const u32 ic,
@@ -87,7 +98,16 @@ static inline s64 ntfs_attr_size(const struct attr_record *a)
 
 void ntfs_attr_reinit_search_ctx(struct ntfs_attr_search_ctx *ctx);
 struct ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(struct ntfs_inode *ni,
-		struct mft_record *mrec);
+		struct mft_record *mrec,
+		enum ntfs_attr_search_ctx_lock_mode lock_mode);
+struct ntfs_inode *ntfs_attr_list_lock(struct ntfs_inode *ni,
+				       enum ntfs_attr_search_ctx_lock_mode lock_mode);
+void ntfs_attr_list_unlock(struct ntfs_inode *lock_ni,
+			   enum ntfs_attr_search_ctx_lock_mode lock_mode);
+void ntfs_attr_lock_search_ctx(struct ntfs_attr_search_ctx *ctx,
+			       struct ntfs_inode *ni,
+			       enum ntfs_attr_search_ctx_lock_mode lock_mode);
+void ntfs_attr_unlock_search_ctx(struct ntfs_attr_search_ctx *ctx);
 void ntfs_attr_put_search_ctx(struct ntfs_attr_search_ctx *ctx);
 int ntfs_attr_size_bounds_check(const struct ntfs_volume *vol,
 		const __le32 type, const s64 size);
@@ -133,6 +153,7 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
 		__le16 *name, u8 name_len, u8 *val, u32 size,
 		__le16 flags);
 int ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni, s64 from_vcn);
+int ntfs_attr_update_mapping_pairs_nolock(struct ntfs_inode *ni, s64 from_vcn);
 struct runlist_element *ntfs_attr_vcn_to_rl(struct ntfs_inode *ni, s64 vcn, s64 *lcn);
 
 /*
@@ -148,7 +169,9 @@ struct runlist_element *ntfs_attr_vcn_to_rl(struct ntfs_inode *ni, s64 vcn, s64
  *	    @ni, you can simply do:
  *
  *	int err;
- *	struct ntfs_attr_search_ctx *ctx = ntfs_attr_get_search_ctx(ni, NULL);
+ *	struct ntfs_attr_search_ctx *ctx;
+ *
+ *	ctx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_READ);
  *	if (!ctx)
  *		// Error code is in errno. Handle this case.
  *	while (!(err = ntfs_attrs_walk(ctx))) {
fs/ntfs/attrlist.c +21 -9
index afb13038ba425..fd5eac0612784 100644
@@ -153,7 +153,7 @@ int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
 		return -ENOMEM;
 
 	/* Find place for the new entry. */
-	ctx = ntfs_attr_get_search_ctx(ni, NULL);
+	ctx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_WRITE);
 	if (!ctx) {
 		err = -ENOMEM;
 		ntfs_error(ni->vol->sb, "Failed to get search context");
@@ -176,7 +176,6 @@ int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
 		if (ctx->al_entry->lowest_vcn == lowest_vcn) {
 			err = -EEXIST;
 			ntfs_debug("Such attribute already present in the attribute list.\n");
-			ntfs_attr_put_search_ctx(ctx);
 			goto err_out;
 		}
 		/* Add new entry after this extent. */
@@ -186,15 +185,11 @@ int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
 		/* Check for real errors. */
 		if (err != -ENOENT) {
 			ntfs_debug("Attribute lookup failed.\n");
-			ntfs_attr_put_search_ctx(ctx);
 			goto err_out;
 		}
 		/* No previous extents found. */
 		ale = ctx->al_entry;
 	}
-	/* Don't need it anymore, @ctx->al_entry points to @ni->attr_list. */
-	ntfs_attr_put_search_ctx(ctx);
-
 	/* Determine new entry offset. */
 	entry_offset = ((u8 *)ale - ni->attr_list);
 	/* Set pointer to new entry. */
@@ -223,16 +218,22 @@ int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
 	old_al = ni->attr_list;
 	ni->attr_list = new_al;
 	ni->attr_list_size = ni->attr_list_size + entry_len;
+	ntfs_attr_put_search_ctx(ctx);
+	ctx = NULL;
 
 	err = ntfs_attrlist_update(ni);
 	if (err) {
+		down_write(&ni->attr_list_lock);
 		ni->attr_list = old_al;
 		ni->attr_list_size -= entry_len;
+		up_write(&ni->attr_list_lock);
 		goto err_out;
 	}
 	kvfree(old_al);
 	return 0;
 err_out:
+	if (ctx)
+		ntfs_attr_put_search_ctx(ctx);
 	kvfree(new_al);
 	return err;
 }
@@ -248,9 +249,12 @@ int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
 int ntfs_attrlist_entry_rm(struct ntfs_attr_search_ctx *ctx)
 {
 	u8 *new_al;
-	int new_al_len;
+	int err, new_al_len;
 	struct ntfs_inode *base_ni;
 	struct attr_list_entry *ale;
+	enum ntfs_attr_search_ctx_lock_mode lock_mode;
+	bool ctx_locked;
+	u8 *old_al;
 
 	if (!ctx || !ctx->ntfs_ino || !ctx->al_entry) {
 		ntfs_debug("Invalid arguments.\n");
@@ -285,9 +289,17 @@ int ntfs_attrlist_entry_rm(struct ntfs_attr_search_ctx *ctx)
 				ale->length), new_al_len - ((u8 *)ale - base_ni->attr_list));
 
 	/* Set new runlist. */
-	kvfree(base_ni->attr_list);
+	old_al = base_ni->attr_list;
 	base_ni->attr_list = new_al;
 	base_ni->attr_list_size = new_al_len;
+	kvfree(old_al);
 
-	return ntfs_attrlist_update(base_ni);
+	ctx_locked = ctx->attr_list_lock_ni;
+	lock_mode = ctx->attr_list_lock_mode;
+	if (ctx_locked)
+		ntfs_attr_unlock_search_ctx(ctx);
+	err = ntfs_attrlist_update(base_ni);
+	if (ctx_locked)
+		ntfs_attr_lock_search_ctx(ctx, base_ni, lock_mode);
+	return err;
 }
fs/ntfs/compress.c +5 -1
index 76bd806b41edd..bac6b32069475 100644
@@ -1287,6 +1287,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	/* more compressed zeroes, to be followed by some count */
 	static char morezeroes[] = {0x03, 0xb0, 0x02, 0x00};
 	struct page **pages_disk = NULL, *pg;
+	struct ntfs_inode *attr_lock_ni;
 	s64 bio_lcn;
 	struct runlist_element *rlc, *rl;
 	int i, err;
@@ -1390,10 +1391,12 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	}
 
 	bio_lcn = rlc->lcn;
+	attr_lock_ni = ntfs_attr_list_lock(ni, NTFS_ATTR_CTX_LOCK_WRITE);
 	down_write(&ni->runlist.lock);
 	rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count);
 	if (IS_ERR(rl)) {
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_WRITE);
 		ntfs_error(vol->sb, "Failed to merge runlists");
 		err = PTR_ERR(rl);
 		if (ntfs_cluster_free_from_rl(vol, rlc))
@@ -1405,8 +1408,9 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
 	ni->runlist.count = new_rl_count;
 	ni->runlist.rl = rl;
 
-	err = ntfs_attr_update_mapping_pairs(ni, 0);
+	err = ntfs_attr_update_mapping_pairs_nolock(ni, 0);
 	up_write(&ni->runlist.lock);
+	ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_WRITE);
 	if (err) {
 		err = -EIO;
 		goto out;
fs/ntfs/dir.c +4 -4
index 4b6bd5f30c65e..165503d059c6d 100644
@@ -91,7 +91,7 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
 				-PTR_ERR(m));
 		return ERR_MREF(PTR_ERR(m));
 	}
-	ctx = ntfs_attr_get_search_ctx(dir_ni, m);
+	ctx = ntfs_attr_get_search_ctx(dir_ni, m, NTFS_ATTR_CTX_LOCK_READ);
 	if (unlikely(!ctx)) {
 		err = -ENOMEM;
 		goto err_out;
@@ -814,7 +814,7 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
 		file->private_data = private;
 	}
 
-	ctx = ntfs_attr_get_search_ctx(ndir, NULL);
+	ctx = ntfs_attr_get_search_ctx(ndir, NULL, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx) {
 		err = -ENOMEM;
 		goto out;
@@ -1015,7 +1015,7 @@ int ntfs_check_empty_dir(struct ntfs_inode *ni, struct mft_record *ni_mrec)
 	if (!(ni_mrec->flags & MFT_RECORD_IS_DIRECTORY))
 		return 0;
 
-	ctx = ntfs_attr_get_search_ctx(ni, NULL);
+	ctx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx) {
 		ntfs_error(ni->vol->sb, "Failed to get search context");
 		return -ENOMEM;
@@ -1116,7 +1116,7 @@ static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
 	if (NVolShutdown(vol))
 		return -EIO;
 
-	ctx = ntfs_attr_get_search_ctx(ni, NULL);
+	ctx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx)
 		return -ENOMEM;
 
fs/ntfs/ea.c +1 -1
index 0cd192752b7cd..284c985807a6e 100644
@@ -587,7 +587,7 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
 	if (IS_ERR(m))
 		return PTR_ERR(m);
 
-	ctx = ntfs_attr_get_search_ctx(ni, m);
+	ctx = ntfs_attr_get_search_ctx(ni, m, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx) {
 		err = -ENOMEM;
 		goto err_out;
fs/ntfs/file.c +10 -3
index 6a7b638e523d9..dcb5554ff1b71 100644
@@ -23,6 +23,7 @@
 #include "iomap.h"
 #include "bitmap.h"
 #include "volume.h"
+#include "attrib.h"
 
 #include <linux/filelock.h>
 
@@ -79,6 +80,7 @@ static int ntfs_trim_prealloc(struct inode *vi)
 {
 	struct ntfs_inode *ni = NTFS_I(vi);
 	struct ntfs_volume *vol = ni->vol;
+	struct ntfs_inode *attr_lock_ni;
 	struct runlist_element *rl;
 	s64 aligned_data_size;
 	s64 vcn_ds, vcn_tr;
@@ -87,6 +89,7 @@ static int ntfs_trim_prealloc(struct inode *vi)
 
 	inode_lock(vi);
 	mutex_lock(&ni->mrec_lock);
+	attr_lock_ni = ntfs_attr_list_lock(ni, NTFS_ATTR_CTX_LOCK_WRITE);
 	down_write(&ni->runlist.lock);
 
 	aligned_data_size = round_up(ni->data_size, vol->cluster_size);
@@ -111,7 +114,7 @@ static int ntfs_trim_prealloc(struct inode *vi)
 			ntfs_error(vol->sb, "Preallocated block rollback failed");
 		} else {
 			ni->allocated_size = ntfs_cluster_to_bytes(vol, vcn_tr);
-			err = ntfs_attr_update_mapping_pairs(ni, 0);
+			err = ntfs_attr_update_mapping_pairs_nolock(ni, 0);
 			if (err)
 				ntfs_error(vol->sb,
 					   "Failed to rollback mapping pairs for prealloc");
@@ -120,6 +123,7 @@ static int ntfs_trim_prealloc(struct inode *vi)
 
 out_unlock:
 	up_write(&ni->runlist.lock);
+	ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_WRITE);
 	mutex_unlock(&ni->mrec_lock);
 	inode_unlock(vi);
 
@@ -177,7 +181,7 @@ static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
 		ret = __ntfs_write_inode(vi, 1);
 	write_inode_now(vi, !datasync);
 
-	ctx = ntfs_attr_get_search_ctx(ni, NULL);
+	ctx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx)
 		return -ENOMEM;
 
@@ -1057,6 +1061,7 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t le
 	struct inode *vi = file_inode(file);
 	struct ntfs_inode *ni = NTFS_I(vi);
 	struct ntfs_volume *vol = ni->vol;
+	struct ntfs_inode *attr_lock_ni;
 	int err = 0;
 	loff_t old_size;
 	bool map_locked = false;
@@ -1071,9 +1076,11 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t le
 		return -ENOSPC;
 
 	if (NInoNonResident(ni) && !NInoFullyMapped(ni)) {
+		attr_lock_ni = ntfs_attr_list_lock(ni, NTFS_ATTR_CTX_LOCK_READ);
 		down_write(&ni->runlist.lock);
-		err = ntfs_attr_map_whole_runlist(ni);
+		err = ntfs_attr_map_whole_runlist_nolock(ni);
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 		if (err)
 			return err;
 	}
fs/ntfs/index.c +3 -2
index c5f2cf75b750e..36a70aaa9e2de 100644
@@ -575,7 +575,7 @@ static struct index_root *ntfs_ir_lookup(struct ntfs_inode *ni, __le16 *name,
 	struct index_root *ir = NULL;
 
 	ntfs_debug("Entering\n");
-	*ctx = ntfs_attr_get_search_ctx(ni, NULL);
+	*ctx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_WRITE);
 	if (!*ctx) {
 		ntfs_error(ni->vol->sb, "%s, Failed to get search context", __func__);
 		return NULL;
@@ -1332,7 +1332,8 @@ static int ntfs_ir_reparent(struct ntfs_index_context *icx)
 	 * When there is no space to build a non-resident
 	 * index, we may have to move the root to an extent
 	 */
-	if ((ret == -ENOSPC) && (ctx->al_entry || !ntfs_inode_add_attrlist(icx->idx_ni))) {
+	if ((ret == -ENOSPC) &&
+	    (ctx->al_entry || !ntfs_inode_add_attrlist_nolock(icx->idx_ni))) {
 		ntfs_attr_put_search_ctx(ctx);
 		ctx = NULL;
 		ir = ntfs_ir_lookup(icx->idx_ni, icx->name, icx->name_len, &ctx);
fs/ntfs/inode.c +71 -17

252 lines not on this page · raw message

fs/ntfs/inode.h +7 -0
index 9aacd5787ffea..4d0fbd53e1c4a 100644
@@ -10,6 +10,8 @@
 #ifndef _LINUX_NTFS_INODE_H
 #define _LINUX_NTFS_INODE_H
 
+#include <linux/rwsem.h>
+
 #include "debug.h"
 
 enum ntfs_inode_mutex_lock_class {
@@ -66,6 +68,7 @@ enum ntfs_inode_mutex_lock_class {
  * Attribute list support (only for use by the attribute lookup
  * functions). Setup during read_inode for all inodes with attribute
  * lists. Only valid if NI_AttrList is set in state.
+ * @attr_list_lock: Serializes attribute list pointer updates with lookups.
  * @attr_list_size: Length of attribute list value in bytes.
  * @attr_list: Attribute list value itself.
  *
@@ -118,6 +121,7 @@ struct ntfs_inode {
 	int folio_ofs;
 	s64 mft_lcn[2];
 	unsigned int mft_lcn_count;
+	struct rw_semaphore attr_list_lock;
 	u32 attr_list_size;
 	u8 *attr_list;
 	union {
@@ -336,9 +340,12 @@ int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
 
 int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni);
 int __ntfs_write_inode(struct inode *vi, int sync);
+int ntfs_inode_attach_all_extents_nolock(struct ntfs_inode *ni);
 int ntfs_inode_attach_all_extents(struct ntfs_inode *ni);
+int ntfs_inode_add_attrlist_nolock(struct ntfs_inode *ni);
 int ntfs_inode_add_attrlist(struct ntfs_inode *ni);
 void ntfs_destroy_ext_inode(struct ntfs_inode *ni);
+int ntfs_inode_free_space_nolock(struct ntfs_inode *ni, int size);
 int ntfs_inode_free_space(struct ntfs_inode *ni, int size);
 s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf);
 s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf,
fs/ntfs/iomap.c +21 -2
index 52eecf5cb256a..3af2c6d32b0f7 100644
@@ -95,7 +95,7 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
 	else
 		base_ni = ni;
 
-	ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
+	ctx = ntfs_attr_get_search_ctx(base_ni, NULL, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx) {
 		err = -ENOMEM;
 		goto out;
@@ -187,6 +187,7 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
 		bool need_unwritten)
 {
 	struct ntfs_inode *ni = NTFS_I(inode);
+	struct ntfs_inode *attr_lock_ni;
 	s64 vcn;
 	s64 lcn;
 	struct runlist_element *rl;
@@ -197,20 +198,24 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
 	vcn = ntfs_bytes_to_cluster(vol, offset);
 	vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset);
 
+	attr_lock_ni = ntfs_attr_list_lock(ni, NTFS_ATTR_CTX_LOCK_READ);
 	down_write(&ni->runlist.lock);
 	rl = ntfs_attr_vcn_to_rl(ni, vcn, &lcn);
 	if (IS_ERR(rl)) {
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 		return PTR_ERR(rl);
 	}
 
 	if (flags & IOMAP_REPORT) {
 		if (lcn < LCN_HOLE) {
 			up_write(&ni->runlist.lock);
+			ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 			return -ENOENT;
 		}
 	} else if (lcn < LCN_ENOENT) {
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 		return -EINVAL;
 	}
 
@@ -238,6 +243,7 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
 				"runlist(vcn : %lld, length : %lld, lcn : %lld) is corrupted\n",
 				rl->vcn, rl->length, rl->lcn);
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 		return -EIO;
 	}
 
@@ -246,6 +252,7 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
 	else
 		iomap->length = length;
 	up_write(&ni->runlist.lock);
+	ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 
 	if (!(flags & IOMAP_ZERO) &&
 			iomap->type == IOMAP_MAPPED &&
@@ -365,6 +372,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
 {
 	struct ntfs_inode *ni = NTFS_I(inode);
 	struct ntfs_volume *vol = ni->vol;
+	struct ntfs_inode *attr_lock_ni;
 	loff_t vcn_ofs, rl_length;
 	struct runlist_element *rl, *rlc;
 	bool is_retry = false;
@@ -390,12 +398,14 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
 	}
 	up_read(&ni->runlist.lock);
 
+	attr_lock_ni = ntfs_attr_list_lock(ni, NTFS_ATTR_CTX_LOCK_READ);
 	down_write(&ni->runlist.lock);
 remap_rl:
 	/* Seek to element containing target vcn. */
 	rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
 	if (IS_ERR(rl)) {
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 		mutex_unlock(&ni->mrec_lock);
 		return -EIO;
 	}
@@ -415,6 +425,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
 				"runlist(vcn : %lld, length : %lld) is corrupted\n",
 				rl->vcn, rl->length);
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 		mutex_unlock(&ni->mrec_lock);
 		return -EIO;
 	}
@@ -429,6 +440,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
 			if (max_clu_count < 0) {
 				err = max_clu_count;
 				up_write(&ni->runlist.lock);
+				ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 				mutex_unlock(&ni->mrec_lock);
 				return err;
 			}
@@ -444,6 +456,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
 					GFP_NOFS);
 			if (!rlc) {
 				up_write(&ni->runlist.lock);
+				ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 				mutex_unlock(&ni->mrec_lock);
 				return -ENOMEM;
 			}
@@ -461,6 +474,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
 			if (IS_ERR(rl)) {
 				ntfs_error(vol->sb, "Failed to merge runlists");
 				up_write(&ni->runlist.lock);
+				ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 				mutex_unlock(&ni->mrec_lock);
 				kvfree(rlc);
 				return PTR_ERR(rl);
@@ -471,6 +485,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
 			ni->i_dealloc_clusters += max_clu_count;
 		}
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 		mutex_unlock(&ni->mrec_lock);
 
 		if (lcn < LCN_DELALLOC)
@@ -523,6 +538,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
 		}
 	} else {
 		up_write(&ni->runlist.lock);
+		ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 		mutex_unlock(&ni->mrec_lock);
 
 		iomap->type = IOMAP_MAPPED;
@@ -549,6 +565,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
 {
 	struct ntfs_inode *ni = NTFS_I(inode);
 	struct ntfs_volume *vol = ni->vol;
+	struct ntfs_inode *attr_lock_ni;
 	loff_t vcn_ofs, rl_length;
 	s64 vcn, start_lcn, lcn_count;
 	bool balloc = false, update_mp;
@@ -561,11 +578,13 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
 
 	update_mp = ntfs_iomap_flags & (NTFS_IOMAP_FLAGS_DIO | NTFS_IOMAP_FLAGS_MKWRITE) ||
 			NInoAttr(ni) || ni->mft_no < FILE_first_user;
+	attr_lock_ni = ntfs_attr_list_lock(ni, NTFS_ATTR_CTX_LOCK_WRITE);
 	down_write(&ni->runlist.lock);
 	err = ntfs_attr_map_cluster(ni, vcn, &start_lcn, &lcn_count,
 			max_clu_count, &balloc, update_mp,
 			ntfs_iomap_flags & NTFS_IOMAP_FLAGS_WRITEBACK);
 	up_write(&ni->runlist.lock);
+	ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_WRITE);
 	mutex_unlock(&ni->mrec_lock);
 	if (err) {
 		ni->i_dealloc_clusters = 0;
@@ -637,7 +656,7 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
 	int err = 0;
 	char *kattr;
 
-	ctx = ntfs_attr_get_search_ctx(ni, NULL);
+	ctx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx) {
 		err = -ENOMEM;
 		goto out;
fs/ntfs/logfile.c +4 -0
index 024ddee42dc8f..bf967ef8ab68c 100644
@@ -643,6 +643,7 @@ bool ntfs_empty_logfile(struct inode *log_vi)
 	struct ntfs_inode *log_ni = NTFS_I(log_vi);
 	struct ntfs_volume *vol = log_ni->vol;
 	struct super_block *sb = vol->sb;
+	struct ntfs_inode *attr_lock_ni;
 	struct runlist_element *rl;
 	unsigned long flags;
 	int err;
@@ -669,6 +670,7 @@ bool ntfs_empty_logfile(struct inode *log_vi)
 			vol->cluster_size_bits;
 	read_unlock_irqrestore(&log_ni->size_lock, flags);
 	truncate_inode_pages(log_vi->i_mapping, 0);
+	attr_lock_ni = ntfs_attr_list_lock(log_ni, NTFS_ATTR_CTX_LOCK_READ);
 	down_write(&log_ni->runlist.lock);
 	rl = log_ni->runlist.rl;
 	if (unlikely(!rl || vcn < rl->vcn || !rl->length)) {
@@ -755,6 +757,7 @@ bool ntfs_empty_logfile(struct inode *log_vi)
 		} while (start < end);
 	} while ((++rl)->vcn < end_vcn);
 	up_write(&log_ni->runlist.lock);
+	ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 	kvfree(empty_buf);
 	kfree(ra);
 	truncate_inode_pages(log_vi->i_mapping, 0);
@@ -774,6 +777,7 @@ bool ntfs_empty_logfile(struct inode *log_vi)
 	kvfree(empty_buf);
 	kfree(ra);
 	up_write(&log_ni->runlist.lock);
+	ntfs_attr_list_unlock(attr_lock_ni, NTFS_ATTR_CTX_LOCK_READ);
 	ntfs_error(sb, "Failed to fill LogFile with 0xff bytes (error %d).",
 			-err);
 	return false;
fs/ntfs/mft.c +46 -17

251 lines not on this page · raw message

fs/ntfs/namei.c +3 -3
index 78c159519f9ce..cd1468960ca43 100644
@@ -267,7 +267,7 @@ static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
 				ctx = NULL;
 				goto err_out;
 			}
-			ctx = ntfs_attr_get_search_ctx(ni, m);
+			ctx = ntfs_attr_get_search_ctx(ni, m, NTFS_ATTR_CTX_LOCK_READ);
 			if (unlikely(!ctx)) {
 				err = -ENOMEM;
 				goto err_out;
@@ -859,7 +859,7 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
 	 * If filename in DOS or in WIN32 namespace, then remove DOS name first,
 	 * only then remove WIN32 name.
 	 */
-	actx = ntfs_attr_get_search_ctx(ni, NULL);
+	actx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_WRITE);
 	if (!actx) {
 		ntfs_error(sb, "%s, Failed to get search context", __func__);
 		if (need_lock) {
@@ -1607,7 +1607,7 @@ static struct dentry *ntfs_get_parent(struct dentry *child_dent)
 	if (IS_ERR(mrec))
 		return ERR_CAST(mrec);
 	/* Find the first file name attribute in the mft record. */
-	ctx = ntfs_attr_get_search_ctx(ni, mrec);
+	ctx = ntfs_attr_get_search_ctx(ni, mrec, NTFS_ATTR_CTX_LOCK_READ);
 	if (unlikely(!ctx)) {
 		unmap_mft_record(ni);
 		return ERR_PTR(-ENOMEM);
fs/ntfs/super.c +3 -3
index 8abe7bee4c0d9..6af24fbeb918d 100644
@@ -380,7 +380,7 @@ static int ntfs_write_volume_flags(struct ntfs_volume *vol, const __le16 flags)
 	if (vol->vol_flags == flags)
 		goto done;
 
-	ctx = ntfs_attr_get_search_ctx(ni, NULL);
+	ctx = ntfs_attr_get_search_ctx(ni, NULL, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx) {
 		err = -ENOMEM;
 		goto put_unm_err_out;
@@ -476,7 +476,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 	}
 
 	mutex_lock(&vol_ni->mrec_lock);
-	ctx = ntfs_attr_get_search_ctx(vol_ni, NULL);
+	ctx = ntfs_attr_get_search_ctx(vol_ni, NULL, NTFS_ATTR_CTX_LOCK_WRITE);
 	if (!ctx) {
 		ret = -ENOMEM;
 		goto out;
@@ -1481,7 +1481,7 @@ static bool load_system_files(struct ntfs_volume *vol)
 		goto volume_failed;
 	}
 
-	ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m);
+	ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m, NTFS_ATTR_CTX_LOCK_READ);
 	if (!ctx) {
 		ntfs_error(sb, "Failed to get attribute search context.");
 		goto get_ctx_vol_failed;
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi Cen,

While running generic/013 from xfstests, I observed the following hung task warning:

[  492.533956] INFO: task fsstress:934 blocked for more than 122 seconds.
[  492.534445]       Tainted: G           O        7.1.0-g81fe702ff176 #2
[  492.534870] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs"
disables this message.
[  492.535914] INFO: task fsstress:934 <reader> blocked on an
rw-semaphore likely owned by task fsstress:934 <writer>
[  492.536788] INFO: lockdep is turned off.
# ps -eo pid,comm,stat | grep " D"
    460 kworker/u16:4+f D
    934 fsstress        D+
# cat /proc/934/stack
[<0>] ntfs_attr_list_lock+0x2a/0x60 [ntfs]
[<0>] ntfs_attr_get_search_ctx+0x97/0x100 [ntfs]
[<0>] ntfs_resident_attr_resize+0x64/0x610 [ntfs]
[<0>] ntfs_ir_truncate+0x82/0x120 [ntfs]
[<0>] ntfs_ie_add+0x35/0x200 [ntfs]
[<0>] ntfs_index_add_filename+0xca/0x120 [ntfs]
[<0>] __ntfs_create+0xa9a/0xd80 [ntfs]
[<0>] ntfs_create+0xbd/0x170 [ntfs]
[<0>] path_openat+0x1014/0x1400
[<0>] do_file_open+0xdf/0x1a0
[<0>] do_sys_openat2+0x7b/0xe0
[<0>] __x64_sys_creat+0x45/0x70
[<0>] do_syscall_64+0x101/0x6a0
[<0>] entry_SYSCALL_64_after_hwframe+0x77/0x7f
# cat /proc/460/stack
[<0>] __ntfs_write_inode+0x7b/0x6b0 [ntfs]
[<0>] __writeback_single_inode+0x3f1/0x580
[<0>] writeback_sb_inodes+0x25b/0x8c0
[<0>] __writeback_inodes_wb+0x47/0xf0
[<0>] wb_writeback+0x216/0x440
[<0>] wb_workfn+0x3e6/0x510
[<0>] process_one_work+0x200/0x5e0
[<0>] worker_thread+0x1a0/0x370
[<0>] kthread+0xef/0x120
[<0>] ret_from_fork+0x270/0x330
[<0>] ret_from_fork_asm+0x1a/0x30
author
#2 lore raw

Hi, Hyunchul

Quoted 34 lines · Hyunchul Lee <[email protected]> 于2026年7月3日周五 09:41写道:

Hi Cen,

While running generic/013 from xfstests, I observed the following hung task warning:

[  492.533956] INFO: task fsstress:934 blocked for more than 122 seconds.
[  492.534445]       Tainted: G           O        7.1.0-g81fe702ff176 #2
[  492.534870] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs"
disables this message.
[  492.535914] INFO: task fsstress:934 <reader> blocked on an
rw-semaphore likely owned by task fsstress:934 <writer>
[  492.536788] INFO: lockdep is turned off.
# ps -eo pid,comm,stat | grep " D"
    460 kworker/u16:4+f D
    934 fsstress        D+
# cat /proc/934/stack
[<0>] ntfs_attr_list_lock+0x2a/0x60 [ntfs]
[<0>] ntfs_attr_get_search_ctx+0x97/0x100 [ntfs]
[<0>] ntfs_resident_attr_resize+0x64/0x610 [ntfs]
[<0>] ntfs_ir_truncate+0x82/0x120 [ntfs]
[<0>] ntfs_ie_add+0x35/0x200 [ntfs]
[<0>] ntfs_index_add_filename+0xca/0x120 [ntfs]
[<0>] __ntfs_create+0xa9a/0xd80 [ntfs]
[<0>] ntfs_create+0xbd/0x170 [ntfs]
[<0>] path_openat+0x1014/0x1400
[<0>] do_file_open+0xdf/0x1a0
[<0>] do_sys_openat2+0x7b/0xe0
[<0>] __x64_sys_creat+0x45/0x70
[<0>] do_syscall_64+0x101/0x6a0
[<0>] entry_SYSCALL_64_after_hwframe+0x77/0x7f
Quoted 12 lines
# cat /proc/460/stack
[<0>] __ntfs_write_inode+0x7b/0x6b0 [ntfs]
[<0>] __writeback_single_inode+0x3f1/0x580
[<0>] writeback_sb_inodes+0x25b/0x8c0
[<0>] __writeback_inodes_wb+0x47/0xf0
[<0>] wb_writeback+0x216/0x440
[<0>] wb_workfn+0x3e6/0x510
[<0>] process_one_work+0x200/0x5e0
[<0>] worker_thread+0x1a0/0x370
[<0>] kthread+0xef/0x120
[<0>] ret_from_fork+0x270/0x330
[<0>] ret_from_fork_asm+0x1a/0x30

Thanks for the review and test.

I tried to solve this crash, then kept refining the locking after testing it with xfstests. During that process I ran into more and more issues, which forced me to keep changing and expanding the patch. As a result, the current patch has become quite large and complex, although I have not found new issues with the current version under xfstests.

However, this version was also prepared with LLM assistance, and although I reviewed and tested the result, I am not fully confident that this is already the best or complete solution. I am therefore sending it as an RFC first. I would appreciate your review on whether this direction and lock ordering look reasonable, or whether you would prefer a smaller/different approach. It would also be great if you could help solve this together; I may have made it more complicated than it needed to be.

Best Regards,
Cen Zhang

maintainer
#3 lore raw

2026년 7월 5일 (일) 오전 12:40, Cen Zhang <[email protected]>님이 작성:

Quoted 59 lines

Hi, Hyunchul

Quoted 34 lines · Hyunchul Lee <[email protected]> 于2026年7月3日周五 09:41写道:

Hi Cen,

While running generic/013 from xfstests, I observed the following hung task warning:

[  492.533956] INFO: task fsstress:934 blocked for more than 122 seconds.
[  492.534445]       Tainted: G           O        7.1.0-g81fe702ff176 #2
[  492.534870] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs"
disables this message.
[  492.535914] INFO: task fsstress:934 <reader> blocked on an
rw-semaphore likely owned by task fsstress:934 <writer>
[  492.536788] INFO: lockdep is turned off.
# ps -eo pid,comm,stat | grep " D"
    460 kworker/u16:4+f D
    934 fsstress        D+
# cat /proc/934/stack
[<0>] ntfs_attr_list_lock+0x2a/0x60 [ntfs]
[<0>] ntfs_attr_get_search_ctx+0x97/0x100 [ntfs]
[<0>] ntfs_resident_attr_resize+0x64/0x610 [ntfs]
[<0>] ntfs_ir_truncate+0x82/0x120 [ntfs]
[<0>] ntfs_ie_add+0x35/0x200 [ntfs]
[<0>] ntfs_index_add_filename+0xca/0x120 [ntfs]
[<0>] __ntfs_create+0xa9a/0xd80 [ntfs]
[<0>] ntfs_create+0xbd/0x170 [ntfs]
[<0>] path_openat+0x1014/0x1400
[<0>] do_file_open+0xdf/0x1a0
[<0>] do_sys_openat2+0x7b/0xe0
[<0>] __x64_sys_creat+0x45/0x70
[<0>] do_syscall_64+0x101/0x6a0
[<0>] entry_SYSCALL_64_after_hwframe+0x77/0x7f
Quoted 12 lines
# cat /proc/460/stack
[<0>] __ntfs_write_inode+0x7b/0x6b0 [ntfs]
[<0>] __writeback_single_inode+0x3f1/0x580
[<0>] writeback_sb_inodes+0x25b/0x8c0
[<0>] __writeback_inodes_wb+0x47/0xf0
[<0>] wb_writeback+0x216/0x440
[<0>] wb_workfn+0x3e6/0x510
[<0>] process_one_work+0x200/0x5e0
[<0>] worker_thread+0x1a0/0x370
[<0>] kthread+0xef/0x120
[<0>] ret_from_fork+0x270/0x330
[<0>] ret_from_fork_asm+0x1a/0x30

Thanks for the review and test.

I tried to solve this crash, then kept refining the locking after testing it with xfstests. During that process I ran into more and more issues, which forced me to keep changing and expanding the patch. As a result, the current patch has become quite large and complex, although I have not found new issues with the current version under xfstests.

Quoted 7 lines

However, this version was also prepared with LLM assistance, and although I reviewed and tested the result, I am not fully confident that this is already the best or complete solution. I am therefore sending it as an RFC first. I would appreciate your review on whether this direction and lock ordering look reasonable, or whether you would prefer a smaller/different approach. It would also be great if you could help solve this together; I may have made it more complicated than it needed to be.

Could you summarize what issues were found and how they were resolved?

There are so many changes in the v5 patch, it is difficult to understand the overall picture.

Quoted 3 lines

Best Regards,
Cen Zhang

Signature · 2 lines
Thanks,
Hyunchul
author
#4 lore raw

Hi, Hyunchul

Quoted 6 lines

Could you summarize what issues were found and how they were resolved?

There are so many changes in the v5 patch, it is difficult to understand the overall picture.

Thanks.

I attempted to create the following summary with the help of LLM.

Compared with v4, the main changes are not for a different original crash, but for the locking problems I found while trying to validate the same attr-list lifetime fix with xfstests.

The rough summary is:

1. generic/013 self-deadlock

The stack you reported showed a path already holding attr_list_lock for writing and then re-entering ntfs_attr_get_search_ctx(), which tried to take the same rwsem again for reading.

In v4 I tried to address that by adding LOCK_NONE/nolock paths for callers that already control attr_list_lock.

2. Nested index/$BITMAP locking

Later, generic/467 reported a recursive locking problem. The nested directory/index path propagated nested attr-list locking to the index search context, but the later $BITMAP attribute inode open still used the normal ntfs_attr_iget() path.

v5 propagates the nested lock mode through the index open and the related $BITMAP attribute open.

3. attr_list_lock vs runlist.lock ordering

Some paths reached attr-list lookup/update while runlist.lock was already held, or updated mapping pairs while still depending on the runlist lock. That conflicted with the intended order:

mrec_lock => attr_list_lock => runlist.lock

v5 adds explicit nolock/runlist-locked variants so callers that already hold one side do not acquire the locks again in the wrong order.

4. Mapping-pairs update paths

Some mapping-pairs updates needed to be moved out of runlist-locked sections. v5 snapshots the runlist where needed and defers mapping-pairs updates in write/fallocate/writeback paths until after runlist.lock is dropped.

5. Propagating the lock mode through helper layers

Once the search-context lock mode was used to protect ctx->al_entry for the whole context lifetime, related helpers also had to carry the same mode:
ntfs_attr_iget(), ntfs_index_iget(), iomap/aops, inode attr read/write, and some index/reparse paths. This is the main reason v5 became much larger than v4.

So the high-level picture is: v4 fixed the first obvious re-entry/deadlock case, but wider xfstests coverage exposed that the lock mode had to be carried through more NTFS helper paths to keep the attr-list cursor lifetime and lock ordering consistent. That is why I sent v5 as RFC rather than as a final patch.

Best regards
Cen Zhang.