diff mbox series

[v7,1/4] sgl_alloc_order: remove 4 GiB limit

Message ID 20220201034915.183117-2-dgilbert@interlog.com
State New
Headers show
Series scatterlist: add new capabilities | expand

Commit Message

Douglas Gilbert Feb. 1, 2022, 3:49 a.m. UTC
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 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.

In early 2021 there was discussion between Jason Gunthorpe
<jgg@ziepe.ca> and Bodo Stroesser <bostroesser@gmail.com> about the
way to check for overflow caused by order (an exponent) being
too large. Take the solution proposed by Bodo in post dated
20210118 to the linux-scsi and linux-block lists.

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 .

Cc: Jason Gunthorpe <jgg@ziepe.ca>
Reviewed-by: Bodo Stroesser <bostroesser@gmail.com>
Signed-off-by: Douglas Gilbert <dgilbert@interlog.com>
---
 include/linux/scatterlist.h |  1 +
 lib/scatterlist.c           | 24 +++++++++++++-----------
 2 files changed, 14 insertions(+), 11 deletions(-)

Comments

kernel test robot Feb. 1, 2022, 1:46 p.m. UTC | #1
Hi Douglas,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.17-rc2 next-20220131]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Douglas-Gilbert/scatterlist-add-new-capabilities/20220201-115047
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 26291c54e111ff6ba87a164d85d4a4e134b7315c
config: i386-randconfig-a003-20220131 (https://download.01.org/0day-ci/archive/20220201/202202012125.JGVcLupw-lkp@intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/be1e80a043970c400c00709be739ab26f931331a
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Douglas-Gilbert/scatterlist-add-new-capabilities/20220201-115047
        git checkout be1e80a043970c400c00709be739ab26f931331a
        # save the config file to linux build tree
        mkdir build_dir
        make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   ld: lib/scatterlist.o: in function `sgl_alloc_order':
>> lib/scatterlist.c:612: undefined reference to `__udivdi3'


vim +612 lib/scatterlist.c

   586	
   587	/**
   588	 * sgl_alloc_order - allocate a scatterlist with equally sized elements each
   589	 *		     of which has 2^@order continuous pages
   590	 * @length: Length in bytes of the scatterlist. Must be at least one
   591	 * @order:  Second argument for alloc_pages(). Each sgl element size will
   592	 *	    be (PAGE_SIZE*2^@order) bytes. @order must not exceed 16.
   593	 * @chainable: Whether or not to allocate an extra element in the scatterlist
   594	 *	       for scatterlist chaining purposes
   595	 * @gfp: Memory allocation flags
   596	 * @nent_p: [out] Number of entries in the scatterlist that have pages.
   597	 *		  Ignored if @nent_p is NULL.
   598	 *
   599	 * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
   600	 */
   601	struct scatterlist *sgl_alloc_order(unsigned long long length,
   602					    unsigned int order, bool chainable,
   603					    gfp_t gfp, unsigned int *nent_p)
   604	{
   605		struct scatterlist *sgl, *sg;
   606		struct page *page;
   607		unsigned int nent, nalloc;
   608		u32 elem_len;
   609	
   610		if (length >> (PAGE_SHIFT + order) >= UINT_MAX)
   611			return NULL;
 > 612		nent = DIV_ROUND_UP(length, PAGE_SIZE << order);
   613	
   614		if (chainable) {
   615			if (check_add_overflow(nent, 1U, &nalloc))
   616				return NULL;
   617		} else {
   618			nalloc = nent;
   619		}
   620		sgl = kmalloc_array(nalloc, sizeof(struct scatterlist),
   621				    gfp & ~GFP_DMA);
   622		if (!sgl)
   623			return NULL;
   624	
   625		sg_init_table(sgl, nalloc);
   626		sg = sgl;
   627		while (length) {
   628			elem_len = min_t(u64, length, PAGE_SIZE << order);
   629			page = alloc_pages(gfp, order);
   630			if (!page) {
   631				sgl_free_order(sgl, order);
   632				return NULL;
   633			}
   634	
   635			sg_set_page(sg, page, elem_len, 0);
   636			length -= elem_len;
   637			sg = sg_next(sg);
   638		}
   639		WARN_ONCE(length, "length = %lld\n", length);
   640		if (nent_p)
   641			*nent_p = nent;
   642		return sgl;
   643	}
   644	EXPORT_SYMBOL(sgl_alloc_order);
   645	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h
index 7ff9d6386c12..03130be581bb 100644
--- a/include/linux/scatterlist.h
+++ b/include/linux/scatterlist.h
@@ -357,6 +357,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 */
 
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index d5e82e4a57ad..ed6d0465c78e 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -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.
  */
@@ -604,16 +607,15 @@  struct scatterlist *sgl_alloc_order(unsigned long long length,
 	unsigned int 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)))
+	if (length >> (PAGE_SHIFT + order) >= UINT_MAX)
 		return NULL;
-	nalloc = nent;
+	nent = DIV_ROUND_UP(length, PAGE_SIZE << order);
+
 	if (chainable) {
-		/* Check for integer overflow */
-		if (nalloc + 1 < nalloc)
+		if (check_add_overflow(nent, 1U, &nalloc))
 			return NULL;
-		nalloc++;
+	} else {
+		nalloc = nent;
 	}
 	sgl = kmalloc_array(nalloc, sizeof(struct scatterlist),
 			    gfp & ~GFP_DMA);