Module Name:    src
Committed By:   jmcneill
Date:           Mon Jun 21 21:18:47 UTC 2021

Modified Files:
        src/sys/stand/efiboot: boot.c efiblock.c efiblock.h

Log Message:
efiboot: Add readahead support.

Reading data through libsa file-systems ends up breaking block I/O
accesses into very small (512-byte or 2048-byte) accesses. This can be
very inefficient, and causes Ampere eMAG w/ BMC image direction to take
_minutes_ to load the install image and kernel. So slow in fact that
the default watchdog timeout will fire before it finishes.

So, when loading big files, optimistically read ahead up to 64KB of data.
Brings the time to boot the install ISO down to around 40 seconds -- still
not ideal but way better than before.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/sys/stand/efiboot/boot.c
cvs rdiff -u -r1.13 -r1.14 src/sys/stand/efiboot/efiblock.c
cvs rdiff -u -r1.5 -r1.6 src/sys/stand/efiboot/efiblock.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/stand/efiboot/boot.c
diff -u src/sys/stand/efiboot/boot.c:1.31 src/sys/stand/efiboot/boot.c:1.32
--- src/sys/stand/efiboot/boot.c:1.31	Mon Jun 21 19:07:30 2021
+++ src/sys/stand/efiboot/boot.c	Mon Jun 21 21:18:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: boot.c,v 1.31 2021/06/21 19:07:30 nia Exp $	*/
+/*	$NetBSD: boot.c,v 1.32 2021/06/21 21:18:47 jmcneill Exp $	*/
 
 /*-
  * Copyright (c) 2016 Kimihiro Nonaka <non...@netbsd.org>
@@ -171,7 +171,9 @@ command_boot(char *arg)
 	if (!*bootargs)
 		bootargs = netbsd_args;
 
+	efi_block_set_readahead(true);
 	exec_netbsd(kernel, bootargs);
+	efi_block_set_readahead(false);
 }
 
 void
@@ -498,7 +500,9 @@ boot(void)
 		if (c != '\r' && c != '\n' && c != '\0')
 			bootprompt(); /* does not return */
 
+		efi_block_set_readahead(true);
 		exec_netbsd(netbsd_path, netbsd_args);
+		efi_block_set_readahead(false);
 	}
 
 	bootprompt();	/* does not return */

Index: src/sys/stand/efiboot/efiblock.c
diff -u src/sys/stand/efiboot/efiblock.c:1.13 src/sys/stand/efiboot/efiblock.c:1.14
--- src/sys/stand/efiboot/efiblock.c:1.13	Mon Jun 21 11:11:33 2021
+++ src/sys/stand/efiboot/efiblock.c	Mon Jun 21 21:18:47 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: efiblock.c,v 1.13 2021/06/21 11:11:33 jmcneill Exp $ */
+/* $NetBSD: efiblock.c,v 1.14 2021/06/21 21:18:47 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2016 Kimihiro Nonaka <non...@netbsd.org>
@@ -38,6 +38,7 @@
 #include "efiboot.h"
 #include "efiblock.h"
 
+#define	EFI_BLOCK_READAHEAD	(64 * 1024)
 #define	EFI_BLOCK_TIMEOUT	120
 #define	EFI_BLOCK_TIMEOUT_CODE	0x810c0000
 
@@ -52,6 +53,12 @@ static EFI_HANDLE *efi_block;
 static UINTN efi_nblock;
 static struct efi_block_part *efi_block_booted = NULL;
 
+static bool efi_ra_enable = false;
+static UINT8 *efi_ra_buffer = NULL;
+static UINT32 efi_ra_media_id;
+static UINT64 efi_ra_start = 0;
+static UINT64 efi_ra_length = 0;
+
 static TAILQ_HEAD(, efi_block_dev) efi_block_devs = TAILQ_HEAD_INITIALIZER(efi_block_devs);
 
 static int
@@ -111,9 +118,51 @@ efi_block_generate_hash_mbr(struct efi_b
 }
 
 static EFI_STATUS
+efi_block_disk_readahead(struct efi_block_dev *bdev, UINT64 off, void *buf,
+    UINTN bufsize)
+{
+	EFI_STATUS status;
+	UINT64 mediasize, len;
+
+	if (efi_ra_buffer == NULL) {
+		efi_ra_buffer = AllocatePool(EFI_BLOCK_READAHEAD);
+		if (efi_ra_buffer == NULL) {
+			return EFI_OUT_OF_RESOURCES;
+		}
+	}
+
+	if (bdev->media_id != efi_ra_media_id ||
+	    off < efi_ra_start ||
+	    off + bufsize > efi_ra_start + efi_ra_length) {
+		mediasize = bdev->bio->Media->BlockSize *
+		    (bdev->bio->Media->LastBlock + 1);
+		len = EFI_BLOCK_READAHEAD;
+		if (len > mediasize - off) {
+			len = mediasize - off;
+		}
+		status = uefi_call_wrapper(bdev->dio->ReadDisk, 5, bdev->dio,
+		    bdev->media_id, off, len, efi_ra_buffer);
+		if (EFI_ERROR(status)) {
+			efi_ra_start = efi_ra_length = 0;
+			return status;
+		}
+		efi_ra_start = off;
+		efi_ra_length = len;
+		efi_ra_media_id = bdev->media_id;
+	}
+
+	memcpy(buf, &efi_ra_buffer[off - efi_ra_start], bufsize);
+	return EFI_SUCCESS;
+}
+
+static EFI_STATUS
 efi_block_disk_read(struct efi_block_dev *bdev, UINT64 off, void *buf,
     UINTN bufsize)
 {
+	if (efi_ra_enable) {
+		return efi_block_disk_readahead(bdev, off, buf, bufsize);
+	}
+
 	return uefi_call_wrapper(bdev->dio->ReadDisk, 5, bdev->dio,
 	    bdev->media_id, off, bufsize, buf);
 }
@@ -588,3 +637,9 @@ efi_block_strategy(void *devdata, int rw
 
 	return 0;
 }
+
+void
+efi_block_set_readahead(bool onoff)
+{
+	efi_ra_enable = onoff;
+}

Index: src/sys/stand/efiboot/efiblock.h
diff -u src/sys/stand/efiboot/efiblock.h:1.5 src/sys/stand/efiboot/efiblock.h:1.6
--- src/sys/stand/efiboot/efiblock.h:1.5	Mon Jun 21 11:11:33 2021
+++ src/sys/stand/efiboot/efiblock.h	Mon Jun 21 21:18:47 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: efiblock.h,v 1.5 2021/06/21 11:11:33 jmcneill Exp $ */
+/* $NetBSD: efiblock.h,v 1.6 2021/06/21 21:18:47 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcne...@invisible.ca>
@@ -80,3 +80,5 @@ struct efi_block_part *efi_block_boot_pa
 int efi_block_open(struct open_file *, ...);
 int efi_block_close(struct open_file *);
 int efi_block_strategy(void *, int, daddr_t, size_t, void *, size_t *);
+
+void efi_block_set_readahead(bool);

Reply via email to