You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current implementation of NvmCtxMgmtRestore allocates many kilobytes memory on the stack stack. This could be avoided if the semantics of MIB_NVM_CTXS were cleaned up.
The current semantics seem to be that a read of MIB_NVM_CTXS returns a buffer pointer that should be assumed to be read-only and a size for every context. Setting MIB_NVM_CTXS copies data from a caller-allocated buffer to an internal context.
As a consequence, the current implementation of NvmCtxMgmtRestore looks something like this:
Get MIB_NVM_CTXS to get the sizes of all of the contexts.
Allocate enough space to store the contexts on the stack (this is generally considered bad practice).
Read contexts from NVM to temporary on the stack.
Set MIB_NVM_CTXS to apply the restored contexts.
Each module performs a memcpy1 from the temporary buffer to its internal NVM context buffer.
The semantics could be changed to mean that getting MIB_NVM_CTXS returns non-constant pointers and sizes to the internal state of the stack and setting MIB_NVM_CTXS notifies the stack that the data pointed to by those pointers has been updated. This would significantly simplify the code for the caller and avoid dynamic memory allocation. In this case, NvmCtxMgmtRestore would look something like this:
Get MIB_NVM_CTXS to get the sizes of all of the contexts.
Read contexts from NVM to pointers received in step 1.
Set MIB_NVM_CTXS to apply the restored contexts.
This would remove the need for a heap allocation or large stack allocation in NvmCtxMgmtRestore and remove one buffer copy. The current use of large stack allocations makes the code unpredictable in resource constrained environments.
In terms of code changes, the only change necessary would be to update NvmCtxMgmtRestore to read straight into the context buffers and to remove the memcpy1 calls in the context handlers.
The text was updated successfully, but these errors were encountered:
I was mostly concerned about the amount of dynamic memory (i.e., stack or heap) used by the previous implementation which would lead to unpredictable out-of-memory conditions at runtime. The new version seems to solve this problem.
The current implementation of
NvmCtxMgmtRestore
allocates many kilobytes memory on the stack stack. This could be avoided if the semantics ofMIB_NVM_CTXS
were cleaned up.The current semantics seem to be that a read of MIB_NVM_CTXS returns a buffer pointer that should be assumed to be read-only and a size for every context. Setting MIB_NVM_CTXS copies data from a caller-allocated buffer to an internal context.
As a consequence, the current implementation of
NvmCtxMgmtRestore
looks something like this:MIB_NVM_CTXS
to get the sizes of all of the contexts.MIB_NVM_CTXS
to apply the restored contexts.memcpy1
from the temporary buffer to its internal NVM context buffer.The semantics could be changed to mean that getting MIB_NVM_CTXS returns non-constant pointers and sizes to the internal state of the stack and setting MIB_NVM_CTXS notifies the stack that the data pointed to by those pointers has been updated. This would significantly simplify the code for the caller and avoid dynamic memory allocation. In this case,
NvmCtxMgmtRestore
would look something like this:MIB_NVM_CTXS
to get the sizes of all of the contexts.MIB_NVM_CTXS
to apply the restored contexts.This would remove the need for a heap allocation or large stack allocation in
NvmCtxMgmtRestore
and remove one buffer copy. The current use of large stack allocations makes the code unpredictable in resource constrained environments.In terms of code changes, the only change necessary would be to update
NvmCtxMgmtRestore
to read straight into the context buffers and to remove thememcpy1
calls in the context handlers.The text was updated successfully, but these errors were encountered: