Skip to contentSkip to conversation

ptr_ring: fail early if queue occupies more than KMALLOC_MAX_SIZE

PATCH Applied Jason Wang 4 participants 9 messages Feb 9, 2018 – Feb 11, 2018

Subsystems (2)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

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.

Reported-by [email protected]
Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")
---
 include/linux/ptr_ring.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +2 -0
index 1883d61..6051a5f 100644
@@ -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
author
#1 lore raw

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.

Reported-by [email protected]
Fixes 2e0ab8ca83c12 ("ptr_ring: array based FIFO for pointers")
---
 include/linux/ptr_ring.h | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
include/linux/ptr_ring.h +8 -5
index 6051a5f..b884b77 100644
@@ -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
Acked-by on v4 2/2
#2 lore raw

Re: [PATCH V4 2/2] ptr_ring: try vmalloc() when kmalloc() fails

Quoted 2 lines · Jason Wang

This patch switch to use kvmalloc_array() for using a vmalloc() fallback to help in case kmalloc() 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
---
 include/linux/ptr_ring.h | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
include/linux/ptr_ring.h +8 -5
index 6051a5f..b884b77 100644
@@ -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
author
#3 lore raw

Re: [PATCH V4 2/2] ptr_ring: try vmalloc() when kmalloc() fails

Quoted 8 lines · Michael S. Tsirkin
Quoted 2 lines · Jason Wang

This patch switch to use kvmalloc_array() for using a vmalloc() fallback to help in case kmalloc() fails.

Above isn't really saying anything about the motivation, it just explains what kvmalloc_array does.

How about:

Quoted 8 lines

Switch ptr_ring from 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

Acked-by on v4 1/2
#5 lore raw
In reply to Jason Wang
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")
Quoted 19 lines
---
 include/linux/ptr_ring.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +2 -0
index 1883d61..6051a5f 100644
@@ -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
#7 lore raw
In reply to Jason Wang

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")
---
 include/linux/ptr_ring.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +0 -0
index 1883d61..6051a5f 100644

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

author
#8 lore raw
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")
---
  include/linux/ptr_ring.h | 2 ++
  1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +0 -0
index 1883d61..6051a5f 100644

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:

Quoted 4 lines
	if (size > KMALLOC_MAX_SIZE / sizeof(void *))
		return NULL;

- Eric

Good catch.

Will post a fix.

Thanks