Skip to contentSkip to conversation

btrfs: unify the read and writer locks for btrfs_subpage

PATCH Superseded Qu Wenruo 3 participants 5 messages Oct 7, 2024 – Oct 8, 2024 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: unify the read and writer locks for btrfs_subpage

When the handling of sector size < page size is introduced, there are two types of locking, reader and writer lock.

The main reason for the reader lock is to handle metadata to make sure the page::private is not released when there is still a metadata being read.

However since commit d7172f52e993 ("btrfs: use per-buffer locking for extent_buffer reading"), metadata read no longer relies on btrfs_subpage::readers.

Making the writer lock as the only utilized subpage locking.

This patchset converts all the existing reader lock usage and rename the writer lock into a generic lock.

This patchset relies on this patch "btrfs: fix the delalloc range locking if sector size < page size", as it removes the last user of btrfs_folio_start_writer_lock().

Qu Wenruo (2):
btrfs: unify to use writer locks for subpage locking
btrfs: rename btrfs_folio_(set|start|end)_writer_lock()

 fs/btrfs/compression.c |   3 +-
 fs/btrfs/extent_io.c   |  20 +++----
 fs/btrfs/subpage.c     | 126 ++++++++---------------------------------
 fs/btrfs/subpage.h     |  33 ++++-------
 4 files changed, 44 insertions(+), 138 deletions(-)
Signature · 1 line
2.46.2
author
#1 lore raw

PATCH 1/2 btrfs: unify to use writer locks for subpage locking

Since commit d7172f52e993 ("btrfs: use per-buffer locking for extent_buffer reading"), metadata read no longer relies on the subpage reader locking.

This means we do not need to maintain a different metadata/data split for locking, so we can convert the existing reader lock users by:

- add_ra_bio_pages()
Convert to btrfs_folio_set_writer_lock()

- end_folio_read()
Convert to btrfs_folio_end_writer_lock()

- begin_folio_read()
Convert to btrfs_folio_set_writer_lock()

- folio_range_has_eb()
Remove the subpage->readers checks, since it is always 0.

- Remove btrfs_subpage_start_reader() and btrfs_subpage_end_reader()

---
 fs/btrfs/compression.c |  3 +-
 fs/btrfs/extent_io.c   | 10 ++----
 fs/btrfs/subpage.c     | 82 ++----------------------------------------
 fs/btrfs/subpage.h     | 19 +++-------
 4 files changed, 10 insertions(+), 104 deletions(-)
fs/btrfs/compression.c +1 -2
index 6e9c4a5e0d51..74799270eb78 100644
@@ -545,8 +545,7 @@ static noinline int add_ra_bio_pages(struct inode *inode,
 		 * subpage::readers and to unlock the page.
 		 */
 		if (fs_info->sectorsize < PAGE_SIZE)
-			btrfs_subpage_start_reader(fs_info, folio, cur,
-						   add_size);
+			btrfs_folio_set_writer_lock(fs_info, folio, cur, add_size);
 		folio_put(folio);
 		cur += add_size;
 	}
fs/btrfs/extent_io.c +2 -8
index 0448cee2b983..04b190bc047f 100644
@@ -436,7 +436,7 @@ static void end_folio_read(struct folio *folio, bool uptodate, u64 start, u32 le
 	if (!btrfs_is_subpage(fs_info, folio->mapping))
 		folio_unlock(folio);
 	else
-		btrfs_subpage_end_reader(fs_info, folio, start, len);
+		btrfs_folio_end_writer_lock(fs_info, folio, start, len);
 }
 
 /*
@@ -493,7 +493,7 @@ static void begin_folio_read(struct btrfs_fs_info *fs_info, struct folio *folio)
 		return;
 
 	ASSERT(folio_test_private(folio));
-	btrfs_subpage_start_reader(fs_info, folio, folio_pos(folio), PAGE_SIZE);
+	btrfs_folio_set_writer_lock(fs_info, folio, folio_pos(folio), PAGE_SIZE);
 }
 
 /*
@@ -2508,12 +2508,6 @@ static bool folio_range_has_eb(struct btrfs_fs_info *fs_info, struct folio *foli
 		subpage = folio_get_private(folio);
 		if (atomic_read(&subpage->eb_refs))
 			return true;
-		/*
-		 * Even there is no eb refs here, we may still have
-		 * end_folio_read() call relying on page::private.
-		 */
-		if (atomic_read(&subpage->readers))
-			return true;
 	}
 	return false;
 }
fs/btrfs/subpage.c +2 -80

95 lines not on this page · raw message

fs/btrfs/subpage.h +5 -14
index cbee866152de..b6337543f7dd 100644
@@ -45,14 +45,6 @@ enum {
 struct btrfs_subpage {
 	/* Common members for both data and metadata pages */
 	spinlock_t lock;
-	/*
-	 * Both data and metadata needs to track how many readers are for the
-	 * page.
-	 * Data relies on @readers to unlock the page when last reader finished.
-	 * While metadata doesn't need page unlock, it needs to prevent
-	 * page::private get cleared before the last end_page_read().
-	 */
-	atomic_t readers;
 	union {
 		/*
 		 * Structures only used by metadata
@@ -62,7 +54,11 @@ struct btrfs_subpage {
 		 */
 		atomic_t eb_refs;
 
-		/* Structures only used by data */
+		/*
+		 * Structures only used by data,
+		 *
+		 * How many sectors inside the page is locked.
+		 */
 		atomic_t writers;
 	};
 	unsigned long bitmaps[];
@@ -95,11 +91,6 @@ void btrfs_free_subpage(struct btrfs_subpage *subpage);
 void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
 void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
 
-void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
-				struct folio *folio, u64 start, u32 len);
-void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
-			      struct folio *folio, u64 start, u32 len);
-
 void btrfs_folio_end_writer_lock(const struct btrfs_fs_info *fs_info,
 				 struct folio *folio, u64 start, u32 len);
 void btrfs_folio_set_writer_lock(const struct btrfs_fs_info *fs_info,
Signature · 1 line
2.46.2
author
#2 lore raw

PATCH 2/2 btrfs: rename btrfs_folio_(set|start|end)_writer_lock()

In reply to Qu Wenruo

Since there is no user of reader locks, rename the writer locks into a more generic name, by removing the "_writer" part from the name.

And also rename btrfs_subpage::writer into btrfs_subpage::locked.

---
 fs/btrfs/compression.c |  2 +-
 fs/btrfs/extent_io.c   | 14 ++++++-------
 fs/btrfs/subpage.c     | 46 +++++++++++++++++++++---------------------
 fs/btrfs/subpage.h     | 14 ++++++-------
 4 files changed, 38 insertions(+), 38 deletions(-)
fs/btrfs/compression.c +1 -1
index 74799270eb78..dfa872cf09d5 100644
@@ -545,7 +545,7 @@ static noinline int add_ra_bio_pages(struct inode *inode,
 		 * subpage::readers and to unlock the page.
 		 */
 		if (fs_info->sectorsize < PAGE_SIZE)
-			btrfs_folio_set_writer_lock(fs_info, folio, cur, add_size);
+			btrfs_folio_set_lock(fs_info, folio, cur, add_size);
 		folio_put(folio);
 		cur += add_size;
 	}
fs/btrfs/extent_io.c +7 -7
index 04b190bc047f..09c7f41555d8 100644
@@ -190,7 +190,7 @@ static void process_one_folio(struct btrfs_fs_info *fs_info,
 		btrfs_folio_clamp_clear_writeback(fs_info, folio, start, len);
 
 	if (folio != locked_folio && (page_ops & PAGE_UNLOCK))
-		btrfs_folio_end_writer_lock(fs_info, folio, start, len);
+		btrfs_folio_end_lock(fs_info, folio, start, len);
 }
 
 static void __process_folios_contig(struct address_space *mapping,
@@ -274,7 +274,7 @@ static noinline int lock_delalloc_folios(struct inode *inode,
 				folio_unlock(folio);
 				goto out;
 			}
-			btrfs_folio_set_writer_lock(fs_info, folio, range_start, range_len);
+			btrfs_folio_set_lock(fs_info, folio, range_start, range_len);
 
 			processed_end = range_start + range_len - 1;
 		}
@@ -436,7 +436,7 @@ static void end_folio_read(struct folio *folio, bool uptodate, u64 start, u32 le
 	if (!btrfs_is_subpage(fs_info, folio->mapping))
 		folio_unlock(folio);
 	else
-		btrfs_folio_end_writer_lock(fs_info, folio, start, len);
+		btrfs_folio_end_lock(fs_info, folio, start, len);
 }
 
 /*
@@ -493,7 +493,7 @@ static void begin_folio_read(struct btrfs_fs_info *fs_info, struct folio *folio)
 		return;
 
 	ASSERT(folio_test_private(folio));
-	btrfs_folio_set_writer_lock(fs_info, folio, folio_pos(folio), PAGE_SIZE);
+	btrfs_folio_set_lock(fs_info, folio, folio_pos(folio), PAGE_SIZE);
 }
 
 /*
@@ -1175,7 +1175,7 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
 	for_each_set_bit(bit, &bio_ctrl->submit_bitmap, fs_info->sectors_per_page) {
 		u64 start = page_start + (bit << fs_info->sectorsize_bits);
 
-		btrfs_folio_set_writer_lock(fs_info, folio, start, fs_info->sectorsize);
+		btrfs_folio_set_lock(fs_info, folio, start, fs_info->sectorsize);
 	}
 
 	/* Lock all (subpage) delalloc ranges inside the folio first. */
@@ -1511,7 +1511,7 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
 	 * Only unlock ranges that are submitted. As there can be some async
 	 * submitted ranges inside the folio.
 	 */
-	btrfs_folio_end_writer_lock_bitmap(fs_info, folio, bio_ctrl->submit_bitmap);
+	btrfs_folio_end_lock_bitmap(fs_info, folio, bio_ctrl->submit_bitmap);
 	ASSERT(ret <= 0);
 	return ret;
 }
@@ -2289,7 +2289,7 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f
 						       cur, cur_len, !ret);
 			mapping_set_error(mapping, ret);
 		}
-		btrfs_folio_end_writer_lock(fs_info, folio, cur, cur_len);
+		btrfs_folio_end_lock(fs_info, folio, cur, cur_len);
 		if (ret < 0)
 			found_error = true;
 next_page:
fs/btrfs/subpage.c +23 -23

118 lines not on this page · raw message

fs/btrfs/subpage.h +7 -7
index b6337543f7dd..96a405a90827 100644
@@ -59,7 +59,7 @@ struct btrfs_subpage {
 		 *
 		 * How many sectors inside the page is locked.
 		 */
-		atomic_t writers;
+		atomic_t locked;
 	};
 	unsigned long bitmaps[];
 };
@@ -91,12 +91,12 @@ void btrfs_free_subpage(struct btrfs_subpage *subpage);
 void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
 void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
 
-void btrfs_folio_end_writer_lock(const struct btrfs_fs_info *fs_info,
-				 struct folio *folio, u64 start, u32 len);
-void btrfs_folio_set_writer_lock(const struct btrfs_fs_info *fs_info,
-				 struct folio *folio, u64 start, u32 len);
-void btrfs_folio_end_writer_lock_bitmap(const struct btrfs_fs_info *fs_info,
-					struct folio *folio, unsigned long bitmap);
+void btrfs_folio_end_lock(const struct btrfs_fs_info *fs_info,
+			  struct folio *folio, u64 start, u32 len);
+void btrfs_folio_set_lock(const struct btrfs_fs_info *fs_info,
+			  struct folio *folio, u64 start, u32 len);
+void btrfs_folio_end_lock_bitmap(const struct btrfs_fs_info *fs_info,
+				 struct folio *folio, unsigned long bitmap);
 /*
  * Template for subpage related operations.
  *
Signature · 1 line
2.46.2
#3 lore raw

Re: [PATCH 2/2] btrfs: rename btrfs_folio_(set|start|end)_writer_lock()

Quoted 4 lines · Qu Wenruo

Since there is no user of reader locks, rename the writer locks into a more generic name, by removing the "_writer" part from the name.

And also rename btrfs_subpage::writer into btrfs_subpage::locked.

'locked' looks confusing in connection with atomics. It sounds like a predicate but is in fact a counter, so I'd suggest to rename it to nr_locked or nr_locks or something like that.

#4 lore raw

Re: [PATCH 2/2] btrfs: rename btrfs_folio_(set|start|end)_writer_lock()

Quoted 10 lines · David Sterba
Quoted 4 lines · Qu Wenruo

Since there is no user of reader locks, rename the writer locks into a more generic name, by removing the "_writer" part from the name.

And also rename btrfs_subpage::writer into btrfs_subpage::locked.

'locked' looks confusing in connection with atomics. It sounds like a predicate but is in fact a counter, so I'd suggest to rename it to nr_locked or nr_locks or something like that.

nr_locked looks much better, and way better than my original "sectors_locked".

Thanks,
Qu