Skip to contentSkip to conversation

Introduce set/clear_fs_page_private to cleanup code

RFC PATCH In review Guoqing Jiang 7 participants 30 messages Apr 26, 2020 – Apr 27, 2020

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover Introduce set/clear_fs_page_private to cleanup code

Hi,

Based on the previous thread [1], this patchset introduces set_fs_page_private and clear_fs_page_private to replace attach_page_buffers and __clear_page_buffers. Thanks a lot for the constructive suggestion from Matthew and Dave.

And sorry for cross post to different lists since it modifies different subsystems.

[1]. https://lore.kernel.org/linux-fsdevel/[email protected]/

Thanks,
Guoqing

Guoqing Jiang (9):
include/linux/pagemap.h: introduce set/clear_fs_page_private
md: remove __clear_page_buffers and use set/clear_fs_page_private
btrfs: use set/clear_fs_page_private
fs/buffer.c: use set/clear_fs_page_private
f2fs: use set/clear_fs_page_private
iomap: use set/clear_fs_page_private
ntfs: replace attach_page_buffers with set_fs_page_private
orangefs: use set/clear_fs_page_private
buffer_head.h: remove attach_page_buffers

 drivers/md/md-bitmap.c      | 12 ++----------
 fs/btrfs/disk-io.c          |  4 +---
 fs/btrfs/extent_io.c        | 21 ++++++---------------
 fs/btrfs/inode.c            | 17 ++++-------------
 fs/buffer.c                 | 16 ++++------------
 fs/f2fs/f2fs.h              | 11 ++---------
 fs/iomap/buffered-io.c      | 14 +++-----------
 fs/ntfs/aops.c              |  2 +-
 fs/ntfs/mft.c               |  2 +-
 fs/orangefs/inode.c         | 24 ++++++------------------
 include/linux/buffer_head.h |  8 --------
 include/linux/pagemap.h     | 22 ++++++++++++++++++++++
 12 files changed, 52 insertions(+), 101 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH 1/9 include/linux/pagemap.h: introduce set/clear_fs_page_private

The logic in attach_page_buffers and __clear_page_buffers are quite paired, but

1. they are located in different files.

2. attach_page_buffers is implemented in buffer_head.h, so it could be used by other files. But __clear_page_buffers is static function in buffer.c and other potential users can't call the function, md-bitmap even copied the function.

So, introduce the new set/clear_fs_page_private to replace them since they are mostly used in fs layer. With the new pair of function, we will remove the usage of attach_page_buffers and __clear_page_buffers in next patches.

Cc Andrew Morton <[email protected]>
Cc "Darrick J. Wong" <[email protected]>
Cc William Kucharski <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Andreas Gruenbacher <[email protected]>
Cc Yang Shi <[email protected]>
Cc Yafang Shao <[email protected]>
---
 include/linux/pagemap.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
include/linux/pagemap.h +22 -0
index a8f7bd8ea1c6..2a165ea647fe 100644
@@ -205,6 +205,28 @@ static inline int page_cache_add_speculative(struct page *page, int count)
 	return __page_cache_add_speculative(page, count);
 }
 
+static inline void *set_fs_page_private(struct page *page, void *data)
+{
+	get_page(page);
+	set_page_private(page, (unsigned long)data);
+	SetPagePrivate(page);
+
+	return data;
+}
+
+static inline void *clear_fs_page_private(struct page *page)
+{
+	void *data = (void *)page_private(page);
+
+	if (!PagePrivate(page))
+		return NULL;
+	ClearPagePrivate(page);
+	set_page_private(page, 0);
+	put_page(page);
+
+	return data;
+}
+
 #ifdef CONFIG_NUMA
 extern struct page *__page_cache_alloc(gfp_t gfp);
 #else
Signature · 1 line
2.17.1
#2 lore raw

Re: [RFC PATCH 1/9] include/linux/pagemap.h: introduce set/clear_fs_page_private

Why not attach_page_private and clear_page_private as that conveys the use case a little better?

Quoted 11 lines
include/linux/pagemap.h +11 -0
+static inline void *set_fs_page_private(struct page *page, void *data)
+{
+	get_page(page);
+	set_page_private(page, (unsigned long)data);
+	SetPagePrivate(page);
+
+	return data;
+}
+
+static inline void *clear_fs_page_private(struct page *page)
+{

include/linux/pagemap.h static inline int page_cache_add_speculative(struct page *page, int count) · line 228

Quoted 10 lines
include/linux/pagemap.h +10 -0
+	void *data = (void *)page_private(page);
+
+	if (!PagePrivate(page))
+		return NULL;
+	ClearPagePrivate(page);
+	set_page_private(page, 0);
+	put_page(page);
+
+	return data;
+}

Can you add kerneldoc comments describing them, including why we take the refernces? Also what is the point of the return value of set_fs_page_private?

#3 lore raw

Re: [RFC PATCH 1/9] include/linux/pagemap.h: introduce set/clear_fs_page_private

Hi Christoph,

Quoted 2 lines · Christoph Hellwig

Why not attach_page_private and clear_page_private as that conveys the use case a little better?

Yes, thanks for the good suggestion. Will rename them if no one else has new idea ...

Quoted 15 lines
Quoted 15 lines
include/linux/pagemap.h +15 -0
+static inline void *set_fs_page_private(struct page *page, void *data)
+{
+	get_page(page);
+	set_page_private(page, (unsigned long)data);
+	SetPagePrivate(page);
+
+	return data;
+}
+
+static inline void *clear_fs_page_private(struct page *page)
+{
+	void *data = (void *)page_private(page);
+
+	if (!PagePrivate(page))
+		return NULL;
Quoted 8 lines

include/linux/pagemap.h static inline int page_cache_add_speculative(struct page *page, int count) · line 228

Quoted 6 lines
include/linux/pagemap.h +6 -0
+	ClearPagePrivate(page);
+	set_page_private(page, 0);
+	put_page(page);
+
+	return data;
+}

Can you add kerneldoc comments describing them, including why we take the refernces?

Ok, will do.

Quoted 1 line

Also what is the point of the return value of set_fs_page_private?

In this way, iomap_page_create can just return the function, but you don't like this way as you replied, will change the return value to "void".

Thanks,
Guoqing

#4 lore raw

RFC PATCH 2/9 md: remove __clear_page_buffers and use set/clear_fs_page_private

In reply to Guoqing Jiang

After introduce set/clear_fs_page_private in pagemap.h, we can remove the duplicat code and call the new functions.

---
 drivers/md/md-bitmap.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/md/md-bitmap.c +2 -10
index b952bd45bd6a..b935473c82b7 100644
@@ -324,14 +324,6 @@ static void end_bitmap_write(struct buffer_head *bh, int uptodate)
 		wake_up(&bitmap->write_wait);
 }
 
-/* copied from buffer.c */
-static void
-__clear_page_buffers(struct page *page)
-{
-	ClearPagePrivate(page);
-	set_page_private(page, 0);
-	put_page(page);
-}
 static void free_buffers(struct page *page)
 {
 	struct buffer_head *bh;
@@ -345,7 +337,7 @@ static void free_buffers(struct page *page)
 		free_buffer_head(bh);
 		bh = next;
 	}
-	__clear_page_buffers(page);
+	clear_fs_page_private(page);
 	put_page(page);
 }
 
@@ -374,7 +366,7 @@ static int read_page(struct file *file, unsigned long index,
 		ret = -ENOMEM;
 		goto out;
 	}
-	attach_page_buffers(page, bh);
+	set_fs_page_private(page, bh);
 	blk_cur = index << (PAGE_SHIFT - inode->i_blkbits);
 	while (bh) {
 		block = blk_cur;
Signature · 1 line
2.17.1
#5 lore raw

RFC PATCH 3/9 btrfs: use set/clear_fs_page_private

In reply to Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in btrfs.

---
 fs/btrfs/disk-io.c   |  4 +---
 fs/btrfs/extent_io.c | 21 ++++++---------------
 fs/btrfs/inode.c     | 17 ++++-------------
 3 files changed, 11 insertions(+), 31 deletions(-)
fs/btrfs/disk-io.c +1 -3
index a6cb5cbbdb9f..1230863e80f9 100644
@@ -980,9 +980,7 @@ static void btree_invalidatepage(struct page *page, unsigned int offset,
 		btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info,
 			   "page private not zero on page %llu",
 			   (unsigned long long)page_offset(page));
-		ClearPagePrivate(page);
-		set_page_private(page, 0);
-		put_page(page);
+		clear_fs_page_private(page);
 	}
 }
 
fs/btrfs/extent_io.c +6 -15
index 39e45b8a5031..de8c2d5a99db 100644
@@ -3076,22 +3076,16 @@ static int submit_extent_page(unsigned int opf,
 static void attach_extent_buffer_page(struct extent_buffer *eb,
 				      struct page *page)
 {
-	if (!PagePrivate(page)) {
-		SetPagePrivate(page);
-		get_page(page);
-		set_page_private(page, (unsigned long)eb);
-	} else {
+	if (!PagePrivate(page))
+		set_fs_page_private(page, eb);
+	else
 		WARN_ON(page->private != (unsigned long)eb);
-	}
 }
 
 void set_page_extent_mapped(struct page *page)
 {
-	if (!PagePrivate(page)) {
-		SetPagePrivate(page);
-		get_page(page);
-		set_page_private(page, EXTENT_PAGE_PRIVATE);
-	}
+	if (!PagePrivate(page))
+		set_fs_page_private(page, (void *)EXTENT_PAGE_PRIVATE);
 }
 
 static struct extent_map *
@@ -4929,10 +4923,7 @@ static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
 			 * We need to make sure we haven't be attached
 			 * to a new eb.
 			 */
-			ClearPagePrivate(page);
-			set_page_private(page, 0);
-			/* One for the page private */
-			put_page(page);
+			clear_fs_page_private(page);
 		}
 
 		if (mapped)
fs/btrfs/inode.c +4 -13
index 320d1062068d..07871c57ba96 100644
@@ -8303,11 +8303,8 @@ btrfs_readpages(struct file *file, struct address_space *mapping,
 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
 {
 	int ret = try_release_extent_mapping(page, gfp_flags);
-	if (ret == 1) {
-		ClearPagePrivate(page);
-		set_page_private(page, 0);
-		put_page(page);
-	}
+	if (ret == 1)
+		clear_fs_page_private(page);
 	return ret;
 }
 
@@ -8331,11 +8328,9 @@ static int btrfs_migratepage(struct address_space *mapping,
 
 	if (page_has_private(page)) {
 		ClearPagePrivate(page);
-		get_page(newpage);
-		set_page_private(newpage, page_private(page));
+		set_fs_page_private(newpage, (void *)page_private(page));
 		set_page_private(page, 0);
 		put_page(page);
-		SetPagePrivate(newpage);
 	}
 
 	if (PagePrivate2(page)) {
@@ -8458,11 +8453,7 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
 	}
 
 	ClearPageChecked(page);
-	if (PagePrivate(page)) {
-		ClearPagePrivate(page);
-		set_page_private(page, 0);
-		put_page(page);
-	}
+	clear_fs_page_private(page);
 }
 
 /*
Signature · 1 line
2.17.1
#6 lore raw

Re: [RFC PATCH 3/9] btrfs: use set/clear_fs_page_private

Quoted 8 lines · Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in btrfs.

....

fs/btrfs/extent_io.c static int submit_extent_page(unsigned int opf, · line 3088

Quoted 9 lines

void set_page_extent_mapped(struct page *page)
{

fs/btrfs/extent_io.c +2 -5
-	if (!PagePrivate(page)) {
-		SetPagePrivate(page);
-		get_page(page);
-		set_page_private(page, EXTENT_PAGE_PRIVATE);
-	}
+	if (!PagePrivate(page))
+		set_fs_page_private(page, (void *)EXTENT_PAGE_PRIVATE);

Change the definition of EXTENT_PAGE_PRIVATE so the cast is not needed? Nothing ever reads EXTENT_PAGE_PRIVATE; it's only there to set the private flag for other code to check and release the extent mapping reference to the page...

fs/btrfs/inode.c static int btrfs_migratepage(struct address_space *mapping, · line 8334

Quoted 11 lines
fs/btrfs/inode.c +1 -3
@@ -8331,11 +8328,9 @@ static int btrfs_migratepage(struct address_space *mapping,
 
 	if (page_has_private(page)) {
 		ClearPagePrivate(page);
-		get_page(newpage);
-		set_page_private(newpage, page_private(page));
+		set_fs_page_private(newpage, (void *)page_private(page));
 		set_page_private(page, 0);
 		put_page(page);
-		SetPagePrivate(newpage);
 	}

This is just:

		set_fs_page_private(newpage, clear_fs_page_private(page));

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#7 lore raw

Re: [RFC PATCH 3/9] btrfs: use set/clear_fs_page_private

Quoted 6 lines · Dave Chinner
Quoted 6 lines

void set_page_extent_mapped(struct page *page)
{

fs/btrfs/extent_io.c +0 -4
-	if (!PagePrivate(page)) {
-		SetPagePrivate(page);
-		get_page(page);
-		set_page_private(page, EXTENT_PAGE_PRIVATE);
Quoted 8 lines

fs/btrfs/extent_io.c static int submit_extent_page(unsigned int opf, · line 3088

Quoted 3 lines
fs/btrfs/extent_io.c +2 -1
-	}
+	if (!PagePrivate(page))
+		set_fs_page_private(page, (void *)EXTENT_PAGE_PRIVATE);

Change the definition of EXTENT_PAGE_PRIVATE so the cast is not needed? Nothing ever reads EXTENT_PAGE_PRIVATE; it's only there to set the private flag for other code to check and release the extent mapping reference to the page...

IIRC there as a patch on the btrfs list to remove EXTENT_PAGE_PRIVATE, it might be better to not bother changing it. Maybe the btrfs maintainers remember this better.

#8 lore raw

Re: [RFC PATCH 3/9] btrfs: use set/clear_fs_page_private

Quoted 16 lines · Christoph Hellwig
Quoted 6 lines · Dave Chinner
Quoted 6 lines

void set_page_extent_mapped(struct page *page)
{

fs/btrfs/extent_io.c +0 -4
-	if (!PagePrivate(page)) {
-		SetPagePrivate(page);
-		get_page(page);
-		set_page_private(page, EXTENT_PAGE_PRIVATE);
Quoted 8 lines

fs/btrfs/extent_io.c static int submit_extent_page(unsigned int opf, · line 3088

Quoted 3 lines
fs/btrfs/extent_io.c +2 -1
-	}
+	if (!PagePrivate(page))
+		set_fs_page_private(page, (void *)EXTENT_PAGE_PRIVATE);

Change the definition of EXTENT_PAGE_PRIVATE so the cast is not needed? Nothing ever reads EXTENT_PAGE_PRIVATE; it's only there to set the private flag for other code to check and release the extent mapping reference to the page...

Quoted 3 lines

IIRC there as a patch on the btrfs list to remove EXTENT_PAGE_PRIVATE, it might be better to not bother changing it. Maybe the btrfs maintainers remember this better.

The patch removing it is part of patchset adding full iomap support to btrfs,
(https://lore.kernel.org/linux-btrfs/[email protected]/)
but it'll still take some time so I'm OK with using the set_fs_page_private helper and adding the cast to EXTENT_PAGE_PRIVATE definition.

#9 lore raw

Re: [RFC PATCH 3/9] btrfs: use set/clear_fs_page_private

In reply to Dave Chinner
Quoted 11 lines · Dave Chinner
Quoted 8 lines · Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in btrfs.

....

Quoted 13 lines

fs/btrfs/extent_io.c static int submit_extent_page(unsigned int opf, · line 3088

Quoted 9 lines
  void set_page_extent_mapped(struct page *page)
  {
fs/btrfs/extent_io.c +2 -5
-	if (!PagePrivate(page)) {
-		SetPagePrivate(page);
-		get_page(page);
-		set_page_private(page, EXTENT_PAGE_PRIVATE);
-	}
+	if (!PagePrivate(page))
+		set_fs_page_private(page, (void *)EXTENT_PAGE_PRIVATE);

Change the definition of EXTENT_PAGE_PRIVATE so the cast is not needed? Nothing ever reads EXTENT_PAGE_PRIVATE; it's only there to set the private flag for other code to check and release the extent mapping reference to the page...

Not know the code well, so I just make the cast ...

Quoted 12 lines

fs/btrfs/inode.c static int btrfs_migratepage(struct address_space *mapping, · line 8334

Quoted 11 lines
fs/btrfs/inode.c +1 -3
@@ -8331,11 +8328,9 @@ static int btrfs_migratepage(struct address_space *mapping,
 
  	if (page_has_private(page)) {
  		ClearPagePrivate(page);
-		get_page(newpage);
-		set_page_private(newpage, page_private(page));
+		set_fs_page_private(newpage, (void *)page_private(page));
  		set_page_private(page, 0);
  		put_page(page);
-		SetPagePrivate(newpage);
  	}

This is just:

Quoted 2 lines
		set_fs_page_private(newpage, clear_fs_page_private(page));

Thanks a lot! It is more better.

Thanks,
Guoqing

#10 lore raw

RFC PATCH 4/9 fs/buffer.c: use set/clear_fs_page_private

In reply to Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in buffer.c.

---
 fs/buffer.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
fs/buffer.c +4 -12
index a60f60396cfa..1d9b32e77c2b 100644
@@ -123,14 +123,6 @@ void __wait_on_buffer(struct buffer_head * bh)
 }
 EXPORT_SYMBOL(__wait_on_buffer);
 
-static void
-__clear_page_buffers(struct page *page)
-{
-	ClearPagePrivate(page);
-	set_page_private(page, 0);
-	put_page(page);
-}
-
 static void buffer_io_error(struct buffer_head *bh, char *msg)
 {
 	if (!test_bit(BH_Quiet, &bh->b_state))
@@ -906,7 +898,7 @@ link_dev_buffers(struct page *page, struct buffer_head *head)
 		bh = bh->b_this_page;
 	} while (bh);
 	tail->b_this_page = head;
-	attach_page_buffers(page, head);
+	set_fs_page_private(page, head);
 }
 
 static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
@@ -1580,7 +1572,7 @@ void create_empty_buffers(struct page *page,
 			bh = bh->b_this_page;
 		} while (bh != head);
 	}
-	attach_page_buffers(page, head);
+	set_fs_page_private(page, head);
 	spin_unlock(&page->mapping->private_lock);
 }
 EXPORT_SYMBOL(create_empty_buffers);
@@ -2567,7 +2559,7 @@ static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
 			bh->b_this_page = head;
 		bh = bh->b_this_page;
 	} while (bh != head);
-	attach_page_buffers(page, head);
+	set_fs_page_private(page, head);
 	spin_unlock(&page->mapping->private_lock);
 }
 
@@ -3227,7 +3219,7 @@ drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
 		bh = next;
 	} while (bh != head);
 	*buffers_to_free = head;
-	__clear_page_buffers(page);
+	clear_fs_page_private(page);
 	return 1;
 failed:
 	return 0;
Signature · 1 line
2.17.1
#11 lore raw

RFC PATCH 5/9 f2fs: use set/clear_fs_page_private

In reply to Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in f2fs.h.

---
 fs/f2fs/f2fs.h | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
fs/f2fs/f2fs.h +2 -9
index ba470d5687fe..70ad8c51e0fc 100644
@@ -3051,19 +3051,12 @@ static inline void f2fs_set_page_private(struct page *page,
 	if (PagePrivate(page))
 		return;
 
-	get_page(page);
-	SetPagePrivate(page);
-	set_page_private(page, data);
+	set_fs_page_private(page, (void *)data);
 }
 
 static inline void f2fs_clear_page_private(struct page *page)
 {
-	if (!PagePrivate(page))
-		return;
-
-	set_page_private(page, 0);
-	ClearPagePrivate(page);
-	f2fs_put_page(page, 0);
+	clear_fs_page_private(page);
 }
 
 /*
Signature · 1 line
2.17.1
#14 lore raw

RFC PATCH 6/9 iomap: use set/clear_fs_page_private

In reply to Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in iomap.

Cc Christoph Hellwig <[email protected]>
Cc "Darrick J. Wong" <[email protected]>
---
 fs/iomap/buffered-io.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
fs/iomap/buffered-io.c +3 -11
index 89e21961d1ad..cc48bf4f1193 100644
@@ -59,24 +59,18 @@ iomap_page_create(struct inode *inode, struct page *page)
 	 * migrate_page_move_mapping() assumes that pages with private data have
 	 * their count elevated by 1.
 	 */
-	get_page(page);
-	set_page_private(page, (unsigned long)iop);
-	SetPagePrivate(page);
-	return iop;
+	return (struct iomap_page *)set_fs_page_private(page, iop);
 }
 
 static void
 iomap_page_release(struct page *page)
 {
-	struct iomap_page *iop = to_iomap_page(page);
+	struct iomap_page *iop = clear_fs_page_private(page);
 
 	if (!iop)
 		return;
 	WARN_ON_ONCE(atomic_read(&iop->read_count));
 	WARN_ON_ONCE(atomic_read(&iop->write_count));
-	ClearPagePrivate(page);
-	set_page_private(page, 0);
-	put_page(page);
 	kfree(iop);
 }
 
@@ -556,11 +550,9 @@ iomap_migrate_page(struct address_space *mapping, struct page *newpage,
 
 	if (page_has_private(page)) {
 		ClearPagePrivate(page);
-		get_page(newpage);
-		set_page_private(newpage, page_private(page));
+		set_fs_page_private(newpage, (void *)page_private(page));
 		set_page_private(page, 0);
 		put_page(page);
-		SetPagePrivate(newpage);
 	}
 
 	if (mode != MIGRATE_SYNC_NO_COPY)
Signature · 1 line
2.17.1
maintainer
#15 lore raw

Re: [RFC PATCH 6/9] iomap: use set/clear_fs_page_private

fs/iomap/buffered-io.c iomap_page_create(struct inode *inode, struct page *page) · line 63

Quoted 10 lines · Guoqing Jiang
fs/iomap/buffered-io.c +1 -4
@@ -59,24 +59,18 @@ iomap_page_create(struct inode *inode, struct page *page)
 	 * migrate_page_move_mapping() assumes that pages with private data have
 	 * their count elevated by 1.
 	 */
-	get_page(page);
-	set_page_private(page, (unsigned long)iop);
-	SetPagePrivate(page);
-	return iop;
+	return (struct iomap_page *)set_fs_page_private(page, iop);
 }

This cast is unnecessary. void * will be automatically cast to the appropriate pointer type.

fs/iomap/buffered-io.c iomap_migrate_page(struct address_space *mapping, struct page *newpage, · line 556

Quoted 11 lines
fs/iomap/buffered-io.c +1 -3
@@ -556,11 +550,9 @@ iomap_migrate_page(struct address_space *mapping, struct page *newpage,
 
 	if (page_has_private(page)) {
 		ClearPagePrivate(page);
-		get_page(newpage);
-		set_page_private(newpage, page_private(page));
+		set_fs_page_private(newpage, (void *)page_private(page));
 		set_page_private(page, 0);
 		put_page(page);
-		SetPagePrivate(newpage);
 	}

Same comment here as for the btrfs migrate page that Dave reviewed.

#16 lore raw

Re: [RFC PATCH 6/9] iomap: use set/clear_fs_page_private

Quoted 14 lines · Matthew Wilcox

fs/iomap/buffered-io.c iomap_page_create(struct inode *inode, struct page *page) · line 63

Quoted 10 lines · Guoqing Jiang
fs/iomap/buffered-io.c +1 -4
@@ -59,24 +59,18 @@ iomap_page_create(struct inode *inode, struct page *page)
 	 * migrate_page_move_mapping() assumes that pages with private data have
 	 * their count elevated by 1.
 	 */
-	get_page(page);
-	set_page_private(page, (unsigned long)iop);
-	SetPagePrivate(page);
-	return iop;
+	return (struct iomap_page *)set_fs_page_private(page, iop);
 }

This cast is unnecessary. void * will be automatically cast to the appropriate pointer type.

I also find the pattern eather strange. A:

	attach_page_private(page, iop);
	return iop;

explains the intent much better.

#17 lore raw

Re: [RFC PATCH 6/9] iomap: use set/clear_fs_page_private

Quoted 14 lines · Christoph Hellwig
Quoted 13 lines · Matthew Wilcox

fs/iomap/buffered-io.c iomap_page_create(struct inode *inode, struct page *page) · line 63

Quoted 10 lines · Guoqing Jiang
fs/iomap/buffered-io.c +1 -4
@@ -59,24 +59,18 @@ iomap_page_create(struct inode *inode, struct page *page)
  	 * migrate_page_move_mapping() assumes that pages with private data have
  	 * their count elevated by 1.
  	 */
-	get_page(page);
-	set_page_private(page, (unsigned long)iop);
-	SetPagePrivate(page);
-	return iop;
+	return (struct iomap_page *)set_fs_page_private(page, iop);
  }

This cast is unnecessary. void * will be automatically cast to the appropriate pointer type.

Quoted 6 lines

I also find the pattern eather strange. A:

	attach_page_private(page, iop);
	return iop;

explains the intent much better.

Thanks for the review, will do it.

Thanks,
Guoqing

#18 lore raw

Re: [RFC PATCH 6/9] iomap: use set/clear_fs_page_private

In reply to Matthew Wilcox
Quoted 16 lines · Matthew Wilcox

fs/iomap/buffered-io.c iomap_page_create(struct inode *inode, struct page *page) · line 63

Quoted 10 lines · Guoqing Jiang
fs/iomap/buffered-io.c +1 -4
@@ -59,24 +59,18 @@ iomap_page_create(struct inode *inode, struct page *page)
  	 * migrate_page_move_mapping() assumes that pages with private data have
  	 * their count elevated by 1.
  	 */
-	get_page(page);
-	set_page_private(page, (unsigned long)iop);
-	SetPagePrivate(page);
-	return iop;
+	return (struct iomap_page *)set_fs_page_private(page, iop);
  }

This cast is unnecessary. void * will be automatically cast to the appropriate pointer type.

Quoted 2 lines
(quoted diff) +0 -0
@@ -556,11 +550,9 @@ iomap_migrate_page(struct address_space *mapping, struct page *newpage,
 
Quoted 10 lines

fs/iomap/buffered-io.c iomap_migrate_page(struct address_space *mapping, struct page *newpage, · line 556

Quoted 9 lines
fs/iomap/buffered-io.c +1 -3
  	if (page_has_private(page)) {
  		ClearPagePrivate(page);
-		get_page(newpage);
-		set_page_private(newpage, page_private(page));
+		set_fs_page_private(newpage, (void *)page_private(page));
  		set_page_private(page, 0);
  		put_page(page);
-		SetPagePrivate(newpage);
  	}

Same comment here as for the btrfs migrate page that Dave reviewed.

Yes, thanks for the review.

Thanks,
Guoqing

#19 lore raw

Re: [RFC PATCH 6/9] iomap: use set/clear_fs_page_private

In reply to Guoqing Jiang

FYI, you've only Cced the xfs list on this one patch. Please Cc the whole list to everyone, otherwise a person just on the xfs list has no idea what your helpers added in patch 1 actually do.

#20 lore raw

Re: [RFC PATCH 6/9] iomap: use set/clear_fs_page_private

Quoted 3 lines · Christoph Hellwig

FYI, you've only Cced the xfs list on this one patch. Please Cc the whole list to everyone, otherwise a person just on the xfs list has no idea what your helpers added in patch 1 actually do.

Sorry, I should cc more lists for patch 1, thanks for reminder!

Thanks,
Guoqing

#21 lore raw

RFC PATCH 7/9 ntfs: replace attach_page_buffers with set_fs_page_private

In reply to Guoqing Jiang

Call the new function since attach_page_buffers will be removed.

---
 fs/ntfs/aops.c | 2 +-
 fs/ntfs/mft.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/ntfs/aops.c +1 -1
index 554b744f41bf..d8c60e5793c0 100644
@@ -1732,7 +1732,7 @@ void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
 				bh = bh->b_this_page;
 			} while (bh);
 			tail->b_this_page = head;
-			attach_page_buffers(page, head);
+			set_fs_page_private(page, head);
 		} else
 			buffers_to_free = bh;
 	}
fs/ntfs/mft.c +1 -1
index 3aac5c917afe..e61cb280aee0 100644
@@ -504,7 +504,7 @@ int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no,
 			bh = bh->b_this_page;
 		} while (bh);
 		tail->b_this_page = head;
-		attach_page_buffers(page, head);
+		set_fs_page_private(page, head);
 	}
 	bh = head = page_buffers(page);
 	BUG_ON(!bh);
Signature · 1 line
2.17.1
#22 lore raw

RFC PATCH 8/9 orangefs: use set/clear_fs_page_private

In reply to Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in orangefs.

Cc Mike Marshall <[email protected]>
Cc Martin Brandenburg <[email protected]>
---
 fs/orangefs/inode.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
fs/orangefs/inode.c +6 -18
index 12ae630fbed7..893099d36e20 100644
@@ -64,9 +64,7 @@ static int orangefs_writepage_locked(struct page *page,
 	}
 	if (wr) {
 		kfree(wr);
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);
 	}
 	return ret;
 }
@@ -409,9 +407,7 @@ static int orangefs_write_begin(struct file *file,
 	wr->len = len;
 	wr->uid = current_fsuid();
 	wr->gid = current_fsgid();
-	SetPagePrivate(page);
-	set_page_private(page, (unsigned long)wr);
-	get_page(page);
+	set_fs_page_private(page, wr);
 okay:
 	return 0;
 }
@@ -460,17 +456,13 @@ static void orangefs_invalidatepage(struct page *page,
 
 	if (offset == 0 && length == PAGE_SIZE) {
 		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);
 		return;
 	/* write range entirely within invalidate range (or equal) */
 	} else if (page_offset(page) + offset <= wr->pos &&
 	    wr->pos + wr->len <= page_offset(page) + offset + length) {
 		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);
 		/* XXX is this right? only caller in fs */
 		cancel_dirty_page(page);
 		return;
@@ -537,9 +529,7 @@ static void orangefs_freepage(struct page *page)
 {
 	if (PagePrivate(page)) {
 		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);
 	}
 }
 
@@ -740,9 +730,7 @@ vm_fault_t orangefs_page_mkwrite(struct vm_fault *vmf)
 	wr->len = PAGE_SIZE;
 	wr->uid = current_fsuid();
 	wr->gid = current_fsgid();
-	SetPagePrivate(page);
-	set_page_private(page, (unsigned long)wr);
-	get_page(page);
+	set_fs_page_private(page, wr);
 okay:
 
 	file_update_time(vmf->vma->vm_file);
Signature · 1 line
2.17.1
#23 lore raw

Re: [RFC PATCH 8/9] orangefs: use set/clear_fs_page_private

Quoted 16 lines · Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in orangefs.

Cc Mike Marshall <[email protected]>
Cc Martin Brandenburg <[email protected]>
---
 fs/orangefs/inode.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
fs/orangefs/inode.c +0 -0
index 12ae630fbed7..893099d36e20 100644

fs/orangefs/inode.c static int orangefs_writepage_locked(struct page *page, · line 67

Quoted 7 lines
fs/orangefs/inode.c +1 -3
@@ -64,9 +64,7 @@ static int orangefs_writepage_locked(struct page *page,
 	}
 	if (wr) {
 		kfree(wr);
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

THis is a pre-existing potential use-after-free vector. The wr pointer held in the page->private needs to be cleared from the page before it is freed.

Quoted 15 lines
fs/orangefs/inode.c +1 -3
 	}
 	return ret;
 }
@@ -409,9 +407,7 @@ static int orangefs_write_begin(struct file *file,
 	wr->len = len;
 	wr->uid = current_fsuid();
 	wr->gid = current_fsgid();
-	SetPagePrivate(page);
-	set_page_private(page, (unsigned long)wr);
-	get_page(page);
+	set_fs_page_private(page, wr);
 okay:
 	return 0;
 }

fs/orangefs/inode.c static void orangefs_invalidatepage(struct page *page, · line 459

Quoted 7 lines
fs/orangefs/inode.c +1 -3
@@ -460,17 +456,13 @@ static void orangefs_invalidatepage(struct page *page,
 
 	if (offset == 0 && length == PAGE_SIZE) {
 		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

Ditto:

		wr = clear_fs_page_private(page);
		kfree(wr);

fs/orangefs/inode.c static void orangefs_invalidatepage(struct page *page, · line 465

Quoted 9 lines
fs/orangefs/inode.c +1 -3
 		return;
 	/* write range entirely within invalidate range (or equal) */
 	} else if (page_offset(page) + offset <= wr->pos &&
 	    wr->pos + wr->len <= page_offset(page) + offset + length) {
 		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

And again.

fs/orangefs/inode.c static void orangefs_freepage(struct page *page) · line 532

Quoted 11 lines
fs/orangefs/inode.c +1 -3
 		/* XXX is this right? only caller in fs */
 		cancel_dirty_page(page);
 		return;
@@ -537,9 +529,7 @@ static void orangefs_freepage(struct page *page)
 {
 	if (PagePrivate(page)) {
 		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

And again.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
maintainer
#24 lore raw

Re: [RFC PATCH 8/9] orangefs: use set/clear_fs_page_private

Quoted 10 lines · Dave Chinner

fs/orangefs/inode.c static void orangefs_invalidatepage(struct page *page, · line 459

Quoted 8 lines
fs/orangefs/inode.c +1 -3
@@ -460,17 +456,13 @@ static void orangefs_invalidatepage(struct page *page,
 
 	if (offset == 0 && length == PAGE_SIZE) {
 		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

Ditto:

Quoted 2 lines
		wr = clear_fs_page_private(page);
		kfree(wr);

You don't want to be as succinct as the btrfs change you suggested?

		kfree(clear_fs_page_private(page));
#25 lore raw

Re: [RFC PATCH 8/9] orangefs: use set/clear_fs_page_private

Quoted 14 lines · Matthew Wilcox
Quoted 10 lines · Dave Chinner

fs/orangefs/inode.c static void orangefs_invalidatepage(struct page *page, · line 459

Quoted 8 lines
fs/orangefs/inode.c +1 -3
@@ -460,17 +456,13 @@ static void orangefs_invalidatepage(struct page *page,
 
 	if (offset == 0 && length == PAGE_SIZE) {
 		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

Ditto:

Quoted 2 lines
		wr = clear_fs_page_private(page);
		kfree(wr);
Quoted 3 lines

You don't want to be as succinct as the btrfs change you suggested?

		kfree(clear_fs_page_private(page));

That could be done, yes. I was really just trying to point out the use after free that was occurring here rather than write compact code...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#26 lore raw

Re: [RFC PATCH 8/9] orangefs: use set/clear_fs_page_private

Hi Mattew and Dave,

Quoted 13 lines · Dave Chinner
Quoted 12 lines · Matthew Wilcox
Quoted 11 lines · Dave Chinner

fs/orangefs/inode.c static void orangefs_invalidatepage(struct page *page, · line 459

Quoted 8 lines
fs/orangefs/inode.c +1 -3
@@ -460,17 +456,13 @@ static void orangefs_invalidatepage(struct page *page,
 
  	if (offset == 0 && length == PAGE_SIZE) {
  		kfree((struct orangefs_write_range *)page_private(page));
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

Ditto:

		wr = clear_fs_page_private(page);
		kfree(wr);
Quoted 6 lines
Quoted 3 lines

You don't want to be as succinct as the btrfs change you suggested?

		kfree(clear_fs_page_private(page));

That could be done, yes. I was really just trying to point out the use after free that was occurring here rather than write compact code...

Really appreciate for your review, thanks.

Best Regards,
Guoqing

#27 lore raw

Re: [RFC PATCH 8/9] orangefs: use set/clear_fs_page_private

In reply to Dave Chinner
Quoted 12 lines · Dave Chinner
Quoted 11 lines · Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in orangefs.

Cc Mike Marshall <[email protected]>
Cc Martin Brandenburg <[email protected]>
---
 fs/orangefs/inode.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
Quoted 16 lines

fs/orangefs/inode.c static int orangefs_writepage_locked(struct page *page, · line 67

Quoted 12 lines
fs/orangefs/inode.c +1 -3
index 12ae630fbed7..893099d36e20 100644
@@ -64,9 +64,7 @@ static int orangefs_writepage_locked(struct page *page,
 	}
 	if (wr) {
 		kfree(wr);
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

THis is a pre-existing potential use-after-free vector. The wr pointer held in the page->private needs to be cleared from the page before it is freed.

I'm not familar with orangefs. In my opinion, generally all temporary page->private access (r/w) should be properly protected by some locks, most of time I think it could be at least page lock since .migratepage, .invalidatepage, .releasepage, .. (such paths) are already called with page locked (honestly I'm interested in this topic, please correct me if I'm wrong).

I agree that the suggested modification is more clear and easy to read.

Thanks,
Gao Xiang

#28 lore raw

Re: [RFC PATCH 8/9] orangefs: use set/clear_fs_page_private

Quoted 30 lines · Gao Xiang
Quoted 12 lines · Dave Chinner
Quoted 11 lines · Guoqing Jiang

Since the new pair function is introduced, we can call them to clean the code in orangefs.

Cc Mike Marshall <[email protected]>
Cc Martin Brandenburg <[email protected]>
---
 fs/orangefs/inode.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
Quoted 16 lines

fs/orangefs/inode.c static int orangefs_writepage_locked(struct page *page, · line 67

Quoted 12 lines
fs/orangefs/inode.c +1 -3
index 12ae630fbed7..893099d36e20 100644
@@ -64,9 +64,7 @@ static int orangefs_writepage_locked(struct page *page,
 	}
 	if (wr) {
 		kfree(wr);
-		set_page_private(page, 0);
-		ClearPagePrivate(page);
-		put_page(page);
+		clear_fs_page_private(page);

THis is a pre-existing potential use-after-free vector. The wr pointer held in the page->private needs to be cleared from the page before it is freed.

Quoted 2 lines

I'm not familar with orangefs. In my opinion, generally all temporary page->private access (r/w) should be properly protected by some locks,

... page->private pointers (there may be some other uses rather than as references). sorry about that...

Quoted 11 lines

most of time I think it could be at least page lock since .migratepage, .invalidatepage, .releasepage, .. (such paths) are already called with page locked (honestly I'm interested in this topic, please correct me if I'm wrong).

I agree that the suggested modification is more clear and easy to read.

Thanks,
Gao Xiang

#29 lore raw

RFC PATCH 9/9 buffer_head.h: remove attach_page_buffers

In reply to Guoqing Jiang

All the callers have replaced attach_page_buffers with the new function set_fs_page_private, so remove it.

Cc Thomas Gleixner <[email protected]>
Cc Sebastian Andrzej Siewior <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Andreas Dilger <[email protected]>
---
 include/linux/buffer_head.h | 8 --------
 1 file changed, 8 deletions(-)
include/linux/buffer_head.h +0 -8
index 15b765a181b8..22fb11e2d2e0 100644
@@ -272,14 +272,6 @@ void buffer_init(void);
  * inline definitions
  */
 
-static inline void attach_page_buffers(struct page *page,
-		struct buffer_head *head)
-{
-	get_page(page);
-	SetPagePrivate(page);
-	set_page_private(page, (unsigned long)head);
-}
-
 static inline void get_bh(struct buffer_head *bh)
 {
         atomic_inc(&bh->b_count);
Signature · 1 line
2.17.1