scatterlist: add operations for scsi_debug
Subsystems (2)
cover scatterlist: add operations for scsi_debug
The scsi_debug driver is essentially a ramdisk dressed up as a SCSI host with one or more SCSI devices attached. Like all low level SCSI drivers, the scsi_debug driver receives and provides data to the SCSI mid-level (and the block layer) using scatterlists whose interface is found in include/linux/scatterlist.h .
After trying kmalloc() then vmalloc() based storage for the scsi_debug driver, it was found that certain SCSI commands can be optimized if instead one or more scatterlists is used as its backing store. The specific SCSI command that benefits is VERIFY(BYTCHK=1) and the equivalent NVMe command is COMPARE. These commands have data-out buffers provided by an application that are compared by the storage device with the LBA and count (of block) given in the command. In this case the sgl_equal_sgl() function can be used instead of setting up a temporary buffer.
The implementation of the more common SCSI READ and WRITE commands are simplified by using the sgl_copy_sgl() function.
The first patch in this series removes an undocumented 4 GB limit in the existing sgl_alloc_order() function.
In the final patch of this series, the scsi_debug driver uses the new facilities in scatterlist to replace its vmalloc() backing store.
Douglas Gilbert (5):
sgl_alloc_order: remove 4 GiB limit
scatterlist: add sgl_copy_sgl() function
scatterlist: add sgl_equal_sgl() function
scatterlist: add sgl_memset()
scsi_debug: change store from vmalloc to sgl
drivers/scsi/Kconfig | 3 +- drivers/scsi/scsi_debug.c | 478 +++++++++++++++++++++++++----------- include/linux/scatterlist.h | 33 ++- lib/scatterlist.c | 253 ++++++++++++++++--- 4 files changed, 596 insertions(+), 171 deletions(-)
Signature · 1 line
2.37.3
PATCH 2/5 scatterlist: add sgl_copy_sgl() function
Both the SCSI and NVMe subsystems receive user data from the block layer in scatterlist_s (aka scatter gather lists (sgl) which are often arrays). If drivers in those subsystems represent storage (e.g. a ramdisk) or cache "hot" user data then they may also choose to use scatterlist_s. Currently there are no sgl to sgl operations in the kernel. Start with a sgl to sgl copy. Stops when the first of the number of requested bytes to copy, or the source sgl, or the destination sgl is exhausted. So the destination sgl will _not_ grow.
--- include/linux/scatterlist.h | 4 ++ lib/scatterlist.c | 74 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+)
include/linux/scatterlist.h +4 -0
@@ -445,6 +445,10 @@ size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,size_t sg_zero_buffer (struct scatterlist *sgl,unsigned int nents,size_t buflen,off_t skip); +size_t sgl_copy_sgl (struct scatterlist *d_sgl,unsigned int d_nents,off_t d_skip, +struct scatterlist *s_sgl,unsigned int s_nents,off_t s_skip, +size_t n_bytes); +/* * Maximum number of entries that will be allocated in one piece, if * a list larger than this is required then chaining will be utilized.
lib/scatterlist.c +74 -0
77 lines not on this page · raw message
Signature · 1 line
2.37.3
PATCH 4/5 scatterlist: add sgl_memset()
In reply to Douglas GilbertThe existing sg_zero_buffer() function is a bit restrictive. For example protection information (PI) blocks are usually initialized to 0xff bytes. As its name suggests sgl_memset() is modelled on memset(). One difference is the type of the val argument which is u8 rather than int. Plus it returns the number of bytes (over)written.
Change implementation of sg_zero_buffer() to call this new function.
--- include/linux/scatterlist.h | 20 +++++++++- lib/scatterlist.c | 78 ++++++++++++++++++++----------------- 2 files changed, 61 insertions(+), 37 deletions(-)
include/linux/scatterlist.h +18 -2
@@ -442,8 +442,6 @@ size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents, const void *buf, size_t buflen, off_t skip);size_t sg_pcopy_to_buffer (struct scatterlist *sgl,unsigned int nents,void *buf,size_t buflen,off_t skip); -size_t sg_zero_buffer (struct scatterlist *sgl,unsigned int nents, -size_t buflen,off_t skip); size_t sgl_copy_sgl(struct scatterlist *d_sgl, unsigned int d_nents, off_t d_skip, struct scatterlist *s_sgl, unsigned int s_nents, off_t s_skip,
@@ -457,6 +455,24 @@ bool sgl_equal_sgl_idx(struct scatterlist *x_sgl, unsigned int x_nents, off_t x_ struct scatterlist *y_sgl, unsigned int y_nents, off_t y_skip, size_t n_bytes, size_t *miscompare_idx); +size_t sgl_memset (struct scatterlist *sgl,unsigned int nents,off_t skip, +u8 val,size_t n_bytes); + +/** +* sg_zero_buffer - Zero-out a part of a SG list +* @sgl: The SG list +* @nents: Number of SG entries +* @buflen: The number of bytes to zero out +* @skip: Number of bytes to skip before zeroing +* +* Returns the number of bytes zeroed. +**/ +static inline size_t sg_zero_buffer (struct scatterlist *sgl,unsigned int nents, +size_t buflen,off_t skip) +{ +return sgl_memset (sgl, nents, skip,0 , buflen); +} +/* * Maximum number of entries that will be allocated in one piece, if * a list larger than this is required then chaining will be utilized.
lib/scatterlist.c +43 -35
87 lines not on this page · raw message
Signature · 1 line
2.37.3
PATCH 3/5 scatterlist: add sgl_equal_sgl() function
In reply to Douglas GilbertAfter enabling copies between scatter gather lists (sgl_s), another storage related operation is to compare two sgl_s for equality. This new function is designed to partially implement NVMe's Compare command and the SCSI VERIFY(BYTCHK=1) command. Like memcmp() this function begins scanning at the start (of each sgl) and returns false on the first miscompare and stops comparing.
The sgl_equal_sgl_idx() function additionally yields the index (i.e. byte position) of the first miscompare. The additional parameter, miscompare_idx, is a pointer. If it is non-NULL and a miscompare is detected (i.e. the function returns false) then the byte index of the first miscompare is written to *miscompare_idx. Knowing the location of the first miscompare is needed to implement properly the SCSI COMPARE AND WRITE command.
--- include/linux/scatterlist.h | 8 +++ lib/scatterlist.c | 110 ++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+)
include/linux/scatterlist.h +8 -0
@@ -449,6 +449,14 @@ size_t sgl_copy_sgl(struct scatterlist *d_sgl, unsigned int d_nents, off_t d_ski struct scatterlist *s_sgl, unsigned int s_nents, off_t s_skip, size_t n_bytes); +bool sgl_equal_sgl (struct scatterlist *x_sgl,unsigned int x_nents,off_t x_skip, +struct scatterlist *y_sgl,unsigned int y_nents,off_t y_skip, +size_t n_bytes); + +bool sgl_equal_sgl_idx (struct scatterlist *x_sgl,unsigned int x_nents,off_t x_skip, +struct scatterlist *y_sgl,unsigned int y_nents,off_t y_skip, +size_t n_bytes,size_t *miscompare_idx); +/* * Maximum number of entries that will be allocated in one piece, if * a list larger than this is required then chaining will be utilized.
lib/scatterlist.c +110 -0
113 lines not on this page · raw message
Signature · 1 line
2.37.3
PATCH 1/5 sgl_alloc_order: remove 4 GiB limit
In reply to Douglas GilbertThis patch fixes a check done by sgl_alloc_order() before it starts any allocations. The comment in the original said: "Check for integer overflow" but the right hand side of the expression in the condition is resolved as u32 so it can not exceed UINT32_MAX (4 GiB) which means 'length' can not exceed that value.
This function may be used to replace vmalloc(unsigned long) for a large allocation (e.g. a ramdisk). vmalloc has no limit at 4 GiB so it seems unreasonable that sgl_alloc_order() whose length type is unsigned long long should be limited to 4 GB.
Solutions to this issue were discussed by Jason Gunthorpe <[email protected]> and Bodo Stroesser <[email protected]>. This version is base on a linux-scsi post by Jason titled: "Re:
[PATCH v7 1/4] sgl_alloc_order: remove 4 GiB limit" dated 20220201.
An earlier patch fixed a memory leak in sg_alloc_order() due to the misuse of sgl_free(). Take the opportunity to put a one line comment above sgl_free()'s declaration warning that it is not suitable when order > 0 .
--- include/linux/scatterlist.h | 1 + lib/scatterlist.c | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-)
include/linux/scatterlist.h +1 -0
@@ -426,6 +426,7 @@ struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp, unsigned int *nent_p);void sgl_free_n_order (struct scatterlist *sgl,int nents,int order);void sgl_free_order (struct scatterlist *sgl,int order); +/* Only use sgl_free() when order is 0 */ void sgl_free (struct scatterlist *sgl);#endif /* CONFIG_SGL_ALLOC */
lib/scatterlist.c +12 -9
@@ -585,13 +585,16 @@ EXPORT_SYMBOL(sg_alloc_table_from_pages_segment);#ifdef CONFIG_SGL_ALLOC /** -* sgl_alloc_order - allocate a scatterlist and its pages +* sgl_alloc_order - allocate a scatterlist with equally sized elements each +* of which has 2^@order continuous pages * @length: Length in bytes of the scatterlist. Must be at least one -* @order: Second argument for alloc_pages() +* @order: Second argument for alloc_pages(). Each sgl element size will +* be (PAGE_SIZE*2^@order) bytes. @order must not exceed 16. * @chainable: Whether or not to allocate an extra element in the scatterlist -* for scatterlist chaining purposes +* for scatterlist chaining purposes * @gfp: Memory allocation flags -* @nent_p: [out] Number of entries in the scatterlist that have pages +* @nent_p: [out] Number of entries in the scatterlist that have pages. +* Ignored if @nent_p is NULL. * * Returns: A pointer to an initialized scatterlist or %NULL upon failure. */
@@ -601,14 +604,14 @@ struct scatterlist *sgl_alloc_order(unsigned long long length, {struct scatterlist *sgl, *sg;struct page *page; -unsigned int nent, nalloc; +uint64_t nent; +unsigned int nalloc;u32 elem_len; nent =round_up (length,PAGE_SIZE << order) >> (PAGE_SHIFT + order); -/* Check for integer overflow */ -if (length > (nent << (PAGE_SHIFT + order))) +if (nent >UINT_MAX )return NULL ; - nalloc = nent; + nalloc = (unsigned int )nent;if (chainable) {/* Check for integer overflow */ if (nalloc +1 < nalloc)
@@ -636,7 +639,7 @@ struct scatterlist *sgl_alloc_order(unsigned long long length, }WARN_ONCE (length,"length = %lld\n" , length);if (nent_p) - *nent_p = nent; + *nent_p = (unsigned int )nent;return sgl; }EXPORT_SYMBOL (sgl_alloc_order);
Signature · 1 line
2.37.3
PATCH 5/5 scsi_debug: change store from vmalloc to sgl
In reply to Douglas GilbertA long time ago this driver's store was allocated by kmalloc() or alloc_pages(). When this was switched to vmalloc() the author noticed slower ramdisk access times and more variability in repeated tests. So try going back with sgl_alloc_order() to get uniformly sized allocations in a sometimes large scatter gather _array_. That array is the basis of maintaining O(1) access to the store.
Using sgl_alloc_order() and friends requires CONFIG_SGL_ALLOC so add a 'select' to the Kconfig file.
Remove kcalloc() in resp_verify() as sgl_s can now be compared directly without forming an intermediate buffer. This is a performance win for the SCSI VERIFY command implementation.
Make the SCSI COMPARE AND WRITE command yield the offset of the first miscompared byte when the compare fails (as required by T10).
--- drivers/scsi/Kconfig | 3 +- drivers/scsi/scsi_debug.c | 478 +++++++++++++++++++++++++++----------- 2 files changed, 341 insertions(+), 140 deletions(-)
drivers/scsi/Kconfig +2 -1
@@ -1229,13 +1229,14 @@ config SCSI_DEBUG tristate "SCSI debugging host and device simulator" depends on SCSI select CRC_T10DIF + select SGL_ALLOC help This pseudo driver simulates one or more hosts (SCSI initiators), each with one or more targets, each with one or more logical units. Defaults to one of each, creating a small RAM disk device. Many parameters found in the /sys/bus/pseudo/drivers/scsi_debug directory can be tweaked at run time. - See <http://sg.danny.cz/sg/sdebug26.html> for more information. + See <https://sg.danny.cz/sg/scsi_debug.html> for more information. Mainly used for testing and best as a module. If unsure, say N. config SCSI_MESH
drivers/scsi/scsi_debug.c +339 -139
745 lines not on this page · raw message
Signature · 1 line
2.37.3
Re: [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit
In reply to Douglas GilbertQuoted 11 lines · Douglas Gilbert
This patch fixes a check done by
sgl_alloc_order()before it starts any allocations. The comment in the original said: "Check for integer overflow" but the right hand side of the expression in the condition is resolved as u32 so it can not exceedUINT32_MAX(4 GiB) which means 'length' can not exceed that value.This function may be used to replace vmalloc(unsigned long) for a large allocation (e.g. a ramdisk). vmalloc has no limit at 4 GiB so it seems unreasonable that
sgl_alloc_order()whose length type is unsigned long long should be limited to 4 GB.
Quoted 4 lines
Solutions to this issue were discussed by Jason Gunthorpe <[email protected]> and Bodo Stroesser <[email protected]>. This version is base on a linux-scsi post by Jason titled: "Re:
[PATCH v7 1/4]sgl_alloc_order: remove 4 GiB limit" dated 20220201.
You should link to lore here..
I think I prefer we just fix this so it doesn't have a problem in the first place - nothing needs the weird unsigned long long argument type:
lib/scatterlist.c +7 -10
@@ -595,19 +595,17 @@ EXPORT_SYMBOL(sg_alloc_table_from_pages_segment);* * Returns: A pointer to an initialized scatterlist or %NULL upon failure. */ -struct scatterlist *sgl_alloc_order (unsigned long long length, -unsigned int order,bool chainable, -gfp_t gfp,unsigned int *nent_p) +struct scatterlist *sgl_alloc_order (size_t length,unsigned int order, +bool chainable,gfp_t gfp,size_t *nent_p) {struct scatterlist *sgl, *sg;struct page *page; -unsigned int nent, nalloc; +size_t nent, nalloc;u32 elem_len; - nent =round_up (length,PAGE_SIZE << order) >> (PAGE_SHIFT + order); -/* Check for integer overflow */ -if (length > (nent << (PAGE_SHIFT + order))) -return NULL ; + nent = length >> (PAGE_SHIFT + order); +if (length % (1 << (PAGE_SHIFT + order))) + nent++; nalloc = nent;if (chainable) {/* Check for integer overflow */
@@ -649,8 +647,7 @@ EXPORT_SYMBOL(sgl_alloc_order);* * Returns: A pointer to an initialized scatterlist or %NULL upon failure. */ -struct scatterlist *sgl_alloc (unsigned long long length,gfp_t gfp, -unsigned int *nent_p) +struct scatterlist *sgl_alloc (size_t length,gfp_t gfp,size_t *nent_p) {return sgl_alloc_order (length,0 , false, gfp, nent_p); }
Jason
Re: [PATCH 5/5] scsi_debug: change store from vmalloc to sgl
In reply to Douglas GilbertQuoted 111 lines · Douglas Gilbert
A long time ago this driver's store was allocated by
kmalloc()oralloc_pages(). When this was switched tovmalloc()the author noticed slower ramdisk access times and more variability in repeated tests. So try going back withsgl_alloc_order()to get uniformly sized allocations in a sometimes large scatter gather _array_. That array is the basis of maintaining O(1) access to the store.Using
sgl_alloc_order()and friends requiresCONFIG_SGL_ALLOCso add a 'select' to theKconfigfile.Remove
kcalloc()inresp_verify()assgl_scan now be compared directly without forming an intermediate buffer. This is a performance win for the SCSI VERIFY command implementation.Make the SCSI COMPARE AND WRITE command yield the offset of the first miscompared byte when the compare fails (as required by T10).
Signed-off-by Douglas Gilbert <[email protected]>--- drivers/scsi/Kconfig | 3 +- drivers/scsi/scsi_debug.c | 478 +++++++++++++++++++++++++++----------- 2 files changed, 341 insertions(+), 140 deletions(-)drivers/scsi/Kconfig +2
-1@@ -1229,13 +1229,14 @@ config SCSI_DEBUG tristate "SCSI debugging host and device simulator" depends on SCSI select CRC_T10DIF + select SGL_ALLOC help This pseudo driver simulates one or more hosts (SCSI initiators), each with one or more targets, each with one or more logical units. Defaults to one of each, creating a small RAM disk device. Many parameters found in the /sys/bus/pseudo/drivers/scsi_debug directory can be tweaked at run time. - See <http://sg.danny.cz/sg/sdebug26.html> for more information. + See <https://sg.danny.cz/sg/scsi_debug.html> for more information. Mainly used for testing and best as a module. If unsure, say N. config SCSI_MESHdrivers/scsi/scsi_debug.c +15
-15@@ -221,6 +221,7 @@ static const char *sdebug_version_date = "20210520";#define SDEBUG_CANQUEUE_WORDS 3/* a WORD is bits in a long */ #define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG)#define DEF_CMD_PER_LUN SDEBUG_CANQUEUE +#define SDEB_ORDER_TOO_LARGE 4096/* UA - Unit Attention; SA - Service Action; SSU - Start Stop Unit */ #define F_D_IN 1/* Data-in command (e.g. READ) */ @@ -318,8 +319,11 @@ struct sdebug_host_info {/* There is an xarray of pointers to this struct's objects, one per host */ struct sdeb_store_info { +unsigned int n_elem;/* number of sgl elements */ +unsigned int order;/* as used by alloc_pages() */ +unsigned int elem_pow2;/* PAGE_SHIFT + order */ rwlock_t macc_lck;/* for atomic media access on this store */ -u8 *storep;/* user data storage (ram) */ +struct scatterlist *sgl;/* main store: n_elem array of same sized allocs */ struct t10_pi_tuple *dif_storep;/* protection info */ void *map_storep;/* provisioning map */ };@@ -880,19 +884,6 @@ static inline bool scsi_debug_lbp(void) (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10); } -static void *lba2fake_store (struct sdeb_store_info *sip, -unsigned long long lba) -{ -struct sdeb_store_info *lsip = sip; - - lba =do_div (lba, sdebug_store_sectors); -if (!sip || !sip->storep) { -WARN_ON_ONCE (true); - lsip =xa_load (per_store_ap,0 );/* should never be NULL */ - } -return lsip->storep + lba * sdebug_sector_size; -} -static struct t10_pi_tuple *dif_store (struct sdeb_store_info *sip,sector_t sector) {@@ -1001,7 +992,6 @@ static int scsi_debug_ioctl(struct scsi_device *dev, unsigned int cmd, __func__, cmd); }return -EINVAL ; -/* return -ENOTTY; // correct return but upsets fdisk */ }static void config_cdb_len (struct scsi_device *sdev)@@ -1221,6 +1211,55 @@ static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,return scsi_sg_copy_to_buffer (scp, arr, arr_len); } +static bool sdeb_sgl_cmp_buf (struct scatterlist *sgl,unsigned int nents, +const void *buf,size_t buflen,off_t skip) +{ +bool equ = true; +size_t offset =0 ; +size_t len; +struct sg_mapping_iter miter; + +if (buflen ==0 ) +return true;
drivers/scsi/scsi_debug.c static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr, · line 1233
Quoted 10 lines
drivers/scsi/scsi_debug.c +10
-0+sg_miter_start (&miter, sgl, nents,SG_MITER_ATOMIC |SG_MITER_FROM_SG ); +if (!sg_miter_skip (&miter, skip)) + goto fini; + +while (equ && (offset < buflen) &&sg_miter_next (&miter)) { + len =min (miter.length, buflen - offset); + equ =memcmp (buf + offset, miter.addr, len) ==0 ; + offset += len; + miter.consumed = len; +sg_miter_stop (&miter);
I think, the sg_miter_stop is obsolete here.
Also, I think there is no need to set miter.consumed. If len is less than miter.length, the loop will end and not not call sg_miter_next again.
Bodo
Quoted 6 lines
drivers/scsi/scsi_debug.c +6
-0+ } +fini: +sg_miter_stop (&miter); +return equ; +} +
Re: [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit
In reply to Jason GunthorpeQuoted 38 lines · Jason Gunthorpe
Quoted 11 lines · Douglas Gilbert
This patch fixes a check done by
sgl_alloc_order()before it starts any allocations. The comment in the original said: "Check for integer overflow" but the right hand side of the expression in the condition is resolved as u32 so it can not exceedUINT32_MAX(4 GiB) which means 'length' can not exceed that value.This function may be used to replace vmalloc(unsigned long) for a large allocation (e.g. a ramdisk). vmalloc has no limit at 4 GiB so it seems unreasonable that
sgl_alloc_order()whose length type is unsigned long long should be limited to 4 GB.Quoted 4 lines
Solutions to this issue were discussed by Jason Gunthorpe <[email protected]> and Bodo Stroesser <[email protected]>. This version is base on a linux-scsi post by Jason titled: "Re:
[PATCH v7 1/4]sgl_alloc_order: remove 4 GiB limit" dated 20220201.You should link to lore here..
I think I prefer we just fix this so it doesn't have a problem in the first place - nothing needs the weird unsigned long long argument type:
lib/scatterlist.c +2
-3@@ -595,19 +595,17 @@ EXPORT_SYMBOL(sg_alloc_table_from_pages_segment);* * Returns: A pointer to an initialized scatterlist or %NULL upon failure. */ -struct scatterlist *sgl_alloc_order (unsigned long long length, -unsigned int order,bool chainable, -gfp_t gfp,unsigned int *nent_p) +struct scatterlist *sgl_alloc_order (size_t length,unsigned int order, +bool chainable,gfp_t gfp,size_t *nent_p) {struct scatterlist *sgl, *sg;struct page *page;
lib/scatterlist.c EXPORT_SYMBOL(sg_alloc_table_from_pages_segment); · line 607
Quoted 10 lines
lib/scatterlist.c +3
-5-unsigned int nent, nalloc; +size_t nent, nalloc;u32 elem_len; - nent =round_up (length,PAGE_SIZE << order) >> (PAGE_SHIFT + order); -/* Check for integer overflow */ -if (length > (nent << (PAGE_SHIFT + order))) -return NULL ; + nent = length >> (PAGE_SHIFT + order); +if (length % (1 << (PAGE_SHIFT + order)))
This might end up doing a modulo operation for divisor 0, if caller specifies a too high order parameter, right?
To be on the safe side I'd prefer to use the following code instead:
if (length & ((1 << (PAGE_SHIFT + order)) - 1))
Thank you
Bodo
Quoted 16 lines
lib/scatterlist.c +2
-2+ nent++; nalloc = nent;if (chainable) {/* Check for integer overflow */ @@ -649,8 +647,7 @@ EXPORT_SYMBOL(sgl_alloc_order);* * Returns: A pointer to an initialized scatterlist or %NULL upon failure. */ -struct scatterlist *sgl_alloc (unsigned long long length,gfp_t gfp, -unsigned int *nent_p) +struct scatterlist *sgl_alloc (size_t length,gfp_t gfp,size_t *nent_p) {return sgl_alloc_order (length,0 , false, gfp, nent_p); }Jason
Re: [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit
Quoted 9 lines · Bodo Stroesser
Quoted 9 lines
lib/scatterlist.c +3
-2+struct scatterlist *sgl_alloc_order (size_t length,unsigned int order, +bool chainable,gfp_t gfp,size_t *nent_p) {struct scatterlist *sgl, *sg;struct page *page; -unsigned int nent, nalloc; +size_t nent, nalloc;u32 elem_len; - nent =round_up (length,PAGE_SIZE << order) >> (PAGE_SHIFT + order);
Quoted 8 lines
lib/scatterlist.c EXPORT_SYMBOL(sg_alloc_table_from_pages_segment); · line 607
Quoted 5 lines
lib/scatterlist.c +2
-3-/* Check for integer overflow */ -if (length > (nent << (PAGE_SHIFT + order))) -return NULL ; + nent = length >> (PAGE_SHIFT + order); +if (length % (1 << (PAGE_SHIFT + order)))This might end up doing a modulo operation for divisor 0, if caller specifies a too high order parameter, right?
If that happens then the first >> will be busted too and this is all broken..
We assume the caller will pass a valid order paramter it seems, it is not userspace controlled.
Jason
Re: [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit
Quoted 19 lines · Jason Gunthorpe
Quoted 9 lines · Bodo Stroesser
Quoted 9 lines
lib/scatterlist.c +3
-2+struct scatterlist *sgl_alloc_order (size_t length,unsigned int order, +bool chainable,gfp_t gfp,size_t *nent_p) {struct scatterlist *sgl, *sg;struct page *page; -unsigned int nent, nalloc; +size_t nent, nalloc;u32 elem_len; - nent =round_up (length,PAGE_SIZE << order) >> (PAGE_SHIFT + order);Quoted 8 lines
lib/scatterlist.c EXPORT_SYMBOL(sg_alloc_table_from_pages_segment); · line 607
Quoted 5 lines
lib/scatterlist.c +2
-3-/* Check for integer overflow */ -if (length > (nent << (PAGE_SHIFT + order))) -return NULL ; + nent = length >> (PAGE_SHIFT + order); +if (length % (1 << (PAGE_SHIFT + order)))This might end up doing a modulo operation for divisor 0, if caller specifies a too high order parameter, right?
Quoted 5 lines
If that happens then the first >> will be busted too and this is all broken..
We assume the caller will pass a valid order paramter it seems, it is not userspace controlled.
Hi,
I have been trying to remove the unstated 4 GB limit on this existing function, _not_ change its interface. In practice, I don't believe limiting 'nent' to 2^32-1 (i.e. 'unsigned int') is an unreasonable restriction and that is checked at runtime in my patch. Since each 'entity' is at least PAGE_SIZE bytes, that is at least 16 TB (with order==0).
The order_too_large issue I have tried to address with a pre-condition (i.e. words in the description of the 'order' argument). That said, I did notice:
include/linux/mmzone.h :
#ifndef CONFIG_ARCH_FORCE_MAX_ORDER
#define MAX_ORDER 11
#else
#define MAX_ORDER CONFIG_ARCH_FORCE_MAX_ORDER
#endif
Changing the interface of sgl_alloc_order() will break these callers:
drivers/scsi/ipr.c drivers/target/target_core_transport.c
due to change the type (and size on 64 bit machines) of the fifth argument (i.e. 'size_t *nent_p').
If you want to change sgl_alloc_order()'s interface, shouldn't that be done in a separate patch that also fixes the breakages it would otherwise cause?
Doug Gilbert
Re: [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit
In reply to Jason GunthorpeQuoted 19 lines · Jason Gunthorpe
Quoted 9 lines · Bodo Stroesser
Quoted 9 lines
lib/scatterlist.c +3
-2+struct scatterlist *sgl_alloc_order (size_t length,unsigned int order, +bool chainable,gfp_t gfp,size_t *nent_p) {struct scatterlist *sgl, *sg;struct page *page; -unsigned int nent, nalloc; +size_t nent, nalloc;u32 elem_len; - nent =round_up (length,PAGE_SIZE << order) >> (PAGE_SHIFT + order);Quoted 8 lines
lib/scatterlist.c EXPORT_SYMBOL(sg_alloc_table_from_pages_segment); · line 607
Quoted 5 lines
lib/scatterlist.c +2
-3-/* Check for integer overflow */ -if (length > (nent << (PAGE_SHIFT + order))) -return NULL ; + nent = length >> (PAGE_SHIFT + order); +if (length % (1 << (PAGE_SHIFT + order)))This might end up doing a modulo operation for divisor 0, if caller specifies a too high order parameter, right?
Quoted 6 lines
If that happens then the first >> will be busted too and this is all broken..
We assume the caller will pass a valid order paramter it seems, it is not userspace controlled.
If a too high order is passed, alloc_pages will just return NULL, so in the old code sgl_alloc_order simply returns NULL. Using modulo op changes it to possibly crashing the system.
Bodo
Re: [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit
Quoted 23 lines · Bodo Stroesser
Quoted 22 lines · Jason Gunthorpe
Quoted 9 lines · Bodo Stroesser
Quoted 9 lines
lib/scatterlist.c +3
-2+struct scatterlist *sgl_alloc_order (size_t length,unsigned int order, +bool chainable,gfp_t gfp,size_t *nent_p) {struct scatterlist *sgl, *sg;struct page *page; -unsigned int nent, nalloc; +size_t nent, nalloc;u32 elem_len; - nent =round_up (length,PAGE_SIZE << order) >> (PAGE_SHIFT + order);Quoted 8 lines
lib/scatterlist.c EXPORT_SYMBOL(sg_alloc_table_from_pages_segment); · line 607
Quoted 5 lines
lib/scatterlist.c +2
-3-/* Check for integer overflow */ -if (length > (nent << (PAGE_SHIFT + order))) -return NULL ; + nent = length >> (PAGE_SHIFT + order); +if (length % (1 << (PAGE_SHIFT + order)))This might end up doing a modulo operation for divisor 0, if caller specifies a too high order parameter, right?
If that happens then the first >> will be busted too and this is all broken..
Quoted 7 lines
Quoted 3 lines
We assume the caller will pass a valid order paramter it seems, it is not userspace controlled.
If a too high order is passed,
alloc_pageswill just return NULL, so in the old codesgl_alloc_ordersimply returns NULL. Using modulo op changes it to possibly crashing the system.
That is fine, we are not trying to protect against buggy callers passing a weird order
We are trying to protect against correct callers passing a large length because length is user controlled or something.
Jason
Re: [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit
In reply to Douglas GilbertQuoted 6 lines · Douglas Gilbert
Changing the interface of
sgl_alloc_order()will break these callers:
drivers/scsi/ipr.cdrivers/target/target_core_transport.cdue to change the type (and size on 64 bit machines) of the fifth argument (i.e. '
size_t*nent_p').
Yes, you'd fold those small fixes into this patch.
Jason