Re:Kernel programming Q: new aligned memory

Gerard Roudier (groudier@club-internet.fr)
Sat, 5 Sep 1998 15:22:43 +0200 (MET DST)


This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
Send mail to mime@docserver.cac.washington.edu for more info.

--8323328-1140032397-905001763=:1941
Content-Type: TEXT/PLAIN; charset=US-ASCII

On Fri, 4 Sep 1998, Inaky Perez Gonzalez wrote:

> >>>>> "B" == B James Phillippe <bryan@terran.org> writes:
>
> B> Hello Kernel Gurus, I'm wondering what is the best way to align a
> B> chunk of kernel memory (from kmalloc, say) on a particular byte
> B> boundary. 16 bytes, for instance.
>
> I had the same problem; however, I noticed everything was
> using a power-of-two chunk allocation scheme, so I did a dirty
> function [__usb_kmalloc_align]:

My minimal expectation from a kernel memory allocator is to provide
at least alignment on the lowest power of 2 greater than the requested
size. This can be achieved by using power of 2 sized actual memory
chunks in the allocator.
Such a scheme wastes 25% memory in average but provides lots of
goodnesses and is simple to implement.
Linux-2.0 kernel allocator does not provide such power of 2 alignments.

[ 5 minutes code removed ]

> The point is, if the size of the object is smaller then the
> alignment, ask for a chunk sized as the alignment, else the size. The
> rounding to the next power of two will do the same when
> allocating.

If you want to align a object on a power of 2 greater than the lowest
power of 2 greater than the object size, then you just want to waste
memory, in my opinion. Do you really need that ?

> I've successfully allocated _everyone_ of them, even under
> heavy load, but I am not sure it will always success. So I implemented
> a workaround for the case it failed.
>
> If not aligned, it will recursively allocate more chunks,
> until one falls into the wanted alignment; then it will free them and
> let you with the aligned one. It's ugly, but it works. There's a depth
> limit, just in case ...
>
> It's dirty, could be optimized, etc, etc ... but hey, I did it
> in five minutes :)

Most of things we did in five minutes or less generally donnot smell good.
:-))

BTW, I have attached the allocator code I use in latest experimental
sym53c8xx driver for linux-2.0, if this can help.
Seems Linux-2.1 provides the expected power of 2 alignment, but drivers
have to work for linux-2.0 too.

Regards,
Gerard.

--8323328-1140032397-905001763=:1941
Content-Type: TEXT/PLAIN; charset=US-ASCII; name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aW50IHMgPSAoMSA8PCBNRU1PX1NISUZUKTsNCglzdHJ1Y3QgbV9saW5rICpx
Ow0KCWFkZHIgYSwgYjsNCg0KI2lmZGVmIERFQlVHDQoJcHJpbnRrKCJtX2Zy
ZWUoJXAsICVkKVxuIiwgcHRyLCBzaXplKTsNCiNlbmRpZg0KDQoJaWYgKHNp
emUgPiAoUEFHRV9TSVpFIDw8IE1FTU9fUEFHRV9PUkRFUikpDQoJCXJldHVy
bjsNCg0KCXdoaWxlIChzaXplID4gcykgew0KCQlzIDw8PSAxOw0KCQkrK2k7
DQoJfQ0KDQoJYSA9IChhZGRyKSBwdHI7DQoNCgl3aGlsZSAoMSkgew0KI2lm
ZGVmIE1FTU9fRlJFRV9VTlVTRUQNCgkJaWYgKHMgPT0gKFBBR0VfU0laRSA8
PCBNRU1PX1BBR0VfT1JERVIpKSB7DQoJCQlmcmVlX3BhZ2VzKGEsIE1FTU9f
UEFHRV9PUkRFUik7DQoJCQlicmVhazsNCgkJfQ0KI2VuZGlmDQoJCWIgPSBh
IF4gczsNCgkJcSA9ICZoW2ldOw0KCQl3aGlsZSAocS0+bmV4dCAmJiBxLT5u
ZXh0ICE9IChzdHJ1Y3QgbV9saW5rICopIGIpIHsNCgkJCXEgPSBxLT5uZXh0
Ow0KCQl9DQoJCWlmICghcS0+bmV4dCkgew0KCQkJKChzdHJ1Y3QgbV9saW5r
ICopIGEpLT5uZXh0ID0gaFtpXS5uZXh0Ow0KCQkJaFtpXS5uZXh0ID0gKHN0
cnVjdCBtX2xpbmsgKikgYTsNCgkJCWJyZWFrOw0KCQl9DQoJCXEtPm5leHQg
PSBxLT5uZXh0LT5uZXh0Ow0KCQlhID0gYSAmIGI7DQoJCXMgPDw9IDE7DQoJ
CSsraTsNCgl9DQp9DQo=
--8323328-1140032397-905001763=:1941--

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/faq.html