Skip to contentSkip to conversation

relay: use kvcalloc to alloc page array in relay_alloc_page_array

PATCH Applied wuchi 2 participants 3 messages Sep 9, 2022 – Sep 10, 2022 Relay

lore
Share & tools
lore

wuchi

author
#0 lore raw

PATCH 1/1 relay: use kvcalloc to alloc page array in relay_alloc_page_array

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

---
 kernel/relay.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/relay.c +1 -4
index 6a611e779e95..d7edc934c56d 100644
@@ -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);
 }
 
 /*
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 8 lines · On Fri, 9 Sep 2022 18:10:25 +0800 wuchi <[email protected]> wrote:

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

...

kernel/relay.c static const struct vm_operations_struct relay_file_mmap_ops = { · line 64

Quoted 9 lines
kernel/relay.c +1 -4
@@ -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.

author
#2 lore raw
Quoted 25 lines · Andrew Morton <[email protected]> 于2022年9月10日周六 05:48写道:
Quoted 8 lines · On Fri, 9 Sep 2022 18:10:25 +0800 wuchi <[email protected]> wrote:

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

...

kernel/relay.c static const struct vm_operations_struct relay_file_mmap_ops = { · line 64

Quoted 9 lines
kernel/relay.c +1 -4
@@ -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.

Quoted 3 lines

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.