Explorar el Código

fs: keep raw block reads preemptible

SYS_BLK_READ (the Disks app's partition view) ran in the non-preemptible
filesystem section and busy-waited on a stuck USB disk while holding the
BKL, so the whole desktop stopped until the device timeout expired.

- Publish the raw read as a scan-owned section without preempt_disable():
  disk file operations still wait, but the USB HCD waits can sleep/yield
  and drop the BKL.
- Mark BLK_USB raw reads as a best-effort probe (one 5 s attempt) instead
  of the full reset/retry chain.
Johannes Zellner hace 2 días
padre
commit
2ee880a4fc
Se han modificado 4 ficheros con 38 adiciones y 11 borrados
  1. 7 1
      AGENTS.md
  2. 1 1
      docs/filesystems.html
  3. 18 3
      kernel/sys_fs.c
  4. 12 6
      kernel/usbmsc.c

+ 7 - 1
AGENTS.md

@@ -803,7 +803,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   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
+  the PIT keeps running) while a command reads or writes a disk.  A raw
+  sector read (`SYS_BLK_READ`, the Disks app's partition view) publishes
+  itself as a scan-owned section but does not disable preemption: disk file
+  operations wait, while the USB HCD waits can sleep/yield, so a stuck USB
+  disk cannot freeze the WM.  A `BLK_USB` raw read also sets
+  `usbmsc_set_probe_only(1)` for a single 5 s attempt instead of the full
+  reset/retry chain.  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;

+ 1 - 1
docs/filesystems.html

@@ -166,7 +166,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 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.  A rescan (the <code>bootscan</code> thread or the <code>storage</code> command) re-adds <code>/ramfs</code>/<code>/share</code> first and is skipped by their operations too (<code>vfs_mount_is_ram</code>); it runs preemptible, and the floppy/ATA/AHCI/SDHCI wait loops (including the SDHCI command and CMD1 power-up polls) call <code>vfs_scan_poll</code>/<code>vfs_scan_relax</code>, which sleep (dropping the BKL) once a wait passes 2 ms and only for the scan's owning process.  A USB disk read by the rescan claims the USB bus (<code>usb_bus_claim()</code>), so its host-controller waits yield too; <code>storage</code> no longer freezes the WM.  The floppy probe is skipped for a drive whose CMOS type (register <code>0x10</code>) is 0.</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.  A rescan (the <code>bootscan</code> thread or the <code>storage</code> command) re-adds <code>/ramfs</code>/<code>/share</code> first and is skipped by their operations too (<code>vfs_mount_is_ram</code>); it runs preemptible, and the floppy/ATA/AHCI/SDHCI wait loops (including the SDHCI command and CMD1 power-up polls) call <code>vfs_scan_poll</code>/<code>vfs_scan_relax</code>, which sleep (dropping the BKL) once a wait passes 2 ms and only for the scan's owning process.  A USB disk read by the rescan claims the USB bus (<code>usb_bus_claim()</code>), so its host-controller waits yield too; <code>storage</code> no longer freezes the WM.  A raw-sector read (<code>SYS_BLK_READ</code>, the Disks partition view) takes a scan-owned but preemptible section: disk file operations wait, while the USB waits yield and a USB read is best-effort (one 5&nbsp;s attempt), so an unresponsive stick cannot freeze the WM.  The floppy probe is skipped for a drive whose CMOS type (register <code>0x10</code>) is 0.</li>
 			<li><strong>Mount scan.</strong> <code>mount_device()</code> recognises the NebOS MBR layout (<code>mbr_is_nebos_layout()</code>: active type-0x06 P1 at LBA 63 of 1024 sectors, P2 at LBA 1088) and skips P1 without reading it, so the boot stick costs only its MBR read. A valid MBR whose partitions are all non-FAT is not probed as a superfloppy, and an unreadable partition boot sector is skipped rather than mounted as FAT. The boot scan calls <code>vfs_mount_all(1)</code>, which logs USB devices as <code>deferred</code>; <code>vfs_mount_usb()</code> appends them after the bootscan window has closed, so a card reader that NAKs its first read for seconds cannot delay the other mounts or the desktop. The background pass is best-effort: one 5 s first read without the reset/retry chain (<code>usbmsc_set_probe_only</code>). The <code>storage</code> command's rescan still does the full scan (<code>vfs_mount_all(0)</code>) with the normal 15 s first-read deadline and retries.</li>
 		</ul>
 		<p>

+ 18 - 3
kernel/sys_fs.c

@@ -22,6 +22,7 @@
 #include "kmalloc.h"
 #include "paging.h"
 #include "process.h"
+#include "usbmsc.h"
 #include "vfs.h"
 
 /* Wait for a destructive format that owns the disks while its FAT is
@@ -326,16 +327,30 @@ int sys_blk_read(unsigned int index, unsigned int lba, void *buf)
 	if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)buf,
 				  sizeof(sec), 1))
 		return SYS_EINVAL;
-	sys_fs_enter();
+	fs_format_wait();
+	fs_scan_wait();
 	if (index >= blk_dev_count) {
-		sys_fs_exit();
+		__asm__ volatile ("cli");
 		return SYS_ENODEV;
 	}
 	d = &blk_devs[index];
+	/* A raw read does not touch the FAT driver's static buffers, and a USB
+	 * disk can make its HCD wait sleep.  Publish it like a scan (disk file
+	 * operations wait for it) but keep preemption enabled, so
+	 * usb_can_sleep()/vfs_scan_poll() can yield instead of holding the BKL
+	 * and preemption through a multi-second device timeout. */
+	vfs_scan_owner_pid = current_proc()->pid;
+	vfs_scan_busy = 1;
+	__asm__ volatile ("sti");
+	if (d->kind == BLK_USB)
+		usbmsc_set_probe_only(1);
 	r = SYS_EIO;
 	if (d->present && lba < d->total_sectors && blk_read(d, lba, sec, 1) == 0)
 		r = 0;
-	sys_fs_exit();
+	if (d->kind == BLK_USB)
+		usbmsc_set_probe_only(0);
+	__asm__ volatile ("cli");
+	vfs_scan_busy = 0;
 	if (r == 0)
 		for (i = 0; i < sizeof(sec); i++)
 			((unsigned char *)buf)[i] = sec[i];

+ 12 - 6
kernel/usbmsc.c

@@ -297,12 +297,12 @@ static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
 			    unsigned int count, void *buf)
 {
 	unsigned int i, t0 = get_ticks_ms();
-	int probe_only = msc_probe_only && m->first_read;
+	int probe_only = msc_probe_only;
 
 	for (i = 0; i < 3; i++) {
 		int r;
 
-		if (i == 0 && m->first_read)
+		if (i == 0 && (m->first_read || probe_only))
 			m->timeout_ms = probe_only ? MSC_PROBE_MS :
 						    MSC_FIRST_READ_MS;
 		r = msc_read10(m, lba, count, buf);
@@ -318,12 +318,16 @@ static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
 				serial_puts(SERIAL_COM1, " ms\n");
 			}
 		}
-		if (r == 0)
+		if (r == 0) {
+			if (probe_only)
+				m->timeout_ms = MSC_TIMEOUT_MS;
 			return 0;
+		}
 		if (probe_only) {
-			/* Boot background scan: do not spend the reset and
-			 * retry chain on a device that did not answer its
-			 * first read.  `storage` will try the full path. */
+			/* Best-effort probe: do not spend the reset and retry
+			 * chain on a device that did not answer.  `storage`
+			 * will try the full path. */
+			m->timeout_ms = MSC_TIMEOUT_MS;
 			return -1;
 		}
 		/* Only a completed command with a non-zero CSW status has
@@ -339,6 +343,8 @@ static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
 			msc_reconfigure(m);
 		msc_delay(10);
 	}
+	if (probe_only)
+		m->timeout_ms = MSC_TIMEOUT_MS;
 	return -1;
 }