linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] relay: use kvcalloc to alloc page array in relay_alloc_page_array
@ 2022-09-09 10:10 wuchi
  2022-09-09 21:48 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: wuchi @ 2022-09-09 10:10 UTC (permalink / raw)
  To: hch, axboe, akpm; +Cc: linux-kernel

The kvcalloc is safer because it will check the integer overflows,
and using it will simple the logic of allocation size.

Signed-off-by: wuchi <wuchi.zero@gmail.com>
---
 kernel/relay.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/kernel/relay.c b/kernel/relay.c
index 6a611e779e95..d7edc934c56d 100644
--- a/kernel/relay.c
+++ b/kernel/relay.c
@@ -60,10 +60,7 @@ static const struct vm_operations_struct relay_file_mmap_ops = {
  */
 static struct page **relay_alloc_page_array(unsigned int n_pages)
 {
-	const size_t pa_size = n_pages * sizeof(struct page *);
-	if (pa_size > PAGE_SIZE)
-		return vzalloc(pa_size);
-	return kzalloc(pa_size, GFP_KERNEL);
+	return kvcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
 }
 
 /*
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] relay: use kvcalloc to alloc page array in relay_alloc_page_array
  2022-09-09 10:10 [PATCH] relay: use kvcalloc to alloc page array in relay_alloc_page_array wuchi
@ 2022-09-09 21:48 ` Andrew Morton
  2022-09-10 14:24   ` chi wu
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2022-09-09 21:48 UTC (permalink / raw)
  To: wuchi; +Cc: hch, axboe, linux-kernel

On Fri,  9 Sep 2022 18:10:25 +0800 wuchi <wuchi.zero@gmail.com> wrote:

> The kvcalloc is safer because it will check the integer overflows,
> and using it will simple the logic of allocation size.
> 
> ...
>
> --- a/kernel/relay.c
> +++ b/kernel/relay.c
> @@ -60,10 +60,7 @@ static const struct vm_operations_struct relay_file_mmap_ops = {
>   */
>  static struct page **relay_alloc_page_array(unsigned int n_pages)
>  {
> -	const size_t pa_size = n_pages * sizeof(struct page *);
> -	if (pa_size > PAGE_SIZE)
> -		return vzalloc(pa_size);
> -	return kzalloc(pa_size, GFP_KERNEL);
> +	return kvcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
>  }

It isn't really equivalent because kvcalloc() may attempt a large
kmalloc() request, whereas the current relay_alloc_page_array()
implementation avoids this by choosing vmalloc() instead.

But I doubt if it matters - kvcalloc()->kvmalloc_node() does take some
care to prevent that large kmalloc() request from being too disruptive.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] relay: use kvcalloc to alloc page array in relay_alloc_page_array
  2022-09-09 21:48 ` Andrew Morton
@ 2022-09-10 14:24   ` chi wu
  0 siblings, 0 replies; 3+ messages in thread
From: chi wu @ 2022-09-10 14:24 UTC (permalink / raw)
  To: Andrew Morton; +Cc: hch, axboe, linux-kernel

Andrew Morton <akpm@linux-foundation.org> 于2022年9月10日周六 05:48写道:

>
> On Fri,  9 Sep 2022 18:10:25 +0800 wuchi <wuchi.zero@gmail.com> wrote:
>
> > The kvcalloc is safer because it will check the integer overflows,
> > and using it will simple the logic of allocation size.
> >
> > ...
> >
> > --- a/kernel/relay.c
> > +++ b/kernel/relay.c
> > @@ -60,10 +60,7 @@ static const struct vm_operations_struct relay_file_mmap_ops = {
> >   */
> >  static struct page **relay_alloc_page_array(unsigned int n_pages)
> >  {
> > -     const size_t pa_size = n_pages * sizeof(struct page *);
> > -     if (pa_size > PAGE_SIZE)
> > -             return vzalloc(pa_size);
> > -     return kzalloc(pa_size, GFP_KERNEL);
> > +     return kvcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
> >  }
>
> It isn't really equivalent because kvcalloc() may attempt a large
> kmalloc() request, whereas the current relay_alloc_page_array()
> implementation avoids this by choosing vmalloc() instead.
>
> But I doubt if it matters - kvcalloc()->kvmalloc_node() does take some
> care to prevent that large kmalloc() request from being too disruptive.
>
Thanks very much,got it.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-10 14:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-09 10:10 [PATCH] relay: use kvcalloc to alloc page array in relay_alloc_page_array wuchi
2022-09-09 21:48 ` Andrew Morton
2022-09-10 14:24   ` chi wu

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).