huojianchaoyy opened a new pull request, #22101: URL: https://github.com/apache/nuttx/pull/22101
*Note: Please adhere to [Contributing Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).* ## Summary Rework of the backtrace records in `libs/libc/misc/lib_backtrace.c` (`backtrace_record()` and friends): - **sched: move is_idle_task to nuttx/sched.h**: it was private to `sched/`; `lib_backtrace.c` uses it below to not expand the pool in the idle task. - **libc: support dynamic expansion of backtrace records**: the records lived in a static pool sized by `CONFIG_LIBC_BACKTRACE_BUFFSIZE`. They are now allocated from the heap on the first record and the pool and hash table double when `CONFIG_LIBC_BACKTRACE_LOAD_FACTOR` is reached (in the kernel only when the heap is ready and not from an interrupt or the idle loop). `CONFIG_LIBC_BACKTRACE_DEPTH` enables the feature, `CONFIG_LIBC_BACKTRACE_INIT_SIZE` sets the initial size. `backtrace_record()` returns the record (or NULL) instead of an index. - **driver/devfreq: add procfs for devfreq**: devfreq, the only in-tree user of the API, came in #19741 with the record kept in an `int`, for the index that `backtrace_record()` returned then; it is now a `FAR void *`, as in the original version of this change. - **lib/backtrace:need init lock before use**: the dynamic pool has no init function, so the lock is initialized statically (a zeroed mutex of the user-space libc is not valid). - **libc: fix lib_backtrace warning**, **lib_backtrace:fix warning**: format specifiers in `backtrace_dump()`, an unused-value warning in the user-space unlock. - **libc: backtrace pool added robustness check**: assertions on the free list and the reference count. - **backtrace: fix refs overflow in 16bits**: a record shared by more than 65535 holders wrapped its count and was freed while in use. - **drivers/devfreq: guard backtrace code with CONFIG_LIBC_BACKTRACE_DEPTH**: #19741 brought it with a guard on the removed `CONFIG_LIBC_BACKTRACE_BUFFSIZE`; the guard is now `CONFIG_LIBC_BACKTRACE_DEPTH`, as the subject says. - **libc/backtrace: add static pool mode for malloc-free recording**: with `CONFIG_LIBC_BACKTRACE_LOAD_FACTOR=100` the pool is static and never calls the heap, so it can be used from the allocator itself. - **skip KASAN instrumentation** in `backtrace_find()`, `backtrace_hash()`, `backtrace_alloc()` and `backtrace_remove()`, with word loops instead of the instrumented `memcmp()`/`memcpy()` (kept literal with `no_builtin`): with KASAN each record paid a shadow check per byte, which kept a system that records at a high rate busy long enough to look hung. Follow-up fixes for the commits above: - **libs/libc/backtrace: fix nxstyle errors of the dynamic records**: blank lines after declarations. - **libs/libc/backtrace: fix the indentation in backtrace_expand()**: nxstyle, after the static pool commit. The first commit, **drivers/devfreq: fix the indentation in devfreq_read()**, is an nxstyle fix in a file this PR touches. ## Impact - **Kconfig**: `LIBC_BACKTRACE_BUFFSIZE` is removed; `LIBC_BACKTRACE_DEPTH` (default 0) no longer depends on it and now turns the records on. New: `LIBC_BACKTRACE_INIT_SIZE` (64), `LIBC_BACKTRACE_LOAD_FACTOR` (75). No in-tree defconfig sets these options. - **API** (`include/execinfo.h`): `FAR void *backtrace_record(int skip)`, `void backtrace_remove(FAR void *entry)`, `FAR void **backtrace_get(FAR void *entry, FAR int *size)`. `struct qos_request_s::backtrace` becomes a pointer. - Memory: no static pool unless `LOAD_FACTOR` is 100. ## Testing Host: Ubuntu 22.04 x86_64, GCC 13.4.0, aarch64-none-elf-gcc 13.4.0, Arm GNU Toolchain 13.2.rel1, CMake 3.23 + Ninja, QEMU 10.2.1. Base: master 11c71e16384. `./tools/checkpatch.sh -c -u -m -g HEAD~18..HEAD` passes. A test program (not part of this PR) records the backtrace at 7 recursion levels below 10 different functions, 3 times (210 records), prints `backtrace_dump()`, removes all records and dumps again: | build | distinct records | pool after recording | after removing all | |---|---|---|---| | `sim:nsh`, `LIBC_BACKTRACE_DEPTH=8` | 31 | capacity 64 | used 0 | | `sim:nsh`, `LOAD_FACTOR=100` (static pool) | 31 | capacity 64 | used 0 | | `qemu-armv8a:nsh`, `DEPTH=8`, `MM_KASAN` + `MM_KASAN_INSTRUMENT_ALL` | 51 | capacity 128 (expanded once) | used 0, no KASAN report | Builds: `sim:nsh` with `DEVFREQ_PROCFS_QOS` (the devfreq user of the API), with and without `LIBC_BACKTRACE_DEPTH`; `lm3s6965-ek:qemu-protected` with `SCHED_BACKTRACE` and `DEPTH=8` (make) compiles `lib_backtrace.c` for the kernel and for user space without warnings; the image itself no longer fits the user flash of this board once `SCHED_BACKTRACE` adds the unwind tables. Not tested: the overflow counter of the static pool (the test did not exhaust it), devfreq at run time. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
