[PATCH v3] md/raid0: use shifts for power-of-2 stripe mapping
From: Herman Semenoff
Date: Sun Nov 08 2026 - 14:43:29 EST
map_sector() does two 64-bit divisions (sector_div) for every bio:
the chunk offset on the member device, and the member disk index.
On low-latency NVMe, those divisions stall the submission path.
When a strip zone has a power-of-two device count and the chunk
size is a power of two, replace both divisions with a shift and a
mask. Store ilog2(nb_dev) in strip_zone->nb_dev_shift, or -1 when
nb_dev is not a power of two. Other layouts keep sector_div().
Also mark the bio split in raid0_make_request() unlikely(). The
usual path already splits bios in md_submit_bio().
Tested on an 8-disk Intel Optane Memory M10 RAID0 array:
Random 4K read QD1 latency: 22.06 us -> 21.10 us (-4.4%)
Random 4K read QD1 IOPS: 42.6k -> 44.6k (+4.7%)
Random 4K read QD32x8 IOPS: 1.11M -> 1.72M (+54.5%)
Non-power-of-2 arrays use the same fallback path as before.
Signed-off-by: Herman Semenoff <GermanAizek@xxxxxxxxx>
Signed-off-by: Herman Semenoff <GermanAizek@xxxxxxxxxxxxx>
---
v3:
- Shorten the subject and rewrap the message at 72 columns.
- Keep diff lines short so a mailer cannot split hunk headers.
drivers/md/raid0.c | 27 ++++++++++++++++++++-------
drivers/md/raid0.h | 1 +
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/drivers/md/raid0.c b/drivers/md/raid0.c
index 35e103f0c2c3..f0d650f8f44e 100644
--- a/drivers/md/raid0.c
+++ b/drivers/md/raid0.c
@@ -207,6 +207,8 @@
}
zone->nb_dev =3D cnt;
zone->zone_end =3D smallest->sectors * cnt;
+ zone->nb_dev_shift =3D is_power_of_2(zone->nb_dev) ?
+ ilog2(zone->nb_dev) : -1;
curr_zone_end =3D zone->zone_end;
@@ -246,6 +248,8 @@
}
zone->nb_dev =3D c;
+ zone->nb_dev_shift =3D is_power_of_2(zone->nb_dev) ?
+ ilog2(zone->nb_dev) : -1;
sectors =3D (smallest->sectors - zone->dev_start) * c;
pr_debug("md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
mdname(mddev),
@@ -330,6 +334,7 @@
struct r0conf *conf =3D mddev->private;
int raid_disks =3D conf->strip_zone[0].nb_dev;
unsigned int chunk_sects =3D mddev->chunk_sectors;
+ unsigned int disk_idx;
if (is_power_of_2(chunk_sects)) {
int chunksect_bits =3D ffz(~chunk_sects);
@@ -338,20 +343,28 @@
sector >>=3D chunksect_bits;
/* chunk in zone */
chunk =3D *sector_offset;
- /* quotient is the chunk in real device*/
- sector_div(chunk, zone->nb_dev << chunksect_bits);
- } else{
+ if (zone->nb_dev_shift >=3D 0) {
+ chunk >>=3D (zone->nb_dev_shift + chunksect_bits);
+ disk_idx =3D sector & (zone->nb_dev - 1);
+ } else {
+ /* quotient is the chunk in real device*/
+ sector_div(chunk, zone->nb_dev << chunksect_bits);
+ disk_idx =3D sector_div(sector, zone->nb_dev);
+ }
+ *sector_offset =3D (chunk << chunksect_bits) + sect_in_chunk;
+ } else {
sect_in_chunk =3D sector_div(sector, chunk_sects);
chunk =3D *sector_offset;
sector_div(chunk, chunk_sects * zone->nb_dev);
+ disk_idx =3D sector_div(sector, zone->nb_dev);
+ *sector_offset =3D (chunk * chunk_sects) + sect_in_chunk;
}
/*
* position the bio over the real device
* real sector =3D chunk in device + starting of zone
* + the position in the chunk
*/
- *sector_offset =3D (chunk * chunk_sects) + sect_in_chunk;
- return conf->devlist[(zone - conf->strip_zone)*raid_disks
- + sector_div(sector, zone->nb_dev)];
+ return conf->devlist[(zone - conf->strip_zone) * raid_disks +
+ disk_idx];
}
@@ -616,7 +629,7 @@
? (sector & (chunk_sects-1))
: sector_div(sector, chunk_sects));
- if (sectors < bio_sectors(bio)) {
+ if (unlikely(sectors < bio_sectors(bio))) {
bio =3D bio_submit_split_bioset(bio, sectors,
&mddev->bio_set);
if (!bio)
diff --git a/drivers/md/raid0.h b/drivers/md/raid0.h
index 8cc761ca7423..70b73511fb14 100644
--- a/drivers/md/raid0.h
+++ b/drivers/md/raid0.h
@@ -7,6 +7,7 @@
sector_t dev_start; /* Zone offset in real dev (in sectors) */
int nb_dev; /* # of devices attached to the zone */
int disk_shift; /* start disk for the original layout */
+ int nb_dev_shift; /* log2(nb_dev) if power of 2, else -1 */
};
/* Linux 3.14 (20d0189b101) made an unintended change to
--=20
2.55.0
base-commit: af5f12805e5cefa4fe68d6127c7e1fb78cd5535c
=D0=B2=D1=81, 29 =D0=BD=D0=BE=D1=8F=D0=B1. 2026=E2=80=AF=D0=B3. =D0=B2 05:5=
5, yu kuai <yukuai@xxxxxxx>:
>
> Hi,
>
> =E5=9C=A8 2026/11/22 1:25, Herman Semenoff =E5=86=99=E9=81=93:
> > From 450ebc4be1d31f25855c6771e67d27718ab6540f Mon Sep 17 00:00:00 2001
> > From: Herman Semenoff <GermanAizek@xxxxxxxxx>
> > Date: Sun, 8 Nov 2026 22:43:29 +0300
> > Subject: [PATCH v2] md/raid0: optimize stripe mapping with bitwise
> > shifts for power-of-2 devices
> >
> > In drivers/md/raid0.c, map_sector() performs two 64-bit integer divisio=
ns
> > (sector_div) on every submitted bio:
> > 1. sector_div(chunk, zone->nb_dev << chunksect_bits) to compute the =
chunk
> > offset within the member device.
> > 2. sector_div(sector, zone->nb_dev) to select the target member disk=
.
> >
> > For ultra-low latency solid-state storage devices, such as Intel Optane
> > (3D XPoint NVMe SSDs) capable of sub-10us latencies and multi-million I=
OPS,
> > these un-pipelined 64-bit division instructions create unnecessary CPU
> > pipeline stalls and latency overhead in the I/O submission hot path.
> >
> > When the number of member devices in a strip zone is a power of two
> > (e.g., 2, 4, 8, 16 disks, which is the predominant configuration for
> > NVMe arrays), both divisions can be replaced with bitwise shifts and
> > masks:
> > - chunk >>=3D (zone->nb_dev_shift + chunksect_bits)
> > - disk_idx =3D sector & (zone->nb_dev - 1)
> > - *sector_offset =3D (chunk << chunksect_bits) + sect_in_chunk
> >
> > Store nb_dev_shift (ilog2(zone->nb_dev)) in struct strip_zone during zo=
ne
> > initialization when is_power_of_2(zone->nb_dev) is true, or -1 otherwis=
e.
> > If nb_dev_shift is non-negative and chunk_sects is a power of two, fast
> > bitwise math is used with zero divisions. Arrays with non-power-of-2 di=
sk
> > counts fall back to the existing sector_div() path without any behavior
> > or compatibility change.
> >
> > In addition, annotate the bio split check in raid0_make_request() with
> > unlikely(), as standard MD devices already enforce chunk_sectors limits
> > via bio_split_to_limits() in md_submit_bio().
> >
> > Tested on an 8-disk Intel Optane Memory M10 (MEMPEK1J016GAD) RAID0 arra=
y:
> > - Random 4K read QD1 latency: avg lat 22.06 us -> 21.10 us (-4.4%)
> > - Random 4K read QD1 IOPS: 42.6k IOPS -> 44.6k IOPS (+4.7%=
)
> > - Random 4K read QD32x8 IOPS: 1.11M IOPS -> 1.72M IOPS (+54.5=
%)
> > - Non-power-of-2 disk arrays: unaffected, identical fallback path
> >
> > Signed-off-by: Herman Semenoff <GermanAizek@xxxxxxxxx>
> > Signed-off-by: Herman Semenoff <GermanAizek@xxxxxxxxxxxxx>
> > ---
> > drivers/md/raid0.c | 26 +++++++++++++++++++-------
> > drivers/md/raid0.h | 1 +
> > 2 files changed, 20 insertions(+), 7 deletions(-)
>
> This patch is still unacceptable, the format problem is still the same as=
v1.
>
> >
> > diff --git a/drivers/md/raid0.c b/drivers/md/raid0.c
> > index 35e103f0c2c3..aeb305077a5e 100644
> > --- a/drivers/md/raid0.c
> > +++ b/drivers/md/raid0.c
> > @@ -207,6 +207,8 @@ static int create_strip_zones(struct mddev *mddev,
> > struct r0conf **private_conf)
> > }
> > zone->nb_dev =3D cnt;
> > zone->zone_end =3D smallest->sectors * cnt;
> > + zone->nb_dev_shift =3D is_power_of_2(zone->nb_dev) ?
> > + ilog2(zone->nb_dev) : -1;
> >
> > curr_zone_end =3D zone->zone_end;
> >
> > @@ -246,6 +248,8 @@ static int create_strip_zones(struct mddev *mddev,
> > struct r0conf **private_conf)
> > }
> >
> > zone->nb_dev =3D c;
> > + zone->nb_dev_shift =3D is_power_of_2(zone->nb_dev) ?
> > + ilog2(zone->nb_dev) : -1;
> > sectors =3D (smallest->sectors - zone->dev_start) * c;
> > pr_debug("md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
> > mdname(mddev),
> > @@ -330,6 +334,7 @@ static struct md_rdev *map_sector(struct mddev
> > *mddev, struct strip_zone *zone,
> > struct r0conf *conf =3D mddev->private;
> > int raid_disks =3D conf->strip_zone[0].nb_dev;
> > unsigned int chunk_sects =3D mddev->chunk_sectors;
> > + unsigned int disk_idx;
> >
> > if (is_power_of_2(chunk_sects)) {
> > int chunksect_bits =3D ffz(~chunk_sects);
> > @@ -338,21 +343,28 @@ static struct md_rdev *map_sector(struct mddev
> > *mddev, struct strip_zone *zone,
> > sector >>=3D chunksect_bits;
> > /* chunk in zone */
> > chunk =3D *sector_offset;
> > - /* quotient is the chunk in real device*/
> > - sector_div(chunk, zone->nb_dev << chunksect_bits);
> > - } else{
> > + if (zone->nb_dev_shift >=3D 0) {
> > + chunk >>=3D (zone->nb_dev_shift + chunksect_bits);
> > + disk_idx =3D sector & (zone->nb_dev - 1);
> > + } else {
> > + /* quotient is the chunk in real device*/
> > + sector_div(chunk, zone->nb_dev << chunksect_bits);
> > + disk_idx =3D sector_div(sector, zone->nb_dev);
> > + }
> > + *sector_offset =3D (chunk << chunksect_bits) + sect_in_chunk;
> > + } else {
> > sect_in_chunk =3D sector_div(sector, chunk_sects);
> > chunk =3D *sector_offset;
> > sector_div(chunk, chunk_sects * zone->nb_dev);
> > + disk_idx =3D sector_div(sector, zone->nb_dev);
> > + *sector_offset =3D (chunk * chunk_sects) + sect_in_chunk;
> > }
> > /*
> > * position the bio over the real device
> > * real sector =3D chunk in device + starting of zone
> > * + the position in the chunk
> > */
> > - *sector_offset =3D (chunk * chunk_sects) + sect_in_chunk;
> > - return conf->devlist[(zone - conf->strip_zone)*raid_disks
> > - + sector_div(sector, zone->nb_dev)];
> > + return conf->devlist[(zone - conf->strip_zone) * raid_disks + disk_id=
x];
> > }
> >
> > static sector_t raid0_size(struct mddev *mddev, sector_t sectors, int
> > raid_disks)
> > @@ -616,7 +628,7 @@ static bool raid0_make_request(struct mddev
> > *mddev, struct bio *bio)
> > ? (sector & (chunk_sects-1))
> > : sector_div(sector, chunk_sects));
> >
> > - if (sectors < bio_sectors(bio)) {
> > + if (unlikely(sectors < bio_sectors(bio))) {
> > bio =3D bio_submit_split_bioset(bio, sectors,
> > &mddev->bio_set);
> > if (!bio)
> > diff --git a/drivers/md/raid0.h b/drivers/md/raid0.h
> > index 8cc761ca7423..70b73511fb14 100644
> > --- a/drivers/md/raid0.h
> > +++ b/drivers/md/raid0.h
> > @@ -7,6 +7,7 @@ struct strip_zone {
> > sector_t dev_start; /* Zone offset in real dev (in sectors) */
> > int nb_dev; /* # of devices attached to the zone */
> > int disk_shift; /* start disk for the original layout */
> > + int nb_dev_shift; /* log2(nb_dev) if power of 2, else -1 */
> > };
> >
> > /* Linux 3.14 (20d0189b101) made an unintended change to
>
> --
> Thanks,
> Kuai