Skip to content

[RFC] [proposal] alloc: treat memory caps separately - #1394

Closed
mrajwa wants to merge 2 commits into
thesofproject:masterfrom
mrajwa:mem_aloc
Closed

mrajwa wants to merge 2 commits into
thesofproject:masterfrom
mrajwa:mem_aloc

Conversation

@mrajwa

@mrajwa mrajwa commented May 8, 2019

Copy link
Copy Markdown
Contributor

Current implementation accept allocation for specific
memory region only if all CAPS supported by that region
match requested CAPS. This is wrong we should accept
request if at least one CAP match request.

Signed-off-by: Marcin Rajwa marcin.rajwa@linux.intel.com

mrajwa added 2 commits May 8, 2019 21:33
Current implementation accept allocation for specific
memory region only if all CAPS supported by that region
match requested CAPS. This is wrong we should accept
request if at least one CAPS match request.

Signed-off-by: Marcin Rajwa <marcin.rajwa@linux.intel.com>
This check is not needed since at this point
we already know there was no sigle block which
could fullfill this request.

Signed-off-by: Marcin Rajwa <marcin.rajwa@linux.intel.com>
Comment thread src/lib/alloc.c
for (i = 0; i < count; i++) {
mask = heap[i].caps & caps;
if (mask == caps)
if (heap[i].caps & caps)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't the output the same?

heap[i].caps = SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_EXT | SOF_MEM_CAPS_CACHE
caps = SOF_MEM_CAPS_RAM
mask = SOF_MEM_CAPS_RAM

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@tlauda
Please image:
heap[i].caps = SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_EXT
caps = SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_CACHE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, and in the case @libinyang shown the allocation for this heap will be rejected with current version because heap[i].caps != caps . This is wrong we should accept the request because both caps have common SOF_MEM_CAPS_RAM and this is exactly what this PR does :)

@tlauda , do you see it now?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@libinyang Your example shows, that this change is even worse, because it actually breaks things. You are requesting memory which should have the RAM and CACHE capabilities. Why do you expect to have successful allocation on the heap, which supports only one of the caps?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@tlauda, the question is how do we treat CAPS? When we pass 3 different caps along side with request do we want to allocate from first memory region which support either one of these caps OR we want to allocate from memory region which supports all of the caps?
I feel like only the former approach is correct - one of the caps is supported then we can accept request.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@mrajwa It's probably very subjective and depends on how to look at it, but in this case I would assume, that the caps are there to enforce something. If I need to allocate memory which requires CACHE and DMA, then I don't want to have it allocated on the heap which is only CACHE. I view them rather as CACHE AND DMA, not CACHE OR DMA. What is your specific usage scenario? Maybe you just need more generic caps created instead of changing the current logic?

@mrajwa mrajwa May 9, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe you just need more generic caps created instead of changing the current logic?

Well I think the reason why we send multiple CAPS in request is to have, as you said "generic caps". Meaning "please try to allocate from memory region which supports SOF_MEM_CAPS_RAM, but if you fail (not enough space?) instead of terminating the allocation please try with SOF_MEM_CAPS_HP". So this is the way I see it being use.

What is your specific usage scenario?

So we have an issue raised by @keyonjie #1382. Basically, with complete, full WoV topology (multiple pipelines, many components, etc) one of the component (selector in this case but could be any other) failed the allocation of its comp_buffer because it requested SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_HP | SOF_MEM_CAPS_CACHE | SOF_MEM_CAPS_DMA. There is only one memory region which supports all of these caps, namely heap hp buffer. So it looks like this memory region was already consumed at the time selector tried to allocate its buffer. Since no other memory region has exact same mem caps whole allocation failed. If we change the logic to pick memory region which supports one of the requested caps, memory allocation continues with next block and and all works fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes. I'm now agree @tlauda . Otherwise, how can we try to allocate the memory meet both CACHE AND DMA?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@libinyang, with my patch you can not. However do we have such a use case to allocate memory region which supports both CACHE and DMA? Do we in general want to differentiate memory blocks in a way This is for SOF_MEM_CAPS_CACHE AND SOF_MEM_CAPS_DMA but not for SOF_MEM_CAPS_CACHE or SOF_MEM_CAPS_DMA separately?

@mrajwa mrajwa May 9, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So I think it all boils down to the question:
When we send multiple memory CAPS do we want to allocate memory from region which supports either one of the CAPS or only all of them?

Comment thread src/lib/alloc.c
/* allocate if block size is smaller than request */
if (heap->size >= bytes && map->block_size < bytes) {
/* does whole heap have enough space? */
if (heap->size >= bytes) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The original code have a high performance. I suggest using the original code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, this check was already performed in preceding stage and does not belong here.

@mrajwa
mrajwa requested a review from keyonjie May 9, 2019 09:04
@keyonjie

keyonjie commented May 9, 2019

Copy link
Copy Markdown
Contributor

Hi @mrajwa going back to #1382, the allocation in test_keyword_new() is with SOF_MEM_CAPS_RAM caps, so your fix is not only change for the specific case, it will change the whole policy of the allocation, which actually loosen the criteria of the "Matched" memory selection and possible to support more fully use the the whole memory.

My concern is that as your PR changes the mindset what we defined and might lead to regression under some cases, as all users to the allocation interface assumed that it will get memory with full capabilities it listed.

@mrajwa

mrajwa commented May 9, 2019 •

Copy link
Copy Markdown
Contributor Author

@keyonjie, that's true. Let me ask this question, at the time of selector's buffer allocation which failed you sent following request:

SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_HP | SOF_MEM_CAPS_CACHE | SOF_MEM_CAPS_DMA

Now, have you meant "Please allocate memory from region which supports all of these caps or either one of them?"

@tlauda

tlauda commented May 9, 2019

Copy link
Copy Markdown
Contributor

I see couple of issues with such selector caps:

  1. Why does selector require SOF_MEM_CAPS_DMA? It doesn't seem correct.
  2. For cAVS platforms basically every memory address is HP, besides the ones that are part of LPSRAM (and marked LP). So maybe our problem is in how we set the capabilities for our heaps.

We need some additional comments to come up with the right solution @mmaka1 @lgirdwood?

@keyonjie

keyonjie commented May 9, 2019

Copy link
Copy Markdown
Contributor

@keyonjie, that true. Let me ask this question, at the time of selector's buffer allocation you sent following request for memory region:

SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_HP | SOF_MEM_CAPS_CACHE | SOF_MEM_CAPS_DMA

Now, have you meant "Please allocate memory from region which supports all of these caps or either one of them?"

Really? I think we don't need this strict caps for detector buffer, let me check.

@keyonjie

keyonjie commented May 9, 2019

Copy link
Copy Markdown
Contributor

I see couple of issues with such selector caps:

  1. Why does selector require SOF_MEM_CAPS_DMA? It doesn't seem correct.

No, it is incorrect. I also noticed that and figure out solution to fix #1382 with tplg change, verified works, @mrajwa so you can take it easy now. :)

  1. For cAVS platforms basically every memory address is HP, besides the ones that are part of LPSRAM (and marked LP). So maybe our problem is in how we set the capabilities for our heaps.

We need some additional comments to come up with the right solution @mmaka1 @lgirdwood?

@keyonjie

keyonjie commented May 9, 2019

Copy link
Copy Markdown
Contributor

@mrajwa @tlauda here it is the correction for the detector/selector caps: #1399

@mrajwa

mrajwa commented May 9, 2019 •

Copy link
Copy Markdown
Contributor Author

Okay so, we figured out there is a flaw in the topology which was setting incorrect MEM_CAPS for firmware components. However the issue addressed in this PR is still valid and needs to be discussed further. Therefore I opened the discussion here https://github.com/orgs/thesofproject/teams/sof-developers/discussions/14.

I would like to invite you to express your opinion there

@mrajwa mrajwa changed the title alloc: fix bug in memory caps [proposal]alloc: treat memory caps separately May 9, 2019
@mmaka1

mmaka1 commented May 9, 2019 •

Copy link
Copy Markdown

We need some additional comments to come up with the right solution @mmaka1 @lgirdwood?

In general, modelling mutually exclusive values of the same attribute as bits (flags) might make the readability a bit worse. In this example it applies to RAM vs. ROM vs. EXT vs. DMA for example. Coding them as an "enum" using a couple of bits from the caps dword could help to avoid confusing and incorrect RAM | DMA unions in a caller's code.

Anyway, it seems to me that the following platform abstract mem classes are available:

  • RAM,
  • ROM,
  • EXT,
  • DMA

Now, we know that we may combine RAM and DMA with HP and LP attributes and advanced caller may either request a specific type depending on his needs or skip it to indicate that he does not care. In the latter case a platform may have just a single RAM heap (specifies RAM|HP|LP as the caps) or separate ones and should provide them in the preferred allocation order to the allocation unit (eg. RAM|LP goes first followed by RAM|HP). Then a simple (req_caps & heap_caps) == req_caps would work. Other attributes like CACHE shold be filtered out before searching for the heap and processed later (really depends on the attribute).

Edit: CACHE does not seem to be good example of cap ignored while searching for the pool. In case of mem caps, all seem important while registering a pool.

@lgirdwood

Copy link
Copy Markdown
Member

Current implementation accept allocation for specific
memory region only if all CAPS supported by that region
match requested CAPS. This is wrong we should accept
request if at least one CAP match request.

Signed-off-by: Marcin Rajwa marcin.rajwa@linux.intel.com

NAK all specified caller flags must be respected in alloc request.

@lgirdwood lgirdwood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Flags must be respected.

@jajanusz jajanusz changed the title [proposal]alloc: treat memory caps separately [RFC] [proposal] alloc: treat memory caps separately May 20, 2019
@lgirdwood

Copy link
Copy Markdown
Member

@mrajwa this is modelled on how similar API's work on Linux. It must be a bit mask and not an enum so combinations can be explicitly requested. We also cannot remove HP/LP from the request as they have differences in runtime performance.

However, if just RAM was requested with no HP or LP flag then it would be fair to allocate from either HP or LP pool (based on allocator preference).

Btw, you have not mentioned where, why, how exactly you are running out of memory. There are allocator PRs pending that will help here by making the pools more granular.

@tlauda

tlauda commented May 30, 2019

Copy link
Copy Markdown
Contributor

@lgirdwood @mrajwa Can we leave only the second commit and deal with the potential caps changes in the future after deciding how to proceed? The discussion has been already started here: https://github.com/orgs/thesofproject/teams/sof-developers/discussions/14.

@mrajwa

mrajwa commented May 30, 2019 •

Copy link
Copy Markdown
Contributor Author

@lgirdwood the problem was somebody from driver requested allocation of memory poll in SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_HP hoping that FW will allocate it in region which supports either SOF_MEM_CAPS_RAM or SOF_MEM_CAPS_HP. However this is not going to happen since FW will look for and accept only a region which supports both of this flags. This is a bit unintuitive in my opinion.

Although since probably most of our developers are used to it and it works, we can close this PR and in the future we can go forward by defining narrower regions.

PS: I have also added new comment in sof developers discussion https://github.com/orgs/thesofproject/teams/sof-developers/discussions/14

@mmaka1

mmaka1 commented May 31, 2019 •

Copy link
Copy Markdown

@mrajwa The RAM | HP request is valid. It is equal to simpler HP one. HP is RAM (full abbrev. is HPSRAM), so this mem pool should have RAM capability bit set (LP one as well).

In case the client does not care which *P is used it should request just RAM and selection would depend on the order of declaration (I would prefer HP followed by LP to make sure the HP one is selected by default in this case).

@lgirdwood

lgirdwood commented Jun 5, 2019 •

Copy link
Copy Markdown
Member

@mrajwa ok, lets take the second patch and rework the first as discussed https://github.com/orgs/thesofproject/teams/sof-developers/discussions/14
The second patch could come in another PR if its easier.

@lbetlej

lbetlej commented Jan 27, 2020

Copy link
Copy Markdown
Collaborator

@mrajwa What is a status of the RFC ? DO you agree to close it ?

@mrajwa

mrajwa commented Jan 28, 2020

Copy link
Copy Markdown
Contributor Author

@lbetlej, yes I am closing now as there is no need for these changes.

@mrajwa mrajwa closed this Jan 28, 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.

7 participants