This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new a71866e2fd7 fs/vfs: Call no file method in the critical section of
sync().
a71866e2fd7 is described below
commit a71866e2fd7bbb1ed973198b835c81027c08a14b
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 27 23:55:34 2026 +0100
fs/vfs: Call no file method in the critical section of sync().
sync() called file_fsync() and file_put() for each open file of each
task in the callback of nxsched_foreach(), which holds the critical
section for the whole walk. A driver method can take a spinlock of the
driver. A pipe takes d_bflock in pipecommon_ioctl() for BIOC_FLUSH and
in pipecommon_close(), and pipecommon_write() holds d_bflock when it
posts the semaphore of a waiting reader; nxsem_post() then enters the
critical section. On SMP the caller of sync() held the critical section
and waited for d_bflock, and the writer held d_bflock and waited for the
critical section. All CPUs stopped.
Each walk of nxsched_foreach() now only collects the IDs of up to 16
tasks, in the order of the IDs. After the walk, for each ID and each
descriptor, sync() enters the critical section, finds the task again
and takes a reference to the file with fdlist_get(). file_fsync() and
file_put() come after the critical section. The TCB and the descriptor
list are used only in the critical section that found them. The
reference keeps the file when the task closes the descriptor or ends:
fdlist_free() empties the table under fl_lock before it closes a file,
so a later look-up finds no file. sync() then walks again for the IDs
above the last one, until a walk finds fewer than 16. It needs no
memory allocation. A task that starts after sync() began can be left
out.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
fs/vfs/fs_sync.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 101 insertions(+), 17 deletions(-)
diff --git a/fs/vfs/fs_sync.c b/fs/vfs/fs_sync.c
index c856ebd2bb4..67ab7db738a 100644
--- a/fs/vfs/fs_sync.c
+++ b/fs/vfs/fs_sync.c
@@ -27,52 +27,112 @@
#include <nuttx/config.h>
#include <limits.h>
+#include <string.h>
#include <unistd.h>
#include <nuttx/fs/fs.h>
+#include <nuttx/irq.h>
#include <nuttx/sched.h>
#include "inode/inode.h"
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The number of task IDs that one walk of the task list collects */
+
+#define SYNC_NPIDS 16
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sync_pids_s
+{
+ pid_t last; /* Collect only the IDs above this one */
+ int npids; /* The number of IDs in pids[] */
+ pid_t pids[SYNC_NPIDS]; /* The smallest IDs above last, in order */
+};
+
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
- * Name: task_fssync
+ * Name: sync_collect
*
* Description:
- * nxsched_foreach() callback that fsyncs every open file of one task.
+ * nxsched_foreach() callback that keeps the SYNC_NPIDS smallest task IDs
+ * above sp->last, in order. It runs in the critical section, so it only
+ * reads the TCB.
*
****************************************************************************/
-static void task_fssync(FAR struct tcb_s *tcb, FAR void *arg)
+static void sync_collect(FAR struct tcb_s *tcb, FAR void *arg)
{
- FAR struct tcb_s *ctcb;
- int pid = tcb->pid;
- int count;
- int fd;
+ FAR struct sync_pids_s *sp = arg;
+ pid_t pid = tcb->pid;
+ int n;
+ int i;
- if (tcb->group == NULL)
+ if (pid <= sp->last ||
+ (sp->npids == SYNC_NPIDS && pid > sp->pids[SYNC_NPIDS - 1]))
{
return;
}
- count = fdlist_count(&tcb->group->tg_fdlist);
+ /* Insert the ID in order. If the array is full, the greatest ID drops
+ * out, and a later walk collects it again.
+ */
- for (fd = 0; fd < count; fd++)
+ n = sp->npids < SYNC_NPIDS ? sp->npids++ : SYNC_NPIDS - 1;
+ i = 0;
+ while (i < n && sp->pids[i] < pid)
{
- FAR struct file *filep;
+ i++;
+ }
+
+ memmove(&sp->pids[i + 1], &sp->pids[i], (n - i) * sizeof(sp->pids[0]));
+ sp->pids[i] = pid;
+}
+
+/****************************************************************************
+ * Name: task_fssync
+ *
+ * Description:
+ * Fsync every open file of one task. This runs outside the critical
+ * section.
+ *
+ ****************************************************************************/
- /* file_fsync() may block and let the task exit meanwhile */
+static void task_fssync(pid_t pid)
+{
+ FAR struct tcb_s *tcb;
+ FAR struct file *filep;
+ irqstate_t flags;
+ int ret;
+ int fd;
- ctcb = nxsched_get_tcb(pid);
- if (ctcb == NULL || ctcb->group == NULL || ctcb != tcb)
+ for (fd = 0; ; fd++)
+ {
+ /* The TCB and the descriptor list are valid only in the critical
+ * section that finds the task. The file reference keeps the file.
+ */
+
+ flags = enter_critical_section();
+ tcb = nxsched_get_tcb(pid);
+ if (tcb == NULL || tcb->group == NULL ||
+ fd >= fdlist_count(&tcb->group->tg_fdlist))
{
- return;
+ leave_critical_section(flags);
+ break;
}
- if (fdlist_get(&ctcb->group->tg_fdlist, fd, &filep) >= 0)
+ ret = fdlist_get(&tcb->group->tg_fdlist, fd, &filep);
+ leave_critical_section(flags);
+
+ if (ret >= 0)
{
file_fsync(filep);
file_put(filep);
@@ -118,7 +178,31 @@ static int mountpt_syncfs(FAR struct inode *inode,
void sync(void)
{
- nxsched_foreach(task_fssync, NULL);
+ struct sync_pids_s sp;
+ int i;
+
+ /* A file method can take a driver spinlock whose holder enters the
+ * critical section (a pipe posts a semaphore with d_bflock held). So
+ * each walk only collects task IDs, and the fsyncs come after the walk.
+ */
+
+ sp.last = -1;
+ do
+ {
+ sp.npids = 0;
+ nxsched_foreach(sync_collect, &sp);
+
+ for (i = 0; i < sp.npids; i++)
+ {
+ task_fssync(sp.pids[i]);
+ }
+
+ if (sp.npids > 0)
+ {
+ sp.last = sp.pids[sp.npids - 1];
+ }
+ }
+ while (sp.npids == SYNC_NPIDS);
#ifndef CONFIG_DISABLE_MOUNTPOINT
foreach_inode(mountpt_syncfs, NULL, true);