From: Nick Piggin <npiggin@suse.de>
To: Andrew Morton <akpm@linux-foundation.org>,
Matt Mackall <mpm@selenic.com>,
Linux Memory Management List <linux-mm@kvack.org>
Subject: [patch 3/3] slob: improved alignment handling
Date: Tue, 22 May 2007 09:41:24 +0200 [thread overview]
Message-ID: <20070522074124.GF17051@wotan.suse.de> (raw)
In-Reply-To: <20070522073958.GE17051@wotan.suse.de>
Remove the core slob allocator's minimum alignment restrictions, and
instead introduce the alignment restrictions at the slab API layer.
This lets us heed the ARCH_KMALLOC/SLAB_MINALIGN directives, and also
use __alignof__ (unsigned long) for the default alignment (which should
allow relaxed alignment architectures to take better advantage of SLOB's
small minimum alignment).
Signed-off-by: Nick Piggin <npiggin@suse.de>
Index: linux-2.6/mm/slob.c
===================================================================
--- linux-2.6.orig/mm/slob.c
+++ linux-2.6/mm/slob.c
@@ -7,8 +7,8 @@
*
* The core of SLOB is a traditional K&R style heap allocator, with
* support for returning aligned objects. The granularity of this
- * allocator is 4 bytes on 32-bit and 8 bytes on 64-bit, though it
- * could be as low as 2 if the compiler alignment requirements allow.
+ * allocator is as little as 2 bytes, however typically most architectures
+ * will require 4 bytes on 32-bit and 8 bytes on 64-bit.
*
* The slob heap is a linked list of pages from __get_free_page, and
* within each page, there is a singly-linked list of free blocks (slob_t).
@@ -16,7 +16,7 @@
* first-fit.
*
* Above this is an implementation of kmalloc/kfree. Blocks returned
- * from kmalloc are 4-byte aligned and prepended with a 4-byte header.
+ * from kmalloc are prepended with a 4-byte header with the kmalloc size.
* If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
* __get_free_pages directly, allocating compound pages so the page order
* does not have to be separately tracked, and also stores the exact
@@ -45,13 +45,6 @@
#include <linux/list.h>
#include <asm/atomic.h>
-/* SLOB_MIN_ALIGN == sizeof(long) */
-#if BITS_PER_BYTE == 32
-#define SLOB_MIN_ALIGN 4
-#else
-#define SLOB_MIN_ALIGN 8
-#endif
-
/*
* slob_block has a field 'units', which indicates size of block if +ve,
* or offset of next block if -ve (in SLOB_UNITs).
@@ -60,19 +53,15 @@
* Those with larger size contain their size in the first SLOB_UNIT of
* memory, and the offset of the next free block in the second SLOB_UNIT.
*/
-#if PAGE_SIZE <= (32767 * SLOB_MIN_ALIGN)
+#if PAGE_SIZE <= (32767 * 2)
typedef s16 slobidx_t;
#else
typedef s32 slobidx_t;
#endif
-/*
- * Align struct slob_block to long for now, but can some embedded
- * architectures get away with less?
- */
struct slob_block {
slobidx_t units;
-} __attribute__((aligned(SLOB_MIN_ALIGN)));
+};
typedef struct slob_block slob_t;
/*
@@ -384,14 +373,25 @@ out:
* End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
*/
+#ifndef ARCH_KMALLOC_MINALIGN
+#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long)
+#endif
+
+#ifndef ARCH_SLAB_MINALIGN
+#define ARCH_SLAB_MINALIGN __alignof__(unsigned long)
+#endif
+
+
void *__kmalloc(size_t size, gfp_t gfp)
{
- if (size < PAGE_SIZE - SLOB_UNIT) {
- slob_t *m;
- m = slob_alloc(size + SLOB_UNIT, gfp, 0);
+ int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
+
+ if (size < PAGE_SIZE - align) {
+ unsigned int *m;
+ m = slob_alloc(size + align, gfp, align);
if (m)
- m->units = size;
- return m+1;
+ *m = size;
+ return (void *)m + align;
} else {
void *ret;
@@ -449,8 +449,9 @@ void kfree(const void *block)
sp = (struct slob_page *)virt_to_page(block);
if (slob_page(sp)) {
- slob_t *m = (slob_t *)block - 1;
- slob_free(m, m->units + SLOB_UNIT);
+ int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
+ unsigned int *m = (unsigned int *)(block - align);
+ slob_free(m, *m + align);
} else
put_page(&sp->page);
}
@@ -499,6 +500,8 @@ struct kmem_cache *kmem_cache_create(con
c->ctor = ctor;
/* ignore alignment unless it's forced */
c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
+ if (c->align < ARCH_SLAB_MINALIGN)
+ c->align = ARCH_SLAB_MINALIGN;
if (c->align < align)
c->align = align;
} else if (flags & SLAB_PANIC)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2007-05-22 7:41 UTC|newest]
Thread overview: 66+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-05-22 7:39 [patch 1/3] slob: rework freelist handling Nick Piggin
2007-05-22 7:39 ` [patch 2/3] slob: remove bigblock tracking Nick Piggin
2007-05-22 7:41 ` Nick Piggin [this message]
2007-05-22 14:53 ` [patch 1/3] slob: rework freelist handling Matt Mackall
2007-05-22 19:18 ` Christoph Lameter
2007-05-23 3:06 ` Nick Piggin
2007-05-23 4:55 ` Christoph Lameter
2007-05-23 4:59 ` Nick Piggin
2007-05-23 5:01 ` Christoph Lameter
2007-05-23 5:03 ` Nick Piggin
2007-05-23 5:06 ` Christoph Lameter
2007-05-23 5:11 ` Nick Piggin
2007-05-23 5:14 ` Christoph Lameter
2007-05-23 5:22 ` Nick Piggin
2007-05-23 5:28 ` Christoph Lameter
2007-05-23 6:17 ` Nick Piggin
2007-05-23 6:28 ` Christoph Lameter
2007-05-23 7:12 ` Nick Piggin
2007-05-23 17:03 ` Christoph Lameter
2007-05-23 18:32 ` Matt Mackall
2007-05-23 19:15 ` Christoph Lameter
2007-05-23 19:58 ` Matt Mackall
2007-05-23 20:02 ` Christoph Lameter
2007-05-23 20:16 ` Christoph Lameter
2007-05-23 21:14 ` Matt Mackall
2007-05-23 21:06 ` Matt Mackall
2007-05-23 22:26 ` Christoph Lameter
2007-05-23 22:42 ` Matt Mackall
2007-05-23 22:48 ` Christoph Lameter
2007-05-24 2:05 ` Nick Piggin
2007-05-24 2:45 ` Christoph Lameter
2007-05-24 2:47 ` Nick Piggin
2007-05-24 2:55 ` Christoph Lameter
2007-05-24 3:17 ` Nick Piggin
2007-05-24 2:49 ` Christoph Lameter
2007-05-24 3:15 ` Nick Piggin
2007-05-24 3:51 ` Christoph Lameter
2007-05-24 6:11 ` Matt Mackall
2007-05-24 16:36 ` Christoph Lameter
2007-05-24 17:22 ` Matt Mackall
2007-05-24 17:27 ` Christoph Lameter
2007-05-24 17:44 ` Matt Mackall
2007-05-23 6:38 ` Christoph Lameter
2007-05-23 7:18 ` Nick Piggin
2007-05-23 17:06 ` Christoph Lameter
2007-05-23 7:46 ` Nick Piggin
2007-05-23 17:07 ` Christoph Lameter
2007-05-23 19:35 ` Matt Mackall
2007-05-23 19:59 ` Christoph Lameter
2007-05-23 20:51 ` Matt Mackall
2007-05-24 3:39 ` Nick Piggin
2007-05-24 3:55 ` Christoph Lameter
2007-05-24 4:13 ` Nick Piggin
2007-05-24 4:23 ` Christoph Lameter
2007-05-24 4:31 ` Nick Piggin
2007-05-24 4:35 ` Christoph Lameter
2007-05-24 4:39 ` Nick Piggin
2007-05-24 4:46 ` Christoph Lameter
2007-05-24 4:49 ` Nick Piggin
2007-05-24 5:07 ` Christoph Lameter
2007-05-24 3:24 ` Nick Piggin
2007-05-24 3:49 ` Christoph Lameter
2007-05-24 4:01 ` Nick Piggin
2007-05-24 4:05 ` Christoph Lameter
2007-05-24 4:24 ` Nick Piggin
2007-05-23 18:04 ` Christoph Lameter
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=20070522074124.GF17051@wotan.suse.de \
--to=npiggin@suse.de \
--cc=akpm@linux-foundation.org \
--cc=linux-mm@kvack.org \
--cc=mpm@selenic.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox