Skip to contentSkip to conversation

btrfs: subpage: pack all subpage bitmaps into a larger bitmap

PATCH Superseded Qu Wenruo 4 participants 10 messages Aug 16, 2021 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: subpage: pack all subpage bitmaps into a larger bitmap

Currently we use u16 bitmap to make 4k sectorsize work for 64K page size.

But this u16 bitmap is not large enough to contain larger page size like 128K, nor is space efficient for 16K page size.

To handle both cases, here we pack all subpage bitmaps into a larger bitmap, now btrfs_subpage::bitmaps[] will be the ultimate bitmap for subpage usage.

This is the first step towards more page size support.

Although to really enable extra page size like 16K and 128K, we need to rework the metadata alignment check first.
Which will happen in another patchset.

Qu Wenruo (2):
btrfs: introduce btrfs_subpage_bitmap_info
btrfs: subpage: pack all subpage bitmaps into a larger bitmap

 fs/btrfs/ctree.h     |   1 +
 fs/btrfs/disk-io.c   |  12 ++-
 fs/btrfs/extent_io.c |  58 +++++++++------
 fs/btrfs/subpage.c   | 172 ++++++++++++++++++++++++++++++++-----------
 fs/btrfs/subpage.h   |  41 +++++++----
 5 files changed, 201 insertions(+), 83 deletions(-)
Signature · 1 line
2.32.0
author
#1 lore raw

PATCH 1/2 btrfs: introduce btrfs_subpage_bitmap_info

Currently we use fixed size u16 bitmap for subpage bitmap. This is fine for 4K sectorsize with 64K page size.

But for 4K sectorsize and larger page size, the bitmap is too small, while for smaller page size like 16K, u16 bitmaps waste too much space.

Here we introduce a new helper structure, btrfs_subpage_bitmap_info, to record the proper bitmap size, and where each bitmap should start at.

By this, we can later compact all subpage bitmaps into one u32 bitmap.

This patch is the first step towards such compact bitmap.

---
 fs/btrfs/ctree.h   |  1 +
 fs/btrfs/disk-io.c | 12 ++++++++++--
 fs/btrfs/subpage.c | 35 +++++++++++++++++++++++++++++++++++
 fs/btrfs/subpage.h | 28 ++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 2 deletions(-)
fs/btrfs/ctree.h +1 -0
index 4a69aa604ac5..a98fd6a24113 100644
@@ -898,6 +898,7 @@ struct btrfs_fs_info {
 	struct btrfs_workqueue *scrub_workers;
 	struct btrfs_workqueue *scrub_wr_completion_workers;
 	struct btrfs_workqueue *scrub_parity_workers;
+	struct btrfs_subpage_info *subpage_info;
 
 	struct btrfs_discard_ctl discard_ctl;
 
fs/btrfs/disk-io.c +10 -2
index 1510a9d92858..f35b875f9e53 100644
@@ -1644,6 +1644,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
 	btrfs_extent_buffer_leak_debug_check(fs_info);
 	kfree(fs_info->super_copy);
 	kfree(fs_info->super_for_commit);
+	kfree(fs_info->subpage_info);
 	kvfree(fs_info);
 }
 
@@ -3393,11 +3394,13 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 	}
 
 	if (sectorsize != PAGE_SIZE) {
+		struct btrfs_subpage_info *subpage_info;
+
+		ASSERT(sectorsize < PAGE_SIZE);
+
 		btrfs_warn(fs_info,
 	"read-write for sector size %u with page size %lu is experimental",
 			   sectorsize, PAGE_SIZE);
-	}
-	if (sectorsize != PAGE_SIZE) {
 		if (btrfs_super_incompat_flags(fs_info->super_copy) &
 			BTRFS_FEATURE_INCOMPAT_RAID56) {
 			btrfs_err(fs_info,
@@ -3406,6 +3409,11 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 			err = -EINVAL;
 			goto fail_alloc;
 		}
+		subpage_info = kzalloc(sizeof(*subpage_info), GFP_NOFS);
+		if (!subpage_info)
+			goto fail_alloc;
+		btrfs_init_subpage_info(subpage_info, sectorsize);
+		fs_info->subpage_info = subpage_info;
 	}
 
 	ret = btrfs_init_workqueues(fs_info, fs_devices);
fs/btrfs/subpage.c +35 -0
index a61aa33aeeee..014256d47beb 100644
@@ -63,6 +63,41 @@
  *   This means a slightly higher tree locking latency.
  */
 
+void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info,
+			     u32 sectorsize)
+{
+	unsigned int cur = 0;
+	unsigned int nr_bits;
+
+	/*
+	 * Just in case we have super large PAGE_SIZE that unsigned int is not
+	 * enough to contain the number of sectors for the minimal sectorsize.
+	 */
+	BUILD_BUG_ON(UINT_MAX * SZ_4K < PAGE_SIZE);
+
+	ASSERT(IS_ALIGNED(PAGE_SIZE, sectorsize));
+
+	nr_bits = PAGE_SIZE / sectorsize;
+	subpage_info->bitmap_nr_bits = nr_bits;
+
+	subpage_info->uptodate_start = cur;
+	cur += nr_bits;
+
+	subpage_info->error_start = cur;
+	cur += nr_bits;
+
+	subpage_info->dirty_start = cur;
+	cur += nr_bits;
+
+	subpage_info->writeback_start = cur;
+	cur += nr_bits;
+
+	subpage_info->ordered_start = cur;
+	cur += nr_bits;
+
+	subpage_info->total_nr_bits = cur;
+}
+
 int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
 			 struct page *page, enum btrfs_subpage_type type)
 {
fs/btrfs/subpage.h +28 -0
index 9aa40d795ba9..ea90ba42c97b 100644
@@ -11,6 +11,32 @@
  */
 #define BTRFS_SUBPAGE_BITMAP_SIZE	16
 
+/*
+ * Extra info for subpapge bitmap.
+ *
+ * For subpage we integrate all uptodate/error/dirty/writeback/ordered
+ * bitmaps into one larger bitmap.
+ * This structure records the basic info.
+ */
+struct btrfs_subpage_info {
+	/* Number of bits for each bitmap*/
+	unsigned int bitmap_nr_bits;
+
+	/* Total number of bits for the whole bitmap */
+	unsigned int total_nr_bits;
+
+	/*
+	 * *_start indicates where the bitmap starts, the length
+	 * is always @bitmap_size, which is calculated from
+	 * PAGE_SIZE / sectorsize.
+	 */
+	unsigned int uptodate_start;
+	unsigned int error_start;
+	unsigned int dirty_start;
+	unsigned int writeback_start;
+	unsigned int ordered_start;
+};
+
 /*
  * Structure to trace status of each sector inside a page, attached to
  * page::private for both data and metadata inodes.
@@ -53,6 +79,8 @@ enum btrfs_subpage_type {
 	BTRFS_SUBPAGE_DATA,
 };
 
+void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info,
+			     u32 sectorsize);
 int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
 			 struct page *page, enum btrfs_subpage_type type);
 void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
Signature · 1 line
2.32.0
#2 lore raw

Re: [PATCH 1/2] btrfs: introduce btrfs_subpage_bitmap_info

Quoted 46 lines · Qu Wenruo

Currently we use fixed size u16 bitmap for subpage bitmap. This is fine for 4K sectorsize with 64K page size.

But for 4K sectorsize and larger page size, the bitmap is too small, while for smaller page size like 16K, u16 bitmaps waste too much space.

Here we introduce a new helper structure, btrfs_subpage_bitmap_info, to record the proper bitmap size, and where each bitmap should start at.

By this, we can later compact all subpage bitmaps into one u32 bitmap.

This patch is the first step towards such compact bitmap.

---
 fs/btrfs/ctree.h   |  1 +
 fs/btrfs/disk-io.c | 12 ++++++++++--
 fs/btrfs/subpage.c | 35 +++++++++++++++++++++++++++++++++++
 fs/btrfs/subpage.h | 28 ++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 2 deletions(-)
fs/btrfs/ctree.h +1 -0
index 4a69aa604ac5..a98fd6a24113 100644
@@ -898,6 +898,7 @@ struct btrfs_fs_info {
 	struct btrfs_workqueue *scrub_workers;
 	struct btrfs_workqueue *scrub_wr_completion_workers;
 	struct btrfs_workqueue *scrub_parity_workers;
+	struct btrfs_subpage_info *subpage_info;
 
 	struct btrfs_discard_ctl discard_ctl;
 
fs/btrfs/disk-io.c +1 -0
index 1510a9d92858..f35b875f9e53 100644
@@ -1644,6 +1644,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
 	btrfs_extent_buffer_leak_debug_check(fs_info);
 	kfree(fs_info->super_copy);
 	kfree(fs_info->super_for_commit);
+	kfree(fs_info->subpage_info);
 	kvfree(fs_info);
 }
 

fs/btrfs/disk-io.c int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · line 3399

Quoted 6 lines
fs/btrfs/disk-io.c +3 -0
@@ -3393,11 +3394,13 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 	}
 
 	if (sectorsize != PAGE_SIZE) {
+		struct btrfs_subpage_info *subpage_info;
+
+		ASSERT(sectorsize < PAGE_SIZE);

nit: Simply make the check sectorsize < PAGE_SIZE and that renders the assert redundant.

Quoted 52 lines
fs/btrfs/disk-io.c +6 -2
+
 		btrfs_warn(fs_info,
 	"read-write for sector size %u with page size %lu is experimental",
 			   sectorsize, PAGE_SIZE);
-	}
-	if (sectorsize != PAGE_SIZE) {
 		if (btrfs_super_incompat_flags(fs_info->super_copy) &
 			BTRFS_FEATURE_INCOMPAT_RAID56) {
 			btrfs_err(fs_info,
@@ -3406,6 +3409,11 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 			err = -EINVAL;
 			goto fail_alloc;
 		}
+		subpage_info = kzalloc(sizeof(*subpage_info), GFP_NOFS);
+		if (!subpage_info)
+			goto fail_alloc;
+		btrfs_init_subpage_info(subpage_info, sectorsize);
+		fs_info->subpage_info = subpage_info;
 	}
 
 	ret = btrfs_init_workqueues(fs_info, fs_devices);
fs/btrfs/subpage.c +23 -0
index a61aa33aeeee..014256d47beb 100644
@@ -63,6 +63,41 @@
  *   This means a slightly higher tree locking latency.
  */
 
+void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info,
+			     u32 sectorsize)
+{
+	unsigned int cur = 0;
+	unsigned int nr_bits;
+
+	/*
+	 * Just in case we have super large PAGE_SIZE that unsigned int is not
+	 * enough to contain the number of sectors for the minimal sectorsize.
+	 */
+	BUILD_BUG_ON(UINT_MAX * SZ_4K < PAGE_SIZE);
+
+	ASSERT(IS_ALIGNED(PAGE_SIZE, sectorsize));
+
+	nr_bits = PAGE_SIZE / sectorsize;
+	subpage_info->bitmap_nr_bits = nr_bits;
+
+	subpage_info->uptodate_start = cur;
+	cur += nr_bits;
+
+	subpage_info->error_start = cur;
+	cur += nr_bits;
+

fs/btrfs/subpage.c line 98

Quoted 10 lines
fs/btrfs/subpage.c +10 -0
+	subpage_info->dirty_start = cur;
+	cur += nr_bits;
+
+	subpage_info->writeback_start = cur;
+	cur += nr_bits;
+
+	subpage_info->ordered_start = cur;
+	cur += nr_bits;
+
+	subpage_info->total_nr_bits = cur;

So those values are really consts, however due to them being allocated on the heap you can't simply define them as const and initialize them. What a bummer...

On the other hand the namings are a bit generic, those are really offsets into subpage->bitmaps. As such I'd prefer something along the lines of
writeback_bitmap_offset
ordered_bitmap_offset etc.

Also I believe a graphical representation is in order i.e

[u][u][u][u][e][e][e][e][e]
^ ^
|-uptodate_start |- error_start etc

Since it's a bit unexpected to have multiple, logically independent bitmaps be tracked in the same physical location.

Quoted 52 lines
fs/btrfs/subpage.c +2 -0
+}
+
 int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
 			 struct page *page, enum btrfs_subpage_type type)
 {
fs/btrfs/subpage.h +28 -0
index 9aa40d795ba9..ea90ba42c97b 100644
@@ -11,6 +11,32 @@
  */
 #define BTRFS_SUBPAGE_BITMAP_SIZE	16
 
+/*
+ * Extra info for subpapge bitmap.
+ *
+ * For subpage we integrate all uptodate/error/dirty/writeback/ordered
+ * bitmaps into one larger bitmap.
+ * This structure records the basic info.
+ */
+struct btrfs_subpage_info {
+	/* Number of bits for each bitmap*/
+	unsigned int bitmap_nr_bits;
+
+	/* Total number of bits for the whole bitmap */
+	unsigned int total_nr_bits;
+
+	/*
+	 * *_start indicates where the bitmap starts, the length
+	 * is always @bitmap_size, which is calculated from
+	 * PAGE_SIZE / sectorsize.
+	 */
+	unsigned int uptodate_start;
+	unsigned int error_start;
+	unsigned int dirty_start;
+	unsigned int writeback_start;
+	unsigned int ordered_start;
+};
+
 /*
  * Structure to trace status of each sector inside a page, attached to
  * page::private for both data and metadata inodes.
@@ -53,6 +79,8 @@ enum btrfs_subpage_type {
 	BTRFS_SUBPAGE_DATA,
 };
 
+void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info,
+			     u32 sectorsize);
 int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
 			 struct page *page, enum btrfs_subpage_type type);
 void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
#3 lore raw

Re: [PATCH 1/2] btrfs: introduce btrfs_subpage_bitmap_info

Quoted 2 lines · Nikolay Borisov

[...]

Quoted 8 lines

fs/btrfs/disk-io.c int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · line 3399

Quoted 5 lines
  	if (sectorsize != PAGE_SIZE) {
fs/btrfs/disk-io.c +3 -0
+		struct btrfs_subpage_info *subpage_info;
+
+		ASSERT(sectorsize < PAGE_SIZE);

nit: Simply make the check sectorsize < PAGE_SIZE and that renders the assert redundant.

I guess that's the way to go.

Since I don't believe we will support multi-page sectorsize anyway, we don't need to bother sectorsize > PAGE_SIZE forever.
Thus we can forget the sectorsize > PAGE_SIZE case.

[...]

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	subpage_info->total_nr_bits = cur;

So those values are really consts, however due to them being allocated on the heap you can't simply define them as const and initialize them. What a bummer...

Yeah, that's the limit of C.

Quoted 7 lines

On the other hand the namings are a bit generic, those are really offsets into subpage->bitmaps. As such I'd prefer something along the lines of
writeback_bitmap_offset
ordered_bitmap_offset etc.

The *_bitmap_offset seems a little long, especially in the 2nd patch we're already introducing too many new lines just to handle the *_start naming.

Especially all these members are used with thing like fs_info->subpage_info->uptodate_start.

What about the name "uptodate_offset"?

Quoted 7 lines

Also I believe a graphical representation is in order i.e

[u][u][u][u][e][e][e][e][e]
^ ^
|-uptodate_start |- error_start etc

That looks awesome.

Quoted 3 lines

Since it's a bit unexpected to have multiple, logically independent bitmaps be tracked in the same physical location.

That's also I'm concerning of.

I haven't seen other code sides doing the same behavior. IOmap just waste all the memory by going full u32 bitmap even for case like 16K page size and 4K sectorsize, exactly I want to avoid.

Thanks,
Qu

Quoted 52 lines
Quoted 52 lines
fs/btrfs/subpage.c +2 -0
+}
+
  int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
  			 struct page *page, enum btrfs_subpage_type type)
  {
fs/btrfs/subpage.h +28 -0
index 9aa40d795ba9..ea90ba42c97b 100644
@@ -11,6 +11,32 @@
   */
  #define BTRFS_SUBPAGE_BITMAP_SIZE	16
 
+/*
+ * Extra info for subpapge bitmap.
+ *
+ * For subpage we integrate all uptodate/error/dirty/writeback/ordered
+ * bitmaps into one larger bitmap.
+ * This structure records the basic info.
+ */
+struct btrfs_subpage_info {
+	/* Number of bits for each bitmap*/
+	unsigned int bitmap_nr_bits;
+
+	/* Total number of bits for the whole bitmap */
+	unsigned int total_nr_bits;
+
+	/*
+	 * *_start indicates where the bitmap starts, the length
+	 * is always @bitmap_size, which is calculated from
+	 * PAGE_SIZE / sectorsize.
+	 */
+	unsigned int uptodate_start;
+	unsigned int error_start;
+	unsigned int dirty_start;
+	unsigned int writeback_start;
+	unsigned int ordered_start;
+};
+
  /*
   * Structure to trace status of each sector inside a page, attached to
   * page::private for both data and metadata inodes.
@@ -53,6 +79,8 @@ enum btrfs_subpage_type {
  	BTRFS_SUBPAGE_DATA,
  };
 
+void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info,
+			     u32 sectorsize);
  int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
  			 struct page *page, enum btrfs_subpage_type type);
  void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
#4 lore raw

Re: [PATCH 1/2] btrfs: introduce btrfs_subpage_bitmap_info

Quoted 8 lines · Qu Wenruo
Quoted 5 lines

Also I believe a graphical representation is in order i.e

[u][u][u][u][e][e][e][e][e]
^ ^
|-uptodate_start |- error_start etc

That looks awesome.

Quoted 8 lines
Quoted 2 lines

Since it's a bit unexpected to have multiple, logically independent bitmaps be tracked in the same physical location.

That's also I'm concerning of.

I haven't seen other code sides doing the same behavior. IOmap just waste all the memory by going full u32 bitmap even for case like 16K page size and 4K sectorsize, exactly I want to avoid.

Yeah, it is mixing independent things in one structure, but that is to decrease the waste and memory consumption. The subpage is attached to each page, that come in large numbers so this is a justified optimization, even if it's not "conceptually clean".

In the first implementation there were 4 bitmaps, so we see how things work without the optimization. The switch to one bitmap was about to happen once we get things working.

author
#5 lore raw

PATCH 2/2 btrfs: subpage: pack all subpage bitmaps into a larger bitmap

In reply to Qu Wenruo

Currently we use u16 bitmap to make 4k sectorsize work for 64K page size.

But this u16 bitmap is not large enough to contain larger page size like 128K, nor is space efficient for 16K page size.

To handle both cases, here we pack all subpage bitmaps into a larger bitmap, now btrfs_subpage::bitmaps[] will be the ultimate bitmap for subpage usage.

Each sub-bitmap will has its start bit number recorded in btrfs_subpage_info::*_start, and its bitmap length will be recorded in btrfs_subpage_info::bitmap_nr_bits.

All subpage bitmap operations will be converted from using direct u16 operations to bitmap operations, with above *_start calculated.

For 64K page size with 4K sectorsize, this should not cause much difference. While for 16K page size, we will only need 1 unsigned long (u32) to restore the bitmap.
And will be able to support 128K page size in the future.

---
 fs/btrfs/extent_io.c |  58 ++++++++++--------
 fs/btrfs/subpage.c   | 137 +++++++++++++++++++++++++++++--------------
 fs/btrfs/subpage.h   |  19 +-----
 3 files changed, 130 insertions(+), 84 deletions(-)
fs/btrfs/extent_io.c +35 -23
index 543f87ea372e..e428d6208bb7 100644
@@ -3865,10 +3865,9 @@ static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
 	struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
 	u64 orig_start = *start;
 	/* Declare as unsigned long so we can use bitmap ops */
-	unsigned long dirty_bitmap;
 	unsigned long flags;
-	int nbits = (orig_start - page_offset(page)) >> fs_info->sectorsize_bits;
-	int range_start_bit = nbits;
+	int nbits = offset_in_page(orig_start) >> fs_info->sectorsize_bits;
+	int range_start_bit = nbits + fs_info->subpage_info->dirty_start;
 	int range_end_bit;
 
 	/*
@@ -3883,11 +3882,14 @@ static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
 
 	/* We should have the page locked, but just in case */
 	spin_lock_irqsave(&subpage->lock, flags);
-	dirty_bitmap = subpage->dirty_bitmap;
+	bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit,
+			       fs_info->subpage_info->dirty_start +
+			       fs_info->subpage_info->bitmap_nr_bits);
 	spin_unlock_irqrestore(&subpage->lock, flags);
 
-	bitmap_next_set_region(&dirty_bitmap, &range_start_bit, &range_end_bit,
-			       BTRFS_SUBPAGE_BITMAP_SIZE);
+	range_start_bit -= fs_info->subpage_info->dirty_start;
+	range_end_bit -= fs_info->subpage_info->dirty_start;
+
 	*start = page_offset(page) + range_start_bit * fs_info->sectorsize;
 	*end = page_offset(page) + range_end_bit * fs_info->sectorsize;
 }
@@ -4613,7 +4615,7 @@ static int submit_eb_subpage(struct page *page,
 	int submitted = 0;
 	u64 page_start = page_offset(page);
 	int bit_start = 0;
-	const int nbits = BTRFS_SUBPAGE_BITMAP_SIZE;
+	const int nbits = fs_info->subpage_info->bitmap_nr_bits;
 	int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits;
 	int ret;
 
@@ -4634,7 +4636,8 @@ static int submit_eb_subpage(struct page *page,
 			break;
 		}
 		spin_lock_irqsave(&subpage->lock, flags);
-		if (!((1 << bit_start) & subpage->dirty_bitmap)) {
+		if (!test_bit(bit_start + fs_info->subpage_info->dirty_start,
+			      subpage->bitmaps)) {
 			spin_unlock_irqrestore(&subpage->lock, flags);
 			spin_unlock(&page->mapping->private_lock);
 			bit_start++;
@@ -7178,32 +7181,41 @@ void memmove_extent_buffer(const struct extent_buffer *dst,
 	}
 }
 
+#define GANG_LOOKUP_SIZE	16
 static struct extent_buffer *get_next_extent_buffer(
 		struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
 {
-	struct extent_buffer *gang[BTRFS_SUBPAGE_BITMAP_SIZE];
+	struct extent_buffer *gang[GANG_LOOKUP_SIZE];
 	struct extent_buffer *found = NULL;
 	u64 page_start = page_offset(page);
-	int ret;
-	int i;
+	u64 cur = page_start;
 
 	ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
-	ASSERT(PAGE_SIZE / fs_info->nodesize <= BTRFS_SUBPAGE_BITMAP_SIZE);
 	lockdep_assert_held(&fs_info->buffer_lock);
 
-	ret = radix_tree_gang_lookup(&fs_info->buffer_radix, (void **)gang,
-			bytenr >> fs_info->sectorsize_bits,
-			PAGE_SIZE / fs_info->nodesize);
-	for (i = 0; i < ret; i++) {
-		/* Already beyond page end */
-		if (gang[i]->start >= page_start + PAGE_SIZE)
-			break;
-		/* Found one */
-		if (gang[i]->start >= bytenr) {
-			found = gang[i];
-			break;
+	while (cur < page_start + PAGE_SIZE) {
+		int ret;
+		int i;
+
+		ret = radix_tree_gang_lookup(&fs_info->buffer_radix,
+				(void **)gang, cur >> fs_info->sectorsize_bits,
+				min_t(unsigned int, GANG_LOOKUP_SIZE,
+				      PAGE_SIZE / fs_info->nodesize));
+		if (ret == 0)
+			goto out;
+		for (i = 0; i < ret; i++) {
+			/* Already beyond page end */
+			if (gang[i]->start >= page_start + PAGE_SIZE)
+				goto out;
+			/* Found one */
+			if (gang[i]->start >= bytenr) {
+				found = gang[i];
+				break;
+			}
 		}
+		cur = gang[ret - 1]->start + gang[ret - 1]->len;
 	}
+out:
 	return found;
 }
 
fs/btrfs/subpage.c +92 -45

260 lines not on this page · raw message

fs/btrfs/subpage.h +3 -16
index ea90ba42c97b..6276947c4020 100644
@@ -5,12 +5,6 @@
 
 #include <linux/spinlock.h>
 
-/*
- * Maximum page size we support is 64K, minimum sector size is 4K, u16 bitmap
- * is sufficient. Regular bitmap_* is not used due to size reasons.
- */
-#define BTRFS_SUBPAGE_BITMAP_SIZE	16
-
 /*
  * Extra info for subpapge bitmap.
  *
@@ -44,10 +38,6 @@ struct btrfs_subpage_info {
 struct btrfs_subpage {
 	/* Common members for both data and metadata pages */
 	spinlock_t lock;
-	u16 uptodate_bitmap;
-	u16 error_bitmap;
-	u16 dirty_bitmap;
-	u16 writeback_bitmap;
 	/*
 	 * Both data and metadata needs to track how many readers are for the
 	 * page.
@@ -64,14 +54,11 @@ struct btrfs_subpage {
 		 * manages whether the subpage can be detached.
 		 */
 		atomic_t eb_refs;
-		/* Structures only used by data */
-		struct {
-			atomic_t writers;
 
-			/* Tracke pending ordered extent in this sector */
-			u16 ordered_bitmap;
-		};
+		/* Structures only used by data */
+		atomic_t writers;
 	};
+	unsigned long bitmaps[];
 };
 
 enum btrfs_subpage_type {
Signature · 1 line
2.32.0
#6 lore raw
Quoted 34 lines · Qu Wenruo

Currently we use u16 bitmap to make 4k sectorsize work for 64K page size.

But this u16 bitmap is not large enough to contain larger page size like 128K, nor is space efficient for 16K page size.

To handle both cases, here we pack all subpage bitmaps into a larger bitmap, now btrfs_subpage::bitmaps[] will be the ultimate bitmap for subpage usage.

Each sub-bitmap will has its start bit number recorded in btrfs_subpage_info::*_start, and its bitmap length will be recorded in btrfs_subpage_info::bitmap_nr_bits.

All subpage bitmap operations will be converted from using direct u16 operations to bitmap operations, with above *_start calculated.

For 64K page size with 4K sectorsize, this should not cause much difference. While for 16K page size, we will only need 1 unsigned long (u32) to restore the bitmap.
And will be able to support 128K page size in the future.

---
 fs/btrfs/extent_io.c |  58 ++++++++++--------
 fs/btrfs/subpage.c   | 137 +++++++++++++++++++++++++++++--------------
 fs/btrfs/subpage.h   |  19 +-----
 3 files changed, 130 insertions(+), 84 deletions(-)
fs/btrfs/extent_io.c +0 -0
index 543f87ea372e..e428d6208bb7 100644

fs/btrfs/extent_io.c static void find_next_dirty_byte(struct btrfs_fs_info *fs_info, · line 3869

Quoted 8 lines
fs/btrfs/extent_io.c +1 -3
@@ -3865,10 +3865,9 @@ static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
 	struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
 	u64 orig_start = *start;
 	/* Declare as unsigned long so we can use bitmap ops */
-	unsigned long dirty_bitmap;
 	unsigned long flags;
-	int nbits = (orig_start - page_offset(page)) >> fs_info->sectorsize_bits;
-	int range_start_bit = nbits;
+	int nbits = offset_in_page(orig_start) >> fs_info->sectorsize_bits;

nbits is rather dubious, by reading it I'd expect it to hold a count/number of bits. In fact it holds a sector index. A better name would be sector_index or block_index.

Quoted 23 lines
fs/btrfs/extent_io.c +7 -3
+	int range_start_bit = nbits + fs_info->subpage_info->dirty_start;
 	int range_end_bit;
 
 	/*
@@ -3883,11 +3882,14 @@ static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
 
 	/* We should have the page locked, but just in case */
 	spin_lock_irqsave(&subpage->lock, flags);
-	dirty_bitmap = subpage->dirty_bitmap;
+	bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit,
+			       fs_info->subpage_info->dirty_start +
+			       fs_info->subpage_info->bitmap_nr_bits);
 	spin_unlock_irqrestore(&subpage->lock, flags);
 
-	bitmap_next_set_region(&dirty_bitmap, &range_start_bit, &range_end_bit,
-			       BTRFS_SUBPAGE_BITMAP_SIZE);
+	range_start_bit -= fs_info->subpage_info->dirty_start;
+	range_end_bit -= fs_info->subpage_info->dirty_start;
+
 	*start = page_offset(page) + range_start_bit * fs_info->sectorsize;
 	*end = page_offset(page) + range_end_bit * fs_info->sectorsize;
 }

fs/btrfs/extent_io.c static int submit_eb_subpage(struct page *page, · line 4618

Quoted 5 lines
fs/btrfs/extent_io.c +1 -1
@@ -4613,7 +4615,7 @@ static int submit_eb_subpage(struct page *page,
 	int submitted = 0;
 	u64 page_start = page_offset(page);
 	int bit_start = 0;
-	const int nbits = BTRFS_SUBPAGE_BITMAP_SIZE;
+	const int nbits = fs_info->subpage_info->bitmap_nr_bits;

nit: do we really need nbits, given that fs_info->subpage_info->bitmap_nr_bits fits on one line inside the while expression. Also see the discrepancy - nbits here indeed holds a "number of bits value".

Quoted 3 lines
fs/btrfs/extent_io.c +0 -0
 	int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits;
 	int ret;
 

<snip>

Quoted 39 lines
fs/btrfs/extent_io.c +11 -15
@@ -7178,32 +7181,41 @@ void memmove_extent_buffer(const struct extent_buffer *dst,
 	}
 }
 
+#define GANG_LOOKUP_SIZE	16
 static struct extent_buffer *get_next_extent_buffer(
 		struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
 {
-	struct extent_buffer *gang[BTRFS_SUBPAGE_BITMAP_SIZE];
+	struct extent_buffer *gang[GANG_LOOKUP_SIZE];
 	struct extent_buffer *found = NULL;
 	u64 page_start = page_offset(page);
-	int ret;
-	int i;
+	u64 cur = page_start;
 
 	ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
-	ASSERT(PAGE_SIZE / fs_info->nodesize <= BTRFS_SUBPAGE_BITMAP_SIZE);
 	lockdep_assert_held(&fs_info->buffer_lock);
 
-	ret = radix_tree_gang_lookup(&fs_info->buffer_radix, (void **)gang,
-			bytenr >> fs_info->sectorsize_bits,
-			PAGE_SIZE / fs_info->nodesize);
-	for (i = 0; i < ret; i++) {
-		/* Already beyond page end */
-		if (gang[i]->start >= page_start + PAGE_SIZE)
-			break;
-		/* Found one */
-		if (gang[i]->start >= bytenr) {
-			found = gang[i];
-			break;
+	while (cur < page_start + PAGE_SIZE) {
+		int ret;
+		int i;
+
+		ret = radix_tree_gang_lookup(&fs_info->buffer_radix,
+				(void **)gang, cur >> fs_info->sectorsize_bits,
+				min_t(unsigned int, GANG_LOOKUP_SIZE,
+				      PAGE_SIZE / fs_info->nodesize));

fs/btrfs/extent_io.c void memmove_extent_buffer(const struct extent_buffer *dst, · line 7213

Quoted 10 lines
fs/btrfs/extent_io.c +10 -0
+		if (ret == 0)
+			goto out;
+		for (i = 0; i < ret; i++) {
+			/* Already beyond page end */
+			if (gang[i]->start >= page_start + PAGE_SIZE)
+				goto out;
+			/* Found one */
+			if (gang[i]->start >= bytenr) {
+				found = gang[i];
+				break;

Instead of break, don't you want to do got out? Break would exit from the inner for() loop and continue at the outter while loop.

Quoted 13 lines
fs/btrfs/extent_io.c +3 -0
+			}
 		}
+		cur = gang[ret - 1]->start + gang[ret - 1]->len;
 	}
+out:
 	return found;
 }
 
fs/btrfs/subpage.c +0 -0
index 014256d47beb..9d6da9f2d77e 100644

fs/btrfs/subpage.c int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info, · line 145

Quoted 7 lines
fs/btrfs/subpage.c +2 -0
@@ -139,10 +139,14 @@ int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
 			struct btrfs_subpage **ret,
 			enum btrfs_subpage_type type)
 {
+	unsigned int real_size;
+
 	if (fs_info->sectorsize == PAGE_SIZE)
 		return 0;

offtopic: Instead of having a bunch of those checks can't we replace them with ASSERTS and ensure that the decision whether we do subpage or not is taken at a higher level ?

fs/btrfs/subpage.c int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info, · line 148

Quoted 4 lines
fs/btrfs/subpage.c +2 -1
-	*ret = kzalloc(sizeof(struct btrfs_subpage), GFP_NOFS);
+	real_size = BITS_TO_LONGS(fs_info->subpage_info->total_nr_bits) *
+		    sizeof(unsigned long) + sizeof(struct btrfs_subpage);

real_size = struct_size(*ret, bitmaps, BITS_TO_LONGS(fs_info->subpage_info->total_nr_bits));

And the calling convention for this function is awful. Just make it return struct btrfs_subpage * and kill the dreadful **ret....

Quoted 14 lines
fs/btrfs/subpage.c +1 -6
+	*ret = kzalloc(real_size, GFP_NOFS);
 	if (!*ret)
 		return -ENOMEM;
 	spin_lock_init(&(*ret)->lock);
@@ -324,37 +328,60 @@ void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
 		unlock_page(page);
 }
 
-/*
- * Convert the [start, start + len) range into a u16 bitmap
- *
- * For example: if start == page_offset() + 16K, len = 16K, we get 0x00f0.
- */
-static u16 btrfs_subpage_calc_bitmap(const struct btrfs_fs_info *fs_info,

fs/btrfs/subpage.c void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info, · line 336

Quoted 10 lines
fs/btrfs/subpage.c +4 -4
-		struct page *page, u64 start, u32 len)
+static bool bitmap_test_range_all_set(unsigned long *addr, unsigned start,
+				      unsigned int nbits)
 {
-	const int bit_start = offset_in_page(start) >> fs_info->sectorsize_bits;
-	const int nbits = len >> fs_info->sectorsize_bits;
+	unsigned int found_zero;
 
-	btrfs_subpage_assert(fs_info, page, start, len);
+	found_zero = find_next_zero_bit(addr, start + nbits, start);

2 argument of find_next_zero_bit is 'size' which would be nbits as it expects the size to be in bits , not start + nbit. Every logical bitmap in ->bitmaps is defined by [start, nbits] no ? Unfortunately there is a discrepancy between the order of documentation and the order of actual arguments in the definition of this function....

Quoted 10 lines
fs/btrfs/subpage.c +4 -5
+	if (found_zero == start + nbits)
+		return true;
+	return false;
+}
 
-	/*
-	 * Here nbits can be 16, thus can go beyond u16 range. We make the
-	 * first left shift to be calculate in unsigned long (at least u32),
-	 * then truncate the result to u16.
-	 */

fs/btrfs/subpage.c void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info, · line 350

Quoted 10 lines
fs/btrfs/subpage.c +9 -1
-	return (u16)(((1UL << nbits) - 1) << bit_start);
+static bool bitmap_test_range_all_zero(unsigned long *addr, unsigned start,
+				       unsigned int nbits)
+{
+	unsigned int found_set;
+
+	found_set = find_next_bit(addr, start + nbits, start);
+	if (found_set == start + nbits)
+		return true;
+	return false;

This can be implemented by simply doing !bitmap_test_range_all_set no need to have 2 separate implementations. Given those 2 functions are only used in the definition oof the subpage_test_bitmap* macros I'da say remove one of them and simply code the macros as bitmap_test_range_all_set and !bitmap_test_range_all_set respectively.

Quoted 2 lines
fs/btrfs/subpage.c +0 -0
 }
 

<snip>

#7 lore raw
Quoted 41 lines · Nikolay Borisov
Quoted 38 lines · Qu Wenruo

Currently we use u16 bitmap to make 4k sectorsize work for 64K page size.

But this u16 bitmap is not large enough to contain larger page size like 128K, nor is space efficient for 16K page size.

To handle both cases, here we pack all subpage bitmaps into a larger bitmap, now btrfs_subpage::bitmaps[] will be the ultimate bitmap for subpage usage.

Each sub-bitmap will has its start bit number recorded in btrfs_subpage_info::*_start, and its bitmap length will be recorded in btrfs_subpage_info::bitmap_nr_bits.

All subpage bitmap operations will be converted from using direct u16 operations to bitmap operations, with above *_start calculated.

For 64K page size with 4K sectorsize, this should not cause much difference. While for 16K page size, we will only need 1 unsigned long (u32) to restore the bitmap.
And will be able to support 128K page size in the future.

---
  fs/btrfs/extent_io.c |  58 ++++++++++--------
  fs/btrfs/subpage.c   | 137 +++++++++++++++++++++++++++++--------------
  fs/btrfs/subpage.h   |  19 +-----
  3 files changed, 130 insertions(+), 84 deletions(-)
fs/btrfs/extent_io.c +0 -1
index 543f87ea372e..e428d6208bb7 100644
@@ -3865,10 +3865,9 @@ static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
  	struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
  	u64 orig_start = *start;
  	/* Declare as unsigned long so we can use bitmap ops */
-	unsigned long dirty_bitmap;
Quoted 8 lines

fs/btrfs/extent_io.c static void find_next_dirty_byte(struct btrfs_fs_info *fs_info, · line 3869

Quoted 4 lines
fs/btrfs/extent_io.c +1 -2
  	unsigned long flags;
-	int nbits = (orig_start - page_offset(page)) >> fs_info->sectorsize_bits;
-	int range_start_bit = nbits;
+	int nbits = offset_in_page(orig_start) >> fs_info->sectorsize_bits;

nbits is rather dubious, by reading it I'd expect it to hold a count/number of bits. In fact it holds a sector index. A better name would be sector_index or block_index.

Not sure if "sector" in the context of bitmap is preferred.

But since nbits is only used once, deleting nbits completely would be better.

Quoted 26 lines
Quoted 26 lines
fs/btrfs/extent_io.c +7 -3
+	int range_start_bit = nbits + fs_info->subpage_info->dirty_start;
  	int range_end_bit;
 
  	/*
@@ -3883,11 +3882,14 @@ static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
 
  	/* We should have the page locked, but just in case */
  	spin_lock_irqsave(&subpage->lock, flags);
-	dirty_bitmap = subpage->dirty_bitmap;
+	bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit,
+			       fs_info->subpage_info->dirty_start +
+			       fs_info->subpage_info->bitmap_nr_bits);
  	spin_unlock_irqrestore(&subpage->lock, flags);
 
-	bitmap_next_set_region(&dirty_bitmap, &range_start_bit, &range_end_bit,
-			       BTRFS_SUBPAGE_BITMAP_SIZE);
+	range_start_bit -= fs_info->subpage_info->dirty_start;
+	range_end_bit -= fs_info->subpage_info->dirty_start;
+
  	*start = page_offset(page) + range_start_bit * fs_info->sectorsize;
  	*end = page_offset(page) + range_end_bit * fs_info->sectorsize;
  }
@@ -4613,7 +4615,7 @@ static int submit_eb_subpage(struct page *page,
  	int submitted = 0;
  	u64 page_start = page_offset(page);
Quoted 8 lines

fs/btrfs/extent_io.c static int submit_eb_subpage(struct page *page, · line 4618

Quoted 3 lines
fs/btrfs/extent_io.c +1 -1
  	int bit_start = 0;
-	const int nbits = BTRFS_SUBPAGE_BITMAP_SIZE;
+	const int nbits = fs_info->subpage_info->bitmap_nr_bits;

nit: do we really need nbits, given that fs_info->subpage_info->bitmap_nr_bits fits on one line inside the while expression. Also see the discrepancy - nbits here indeed holds a "number of bits value".

Yeah, deleteing nbits completely here would be better.

Quoted 51 lines
Quoted 4 lines
fs/btrfs/extent_io.c +0 -0
  	int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits;
  	int ret;
 

<snip>

Quoted 44 lines
fs/btrfs/extent_io.c +16 -15
@@ -7178,32 +7181,41 @@ void memmove_extent_buffer(const struct extent_buffer *dst,
  	}
  }
 
+#define GANG_LOOKUP_SIZE	16
  static struct extent_buffer *get_next_extent_buffer(
  		struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
  {
-	struct extent_buffer *gang[BTRFS_SUBPAGE_BITMAP_SIZE];
+	struct extent_buffer *gang[GANG_LOOKUP_SIZE];
  	struct extent_buffer *found = NULL;
  	u64 page_start = page_offset(page);
-	int ret;
-	int i;
+	u64 cur = page_start;
 
  	ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
-	ASSERT(PAGE_SIZE / fs_info->nodesize <= BTRFS_SUBPAGE_BITMAP_SIZE);
  	lockdep_assert_held(&fs_info->buffer_lock);
 
-	ret = radix_tree_gang_lookup(&fs_info->buffer_radix, (void **)gang,
-			bytenr >> fs_info->sectorsize_bits,
-			PAGE_SIZE / fs_info->nodesize);
-	for (i = 0; i < ret; i++) {
-		/* Already beyond page end */
-		if (gang[i]->start >= page_start + PAGE_SIZE)
-			break;
-		/* Found one */
-		if (gang[i]->start >= bytenr) {
-			found = gang[i];
-			break;
+	while (cur < page_start + PAGE_SIZE) {
+		int ret;
+		int i;
+
+		ret = radix_tree_gang_lookup(&fs_info->buffer_radix,
+				(void **)gang, cur >> fs_info->sectorsize_bits,
+				min_t(unsigned int, GANG_LOOKUP_SIZE,
+				      PAGE_SIZE / fs_info->nodesize));
+		if (ret == 0)
+			goto out;
+		for (i = 0; i < ret; i++) {
+			/* Already beyond page end */
+			if (gang[i]->start >= page_start + PAGE_SIZE)
Quoted 8 lines

fs/btrfs/extent_io.c void memmove_extent_buffer(const struct extent_buffer *dst, · line 7213

Quoted 5 lines
fs/btrfs/extent_io.c +5 -0
+				goto out;
+			/* Found one */
+			if (gang[i]->start >= bytenr) {
+				found = gang[i];
+				break;

Instead of break, don't you want to do got out? Break would exit from the inner for() loop and continue at the outter while loop.

Damn, why my subpage tests didn't catch the problem...

I guess since the function is only called in try_release_subpage_extent_buffer(), and it only needs to exhaust all the ebs of the page, it doesn't really care about if we returned some wrong eb.

But still, it should be "goto out".

Thanks for catching this one.

Quoted 9 lines
Quoted 9 lines
fs/btrfs/extent_io.c +3 -0
+			}
  		}
+		cur = gang[ret - 1]->start + gang[ret - 1]->len;
  	}
+out:
  	return found;
  }
 
Quoted 16 lines

fs/btrfs/subpage.c int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info, · line 145

Quoted 12 lines
fs/btrfs/subpage.c +2 -0
index 014256d47beb..9d6da9f2d77e 100644
@@ -139,10 +139,14 @@ int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
  			struct btrfs_subpage **ret,
  			enum btrfs_subpage_type type)
  {
+	unsigned int real_size;
+
  	if (fs_info->sectorsize == PAGE_SIZE)
  		return 0;

offtopic: Instead of having a bunch of those checks can't we replace them with ASSERTS and ensure that the decision whether we do subpage or not is taken at a higher level ?

Nope, in this particular call site, btrfs_alloc_subpage() can be called with regular page size.

I guess it's better to rename this function like btrfs_prepare_page()?

Quoted 10 lines

fs/btrfs/subpage.c int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info, · line 148

Quoted 4 lines
fs/btrfs/subpage.c +2 -1
-	*ret = kzalloc(sizeof(struct btrfs_subpage), GFP_NOFS);
+	real_size = BITS_TO_LONGS(fs_info->subpage_info->total_nr_bits) *
+		    sizeof(unsigned long) + sizeof(struct btrfs_subpage);

real_size = struct_size(*ret, bitmaps, BITS_TO_LONGS(fs_info->subpage_info->total_nr_bits));

And the calling convention for this function is awful. Just make it return struct btrfs_subpage * and kill the dreadful **ret....

Above two advices indeed sound much better.

Quoted 23 lines
Quoted 23 lines
fs/btrfs/subpage.c +4 -9
+	*ret = kzalloc(real_size, GFP_NOFS);
  	if (!*ret)
  		return -ENOMEM;
  	spin_lock_init(&(*ret)->lock);
@@ -324,37 +328,60 @@ void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
  		unlock_page(page);
  }
 
-/*
- * Convert the [start, start + len) range into a u16 bitmap
- *
- * For example: if start == page_offset() + 16K, len = 16K, we get 0x00f0.
- */
-static u16 btrfs_subpage_calc_bitmap(const struct btrfs_fs_info *fs_info,
-		struct page *page, u64 start, u32 len)
+static bool bitmap_test_range_all_set(unsigned long *addr, unsigned start,
+				      unsigned int nbits)
  {
-	const int bit_start = offset_in_page(start) >> fs_info->sectorsize_bits;
-	const int nbits = len >> fs_info->sectorsize_bits;
+	unsigned int found_zero;
 
Quoted 8 lines

fs/btrfs/subpage.c void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info, · line 336

Quoted 2 lines
fs/btrfs/subpage.c +1 -1
-	btrfs_subpage_assert(fs_info, page, start, len);
+	found_zero = find_next_zero_bit(addr, start + nbits, start);

2 argument of find_next_zero_bit is 'size' which would be nbits as it expects the size to be in bits , not start + nbit. Every logical bitmap in ->bitmaps is defined by [start, nbits] no ? Unfortunately there is a discrepancy between the order of documentation and the order of actual arguments in the definition of this function....

IT'S A TRAP!

Paramater 2 (@size) is the total size of the search range, it should be larger than the 3rd parameter.

You can check the _find_next_bit(), where there are a lot of @start >= @nbits checks.

I guess this is the tricky part when we pack the bitmaps into one larger bitmap...

Quoted 19 lines
Quoted 19 lines
fs/btrfs/subpage.c +11 -6
+	if (found_zero == start + nbits)
+		return true;
+	return false;
+}
 
-	/*
-	 * Here nbits can be 16, thus can go beyond u16 range. We make the
-	 * first left shift to be calculate in unsigned long (at least u32),
-	 * then truncate the result to u16.
-	 */
-	return (u16)(((1UL << nbits) - 1) << bit_start);
+static bool bitmap_test_range_all_zero(unsigned long *addr, unsigned start,
+				       unsigned int nbits)
+{
+	unsigned int found_set;
+
+	found_set = find_next_bit(addr, start + nbits, start);
+	if (found_set == start + nbits)
Quoted 8 lines

fs/btrfs/subpage.c void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info, · line 350

Quoted 2 lines
fs/btrfs/subpage.c +2 -0
+		return true;
+	return false;

This can be implemented by simply doing !bitmap_test_range_all_set no need to have 2 separate implementations. Given those 2 functions are only used in the definition oof the subpage_test_bitmap* macros I'da say remove one of them and simply code the macros as bitmap_test_range_all_set and !bitmap_test_range_all_set respectively.

TRAP CARD!

bitmap_test_range_all_set() will only return 1 if all bits in the range are 1.

But bitmap_test_range_all_zero() should only return 1 if all bits in the range are 0.

What if only part of the range is 1?

Thanks,
Qu

Quoted 6 lines
Quoted 3 lines
fs/btrfs/subpage.c +0 -0
  }
 

<snip>

#8 lore raw
Quoted 29 lines · Qu Wenruo
Quoted 26 lines · Nikolay Borisov
Quoted 23 lines · Qu Wenruo

Currently we use u16 bitmap to make 4k sectorsize work for 64K page size.

But this u16 bitmap is not large enough to contain larger page size like 128K, nor is space efficient for 16K page size.

To handle both cases, here we pack all subpage bitmaps into a larger bitmap, now btrfs_subpage::bitmaps[] will be the ultimate bitmap for subpage usage.

Each sub-bitmap will has its start bit number recorded in btrfs_subpage_info::*_start, and its bitmap length will be recorded in btrfs_subpage_info::bitmap_nr_bits.

All subpage bitmap operations will be converted from using direct u16 operations to bitmap operations, with above *_start calculated.

For 64K page size with 4K sectorsize, this should not cause much difference.
While for 16K page size, we will only need 1 unsigned long (u32) to restore the bitmap.
And will be able to support 128K page size in the future.

<snip>

Quoted 9 lines
Quoted 4 lines

offtopic: Instead of having a bunch of those checks can't we replace them with ASSERTS and ensure that the decision whether we do subpage or not is taken at a higher level ?

Nope, in this particular call site, btrfs_alloc_subpage() can be called with regular page size.

I guess it's better to rename this function like btrfs_prepare_page()?

There are currently 2 callers:

btrfs_attach_subpage - this one only calls it iff sectorsize != alloc_subage

alloc_extent_buffer - here it's called unconditionally but that can easily be rectified with an if(subpage) check

<snip>

Quoted 10 lines
Quoted 5 lines

2 argument of find_next_zero_bit is 'size' which would be nbits as it expects the size to be in bits , not start + nbit. Every logical bitmap in ->bitmaps is defined by [start, nbits] no ? Unfortunately there is a discrepancy between the order of documentation and the order of actual arguments in the definition of this function....

IT'S A TRAP!

Paramater 2 (@size) is the total size of the search range, it should be larger than the 3rd parameter.

It's not even that it's the end index of the bit we are looking for. I.e if we want to check bits 20-29 we'd pass 20 as start, and 29 as size ... This is fucked, but it is what it is. I guess the documentation of the bits function is dodgy...

<snip>

#9 lore raw
Quoted 63 lines · Nikolay Borisov
Quoted 29 lines · Qu Wenruo
Quoted 26 lines · Nikolay Borisov
Quoted 23 lines · Qu Wenruo

Currently we use u16 bitmap to make 4k sectorsize work for 64K page size.

But this u16 bitmap is not large enough to contain larger page size like 128K, nor is space efficient for 16K page size.

To handle both cases, here we pack all subpage bitmaps into a larger bitmap, now btrfs_subpage::bitmaps[] will be the ultimate bitmap for subpage usage.

Each sub-bitmap will has its start bit number recorded in btrfs_subpage_info::*_start, and its bitmap length will be recorded in btrfs_subpage_info::bitmap_nr_bits.

All subpage bitmap operations will be converted from using direct u16 operations to bitmap operations, with above *_start calculated.

For 64K page size with 4K sectorsize, this should not cause much difference.
While for 16K page size, we will only need 1 unsigned long (u32) to restore the bitmap.
And will be able to support 128K page size in the future.

<snip>

Quoted 9 lines
Quoted 4 lines

offtopic: Instead of having a bunch of those checks can't we replace them with ASSERTS and ensure that the decision whether we do subpage or not is taken at a higher level ?

Nope, in this particular call site, btrfs_alloc_subpage() can be called with regular page size.

I guess it's better to rename this function like btrfs_prepare_page()?

There are currently 2 callers:

btrfs_attach_subpage - this one only calls it iff sectorsize != alloc_subage

alloc_extent_buffer - here it's called unconditionally but that can easily be rectified with an if(subpage) check

<snip>

Quoted 8 lines
Quoted 5 lines

2 argument of find_next_zero_bit is 'size' which would be nbits as it expects the size to be in bits , not start + nbit. Every logical bitmap in ->bitmaps is defined by [start, nbits] no ? Unfortunately there is a discrepancy between the order of documentation and the order of actual arguments in the definition of this function....

IT'S A TRAP!

Quoted 7 lines
Quoted 2 lines

Paramater 2 (@size) is the total size of the search range, it should be larger than the 3rd parameter.

It's not even that it's the end index of the bit we are looking for. I.e if we want to check bits 20-29 we'd pass 20 as start, and 29 as size ... This is fucked, but it is what it is. I guess the documentation of the bits function is dodgy...

Well, the size part is not correct IMHO.

Just a simple run could prove that:

number=0b0011100010111010101010001010011
find_next_zero_bit(&number, 3, 0)=2

In fact, if you pass 29 as size, it will not touch bit 29, and bit 28 is the last bit to be touched.

As that function will return @size to indicate that there is no matching bit found.

Thanks,
Qu

Quoted 3 lines

<snip>