Skip to content

Conversation

@lyakh
Copy link
Collaborator

@lyakh lyakh commented Nov 13, 2020

Currently when the allocator tries to allocate aligned buffers, if it detects, that the current alignment isn't suitable, it adds alignment to the requested size and tries to allocate that. This is too imprecise and counterintuitive. In the very worst theoretical case alignment - 1 bytes might be needed additionally. Also no need to begin grabbing buffers for continuous allocations, which don't contain an address with required alignment. Simplify the alignment macro too to only perform division once.

Copy link
Member

@lgirdwood lgirdwood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give some background here, what is this fixing ?

@lyakh
Copy link
Collaborator Author

lyakh commented Nov 16, 2020

Can you give some background here, what is this fixing ?

@lgirdwood

  1. the ALIGN_UP() macro: you don't need two divisions, one is enough. In fact I very much suspect, that we only ever align on power of 2 values. So I would put a runtime debug-enabled check for a power of two there and use AND instead of division.
  2. align_ptr() is effectively doing the same again: aligning a pointer up, but instead of using an existing macro it again open-codes it and again uses two divisions.
  3. I think the worst is, that alloc_cont_blocks() isn't taking alignment into account when finding contiguous blocks. Think what would happen if you try to allocate 128 bytes 128-byte aligned from a heap of 32-byte buffers? It would just grab 4 contiguous blocks with no consideration for alignment. After that it would align the starting address, but if it wasn't aligned by chance initially - you'd be overrunning the allocation.

@dbaluta
Copy link
Collaborator

dbaluta commented Nov 16, 2020

@lyakh after your explanation i feel this PR should be split into 3 patches each with proper commit message explaining why it is needed.
Your comment above will be buried in the dark corners of github and no one will ever find this excellent explanation you gave here.

@lgirdwood
Copy link
Member

@lyakh please also check CI

No need for two divisions for an alignment macro, one is enough.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Simplify the align_ptr() function by re-using an existing ALIGN() macro.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
alloc_block() and alloc_cont_blocks() don't use their "caps" parameter,
remote it.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
The condition "size + alignment <= block_size" for allocating
memory from a signle buffer is sufficient but not precise enough.
For example if we want to allocate 20 bytes with 64-byte alignment,
a 32-byte buffer *might* be sufficient if it's suitably aligned.
Fix the algorithm to account for such cases.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
alloc_heap_buffer() is trying to allocate memory from a single heap,
no need to check its size on each iteration of an internal loop over
maps.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
When allocating multiple buffers the first free buffer might not
contain a suitably aligned start address. Skip it instead of
allocating it with the others. Also don't add alignment to the
allocation size, but find a suitably aligned start address
instead.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
@lyakh
Copy link
Collaborator Author

lyakh commented Nov 23, 2020

SOFCI TEST

@lgirdwood
Copy link
Member

@dbaluta it looks like you got 6 patches now !

@lyakh lyakh mentioned this pull request Nov 24, 2020
@lgirdwood
Copy link
Member

@lyakh can you check CI.

@lyakh
Copy link
Collaborator Author

lyakh commented Nov 27, 2020

@lgirdwood splitting this into #3642, #3646 and #3647, we can close this one then.

@lyakh lyakh closed this Nov 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants