Skip to contentSkip to conversation

radix tree test suite: Add support for fallthrough attribute

PATCH In review Liam Howlett 2 participants 6 messages May 12, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 radix tree test suite: Add support for fallthrough attribute

Add support for fallthrough on case statements. Note this does *NOT* check for missing fallthrough, but does allow compiling of code with fallthrough in case statements.

---
 tools/testing/radix-tree/linux/kernel.h | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/radix-tree/linux/kernel.h +7 -0
index 39867fd80c8f..c400a27e544a 100644
@@ -23,4 +23,11 @@
 #define __must_hold(x)
 
 #define EXPORT_PER_CPU_SYMBOL_GPL(x)
+
+#if __has_attribute(__fallthrough__)
+# define fallthrough                    __attribute__((__fallthrough__))
+#else
+# define fallthrough                    do {} while (0)  /* fallthrough */
+#endif /* __has_attribute */
+
 #endif /* _KERNEL_H */
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/4 radix tree test suite: Add __must_be_array() support

Copy __must_be_array() define from include/linux/compiler.h for use in the radix tree test suite userspace compiles.

---
 tools/testing/radix-tree/linux/kernel.h | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/radix-tree/linux/kernel.h +2 -0
index c400a27e544a..2c3771fff2c0 100644
@@ -30,4 +30,6 @@
 # define fallthrough                    do {} while (0)  /* fallthrough */
 #endif /* __has_attribute */
 
+#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
+
 #endif /* _KERNEL_H */
Signature · 1 line
2.30.2
maintainer
#2 lore raw

Re: [PATCH 2/4] radix tree test suite: Add __must_be_array() support

Quoted 2 lines · Liam Howlett

Copy __must_be_array() define from include/linux/compiler.h for use in the radix tree test suite userspace compiles.

We needed this earlier, but see commit
7487de534dcbe143e6f41da751dd3ffcf93b00ee

I bet if you revert this commit, it'll still build fine.

Also, I bet patch 1/4 is the same -- I see a definition of fallthrough in tools/include/linux/compiler-gcc.h

Quoted 18 lines
---
 tools/testing/radix-tree/linux/kernel.h | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/radix-tree/linux/kernel.h +2 -0
index c400a27e544a..2c3771fff2c0 100644
@@ -30,4 +30,6 @@
 # define fallthrough                    do {} while (0)  /* fallthrough */
 #endif /* __has_attribute */
 
+#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
+
 #endif /* _KERNEL_H */
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH 2/4] radix tree test suite: Add __must_be_array() support

Quoted 11 lines · Matthew Wilcox
Quoted 2 lines · Liam Howlett

Copy __must_be_array() define from include/linux/compiler.h for use in the radix tree test suite userspace compiles.

We needed this earlier, but see commit
7487de534dcbe143e6f41da751dd3ffcf93b00ee

I bet if you revert this commit, it'll still build fine.

Also, I bet patch 1/4 is the same -- I see a definition of fallthrough in tools/include/linux/compiler-gcc.h

You are correct. Please disregard patch 1 and 2.

Thanks,
Liam

Quoted 19 lines
Quoted 19 lines
---
 tools/testing/radix-tree/linux/kernel.h | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/radix-tree/linux/kernel.h +2 -0
index c400a27e544a..2c3771fff2c0 100644
@@ -30,4 +30,6 @@
 # define fallthrough                    do {} while (0)  /* fallthrough */
 #endif /* __has_attribute */
 
+#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
+
 #endif /* _KERNEL_H */
Signature · 1 line
2.30.2
#4 lore raw

PATCH 3/4 radix tree test suite: Add kmem_cache enhancements and pr_err

In reply to Liam Howlett

Add kmem_cache_set_non_kernel(), a mechanism to allow a certain number of kmem_cache_alloc requests to succeed even when GFP_KERNEL is not set in the flags.

Add kmem_cache_get_alloc() to see the size of the allocated kmem_cache.

Add a define of pr_err to printk.

---
 tools/testing/radix-tree/linux.c        | 20 ++++++++++++++++++--
 tools/testing/radix-tree/linux/kernel.h |  1 +
 2 files changed, 19 insertions(+), 2 deletions(-)
tools/testing/radix-tree/linux.c +18 -2
index 2d9c59df60de..7225b5c46bb6 100644
@@ -24,14 +24,29 @@ struct kmem_cache {
 	int nr_objs;
 	void *objs;
 	void (*ctor)(void *);
+	unsigned int non_kernel;
 };
 
+void kmem_cache_set_non_kernel(struct kmem_cache *cachep, unsigned int val)
+{
+	cachep->non_kernel = val;
+}
+
+unsigned long kmem_cache_get_alloc(struct kmem_cache *cachep)
+{
+	return cachep->size * nr_allocated;
+}
+
 void *kmem_cache_alloc(struct kmem_cache *cachep, int gfp)
 {
 	void *p;
 
-	if (!(gfp & __GFP_DIRECT_RECLAIM))
-		return NULL;
+	if (!(gfp & __GFP_DIRECT_RECLAIM)) {
+		if (!cachep->non_kernel)
+			return NULL;
+
+		cachep->non_kernel--;
+	}
 
 	pthread_mutex_lock(&cachep->lock);
 	if (cachep->nr_objs) {
@@ -116,5 +131,6 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align,
 	ret->nr_objs = 0;
 	ret->objs = NULL;
 	ret->ctor = ctor;
+	ret->non_kernel = 0;
 	return ret;
 }
tools/testing/radix-tree/linux/kernel.h +1 -0
index 2c3771fff2c0..e44603a181da 100644
@@ -14,6 +14,7 @@
 #include "../../../include/linux/kconfig.h"
 
 #define printk printf
+#define pr_err printk
 #define pr_info printk
 #define pr_debug printk
 #define pr_cont printk
Signature · 1 line
2.30.2
#5 lore raw

PATCH 4/4 radix tree test suite: Add support for slab bulk APIs

In reply to Liam Howlett

Add support for kmem_cache_free_bulk() and kmem_cache_alloc_bulk() to the radix tree test suite.

---
 tools/testing/radix-tree/linux.c      | 61 +++++++++++++++++++++++++++
 tools/testing/radix-tree/linux/slab.h |  3 ++
 2 files changed, 64 insertions(+)
tools/testing/radix-tree/linux.c +61 -0
index 7225b5c46bb6..726407f934d3 100644
@@ -93,6 +93,67 @@ void kmem_cache_free(struct kmem_cache *cachep, void *objp)
 	pthread_mutex_unlock(&cachep->lock);
 }
 
+void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
+{
+	if (kmalloc_verbose)
+		printk("Bulk free %p[0-%lu]\n", list, size - 1);
+
+	for (int i = 0; i < size; i++)
+		kmem_cache_free(cachep, list[i]);
+}
+
+int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
+			  void **p)
+{
+	size_t i;
+
+	if (kmalloc_verbose)
+		printk("Bulk alloc %lu\n", size);
+
+	if (!(gfp & __GFP_DIRECT_RECLAIM)) {
+		if (cachep->non_kernel < size)
+			return 0;
+
+		cachep->non_kernel -= size;
+	}
+
+	pthread_mutex_lock(&cachep->lock);
+	if (cachep->nr_objs >= size) {
+		struct radix_tree_node *node = cachep->objs;
+
+		for (i = 0; i < size; i++) {
+			cachep->nr_objs--;
+			cachep->objs = node->parent;
+			p[i] = cachep->objs;
+		}
+		pthread_mutex_unlock(&cachep->lock);
+		node->parent = NULL;
+	} else {
+		pthread_mutex_unlock(&cachep->lock);
+		for (i = 0; i < size; i++) {
+			if (cachep->align) {
+				posix_memalign(&p[i], cachep->align,
+					       cachep->size * size);
+			} else {
+				p[i] = malloc(cachep->size * size);
+			}
+			if (cachep->ctor)
+				cachep->ctor(p[i]);
+			else if (gfp & __GFP_ZERO)
+				memset(p[i], 0, cachep->size);
+		}
+	}
+
+	for (i = 0; i < size; i++) {
+		uatomic_inc(&nr_allocated);
+		uatomic_inc(&nr_tallocated);
+		if (kmalloc_verbose)
+			printf("Allocating %p from slab\n", p[i]);
+	}
+
+	return size;
+}
+
 void *kmalloc(size_t size, gfp_t gfp)
 {
 	void *ret;
tools/testing/radix-tree/linux/slab.h +3 -0
index 2958830ce4d7..de3befdf14df 100644
@@ -24,4 +24,7 @@ struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
 			unsigned int align, unsigned int flags,
 			void (*ctor)(void *));
 
+void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t, void **);
+int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t, size_t, void **);
+
 #endif		/* SLAB_H */
Signature · 1 line
2.30.2