Skip to contentSkip to conversation

iomap/ntfs: remove over-strict inline data check and optimize ntfs

PATCH Superseded Namjae Jeon 4 participants 8 messages Apr 23 – Apr 27

Subsystems (3)
lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

cover iomap/ntfs: remove over-strict inline data check and optimize ntfs

This patch series aims to remove an unnecessarily strict boundary check for IOMAP_INLINE data and, as a result, optimize the NTFS driver's handling of resident attributes. The first patch removes iomap_inline_data_valid() and its callers in the iomap core. Currently, this check prevents inline_data from crossing a PAGE_SIZE boundary. If the filesystem provides a valid mapping, iomap should trust it regardless of page boundaries.

The second patch updates the NTFS driver to take advantage of this change. Previously, NTFS performed redundant page allocations and memcpy operations to ensure page-aligned inline data. By removing these steps, we simplify the NTFS iomap implementation and improve performance for resident file I/O.

Namjae Jeon (2):
iomap: remove over-strict inline data boundary check
ntfs: use direct pointer for inline data to avoid redundant allocation

 fs/iomap/buffered-io.c |  1 -
 fs/iomap/direct-io.c   |  3 --
 fs/ntfs/iomap.c        | 76 ++++--------------------------------------
 include/linux/iomap.h  | 10 ------
 4 files changed, 6 insertions(+), 84 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH 1/2 iomap: remove over-strict inline data boundary check

The current iomap_inline_data_valid() check ensures that inline data does not cross a PAGE_SIZE boundary. However, this is an unnecessarily strict constraint. If a filesystem provides a valid iomap::inline_data pointer and iomap::length, we should trust that the caller has mapped sufficient memory for the range, even if it spans across page boundaries. Removing this check allows filesystems to point directly to their internal data structures without forced page-alignment or additional redundant allocations. This remove iomap_inline_data_valid() and its callers in buffered and direct I/O paths.

---
 fs/iomap/buffered-io.c |  1 -
 fs/iomap/direct-io.c   |  3 ---
 include/linux/iomap.h  | 10 ----------
 3 files changed, 14 deletions(-)
fs/iomap/buffered-io.c +0 -1
index d7b648421a70..c4c5bddcbe3a 100644
@@ -1058,7 +1058,6 @@ static bool iomap_write_end_inline(const struct iomap_iter *iter,
 	void *addr;
 
 	WARN_ON_ONCE(!folio_test_uptodate(folio));
-	BUG_ON(!iomap_inline_data_valid(iomap));
 
 	if (WARN_ON_ONCE(!iomap->inline_data))
 		return false;
fs/iomap/direct-io.c +0 -3
index b0a6549b3848..a4459c5abb07 100644
@@ -603,9 +603,6 @@ static int iomap_dio_inline_iter(struct iomap_iter *iomi, struct iomap_dio *dio)
 	if (WARN_ON_ONCE(!inline_data))
 		return -EIO;
 
-	if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap)))
-		return -EIO;
-
 	if (dio->flags & IOMAP_DIO_WRITE) {
 		loff_t size = iomi->inode->i_size;
 
include/linux/iomap.h +0 -10
index 2c5685adf3a9..52bfd6783417 100644
@@ -142,16 +142,6 @@ static inline void *iomap_inline_data(const struct iomap *iomap, loff_t pos)
 	return iomap->inline_data + pos - iomap->offset;
 }
 
-/*
- * Check if the mapping's length is within the valid range for inline data.
- * This is used to guard against accessing data beyond the page inline_data
- * points at.
- */
-static inline bool iomap_inline_data_valid(const struct iomap *iomap)
-{
-	return iomap->length <= PAGE_SIZE - offset_in_page(iomap->inline_data);
-}
-
 /*
  * When get_folio succeeds, put_folio will always be called to do any
  * cleanup work necessary.  put_folio is responsible for unlocking and putting
Signature · 1 line
2.25.1
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] iomap: remove over-strict inline data boundary check

In reply to Namjae Jeon
Quoted 10 lines · Namjae Jeon

The current iomap_inline_data_valid() check ensures that inline data does not cross a PAGE_SIZE boundary. However, this is an unnecessarily strict constraint. If a filesystem provides a valid iomap::inline_data pointer and iomap::length, we should trust that the caller has mapped sufficient memory for the range, even if it spans across page boundaries. Removing this check allows filesystems to point directly to their internal data structures without forced page-alignment or additional redundant allocations. This remove iomap_inline_data_valid() and its callers in buffered and direct I/O paths.

Quoted 2 lines

Thanks,
Gao Xiang

author maintainer
#4 lore raw

Re: [PATCH 1/2] iomap: remove over-strict inline data boundary check

Quoted 14 lines · Gao Xiang
Quoted 10 lines · Namjae Jeon

The current iomap_inline_data_valid() check ensures that inline data does not cross a PAGE_SIZE boundary. However, this is an unnecessarily strict constraint. If a filesystem provides a valid iomap::inline_data pointer and iomap::length, we should trust that the caller has mapped sufficient memory for the range, even if it spans across page boundaries. Removing this check allows filesystems to point directly to their internal data structures without forced page-alignment or additional redundant allocations. This remove iomap_inline_data_valid() and its callers in buffered and direct I/O paths.

Quoted 4 lines
Quoted 2 lines

Hi Christian,
Christoph and Gao have reviewed this patch. Could you please take this into your queue?

Thanks!

author maintainer
#5 lore raw

PATCH 2/2 ntfs: use direct pointer for inline data to avoid redundant allocation

In reply to Namjae Jeon

Previously, NTFS used page allocation for IOMAP_INLINE to ensure that the inline_data pointer was page-aligned, avoiding strict boundary checks in the iomap core. Since the previous patch has removed the over-strict PAGE_SIZE boundary check in iomap, NTFS can now safely point iomap::inline_data directly to the MFT record.
This change eliminates redundant memory allocations and memcpy operations in both read and write paths. It also simplifies the iomap_ops by removing the need for a iomap_end callback that was previously used to free the temporary page.

---
 fs/ntfs/iomap.c | 76 ++++---------------------------------------------
 1 file changed, 6 insertions(+), 70 deletions(-)
fs/ntfs/iomap.c +6 -70
index 7a170df39e72..ca8e221e5b7b 100644
@@ -89,7 +89,6 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
 	u32 attr_len;
 	int err = 0;
 	char *kattr;
-	struct page *ipage;
 
 	if (NInoAttr(ni))
 		base_ni = ni->ext.base_ntfs_ino;
@@ -130,18 +129,10 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
 
 	kattr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset);
 
-	ipage = alloc_page(GFP_NOFS | __GFP_ZERO);
-	if (!ipage) {
-		err = -ENOMEM;
-		goto out;
-	}
-
-	memcpy(page_address(ipage), kattr, attr_len);
 	iomap->type = IOMAP_INLINE;
-	iomap->inline_data = page_address(ipage);
+	iomap->inline_data = kattr;
 	iomap->offset = 0;
 	iomap->length = attr_len;
-	iomap->private = ipage;
 
 out:
 	if (ctx)
@@ -286,21 +277,8 @@ static int ntfs_read_iomap_begin(struct inode *inode, loff_t offset, loff_t leng
 			srcmap, true);
 }
 
-static int ntfs_read_iomap_end(struct inode *inode, loff_t pos, loff_t length,
-		ssize_t written, unsigned int flags, struct iomap *iomap)
-{
-	if (iomap->type == IOMAP_INLINE) {
-		struct page *ipage = iomap->private;
-
-		put_page(ipage);
-	}
-
-	return written;
-}
-
 const struct iomap_ops ntfs_read_iomap_ops = {
 	.iomap_begin = ntfs_read_iomap_begin,
-	.iomap_end = ntfs_read_iomap_end,
 };
 
 /*
@@ -358,7 +336,6 @@ static const struct iomap_ops ntfs_zero_read_iomap_ops = {
 
 const struct iomap_ops ntfs_seek_iomap_ops = {
 	.iomap_begin = ntfs_seek_iomap_begin,
-	.iomap_end = ntfs_read_iomap_end,
 };
 
 int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length)
@@ -659,7 +636,6 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
 	u32 attr_len;
 	int err = 0;
 	char *kattr;
-	struct page *ipage;
 
 	ctx = ntfs_attr_get_search_ctx(ni, NULL);
 	if (!ctx) {
@@ -680,24 +656,18 @@ static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset,
 	attr_len = le32_to_cpu(a->data.resident.value_length);
 	kattr = (u8 *)a + le16_to_cpu(a->data.resident.value_offset);
 
-	ipage = alloc_page(GFP_NOFS | __GFP_ZERO);
-	if (!ipage) {
-		err = -ENOMEM;
-		goto out;
-	}
-
-	memcpy(page_address(ipage), kattr, attr_len);
 	iomap->type = IOMAP_INLINE;
-	iomap->inline_data = page_address(ipage);
+	iomap->inline_data = kattr;
 	iomap->offset = 0;
-	/* iomap requires there is only one INLINE_DATA extent */
 	iomap->length = attr_len;
-	iomap->private = ipage;
 
 out:
 	if (ctx)
 		ntfs_attr_put_search_ctx(ctx);
-	mutex_unlock(&ni->mrec_lock);
+
+	if (err)
+		mutex_unlock(&ni->mrec_lock);
+
 	return err;
 }
 
@@ -778,43 +748,9 @@ static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos,
 					 unsigned int flags, struct iomap *iomap)
 {
 	struct ntfs_inode *ni = NTFS_I(inode);
-	struct ntfs_attr_search_ctx *ctx;
-	u32 attr_len;
-	int err;
-	char *kattr;
-	struct page *ipage = iomap->private;
-
-	mutex_lock(&ni->mrec_lock);
-	ctx = ntfs_attr_get_search_ctx(ni, NULL);
-	if (!ctx) {
-		written = -ENOMEM;
-		mutex_unlock(&ni->mrec_lock);
-		return written;
-	}
-
-	err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
-			       CASE_SENSITIVE, 0, NULL, 0, ctx);
-	if (err) {
-		if (err == -ENOENT)
-			err = -EIO;
-		written = err;
-		goto err_out;
-	}
 
-	/* The total length of the attribute value. */
-	attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
-	if (pos >= attr_len || pos + written > attr_len)
-		goto err_out;
-
-	kattr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset);
-	memcpy(kattr + pos, iomap_inline_data(iomap, pos), written);
-	mark_mft_record_dirty(ctx->ntfs_ino);
-err_out:
-	ntfs_attr_put_search_ctx(ctx);
-	put_page(ipage);
 	mutex_unlock(&ni->mrec_lock);
 	return written;
-
 }
 
 static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length,
Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH 2/2] ntfs: use direct pointer for inline data to avoid redundant allocation

In reply to Namjae Jeon
Quoted 5 lines · Namjae Jeon

Previously, NTFS used page allocation for IOMAP_INLINE to ensure that the inline_data pointer was page-aligned, avoiding strict boundary checks in the iomap core. Since the previous patch has removed the over-strict PAGE_SIZE boundary check in iomap, NTFS can now safely point iomap::inline_data directly to the MFT record.

Just a note in case it is useful for the commit log: this also removes a pre-existing page leak in ntfs_write_iomap_end_resident(). On the ntfs_attr_get_search_ctx() == NULL path the function returns without releasing iomap->private:

      ctx = ntfs_attr_get_search_ctx(ni, NULL);
      if (!ctx) {
              written = -ENOMEM;
              mutex_unlock(&ni->mrec_lock);
              return written;   /* iomap->private not put_page()'d */
      }

Since this patch drops the page allocation and the iomap_end callback entirely, the leak goes away as a side effect. Worth mentioning in the commit log -- it makes the change a candidate for stable backport even setting aside the optimization angle.

Thanks,
DaeMyung