Skip to contentSkip to conversation

block: simplify with PAGE_SECTORS_SHIFT

PATCH Superseded Luis Chamberlain 7 participants 16 messages Apr 21, 2023 – Apr 24, 2023

Subsystems (7)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover block: simplify with PAGE_SECTORS_SHIFT

A bit of block drivers have their own incantations with PAGE_SHIFT - SECTOR_SHIFT. Just simplfy and use PAGE_SECTORS_SHIFT all over.

Based on linux-next next-20230421.

Luis Chamberlain (5):
dm integrity: simplify by using PAGE_SECTORS_SHIFT
drbd: use PAGE_SECTORS_SHIFT and PAGE_SECTORS
iomap: simplify iomap_init() with PAGE_SECTORS dm bufio: simplify by using PAGE_SECTORS_SHIFT
zram: use generic PAGE_SECTORS and PAGE_SECTORS_SHIFT

 drivers/block/drbd/drbd_bitmap.c |  4 ++--
 drivers/block/zram/zram_drv.c    | 12 ++++++------
 drivers/block/zram/zram_drv.h    |  2 --
 drivers/md/dm-bufio.c            |  4 ++--
 drivers/md/dm-integrity.c        | 10 +++++-----
 fs/iomap/buffered-io.c           |  2 +-
 6 files changed, 16 insertions(+), 18 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/5 dm integrity: simplify by using PAGE_SECTORS_SHIFT

The PAGE_SHIFT - SECTOR_SHIFT constant be replaced with PAGE_SECTORS_SHIFT defined in linux/blt_types.h, which is included by linux/blkdev.h.

This produces no functional changes.

---
 drivers/md/dm-integrity.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/md/dm-integrity.c +5 -5
index 31838b13ea54..a179265970dd 100644
@@ -752,7 +752,7 @@ static void page_list_location(struct dm_integrity_c *ic, unsigned int section,
 
 	sector = section * ic->journal_section_sectors + offset;
 
-	*pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
+	*pl_index = sector >> (PAGE_SECTORS_SHIFT);
 	*pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
 }
 
@@ -1077,7 +1077,7 @@ static void rw_journal_sectors(struct dm_integrity_c *ic, blk_opf_t opf,
 		return;
 	}
 
-	pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
+	pl_index = sector >> (PAGE_SECTORS_SHIFT);
 	pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
 
 	io_req.bi_opf = opf;
@@ -1201,7 +1201,7 @@ static void copy_from_journal(struct dm_integrity_c *ic, unsigned int section, u
 
 	sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
 
-	pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
+	pl_index = sector >> (PAGE_SECTORS_SHIFT);
 	pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
 
 	io_req.bi_opf = REQ_OP_WRITE;
@@ -3765,7 +3765,7 @@ static int create_journal(struct dm_integrity_c *ic, char **error)
 	ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
 
 	journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
-				PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
+				PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SECTORS_SHIFT);
 	journal_desc_size = journal_pages * sizeof(struct page_list);
 	if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) {
 		*error = "Journal doesn't fit into memory";
@@ -4542,7 +4542,7 @@ static int dm_integrity_ctr(struct dm_target *ti, unsigned int argc, char **argv
 			spin_lock_init(&bbs->bio_queue_lock);
 
 			sector = i * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT);
-			pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
+			pl_index = sector >> (PAGE_SECTORS_SHIFT);
 			pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
 
 			bbs->bitmap = lowmem_page_address(ic->journal[pl_index].page) + pl_offset;
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH 1/5] dm integrity: simplify by using PAGE_SECTORS_SHIFT

drivers/md/dm-integrity.c static void page_list_location(struct dm_integrity_c *ic, unsigned int section, · line 755

Quoted 2 lines · Luis Chamberlain
drivers/md/dm-integrity.c +1 -1
-	*pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
+	*pl_index = sector >> (PAGE_SECTORS_SHIFT);

You could/should remove the () around PAGE_SECTORS_SHIFT

(throughout)

#3 lore raw

PATCH 2/5 drbd: use PAGE_SECTORS_SHIFT and PAGE_SECTORS

In reply to Luis Chamberlain

Replace common constants with generic versions.
This produces no functional changes.

---
 drivers/block/drbd/drbd_bitmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/drbd/drbd_bitmap.c +2 -2
index 6ac8c54b44c7..b556e6634f13 100644
@@ -1000,7 +1000,7 @@ static void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_ho
 	unsigned int len;
 
 	first_bm_sect = device->ldev->md.md_offset + device->ldev->md.bm_offset;
-	on_disk_sector = first_bm_sect + (((sector_t)page_nr) << (PAGE_SHIFT-SECTOR_SHIFT));
+	on_disk_sector = first_bm_sect + (((sector_t)page_nr) << PAGE_SECTORS_SHIFT);
 
 	/* this might happen with very small
 	 * flexible external meta data device,
@@ -1008,7 +1008,7 @@ static void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_ho
 	last_bm_sect = drbd_md_last_bitmap_sector(device->ldev);
 	if (first_bm_sect <= on_disk_sector && last_bm_sect >= on_disk_sector) {
 		sector_t len_sect = last_bm_sect - on_disk_sector + 1;
-		if (len_sect < PAGE_SIZE/SECTOR_SIZE)
+		if (len_sect < PAGE_SECTORS)
 			len = (unsigned int)len_sect*SECTOR_SIZE;
 		else
 			len = PAGE_SIZE;
Signature · 1 line
2.39.2
maintainer Acked-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] drbd: use PAGE_SECTORS_SHIFT and PAGE_SECTORS

Am 21.04.23 um 21:58 schrieb Luis Chamberlain:

Quoted 22 lines

Replace common constants with generic versions.
This produces no functional changes.

---
 drivers/block/drbd/drbd_bitmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/drbd/drbd_bitmap.c +1 -1
index 6ac8c54b44c7..b556e6634f13 100644
@@ -1000,7 +1000,7 @@ static void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_ho
 	unsigned int len;
 
 	first_bm_sect = device->ldev->md.md_offset + device->ldev->md.bm_offset;
-	on_disk_sector = first_bm_sect + (((sector_t)page_nr) << (PAGE_SHIFT-SECTOR_SHIFT));
+	on_disk_sector = first_bm_sect + (((sector_t)page_nr) << PAGE_SECTORS_SHIFT);
 
 	/* this might happen with very small
 	 * flexible external meta data device,

drivers/block/drbd/drbd_bitmap.c static void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_ho · line 1014

Quoted 8 lines
drivers/block/drbd/drbd_bitmap.c +1 -1
@@ -1008,7 +1008,7 @@ static void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_ho
 	last_bm_sect = drbd_md_last_bitmap_sector(device->ldev);
 	if (first_bm_sect <= on_disk_sector && last_bm_sect >= on_disk_sector) {
 		sector_t len_sect = last_bm_sect - on_disk_sector + 1;
-		if (len_sect < PAGE_SIZE/SECTOR_SIZE)
+		if (len_sect < PAGE_SECTORS)
 			len = (unsigned int)len_sect*SECTOR_SIZE;
 		else
 			len = PAGE_SIZE;
Signature · 3 lines
Christoph Böhmwalder
LINBIT | Keeping the Digital World Running
DRBD HA —  Disaster Recovery — Software defined Storage
#5 lore raw

PATCH 3/5 iomap: simplify iomap_init() with PAGE_SECTORS

In reply to Luis Chamberlain

Just use the PAGE_SECTORS generic define. This produces no functional changes. While at it use left shift to simplify this even further.

---
 fs/iomap/buffered-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/iomap/buffered-io.c +1 -1
index 063133ec77f4..ba2824f405df 100644
@@ -1819,7 +1819,7 @@ EXPORT_SYMBOL_GPL(iomap_writepages);
 
 static int __init iomap_init(void)
 {
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, PAGE_SECTORS << 2,
 			   offsetof(struct iomap_ioend, io_inline_bio),
 			   BIOSET_NEED_BVECS);
 }
Signature · 1 line
2.39.2
#6 lore raw

Re: [PATCH 3/5] iomap: simplify iomap_init() with PAGE_SECTORS

Quoted 2 lines · Luis Chamberlain

Just use the PAGE_SECTORS generic define. This produces no functional changes. While at it use left shift to simplify this even further.

How is FOO << 2 simpler than FOO * 4?

Quoted 2 lines
fs/iomap/buffered-io.c +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, PAGE_SECTORS << 2,
#7 lore raw

Re: [PATCH 3/5] iomap: simplify iomap_init() with PAGE_SECTORS

Quoted 8 lines · Matthew Wilcox
Quoted 2 lines · Luis Chamberlain

Just use the PAGE_SECTORS generic define. This produces no functional changes. While at it use left shift to simplify this even further.

How is FOO << 2 simpler than FOO * 4?

fs/iomap/buffered-io.c EXPORT_SYMBOL_GPL(iomap_writepages); · line 1822

Quoted 2 lines
fs/iomap/buffered-io.c +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, PAGE_SECTORS << 2,

We could just do:

- return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+ return bioset_init(&iomap_ioend_bioset, 4 * PAGE_SECTORS,

The shift just seemed optimal if we're just going to change it.

Luis

maintainer
#8 lore raw

Re: [PATCH 3/5] iomap: simplify iomap_init() with PAGE_SECTORS

Quoted 16 lines · Luis Chamberlain
Quoted 8 lines · Matthew Wilcox
Quoted 2 lines · Luis Chamberlain

Just use the PAGE_SECTORS generic define. This produces no functional changes. While at it use left shift to simplify this even further.

How is FOO << 2 simpler than FOO * 4?

fs/iomap/buffered-io.c EXPORT_SYMBOL_GPL(iomap_writepages); · line 1822

Quoted 2 lines
fs/iomap/buffered-io.c +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, PAGE_SECTORS << 2,

We could just do:

(quoted diff) +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, 4 * PAGE_SECTORS,
Quoted 1 line

The shift just seemed optimal if we're just going to change it.

It's going to generate the same code, but the multiplication is arguably easier to read (or harder to misread).

Signature · 1 line
Jens Axboe
#9 lore raw

Re: [PATCH 3/5] iomap: simplify iomap_init() with PAGE_SECTORS

Quoted 14 lines · Jens Axboe
Quoted 13 lines · Luis Chamberlain
Quoted 8 lines · Matthew Wilcox
Quoted 2 lines · Luis Chamberlain

Just use the PAGE_SECTORS generic define. This produces no functional changes. While at it use left shift to simplify this even further.

How is FOO << 2 simpler than FOO * 4?

fs/iomap/buffered-io.c EXPORT_SYMBOL_GPL(iomap_writepages); · line 1822

Quoted 2 lines
fs/iomap/buffered-io.c +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, PAGE_SECTORS << 2,

We could just do:

Quoted 7 lines
Quoted 4 lines
(quoted diff) +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, 4 * PAGE_SECTORS,

The shift just seemed optimal if we're just going to change it.

It's going to generate the same code, but the multiplication is arguably easier to read (or harder to misread).

Then let's stick with the 4 * PAGE_SECTORS. Let me know if you need another patch.

Luis

maintainer
#10 lore raw

Re: [PATCH 3/5] iomap: simplify iomap_init() with PAGE_SECTORS

Quoted 18 lines · Luis Chamberlain
Quoted 17 lines · Jens Axboe
Quoted 16 lines · Luis Chamberlain
Quoted 8 lines · Matthew Wilcox
Quoted 2 lines · Luis Chamberlain

Just use the PAGE_SECTORS generic define. This produces no functional changes. While at it use left shift to simplify this even further.

How is FOO << 2 simpler than FOO * 4?

fs/iomap/buffered-io.c EXPORT_SYMBOL_GPL(iomap_writepages); · line 1822

Quoted 2 lines
fs/iomap/buffered-io.c +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, PAGE_SECTORS << 2,

We could just do:

(quoted diff) +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, 4 * PAGE_SECTORS,
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The shift just seemed optimal if we're just going to change it.

It's going to generate the same code, but the multiplication is arguably easier to read (or harder to misread).

Then let's stick with the 4 * PAGE_SECTORS. Let me know if you need another patch.

Just send out a v2 at some point, you've also got a number of cases where there are superfluous parenthesis, at least in patch 4, and Willy pointed one out in an earlier patch too. Didn't check the last one.

This will be 6.5 anyway I think, I already sent out the changes for the 6.4 merge window.

Signature · 1 line
Jens Axboe
#11 lore raw

Re: [PATCH 3/5] iomap: simplify iomap_init() with PAGE_SECTORS

In reply to Luis Chamberlain
Quoted 13 lines · Luis Chamberlain
Quoted 8 lines · Matthew Wilcox
Quoted 2 lines · Luis Chamberlain

Just use the PAGE_SECTORS generic define. This produces no functional changes. While at it use left shift to simplify this even further.

How is FOO << 2 simpler than FOO * 4?

fs/iomap/buffered-io.c EXPORT_SYMBOL_GPL(iomap_writepages); · line 1822

Quoted 2 lines
fs/iomap/buffered-io.c +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, PAGE_SECTORS << 2,

We could just do:

Review

Quoted 2 lines
(quoted diff) +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, 4 * PAGE_SECTORS,

Yes, please.

Quoted 1 line

The shift just seemed optimal if we're just going to change it.

Nope, it's just premature optimisation at the expense of maintainability. The compiler will optimise the multiplication into shifts if that is the fastest way to do it for the given architecture the code is being compiled to.

-Dave.

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

Re: [PATCH 3/5] iomap: simplify iomap_init() with PAGE_SECTORS

Quoted 6 lines · Dave Chinner

Review

Quoted 3 lines
(quoted diff) +1 -1
-	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
+	return bioset_init(&iomap_ioend_bioset, 4 * PAGE_SECTORS,

Yes, please.

Quoted 6 lines
Quoted 1 line

The shift just seemed optimal if we're just going to change it.

Nope, it's just premature optimisation at the expense of maintainability. The compiler will optimise the multiplication into shifts if that is the fastest way to do it for the given architecture the code is being compiled to.

We still had cases of the compiler not doing obvious multiplication/division to shift conversion lately. That being said:

1) this is an initialization path, no one actually cares
2) we're dealing with constants here, and compilers are really good at constant folding

so yes, this should be using the much more readable version.

#13 lore raw

PATCH 4/5 dm bufio: simplify by using PAGE_SECTORS_SHIFT

In reply to Luis Chamberlain

The PAGE_SHIFT - SECTOR_SHIFT constant be replaced with PAGE_SECTORS_SHIFT defined in linux/blt_types.h, which is included by linux/blkdev.h.

This produces no functional changes.

---
 drivers/md/dm-bufio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/md/dm-bufio.c +2 -2
index eea977662e81..08c4730e1819 100644
@@ -1152,7 +1152,7 @@ static void *alloc_buffer_data(struct dm_bufio_client *c, gfp_t gfp_mask,
 	    gfp_mask & __GFP_NORETRY) {
 		*data_mode = DATA_MODE_GET_FREE_PAGES;
 		return (void *)__get_free_pages(gfp_mask,
-						c->sectors_per_block_bits - (PAGE_SHIFT - SECTOR_SHIFT));
+						c->sectors_per_block_bits - (PAGE_SECTORS_SHIFT));
 	}
 
 	*data_mode = DATA_MODE_VMALLOC;
@@ -1190,7 +1190,7 @@ static void free_buffer_data(struct dm_bufio_client *c,
 
 	case DATA_MODE_GET_FREE_PAGES:
 		free_pages((unsigned long)data,
-			   c->sectors_per_block_bits - (PAGE_SHIFT - SECTOR_SHIFT));
+			   c->sectors_per_block_bits - (PAGE_SECTORS_SHIFT));
 		break;
 
 	case DATA_MODE_VMALLOC:
Signature · 1 line
2.39.2
#14 lore raw

PATCH 5/5 zram: use generic PAGE_SECTORS and PAGE_SECTORS_SHIFT

In reply to Luis Chamberlain

Instead of re-defining the already existing constants use the provided ones:

So replace:

o SECTORS_PER_PAGE_SHIFT with PAGE_SECTORS_SHIFT
o SECTORS_PER_PAGE with PAGE_SECTORS

This produces no functional changes.

---
 drivers/block/zram/zram_drv.c | 12 ++++++------
 drivers/block/zram/zram_drv.h |  2 --
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/block/zram/zram_drv.c +6 -6
index a84c4268257a..11c9b5a9ac7a 100644
@@ -1834,8 +1834,8 @@ static ssize_t recompress_store(struct device *dev,
 static void zram_bio_discard(struct zram *zram, struct bio *bio)
 {
 	size_t n = bio->bi_iter.bi_size;
-	u32 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
-	u32 offset = (bio->bi_iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
+	u32 index = bio->bi_iter.bi_sector >> PAGE_SECTORS_SHIFT;
+	u32 offset = (bio->bi_iter.bi_sector & (PAGE_SECTORS - 1)) <<
 			SECTOR_SHIFT;
 
 	/*
@@ -1876,8 +1876,8 @@ static void zram_bio_read(struct zram *zram, struct bio *bio)
 
 	start_time = bio_start_io_acct(bio);
 	bio_for_each_segment(bv, bio, iter) {
-		u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
-		u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
+		u32 index = iter.bi_sector >> PAGE_SECTORS_SHIFT;
+		u32 offset = (iter.bi_sector & (PAGE_SECTORS - 1)) <<
 				SECTOR_SHIFT;
 
 		if (zram_bvec_read(zram, &bv, index, offset, bio) < 0) {
@@ -1903,8 +1903,8 @@ static void zram_bio_write(struct zram *zram, struct bio *bio)
 
 	start_time = bio_start_io_acct(bio);
 	bio_for_each_segment(bv, bio, iter) {
-		u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
-		u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
+		u32 index = iter.bi_sector >> PAGE_SECTORS_SHIFT;
+		u32 offset = (iter.bi_sector & (PAGE_SECTORS - 1)) <<
 				SECTOR_SHIFT;
 
 		if (zram_bvec_write(zram, &bv, index, offset, bio) < 0) {
drivers/block/zram/zram_drv.h +0 -2
index ca7a15bd4845..9f2543af5c76 100644
@@ -21,8 +21,6 @@
 
 #include "zcomp.h"
 
-#define SECTORS_PER_PAGE_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
-#define SECTORS_PER_PAGE	(1 << SECTORS_PER_PAGE_SHIFT)
 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
 #define ZRAM_LOGICAL_BLOCK_SIZE	(1 << ZRAM_LOGICAL_BLOCK_SHIFT)
 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK	\
Signature · 1 line
2.39.2
maintainer Reviewed-by on v1 5/5
#15 lore raw

Re: [PATCH 5/5] zram: use generic PAGE_SECTORS and PAGE_SECTORS_SHIFT

Quoted 11 lines · Luis Chamberlain

Instead of re-defining the already existing constants use the provided ones:

So replace:

o SECTORS_PER_PAGE_SHIFT with PAGE_SECTORS_SHIFT
o SECTORS_PER_PAGE with PAGE_SECTORS

This produces no functional changes.