2001-12-04 18:52:44

by Matthew M

[permalink] [raw]
Subject: [PATCH] kmem_find_general_cachep to static inline

Hi all,

Inlined patch against 2.5.1-pre5 moves kmem_find_general_cachep and the
cache_sizes struct to slab.h and declares it static inline, prompted by a
comment in the source. Compiles and runs fine here.

First patch, please explain what I did wrong :)

*MatthewM*

--
<Addi> Alter.net seems to have replaced one of its router with a zucchini.
--

--- ./include/linux/slab.h~ Mon Dec 3 20:23:27 2001
+++ ./include/linux/slab.h Mon Dec 3 20:26:01 2001
@@ -45,6 +45,44 @@
#define SLAB_CTOR_ATOMIC 0x002UL /* tell constructor it can't sleep */
#define SLAB_CTOR_VERIFY 0x004UL /* tell constructor it's a verify call */

+/* Size description struct for general caches. */
+typedef struct cache_sizes {
+ size_t cs_size;
+ kmem_cache_t *cs_cachep;
+ kmem_cache_t *cs_dmacachep;
+} cache_sizes_t;
+
+static cache_sizes_t cache_sizes[] = {
+#if PAGE_SIZE == 4096
+ { 32, NULL, NULL},
+#endif
+ { 64, NULL, NULL},
+ { 128, NULL, NULL},
+ { 256, NULL, NULL},
+ { 512, NULL, NULL},
+ { 1024, NULL, NULL},
+ { 2048, NULL, NULL},
+ { 4096, NULL, NULL},
+ { 8192, NULL, NULL},
+ { 16384, NULL, NULL},
+ { 32768, NULL, NULL},
+ { 65536, NULL, NULL},
+ {131072, NULL, NULL},
+ { 0, NULL, NULL}
+};
+
+static inline kmem_cache_t * kmem_find_general_cachep (size_t size, int
gfpflags)
+{
+ cache_sizes_t *csizep = cache_sizes;
+
+ for ( ; csizep->cs_size; csizep++) {
+ if (size > csizep->cs_size)
+ continue;
+ break;
+ }
+ return (gfpflags & GFP_DMA) ? csizep->cs_dmacachep : csizep->cs_cachep;
+}
+
/* prototypes */
extern void kmem_cache_init(void);
extern void kmem_cache_sizes_init(void);
--- ./mm/slab.c~ Tue Dec 4 18:12:45 2001
+++ ./mm/slab.c Tue Dec 4 18:12:50 2001
@@ -326,32 +326,6 @@
#define SET_PAGE_SLAB(pg,x) ((pg)->list.prev = (struct list_head *)(x))
#define GET_PAGE_SLAB(pg) ((slab_t *)(pg)->list.prev)

-/* Size description struct for general caches. */
-typedef struct cache_sizes {
- size_t cs_size;
- kmem_cache_t *cs_cachep;
- kmem_cache_t *cs_dmacachep;
-} cache_sizes_t;
-
-static cache_sizes_t cache_sizes[] = {
-#if PAGE_SIZE == 4096
- { 32, NULL, NULL},
-#endif
- { 64, NULL, NULL},
- { 128, NULL, NULL},
- { 256, NULL, NULL},
- { 512, NULL, NULL},
- { 1024, NULL, NULL},
- { 2048, NULL, NULL},
- { 4096, NULL, NULL},
- { 8192, NULL, NULL},
- { 16384, NULL, NULL},
- { 32768, NULL, NULL},
- { 65536, NULL, NULL},
- {131072, NULL, NULL},
- { 0, NULL, NULL}
-};
-
/* internal cache of cache description objs */
static kmem_cache_t cache_cache = {
slabs_full: LIST_HEAD_INIT(cache_cache.slabs_full),
@@ -1587,22 +1561,6 @@
c = GET_PAGE_CACHE(virt_to_page(objp));
__kmem_cache_free(c, (void*)objp);
local_irq_restore(flags);
-}
-
-kmem_cache_t * kmem_find_general_cachep (size_t size, int gfpflags)
-{
- cache_sizes_t *csizep = cache_sizes;
-
- /* This function could be moved to the header file, and
- * made inline so consumers can quickly determine what
- * cache pointer they require.
- */
- for ( ; csizep->cs_size; csizep++) {
- if (size > csizep->cs_size)
- continue;
- break;
- }
- return (gfpflags & GFP_DMA) ? csizep->cs_dmacachep : csizep->cs_cachep;
}

#ifdef CONFIG_SMP


2001-12-05 06:59:09

by Bernd Eckenfels

[permalink] [raw]
Subject: Re: [PATCH] kmem_find_general_cachep to static inline

In article <m16BKdv-000CgdC@Wasteland> you wrote:
> First patch, please explain what I did wrong :)

Nothing, but can be improved further.

One can use that function in kmalloc() and then optimize the function using
a binary search.

--- slab.c 2001/09/21 16:28:50 1.27
+++ slab.c 2001/12/05 07:51:58
@@ -1533,14 +1533,13 @@
*/
void * kmalloc (size_t size, int flags)
{
- cache_sizes_t *csizep = cache_sizes;
+ kmem_cache_t *cp;
+
+ cp = kmem_find_general_cachep(size, flags);
+
+ if (cp)
+ return __kmem_cache_alloc(cp, flags);

- for (; csizep->cs_size; csizep++) {
- if (size > csizep->cs_size)
- continue;
- return __kmem_cache_alloc(flags & GFP_DMA ?
- csizep->cs_dmacachep : csizep->cs_cachep, flags);
- }
return NULL;
}


the patch for having the binary search will look something like:

From: Momchil Velikov <[email protected]>

@@ -1589,18 +1583,66 @@ void kfree (const void *objp)

kmem_cache_t * kmem_find_general_cachep (size_t size, int gfpflags)
{
- cache_sizes_t *csizep = cache_sizes;
+ int idx;

- /* This function could be moved to the header file, and
- * made inline so consumers can quickly determine what
- * cache pointer they require.
- */
- for ( ; csizep->cs_size; csizep++) {
- if (size > csizep->cs_size)
- continue;
+ switch (size) {
+#if PAGE_SIZE == 4096
+ case 0 ... 32:
+ idx = 0;
+ break;
+ case 33 ... 64:
+ idx = 1;
+ break;
+#else
+ case 0 ... 64:
+ idx = 1;
+ break;
+#endif
+ case 65 ... 128:
+ idx = 2;
+ break;
+ case 129 ... 256:
+ idx = 3;
+ break;
+ case 257 ...512:
+ idx = 4;
+ break;
+ case 513 ... 1024:
+ idx = 5;
+ break;
+ case 1025 ... 2048:
+ idx = 6;
+ break;
+ case 2049 ... 4096:
+ idx = 7;
+ break;
+ case 4097 ... 8192:
+ idx = 8;
+ break;
+ case 8193 ... 16384:
+ idx = 9;
+ break;
+ case 16385 ... 32768:
+ idx = 10;
+ break;
+ case 32769 ... 65536:
+ idx = 11;
+ break;
+ case 65537 ... 131072:
+ idx = 12;
+ break;
+ default:
+ idx = -1;
break;
}
- return (gfpflags & GFP_DMA) ? csizep->cs_dmacachep : csizep->cs_cachep;
+
+ if (idx == -1)
+ return NULL;
+
+#if PAGE_SIZE != 4096
+ idx = idx - 1;
+#endif
+ return (gfpflags & GFP_DMA) ? cache_sizes [idx].cs_dmacachep : cache_sizes [idx].cs_cachep;
}

#ifdef CONFIG_SMP


I have not tested yet if the "idx -1" is optimized good, or if it is better
to have assigend idx the values depending on the #define. But otherwise it
should work well.

Greetings
Bernd