ptr_ring: fail early if queue occupies more than KMALLOC_MAX_SIZE
Subsystems (2)
PATCH v4 1/2 ptr_ring: fail early if queue occupies more than KMALLOC_MAX_SIZE
To avoid slab to warn about exceeded size, fail early if queue occupies more than KMALLOC_MAX_SIZE.
--- include/linux/ptr_ring.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +2 -0
@@ -466,6 +466,8 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,static inline void **__ptr_ring_init_queue_alloc (unsigned int size,gfp_t gfp) { +if (size *sizeof (void *) >KMALLOC_MAX_SIZE ) +return NULL ;return kcalloc (size,sizeof (void *), gfp); }
Signature · 1 line
2.7.4
PATCH v4 2/2 ptr_ring: try vmalloc() when kmalloc() fails
This patch switch to use kvmalloc_array() for using a vmalloc() fallback to help in case kmalloc() fails.
--- include/linux/ptr_ring.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
include/linux/ptr_ring.h +8 -5
@@ -464,11 +464,14 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r, __PTR_RING_PEEK_CALL_v; \ }) +/* Not all gfp_t flags (besides GFP_KERNEL) are allowed. See +* documentation for vmalloc for which of them are legal. +*/ static inline void **__ptr_ring_init_queue_alloc (unsigned int size,gfp_t gfp) {if (size *sizeof (void *) >KMALLOC_MAX_SIZE )return NULL ; -return kcalloc (size,sizeof (void *), gfp); +return kvmalloc_array (size,sizeof (void *), gfp | __GFP_ZERO); }static inline void __ptr_ring_set_size (struct ptr_ring *r,int size)
@@ -603,7 +606,7 @@ static inline int ptr_ring_resize(struct ptr_ring *r, int size, gfp_t gfp,spin_unlock (&(r)->producer_lock);spin_unlock_irqrestore (&(r)->consumer_lock, flags); -kfree (old); +kvfree (old);return 0 ; }
@@ -643,7 +646,7 @@ static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, }for (i =0 ; i < nrings; ++i) -kfree (queues[i]); +kvfree (queues[i]);kfree (queues);
@@ -651,7 +654,7 @@ static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, nomem:while (--i >=0 ) -kfree (queues[i]); +kvfree (queues[i]);kfree (queues);
@@ -666,7 +669,7 @@ static inline void ptr_ring_cleanup(struct ptr_ring *r, void (*destroy)(void *))if (destroy)while ((ptr =ptr_ring_consume (r)))destroy (ptr); - kfree(r->queue); + kvfree(r->queue); }#endif /* _LINUX_PTR_RING_H */
Signature · 1 line
2.7.4
Quoted 6 lines · Jason Wang
To avoid slab to warn about exceeded size, fail early if queue occupies more than
KMALLOC_MAX_SIZE.Reported-by [email protected]Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")Signed-off-by Jason Wang <[email protected]>
Quoted 19 lines
--- include/linux/ptr_ring.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/ptr_ring.h +2
-0@@ -466,6 +466,8 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,static inline void **__ptr_ring_init_queue_alloc (unsigned int size,gfp_t gfp) { +if (size *sizeof (void *) >KMALLOC_MAX_SIZE ) +return NULL ;return kcalloc (size,sizeof (void *), gfp); }Signature · 1 line
2.7.4
Re: [PATCH V4 2/2] ptr_ring: try vmalloc() when kmalloc() fails
In reply to Jason WangQuoted 2 lines · Jason Wang
This patch switch to use
kvmalloc_array()for using avmalloc()fallback to help in casekmalloc()fails.
Above isn't really saying anything about the motivation, it just explains what kvmalloc_array does.
How about:
Switch ptr_ring from kmalloc to kvmalloc. This way it can support larger ring sizes.
Quoted 3 lines
Reported-by [email protected]Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")
I don't see this as a bugfix - it's more of an enhancement.
Do we need this in net or can we defer this to net-next?
I'm not sure myself. For now
Quoted 65 lines
Signed-off-by Jason Wang <[email protected]>--- include/linux/ptr_ring.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)include/linux/ptr_ring.h +8
-5@@ -464,11 +464,14 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r, __PTR_RING_PEEK_CALL_v; \ }) +/* Not all gfp_t flags (besides GFP_KERNEL) are allowed. See +* documentation for vmalloc for which of them are legal. +*/ static inline void **__ptr_ring_init_queue_alloc (unsigned int size,gfp_t gfp) {if (size *sizeof (void *) >KMALLOC_MAX_SIZE )return NULL ; -return kcalloc (size,sizeof (void *), gfp); +return kvmalloc_array (size,sizeof (void *), gfp | __GFP_ZERO); }static inline void __ptr_ring_set_size (struct ptr_ring *r,int size)@@ -603,7 +606,7 @@ static inline int ptr_ring_resize(struct ptr_ring *r, int size, gfp_t gfp,spin_unlock (&(r)->producer_lock);spin_unlock_irqrestore (&(r)->consumer_lock, flags); -kfree (old); +kvfree (old);return 0 ; }@@ -643,7 +646,7 @@ static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, }for (i =0 ; i < nrings; ++i) -kfree (queues[i]); +kvfree (queues[i]);kfree (queues);@@ -651,7 +654,7 @@ static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, nomem:while (--i >=0 ) -kfree (queues[i]); +kvfree (queues[i]);kfree (queues);@@ -666,7 +669,7 @@ static inline void ptr_ring_cleanup(struct ptr_ring *r, void (*destroy)(void *))if (destroy)while ((ptr =ptr_ring_consume (r)))destroy (ptr); - kfree(r->queue); + kvfree(r->queue); }#endif /* _LINUX_PTR_RING_H */ Signature · 1 line
2.7.4
From: Jason Wang <[email protected]>
Date: Fri, 9 Feb 2018 17:45:49 +0800
Quoted 6 lines
To avoid slab to warn about exceeded size, fail early if queue occupies more than
KMALLOC_MAX_SIZE.Reported-by [email protected]Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")Signed-off-by Jason Wang <[email protected]>
Applied.
Re: [PATCH V4 2/2] ptr_ring: try vmalloc() when kmalloc() fails
In reply to Jason WangFrom: Jason Wang <[email protected]>
Date: Fri, 9 Feb 2018 17:45:50 +0800
Quoted 6 lines
This patch switch to use
kvmalloc_array()for using avmalloc()fallback to help in casekmalloc()fails.Reported-by [email protected]Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")Signed-off-by Jason Wang <[email protected]>
Applied.
Hi Jason,
Quoted 15 lines · Jason Wang
To avoid slab to warn about exceeded size, fail early if queue occupies more than
KMALLOC_MAX_SIZE.Reported-by [email protected]Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")Signed-off-by Jason Wang <[email protected]>--- include/linux/ptr_ring.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/ptr_ring.h +0
-0
include/linux/ptr_ring.h static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r, · line 470
Quoted 5 lines
include/linux/ptr_ring.h +2
-0@@ -466,6 +466,8 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,static inline void **__ptr_ring_init_queue_alloc (unsigned int size,gfp_t gfp) { +if (size *sizeof (void *) >KMALLOC_MAX_SIZE ) +return NULL ;
Are you sure that size can't be over 0x40000000? The proper way to write this (safe from integer overflow) would be:
if (size > KMALLOC_MAX_SIZE / sizeof(void *)) return NULL;
- Eric
Re: [PATCH V4 2/2] ptr_ring: try vmalloc() when kmalloc() fails
In reply to Michael S. TsirkinQuoted 8 lines · Michael S. Tsirkin
Quoted 2 lines · Jason Wang
This patch switch to use
kvmalloc_array()for using avmalloc()fallback to help in casekmalloc()fails.Above isn't really saying anything about the motivation, it just explains what
kvmalloc_arraydoes.How about:
Quoted 8 lines
Switch
ptr_ringfrom kmalloc to kvmalloc. This way it can support larger ring sizes.Quoted 2 lines
Reported-by [email protected]Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")I don't see this as a bugfix - it's more of an enhancement.
Do we need this in net or can we defer this to net-next?
Consider they are users of tx_queue_len of more than 1000000 from google searching. We'd better have this in -net.
Thanks
Quoted 5 lines
I'm not sure myself. For now
Quoted 26 lines · Eric Biggers
Hi Jason,
Quoted 15 lines · Jason Wang
To avoid slab to warn about exceeded size, fail early if queue occupies more than
KMALLOC_MAX_SIZE.Reported-by [email protected]Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")Signed-off-by Jason Wang <[email protected]>--- include/linux/ptr_ring.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/ptr_ring.h +0
-0include/linux/ptr_ring.h static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r, · line 470
Quoted 5 lines
include/linux/ptr_ring.h +2
-0@@ -466,6 +466,8 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,static inline void **__ptr_ring_init_queue_alloc (unsigned int size,gfp_t gfp) { +if (size *sizeof (void *) >KMALLOC_MAX_SIZE ) +return NULL ;Are you sure that size can't be over
0x40000000? The proper way to write this (safe from integer overflow) would be:
Quoted 4 lines
if (size > KMALLOC_MAX_SIZE / sizeof(void *)) return NULL;- Eric
Good catch.
Will post a fix.
Thanks