* kmalloc() alignment
@ 2001-03-04 22:17 Kenn Humborg
2001-03-04 22:34 ` Alan Cox
0 siblings, 1 reply; 16+ 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] 16+ 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; 16+ 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] 16+ messages in thread
* Re: kmalloc() alignment
@ 2001-03-04 22:41 Manfred Spraul
2001-03-06 0:06 ` Kenn Humborg
0 siblings, 1 reply; 16+ 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] 16+ 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; 16+ 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] 16+ messages in thread
* Re: kmalloc() alignment
2001-03-05 13:24 ` Alan Cox
@ 2001-03-05 13:22 ` Rogier Wolff
0 siblings, 0 replies; 16+ 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] 16+ 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; 16+ 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] 16+ 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; 16+ 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] 16+ 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; 16+ 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] 16+ 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; 16+ 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] 16+ messages in thread
* Re: kmalloc() alignment
2001-03-06 0:29 ` Kenn Humborg
@ 2001-03-06 2:14 ` Alan Cox
2001-03-06 5:05 ` H. Peter Anvin
0 siblings, 1 reply; 16+ messages in thread
From: Alan Cox @ 2001-03-06 2:14 UTC (permalink / raw)
To: Kenn Humborg; +Cc: H. Peter Anvin, linux-kernel
> > It might be worth asking the question if larger blocks are more
> > aligned?
>
> OK, I'll bite...
> Are larger blocks more aligned?
Only get_free_page()
Alan
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: kmalloc() alignment
2001-03-06 2:14 ` Alan Cox
@ 2001-03-06 5:05 ` H. Peter Anvin
0 siblings, 0 replies; 16+ messages in thread
From: H. Peter Anvin @ 2001-03-06 5:05 UTC (permalink / raw)
To: Alan Cox; +Cc: Kenn Humborg, H. Peter Anvin, linux-kernel
Alan Cox wrote:
>
> > > It might be worth asking the question if larger blocks are more
> > > aligned?
> >
> > OK, I'll bite...
> > Are larger blocks more aligned?
>
> Only get_free_page()
>
I wonder if it would be practical/reasonable to guarantee better
alignment for larger allocations (at least for sizes that are powers of
two); especially 8- and 16-byte alignment is sometimes necessary.
-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] 16+ 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; 16+ 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] 16+ 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; 16+ 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] 16+ messages in thread
* Re: kmalloc() alignment
2001-03-06 8:31 ` Rogier Wolff
@ 2001-03-06 12:10 ` Xavier Bestel
0 siblings, 0 replies; 16+ 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] 16+ messages in thread
* Re: kmalloc() alignment
2001-03-06 8:59 ` Philipp Rumpf
@ 2001-03-06 12:14 ` Alan Cox
0 siblings, 0 replies; 16+ 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] 16+ messages in thread
* kmalloc alignment
@ 2003-05-05 22:03 Thomas Heinz
0 siblings, 0 replies; 16+ 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] 16+ messages in thread
end of thread, other threads:[~2003-05-05 21:50 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-05 22:03 kmalloc alignment Thomas Heinz
-- strict thread matches above, loose matches on Subject: below --
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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox