ptr_ring: try vmalloc() when kmalloc() fails
Subsystems (2)
PATCH v3 1/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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
include/linux/ptr_ring.h +5 -5
@@ -466,7 +466,7 @@ 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) { -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)
@@ -601,7 +601,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 ; }
@@ -641,7 +641,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);
@@ -649,7 +649,7 @@ static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, nomem:while (--i >=0 ) -kfree (queues[i]); +kvfree (queues[i]);kfree (queues);
@@ -664,7 +664,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
PATCH v3 2/2 ptr_ring: fail on large queue size (>64K)
We need limit the maximum size of queue, otherwise it may cause several side effects e.g slab will warn when the size exceeds KMALLOC_MAX_SIZE. Using KMALLOC_MAX_SIZE still looks too so this patch tries to limit it to 64K. This value could be revisited if we found a real case that needs more.
--- include/linux/ptr_ring.h | 4 ++++ 1 file changed, 4 insertions(+)
include/linux/ptr_ring.h +4 -0
@@ -44,6 +44,8 @@ struct ptr_ring {void **queue; }; +#define PTR_RING_MAX_ALLOC 65536 +/* Note: callers invoking this in a loop must use a compiler barrier, * for example cpu_relax(). *
@@ -466,6 +468,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 >PTR_RING_MAX_ALLOC ) +return NULL ;return kvmalloc_array (size,sizeof (void *), gfp | __GFP_ZERO); }
Signature · 1 line
2.7.4
Quoted 5 lines · Jason Wang
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")
I guess the actual patch is the one that switches tun to ptr_ring.
In fact, I think the actual bugfix is patch 2/2. This specific one just makes kmalloc less likely to fail but that's
not what syzbot reported.
Then I would add this patch on top to make kmalloc less likely to fail.
Quoted 13 lines
Signed-off-by Jason Wang <[email protected]>--- include/linux/ptr_ring.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)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 472
Quoted 8 lines
include/linux/ptr_ring.h +1
-1@@ -466,7 +466,7 @@ 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) { -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)
This implies a bunch of limitations on the flags. From kvmalloc_node docs:
* Reclaim modifiers - __GFP_NORETRY and __GFP_NOFAIL are not supported.
* __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is
* preferable to the vmalloc fallback, due to visible performance drawbacks.
Fine with all the current users, but if we go this way, please add documentation so future users don't misuse this API. Alternatively, test flags and call kvmalloc or kcalloc?
Quoted 38 lines
include/linux/ptr_ring.h +4
-4@@ -601,7 +601,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 ; }@@ -641,7 +641,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);@@ -649,7 +649,7 @@ static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, nomem:while (--i >=0 ) -kfree (queues[i]); +kvfree (queues[i]);kfree (queues);@@ -664,7 +664,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
Re: [PATCH net V3 2/2] ptr_ring: fail on large queue size (>64K)
In reply to Jason WangQuoted 18 lines · Jason Wang
We need limit the maximum size of queue, otherwise it may cause several side effects e.g slab will warn when the size exceeds
KMALLOC_MAX_SIZE. UsingKMALLOC_MAX_SIZEstill looks too so this patch tries to limit it to 64K. This value could be revisited if we found a real case that needs more.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 | 4 ++++ 1 file changed, 4 insertions(+)include/linux/ptr_ring.h +0
-0
include/linux/ptr_ring.h struct ptr_ring { · line 46
Quoted 3 lines
include/linux/ptr_ring.h +0
-0@@ -44,6 +44,8 @@ struct ptr_ring {void **queue; };
Seems like a weird location for a define. Either put defines on top of the file, or near where they are used. I prefer the second option.
include/linux/ptr_ring.h struct ptr_ring { · line 48
Quoted 2 lines
include/linux/ptr_ring.h +2
-0+#define PTR_RING_MAX_ALLOC 65536 +
I guess it's an arbitrary number. Seems like a sufficiently large one, but pls add a comment so readers don't wonder. And please explain what it does:
/* Callers can create ptr_ring structures with userspace-supplied
* parameters. This sets a limit on the size to make that usecase
* safe. If you ever change this, make sure to audit all callers. */
Also I think we should generally use either hex 0x10000 or (1 << 16).
Quoted 14 lines
include/linux/ptr_ring.h +2
-0/* Note: callers invoking this in a loop must use a compiler barrier, * for example cpu_relax(). * @@ -466,6 +468,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 >PTR_RING_MAX_ALLOC ) +return NULL ;return kvmalloc_array (size,sizeof (void *), gfp | __GFP_ZERO); }Signature · 1 line
2.7.4
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
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")I guess the actual patch is the one that switches tun to
ptr_ring.
I think not, since the issue was large allocation.
Quoted 4 lines
In fact, I think the actual bugfix is patch 2/2. This specific one just makes kmalloc less likely to fail but that's
not what syzbot reported.
Agree.
Quoted 2 lines
Then I would add this patch on top to make kmalloc less likely to fail.
Ok.
Quoted 23 lines
Quoted 12 lines
Signed-off-by Jason Wang <[email protected]>--- include/linux/ptr_ring.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)include/linux/ptr_ring.h +0
-0include/linux/ptr_ring.h static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r, · line 472
Quoted 8 lines
include/linux/ptr_ring.h +1
-1@@ -466,7 +466,7 @@ 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) { -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)This implies a bunch of limitations on the flags. From
kvmalloc_nodedocs:
Quoted 6 lines
* Reclaim modifiers -
__GFP_NORETRYand__GFP_NOFAILare not supported.
*__GFP_RETRY_MAYFAILis supported, and it should be used only if kmalloc is
* preferable to the vmalloc fallback, due to visible performance drawbacks.Fine with all the current users, but if we go this way, please add documentation so future users don't misuse this API.
I suspect this is somehow a overkill since this means we need sync with mm/vmalloc changes in the future to keep it synced.
Quoted 1 line
Alternatively, test flags and call kvmalloc or kcalloc?
Similar to the above issue, I would rather leave it as is.
Thanks
Quoted 40 lines
Quoted 40 lines
include/linux/ptr_ring.h +4
-4@@ -601,7 +601,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 ; }@@ -641,7 +641,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);@@ -649,7 +649,7 @@ static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, nomem:while (--i >=0 ) -kfree (queues[i]); +kvfree (queues[i]);kfree (queues);@@ -664,7 +664,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
Re: [PATCH net V3 2/2] ptr_ring: fail on large queue size (>64K)
In reply to Michael S. TsirkinQuoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Wang
We need limit the maximum size of queue, otherwise it may cause several side effects e.g slab will warn when the size exceeds
KMALLOC_MAX_SIZE. UsingKMALLOC_MAX_SIZEstill looks too so this patch tries to limit it to 64K. This value could be revisited if we found a real case that needs more.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 | 4 ++++ 1 file changed, 4 insertions(+)
Quoted 11 lines
include/linux/ptr_ring.h struct ptr_ring { · line 46
Quoted 8 lines
include/linux/ptr_ring.h +0
-0@@ -44,6 +44,8 @@ struct ptr_ring {void **queue; };Seems like a weird location for a define. Either put defines on top of the file, or near where they are used. I prefer the second option.
Ok.
Quoted 7 lines
include/linux/ptr_ring.h struct ptr_ring { · line 48
Quoted 3 lines
include/linux/ptr_ring.h +2
-0+#define PTR_RING_MAX_ALLOC 65536 +I guess it's an arbitrary number. Seems like a sufficiently large one, but pls add a comment so readers don't wonder. And please explain what it does:
Quoted 6 lines
/* Callers can create
ptr_ringstructures with userspace-supplied
* parameters. This sets a limit on the size to make that usecase
* safe. If you ever change this, make sure to audit all callers. */Also I think we should generally use either hex
0x10000or (1 << 16).
I agree the number is arbitrary, so I still prefer the KMALLOC_MAX_SIZE especially consider it was used by pfifo_fast now. Try to limit it to an arbitrary may break lots of exist setups. E.g just google "txqueuelen 100000" can give me a lots of search results.
We can do any kind of optimization on top but not for -net now.
Thanks
Quoted 15 lines
Quoted 15 lines
include/linux/ptr_ring.h +2
-0/* Note: callers invoking this in a loop must use a compiler barrier, * for example cpu_relax(). * @@ -466,6 +468,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 >PTR_RING_MAX_ALLOC ) +return NULL ;return kvmalloc_array (size,sizeof (void *), gfp | __GFP_ZERO); }Signature · 1 line
2.7.4
Re: [PATCH net V3 2/2] ptr_ring: fail on large queue size (>64K)
Quoted 44 lines · Jason Wang
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Wang
We need limit the maximum size of queue, otherwise it may cause several side effects e.g slab will warn when the size exceeds
KMALLOC_MAX_SIZE. UsingKMALLOC_MAX_SIZEstill looks too so this patch tries to limit it to 64K. This value could be revisited if we found a real case that needs more.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 | 4 ++++ 1 file changed, 4 insertions(+)Quoted 10 lines
include/linux/ptr_ring.h struct ptr_ring { · line 45
Quoted 7 lines
include/linux/ptr_ring.h +0
-0@@ -44,6 +44,8 @@ struct ptr_ring {void **queue; };Seems like a weird location for a define. Either put defines on top of the file, or near where they are used. I prefer the second option.
Ok.
Quoted 7 lines
include/linux/ptr_ring.h struct ptr_ring { · line 48
Quoted 3 lines
include/linux/ptr_ring.h +2
-0+#define PTR_RING_MAX_ALLOC 65536 +I guess it's an arbitrary number. Seems like a sufficiently large one, but pls add a comment so readers don't wonder. And please explain what it does:
Quoted 6 lines
/* Callers can create
ptr_ringstructures with userspace-supplied
* parameters. This sets a limit on the size to make that usecase
* safe. If you ever change this, make sure to audit all callers. */Also I think we should generally use either hex
0x10000or (1 << 16).
Quoted 8 lines
I agree the number is arbitrary, so I still prefer the
KMALLOC_MAX_SIZEespecially consider it was used bypfifo_fastnow. Try to limit it to an arbitrary may break lots of exist setups. E.g just google "txqueuelen 100000" can give me a lots of search results.We can do any kind of optimization on top but not for -net now.
Thanks
Interesting. I have an idea for fixing this, but maybe for now KMALLOC_MAX_SIZE does make sense. It's unfortunate that this value is architecture dependent.
The patch still needs code comments though, and fix the math to use the proper size.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
include/linux/ptr_ring.h +2
-0/* Note: callers invoking this in a loop must use a compiler barrier, * for example cpu_relax(). * @@ -466,6 +468,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 >PTR_RING_MAX_ALLOC ) +return NULL ;return kvmalloc_array (size,sizeof (void *), gfp | __GFP_ZERO); }Signature · 1 line
2.7.4
David Miller
maintainerRe: [PATCH net V3 2/2] ptr_ring: fail on large queue size (>64K)
In reply to Jason WangFrom: Jason Wang <[email protected]>
Date: Thu, 8 Feb 2018 11:59:25 +0800
Quoted 9 lines
We need limit the maximum size of queue, otherwise it may cause several side effects e.g slab will warn when the size exceeds
KMALLOC_MAX_SIZE. UsingKMALLOC_MAX_SIZEstill looks too so this patch tries to limit it to 64K. This value could be revisited if we found a real case that needs more.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 static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r, · line 474
Quoted 8 lines
include/linux/ptr_ring.h +2
-0@@ -466,6 +468,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 >PTR_RING_MAX_ALLOC ) +return NULL ;return kvmalloc_array (size,sizeof (void *), gfp | __GFP_ZERO); }
This doesn't limit the allocation to 64K. It limits it to 64K * sizeof(void *).
Quoted 53 lines · Jason Wang
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
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")I guess the actual patch is the one that switches tun to
ptr_ring.I think not, since the issue was large allocation.
Quoted 4 lines
In fact, I think the actual bugfix is patch 2/2. This specific one just makes kmalloc less likely to fail but that's
not what syzbot reported.Agree.
Quoted 2 lines
Then I would add this patch on top to make kmalloc less likely to fail.
Ok.
Quoted 21 lines
Quoted 12 lines
Signed-off-by Jason Wang <[email protected]>--- include/linux/ptr_ring.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)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 6 lines
include/linux/ptr_ring.h +1
-1@@ -466,7 +466,7 @@ 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) { -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)This implies a bunch of limitations on the flags. From
kvmalloc_nodedocs:Quoted 6 lines
* Reclaim modifiers -
__GFP_NORETRYand__GFP_NOFAILare not supported.
*__GFP_RETRY_MAYFAILis supported, and it should be used only if kmalloc is
* preferable to the vmalloc fallback, due to visible performance drawbacks.Fine with all the current users, but if we go this way, please add documentation so future users don't misuse this API.
Quoted 8 lines
I suspect this is somehow a overkill since this means we need sync with mm/vmalloc changes in the future to keep it synced.
Quoted 1 line
Alternatively, test flags and call kvmalloc or kcalloc?
Similar to the above issue, I would rather leave it as is.
Thanks
How do we prevent someone from inevitably trying to use this with GFP_ATOMIC?
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
include/linux/ptr_ring.h +4
-4@@ -601,7 +601,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 ; }@@ -641,7 +641,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);@@ -649,7 +649,7 @@ static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, nomem:while (--i >=0 ) -kfree (queues[i]); +kvfree (queues[i]);kfree (queues);@@ -664,7 +664,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 47 lines · Michael S. Tsirkin
Quoted 46 lines · Jason Wang
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
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")I guess the actual patch is the one that switches tun to
ptr_ring.I think not, since the issue was large allocation.
Quoted 3 lines
In fact, I think the actual bugfix is patch 2/2. This specific one just makes kmalloc less likely to fail but that's
not what syzbot reported.Agree.
Quoted 1 line
Then I would add this patch on top to make kmalloc less likely to fail.
Ok.
Quoted 19 lines
Quoted 10 lines
Signed-off-by Jason Wang<[email protected]>--- include/linux/ptr_ring.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)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 6 lines
include/linux/ptr_ring.h +1
-1@@ -466,7 +466,7 @@ 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) { -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)This implies a bunch of limitations on the flags. From
kvmalloc_nodedocs:Quoted 6 lines
* Reclaim modifiers -
__GFP_NORETRYand__GFP_NOFAILare not supported.
*__GFP_RETRY_MAYFAILis supported, and it should be used only if kmalloc is
* preferable to the vmalloc fallback, due to visible performance drawbacks.Fine with all the current users, but if we go this way, please add documentation so future users don't misuse this API.
I suspect this is somehow a overkill since this means we need sync with mm/vmalloc changes in the future to keep it synced.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Alternatively, test flags and call kvmalloc or kcalloc?
Similar to the above issue, I would rather leave it as is.
Thanks
How do we prevent someone from inevitably trying to use this with
GFP_ATOMIC?
Well, we somehow can't prevent this even if there's a documentation, that's why there's a BUG() in vmalloc code I think. And kvmalloc also requires GFP_KERNEL otherewise another WARN().
So looks like the WARN()/BUG() should be sufficient?
Thanks
Another thing is kvm
Re: [PATCH net V3 2/2] ptr_ring: fail on large queue size (>64K)
In reply to Michael S. TsirkinQuoted 47 lines · Michael S. Tsirkin
Quoted 46 lines · Jason Wang
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Wang
We need limit the maximum size of queue, otherwise it may cause several side effects e.g slab will warn when the size exceeds
KMALLOC_MAX_SIZE. UsingKMALLOC_MAX_SIZEstill looks too so this patch tries to limit it to 64K. This value could be revisited if we found a real case that needs more.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 | 4 ++++ 1 file changed, 4 insertions(+)Quoted 10 lines
include/linux/ptr_ring.h struct ptr_ring { · line 45
Quoted 7 lines
include/linux/ptr_ring.h +0
-0@@ -44,6 +44,8 @@ struct ptr_ring {void **queue; };Seems like a weird location for a define. Either put defines on top of the file, or near where they are used. I prefer the second option.
Ok.
Quoted 6 lines
include/linux/ptr_ring.h struct ptr_ring { · line 48
Quoted 2 lines
include/linux/ptr_ring.h +2
-0+#define PTR_RING_MAX_ALLOC 65536 +I guess it's an arbitrary number. Seems like a sufficiently large one, but pls add a comment so readers don't wonder. And please explain what it does:
Quoted 6 lines
/* Callers can create
ptr_ringstructures with userspace-supplied
* parameters. This sets a limit on the size to make that usecase
* safe. If you ever change this, make sure to audit all callers. */Also I think we should generally use either hex
0x10000or (1 << 16).I agree the number is arbitrary, so I still prefer the
KMALLOC_MAX_SIZEespecially consider it was used bypfifo_fastnow. Try to limit it to an arbitrary may break lots of exist setups. E.g just google "txqueuelen 100000" can give me a lots of search results.We can do any kind of optimization on top but not for -net now.
Quoted 8 lines
Quoted 1 line
Thanks
Interesting. I have an idea for fixing this, but maybe for now
KMALLOC_MAX_SIZEdoes make sense. It's unfortunate that this value is architecture dependent.The patch still needs code comments though, and fix the math to use the proper size.
Yes.
Thanks
Re: [PATCH net V3 2/2] ptr_ring: fail on large queue size (>64K)
In reply to David MillerQuoted 15 lines · David Miller
From: Jason Wang <[email protected]>
Date: Thu, 8 Feb 2018 11:59:25 +0800Quoted 9 lines
We need limit the maximum size of queue, otherwise it may cause several side effects e.g slab will warn when the size exceeds
KMALLOC_MAX_SIZE. UsingKMALLOC_MAX_SIZEstill looks too so this patch tries to limit it to 64K. This value could be revisited if we found a real case that needs more.Reported-by [email protected]Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")Signed-off-by Jason Wang <[email protected]>...
Quoted 2 lines
(quoted diff) +0
-0@@ -466,6 +468,8 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,
Quoted 7 lines
include/linux/ptr_ring.h static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r, · line 474
Quoted 6 lines
include/linux/ptr_ring.h +2
-0static inline void **__ptr_ring_init_queue_alloc (unsigned int size,gfp_t gfp) { +if (size >PTR_RING_MAX_ALLOC ) +return NULL ;return kvmalloc_array (size,sizeof (void *), gfp | __GFP_ZERO); }This doesn't limit the allocation to 64K. It limits it to 64K *
sizeof(void *).
Right, will fix this.
Thanks
Quoted 58 lines · Jason Wang
Quoted 47 lines · Michael S. Tsirkin
Quoted 46 lines · Jason Wang
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
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")I guess the actual patch is the one that switches tun to
ptr_ring.I think not, since the issue was large allocation.
Quoted 3 lines
In fact, I think the actual bugfix is patch 2/2. This specific one just makes kmalloc less likely to fail but that's
not what syzbot reported.Agree.
Quoted 1 line
Then I would add this patch on top to make kmalloc less likely to fail.
Ok.
Quoted 19 lines
Quoted 10 lines
Signed-off-by Jason Wang<[email protected]>--- include/linux/ptr_ring.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)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 6 lines
include/linux/ptr_ring.h +1
-1@@ -466,7 +466,7 @@ 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) { -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)This implies a bunch of limitations on the flags. From
kvmalloc_nodedocs:Quoted 6 lines
* Reclaim modifiers -
__GFP_NORETRYand__GFP_NOFAILare not supported.
*__GFP_RETRY_MAYFAILis supported, and it should be used only if kmalloc is
* preferable to the vmalloc fallback, due to visible performance drawbacks.Fine with all the current users, but if we go this way, please add documentation so future users don't misuse this API.
I suspect this is somehow a overkill since this means we need sync with mm/vmalloc changes in the future to keep it synced.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Alternatively, test flags and call kvmalloc or kcalloc?
Similar to the above issue, I would rather leave it as is.
Thanks
How do we prevent someone from inevitably trying to use this with
GFP_ATOMIC?
Quoted 5 lines
Well, we somehow can't prevent this even if there's a documentation, that's why there's a
BUG()in vmalloc code I think. And kvmalloc also requiresGFP_KERNELotherewise anotherWARN().So looks like the WARN()/BUG() should be sufficient?
Well vmalloc only triggers when you pass in a huge size. Let's settle for
/* Not all gfp_t flags (besides GFP_KERNEL) are allowed. See
* documentation for vmalloc for which of them are legal. */
Quoted 3 lines
Thanks
Another thing is kvm
?
Quoted 57 lines · Michael S. Tsirkin
Quoted 56 lines · Jason Wang
Quoted 54 lines · Michael S. Tsirkin
Quoted 43 lines · Jason Wang
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
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")I guess the actual patch is the one that switches tun to
ptr_ring.I think not, since the issue was large allocation.
Quoted 3 lines
In fact, I think the actual bugfix is patch 2/2. This specific one just makes kmalloc less likely to fail but that's
not what syzbot reported.Agree.
Quoted 1 line
Then I would add this patch on top to make kmalloc less likely to fail.
Ok.
Quoted 19 lines
Quoted 10 lines
Signed-off-by Jason Wang<[email protected]>--- include/linux/ptr_ring.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)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 6 lines
include/linux/ptr_ring.h +1
-1@@ -466,7 +466,7 @@ 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) { -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)This implies a bunch of limitations on the flags. From
kvmalloc_nodedocs:Quoted 6 lines
* Reclaim modifiers -
__GFP_NORETRYand__GFP_NOFAILare not supported.
*__GFP_RETRY_MAYFAILis supported, and it should be used only if kmalloc is
* preferable to the vmalloc fallback, due to visible performance drawbacks.Fine with all the current users, but if we go this way, please add documentation so future users don't misuse this API.
Quoted 7 lines
I suspect this is somehow a overkill since this means we need sync with mm/vmalloc changes in the future to keep it synced.
Quoted 1 line
Alternatively, test flags and call kvmalloc or kcalloc?
Similar to the above issue, I would rather leave it as is.
Thanks
How do we prevent someone from inevitably trying to use this with
GFP_ATOMIC?
Quoted 7 lines
Quoted 5 lines
Well, we somehow can't prevent this even if there's a documentation, that's why there's a
BUG()in vmalloc code I think. And kvmalloc also requiresGFP_KERNELotherewise anotherWARN().So looks like the WARN()/BUG() should be sufficient?
Well vmalloc only triggers when you pass in a huge size. Let's settle for
There's a:
BUG_ON(in_interrupt());
in __get_vm_area_node().
Quoted 4 lines
/* Not all
gfp_tflags (besidesGFP_KERNEL) are allowed. See
* documentation for vmalloc for which of them are legal. */
Fine with me.
Quoted 4 lines
Quoted 3 lines
Thanks
Another thing is kvm
?
Sorry typo.
Thanks