jingfei195887 opened a new pull request, #22354:
URL: https://github.com/apache/nuttx/pull/22354
## Summary
Use `lib_get_tempbuffer()` for the pathname components in NFS create,
remove, mkdir, rmdir and rename. This reduces their stack frames when the
existing shared temporary-buffer pool is enabled. Release acquired buffers
on every exit, including a failed second allocation in rename.
## Impact
- NFS implementation only; no public API, Kconfig or build-system changes.
- The existing `CONFIG_LIBC_TEMPBUFFER` policy applies. With the pool
enabled,
allocation failure returns `ENOMEM`. Rename holds two temporary buffers;
VFS callers and other tasks can hold additional buffers. With
`CONFIG_LIBC_TEMPBUFFER_MALLOC` disabled, provision enough shared slots and
buffer capacity for the complete call chain, or these operations can fail.
- With `CONFIG_LIBC_TEMPBUFFER` disabled the API uses `alloca()`; this case
has
no stack-saving claim. GCC reports dynamic frames in this configuration.
- No dependency on another PR. No new global NFS buffers.
## Testing
Tested locally on Linux x86_64 with `gcc (Ubuntu 13.1.0-8ubuntu1~22.04)
13.1.0`.
Baseline: `4f8706c4b351`. Tested code: `74e7c338d4fb`.
The local sim:nsh harness compiles the actual `nfs_vfsops.c` and uses the
real
temporary allocator. Directory lookup and RPC exchange are stubbed. It checks
all five operations, maximum-length component encoding, lookup/RPC/lock
errors,
missing CREATE handles, first/second allocation failure, pool exhaustion,
malloc fallback, balanced releases, lock release and pool reuse.
112 checks pass across pool+malloc, pool without malloc, and pool disabled.
The baseline migration checks fail as expected because stack arrays never
call
the temporary allocator; they do not indicate that normal baseline NFS calls
fail.
The local harness is attached below because it relies on private NFS
functions
and RPC stubs, rather than a portable public-API regression test.
Testing logs before change:
```text
FAIL op=4 second-allocation-failure ret=0 expected=-12 attempts=0/2 alloc=0
free=0 live=0 lock=1/1 encoding=0
NFSTEMP_RESULT cases=42 failures=42
```
Testing logs after change:
```text
PASS op=4 second-allocation-failure ret=-12 attempts=2 peak=1 alloc=1 free=1
PASS op=4 exhausted-pool-malloc ret=0 attempts=2 peak=2 alloc=2 free=2
NFSTEMP_RESULT cases=42 failures=0
PASS op=4 one-slot-left-second-allocation-failure ret=-12 attempts=2 peak=1
alloc=1 free=1
PASS op=4 two-slots-reused ret=0 attempts=2 peak=2 alloc=2 free=2
NFSTEMP_RESULT cases=44 failures=0
NFSTEMP_RESULT cases=26 failures=0
```
Small-build comparison: identical sim:nsh configurations with NFS,
`DEFAULT_SMALL=y`, the pool enabled, `NAME_MAX=32`, full optimization and
coverage disabled. The local harness is disabled. GCC `-fstack-usage`
reports:
| Function | Before | After |
|---|---:|---:|
| `nfs_open` | 448 B | 416 B |
| `nfs_remove` | 272 B | 240 B |
| `nfs_mkdir` | 304 B | 256 B |
| `nfs_rmdir` | 272 B | 240 B |
| `nfs_rename` | 416 B | 336 B |
`nfs_filecreate` is inlined into `nfs_open` in this build. These are compiler
frame sizes, not measurements of the maximum stack used by complete NFS
calls.
```text
Before:
text data bss dec hex filename
633773 4048 24064 661885 a197d nuttx
After:
text data bss dec hex filename
634061 4048 24064 662173 a1a9d nuttx
```
The linked image gains 288 text bytes; data and bss are unchanged.
NFS-enabled protected and kernel builds also pass (build only). These board
presets use `NETDEV_LATEINIT=y` because no network driver is enabled:
```text
qemu-armv7r:pnsh: NFS-enabled build PASS
qemu-armv8a:knsh: NFS-enabled build PASS
arm-none-eabi-gcc (Arm GNU Toolchain 13.2.rel1 (Build arm-13.7)) 13.2.1
20231009
aarch64-none-elf-gcc (GCC) 13.4.0
```
Make/CMake, ostest and the final HEAD gate:
```text
target : nuttx
branch : fs/nfs-tempbuffer
head : 74e7c338d4fb8eea83c3394c668d308176db02dc
subject : fs/nfs:use lib_get_tempbuffer for pathname
commits : 1
base : 4f8706c4b3 (merge-base with upstream/master)
behind : 19 commits of upstream/master
nuttx : 74e7c338d4
apps : f0dc662c01
options : CONFIG_ALLOW_BSD_COMPONENTS CONFIG_NFS CONFIG_NET CONFIG_NET_IPv4
CONFIG_NET_TCP CONFIG_NET_UDP CONFIG_NET_SOCKOPTS CONFIG_LIBC_TEMPBUFFER
CONFIG_LIBC_MAX_TEMPBUFFER=2 CONFIG_LIBC_TEMPBUFFER_MALLOC CONFIG_DEBUG_FULLOPT
CONFIG_COVERAGE_NONE
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
```
No real hardware, live NFS server, protected/kernel runtime, SMP stress,
network throughput or end-to-end stack high-water testing was performed.
<details>
<summary>Local sim harness and reproduction</summary>
Create these files under `apps/examples/nfstempcheck/`. Enable
`CONFIG_EXAMPLES_NFSTEMPCHECK` and NFS (including its BSD-component license
option and socket options), build sim:nsh and run `nfstempcheck`.
The directory layout is `nuttx/` next to `apps/`. Repeat with
temporary-buffer
malloc enabled, disabled, and with the temporary-buffer pool disabled.
The no-malloc test uses `CONFIG_LIBC_MAX_TEMPBUFFER=2`.
`Kconfig`
```text
config EXAMPLES_NFSTEMPCHECK
tristate "Local NFS temporary buffer checks"
default n
depends on NFS
```
`Make.defs`
```text
ifneq ($(CONFIG_EXAMPLES_NFSTEMPCHECK),)
CONFIGURED_APPS += $(APPDIR)/examples/nfstempcheck
endif
```
`Makefile`
```text
include $(APPDIR)/Make.defs
PROGNAME = nfstempcheck
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 16384
MODULE = $(CONFIG_EXAMPLES_NFSTEMPCHECK)
MAINSRC = nfstempcheck_main.c
CFLAGS += -I$(TOPDIR)/fs
include $(APPDIR)/Application.mk
```
`nfstempcheck_main.c`
```c
#include <nuttx/config.h>
#include <nuttx/lib/lib.h>
#include <nuttx/mutex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
static int g_attempts;
static int g_allocations;
static int g_releases;
static int g_live;
static int g_peak;
static int g_fail_allocate;
static int g_lock_error;
static int g_locks;
static int g_unlocks;
static int g_find_calls;
static int g_find_error_at;
static int g_rpc_error;
static int g_rpc_calls;
static int g_missing_handle;
static int g_encoding_error;
static int g_failures;
static int g_cases;
static const char *g_old_path = "parent/oldname";
static const char *g_new_path = "other/newname";
#ifdef CONFIG_LIBC_TEMPBUFFER
static void *nfstest_allocate(size_t size)
{
void *buffer;
g_attempts++;
if (g_attempts == g_fail_allocate)
{
return NULL;
}
buffer = lib_get_tempbuffer(size);
if (buffer != NULL)
{
g_allocations++;
g_live++;
if (g_live > g_peak)
{
g_peak = g_live;
}
}
return buffer;
}
static void nfstest_release(void *buffer)
{
g_releases++;
g_live--;
lib_put_tempbuffer(buffer);
}
#define lib_get_tempbuffer nfstest_allocate
#define lib_put_tempbuffer nfstest_release
#endif
static int nfstest_lock(mutex_t *mutex)
{
if (g_lock_error != 0)
{
return g_lock_error;
}
g_locks++;
return nxmutex_lock(mutex);
}
static int nfstest_unlock(mutex_t *mutex)
{
g_unlocks++;
return nxmutex_unlock(mutex);
}
#define nxmutex_lock nfstest_lock
#define nxmutex_unlock nfstest_unlock
#define nfs_finddir nfstest_finddir
#define nfs_request nfstest_request
#define g_nfs_operations g_nfstest_operations
#include "../../../nuttx/fs/nfs/nfs_vfsops.c"
#undef g_nfs_operations
#undef nfs_request
#undef nfs_finddir
#undef nxmutex_lock
#undef nxmutex_unlock
#ifdef CONFIG_LIBC_TEMPBUFFER
#undef lib_get_tempbuffer
#undef lib_put_tempbuffer
#endif
int nfstest_finddir(struct nfsmount *mount, const char *path,
struct file_handle *handle,
struct nfs_fattr *attributes, char *name)
{
const char *component = strrchr(path, '/');
g_find_calls++;
if (g_find_calls == g_find_error_at)
{
return -ENOENT;
}
memset(handle, 0, sizeof(*handle));
memset(attributes, 0, sizeof(*attributes));
handle->length = 4;
memset(&handle->handle, 0x41, 4);
strcpy(name, component == NULL ? path : component + 1);
return 0;
}
static uint32_t *nfstest_decode(uint32_t *words, const char *path)
{
const char *component = strrchr(path, '/');
size_t length;
component = component == NULL ? path : component + 1;
length = strlen(component);
if (fxdr_unsigned(uint32_t, *words++) != 4)
{
g_encoding_error++;
}
if (memcmp(words, "AAAA", 4) != 0)
{
g_encoding_error++;
}
words++;
if (fxdr_unsigned(uint32_t, *words++) != length ||
memcmp(words, component, length) != 0)
{
g_encoding_error++;
}
return words + uint32_increment(length);
}
int nfstest_request(struct nfsmount *mount, int procedure,
void *request, size_t request_size,
void *response, size_t response_size)
{
uint32_t *words;
g_rpc_calls++;
switch (procedure)
{
case NFSPROC_CREATE:
words = (uint32_t *)&((struct rpc_call_create *)request)->create;
break;
case NFSPROC_REMOVE:
words = (uint32_t *)&((struct rpc_call_remove *)request)->remove;
break;
case NFSPROC_MKDIR:
words = (uint32_t *)&((struct rpc_call_mkdir *)request)->mkdir;
break;
case NFSPROC_RMDIR:
words = (uint32_t *)&((struct rpc_call_rmdir *)request)->rmdir;
break;
case NFSPROC_RENAME:
words = (uint32_t *)&((struct rpc_call_rename *)request)->rename;
break;
default:
return -ENOSYS;
}
words = nfstest_decode(words, g_old_path);
if (procedure == NFSPROC_RENAME)
{
nfstest_decode(words, g_new_path);
}
if (g_rpc_error != 0)
{
return g_rpc_error;
}
if (procedure == NFSPROC_CREATE)
{
words = (uint32_t *)&((struct rpc_reply_create *)response)->create;
memset(words, 0, 32);
*words++ = g_missing_handle ? 0 : txdr_unsigned(1);
*words++ = txdr_unsigned(4);
*words++ = 0x41414141;
*words = 0;
}
return 0;
}
static void nfstest_reset(void)
{
g_attempts = 0;
g_allocations = 0;
g_releases = 0;
g_live = 0;
g_peak = 0;
g_fail_allocate = 0;
g_lock_error = 0;
g_locks = 0;
g_unlocks = 0;
g_find_calls = 0;
g_find_error_at = 0;
g_rpc_error = 0;
g_rpc_calls = 0;
g_missing_handle = 0;
g_encoding_error = 0;
}
static void nfstest_case(struct inode *inode, int operation,
const char *name, int expected, int attempts)
{
struct nfsnode node;
int result;
int lock_result;
memset(&node, 0, sizeof(node));
if (operation == 0)
{
result = nfs_filecreate(inode->i_private, &node, g_old_path, 0644);
}
else if (operation == 1)
{
result = nfs_remove(inode, g_old_path);
}
else if (operation == 2)
{
result = nfs_mkdir(inode, g_old_path, 0755);
}
else if (operation == 3)
{
result = nfs_rmdir(inode, g_old_path);
}
else
{
result = nfs_rename(inode, g_old_path, g_new_path);
}
lock_result = nxmutex_trylock(&((struct nfsmount
*)inode->i_private)->nm_lock);
if (lock_result == 0)
{
nxmutex_unlock(&((struct nfsmount *)inode->i_private)->nm_lock);
}
g_cases++;
if (result != expected || g_allocations != g_releases || g_live != 0 ||
g_locks != g_unlocks || lock_result != 0 || g_encoding_error != 0
#ifdef CONFIG_LIBC_TEMPBUFFER
|| g_attempts != attempts
#endif
)
{
printf("FAIL op=%d %s ret=%d expected=%d attempts=%d/%d "
"alloc=%d free=%d live=%d lock=%d/%d encoding=%d\n",
operation, name, result, expected, g_attempts, attempts,
g_allocations, g_releases, g_live, g_locks, g_unlocks,
g_encoding_error);
g_failures++;
}
else
{
printf("PASS op=%d %s ret=%d attempts=%d peak=%d alloc=%d free=%d\n",
operation, name, result, g_attempts, g_peak,
g_allocations, g_releases);
}
}
int nfstempcheck_main(int argc, char **argv)
{
struct nfsmount *mount;
struct inode inode;
char long_path[NAME_MAX + 1];
const char *saved_path;
int operation;
int attempts;
#ifdef CONFIG_LIBC_TEMPBUFFER
void *held[CONFIG_LIBC_MAX_TEMPBUFFER];
int slot;
#endif
mount = calloc(1, SIZEOF_nfsmount(4096));
if (mount == NULL)
{
return 1;
}
memset(&inode, 0, sizeof(inode));
inode.i_private = mount;
mount->nm_buflen = 4096;
nxmutex_init(&mount->nm_lock);
memset(long_path, 'z', NAME_MAX);
long_path[NAME_MAX] = '\0';
g_cases = 0;
g_failures = 0;
for (operation = 0; operation < 5; operation++)
{
attempts = operation == 4 ? 2 : 1;
nfstest_reset();
nfstest_case(&inode, operation, "success", 0, attempts);
nfstest_reset();
saved_path = g_old_path;
g_old_path = long_path;
nfstest_case(&inode, operation, "NAME_MAX", 0, attempts);
g_old_path = saved_path;
nfstest_reset();
g_find_error_at = 1;
nfstest_case(&inode, operation, "lookup-failure", -ENOENT, attempts);
nfstest_reset();
g_rpc_error = -EACCES;
nfstest_case(&inode, operation, "rpc-failure", -EACCES, attempts);
if (operation != 0)
{
nfstest_reset();
g_lock_error = -EINTR;
nfstest_case(&inode, operation, "lock-failure", -EINTR, attempts);
}
#ifdef CONFIG_LIBC_TEMPBUFFER
nfstest_reset();
g_fail_allocate = 1;
nfstest_case(&inode, operation, "first-allocation-failure", -ENOMEM,
1);
if (operation == 4)
{
nfstest_reset();
g_fail_allocate = 2;
nfstest_case(&inode, operation, "second-allocation-failure",
-ENOMEM, 2);
}
for (slot = 0; slot < CONFIG_LIBC_MAX_TEMPBUFFER; slot++)
{
held[slot] = lib_get_tempbuffer(NAME_MAX + 1);
if (held[slot] == NULL)
{
g_failures++;
}
}
nfstest_reset();
#ifdef CONFIG_LIBC_TEMPBUFFER_MALLOC
nfstest_case(&inode, operation, "exhausted-pool-malloc", 0, attempts);
#else
nfstest_case(&inode, operation, "exhausted-pool-no-malloc", -ENOMEM,
1);
#endif
for (slot = 0; slot < CONFIG_LIBC_MAX_TEMPBUFFER; slot++)
{
if (held[slot] != NULL)
{
lib_put_tempbuffer(held[slot]);
}
}
nfstest_reset();
nfstest_case(&inode, operation, "pool-reused", 0, attempts);
#endif
}
nfstest_reset();
g_find_error_at = 2;
nfstest_case(&inode, 4, "second-lookup-failure", -ENOENT, 2);
nfstest_reset();
g_missing_handle = 1;
nfstest_case(&inode, 0, "missing-create-handle", -EINVAL, 1);
#if defined(CONFIG_LIBC_TEMPBUFFER) &&
!defined(CONFIG_LIBC_TEMPBUFFER_MALLOC)
held[0] = lib_get_tempbuffer(NAME_MAX + 1);
nfstest_reset();
nfstest_case(&inode, 4, "one-slot-left-second-allocation-failure",
-ENOMEM, 2);
lib_put_tempbuffer(held[0]);
nfstest_reset();
nfstest_case(&inode, 4, "two-slots-reused", 0, 2);
#endif
nxmutex_destroy(&mount->nm_lock);
free(mount);
printf("NFSTEMP_RESULT cases=%d failures=%d\n", g_cases, g_failures);
return g_failures == 0 ? 0 : 1;
}
```
</details>
## PR verification Self-Check
- [x] This PR introduces only one functional change.
- [x] I have updated all required description fields above.
- [x] The code and commit message pass NuttX checkpatch.
- [ ] 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]