From: Jason Gunthorpe <jgg@ziepe.ca>
To: Douglas Gilbert <dgilbert@interlog.com>
Cc: linux-scsi@vger.kernel.org, linux-block@vger.kernel.org,
target-devel@vger.kernel.org, linux-rdma@vger.kernel.org,
linux-kernel@vger.kernel.org, martin.petersen@oracle.com,
jejb@linux.vnet.ibm.com, bostroesser@gmail.com,
bvanassche@acm.org, ddiss@suse.de
Subject: Re: [PATCH v5 1/4] sgl_alloc_order: remove 4 GiB limit, sgl_free() warning
Date: Thu, 7 Jan 2021 13:44:10 -0400 [thread overview]
Message-ID: <20210107174410.GB504133@ziepe.ca> (raw)
In-Reply-To: <20201228234955.190858-2-dgilbert@interlog.com>
On Mon, Dec 28, 2020 at 06:49:52PM -0500, Douglas Gilbert wrote:
> diff --git a/lib/scatterlist.c b/lib/scatterlist.c
> index a59778946404..4986545beef9 100644
> +++ b/lib/scatterlist.c
> @@ -554,13 +554,15 @@ EXPORT_SYMBOL(sg_alloc_table_from_pages);
> #ifdef CONFIG_SGL_ALLOC
>
> /**
> - * sgl_alloc_order - allocate a scatterlist and its pages
> + * sgl_alloc_order - allocate a scatterlist with equally sized elements
> * @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
> * @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 NULL is given.
> *
> * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
> */
> @@ -574,8 +576,8 @@ struct scatterlist *sgl_alloc_order(unsigned long long length,
> u32 elem_len;
>
> nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order);
> - /* Check for integer overflow */
> - if (length > (nent << (PAGE_SHIFT + order)))
> + /* Integer overflow if: length > nent*2^(PAGE_SHIFT+order) */
> + if (ilog2(length) > ilog2(nent) + PAGE_SHIFT + order)
> return NULL;
> nalloc = nent;
> if (chainable) {
This is a little bit too tortured now, how about this:
if (length >> (PAGE_SHIFT + order) >= UINT_MAX)
return NULL;
nent = length >> (PAGE_SHIFT + order);
if (length & ((1ULL << (PAGE_SHIFT + order)) - 1))
nent++;
if (chainable) {
if (check_add_overflow(nent, 1, &nalloc))
return NULL;
}
else
nalloc = nent;
Jason
next prev parent reply other threads:[~2021-01-07 17:44 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-28 23:49 [PATCH v5 0/4] scatterlist: add new capabilities Douglas Gilbert
2020-12-28 23:49 ` [PATCH v5 1/4] sgl_alloc_order: remove 4 GiB limit, sgl_free() warning Douglas Gilbert
2021-01-07 17:44 ` Jason Gunthorpe [this message]
2021-01-09 22:58 ` Douglas Gilbert
2021-01-11 14:43 ` Jason Gunthorpe
2020-12-28 23:49 ` [PATCH v5 2/4] scatterlist: add sgl_copy_sgl() function Douglas Gilbert
2020-12-28 23:49 ` [PATCH v5 3/4] scatterlist: add sgl_compare_sgl() function Douglas Gilbert
2020-12-28 23:49 ` [PATCH v5 4/4] scatterlist: add sgl_memset() Douglas Gilbert
2021-01-07 17:46 ` Jason Gunthorpe
2021-01-09 23:00 ` Douglas Gilbert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210107174410.GB504133@ziepe.ca \
--to=jgg@ziepe.ca \
--cc=bostroesser@gmail.com \
--cc=bvanassche@acm.org \
--cc=ddiss@suse.de \
--cc=dgilbert@interlog.com \
--cc=jejb@linux.vnet.ibm.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=target-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).