Skip to contentSkip to conversation

block: Improve read ahead size for rotational devices

PATCH Applied Damien Le Moal 5 participants 8 messages Jun 16, 2025 – Jul 29, 2025

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

PATCH 1/1 block: Improve read ahead size for rotational devices

For a device that does not advertize an optimal I/O size, the function blk_apply_bdi_limits() defaults to an initial setting of the ra_pages field of struct backing_dev_info to VM_READAHEAD_PAGES, that is, 128 KB.

This low I/O size value is far from being optimal for hard-disk devices:
when reading files from multiple contexts using buffered I/Os, the seek overhead between the small read commands generated to read-ahead multiple files will significantly limit the performance that can be achieved.

This fact applies to all ATA devices as ATA does not define an optimal I/O size and the SCSI SAT specification does not define a default value to expose to the host.

Modify blk_apply_bdi_limits() to use a device max_sectors limit to calculate the ra_pages field of struct backing_dev_info, when the device is a rotational one (BLK_FEAT_ROTATIONAL feature is set). For a SCSI disk, this defaults to 2560 KB, which significantly improve performance for buffered reads. Using XFS and sequentially reading randomly selected (large) files stored on a SATA HDD, the maximum throughput achieved with 8 readers reading files with 1MB buffered I/Os increases from 122 MB/s to 167 MB/s (+36%). The improvement is even larger when reading files using 128 KB buffered I/Os, with a throughput increasing from 57 MB/s to 165 MB/s (+189%).

---
 block/blk-settings.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-settings.c +10 -2
index a000daafbfb4..66d402de9026 100644
@@ -58,16 +58,24 @@ EXPORT_SYMBOL(blk_set_stacking_limits);
 void blk_apply_bdi_limits(struct backing_dev_info *bdi,
 		struct queue_limits *lim)
 {
+	u64 io_opt = lim->io_opt;
+
 	/*
 	 * For read-ahead of large files to be effective, we need to read ahead
-	 * at least twice the optimal I/O size.
+	 * at least twice the optimal I/O size. For rotational devices that do
+	 * not report an optimal I/O size (e.g. ATA HDDs), use the maximum I/O
+	 * size to avoid falling back to the (rather inefficient) small default
+	 * read-ahead size.
 	 *
 	 * There is no hardware limitation for the read-ahead size and the user
 	 * might have increased the read-ahead size through sysfs, so don't ever
 	 * decrease it.
 	 */
+	if (!io_opt && (lim->features & BLK_FEAT_ROTATIONAL))
+		io_opt = (u64)lim->max_sectors << SECTOR_SHIFT;
+
 	bdi->ra_pages = max3(bdi->ra_pages,
-				lim->io_opt * 2 / PAGE_SIZE,
+				io_opt * 2 >> PAGE_SHIFT,
 				VM_READAHEAD_PAGES);
 	bdi->io_pages = lim->max_sectors >> PAGE_SECTORS_SHIFT;
 }
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Damien!

Quoted 3 lines

Modify blk_apply_bdi_limits() to use a device max_sectors limit to calculate the ra_pages field of struct backing_dev_info, when the device is a rotational one (BLK_FEAT_ROTATIONAL feature is set).

I much prefer doing it here. I don't think overriding io_opt in SCSI is appropriate. Applications and filesystems need to be able to determine whether a SCSI device reports an optimal I/O size or not. Overloading the queue limit with readahead semantics does not belong in SCSI.

Quoted 2 lines

For a SCSI disk, this defaults to 2560 KB, which significantly improve performance for buffered reads.

I believe this number came from a common RAID stripe configuration at the time. However, it's really not a great default and has caused problems with many devices that expect a power of two. Personally, I'd like this default to be something like 2MB or 4MB. MD, DM, and most hardware RAID devices report their stripe width correctly so the existing "RAID-friendly" default really shouldn't be needed.

Anyway. That's orthogonal to this particular change...

Signature · 1 line
Martin K. Petersen
#2 lore raw
Quoted 15 lines · Martin K. Petersen

Hi Damien!

Quoted 3 lines

Modify blk_apply_bdi_limits() to use a device max_sectors limit to calculate the ra_pages field of struct backing_dev_info, when the device is a rotational one (BLK_FEAT_ROTATIONAL feature is set).

I much prefer doing it here. I don't think overriding io_opt in SCSI is appropriate. Applications and filesystems need to be able to determine whether a SCSI device reports an optimal I/O size or not. Overloading the queue limit with readahead semantics does not belong in SCSI.

Quoted 2 lines

For a SCSI disk, this defaults to 2560 KB, which significantly improve performance for buffered reads.

Quoted 6 lines

I believe this number came from a common RAID stripe configuration at the time. However, it's really not a great default and has caused problems with many devices that expect a power of two. Personally, I'd like this default to be something like 2MB or 4MB. MD, DM, and most hardware RAID devices report their stripe width correctly so the existing "RAID-friendly" default really shouldn't be needed.

That sounds good. Recently, I have been doing a lot of performance benchmarks with large IOs on HDDs (2, 4 8 and 16 MB IOs). And with the improved memory allocation these days (transparent huge pages), even a simple malloc() IO buffer can have far less memory segments that the HBA maximum a majority of the time. So doing such large I/Os is fairly easy and really improves HDD performance. And in that context, the current default 1280 value for max_sectors_kb is really a limiting factor. So I am all for increasing the default to something like 4MB. I will send a patch.

Quoted 3 lines

Anyway. That's orthogonal to this particular change...

Thanks.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#3 lore raw
In reply to Martin K. Petersen
Quoted 9 lines · Martin K. Petersen
Quoted 2 lines

For a SCSI disk, this defaults to 2560 KB, which significantly improve performance for buffered reads.

I believe this number came from a common RAID stripe configuration at the time. However, it's really not a great default and has caused problems with many devices that expect a power of two. Personally, I'd like this default to be something like 2MB or 4MB. MD, DM, and most hardware RAID devices report their stripe width correctly so the existing "RAID-friendly" default really shouldn't be needed.

Yeah, that number always felt weird. I'm all for using a more round value here. But as you said, separate isue.

Reviewed-by on v1 1/1
#5 lore raw
In reply to Damien Le Moal
Quoted 44 lines · Damien Le Moal

For a device that does not advertize an optimal I/O size, the function blk_apply_bdi_limits() defaults to an initial setting of the ra_pages field of struct backing_dev_info to VM_READAHEAD_PAGES, that is, 128 KB.

This low I/O size value is far from being optimal for hard-disk devices:
when reading files from multiple contexts using buffered I/Os, the seek overhead between the small read commands generated to read-ahead multiple files will significantly limit the performance that can be achieved.

This fact applies to all ATA devices as ATA does not define an optimal I/O size and the SCSI SAT specification does not define a default value to expose to the host.

Modify blk_apply_bdi_limits() to use a device max_sectors limit to calculate the ra_pages field of struct backing_dev_info, when the device is a rotational one (BLK_FEAT_ROTATIONAL feature is set). For a SCSI disk, this defaults to 2560 KB, which significantly improve performance for buffered reads. Using XFS and sequentially reading randomly selected (large) files stored on a SATA HDD, the maximum throughput achieved with 8 readers reading files with 1MB buffered I/Os increases from 122 MB/s to 167 MB/s (+36%). The improvement is even larger when reading files using 128 KB buffered I/Os, with a throughput increasing from 57 MB/s to 165 MB/s (+189%).

---
  block/blk-settings.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-settings.c +3 -1
index a000daafbfb4..66d402de9026 100644
@@ -58,16 +58,24 @@ EXPORT_SYMBOL(blk_set_stacking_limits);
  void blk_apply_bdi_limits(struct backing_dev_info *bdi,
  		struct queue_limits *lim)
  {
+	u64 io_opt = lim->io_opt;
+
  	/*
  	 * For read-ahead of large files to be effective, we need to read ahead
-	 * at least twice the optimal I/O size.
+	 * at least twice the optimal I/O size. For rotational devices that do

block/blk-settings.c EXPORT_SYMBOL(blk_set_stacking_limits); · line 75

Quoted 10 lines
block/blk-settings.c +5 -0
+	 * not report an optimal I/O size (e.g. ATA HDDs), use the maximum I/O
+	 * size to avoid falling back to the (rather inefficient) small default
+	 * read-ahead size.
  	 *
  	 * There is no hardware limitation for the read-ahead size and the user
  	 * might have increased the read-ahead size through sysfs, so don't ever
  	 * decrease it.
  	 */
+	if (!io_opt && (lim->features & BLK_FEAT_ROTATIONAL))
+		io_opt = (u64)lim->max_sectors << SECTOR_SHIFT;

I'm still not sure it's even possible that lim->max_sectors << SECTOR_SHIFT overflows an unsigned int.

Anyway, FWIW:

#6 lore raw
In reply to Damien Le Moal
Quoted 25 lines · Damien Le Moal

For a device that does not advertize an optimal I/O size, the function blk_apply_bdi_limits() defaults to an initial setting of the ra_pages field of struct backing_dev_info to VM_READAHEAD_PAGES, that is, 128 KB.

This low I/O size value is far from being optimal for hard-disk devices:
when reading files from multiple contexts using buffered I/Os, the seek overhead between the small read commands generated to read-ahead multiple files will significantly limit the performance that can be achieved.

This fact applies to all ATA devices as ATA does not define an optimal I/O size and the SCSI SAT specification does not define a default value to expose to the host.

Modify blk_apply_bdi_limits() to use a device max_sectors limit to calculate the ra_pages field of struct backing_dev_info, when the device is a rotational one (BLK_FEAT_ROTATIONAL feature is set). For a SCSI disk, this defaults to 2560 KB, which significantly improve performance for buffered reads. Using XFS and sequentially reading randomly selected (large) files stored on a SATA HDD, the maximum throughput achieved with 8 readers reading files with 1MB buffered I/Os increases from 122 MB/s to 167 MB/s (+36%). The improvement is even larger when reading files using 128 KB buffered I/Os, with a throughput increasing from 57 MB/s to 165 MB/s (+189%).

Quoted 1 line

Jens,

Ping ? This is reviewed. Did it got lost ? Or will it be part of a different PR ?

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer Applied
#7 lore raw
In reply to Damien Le Moal
Quoted 11 lines · Damien Le Moal

For a device that does not advertize an optimal I/O size, the function blk_apply_bdi_limits() defaults to an initial setting of the ra_pages field of struct backing_dev_info to VM_READAHEAD_PAGES, that is, 128 KB.

This low I/O size value is far from being optimal for hard-disk devices:
when reading files from multiple contexts using buffered I/Os, the seek overhead between the small read commands generated to read-ahead multiple files will significantly limit the performance that can be achieved.

[...]

Applied, thanks!

[1/1] block: Improve read ahead size for rotational devices
(no commit info)

Best regards,

Signature · 1 line
Jens Axboe