* kmalloc alignment
@ 2004-10-19 14:51 Atsushi Nemoto
2004-10-19 16:59 ` Ralf Baechle
0 siblings, 1 reply; 19+ messages in thread
From: Atsushi Nemoto @ 2004-10-19 14:51 UTC (permalink / raw)
To: linux-mips
In include/asm-mips/cache.h:
#define ARCH_KMALLOC_MINALIGN 8
Is this line really needed?
If it was not defined (and ARCH_KMALLOC_FLAGS was also not defined),
default alignment (cache_line_size()) will be used for kmalloc. It is
enough, isn't it?
Also, with current 8 byte alignment, many PCI drivers which are using
kmalloc and dma_map_single are broken on non-coherent system. I was
told that those drivers should use dma_get_cache_alignment() API, but
currently nobody do it anyway. Removing ARCH_KMALLOC_MINALIGN will
help those (broken?) drivers.
---
Atsushi Nemoto
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc alignment
2004-10-19 14:51 kmalloc alignment Atsushi Nemoto
@ 2004-10-19 16:59 ` Ralf Baechle
2004-10-20 5:56 ` Atsushi Nemoto
0 siblings, 1 reply; 19+ messages in thread
From: Ralf Baechle @ 2004-10-19 16:59 UTC (permalink / raw)
To: Atsushi Nemoto; +Cc: linux-mips
On Tue, Oct 19, 2004 at 11:51:29PM +0900, Atsushi Nemoto wrote:
> In include/asm-mips/cache.h:
>
> #define ARCH_KMALLOC_MINALIGN 8
>
> Is this line really needed?
>
> If it was not defined (and ARCH_KMALLOC_FLAGS was also not defined),
> default alignment (cache_line_size()) will be used for kmalloc. It is
> enough, isn't it?
The alignment needs to be large enough to store an arbitrary fundamental
data type including the 64-bit types such as long long or double.
cache_line_size() is only used if a slab has SLAB_HWCACHE_ALIGN set.
The effect of not guaranteeing 8 byte alignment are subtle at times because
the kernel unaligned handling is going to hide the problem. So just
performance will suffer. It used to show up clearly only in the
floating point context switch because we don't support software emulation
of missaligned floating point loads and stores.
> Also, with current 8 byte alignment, many PCI drivers which are using
> kmalloc and dma_map_single are broken on non-coherent system. I was
> told that those drivers should use dma_get_cache_alignment() API, but
> currently nobody do it anyway. Removing ARCH_KMALLOC_MINALIGN will
> help those (broken?) drivers.
The alignment requirements are documented in Documentation/DMA-API.txt
and they are specified the way they are for good reason.
Ralf
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc alignment
2004-10-19 16:59 ` Ralf Baechle
@ 2004-10-20 5:56 ` Atsushi Nemoto
0 siblings, 0 replies; 19+ messages in thread
From: Atsushi Nemoto @ 2004-10-20 5:56 UTC (permalink / raw)
To: ralf; +Cc: linux-mips
>>>>> On Tue, 19 Oct 2004 18:59:01 +0200, Ralf Baechle <ralf@linux-mips.org> said:
ralf> The alignment needs to be large enough to store an arbitrary
ralf> fundamental data type including the 64-bit types such as long
ralf> long or double.
ralf> cache_line_size() is only used if a slab has SLAB_HWCACHE_ALIGN
ralf> set.
SLAB_HWCACHE_ALIGN is default ARCH_KMALLOC_FLAGS, so normal kmalloc
will use cache_line_size() (if no ARCH_KMALLOC_MINALIGN).
ralf> The alignment requirements are documented in
ralf> Documentation/DMA-API.txt and they are specified the way they
ralf> are for good reason.
Hmm... I had been thought of many PCI ether driver (which maps
skbuff), but I found skb_init() calls kmem_cache_create with
SLAB_HWCACHE_ALIGN. Maybe I should learn much about it... Thank you.
^ permalink raw reply [flat|nested] 19+ messages in thread
* kmalloc alignment
@ 2003-05-05 22:03 Thomas Heinz
0 siblings, 0 replies; 19+ messages in thread
From: Thomas Heinz @ 2003-05-05 22:03 UTC (permalink / raw)
To: linux-kernel
Hi
Does the following property hold for kmalloc (2.4.x)?
Allocating a memory block of size: PAGE_SIZE >= 2^i >= 32 (or 64)
returns an address which is at least 2^i bytes aligned.
I flew over the code and as far as I can see the slabs are allocated
via __get_free_pages which returns PAGE_SIZE bytes aligned memory.
Since each slab allocates only blocks of the same size the property
follows immeadiately.
True or not?
Thanks for your help.
BTW, please cc your reply to my private e-mail since I'm currently
not subscribed.
Regards,
Thomas
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
@ 2001-03-04 22:41 Manfred Spraul
2001-03-06 0:06 ` Kenn Humborg
0 siblings, 1 reply; 19+ messages in thread
From: Manfred Spraul @ 2001-03-04 22:41 UTC (permalink / raw)
To: kenn; +Cc: linux-kernel
>
> Does kmalloc() make any guarantees of the alignment of allocated
> blocks? Will the returned block always be 4-, 8- or 16-byte
> aligned, for example?
>
4-byte alignment is guaranteed on 32-bit cpus, 8-byte alignment on
64-bit cpus.
--
Manfred
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-04 22:41 kmalloc() alignment Manfred Spraul
@ 2001-03-06 0:06 ` Kenn Humborg
2001-03-06 0:15 ` H. Peter Anvin
0 siblings, 1 reply; 19+ messages in thread
From: Kenn Humborg @ 2001-03-06 0:06 UTC (permalink / raw)
To: Manfred Spraul; +Cc: linux-kernel
On Sun, Mar 04, 2001 at 11:41:12PM +0100, Manfred Spraul wrote:
> >
> > Does kmalloc() make any guarantees of the alignment of allocated
> > blocks? Will the returned block always be 4-, 8- or 16-byte
> > aligned, for example?
> >
>
> 4-byte alignment is guaranteed on 32-bit cpus, 8-byte alignment on
> 64-bit cpus.
So, to summarise (for 32-bit CPUs):
o Alan Cox & Manfred Spraul say 4-byte alignment is guaranteed.
o If you need larger alignment, you need to alloc a larger space,
round as necessary, and keep the original pointer for kfree()
Maybe I'll just use get_free_pages, since it's a 64KB chunk that
I need (and it's only a once-off).
Thanks for your advice.
Later,
Kenn
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-06 0:06 ` Kenn Humborg
@ 2001-03-06 0:15 ` H. Peter Anvin
2001-03-06 0:29 ` Kenn Humborg
2001-03-06 8:31 ` Rogier Wolff
0 siblings, 2 replies; 19+ messages in thread
From: H. Peter Anvin @ 2001-03-06 0:15 UTC (permalink / raw)
To: linux-kernel
Followup to: <20010306000652.A13992@excalibur.research.wombat.ie>
By author: Kenn Humborg <kenn@linux.ie>
In newsgroup: linux.dev.kernel
>
> On Sun, Mar 04, 2001 at 11:41:12PM +0100, Manfred Spraul wrote:
> > >
> > > Does kmalloc() make any guarantees of the alignment of allocated
> > > blocks? Will the returned block always be 4-, 8- or 16-byte
> > > aligned, for example?
> > >
> >
> > 4-byte alignment is guaranteed on 32-bit cpus, 8-byte alignment on
> > 64-bit cpus.
>
> So, to summarise (for 32-bit CPUs):
>
> o Alan Cox & Manfred Spraul say 4-byte alignment is guaranteed.
>
> o If you need larger alignment, you need to alloc a larger space,
> round as necessary, and keep the original pointer for kfree()
>
> Maybe I'll just use get_free_pages, since it's a 64KB chunk that
> I need (and it's only a once-off).
>
It might be worth asking the question if larger blocks are more
aligned?
-hpa
--
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-06 0:15 ` H. Peter Anvin
@ 2001-03-06 0:29 ` Kenn Humborg
2001-03-06 2:14 ` Alan Cox
2001-03-06 8:31 ` Rogier Wolff
1 sibling, 1 reply; 19+ messages in thread
From: Kenn Humborg @ 2001-03-06 0:29 UTC (permalink / raw)
To: H. Peter Anvin; +Cc: linux-kernel
On Mon, Mar 05, 2001 at 04:15:36PM -0800, H. Peter Anvin wrote:
> > So, to summarise (for 32-bit CPUs):
> >
> > o Alan Cox & Manfred Spraul say 4-byte alignment is guaranteed.
> >
> > o If you need larger alignment, you need to alloc a larger space,
> > round as necessary, and keep the original pointer for kfree()
> >
> > Maybe I'll just use get_free_pages, since it's a 64KB chunk that
> > I need (and it's only a once-off).
> >
>
> It might be worth asking the question if larger blocks are more
> aligned?
OK, I'll bite...
Are larger blocks more aligned?
Later,
Kenn
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-06 0:15 ` H. Peter Anvin
2001-03-06 0:29 ` Kenn Humborg
@ 2001-03-06 8:31 ` Rogier Wolff
2001-03-06 12:10 ` Xavier Bestel
1 sibling, 1 reply; 19+ messages in thread
From: Rogier Wolff @ 2001-03-06 8:31 UTC (permalink / raw)
To: H. Peter Anvin; +Cc: linux-kernel
> Followup to: <20010306000652.A13992@excalibur.research.wombat.ie>
> By author: Kenn Humborg <kenn@linux.ie>
> In newsgroup: linux.dev.kernel
> >
> > On Sun, Mar 04, 2001 at 11:41:12PM +0100, Manfred Spraul wrote:
> > > >
> > > > Does kmalloc() make any guarantees of the alignment of allocated
> > > > blocks? Will the returned block always be 4-, 8- or 16-byte
> > > > aligned, for example?
> > > >
> > >
> > > 4-byte alignment is guaranteed on 32-bit cpus, 8-byte alignment on
> > > 64-bit cpus.
> >
> > So, to summarise (for 32-bit CPUs):
> >
> > o Alan Cox & Manfred Spraul say 4-byte alignment is guaranteed.
> >
> > o If you need larger alignment, you need to alloc a larger space,
> > round as necessary, and keep the original pointer for kfree()
> >
> > Maybe I'll just use get_free_pages, since it's a 64KB chunk that
> > I need (and it's only a once-off).
My old kmalloc would actually use n+10 bytes if you request n bytes.
As memory comes in pools of powers of two, if you request 64k, you
would acutaly use 128k of memory. If you use "get_free_pages", you'll
not have the overhead, and actually allocate the 64k you need.
I'm not sure what the slab stuff does...
Roger.
--
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2137555 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
* There are old pilots, and there are bold pilots.
* There are also old, bald pilots.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-06 8:31 ` Rogier Wolff
@ 2001-03-06 12:10 ` Xavier Bestel
0 siblings, 0 replies; 19+ messages in thread
From: Xavier Bestel @ 2001-03-06 12:10 UTC (permalink / raw)
To: linux-kernel
Le 06 Mar 2001 09:31:01 +0100, Rogier Wolff a écrit :
>
> > Followup to: <20010306000652.A13992@excalibur.research.wombat.ie>
> > By author: Kenn Humborg <kenn@linux.ie>
> > In newsgroup: linux.dev.kernel
> > >
> > > On Sun, Mar 04, 2001 at 11:41:12PM +0100, Manfred Spraul wrote:
> > > > >
> > > > > Does kmalloc() make any guarantees of the alignment of allocated
> > > > > blocks? Will the returned block always be 4-, 8- or 16-byte
> > > > > aligned, for example?
> > > > >
> > > >
> > > > 4-byte alignment is guaranteed on 32-bit cpus, 8-byte alignment on
> > > > 64-bit cpus.
> > >
> > > So, to summarise (for 32-bit CPUs):
> > >
> > > o Alan Cox & Manfred Spraul say 4-byte alignment is guaranteed.
> > >
> > > o If you need larger alignment, you need to alloc a larger space,
> > > round as necessary, and keep the original pointer for kfree()
> > >
> > > Maybe I'll just use get_free_pages, since it's a 64KB chunk that
> > > I need (and it's only a once-off).
>
> My old kmalloc would actually use n+10 bytes if you request n bytes.
> As memory comes in pools of powers of two, if you request 64k, you
> would acutaly use 128k of memory. If you use "get_free_pages", you'll
> not have the overhead, and actually allocate the 64k you need.
>
> I'm not sure what the slab stuff does...
A properly initialised (i.e. default settings) 64k slab would put object
descriptors outside the slab itself, and so use the expected number of
pages for each 64k object, I believe.
Small or non n*512 sized objects are a different story.
Xav
^ permalink raw reply [flat|nested] 19+ messages in thread
* kmalloc() alignment
@ 2001-03-04 22:17 Kenn Humborg
2001-03-04 22:34 ` Alan Cox
0 siblings, 1 reply; 19+ messages in thread
From: Kenn Humborg @ 2001-03-04 22:17 UTC (permalink / raw)
To: Linux-Kernel
Does kmalloc() make any guarantees of the alignment of allocated
blocks? Will the returned block always be 4-, 8- or 16-byte
aligned, for example?
Later,
Kenn
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-04 22:17 Kenn Humborg
@ 2001-03-04 22:34 ` Alan Cox
2001-03-05 9:40 ` Rogier Wolff
2001-03-06 8:59 ` Philipp Rumpf
0 siblings, 2 replies; 19+ messages in thread
From: Alan Cox @ 2001-03-04 22:34 UTC (permalink / raw)
To: Kenn Humborg; +Cc: Linux-Kernel
> Does kmalloc() make any guarantees of the alignment of allocated
> blocks? Will the returned block always be 4-, 8- or 16-byte
> aligned, for example?
There are people who assume 16byte alignment guarantees. I dont think anyone
has formally specified the guarantee beyond 4 bytes tho
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-04 22:34 ` Alan Cox
@ 2001-03-05 9:40 ` Rogier Wolff
2001-03-05 13:24 ` Alan Cox
2001-03-06 8:59 ` Philipp Rumpf
1 sibling, 1 reply; 19+ messages in thread
From: Rogier Wolff @ 2001-03-05 9:40 UTC (permalink / raw)
To: Alan Cox; +Cc: Kenn Humborg, Linux-Kernel
Alan Cox wrote:
> > Does kmalloc() make any guarantees of the alignment of allocated
> > blocks? Will the returned block always be 4-, 8- or 16-byte
> > aligned, for example?
> There are people who assume 16byte alignment guarantees. I dont
> think anyone has formally specified the guarantee beyond 4 bytes tho
What does "formally specified" mean?
As far as I know, you can count on 16-bytes alignment from
kmalloc. The trouble is that you would have to keep the original
pointer and free that if you have to do the "round" yourself.
I once wrote a kmalloc(*) that would allow you to free any pointer
inside the kmalloc-ed area. This is dangerous as freeing a random
pointer is more likely to "work". But in this case it would be very
convenient.
Roger.
(*) Too buggy for anyone but me.
--
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2137555 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
* There are old pilots, and there are bold pilots.
* There are also old, bald pilots.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-05 9:40 ` Rogier Wolff
@ 2001-03-05 13:24 ` Alan Cox
2001-03-05 13:22 ` Rogier Wolff
0 siblings, 1 reply; 19+ messages in thread
From: Alan Cox @ 2001-03-05 13:24 UTC (permalink / raw)
To: Rogier Wolff; +Cc: Alan Cox, Kenn Humborg, Linux-Kernel
> As far as I know, you can count on 16-bytes alignment from
> kmalloc. The trouble is that you would have to keep the original
Actually it depends on the debug settings
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-05 13:24 ` Alan Cox
@ 2001-03-05 13:22 ` Rogier Wolff
0 siblings, 0 replies; 19+ messages in thread
From: Rogier Wolff @ 2001-03-05 13:22 UTC (permalink / raw)
To: Alan Cox; +Cc: Rogier Wolff, Kenn Humborg, Linux-Kernel
Alan Cox wrote:
> > As far as I know, you can count on 16-bytes alignment from
> > kmalloc. The trouble is that you would have to keep the original
>
> Actually it depends on the debug settings
Actually THAT's a bug in the debug stuff....
Roger.
--
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2137555 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
* There are old pilots, and there are bold pilots.
* There are also old, bald pilots.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-04 22:34 ` Alan Cox
2001-03-05 9:40 ` Rogier Wolff
@ 2001-03-06 8:59 ` Philipp Rumpf
2001-03-06 12:14 ` Alan Cox
1 sibling, 1 reply; 19+ messages in thread
From: Philipp Rumpf @ 2001-03-06 8:59 UTC (permalink / raw)
To: Alan Cox; +Cc: Kenn Humborg, Linux-Kernel
On Sun, Mar 04, 2001 at 10:34:31PM +0000, Alan Cox wrote:
> > Does kmalloc() make any guarantees of the alignment of allocated
> > blocks? Will the returned block always be 4-, 8- or 16-byte
> > aligned, for example?
>
> There are people who assume 16byte alignment guarantees. I dont think anyone
> has formally specified the guarantee beyond 4 bytes tho
Userspace malloc is "suitably aligned for any kind of variable", so I think
expecting 8 bytes alignment (long long on 32-bit platforms) should be okay.
>From reading the code it seems as though we actually use L1_CACHE_BYTES,
and I think it might be a good idea to document the current behaviour (as
long as there's no good reason to change it ?)
diff -ur linux/mm/slab.c linux-prumpf/mm/slab.c
--- linux/mm/slab.c Tue Mar 6 00:54:38 2001
+++ linux-prumpf/mm/slab.c Tue Mar 6 01:00:47 2001
@@ -1525,9 +1525,10 @@
* @flags: the type of memory to allocate.
*
* kmalloc is the normal method of allocating memory
- * in the kernel. Note that the @size parameter must be less than or
- * equals to %KMALLOC_MAXSIZE and the caller must ensure this. The @flags
- * argument may be one of:
+ * in the kernel. It returns a pointer (aligned to a hardware cache line
+ * boundary) to the allocated memory, or %NULL in case of failure. Note that
+ * the @size parameter must be less than or equal to %KMALLOC_MAXSIZE and
+ * the caller must ensure this. The @flags argument may be one of:
*
* %GFP_USER - Allocate memory on behalf of user. May sleep.
*
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: kmalloc() alignment
2001-03-06 8:59 ` Philipp Rumpf
@ 2001-03-06 12:14 ` Alan Cox
0 siblings, 0 replies; 19+ messages in thread
From: Alan Cox @ 2001-03-06 12:14 UTC (permalink / raw)
To: Philipp Rumpf; +Cc: Alan Cox, Kenn Humborg, Linux-Kernel
> > There are people who assume 16byte alignment guarantees. I dont think anyone
> > has formally specified the guarantee beyond 4 bytes tho
>
> Userspace malloc is "suitably aligned for any kind of variable", so I think
> expecting 8 bytes alignment (long long on 32-bit platforms) should be okay.
>
> >From reading the code it seems as though we actually use L1_CACHE_BYTES,
> and I think it might be a good idea to document the current behaviour (as
> long as there's no good reason to change it ?)
With slab poisoning I dont belive this is true
^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2004-10-20 5:58 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-19 14:51 kmalloc alignment Atsushi Nemoto
2004-10-19 16:59 ` Ralf Baechle
2004-10-20 5:56 ` Atsushi Nemoto
-- strict thread matches above, loose matches on Subject: below --
2003-05-05 22:03 Thomas Heinz
2001-03-04 22:41 kmalloc() alignment Manfred Spraul
2001-03-06 0:06 ` Kenn Humborg
2001-03-06 0:15 ` H. Peter Anvin
2001-03-06 0:29 ` Kenn Humborg
2001-03-06 2:14 ` Alan Cox
2001-03-06 5:05 ` H. Peter Anvin
2001-03-06 8:31 ` Rogier Wolff
2001-03-06 12:10 ` Xavier Bestel
2001-03-04 22:17 Kenn Humborg
2001-03-04 22:34 ` Alan Cox
2001-03-05 9:40 ` Rogier Wolff
2001-03-05 13:24 ` Alan Cox
2001-03-05 13:22 ` Rogier Wolff
2001-03-06 8:59 ` Philipp Rumpf
2001-03-06 12:14 ` Alan Cox
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.