public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
From: Bodo Stroesser <bostroesser@gmail.com>
To: Jason Gunthorpe <jgg@ziepe.ca>, Douglas Gilbert <dgilbert@interlog.com>
Cc: linux-scsi@vger.kernel.org, martin.petersen@oracle.com,
	jejb@linux.vnet.ibm.com, hare@suse.de, bvanassche@acm.org
Subject: Re: [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit
Date: Mon, 24 Oct 2022 16:32:30 +0200	[thread overview]
Message-ID: <665f8dee-6688-60d1-5097-49f9726c38ec@gmail.com> (raw)
In-Reply-To: <Y1aDQznakNaWD8kd@ziepe.ca>

On 24.10.22 14:21, Jason Gunthorpe wrote:
> On Sun, Oct 23, 2022 at 09:02:40PM -0400, Douglas Gilbert wrote:
>> 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.
>>
>> Solutions to this issue were discussed by Jason Gunthorpe
>> <jgg@ziepe.ca> and Bodo Stroesser <bostroesser@gmail.com>. 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:
> 
> diff --git a/lib/scatterlist.c b/lib/scatterlist.c
> index c8c3d675845c37..c39e19fa174bca 100644
> --- a/lib/scatterlist.c
> +++ b/lib/scatterlist.c
> @@ -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)))

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

> +		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

  reply	other threads:[~2022-10-24 17:02 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-24  1:02 [PATCH 0/5] scatterlist: add operations for scsi_debug Douglas Gilbert
2022-10-24  1:02 ` [PATCH 1/5] sgl_alloc_order: remove 4 GiB limit Douglas Gilbert
2022-10-24 12:21   ` Jason Gunthorpe
2022-10-24 14:32     ` Bodo Stroesser [this message]
2022-10-24 17:32       ` Jason Gunthorpe
2022-10-24 19:58         ` Douglas Gilbert
2022-10-25 12:19           ` Jason Gunthorpe
2022-10-25 10:46         ` Bodo Stroesser
2022-10-25 12:18           ` Jason Gunthorpe
2022-10-24  1:02 ` [PATCH 2/5] scatterlist: add sgl_copy_sgl() function Douglas Gilbert
2022-10-24  1:02 ` [PATCH 3/5] scatterlist: add sgl_equal_sgl() function Douglas Gilbert
2022-10-24  1:02 ` [PATCH 4/5] scatterlist: add sgl_memset() Douglas Gilbert
2022-10-24  1:02 ` [PATCH 5/5] scsi_debug: change store from vmalloc to sgl Douglas Gilbert
2022-10-24 15:08   ` Bodo Stroesser

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=665f8dee-6688-60d1-5097-49f9726c38ec@gmail.com \
    --to=bostroesser@gmail.com \
    --cc=bvanassche@acm.org \
    --cc=dgilbert@interlog.com \
    --cc=hare@suse.de \
    --cc=jejb@linux.vnet.ibm.com \
    --cc=jgg@ziepe.ca \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /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