All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Dobson <colpatch@us.ibm.com>
To: kernel-janitors@lists.osdl.org
Cc: Pekka J Enberg <penberg@cs.Helsinki.FI>, linux-kernel@vger.kernel.org
Subject: [KJ] [PATCH 4/8] Cleanup kmem_cache_create()
Date: Tue, 08 Nov 2005 00:53:33 +0000	[thread overview]
Message-ID: <436FF70D.6040604@us.ibm.com> (raw)
In-Reply-To: <436FF51D.8080509@us.ibm.com>

[-- Attachment #1: Type: text/plain, Size: 266 bytes --]

Cleanup kmem_cache_create()

mcd@arrakis:~/linux/source/linux-2.6.14+slab_cleanup/patches $ diffstat
kmem_cache_create.patch
 slab.c |   69
++++++++++++++++++++++++++++-------------------------------------
 1 files changed, 30 insertions(+), 39 deletions(-)

-Matt


[-- Attachment #2: kmem_cache_create.patch --]
[-- Type: text/x-patch, Size: 5386 bytes --]

General readability fixes.

* Reformat a looong if statement
* Replace a constant (4096) with what it represents (PAGE_SIZE)
* Replace a confusing label (opps) with a more sensible one (out)
* Refactor a do {} while loop w/ a couple labels and gotos into
     a for loop with breaks and continues.
* Rewrite some confusing slab alignment code for readability
* Replace a list_for_each/list_entry combo with an identical but
     more readable list_for_each_entry loop.

Index: linux-2.6.14+slab_cleanup/mm/slab.c
===================================================================
--- linux-2.6.14+slab_cleanup.orig/mm/slab.c	2005-11-07 15:58:48.547771048 -0800
+++ linux-2.6.14+slab_cleanup/mm/slab.c	2005-11-07 15:58:50.495474952 -0800
@@ -1499,21 +1499,18 @@ kmem_cache_t *kmem_cache_create(const ch
 				void (*ctor)(void *, kmem_cache_t *, unsigned long),
 				void (*dtor)(void *, kmem_cache_t *, unsigned long))
 {
-	size_t left_over, slab_size, ralign;
+	size_t left_over, slab_size, aligned_slab_size, ralign;
 	kmem_cache_t *cachep = NULL;
 
 	/*
 	 * Sanity checks... these are all serious usage bugs.
 	 */
-	if ((!name) ||
-		in_interrupt() ||
-		(size < BYTES_PER_WORD) ||
-		(size > (1<<MAX_OBJ_ORDER)*PAGE_SIZE) ||
-		(dtor && !ctor)) {
-			printk(KERN_ERR "%s: Early error in slab %s\n",
-					__FUNCTION__, name);
-			BUG();
-		}
+	if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
+	    (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
+		printk(KERN_ERR "%s: Early error in slab %s\n",
+		       __FUNCTION__, name);
+		BUG();
+	}
 
 #if DEBUG
 	WARN_ON(strchr(name, ' '));	/* It confuses parsers */
@@ -1531,7 +1528,7 @@ kmem_cache_t *kmem_cache_create(const ch
 	 * above the next power of two: caches with object sizes just above a
 	 * power of two have a significant amount of internal fragmentation.
 	 */
-	if ((size < 4096 || fls(size-1) == fls(size-1+3*BYTES_PER_WORD)))
+	if (size < PAGE_SIZE || fls(size-1) == fls(size-1 + 3*BYTES_PER_WORD))
 		flags |= SLAB_RED_ZONE|SLAB_STORE_USER;
 	if (!(flags & SLAB_DESTROY_BY_RCU))
 		flags |= SLAB_POISON;
@@ -1594,7 +1591,7 @@ kmem_cache_t *kmem_cache_create(const ch
 	/* Get cache's description obj. */
 	cachep = (kmem_cache_t *) kmem_cache_alloc(&cache_cache, SLAB_KERNEL);
 	if (!cachep)
-		goto opps;
+		goto out;
 	memset(cachep, 0, sizeof(kmem_cache_t));
 
 #if DEBUG
@@ -1652,9 +1649,9 @@ kmem_cache_t *kmem_cache_create(const ch
 		 * gfp() funcs are more friendly towards high-order requests,
 		 * this should be changed.
 		 */
-		do {
-			unsigned int break_flag = 0;
-cal_wastage:
+		unsigned int break_flag = 0;
+
+		for ( ; ; cachep->gfporder++) {
 			cache_estimate(cachep->gfporder, size, align, flags,
 						&left_over, &cachep->num);
 			if (break_flag)
@@ -1662,13 +1659,13 @@ cal_wastage:
 			if (cachep->gfporder >= MAX_GFP_ORDER)
 				break;
 			if (!cachep->num)
-				goto next;
-			if (flags & CFLGS_OFF_SLAB &&
-					cachep->num > offslab_limit) {
+				continue;
+			if ((flags & CFLGS_OFF_SLAB) &&
+			    (cachep->num > offslab_limit)) {
 				/* This num of objs will cause problems. */
-				cachep->gfporder--;
+				cachep->gfporder -= 2;
 				break_flag++;
-				goto cal_wastage;
+				continue;
 			}
 
 			/*
@@ -1680,33 +1677,29 @@ cal_wastage:
 
 			if ((left_over*8) <= (PAGE_SIZE<<cachep->gfporder))
 				break;	/* Acceptable internal fragmentation. */
-next:
-			cachep->gfporder++;
-		} while (1);
+		}
 	}
 
 	if (!cachep->num) {
 		printk("kmem_cache_create: couldn't create cache %s.\n", name);
 		kmem_cache_free(&cache_cache, cachep);
 		cachep = NULL;
-		goto opps;
+		goto out;
 	}
-	slab_size = ALIGN(cachep->num*sizeof(kmem_bufctl_t)
-				+ sizeof(struct slab), align);
+	slab_size = cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
+	aligned_slab_size = ALIGN(slab_size, align);
 
 	/*
 	 * If the slab has been placed off-slab, and we have enough space then
 	 * move it on-slab. This is at the expense of any extra colouring.
 	 */
-	if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
+	if (flags & CFLGS_OFF_SLAB && left_over >= aligned_slab_size) {
 		flags &= ~CFLGS_OFF_SLAB;
-		left_over -= slab_size;
-	}
-
-	if (flags & CFLGS_OFF_SLAB) {
-		/* really off slab. No need for manual alignment */
-		slab_size = cachep->num*sizeof(kmem_bufctl_t)+sizeof(struct slab);
+		left_over -= aligned_slab_size;
 	}
+	/* On slab, need manual alignment */
+	if (!(flags & CFLGS_OFF_SLAB))
+		slab_size = aligned_slab_size;
 
 	cachep->colour_off = cache_line_size();
 	/* Offset must be a multiple of the alignment. */
@@ -1789,13 +1782,12 @@ next:
 	/* Need the semaphore to access the chain. */
 	down(&cache_chain_sem);
 	{
-		struct list_head *p;
+		kmem_cache_t *pc;
 		mm_segment_t old_fs;
 
 		old_fs = get_fs();
 		set_fs(KERNEL_DS);
-		list_for_each(p, &cache_chain) {
-			kmem_cache_t *pc = list_entry(p, kmem_cache_t, next);
+		list_for_each_entry(pc, &cache_chain, next) {
 			char tmp;
 			/*
 			 * This happens when the module gets unloaded & doesn't
@@ -1821,10 +1813,9 @@ next:
 	list_add(&cachep->next, &cache_chain);
 	up(&cache_chain_sem);
 	unlock_cpu_hotplug();
-opps:
+out:
 	if (!cachep && (flags & SLAB_PANIC))
-		panic("kmem_cache_create(): failed to create slab `%s'\n",
-			name);
+		panic("%s: failed to create slab `%s'\n", __FUNCTION__, name);
 	return cachep;
 }
 EXPORT_SYMBOL(kmem_cache_create);

[-- Attachment #3: Type: text/plain, Size: 168 bytes --]

_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors

WARNING: multiple messages have this Message-ID (diff)
From: Matthew Dobson <colpatch@us.ibm.com>
To: kernel-janitors@lists.osdl.org
Cc: Pekka J Enberg <penberg@cs.Helsinki.FI>, linux-kernel@vger.kernel.org
Subject: [PATCH 4/8] Cleanup kmem_cache_create()
Date: Mon, 07 Nov 2005 16:53:33 -0800	[thread overview]
Message-ID: <436FF70D.6040604@us.ibm.com> (raw)
In-Reply-To: <436FF51D.8080509@us.ibm.com>

[-- Attachment #1: Type: text/plain, Size: 266 bytes --]

Cleanup kmem_cache_create()

mcd@arrakis:~/linux/source/linux-2.6.14+slab_cleanup/patches $ diffstat
kmem_cache_create.patch
 slab.c |   69
++++++++++++++++++++++++++++-------------------------------------
 1 files changed, 30 insertions(+), 39 deletions(-)

-Matt


[-- Attachment #2: kmem_cache_create.patch --]
[-- Type: text/x-patch, Size: 5386 bytes --]

General readability fixes.

* Reformat a looong if statement
* Replace a constant (4096) with what it represents (PAGE_SIZE)
* Replace a confusing label (opps) with a more sensible one (out)
* Refactor a do {} while loop w/ a couple labels and gotos into
     a for loop with breaks and continues.
* Rewrite some confusing slab alignment code for readability
* Replace a list_for_each/list_entry combo with an identical but
     more readable list_for_each_entry loop.

Index: linux-2.6.14+slab_cleanup/mm/slab.c
===================================================================
--- linux-2.6.14+slab_cleanup.orig/mm/slab.c	2005-11-07 15:58:48.547771048 -0800
+++ linux-2.6.14+slab_cleanup/mm/slab.c	2005-11-07 15:58:50.495474952 -0800
@@ -1499,21 +1499,18 @@ kmem_cache_t *kmem_cache_create(const ch
 				void (*ctor)(void *, kmem_cache_t *, unsigned long),
 				void (*dtor)(void *, kmem_cache_t *, unsigned long))
 {
-	size_t left_over, slab_size, ralign;
+	size_t left_over, slab_size, aligned_slab_size, ralign;
 	kmem_cache_t *cachep = NULL;
 
 	/*
 	 * Sanity checks... these are all serious usage bugs.
 	 */
-	if ((!name) ||
-		in_interrupt() ||
-		(size < BYTES_PER_WORD) ||
-		(size > (1<<MAX_OBJ_ORDER)*PAGE_SIZE) ||
-		(dtor && !ctor)) {
-			printk(KERN_ERR "%s: Early error in slab %s\n",
-					__FUNCTION__, name);
-			BUG();
-		}
+	if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
+	    (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
+		printk(KERN_ERR "%s: Early error in slab %s\n",
+		       __FUNCTION__, name);
+		BUG();
+	}
 
 #if DEBUG
 	WARN_ON(strchr(name, ' '));	/* It confuses parsers */
@@ -1531,7 +1528,7 @@ kmem_cache_t *kmem_cache_create(const ch
 	 * above the next power of two: caches with object sizes just above a
 	 * power of two have a significant amount of internal fragmentation.
 	 */
-	if ((size < 4096 || fls(size-1) == fls(size-1+3*BYTES_PER_WORD)))
+	if (size < PAGE_SIZE || fls(size-1) == fls(size-1 + 3*BYTES_PER_WORD))
 		flags |= SLAB_RED_ZONE|SLAB_STORE_USER;
 	if (!(flags & SLAB_DESTROY_BY_RCU))
 		flags |= SLAB_POISON;
@@ -1594,7 +1591,7 @@ kmem_cache_t *kmem_cache_create(const ch
 	/* Get cache's description obj. */
 	cachep = (kmem_cache_t *) kmem_cache_alloc(&cache_cache, SLAB_KERNEL);
 	if (!cachep)
-		goto opps;
+		goto out;
 	memset(cachep, 0, sizeof(kmem_cache_t));
 
 #if DEBUG
@@ -1652,9 +1649,9 @@ kmem_cache_t *kmem_cache_create(const ch
 		 * gfp() funcs are more friendly towards high-order requests,
 		 * this should be changed.
 		 */
-		do {
-			unsigned int break_flag = 0;
-cal_wastage:
+		unsigned int break_flag = 0;
+
+		for ( ; ; cachep->gfporder++) {
 			cache_estimate(cachep->gfporder, size, align, flags,
 						&left_over, &cachep->num);
 			if (break_flag)
@@ -1662,13 +1659,13 @@ cal_wastage:
 			if (cachep->gfporder >= MAX_GFP_ORDER)
 				break;
 			if (!cachep->num)
-				goto next;
-			if (flags & CFLGS_OFF_SLAB &&
-					cachep->num > offslab_limit) {
+				continue;
+			if ((flags & CFLGS_OFF_SLAB) &&
+			    (cachep->num > offslab_limit)) {
 				/* This num of objs will cause problems. */
-				cachep->gfporder--;
+				cachep->gfporder -= 2;
 				break_flag++;
-				goto cal_wastage;
+				continue;
 			}
 
 			/*
@@ -1680,33 +1677,29 @@ cal_wastage:
 
 			if ((left_over*8) <= (PAGE_SIZE<<cachep->gfporder))
 				break;	/* Acceptable internal fragmentation. */
-next:
-			cachep->gfporder++;
-		} while (1);
+		}
 	}
 
 	if (!cachep->num) {
 		printk("kmem_cache_create: couldn't create cache %s.\n", name);
 		kmem_cache_free(&cache_cache, cachep);
 		cachep = NULL;
-		goto opps;
+		goto out;
 	}
-	slab_size = ALIGN(cachep->num*sizeof(kmem_bufctl_t)
-				+ sizeof(struct slab), align);
+	slab_size = cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
+	aligned_slab_size = ALIGN(slab_size, align);
 
 	/*
 	 * If the slab has been placed off-slab, and we have enough space then
 	 * move it on-slab. This is at the expense of any extra colouring.
 	 */
-	if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
+	if (flags & CFLGS_OFF_SLAB && left_over >= aligned_slab_size) {
 		flags &= ~CFLGS_OFF_SLAB;
-		left_over -= slab_size;
-	}
-
-	if (flags & CFLGS_OFF_SLAB) {
-		/* really off slab. No need for manual alignment */
-		slab_size = cachep->num*sizeof(kmem_bufctl_t)+sizeof(struct slab);
+		left_over -= aligned_slab_size;
 	}
+	/* On slab, need manual alignment */
+	if (!(flags & CFLGS_OFF_SLAB))
+		slab_size = aligned_slab_size;
 
 	cachep->colour_off = cache_line_size();
 	/* Offset must be a multiple of the alignment. */
@@ -1789,13 +1782,12 @@ next:
 	/* Need the semaphore to access the chain. */
 	down(&cache_chain_sem);
 	{
-		struct list_head *p;
+		kmem_cache_t *pc;
 		mm_segment_t old_fs;
 
 		old_fs = get_fs();
 		set_fs(KERNEL_DS);
-		list_for_each(p, &cache_chain) {
-			kmem_cache_t *pc = list_entry(p, kmem_cache_t, next);
+		list_for_each_entry(pc, &cache_chain, next) {
 			char tmp;
 			/*
 			 * This happens when the module gets unloaded & doesn't
@@ -1821,10 +1813,9 @@ next:
 	list_add(&cachep->next, &cache_chain);
 	up(&cache_chain_sem);
 	unlock_cpu_hotplug();
-opps:
+out:
 	if (!cachep && (flags & SLAB_PANIC))
-		panic("kmem_cache_create(): failed to create slab `%s'\n",
-			name);
+		panic("%s: failed to create slab `%s'\n", __FUNCTION__, name);
 	return cachep;
 }
 EXPORT_SYMBOL(kmem_cache_create);

  parent reply	other threads:[~2005-11-08  0:53 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-08  0:45 [KJ] [PATCH 0/8] Cleanup slab.c Matthew Dobson
2005-11-08  0:45 ` Matthew Dobson
2005-11-08  0:48 ` [KJ] [PATCH 1/8] Apply CodingStyle to mm/slab.c Matthew Dobson
2005-11-08  0:48   ` Matthew Dobson
2005-11-08  0:50 ` [KJ] [PATCH 2/8] Use 'nid' in slab.c Matthew Dobson
2005-11-08  0:50   ` Matthew Dobson
2005-11-08  7:52   ` [KJ] " Pekka J Enberg
2005-11-08  7:52     ` Pekka J Enberg
2005-11-08  8:48     ` [KJ] " Håkon Løvdal
2005-11-08  0:52 ` [KJ] [PATCH 3/8] Fix alloc_percpu()'s args Matthew Dobson
2005-11-08  0:52   ` Matthew Dobson
2005-11-08  0:53 ` Matthew Dobson [this message]
2005-11-08  0:53   ` [PATCH 4/8] Cleanup kmem_cache_create() Matthew Dobson
2005-11-08  2:14   ` [KJ] " Roland Dreier
2005-11-08  2:14     ` Roland Dreier
2005-11-08  7:34     ` [KJ] " Pekka J Enberg
2005-11-08  7:34       ` Pekka J Enberg
2005-11-08 18:49       ` [KJ] " Matthew Dobson
2005-11-08 18:49         ` Matthew Dobson
2005-11-08 18:52     ` [KJ] " Christoph Lameter
2005-11-08 18:52       ` Christoph Lameter
2005-11-08 19:04       ` [KJ] " Matthew Dobson
2005-11-08 19:04         ` Matthew Dobson
2005-11-08 19:09         ` [KJ] " Christoph Lameter
2005-11-08 19:09           ` Christoph Lameter
2005-11-08 19:21           ` [KJ] " Matthew Dobson
2005-11-08 19:21             ` Matthew Dobson
2005-11-08 19:59             ` [KJ] " Manfred Spraul
2005-11-08 19:59               ` Manfred Spraul
2005-11-08  7:51   ` [KJ] " Pekka J Enberg
2005-11-08  7:51     ` Pekka J Enberg
2005-11-08 18:54     ` [KJ] " Matthew Dobson
2005-11-08 18:54       ` Matthew Dobson
2005-11-08 15:00   ` [KJ] " Matthew Wilcox
2005-11-08 15:00     ` Matthew Wilcox
2005-11-08 15:11     ` Pekka J Enberg
2005-11-08 15:11       ` Pekka J Enberg
2005-11-08 19:10       ` Matthew Dobson
2005-11-08 19:10         ` Matthew Dobson
2005-11-08  0:55 ` [KJ] [PATCH 5/8] Cleanup cache_reap() Matthew Dobson
2005-11-08  0:55   ` Matthew Dobson
2005-11-08  0:57 ` [KJ] [PATCH 6/8] Cleanup slabinfo_write() Matthew Dobson
2005-11-08  0:57   ` Matthew Dobson
2005-11-08 10:50   ` [KJ] " Alexey Dobriyan
2005-11-08 10:50     ` Alexey Dobriyan
2005-11-08 18:56     ` Christoph Lameter
2005-11-08 18:56       ` Christoph Lameter
2005-11-08 19:09       ` Matthew Dobson
2005-11-08 19:09         ` Matthew Dobson
2005-11-08  0:58 ` [KJ] [PATCH 7/8] Cleanup set_slab_attr() Matthew Dobson
2005-11-08  0:58   ` Matthew Dobson
2005-11-08  9:43   ` [KJ] " walter harms
2005-11-08 18:59   ` Matthew Dobson
2005-11-08  1:00 ` [KJ] [PATCH 8/8] Inline 3 functions Matthew Dobson
2005-11-08  1:00   ` Matthew Dobson
2005-11-08  7:39   ` [KJ] " Pekka J Enberg
2005-11-08  7:39     ` Pekka J Enberg
2005-11-08 18:59     ` [KJ] " Christoph Lameter
2005-11-08 18:59       ` Christoph Lameter
2005-11-08 19:08     ` [KJ] " Matthew Dobson
2005-11-08 19:08       ` Matthew Dobson
2005-11-10 10:42       ` [KJ] " Adrian Bunk
2005-11-10 10:42         ` Adrian Bunk
2005-11-10 17:04         ` [KJ] " Matthew Dobson
2005-11-10 17:04           ` Matthew Dobson
2005-11-10 17:38           ` [KJ] " Adrian Bunk
2005-11-10 17:38             ` Adrian Bunk
2005-11-10 18:04             ` [KJ] " Oliver Neukum
2005-11-10 18:04               ` Oliver Neukum
2005-11-10 18:20               ` [KJ] " Adrian Bunk
2005-11-10 18:20                 ` Adrian Bunk
2005-11-10 19:22                 ` [KJ] " Oliver Neukum
2005-11-10 19:22                   ` Oliver Neukum
2005-11-10 20:43                   ` [KJ] " Adrian Bunk
2005-11-10 20:43                     ` Adrian Bunk
2005-11-08  7:58 ` [KJ] Re: [PATCH 0/8] Cleanup slab.c Pekka J Enberg
2005-11-08  7:58   ` Pekka J Enberg
2005-11-08 18:56   ` [KJ] " Matthew Dobson
2005-11-08 18:56     ` Matthew Dobson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=436FF70D.6040604@us.ibm.com \
    --to=colpatch@us.ibm.com \
    --cc=kernel-janitors@lists.osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penberg@cs.Helsinki.FI \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.