CV-Bowen opened a new pull request, #22108:
URL: https://github.com/apache/nuttx/pull/22108

   ## Summary
   
   Two unrelated small fixes.
   
   - `mm/mempool: free the whole multiple pool on init failure`:
     `mempool_multiple_init()` allocates one block holding both the
     `mempool_multiple_s` and the pools array.  The `err_with_pools` path
     handed the interior `pools` pointer to `mempool_multiple_free_chunk()`.
     With `chunksize < expandsize` that calls `free()` on an interior
     pointer; otherwise it takes `mpool->lock`, which is only initialized on
     the success path, and frees nothing because the chunk queue is empty.
     Free the block that was allocated instead.
   - `fs/procfs: show the affinity mask on the status CPU line`: the status
     file showed which CPU a thread runs on but not which CPUs it may run
     on.  Both are needed to tell a scheduling problem from a pinning one.
   
   ## Impact
   
   - A failed `mempool_multiple_init()` no longer frees an interior pointer
     nor takes an uninitialized mutex; the block is returned to the caller's
     allocator as a whole.
   - The `/proc/<pid>/status` `CPU:` line gains the affinity mask in
     parentheses.  The field is otherwise unchanged, and the line is still
     `CPU:         ---` for a thread that is not running.  Anyone parsing the
     line by position would see the extra text; the CPU index itself is now
     zero-padded to three digits to keep the columns aligned.
   - No behavioural change to scheduling; nothing reads this line in-tree.
   
   ## Testing
   
   Host: Ubuntu 22.04.5 LTS x86_64; gcc 13.1.0; `sim:smp`
   (`CONFIG_SMP=y`, 4 CPUs, `CONFIG_MM_HEAP_MEMPOOL`).
   
   **mempool**: the probe calls `mempool_multiple_init()` the way
   `mm_initialize_heap()` does, with `chunksize` 4096 and `expandsize` 8192
   so the `free()`-on-interior-pointer path is the one taken, and a
   `dict_expendsize` large enough that the dictionary chunk cannot be
   allocated, which sends it to `err_with_pools`:
   
   | tree | result |
   |---|---|
   | master | `Assertion failed : at file: mm_heap/mm_malloc_size.c:73 
task(CPU0): hello`, task killed |
   | this PR | `mempool_multiple_init=0`, returns NULL |
   
   `mm_malloc_size.c:73` is `DEBUGASSERT(MM_NODE_IS_ALLOC(node))`, reached
   from `mm_free()` on the interior `pools` pointer.
   
   **procfs**: the probe reads back the `CPU:` line of its own
   `/proc/<pid>/status`:
   
   | tree | line |
   |---|---|
   | master | `CPU:        000` |
   | this PR | `CPU:        000(0x0000000f)` |
   
   Both builds finish with 0 errors, and checkpatch and nxstyle pass on both
   files.
   
   <details><summary>Test programs (not part of this PR)</summary>
   
   ```c
   /* mempool probe: send mempool_multiple_init() to err_with_pools with
    * chunksize < expandsize.
    */
   #include <nuttx/config.h>
   #include <stdio.h>
   #include <nuttx/mm/mm.h>
   #include <nuttx/mm/mempool.h>
   
   int main(int argc, FAR char *argv[])
   {
     static const size_t poolsize[] = { 16, 32, 64 };
     FAR struct mempool_multiple_s *mpool;
   
     mpool = mempool_multiple_init("probe", poolsize, 3,
                                   (mempool_multiple_alloc_t)mm_memalign,
                                   
(mempool_multiple_alloc_size_t)mm_malloc_size,
                                   (mempool_multiple_free_t)mm_free, USR_HEAP,
                                   4096, 8192, 512 * 1024 * 1024);
     printf("mempool_multiple_init=%p\n", mpool);
     return 0;
   }
   ```
   
   ```c
   /* procfs probe: print the CPU line of our own status file. */
   #include <nuttx/config.h>
   #include <stdio.h>
   #include <string.h>
   #include <fcntl.h>
   #include <unistd.h>
   
   int main(int argc, FAR char *argv[])
   {
     char path[32];
     char buf[1024];
     FAR char *p;
     ssize_t n;
     int fd;
   
     snprintf(path, sizeof(path), "/proc/%d/status", getpid());
     fd = open(path, O_RDONLY);
     if (fd < 0)
       {
         printf("open %s failed\n", path);
         return 1;
       }
   
     n = read(fd, buf, sizeof(buf) - 1);
     close(fd);
     if (n <= 0)
       {
         printf("read failed n=%ld\n", (long)n);
         return 1;
       }
   
     buf[n] = '\0';
     for (p = strtok(buf, "\n"); p != NULL; p = strtok(NULL, "\n"))
       {
         if (strncmp(p, "CPU:", 4) == 0)
           {
             printf("STATUS [%s]\n", p);
             return 0;
           }
       }
   
     printf("no CPU line\n");
     return 1;
   }
   ```
   
   </details>
   


-- 
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]

Reply via email to