Extra align in malloc_mgt_init?

James Cook falsifian at falsifian.org
Mon May 17 17:29:42 PDT 2021

On Mon, May 17, 2021 at 12:40:47PM -0900, Matthew Dillon wrote:
> Hmm.  You might be right.  It should work without that first alignment
> operation because the (size) is already aligned.  But for safety's sake I
> would probably want to change the if() into a while() to allow count to be
> adjusted twice if necessary, just to be sure.  The init function is not in
> a critical path.  I'll test it out a bit and commit with a credit to you.
> Thanks!
> -Matt

Great, thanks, I will test your version.

Thinking about it more, I'm pretty sure the loop will always run zero
times. One proof:

1. If there were no alignment, there would definitely be space:
   we know for sure
   count * (size + sizeof(void*)) <= KMALLOC_SLAB_SIZE - offset of fobjs[0]

2. Now imagine growing the objects downward starting at
   KMALLOC_SLAB_SIZE instead of upward starting at fobjs[count]. They
   will still fit: all you've done is shift the storage toward the end
   of the structure a bit. But now they are aligned, because
   KMALLOC_SLAB_SIZE and size are aligned.

3. The line
       offset = __VM_CACHELINE_ALIGN(offset);
   will not change the offset more than step 2 would have. So there is
   enough space this way too.

I guess the loop should stay anyway. My argument could be wrong, or
something could change in the future that breaks it.


More information about the Kernel mailing list