jingfei195887 opened a new pull request, #22418:
URL: https://github.com/apache/nuttx/pull/22418

   ## Summary
   
   Keep the permanent pseudoFS root inode in static storage instead of 
allocating it from `fs_heap` at startup. `inode_unlink()` already rejects 
removing the root. A shared private initializer preserves the inode number, 
tree reference, mode, credentials and timestamps for both the root and ordinary 
heap-allocated inodes.
   
   This removes one startup allocation and its unchecked failure path. It does 
not add a pool or change ordinary inode allocation.
   
   ## Impact
   
   - Functionality: root initialization no longer depends on a successful heap 
allocation. Path lookup, directory operations and root metadata retain their 
existing behavior.
   - Public API, structure layout, configuration, build process and hardware 
changes: NO.
   - Memory: one inode moves from runtime heap storage to BSS; this is not a 
claim that total RAM use falls. Allocation of the remaining inodes is unchanged.
   - User adaptation/documentation: NO new interface or option. The existing 
pseudoFS and chroot descriptions remain applicable.
   - Permissions and synchronization: NO change.
   
   ## Testing
   
   I confirm that changes are verified on the local setup and work as intended.
   
   Base `695d40366dabf7f239c297fdbe57b2473c90cab3`; head 
`03bcfa319ebcce71c087daa30d74f2014b0e4313`.
   Build host: Linux 6.8.0-138-generic x86_64; gcc (Ubuntu 
13.1.0-8ubuntu1~22.04) 13.1.0. Builds ran serially with two CPUs.
   
   - `sim:nsh` baseline and branch use identical configurations with pseudoFS 
attributes, task identity, links and assertions enabled. The local test 
compiles the actual `fs_inodereserve.c` with isolated root storage and an 
allocator wrapper. It injects allocation failure without modifying the running 
root tree. Baseline: **33 checks, 4 failures**. Branch: **33 checks, 0 
failures**. The failed baseline requirements are root initialization under 
allocation failure, absence of the failing allocator call, absence of a normal 
root allocation, and static storage for the live root.
   - On the branch, both injected-failure and normal root initialization make 
**0 allocator calls**. The live boot root is outside the heap. Ordinary inode 
allocation still uses the heap and still fails when allocation is denied. 
Metadata, inode sequence, 1000 root directory traversals, balanced root 
references, child creation/removal and root unlink rejection pass.
   - Attributes/identity disabled: **27 checks, 0 failures**. Independent 64 
KiB FS heap: **33 checks, 0 failures**.
   - Make/CMake `sim:nsh` and OSTest pass. Protected `qemu-armv7r:pnsh` and 
kernel `qemu-armv8a:knsh` build with attributes, identity and an independent FS 
heap. Both ELFs contain the root storage in BSS.
   - Small `sim:nsh` images exclude the local test app and use identical 
`DEFAULT_SMALL` configurations. GNU size deltas: text **+64**, data **+0**, bss 
**+128** bytes. These figures report static image sections, not total runtime 
heap plus BSS use.
   
   Runtime validation is limited to the flat simulator. No real hardware, 
protected/kernel runtime or SMP stress was tested. The fault injection executes 
an isolated copy of the production initializer, while the real boot root is 
independently checked through the VFS and heap membership API. No runtime 
speedup is claimed.
   
   The standalone local regression app is included below. Place the four files 
in `apps/examples/rootinodecheck`, configure `sim:nsh`, enable 
`EXAMPLES_ROOTINODECHECK`, `PSEUDOFS_ATTRIBUTES`, `SCHED_USER_IDENTITY`, 
`PSEUDOFS_FILE`, `FS_LINKS` and `DEBUG_ASSERTIONS`, then build and run 
`rootinodecheck`. It is validation code, not an installed application in this 
OS patch. Both revisions use the same test statements; a comment section label 
was corrected after the baseline run to satisfy checkpatch.
   
   Testing logs before change:
   
   <details>
   <summary>Baseline build and runtime</summary>
   
   ```text
   rev 695d40366da serial/pl011: fix pl011_putc SMP race on IMSC causing RX 
input loss build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-static-root-inode/baseline/run.log
   nsh> rootinodecheck
   ROOT failure injection allocations=1 initialized=0
   FAIL root survives allocation failure errno=0
   FAIL root skips failing allocator errno=0
   ROOT normal allocations=1 inode_bytes=136
   FAIL root needs no allocation errno=0
   LIVE root heap_member=1 refs=1 inode_bytes=136
   FAIL live root in static storage errno=0
   LIVE root traversals=1000
   ROOTINODE RESULT checks=33 failures=4
   nsh> echo ROOTINODE_DONE
   ROOTINODE_DONE
   nsh> poweroff
   ```
   
   </details>
   
   Testing logs after change:
   
   <details>
   <summary>branch build and runtime</summary>
   
   ```text
   rev 03bcfa319eb fs/inode: use static storage for the root inode build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-static-root-inode/branch/run.log
   nsh> rootinodecheck
   ROOT failure injection allocations=0 initialized=1
   ROOT normal allocations=0 inode_bytes=136
   LIVE root heap_member=0 refs=1 inode_bytes=136
   LIVE root traversals=1000
   ROOTINODE RESULT checks=33 failures=0
   nsh> echo ROOTINODE_DONE
   ROOTINODE_DONE
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>branch-minimal build and runtime</summary>
   
   ```text
   rev 03bcfa319eb fs/inode: use static storage for the root inode build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-static-root-inode/branch-minimal/run.log
   nsh> rootinodecheck
   ROOT failure injection allocations=0 initialized=1
   ROOT normal allocations=0 inode_bytes=72
   LIVE root heap_member=0 refs=1 inode_bytes=72
   LIVE root traversals=1000
   ROOTINODE RESULT checks=27 failures=0
   nsh> echo ROOTINODE_DONE
   ROOTINODE_DONE
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>branch-private build and runtime</summary>
   
   ```text
   rev 03bcfa319eb fs/inode: use static storage for the root inode build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-static-root-inode/branch-private/run.log
   nsh> rootinodecheck
   ROOT failure injection allocations=0 initialized=1
   ROOT normal allocations=0 inode_bytes=136
   LIVE root heap_member=0 refs=1 inode_bytes=136
   LIVE root traversals=1000
   ROOTINODE RESULT checks=33 failures=0
   nsh> echo ROOTINODE_DONE
   ROOTINODE_DONE
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>Final Make/CMake/OSTest gate</summary>
   
   ```text
   target   : nuttx
   branch   : fs/static-root-inode
   head     : 03bcfa319ebcce71c087daa30d74f2014b0e4313
   subject  : fs/inode: use static storage for the root inode
   commits  : 1
   base     : 695d40366d (merge-base with upstream/master)
   behind   : 0 commits of upstream/master
   nuttx    : 03bcfa319e
   apps     : 13ec1c27bd
   options  : CONFIG_PSEUDOFS_FILE CONFIG_PSEUDOFS_ATTRIBUTES 
CONFIG_SCHED_USER_IDENTITY CONFIG_FS_LINKS CONFIG_FS_HEAPSIZE=65536 
CONFIG_DEBUG_ASSERTIONS 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
   ```
   
   </details>
   
   <details>
   <summary>protected build</summary>
   
   ```text
   qemu-armv7r:pnsh build PASS
   arm-none-eabi-gcc (Arm GNU Toolchain 13.2.rel1 (Build arm-13.7)) 13.2.1 
20231009
   Copyright (C) 2023 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   
   400026d0 00000068 b g_root_inode_storage
   ```
   
   </details>
   
   <details>
   <summary>kernel build</summary>
   
   ```text
   qemu-armv8a:knsh build PASS
   aarch64-none-elf-gcc (GCC) 13.4.0
   Copyright (C) 2023 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   
   00000000402c6040 0000000000000088 b g_root_inode_storage
   ```
   
   </details>
   
   <details>
   <summary>size-baseline</summary>
   
   ```text
      text         data     bss     dec     hex filename
    550813         2800   12896  566509   8a4ed nuttx
   ```
   
   </details>
   
   <details>
   <summary>size-branch</summary>
   
   ```text
      text         data     bss     dec     hex filename
    550877         2800   13024  566701   8a5ad nuttx
   ```
   
   </details>
   
   <details>
   <summary>Regression app: rootinodecheck_main.c</summary>
   
   ```c
   /****************************************************************************
    * apps/examples/rootinodecheck/rootinodecheck_main.c
    *
    * SPDX-License-Identifier: Apache-2.0
    
****************************************************************************/
   
   /****************************************************************************
    * Included Files
    
****************************************************************************/
   
   #include <nuttx/config.h>
   
   #include <sys/stat.h>
   #include <dirent.h>
   #include <errno.h>
   #include <inttypes.h>
   #include <stdbool.h>
   #include <stdio.h>
   #include <string.h>
   #include <unistd.h>
   
   #include "fs_heap.h"
   #include "inode/inode.h"
   
   /****************************************************************************
    * Private Data
    
****************************************************************************/
   
   static FAR struct inode *g_testroot;
   static unsigned int g_allocations;
   static unsigned int g_checks;
   static unsigned int g_failures;
   static bool g_failalloc;
   
   /****************************************************************************
    * Private Functions
    
****************************************************************************/
   
   static FAR void *test_zalloc(size_t size)
   {
     g_allocations++;
     return g_failalloc ? NULL : fs_heap_zalloc(size);
   }
   
   /****************************************************************************
    * Pre-processor Definitions
    
****************************************************************************/
   
   /* Compile the production initializer with isolated storage and allocation
    * injection so allocation failure cannot corrupt the running root tree.
    */
   
   #undef fs_heap_zalloc
   #define fs_heap_zalloc test_zalloc
   #define g_root_inode g_testroot
   #define inode_root_reserve test_root_reserve
   #define inode_reserve test_inode_reserve
   
   /****************************************************************************
    * Included Files
    
****************************************************************************/
   
   #include "../../../nuttx/fs/inode/fs_inodereserve.c"
   
   /****************************************************************************
    * Pre-processor Definitions
    
****************************************************************************/
   
   #undef g_root_inode
   
   /****************************************************************************
    * Private Functions
    
****************************************************************************/
   
   static bool check(bool ok, FAR const char *label)
   {
     g_checks++;
     if (!ok)
       {
         g_failures++;
         printf("FAIL %s errno=%d\n", label, errno);
       }
   
     return ok;
   }
   
   static void free_testroot(void)
   {
     if (g_testroot != NULL && fs_heap_member(g_testroot))
       {
         fs_heap_free(g_testroot);
       }
   
     g_testroot = NULL;
   }
   
   static void check_initialization(void)
   {
     FAR struct inode *node;
     struct timespec before;
     struct timespec after;
     ino_t rootino;
   
     g_failalloc = true;
     g_allocations = 0;
     test_root_reserve();
     printf("ROOT failure injection allocations=%u initialized=%d\n",
            g_allocations, g_testroot != NULL);
     check(g_testroot != NULL, "root survives allocation failure");
     check(g_allocations == 0, "root skips failing allocator");
     free_testroot();
   
     g_failalloc = false;
     g_allocations = 0;
     clock_gettime(CLOCK_REALTIME, &before);
     test_root_reserve();
     clock_gettime(CLOCK_REALTIME, &after);
     printf("ROOT normal allocations=%u inode_bytes=%zu\n",
            g_allocations, sizeof(struct inode));
     check(g_allocations == 0, "root needs no allocation");
     if (!check(g_testroot != NULL, "root initialized"))
       {
         return;
       }
   
     rootino = g_testroot->i_ino;
     check(rootino != 0, "root inode number initialized");
     check(atomic_read(&g_testroot->i_crefs) == 1, "root tree reference");
     check(g_testroot->i_parent == NULL && g_testroot->i_peer == NULL &&
           g_testroot->i_child == NULL, "root links initialized");
     check(g_testroot->i_name[0] == '\0', "root empty name");
     check(g_testroot->i_flags == 0 && g_testroot->i_size == 0 &&
           g_testroot->i_private == NULL && g_testroot->u.i_ops == NULL,
           "root fields initialized");
   #ifdef CONFIG_PSEUDOFS_ATTRIBUTES
     check(g_testroot->i_mode == 0777, "root mode");
     check(g_testroot->i_atime.tv_sec >= before.tv_sec &&
           g_testroot->i_atime.tv_sec <= after.tv_sec, "root time");
     check(memcmp(&g_testroot->i_atime, &g_testroot->i_mtime,
                  sizeof(struct timespec)) == 0 &&
           memcmp(&g_testroot->i_atime, &g_testroot->i_ctime,
                  sizeof(struct timespec)) == 0, "root times equal");
   #ifdef CONFIG_SCHED_USER_IDENTITY
     check(g_testroot->i_owner == geteuid() &&
           g_testroot->i_group == getegid(), "initializer credentials");
   #endif
   #endif
   
     g_failalloc = true;
     node = inode_alloc("normal/child", 0640);
     check(node == NULL, "normal inode still fails with ENOMEM");
     g_failalloc = false;
     g_allocations = 0;
     node = inode_alloc("normal/child", 0640);
     check(g_allocations == 1, "normal inode uses allocator");
     if (check(node != NULL, "normal inode allocated"))
       {
         check(node->i_ino == rootino + 1, "inode number sequence");
         check(strcmp(node->i_name, "normal") == 0, "normal inode name");
         check(atomic_read(&node->i_crefs) == 1, "normal tree reference");
   #ifdef CONFIG_PSEUDOFS_ATTRIBUTES
         check(node->i_mode == 0640, "normal inode mode");
   #ifdef CONFIG_SCHED_USER_IDENTITY
         check(node->i_owner == geteuid() && node->i_group == getegid(),
               "normal inode credentials");
   #endif
   #endif
         fs_heap_free(node);
       }
   
     free_testroot();
   }
   
   static void check_live_root(void)
   {
     FAR struct inode *root = g_root_inode;
     FAR DIR *dir;
     struct stat before;
     struct stat after;
     int refs = atomic_read(&root->i_crefs);
     int ret;
     int i;
   
     printf("LIVE root heap_member=%d refs=%d inode_bytes=%zu\n",
            fs_heap_member(root), refs, sizeof(*root));
     check(!fs_heap_member(root), "live root in static storage");
     check(stat("/", &before) == 0 && S_ISDIR(before.st_mode), "stat root");
     check(mkdir("/rootcheck", 0750) == 0, "mkdir beneath root");
     check(mkdir("/rootcheck/child", 0700) == 0, "mkdir nested child");
     for (i = 0; i < 1000; i++)
       {
         dir = opendir("/");
         if (dir == NULL || readdir(dir) == NULL || closedir(dir) != 0 ||
             stat("/", &after) != 0 || before.st_ino != after.st_ino)
           {
             break;
           }
       }
   
     printf("LIVE root traversals=%d\n", i);
     check(i == 1000, "repeated root traversal");
     check(atomic_read(&root->i_crefs) == refs, "balanced root references");
     check(rmdir("/") < 0 && errno == ENOTEMPTY, "nonempty root retained");
     inode_lock();
     ret = inode_remove("/");
     inode_unlock();
     check(ret == -ENOENT, "root unlink rejected");
     check(g_root_inode == root && atomic_read(&root->i_crefs) == refs,
           "root retained after rejected unlink");
     check(stat("/", &after) == 0 && before.st_ino == after.st_ino,
           "root identity retained");
     check(rmdir("/rootcheck/child") == 0, "remove nested child");
     check(rmdir("/rootcheck") == 0, "remove root child");
   }
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   int main(int argc, FAR char *argv[])
   {
     check_initialization();
     check_live_root();
     printf("ROOTINODE RESULT checks=%u failures=%u\n", g_checks, g_failures);
     return g_failures != 0;
   }
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Kconfig</summary>
   
   ```text
   config EXAMPLES_ROOTINODECHECK
        bool "Local static root inode checks"
        default n
        depends on BUILD_FLAT && !DISABLE_PSEUDOFS_OPERATIONS
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Makefile</summary>
   
   ```text
   include $(APPDIR)/Make.defs
   PROGNAME = rootinodecheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 16384
   MODULE = $(CONFIG_EXAMPLES_ROOTINODECHECK)
   MAINSRC = rootinodecheck_main.c
   CFLAGS += -I$(abspath $(APPDIR)/../nuttx/fs)
   include $(APPDIR)/Application.mk
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Make.defs</summary>
   
   ```text
   ifeq ($(CONFIG_EXAMPLES_ROOTINODECHECK),y)
   CONFIGURED_APPS += $(APPDIR)/examples/rootinodecheck
   endif
   ```
   
   </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 follows the contributing guidelines for scope, coding style and 
testing.
   - [ ] My PR is still work in progress (not ready for review).
   - [x] My PR is ready for review and can be safely merged into the 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]

  • [PR] fs/inode: use static sto... jingfei195887 (via GitHub) via commits

Reply via email to