Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/stand/efiboot efiboot: Add readahead support.



details:   https://anonhg.NetBSD.org/src/rev/567e7b1fd839
branches:  trunk
changeset: 379847:567e7b1fd839
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Mon Jun 21 21:18:47 2021 +0000

description:
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.

diffstat:

 sys/stand/efiboot/boot.c     |   6 +++-
 sys/stand/efiboot/efiblock.c |  57 +++++++++++++++++++++++++++++++++++++++++++-
 sys/stand/efiboot/efiblock.h |   4 ++-
 3 files changed, 64 insertions(+), 3 deletions(-)

diffs (136 lines):

diff -r 5d00892302c8 -r 567e7b1fd839 sys/stand/efiboot/boot.c
--- a/sys/stand/efiboot/boot.c  Mon Jun 21 21:10:01 2021 +0000
+++ b/sys/stand/efiboot/boot.c  Mon Jun 21 21:18:47 2021 +0000
@@ -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 <nonaka%netbsd.org@localhost>
@@ -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 */
diff -r 5d00892302c8 -r 567e7b1fd839 sys/stand/efiboot/efiblock.c
--- a/sys/stand/efiboot/efiblock.c      Mon Jun 21 21:10:01 2021 +0000
+++ b/sys/stand/efiboot/efiblock.c      Mon Jun 21 21:18:47 2021 +0000
@@ -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 <nonaka%netbsd.org@localhost>
@@ -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;
+}
diff -r 5d00892302c8 -r 567e7b1fd839 sys/stand/efiboot/efiblock.h
--- a/sys/stand/efiboot/efiblock.h      Mon Jun 21 21:10:01 2021 +0000
+++ b/sys/stand/efiboot/efiblock.h      Mon Jun 21 21:18:47 2021 +0000
@@ -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 <jmcneill%invisible.ca@localhost>
@@ -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);



Home | Main Index | Thread Index | Old Index