Bläddra i källkod

Keep the UI responsive during a disk format

The format command is a kernel thread and therefore held the big kernel
lock for its entire run.  The ring-3 WM needs the BKL for every syscall,
so it could not composite a frame while a format ran; the kernel watchdog
killed and restarted it every 15 s.  Even after dropping the BKL,
sys_fs_enter() made every file syscall wait for format_busy, including
/ramfs and /share, so the WM's periodic /share stats write slept for the
whole format.

Release the BKL for the formatter's raw-sector loop (it is only needed
around the zero-buffer page allocation and the final remount), and apply
the format_busy wait only to disk-backed mounts so the RAM mounts stay
usable.  Print every 1% with a 10 ms scheduling point, and yield every 32
sectors in the eMMC PIO loops so the WM and terminal get CPU during the
long polled transfers.  The zero-buffer allocation stays under the BKL:
the frame-allocator bitmap is only interrupt-protected, not SMP-safe.

Validated on the HP Chromebook 11 G5: format mmc0 shows live 1% progress,
the top-bar clock keeps ticking and windows can be dragged while it runs.
Johannes Zellner 1 vecka sedan
förälder
incheckning
7af524f90d
10 ändrade filer med 189 tillägg och 104 borttagningar
  1. 17 10
      AGENTS.md
  2. 3 1
      docs/cpu.html
  3. 6 4
      docs/drivers.html
  4. 1 1
      docs/filesystems.html
  5. 14 1
      fatformat.c
  6. 11 1
      format.c
  7. 9 0
      sdhci.c
  8. 116 86
      sys_fs.c
  9. 7 0
      vfs.c
  10. 5 0
      vfs.h

+ 17 - 10
AGENTS.md

@@ -452,11 +452,16 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   paths from Tab completion ("/ramfs/bin/") work.  `sys_fs.c` is the file
   service for ring-3 commands:
   whole-file read/write, index-based directory listing, mount listing,
-  unlink/mkdir/rmdir (writes to the ramfs return a read-only error).  A FAT
-  operation sets `sys_fs_busy`, which the scheduler honours (no process switch
-  during it): the FAT driver has static state and the floppy still needs its
-  IRQ.  So expect a brief system stall (not ticks — the PIT keeps running)
-  while a command reads or writes a disk.  Directory listings cache the first
+  unlink/mkdir/rmdir (writes to the ramfs return a read-only error).  A file
+  operation enters a non-preemptible section (`preempt_disable`, with
+  interrupts on for the floppy's IRQ 6) because the FAT driver has static
+  state; `vfs_scan_busy` and `format_busy` make it sleep first when a mount
+  scan or a destructive format owns the disks.  A format releases the BKL for
+  its raw-sector loop, so the WM and terminals keep running; `sys_fs_begin()`
+  applies the `format_busy` wait only to disk-backed mounts, so `/ramfs` and
+  `/share` stay usable during a format.  Expect a brief stall (not ticks —
+  the PIT keeps running) while a command reads or writes a disk.  Directory
+  listings cache the first
   64 entries of the last FAT directory (`fat_cache` in `vfs.c`), so the
   WebDAV server's index 0..N-1 sequence is one directory walk instead of N;
   any write/mkdir/rmdir/unlink invalidates it.
@@ -485,11 +490,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   (`SEC_COUNT` at 212, `CACHE_SIZE` at 249), and switches to a 25 MHz data
   clock. Sector I/O is CMD17/CMD24 over the polled PIO buffer port (one
   512-byte block per command; no DMA, IRQ, CMD6 bus-width/high-speed switch
-  or hotplug). `blkdev.c` registers it as `mmc0`, so `storage`, the VFS MBR+
-  FAT mount path and `format mmc0` work exactly like an IDE/SATA disk;
-  `sdhci_flush()` issues the CMD6 `FLUSH_CACHE` when the card reports a
-  write-back cache. A stock eMMC holds the ChromeOS image, so formatting it
-  is destructive.
+  or hotplug).  The read/write loops call `sched_yield()` every 32 sectors so
+  a long format does not starve the WM/terminal (a normal file read has
+  preemption disabled, so the yield is a no-op there). `blkdev.c` registers
+  it as `mmc0`, so `storage`, the VFS MBR+ FAT mount path and `format mmc0`
+  work exactly like an IDE/SATA disk; `sdhci_flush()` issues the CMD6
+  `FLUSH_CACHE` when the card reports a write-back cache. A stock eMMC holds
+  the ChromeOS image, so formatting it is destructive.
 - **Serial**: `serial.c`, `serial_ring_buf.c/h`, `serial_loop.c` — 16550 UART
   with an IRQ-driven ring buffer and the `serial test` loopback check.
 - **Network**: `netdev.c/h` — NIC driver registry (`struct netdev`, first probe

+ 3 - 1
docs/cpu.html

@@ -330,7 +330,9 @@
 			<p><strong>Why one big lock?</strong> The kernel is small and not
 			designed around fine-grained locking. The BKL keeps every kernel
 			invariant simple, at the cost of serializing long kernel operations.
-			Disk I/O still holds it; network and serial waits drop it.</p>
+			Disk I/O still holds it, except a destructive format's raw-sector
+			loop, which releases it so the WM and terminals keep running;
+			network and serial waits drop it.</p>
 		</div>
 	</section>
 

+ 6 - 4
docs/drivers.html

@@ -206,10 +206,12 @@
 			<code>CACHE_SIZE</code> at 249), and switches to a 25 MHz data clock.
 			Sector I/O is CMD17/CMD24 through the polled PIO buffer port at
 			1-bit/25 MHz; there is no DMA, IRQ, bus-width/high-speed switch or
-			hotplug. The eMMC appears as <code>mmc0</code>, and
-			<code>format mmc0</code> writes an MBR + FAT partition exactly like
-			an IDE/SATA disk. The write path flushes the card cache with CMD6
-			<code>FLUSH_CACHE</code> when EXT_CSD reports one.
+			hotplug. The read/write loops yield every 32 sectors so a long
+			format does not starve the window manager. The eMMC appears as
+			<code>mmc0</code>, and <code>format mmc0</code> writes an MBR + FAT
+			partition exactly like an IDE/SATA disk. The write path flushes the
+			card cache with CMD6 <code>FLUSH_CACHE</code> when EXT_CSD reports
+			one.
 		</p>
 	</section>
 

+ 1 - 1
docs/filesystems.html

@@ -157,7 +157,7 @@
 			<li><strong>Read/write.</strong> Read, write, delete, mkdir and rmdir are implemented; FAT32 allocation uses an FSInfo-seeded free-cluster hint.</li>
 			<li><strong>8.3 names.</strong> The on-disk name is the canonical one. The shell and <code>files</code> display the space-padded form; a long name cannot be created.</li>
 			<li><strong>Directory cache.</strong> The last FAT directory's first 64 entries are cached in <code>vfs.c</code>, so WebDAV's index sequence is one walk instead of N. Any write invalidates the cache.</li>
-			<li><strong>Serialization.</strong> A FAT operation sets <code>sys_fs_busy</code>; the scheduler does not switch processes during it because the driver has static state and the floppy needs IRQ 6.</li>
+			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.</li>
 		</ul>
 		<p>
 			<span class="file">fatdetect.c</span> and <span class="file">fatformat.c</span>

+ 14 - 1
fatformat.c

@@ -4,6 +4,7 @@
 #include "fatformat.h"
 #include "io.h"
 #include "mm.h"
+#include "process.h"
 #include "sched.h"
 
 #define SEC_SIZE 512
@@ -55,9 +56,12 @@ static void format_progress(unsigned int sectors)
 	fmt_done += sectors;
 	pct = (fmt_done * 100u) / fmt_total;
 	if (pct > 100) pct = 100;
-	if (pct >= fmt_last_pct + 10 || pct == 100) {
+	if (pct >= fmt_last_pct + 1 || pct == 100) {
 		fmt_last_pct = pct;
 		format_pct(pct);
+		/* Sleep briefly after the line so the ring-3 terminal drains
+		 * the tty and the WM presents the new row promptly. */
+		process_sleep_ms(10);
 	}
 }
 
@@ -361,7 +365,13 @@ int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
 	zchunk = 1;
 	zpages = 0;
 	if (!is_floppy) {
+		/* The caller drops the BKL for the long format loop, but the
+		 * frame allocator bitmap is only protected by interrupts (not
+		 * SMP-safe), so take the BKL around the allocation.  bkl_lock()
+		 * nests when the caller still holds it. */
+		bkl_lock();
 		zbuf = (unsigned char *)mm_alloc_pages(ZERO_CHUNK_PAGES);
+		bkl_unlock();
 		if (zbuf) {
 			unsigned int k;
 			for (k = 0; k < ZERO_CHUNK_PAGES * PAGE_SIZE; k++)
@@ -441,8 +451,11 @@ int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
 out:
 	if (zpages) {
 		unsigned int k;
+
+		bkl_lock();
 		for (k = 0; k < zpages; k++)
 			mm_free_page((unsigned int)zbuf + k * PAGE_SIZE);
+		bkl_unlock();
 	}
 	return rc;
 }

+ 11 - 1
format.c

@@ -232,7 +232,17 @@ void program_format_main(const char *arg)
 	/* Other filesystem syscalls wait while the FAT is half-written. */
 	format_owner_pid = current_proc()->pid;
 	format_busy = 1;
-	if (fat_format_limited(d, limit) != 0) {
+	/* The raw-sector loop does not touch shared kernel state (format_busy
+	 * keeps filesystem users off the disks; the block driver owns the
+	 * controller).  Release the BKL for its duration: a kernel command
+	 * otherwise holds it for its whole lifetime, and a long format starves
+	 * every ring-3 process (the WM stops compositing, the terminal stops
+	 * draining) even though it prints progress.  Reacquire it before the
+	 * remount below. */
+	bkl_unlock();
+	r = fat_format_limited(d, limit);
+	bkl_lock();
+	if (r != 0) {
 		format_busy = 0;
 		io_putstr("format: failed\n");
 		return;

+ 9 - 0
sdhci.c

@@ -18,6 +18,7 @@
 #include "paging.h"
 #include "pci.h"
 #include "process.h"
+#include "sched.h"
 #include "serial.h"
 #include "timer.h"
 
@@ -588,6 +589,12 @@ int sdhci_read_sectors(unsigned int lba, void *buf, unsigned int count)
 			      1, SDHCI_TRNS_READ,
 			      (unsigned char *)buf + i * 512u) != 0)
 			return -1;
+		/* The polled PIO path is slow; give the WM/terminal a slice
+		 * during a long format, which runs outside the filesystem's
+		 * non-preemptible section.  A normal file read disables
+		 * preemption, so the yield is a no-op there. */
+		if ((i & 31u) == 31u)
+			sched_yield();
 	}
 	return 0;
 }
@@ -608,6 +615,8 @@ int sdhci_write_sectors(unsigned int lba, const void *buf, unsigned int count)
 			      1, 0,
 			      (void *)((const unsigned char *)buf + i * 512u)) != 0)
 			return -1;
+		if ((i & 31u) == 31u)
+			sched_yield();
 	}
 	return 0;
 }

+ 116 - 86
sys_fs.c

@@ -10,18 +10,22 @@
  * That keeps each operation atomic (and immune to Ctrl+C killing the process
  * mid-write) at the cost of a brief stall during disk I/O.  Buffers point
  * into the caller's address space, which is also the active one during the
- * syscall, so no copy in/out is needed. */
+ * syscall, so no copy in/out is needed.
+ *
+ * A destructive `format` runs as a kernel thread without the BKL and sets
+ * format_busy while it rewrites a disk.  Disk-backed mounts wait for it;
+ * the RAM-backed mounts (/ramfs, /share) do not, so the WM's periodic
+ * /share stats write and the terminal keep running during a format. */
 
 #include "syscall.h"
 #include "fatformat.h"
 #include "process.h"
 #include "vfs.h"
 
-void sys_fs_enter(void)
+/* Wait for a destructive format that owns the disks while its FAT is
+ * half-written.  Sleeping drops the BKL so the formatter can finish. */
+static void fs_format_wait(void)
 {
-	/* A format owns the disks while its FAT is half-written.  Do not let a
-	 * file syscall read that inode/FAT state; sleeping drops the BKL so the
-	 * formatter can finish. */
 	while (format_busy && current_proc()->pid != format_owner_pid) {
 		if (!process_is_alive(format_owner_pid)) {
 			format_busy = 0;
@@ -29,9 +33,13 @@ void sys_fs_enter(void)
 		}
 		process_sleep_ms(10);
 	}
-	/* A boot-time `storage` scan (or a concurrent storage command) is
-	 * rebuilding the mount table and reading through the FAT static
-	 * buffers.  Wait for it instead of racing. */
+}
+
+/* A boot-time `storage` scan (or a concurrent storage command) is rebuilding
+ * the mount table and reading through the FAT static buffers.  Wait for it
+ * instead of racing. */
+static void fs_scan_wait(void)
+{
 	while (vfs_scan_busy && current_proc()->pid != vfs_scan_owner_pid) {
 		if (vfs_scan_owner_pid && !process_is_alive(vfs_scan_owner_pid)) {
 			vfs_scan_busy = 0;
@@ -39,8 +47,24 @@ void sys_fs_enter(void)
 		}
 		process_sleep_ms(10);
 	}
+}
+
+/* The disk may need its IRQ (floppy), and the FAT driver's static state must
+ * not be preempted in the middle of an operation. */
+static void fs_preempt_enter(void)
+{
 	preempt_disable();
-	__asm__ volatile ("sti");   /* the disk may need its IRQ */
+	__asm__ volatile ("sti");
+}
+
+void sys_fs_enter(void)
+{
+	/* Used by disk-touching services (the storage scan, ELF loads): wait
+	 * for a format and a mount-table rebuild, then take the non-
+	 * preemptible section. */
+	fs_format_wait();
+	fs_scan_wait();
+	fs_preempt_enter();
 }
 
 void sys_fs_exit(void)
@@ -84,19 +108,40 @@ static int fs_resolve(const char *path, struct vfs_mount **m,
 	return 0;
 }
 
+/* Resolve and enter the non-preemptible section for one path operation.
+ * Disk-backed mounts wait for a running format; the RAM mounts do not. */
+static int sys_fs_begin(const char *path, struct vfs_mount **m,
+			char *rel, unsigned int rel_size)
+{
+	int r;
+
+	fs_scan_wait();
+	r = fs_resolve(path, m, rel, rel_size);
+	if (r != 0)
+		return r;
+	if (vfs_mount_is_disk(*m)) {
+		fs_format_wait();
+		/* The mount table (or the mount slot) may have been rebuilt
+		 * while sleeping; resolve again before using it. */
+		r = fs_resolve(path, m, rel, rel_size);
+		if (r != 0)
+			return r;
+	}
+	fs_preempt_enter();
+	return 0;
+}
+
 int sys_read_file(const char *path, void *buf, unsigned int max)
 {
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
-	int r;
+	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
 
-	sys_fs_enter();
-	r = fs_resolve(path, &m, rel, sizeof(rel));
-	if (r == 0) {
-		r = m->ops->read_file(m, rel, buf, max);
-		if (r < 0)
-			r = sys_err(r);
-	}
+	if (r != 0)
+		return r;
+	r = m->ops->read_file(m, rel, buf, max);
+	if (r < 0)
+		r = sys_err(r);
 	sys_fs_exit();
 	return r;
 }
@@ -105,15 +150,13 @@ int sys_write_file(const char *path, const void *buf, unsigned int len)
 {
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
-	int r;
+	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
 
-	sys_fs_enter();
-	r = fs_resolve(path, &m, rel, sizeof(rel));
-	if (r == 0) {
-		r = m->ops->write_file(m, rel, buf, len);
-		if (r < 0)
-			r = sys_err(r);
-	}
+	if (r != 0)
+		return r;
+	r = m->ops->write_file(m, rel, buf, len);
+	if (r < 0)
+		r = sys_err(r);
 	sys_fs_exit();
 	return r;
 }
@@ -122,15 +165,13 @@ int sys_unlink(const char *path)
 {
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
-	int r;
+	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
 
-	sys_fs_enter();
-	r = fs_resolve(path, &m, rel, sizeof(rel));
-	if (r == 0) {
-		r = m->ops->unlink(m, rel);
-		if (r < 0)
-			r = sys_err(r);
-	}
+	if (r != 0)
+		return r;
+	r = m->ops->unlink(m, rel);
+	if (r < 0)
+		r = sys_err(r);
 	sys_fs_exit();
 	return r;
 }
@@ -139,15 +180,13 @@ int sys_mkdir(const char *path)
 {
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
-	int r;
+	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
 
-	sys_fs_enter();
-	r = fs_resolve(path, &m, rel, sizeof(rel));
-	if (r == 0) {
-		r = m->ops->mkdir(m, rel);
-		if (r < 0)
-			r = sys_err(r);
-	}
+	if (r != 0)
+		return r;
+	r = m->ops->mkdir(m, rel);
+	if (r < 0)
+		r = sys_err(r);
 	sys_fs_exit();
 	return r;
 }
@@ -156,15 +195,13 @@ int sys_rmdir(const char *path)
 {
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
-	int r;
+	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
 
-	sys_fs_enter();
-	r = fs_resolve(path, &m, rel, sizeof(rel));
-	if (r == 0) {
-		r = m->ops->rmdir(m, rel);
-		if (r < 0)
-			r = sys_err(r);
-	}
+	if (r != 0)
+		return r;
+	r = m->ops->rmdir(m, rel);
+	if (r < 0)
+		r = sys_err(r);
 	sys_fs_exit();
 	return r;
 }
@@ -173,28 +210,25 @@ int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
 {
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
-	int r;
-
-	sys_fs_enter();
-	r = fs_resolve(path, &m, rel, sizeof(rel));
-	if (r == 0) {
-		struct vfs_dirent d;
-		unsigned int i;
+	struct vfs_dirent d;
+	unsigned int i;
+	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
 
-		r = m->ops->list_dir(m, rel, index, &d);
-		if (r == 1) {
-			for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
-				out->name[i] = d.name[i];
-			out->name[i] = '\0';
-			out->size = d.size;
-			out->is_dir = d.is_dir;
-			out->raw_83 = d.raw_83;
-			r = 1;
-		} else if (r < 0) {
-			r = sys_err(r);
-		} else {
-			r = 0;
-		}
+	if (r != 0)
+		return r;
+	r = m->ops->list_dir(m, rel, index, &d);
+	if (r == 1) {
+		for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
+			out->name[i] = d.name[i];
+		out->name[i] = '\0';
+		out->size = d.size;
+		out->is_dir = d.is_dir;
+		out->raw_83 = d.raw_83;
+		r = 1;
+	} else if (r < 0) {
+		r = sys_err(r);
+	} else {
+		r = 0;
 	}
 	sys_fs_exit();
 	return r;
@@ -234,22 +268,18 @@ int sys_stat_path(const char *path, struct sys_stat *out)
 
 	if (!out)
 		return SYS_EINVAL;
-	size = 0;
-	is_dir = 0;
-	ro = 0;
-	sys_fs_enter();
-	r = fs_resolve(path, &m, rel, sizeof(rel));
-	if (r == 0) {
-		r = m->ops->stat(m, rel, &size, &is_dir);
-		if (r < 0) {
-			r = sys_err(r);
-		} else {
-			ro = (unsigned int)m->ro;
-			out->size = size;
-			out->is_dir = (unsigned int)is_dir;
-			out->ro = ro;
-			r = 0;
-		}
+	r = sys_fs_begin(path, &m, rel, sizeof(rel));
+	if (r != 0)
+		return r;
+	r = m->ops->stat(m, rel, &size, &is_dir);
+	if (r < 0) {
+		r = sys_err(r);
+	} else {
+		ro = (unsigned int)m->ro;
+		out->size = size;
+		out->is_dir = (unsigned int)is_dir;
+		out->ro = ro;
+		r = 0;
 	}
 	sys_fs_exit();
 	return r;

+ 7 - 0
vfs.c

@@ -390,6 +390,13 @@ static const struct fs_ops fat_ops = {
 	.list_dir = fat_op_list_dir,
 };
 
+/* 1 for a block-device-backed mount (the FAT driver).  A destructive format
+ * rewrites such a mount; RAM-backed mounts stay usable while it runs. */
+int vfs_mount_is_disk(const vfs_mount_t *m)
+{
+	return m && m->ops == &fat_ops;
+}
+
 /* ---- ramfs implementation ---- */
 
 static int ramfs_op_read_file(struct vfs_mount *m, const char *path,

+ 5 - 0
vfs.h

@@ -129,4 +129,9 @@ int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_
  * known block device (i.e. it exists but has no mounted filesystem). */
 int vfs_device_known(const char *path);
 
+/* 1 when the mount is backed by a block device (the FAT driver), 0 for the
+ * RAM-backed ramfs/share mounts.  The file service uses this to block only
+ * disk filesystems while a destructive format is running. */
+int vfs_mount_is_disk(const vfs_mount_t *m);
+
 #endif