jingfei195887 opened a new pull request, #22538: URL: https://github.com/apache/nuttx/pull/22538
## Summary Allow `/proc/pressure/memory` to be read in small chunks. A one-byte read currently fails with `ENOMEM`; the same rejection also prevents small reads from reporting EOF. Remove the whole-line capacity check and let the existing `procfs_memcpy()` apply the buffer limit and file position. ## Impact - Functionality: YES. Partial reads and EOF follow the normal procfs read contract. - API and compatibility: no signature or output-format change; small-buffer reads now return data or EOF instead of `ENOMEM`. - Build system, configuration, hardware and documentation changes: NO. - Memory pressure notification behavior and allocation policy: unchanged. ## Testing I confirm that the changed read behavior is verified on the local setup. Build host: Linux x86_64; gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0. Runtime targets: 64-bit and 32-bit `sim:nsh`. Baseline `f9d3eb13c3f4`, branch `77248b50f2d0`. | Configuration | Checks | Failures | |---|---:|---:| | Baseline 64-bit | 211 | 41 | | Branch 64-bit | 822 | 0 | | Baseline 32-bit | 211 | 41 | | Branch 32-bit | 822 | 0 | | Branch: 512KiB FS heap, FDCHECK, large files | 822 | 0 | | Branch: TLSF allocator | 1052 | 0 | The same regression source covers full reads, 1/3/7/16/127-byte buffers, position advancement, tail reads, EOF and beyond EOF, zero-length controls, readv, dup and internal file APIs. More assertions execute after short reads succeed; TLSF produces a different line length. Baseline/branch configurations are byte-identical for each paired run. The quiet single-threaded fixture compares chunk reconstruction without intervening allocation; it does not establish a snapshot guarantee during concurrent heap changes. The kernel build (`qemu-armv8a:knsh`) passes with the pressure feature enabled. The protected build (`qemu-armv7r:pnsh`) fails to link on **both baseline and branch** with identical `mm_notify_pressure` references from the user allocator. The filesystem library, including `pressure_read`, compiles on both. This is a pre-existing whole-image build limitation, not a passing protected build; the patch does not change allocator notification wiring. Make/CMake/OSTest validation passes for the branch. No physical hardware, SMP runtime, or protected/kernel runtime validation was performed. Small `sim:nsh` configuration with memory pressure enabled (the function is linked in both images): | Image | text | data | bss | |---|---:|---:|---:| | baseline | 554227 | 2792 | 13088 | | branch | 554211 | 2792 | 13088 | | delta | -16 | +0 | +0 | <details> <summary>Baseline 64-bit: actual build/runner status</summary> ```text rev f9d3eb13c3f arch/arm/pp502x: Finish start-up in the common no-MMU entry. build rc=0 run rc=0 log=/home/mi/test/nuttx_apache/fs-upstream/pr-pressure-read/baseline/run.log ``` </details> <details> <summary>Baseline 64-bit: actual runtime log</summary> ```text nsh> pressurereadcheck CASE api=user chunk=1 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=1 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=33 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 CASE api=user chunk=3 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=3 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=33 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 CASE api=user chunk=7 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=7 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=33 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 CASE api=user chunk=16 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=16 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=33 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 CASE api=user chunk=127 FAIL small read at EOF succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=127 FAIL small read at EOF succeeds errno=12 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 FAIL readv small segments succeed errno=12 FAIL read through original descriptor errno=12 FAIL read through duplicated descriptor errno=12 PRESSURE READ RESULT checks=211 failures=41 nsh> echo status=$? status=1 nsh> poweroff ``` </details> <details> <summary>Branch 64-bit: actual build/runner status</summary> ```text rev 77248b50f2d fs/procfs: support partial reads of memory pressure build rc=0 run rc=0 log=/home/mi/test/nuttx_apache/fs-upstream/pr-pressure-read/branch/run.log ``` </details> <details> <summary>Branch 64-bit: actual runtime log</summary> ```text nsh> pressurereadcheck CASE api=user chunk=1 CASE api=file chunk=1 CASE api=user chunk=3 CASE api=file chunk=3 CASE api=user chunk=7 CASE api=file chunk=7 CASE api=user chunk=16 CASE api=file chunk=16 CASE api=user chunk=127 CASE api=file chunk=127 PRESSURE READ RESULT checks=822 failures=0 nsh> echo status=$? status=0 nsh> poweroff ``` </details> <details> <summary>Baseline 32-bit: actual build/runner status</summary> ```text rev f9d3eb13c3f arch/arm/pp502x: Finish start-up in the common no-MMU entry. build rc=0 run rc=0 log=/home/mi/test/nuttx_apache/fs-upstream/pr-pressure-read/baseline32/run.log ``` </details> <details> <summary>Baseline 32-bit: actual runtime log</summary> ```text nsh> pressurereadcheck CASE api=user chunk=1 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=1 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=33 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 CASE api=user chunk=3 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=3 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=33 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 CASE api=user chunk=7 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=7 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=33 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 CASE api=user chunk=16 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=16 FAIL small read at EOF succeeds errno=12 FAIL chunk read succeeds errno=33 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 CASE api=user chunk=127 FAIL small read at EOF succeeds errno=12 FAIL tail short read succeeds errno=12 FAIL small read beyond EOF succeeds errno=12 CASE api=file chunk=127 FAIL small read at EOF succeeds errno=12 FAIL tail short read succeeds errno=33 FAIL small read beyond EOF succeeds errno=33 FAIL readv small segments succeed errno=12 FAIL read through original descriptor errno=12 FAIL read through duplicated descriptor errno=12 PRESSURE READ RESULT checks=211 failures=41 nsh> echo status=$? status=1 nsh> poweroff ``` </details> <details> <summary>Branch 32-bit: actual build/runner status</summary> ```text rev 77248b50f2d fs/procfs: support partial reads of memory pressure build rc=0 run rc=0 log=/home/mi/test/nuttx_apache/fs-upstream/pr-pressure-read/branch32/run.log ``` </details> <details> <summary>Branch 32-bit: actual runtime log</summary> ```text nsh> pressurereadcheck CASE api=user chunk=1 CASE api=file chunk=1 CASE api=user chunk=3 CASE api=file chunk=3 CASE api=user chunk=7 CASE api=file chunk=7 CASE api=user chunk=16 CASE api=file chunk=16 CASE api=user chunk=127 CASE api=file chunk=127 PRESSURE READ RESULT checks=822 failures=0 nsh> echo status=$? status=0 nsh> poweroff ``` </details> <details> <summary>Branch: 512KiB FS heap, FDCHECK, large files: actual build/runner status</summary> ```text rev 77248b50f2d fs/procfs: support partial reads of memory pressure build rc=0 run rc=0 log=/home/mi/test/nuttx_apache/fs-upstream/pr-pressure-read/branch-heap/run.log ``` </details> <details> <summary>Branch: 512KiB FS heap, FDCHECK, large files: actual runtime log</summary> ```text nsh> pressurereadcheck CASE api=user chunk=1 CASE api=file chunk=1 CASE api=user chunk=3 CASE api=file chunk=3 CASE api=user chunk=7 CASE api=file chunk=7 CASE api=user chunk=16 CASE api=file chunk=16 CASE api=user chunk=127 CASE api=file chunk=127 PRESSURE READ RESULT checks=822 failures=0 nsh> echo status=$? status=0 nsh> poweroff ``` </details> <details> <summary>Branch: TLSF allocator: actual build/runner status</summary> ```text rev 77248b50f2d fs/procfs: support partial reads of memory pressure build rc=0 run rc=0 log=/home/mi/test/nuttx_apache/fs-upstream/pr-pressure-read/branch-tlsf/run.log ``` </details> <details> <summary>Branch: TLSF allocator: actual runtime log</summary> ```text nsh> pressurereadcheck CASE api=user chunk=1 CASE api=file chunk=1 CASE api=user chunk=3 CASE api=file chunk=3 CASE api=user chunk=7 CASE api=file chunk=7 CASE api=user chunk=16 CASE api=file chunk=16 CASE api=user chunk=127 CASE api=file chunk=127 PRESSURE READ RESULT checks=1052 failures=0 nsh> echo status=$? status=0 nsh> poweroff ``` </details> <details> <summary>Formal local build/runtime validation</summary> ```text target : nuttx branch : fs/pressure-partial-read head : 77248b50f2d053820f6b4d7081182f8e00753a38 subject : fs/procfs: support partial reads of memory pressure commits : 1 base : f9d3eb13c3 (merge-base with upstream/master) behind : 0 commits of upstream/master nuttx : 77248b50f2 apps : 0a37af9866 options : CONFIG_FS_PROCFS CONFIG_FS_PROCFS_INCLUDE_PRESSURE CONFIG_DEBUG_ASSERTIONS host : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0 checkpatch: rc=0 signer : jingfei <[email protected]> commits missing your Signed-off-by: 0 make build: rc=0 warnings in changed files: 0 ostest: Exiting with status 0 cmake reconfigure: rc=0 cmake build: rc=0 worktree tracked changes after: 0 RESULT: PASS ``` </details> <details> <summary>Kernel build result</summary> ```text qemu-armv8a:knsh build PASS ``` </details> <details> <summary>protected-baseline: whole-image link failure</summary> ```text qemu-armv7r:pnsh build FAIL rc=2 /home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/umm_heap/umm_malloc.c:73:(.text.malloc+0x34): undefined reference to `mm_notify_pressure' /home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/umm_heap/umm_realloc.c:120:(.text.realloc+0x38): undefined reference to `mm_notify_pressure' /home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/umm_heap/umm_zalloc.c:80:(.text.zalloc+0x34): undefined reference to `mm_notify_pressure' ``` </details> <details> <summary>protected-branch: whole-image link failure</summary> ```text qemu-armv7r:pnsh build FAIL rc=2 /home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/umm_heap/umm_malloc.c:73:(.text.malloc+0x34): undefined reference to `mm_notify_pressure' /home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/umm_heap/umm_realloc.c:120:(.text.realloc+0x38): undefined reference to `mm_notify_pressure' /home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/umm_heap/umm_zalloc.c:80:(.text.zalloc+0x34): undefined reference to `mm_notify_pressure' ``` </details> The local regression application is included below. Install its four files under `apps/examples/pressurereadcheck/`, enable `CONFIG_FS_PROCFS`, `CONFIG_FS_PROCFS_INCLUDE_PRESSURE` and `CONFIG_EXAMPLES_PRESSUREREADCHECK` in `sim:nsh`, build, and run `pressurereadcheck`. It uses the mounted procfs handler rather than replacing it with a test implementation. <details> <summary>Regression source: Kconfig</summary> ```text config EXAMPLES_PRESSUREREADCHECK tristate "Memory pressure read regression" default n depends on FS_PROCFS_INCLUDE_PRESSURE ``` </details> <details> <summary>Regression source: Makefile</summary> ```text include $(APPDIR)/Make.defs PROGNAME = pressurereadcheck PRIORITY = SCHED_PRIORITY_DEFAULT STACKSIZE = 8192 MODULE = $(CONFIG_EXAMPLES_PRESSUREREADCHECK) MAINSRC = pressurereadcheck_main.c include $(APPDIR)/Application.mk ``` </details> <details> <summary>Regression source: Make.defs</summary> ```text ifneq ($(CONFIG_EXAMPLES_PRESSUREREADCHECK),) CONFIGURED_APPS += $(APPDIR)/examples/pressurereadcheck endif ``` </details> <details> <summary>Regression source: pressurereadcheck_main.c</summary> ```c /**************************************************************************** * apps/examples/pressurereadcheck/pressurereadcheck_main.c * * SPDX-License-Identifier: Apache-2.0 ****************************************************************************/ /**************************************************************************** * Included Files ****************************************************************************/ #include <nuttx/config.h> #include <sys/mount.h> #include <sys/statfs.h> #include <sys/uio.h> #include <errno.h> #include <fcntl.h> #include <stdbool.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <nuttx/fs/fs.h> /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ #define PRESSURE_PATH "/proc/pressure/memory" /**************************************************************************** * Private Data ****************************************************************************/ static unsigned int g_checks; static unsigned int g_failures; /**************************************************************************** * Private Functions ****************************************************************************/ static void check(bool ok, FAR const char *label) { g_checks++; if (!ok) { g_failures++; printf("FAIL %s errno=%d\n", label, errno); } } static ssize_t read_data(FAR struct file *filep, int fd, FAR char *buf, size_t len) { return filep == NULL ? read(fd, buf, len) : file_read(filep, buf, len); } static off_t seek_data(FAR struct file *filep, int fd, off_t pos) { return filep == NULL ? lseek(fd, pos, SEEK_SET) : file_seek(filep, pos, SEEK_SET); } static off_t position(FAR struct file *filep, int fd) { return filep == NULL ? lseek(fd, 0, SEEK_CUR) : filep->f_pos; } static void read_case(bool internal, size_t chunk) { struct file file; FAR struct file *filep = internal ? &file : NULL; char reference[128]; char assembled[128]; char piece[130]; size_t remaining; size_t largest; size_t used; ssize_t length; ssize_t ret; int fd = -1; int status; printf("CASE api=%s chunk=%zu\n", internal ? "file" : "user", chunk); if (internal) { status = file_open(&file, PRESSURE_PATH, O_RDONLY); } else { fd = open(PRESSURE_PATH, O_RDONLY); status = fd >= 0 ? 0 : -1; } check(status == 0, "open pressure file"); if (status != 0) { return; } memset(reference, 0, sizeof(reference)); length = read_data(filep, fd, reference, sizeof(reference) - 1); check(length > 10 && length < sizeof(reference) - 1, "full read succeeds"); if (length <= 10 || length >= sizeof(reference) - 1) { goto out; } check(sscanf(reference, "remaining %zu, largest:%zu\n", &remaining, &largest) == 2, "full read format"); check(position(filep, fd) == length, "full read advances offset"); memset(piece, 0x5a, sizeof(piece)); ret = read_data(filep, fd, piece + 1, 1); check(ret == 0, "small read at EOF succeeds"); check(position(filep, fd) == length, "EOF preserves offset"); check(piece[1] == 0x5a, "EOF leaves buffer untouched"); check(seek_data(filep, fd, 0) == 0, "rewind pressure file"); check(read_data(filep, fd, piece + 1, 0) == 0, "zero length succeeds"); check(position(filep, fd) == 0, "zero length preserves offset"); check(piece[1] == 0x5a, "zero length leaves buffer untouched"); used = 0; memset(assembled, 0, sizeof(assembled)); while (used < sizeof(assembled) - 1) { memset(piece, 0x5a, sizeof(piece)); errno = EDOM; ret = read_data(filep, fd, piece + 1, chunk); check(ret >= 0 && ret <= chunk, "chunk read succeeds"); if (ret < 0 || ret > chunk) { break; } check(errno == EDOM, "successful read preserves errno"); check(piece[0] == 0x5a && piece[chunk + 1] == 0x5a, "chunk buffer guards preserved"); check(used + ret < sizeof(assembled), "assembled line fits"); if (used + ret >= sizeof(assembled)) { break; } memcpy(assembled + used, piece + 1, ret); used += ret; check(position(filep, fd) == used, "chunk offset advances exactly"); if (ret == 0) { break; } } if (ret >= 0) { /* The single-threaded fixture does not allocate between these reads. */ check(used == length && memcmp(assembled, reference, length) == 0, "chunks reconstruct the line"); check(read_data(filep, fd, piece + 1, chunk) == 0, "repeated EOF succeeds"); } check(seek_data(filep, fd, length - 2) == length - 2, "seek to line suffix"); memset(piece, 0x5a, sizeof(piece)); ret = read_data(filep, fd, piece + 1, 7); check(ret == 2, "tail short read succeeds"); if (ret == 2) { check(memcmp(piece + 1, reference + length - 2, 2) == 0, "tail content matches"); check(piece[3] == 0x5a, "short tail leaves rest untouched"); } check(seek_data(filep, fd, 127) == 127, "seek beyond EOF"); check(read_data(filep, fd, piece + 1, 1) == 0, "small read beyond EOF succeeds"); check(position(filep, fd) == 127, "beyond EOF preserves offset"); out: status = filep == NULL ? close(fd) : file_close(filep); check(status == 0, "close pressure file"); } static void vector_case(void) { char first[3]; char second[6]; struct iovec iov[2]; ssize_t ret; int fd; int copy; fd = open(PRESSURE_PATH, O_RDONLY); check(fd >= 0, "open for readv and dup"); if (fd < 0) { return; } iov[0].iov_base = first; iov[0].iov_len = sizeof(first); iov[1].iov_base = second; iov[1].iov_len = sizeof(second); ret = readv(fd, iov, 2); check(ret == 9, "readv small segments succeed"); if (ret == 9) { check(memcmp(first, "rem", 3) == 0, "readv first segment"); check(memcmp(second, "aining", 6) == 0, "readv second segment"); } check(lseek(fd, 0, SEEK_SET) == 0, "rewind before dup"); copy = dup(fd); check(copy >= 0, "dup pressure descriptor"); if (copy >= 0) { check(read(fd, first, sizeof(first)) == sizeof(first), "read through original descriptor"); ret = read(copy, second, sizeof(second)); check(ret == sizeof(second), "read through duplicated descriptor"); if (ret == sizeof(second)) { check(memcmp(second, "aining", 6) == 0, "dup shares offset"); } check(close(copy) == 0, "close duplicate"); } check(close(fd) == 0, "close vector descriptor"); } /**************************************************************************** * Public Functions ****************************************************************************/ int main(int argc, FAR char *argv[]) { static const size_t chunks[] = { 1, 3, 7, 16, 127 }; unsigned int i; struct statfs info; bool mounted = false; int ret; ret = statfs(PRESSURE_PATH, &info); if (ret < 0 || info.f_type != PROCFS_MAGIC) { ret = mount(NULL, "/proc", "procfs", 0, NULL); mounted = ret == 0; check(mounted, "mount test procfs"); } check(statfs(PRESSURE_PATH, &info) == 0 && info.f_type == PROCFS_MAGIC, "procfs mount available"); for (i = 0; i < sizeof(chunks) / sizeof(chunks[0]); i++) { read_case(false, chunks[i]); read_case(true, chunks[i]); } vector_case(); if (mounted) { check(umount("/proc") == 0, "unmount test procfs"); } printf("PRESSURE READ RESULT checks=%u failures=%u\n", g_checks, g_failures); return g_failures != 0; } ``` </details> ## PR verification Self-Check - [x] This PR introduces only one functional change. - [x] I have updated all required description fields above. - [x] My PR adheres to Contributing Guidelines and Documentation (git commit title and message, coding standard, etc). - [ ] My PR is still work in progress (not ready for review). - [x] My PR is ready for review and can be safely merged into a codebase. -- 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]
