[24/94] radix tree test suite: Add keme_cache_alloc_bulk() support
diff mbox series

Message ID 20210428153542.2814175-25-Liam.Howlett@Oracle.com
State New, archived
Headers show
Series
  • Introducing the Maple Tree
Related show

Commit Message

Liam Howlett April 28, 2021, 3:35 p.m. UTC
Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
---
 tools/testing/radix-tree/linux.c      | 51 +++++++++++++++++++++++++++
 tools/testing/radix-tree/linux/slab.h |  1 +
 2 files changed, 52 insertions(+)

Comments

Suren Baghdasaryan May 28, 2021, 6:17 p.m. UTC | #1
On Wed, Apr 28, 2021 at 8:36 AM Liam Howlett <liam.howlett@oracle.com> wrote:
>
> Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> ---
>  tools/testing/radix-tree/linux.c      | 51 +++++++++++++++++++++++++++
>  tools/testing/radix-tree/linux/slab.h |  1 +
>  2 files changed, 52 insertions(+)
>
> diff --git a/tools/testing/radix-tree/linux.c b/tools/testing/radix-tree/linux.c
> index 380bbc0a48d6..fb19a40ebb46 100644
> --- a/tools/testing/radix-tree/linux.c
> +++ b/tools/testing/radix-tree/linux.c
> @@ -99,6 +99,57 @@ void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
>         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) && cachep->non_kernel < size)
> +               return 0;
> +
> +       if (!(gfp & __GFP_DIRECT_RECLAIM))
> +               cachep->non_kernel -= size;
> +
> +       pthread_mutex_lock(&cachep->lock);
> +       if (cachep->nr_objs >= size) {
> +               struct radix_tree_node *node = cachep->objs;
> +

I don't think the loop below is correct because "node" is not being
changed on each iteration:

> +               for (i = 0; i < size; i++) {
> +                       cachep->nr_objs--;
> +                       cachep->objs = node->parent;

In the above assignment cachep->objs will be assigned the same value
on all iterations.

> +                       p[i] = cachep->objs;

p[0] should point to the node, however here it would point to the node->parent.

> +               }
> +               pthread_mutex_unlock(&cachep->lock);
> +               node->parent = NULL;

here you terminated the original cachep->objs which is not even inside
the "p" list at this point (it was skipped).

> +       } 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);

I don't see nr_tallocated even in linux-next branch. Was it introduced
in one of the previous patches and I missed it?

> +               if (kmalloc_verbose)
> +                       printf("Allocating %p from slab\n", p[i]);
> +       }
> +
> +       return size;
> +}
> +
>
>  void *kmalloc(size_t size, gfp_t gfp)
>  {
> diff --git a/tools/testing/radix-tree/linux/slab.h b/tools/testing/radix-tree/linux/slab.h
> index 53b79c15b3a2..ba42b8cc11d0 100644
> --- a/tools/testing/radix-tree/linux/slab.h
> +++ b/tools/testing/radix-tree/linux/slab.h
> @@ -25,4 +25,5 @@ struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
>                         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 */
> --
> 2.30.2
Liam Howlett May 28, 2021, 7:28 p.m. UTC | #2
* Suren Baghdasaryan <surenb@google.com> [210528 14:17]:
> On Wed, Apr 28, 2021 at 8:36 AM Liam Howlett <liam.howlett@oracle.com> wrote:
> >
> > Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> > ---
> >  tools/testing/radix-tree/linux.c      | 51 +++++++++++++++++++++++++++
> >  tools/testing/radix-tree/linux/slab.h |  1 +
> >  2 files changed, 52 insertions(+)
> >
> > diff --git a/tools/testing/radix-tree/linux.c b/tools/testing/radix-tree/linux.c
> > index 380bbc0a48d6..fb19a40ebb46 100644
> > --- a/tools/testing/radix-tree/linux.c
> > +++ b/tools/testing/radix-tree/linux.c
> > @@ -99,6 +99,57 @@ void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
> >         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) && cachep->non_kernel < size)
> > +               return 0;
> > +
> > +       if (!(gfp & __GFP_DIRECT_RECLAIM))
> > +               cachep->non_kernel -= size;
> > +
> > +       pthread_mutex_lock(&cachep->lock);
> > +       if (cachep->nr_objs >= size) {
> > +               struct radix_tree_node *node = cachep->objs;
> > +
> 
> I don't think the loop below is correct because "node" is not being
> changed on each iteration:
> 
> > +               for (i = 0; i < size; i++) {
> > +                       cachep->nr_objs--;
> > +                       cachep->objs = node->parent;
> 
> In the above assignment cachep->objs will be assigned the same value
> on all iterations.
> 
> > +                       p[i] = cachep->objs;
> 
> p[0] should point to the node, however here it would point to the node->parent.
> 
> > +               }
> > +               pthread_mutex_unlock(&cachep->lock);
> > +               node->parent = NULL;
> 
> here you terminated the original cachep->objs which is not even inside
> the "p" list at this point (it was skipped).

I just verified that this code wasn't hit in my current test code.  I
will test and fix this.  Good catch.

> 
> > +       } 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);
> 
> I don't see nr_tallocated even in linux-next branch. Was it introduced
> in one of the previous patches and I missed it?

It was introduced with the maple tree itself.  I will spin this off as
its own patch with the same edits as nr_allocated.

> 
> > +               if (kmalloc_verbose)
> > +                       printf("Allocating %p from slab\n", p[i]);
> > +       }
> > +
> > +       return size;
> > +}
> > +
> >
> >  void *kmalloc(size_t size, gfp_t gfp)
> >  {
> > diff --git a/tools/testing/radix-tree/linux/slab.h b/tools/testing/radix-tree/linux/slab.h
> > index 53b79c15b3a2..ba42b8cc11d0 100644
> > --- a/tools/testing/radix-tree/linux/slab.h
> > +++ b/tools/testing/radix-tree/linux/slab.h
> > @@ -25,4 +25,5 @@ struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
> >                         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 */
> > --
> > 2.30.2

Patch
diff mbox series

diff --git a/tools/testing/radix-tree/linux.c b/tools/testing/radix-tree/linux.c
index 380bbc0a48d6..fb19a40ebb46 100644
--- a/tools/testing/radix-tree/linux.c
+++ b/tools/testing/radix-tree/linux.c
@@ -99,6 +99,57 @@  void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
 	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) && cachep->non_kernel < size)
+		return 0;
+
+	if (!(gfp & __GFP_DIRECT_RECLAIM))
+		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)
 {
diff --git a/tools/testing/radix-tree/linux/slab.h b/tools/testing/radix-tree/linux/slab.h
index 53b79c15b3a2..ba42b8cc11d0 100644
--- a/tools/testing/radix-tree/linux/slab.h
+++ b/tools/testing/radix-tree/linux/slab.h
@@ -25,4 +25,5 @@  struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
 			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 */