anchao commented on PR #11177:
URL: https://github.com/apache/nuttx/pull/11177#issuecomment-1811808812

   > This should be fine from the kernel mode point of view now. I'm just 
wondering will we lose the benefit from your optimizations because of the 
copying ?
   
   This may depend on the application implement, In the previous 
implementation, all file descriptors would be dup in files_duplist and closed 
after nxtask_init in files_close_onexec.
   
   ```
   exec_module
   |
   |->nxtask_init
   |  |
   |   ->group_setuptaskfiles
   |     |
   |      ->files_duplist    /* all files will be duplicate from parent */
   |
   |->spawn_file_actions /* do file actions */
    ->files_close_onexec /* close exec files */
   ```
   
   In the current implementation,
   1. if the file actions has ACTION_CLOSE/OPEN operation, no needs to dup, and 
also no needs to close;
   2. if the file descriptor has the O_CLOEXEC flag, it will not perform 
additional dup/close operations.
   
   ```
   exec_module
   |
    ->nxtask_init
      |
       ->group_setuptaskfiles
         |
          ->files_duplist    /* pass the file actions to avoid  */
            |
            |->spawn_file_is_duplicateable /* 1. SPAWN_FILE_ACTION_CLOSE: will 
be close, do not dup  (*)
            |                               * 2. SPAWN_FILE_ACTION_OPEN:  will 
be open, no needs to dup (*)
            |                               * 3. SPAWN_FILE_ACTION_DUP2:  could 
be dup
            |                               */
             ->(filep->f_oflags & O_CLOEXEC) /* skip dup */
   ```
             
   
   Of course, this improvement is obvious in flat build, but if it is in kernel 
mode, it will depend on the ratio of action/cloexec in the file list.
   
   
   ```
   exec_module
   |
   |->binfmt_copyactions
   |
   |->nxtask_init
   |  |
   |   ->group_setuptaskfiles
   |     |
   |      ->files_duplist    /* pass the file actions to avoid  */
   |        |
   |        |->spawn_file_is_duplicateable /* 1. SPAWN_FILE_ACTION_CLOSE: will 
be close, do not dup  (*)
   |        |                               * 2. SPAWN_FILE_ACTION_OPEN:  will 
be open, no needs to dup (*)
   |        |                               * 3. SPAWN_FILE_ACTION_DUP2:  could 
be dup
   |        |                               */
   |         ->(filep->f_oflags & O_CLOEXEC) /* skip dup */
   |
    ->binfmt_freeactions
   ```
   
   
   In addition, not re-dup the file in nxtask_init could also avoid potential 
problems caused by some files/drivers that do not support multiple instances. 
If the driver sets the O_CLOEXEC flag after open, but the driver will be dup 
multiple times after exec, Then it is also unfriendly to the driver developer.


-- 
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: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to