Skip to content

os/mm: Add API to set custom PID for kernel module memory allocations#7277

Open
vivek1-j wants to merge 1 commit intoSamsung:masterfrom
vivek1-j:15042026_log_dump_pid
Open

os/mm: Add API to set custom PID for kernel module memory allocations#7277
vivek1-j wants to merge 1 commit intoSamsung:masterfrom
vivek1-j:15042026_log_dump_pid

Conversation

@vivek1-j
Copy link
Copy Markdown
Contributor

Problem:
When CONFIG_DEBUG_MM_HEAPINFO is enabled, the memory allocation node stores the PID of the currently running task to identify the memory owner. This causes incorrect attribution when kernel modules allocate memory on behalf of other tasks.

For example, when the log_dump test application runs, it triggers logging via log_dump_save() which internally calls kmm_malloc(). The allocated memory node incorrectly stores the test application's PID instead of the log_dump kernel thread's PID, causing false alerts in memory debugging.

Solution:
Add a new API heapinfo_set_pid() and corresponding DEBUG_SET_PID() macro that allows kernel modules to explicitly set their PID on allocated memory nodes after the allocation.

Changes:

  1. mm/mm_heap/mm_heapinfo_utils.c: Add heapinfo_set_pid() function
  2. include/tinyara/mm/mm.h: Add DEBUG_SET_PID() macro and declaration
  3. kernel/log_dump/log_dump.c: Use DEBUG_SET_PID() to set correct PID for all kmm_malloc() allocations

Design Decisions:

  • Follow existing pattern of DEBUG_SET_CALLER_ADDR() for consistency
  • Use a no-op macro when CONFIG_DEBUG_MM_HEAPINFO is disabled to avoid any overhead in release builds
  • Store log_dump thread's PID at initialization and reuse it for all subsequent allocations
  • The API is generic and can be used by other kernel modules with similar requirements

Impact:

  • Memory allocated by log_dump module is now correctly attributed to the log_dump kernel thread
  • No impact when CONFIG_DEBUG_MM_HEAPINFO is not enabled
  • API is reusable by other kernel modules that need similar functionality

TC1: Flat build (rtl8730e)
log Dump pid = 13

13 | 4 | 10216 | 12496 | 331808 | log_dump()

0x601755f0 | 4128 | A | 0x e025bf3 | 13 |
0x60176610 | 4128 | A | 0x e025bf3 | 13 |

TC2: Loadable build (rtl8730e)
log dump pid: 15

15 | 4 | 10216 | 12496 | 331808 | kernel | log_dump()

0x603a57e0 | 4128 | A | 0x e023003 | 15 |
0x603a6800 | 4128 | A | 0x e023003 | 15 |
0x603a7820 | 4128 | A | 0x e023003 | 15 |

Signed-Off By: Vivek Jain (vivek1.j@samsung.com)

Problem:
When CONFIG_DEBUG_MM_HEAPINFO is enabled, the memory allocation node
stores the PID of the currently running task to identify the memory
owner. This causes incorrect attribution when kernel modules allocate
memory on behalf of other tasks.

For example, when the log_dump test application runs, it triggers
logging via log_dump_save() which internally calls kmm_malloc(). The
allocated memory node incorrectly stores the test application's PID
instead of the log_dump kernel thread's PID, causing false alerts in
memory debugging.

Solution:
Add a new API heapinfo_set_pid() and corresponding DEBUG_SET_PID() macro
that allows kernel modules to explicitly set their PID on allocated
memory nodes after the allocation.

Changes:
1. mm/mm_heap/mm_heapinfo_utils.c: Add heapinfo_set_pid() function
2. include/tinyara/mm/mm.h: Add DEBUG_SET_PID() macro and declaration
3. kernel/log_dump/log_dump.c: Use DEBUG_SET_PID() to set correct PID
   for all kmm_malloc() allocations

Design Decisions:
- Follow existing pattern of DEBUG_SET_CALLER_ADDR() for consistency
- Use a no-op macro when CONFIG_DEBUG_MM_HEAPINFO is disabled to avoid
  any overhead in release builds
- Store log_dump thread's PID at initialization and reuse it for all
  subsequent allocations
- The API is generic and can be used by other kernel modules with
  similar requirements

Impact:
- Memory allocated by log_dump module is now correctly attributed to
  the log_dump kernel thread
- No impact when CONFIG_DEBUG_MM_HEAPINFO is not enabled
- API is reusable by other kernel modules that need similar functionality

TC1: Flat build (rtl8730e)
log Dump pid = 13

13 |     4 | 10216 |     12496 |    331808 | log_dump()

0x601755f0 |     4128 |   A    | 0x e025bf3 |  13   |
0x60176610 |     4128 |   A    | 0x e025bf3 |  13   |

TC2: Loadable build (rtl8730e)
log dump pid: 15

15 |     4 | 10216 |     12496 |    331808 |   kernel | log_dump()

0x603a57e0 |     4128 |   A    | 0x e023003 |  15   |
0x603a6800 |     4128 |   A    | 0x e023003 |  15   |
0x603a7820 |     4128 |   A    | 0x e023003 |  15   |

Signed-Off By: Vivek Jain (vivek1.j@samsung.com)
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.

1 participant