Enable testing small DMA segment sizes
v2 cover Enable testing small DMA segment sizes
Hi Jens,
About one year ago support was merged for DMA segment sizes smaller than the virtual memory page size. No blktests exist yet for the new codepaths related to small segment size support. This patch series makes it possible to test these code paths on a system (e.g. a VM) with 4 KiB pages.
The corresponding blktest patch is available here:
https://lore.kernel.org/linux-block/[email protected]/
Please consider this patch series for the next merge window.
Changes compared to v1:
- Addressed Damien's comment and made the null_blk kernel module parameter more clear.
- Added three patches to this series: one patch that fixes a source code comment and two patches that reduce the number of users of the BLK_MIN_SEGMENT_SIZE constant.
Bart Van Assche (5):
block: Fix a source code comment
block: Fix the max_user_sectors lower bound
block: Remove a DMA segment boundary mask check
block: Reduce the minimum value for the maximum DMA segment size
null_blk: Support configuring the maximum DMA segment size
block/blk-settings.c | 14 ++++---------- block/blk.h | 2 +- drivers/block/null_blk/main.c | 13 +++++++++++++ drivers/block/null_blk/null_blk.h | 1 + 4 files changed, 19 insertions(+), 11 deletions(-)
PATCH v2 1/5 block: Fix a source code comment
Fix a source code comment that is no longer correct since commit 889c57066cee ("block: make segment size limit workable for > 4K PAGE_SIZE").
--- block/blk-settings.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
block/blk-settings.c +2 -2
@@ -470,8 +470,8 @@ int blk_validate_limits(struct queue_limits *lim) }else {/* * The maximum segment size has an odd historic 64k default that -* drivers probably should override. Just like the I/O size we -* require drivers to at least handle a full page per segment. +* drivers probably should override. The maximum DMA segment +* size may be less than the virtual memory page size. */ if (!lim->max_segment_size) lim->max_segment_size =BLK_MAX_SEGMENT_SIZE ;
Re: [PATCH v2 1/5] block: Fix a source code comment
Quoted 6 lines · Bart Van Assche
Fix a source code comment that is no longer correct since commit
889c57066cee("block: make segment size limit workable for > 4KPAGE_SIZE").Cc Ming Lei <[email protected]>Signed-off-by Bart Van Assche <[email protected]>
Looks good:
Re: [PATCH v2 1/5] block: Fix a source code comment
In reply to Bart Van AsscheQuoted 6 lines · Bart Van Assche
Fix a source code comment that is no longer correct since commit
889c57066cee("block: make segment size limit workable for > 4KPAGE_SIZE").Cc Ming Lei <[email protected]>Signed-off-by Bart Van Assche <[email protected]>
Thanks,
Ming
PATCH v2 2/5 block: Fix the max_user_sectors lower bound
In reply to Bart Van AsscheThe lowest value that can be supported for lim->max_user_sectors is the logical block size. This patch prepares for reducing BLK_MIN_SEGMENT_SIZE to a value that may be less than the logical block size.
--- block/blk-settings.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-settings.c +1 -1
@@ -403,7 +403,7 @@ int blk_validate_limits(struct queue_limits *lim) max_hw_sectors =min_not_zero (lim->max_hw_sectors, lim->max_dev_sectors);if (lim->max_user_sectors) { -if (lim->max_user_sectors <BLK_MIN_SEGMENT_SIZE /SECTOR_SIZE ) +if (lim->max_user_sectors < lim->logical_block_size /SECTOR_SIZE )return -EINVAL ; lim->max_sectors =min (max_hw_sectors, lim->max_user_sectors); }else if (lim->io_opt > (BLK_DEF_MAX_SECTORS_CAP <<SECTOR_SHIFT )) {
Re: [PATCH v2 2/5] block: Fix the max_user_sectors lower bound
Quoted 16 lines · Bart Van Assche
The lowest value that can be supported for
lim->max_user_sectorsis the logical block size. This patch prepares for reducingBLK_MIN_SEGMENT_SIZEto a value that may be less than the logical block size.Cc Ming Lei <[email protected]>Fixes 889c57066cee ("block: make segment size limit workable for > 4K PAGE_SIZE")Signed-off-by Bart Van Assche <[email protected]>--- block/blk-settings.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)block/blk-settings.c +0
-0
block/blk-settings.c int blk_validate_limits(struct queue_limits *lim) · line 406
Quoted 5 lines
block/blk-settings.c +1
-1@@ -403,7 +403,7 @@ int blk_validate_limits(struct queue_limits *lim) max_hw_sectors =min_not_zero (lim->max_hw_sectors, lim->max_dev_sectors);if (lim->max_user_sectors) { -if (lim->max_user_sectors <BLK_MIN_SEGMENT_SIZE /SECTOR_SIZE ) +if (lim->max_user_sectors < lim->logical_block_size /SECTOR_SIZE )
Overly long line.
Re: [PATCH v2 2/5] block: Fix the max_user_sectors lower bound
In reply to Bart Van AsscheQuoted 6 lines · Bart Van Assche
The lowest value that can be supported for
lim->max_user_sectorsis the logical block size. This patch prepares for reducingBLK_MIN_SEGMENT_SIZEto a value that may be less than the logical block size.Cc Ming Lei <[email protected]>Fixes 889c57066cee ("block: make segment size limit workable for > 4K PAGE_SIZE")
Before commit 889c57066cee, PAGE_SIZE is applied for the limit, so you don't fix commit 889c57066cee.
The 1st bad commit is actually commit d690cb8ae14b ("block: add an API to atomically update queue limits').
Please fix the fix tag, otherwise feel free to add:
Thanks,
Ming
PATCH v2 3/5 block: Remove a DMA segment boundary mask check
In reply to Bart Van AsscheCommit d690cb8ae14b ("block: add an API to atomically update queue limits") introduced the following code:
/*
* By default there is no limit on the segment boundary alignment,
* but if there is one it can't be smaller than the page size as
* that would break all the normal I/O patterns.
*/
if (!lim->seg_boundary_mask)
lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
if (WARN_ON_ONCE(lim->seg_boundary_mask < PAGE_SIZE - 1))
return -EINVAL;The comment about "breaking normal I/O patterns" is incorrect - block layer code like get_max_segment_size() supports arbitrary boundary mask values as long as the boundary mask is not zero. Remove the comment and the check because both are confusing.
This patch prepares for reducing the value of BLK_MIN_SEGMENT_SIZE.
--- block/blk-settings.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
block/blk-settings.c +1 -7
@@ -447,15 +447,9 @@ int blk_validate_limits(struct queue_limits *lim)if (!lim->max_discard_segments) lim->max_discard_segments =1 ; -/* -* By default there is no limit on the segment boundary alignment, -* but if there is one it can't be smaller than the page size as -* that would break all the normal I/O patterns. -*/ +/* By default there is no limit on the segment boundary alignment. */ if (!lim->seg_boundary_mask) lim->seg_boundary_mask =BLK_SEG_BOUNDARY_MASK ; -if (WARN_ON_ONCE (lim->seg_boundary_mask <BLK_MIN_SEGMENT_SIZE -1 )) -return -EINVAL ;/* * Stacking device may have both virtual boundary and max segment
Re: [PATCH v2 3/5] block: Remove a DMA segment boundary mask check
Quoted 34 lines · Bart Van Assche
Commit
d690cb8ae14b("block: add an API to atomically update queue limits") introduced the following code:/* * By default there is no limit on the segment boundary alignment, * but if there is one it can't be smaller than the page size as * that would break all the normal I/O patterns. */ if (!lim->seg_boundary_mask) lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; if (WARN_ON_ONCE(lim->seg_boundary_mask < PAGE_SIZE - 1)) return -EINVAL;The comment about "breaking normal I/O patterns" is incorrect - block layer code like
get_max_segment_size()supports arbitrary boundary mask values as long as the boundary mask is not zero. Remove the comment and the check because both are confusing.This patch prepares for reducing the value of
BLK_MIN_SEGMENT_SIZE.Fixes d690cb8ae14b ("block: add an API to atomically update queue limits")Signed-off-by Bart Van Assche <[email protected]>--- block/blk-settings.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)block/blk-settings.c +0
-0@@ -447,15 +447,9 @@ int blk_validate_limits(struct queue_limits *lim)if (!lim->max_discard_segments) lim->max_discard_segments =1 ;
block/blk-settings.c int blk_validate_limits(struct queue_limits *lim) · line 458
Quoted 10 lines
block/blk-settings.c +1
-7-/* -* By default there is no limit on the segment boundary alignment, -* but if there is one it can't be smaller than the page size as -* that would break all the normal I/O patterns. -*/ +/* By default there is no limit on the segment boundary alignment. */ if (!lim->seg_boundary_mask) lim->seg_boundary_mask =BLK_SEG_BOUNDARY_MASK ; -if (WARN_ON_ONCE (lim->seg_boundary_mask <BLK_MIN_SEGMENT_SIZE -1 )) -return -EINVAL ;
Please fold this change into the real code for reducing BLK_MIN_SEGMENT_SIZE, then it provides consistent handling/view about ->seg_boundary_mask and BLK_MIN_SEGMENT_SIZE.
Thanks,
Ming
Re: [PATCH v2 3/5] block: Remove a DMA segment boundary mask check
Quoted 9 lines · Ming Lei
Quoted 8 lines · Bart Van Assche
block/blk-settings.c +0
-0@@ -447,15 +447,9 @@ int blk_validate_limits(struct queue_limits *lim)if (!lim->max_discard_segments) lim->max_discard_segments =1 ;
Quoted 14 lines
block/blk-settings.c int blk_validate_limits(struct queue_limits *lim) · line 458
Quoted 10 lines
block/blk-settings.c +1
-7-/* -* By default there is no limit on the segment boundary alignment, -* but if there is one it can't be smaller than the page size as -* that would break all the normal I/O patterns. -*/ +/* By default there is no limit on the segment boundary alignment. */ if (!lim->seg_boundary_mask) lim->seg_boundary_mask =BLK_SEG_BOUNDARY_MASK ; -if (WARN_ON_ONCE (lim->seg_boundary_mask <BLK_MIN_SEGMENT_SIZE -1 )) -return -EINVAL ;Please fold this change into the real code for reducing
BLK_MIN_SEGMENT_SIZE, then it provides consistent handling/view about ->seg_boundary_mask andBLK_MIN_SEGMENT_SIZE.
Hmm ... wouldn't combining these two patches into a single patch violate the "one change per patch" rule?
Thanks,
Bart.
Re: [PATCH v2 3/5] block: Remove a DMA segment boundary mask check
Quoted 24 lines · Bart Van Assche
Quoted 14 lines · Ming Lei
Quoted 13 lines · Bart Van Assche
block/blk-settings.c +1
-5@@ -447,15 +447,9 @@ int blk_validate_limits(struct queue_limits *lim)if (!lim->max_discard_segments) lim->max_discard_segments =1 ; -/* -* By default there is no limit on the segment boundary alignment, -* but if there is one it can't be smaller than the page size as -* that would break all the normal I/O patterns. -*/ +/* By default there is no limit on the segment boundary alignment. */ Quoted 8 lines
block/blk-settings.c int blk_validate_limits(struct queue_limits *lim) · line 458
Quoted 4 lines
block/blk-settings.c +0
-2if (!lim->seg_boundary_mask) lim->seg_boundary_mask =BLK_SEG_BOUNDARY_MASK ; -if (WARN_ON_ONCE (lim->seg_boundary_mask <BLK_MIN_SEGMENT_SIZE -1 )) -return -EINVAL ;Please fold this change into the real code for reducing
BLK_MIN_SEGMENT_SIZE, then it provides consistent handling/view about ->seg_boundary_mask andBLK_MIN_SEGMENT_SIZE.
Quoted 2 lines
Hmm ... wouldn't combining these two patches into a single patch violate the "one change per patch" rule?
OK, looks it is two things.
But why do you remove the check? Looks you just encourage driver to use insane lim->seg_boundary_mask. IMO, it is just fragile.
Thanks,
Ming
Re: [PATCH v2 3/5] block: Remove a DMA segment boundary mask check
Quoted 2 lines · Ming Lei
But why do you remove the check? Looks you just encourage driver to use insane
lim->seg_boundary_mask. IMO, it is just fragile.
Hi Ming,
Users cannot and should not modify lim->seg_boundary_mask. Only block drivers should set this mask.
A possible alternative to removing this check is to change it into the following:
if (WARN_ON_ONCE(lim->seg_boundary_mask < SZ_4K - 1)) return -EINVAL;
All block and SCSI drivers I know of have a seg_boundary_mask that is greater than or equal to 4095. Does this look better to you?
Here is an example of a driver with DMA boundary (the SCSI name for the segment boundary mask) equal to 4095:
drivers/scsi/qedi/qedi.h:67:#define QEDI_HW_DMA_BOUNDARY 0xfffdrivers/scsi/qedi/qedi_iscsi.c:59: .dma_boundary = QEDI_HW_DMA_BOUNDARY,
Thanks,
Bart.
PATCH v2 4/5 block: Reduce the minimum value for the maximum DMA segment size
In reply to Bart Van AsscheAll block devices that are supported by the Linux kernel have a DMA engine that supports DMA segments of 4 KiB or larger. Allow smaller DMA segment sizes because these are useful for block layer testing. Reject values below 512 because such values would result in an excessive number of DMA segments.
The only code affected by this change is the following code:
if (WARN_ON_ONCE(lim->max_segment_size < BLK_MIN_SEGMENT_SIZE)) return -EINVAL;
--- block/blk.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk.h +1 -1
@@ -23,7 +23,7 @@ struct elv_change_ctx;#define BLK_DEF_MAX_SECTORS_CAP (SZ_4M >> SECTOR_SHIFT)#define BLK_DEV_MAX_SECTORS (LLONG_MAX >> 9) -#define BLK_MIN_SEGMENT_SIZE 4096 +#define BLK_MIN_SEGMENT_SIZE 512/* Max future timer expiry for timeouts */ #define BLK_MAX_TIMEOUT (5 * HZ)
PATCH v2 5/5 null_blk: Support configuring the maximum DMA segment size
In reply to Bart Van AsscheAdd support for configuring the maximum DMA segment size. The maximum DMA segment size may be set to a value smaller than the virtual memory page size. No checks are performed on the new max_segment_size parameter because all positive values are valid. The value 0 is converted into a valid value by blk_validate_limits().
Since rq_for_each_segment() may yield bvecs larger than the maximum DMA segment size, add code in the rq_for_each_segment() loop that restricts the bvec length to the maximum DMA segment size.
--- drivers/block/null_blk/main.c | 13 +++++++++++++ drivers/block/null_blk/null_blk.h | 1 + 2 files changed, 14 insertions(+)
drivers/block/null_blk/main.c +13 -0
@@ -169,6 +169,10 @@ static int g_max_sectors;module_param_named (max_sectors, g_max_sectors, int,0444 );MODULE_PARM_DESC (max_sectors,"Maximum size of a command (in 512B sectors)" ); +static unsigned int g_max_segment_size =BLK_MAX_SEGMENT_SIZE ; +module_param_named (max_segment_size, g_max_segment_size, int,0444 ); +MODULE_PARM_DESC (max_segment_size,"Maximum size of a DMA segment in bytes" ); +static unsigned int nr_devices =1 ;module_param (nr_devices, uint,0444 );MODULE_PARM_DESC (nr_devices,"Number of devices to register" );
@@ -450,6 +454,7 @@ NULLB_DEVICE_ATTR(home_node, uint, NULL);NULLB_DEVICE_ATTR (queue_mode, uint,NULL );NULLB_DEVICE_ATTR (blocksize, uint,NULL );NULLB_DEVICE_ATTR (max_sectors, uint,NULL ); +NULLB_DEVICE_ATTR (max_segment_size, uint,NULL );NULLB_DEVICE_ATTR (irqmode, uint,NULL );NULLB_DEVICE_ATTR (hw_queue_depth, uint,NULL );NULLB_DEVICE_ATTR (index, uint,NULL );
@@ -608,6 +613,7 @@ static struct configfs_attribute *nullb_device_attrs[] = { &nullb_device_attr_index, &nullb_device_attr_irqmode, &nullb_device_attr_max_sectors, + &nullb_device_attr_max_segment_size, &nullb_device_attr_mbps, &nullb_device_attr_memory_backed, &nullb_device_attr_no_sched,
@@ -805,6 +811,7 @@ static struct nullb_device *null_alloc_dev(void) dev->queue_mode = g_queue_mode; dev->blocksize = g_bs; dev->max_sectors = g_max_sectors; + dev->max_segment_size = g_max_segment_size; dev->irqmode = g_irqmode; dev->hw_queue_depth = g_hw_queue_depth; dev->blocking = g_blocking;
@@ -1251,6 +1258,9 @@ static blk_status_t null_transfer(struct nullb *nullb, struct page *page,unsigned int valid_len = len;void *p; +WARN_ONCE (len > dev->max_segment_size,"%u > %u\n" , len, + dev->max_segment_size); + p =kmap_local_page (page) + off;if (!is_write) {if (dev->zoned) {
@@ -1298,6 +1308,8 @@ static blk_status_t null_handle_data_transfer(struct nullb_cmd *cmd,spin_lock_irq (&nullb->lock);rq_for_each_segment (bvec, rq, iter) { len = bvec.bv_len; + len =min (bvec.bv_len, nullb->dev->max_segment_size); + bvec.bv_len = len;if (transferred_bytes + len > max_bytes) len = max_bytes - transferred_bytes; err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
@@ -1961,6 +1973,7 @@ static int null_add_dev(struct nullb_device *dev) .logical_block_size = dev->blocksize, .physical_block_size = dev->blocksize, .max_hw_sectors = dev->max_sectors, + .max_segment_size = dev->max_segment_size, .dma_alignment = 1, };
drivers/block/null_blk/null_blk.h +1 -0
@@ -93,6 +93,7 @@ struct nullb_device {unsigned int queue_mode;/* block interface */ unsigned int blocksize;/* block size */ unsigned int max_sectors;/* Max sectors per command */ +unsigned int max_segment_size;/* Max size of a single DMA segment. */ unsigned int irqmode;/* IRQ completion handler */ unsigned int hw_queue_depth;/* queue depth */ unsigned int index;/* index of the disk, only valid with a disk */