Skip to contentSkip to conversation

serial: Cleanup & fix port discovery

PATCH In review Benjamin Herrenschmidt 2 participants 7 messages Dec 23, 2022 – Jan 12, 2023

lore
Share & tools
lore
#0 lore raw

cover serial: Cleanup & fix port discovery

This series cleans up a few things in serial port discovery and fixes detection of duplicates in some cases:

- Existing usage of memcmp to compare strings is replaces with strncmp

- Don't lookup ports by name right after creating them, just return the pointer instead.

- Move the detection of duplicates *into* the various port creation functions. At this point, the various attributes such as port number and address have been properly parsed and there is no confusion between things like wether a 0x prefix is present or not for example, or wether hex digits are upper or lower case.

#1 lore raw

PATCH 1/3 serial: Replace usage of memcmp with strncmp

We are comparing strings after all

---
 grub-core/term/serial.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/term/serial.c +2 -2
index d161e1d36..88bcd4032 100644
@@ -152,7 +152,7 @@ grub_serial_find (const char *name)
       break;
 
 #if (defined(__mips__) || defined (__i386__) || defined (__x86_64__)) && !defined(GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC)
-  if (!port && grub_memcmp (name, "port", sizeof ("port") - 1) == 0
+  if (!port && grub_strncmp (name, "port", sizeof ("port") - 1) == 0
       && grub_isxdigit (name [sizeof ("port") - 1]))
     {
       name = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
@@ -242,7 +242,7 @@ grub_serial_find (const char *name)
 #endif
 
 #ifdef GRUB_MACHINE_IEEE1275
-  if (!port && grub_memcmp (name, "ieee1275/", sizeof ("ieee1275/") - 1) == 0)
+  if (!port && grub_strncmp (name, "ieee1275/", sizeof ("ieee1275/") - 1) == 0)
     {
       name = grub_ofserial_add_port (&name[sizeof ("ieee1275/") - 1]);
       if (!name)
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/3 serial: Avoid double lookup of serial ports

In reply to Benjamin Herrenschmidt

The various functions to add a port used to return port->name, and the callers would immediately iterate all registered ports to "find" the one just created by comparing that return value with ... port->name.

This is a waste of cycles and code. Instead, have those functions return "port" directly.

---
 grub-core/term/arc/serial.c      |  4 ++--
 grub-core/term/ieee1275/serial.c |  6 ++---
 grub-core/term/ns8250-spcr.c     |  2 +-
 grub-core/term/ns8250.c          | 12 +++++-----
 grub-core/term/serial.c          | 41 +++++++++++---------------------
 include/grub/serial.h            | 13 +++++-----
 6 files changed, 33 insertions(+), 45 deletions(-)
grub-core/term/arc/serial.c +2 -2
index 87d1ce821..651f814ee 100644
@@ -100,7 +100,7 @@ struct grub_serial_driver grub_arcserial_driver =
     .put = serial_hw_put
   };
 
-const char *
+struct grub_serial_port *
 grub_arcserial_add_port (const char *path)
 {
   struct grub_serial_port *port;
@@ -120,7 +120,7 @@ grub_arcserial_add_port (const char *path)
 
   grub_serial_register (port);
 
-  return port->name;
+  return port;
 }
 
 static int
grub-core/term/ieee1275/serial.c +3 -3
index 7df2bee6f..b4aa9d5c5 100644
@@ -217,7 +217,7 @@ dev_iterate (struct grub_ieee1275_devalias *alias)
   return 0;
 }
 
-static const char *
+static struct grub_serial_port *
 add_port (struct ofserial_hash_ent *ent)
 {
   struct grub_serial_port *port;
@@ -245,10 +245,10 @@ add_port (struct ofserial_hash_ent *ent)
 
   grub_serial_register (port);
 
-  return port->name;
+  return port;
 }
 
-const char *
+const struct grub_serial_port *
 grub_ofserial_add_port (const char *path)
 {
   struct ofserial_hash_ent *ent;
grub-core/term/ns8250-spcr.c +1 -1
index f4b718833..9fe36733d 100644
@@ -22,7 +22,7 @@
 #include <grub/dl.h>
 #include <grub/acpi.h>
 
-char *
+struct grub_serial_port *
 grub_ns8250_spcr_init (void)
 {
   struct grub_acpi_spcr *spcr;
grub-core/term/ns8250.c +6 -6
index 98f0b3bc3..1eb6a30b6 100644
@@ -347,7 +347,7 @@ grub_ns8250_hw_get_port (const unsigned int unit)
     return 0;
 }
 
-char *
+struct grub_serial_port *
 grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config)
 {
   struct grub_serial_port *p;
@@ -361,7 +361,7 @@ grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config
         /* give the opportunity for SPCR to configure a default com port */
         if (config != NULL)
           grub_serial_port_configure (&com_ports[i], config);
-        return com_names[i];
+        return &com_ports[i];
       }
 
   grub_outb (0x5a, port + UART_SR);
@@ -391,10 +391,10 @@ grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config
     grub_serial_config_defaults (p);
   grub_serial_register (p);
 
-  return p->name;
+  return p;
 }
 
-char *
+struct grub_serial_port *
 grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
                              struct grub_serial_config *config)
 {
@@ -406,7 +406,7 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
       {
         if (config != NULL)
           grub_serial_port_configure (&com_ports[i], config);
-        return com_names[i];
+        return &com_ports[i];
       }
 
   p = grub_malloc (sizeof (*p));
@@ -428,5 +428,5 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
     grub_serial_config_defaults (p);
   grub_serial_register (p);
 
-  return p->name;
+  return p;
 }
grub-core/term/serial.c +14 -27

66 lines not on this page · raw message

include/grub/serial.h +7 -6
index 65ccab4ff..17ee0f08b 100644
@@ -190,10 +190,12 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
 void grub_ns8250_init (void);
-char *grub_ns8250_spcr_init (void);
-char *grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config);
-char *grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
-                                   struct grub_serial_config *config);
+struct grub_serial_port *grub_ns8250_spcr_init (void);
+struct grub_serial_port *grub_serial_ns8250_add_port (grub_port_t port,
+						      struct grub_serial_config *config);
+struct grub_serial_port *grub_serial_ns8250_add_mmio (grub_addr_t addr,
+						      unsigned int acc_size,
+						      struct grub_serial_config *config);
 #endif
 #ifdef GRUB_MACHINE_IEEE1275
 void grub_ofserial_init (void);
@@ -205,8 +207,7 @@ grub_efiserial_init (void);
 #ifdef GRUB_MACHINE_ARC
 void
 grub_arcserial_init (void);
-const char *
-grub_arcserial_add_port (const char *path);
+struct grub_serial_port *grub_arcserial_add_port (const char *path);
 #endif
 
 struct grub_serial_port *grub_serial_find (const char *name);
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/3 serial: Improve detection of duplicate serial ports

In reply to Benjamin Herrenschmidt

We currently rely on some pretty fragile comparison by name to identify wether a serial port being configured is identical

---
 grub-core/term/arc/serial.c      |  4 +++
 grub-core/term/ieee1275/serial.c |  4 +++
 grub-core/term/ns8250.c          | 16 +++++++++
 grub-core/term/serial.c          | 57 +++++++++++++-------------------
 include/grub/serial.h            |  4 +++
 5 files changed, 51 insertions(+), 34 deletions(-)
grub-core/term/arc/serial.c +4 -0
index 651f814ee..487aa1b30 100644
@@ -106,6 +106,10 @@ grub_arcserial_add_port (const char *path)
   struct grub_serial_port *port;
   grub_err_t err;
 
+  FOR_SERIAL_PORTS (port)
+    if (grub_strcmp(path, port->name) == 0)
+      return port;
+
   port = grub_zalloc (sizeof (*port));
   if (!port)
     return NULL;
grub-core/term/ieee1275/serial.c +4 -0
index b4aa9d5c5..afbe8dcda 100644
@@ -227,6 +227,10 @@ add_port (struct ofserial_hash_ent *ent)
   if (!ent->shortest)
     return NULL;
 
+  FOR_SERIAL_PORTS (port)
+    if (port->ent == ent)
+      return port;
+
   port = grub_zalloc (sizeof (*port));
   if (!port)
     return NULL;
grub-core/term/ns8250.c +16 -0
index 1eb6a30b6..074bd08ca 100644
@@ -364,6 +364,14 @@ grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config
         return &com_ports[i];
       }
 
+  FOR_SERIAL_PORTS (p)
+    if (!p->mmio && p->port == port)
+      {
+        if (config != NULL)
+          grub_serial_port_configure (p, config);
+        return p;
+      }
+
   grub_outb (0x5a, port + UART_SR);
   if (grub_inb (port + UART_SR) != 0x5a)
     return NULL;
@@ -409,6 +417,14 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
         return &com_ports[i];
       }
 
+  FOR_SERIAL_PORTS (p)
+    if (p->mmio && p->mmio_base == addr)
+      {
+        if (config != NULL)
+          grub_serial_port_configure (p, config);
+        return p;
+      }
+
   p = grub_malloc (sizeof (*p));
   if (p == NULL)
     return NULL;
grub-core/term/serial.c +23 -34

109 lines not on this page · raw message

include/grub/serial.h +4 -0
index 17ee0f08b..ce3ec10ec 100644
@@ -219,4 +219,8 @@ extern void grub_serial_init (void);
 extern void grub_serial_fini (void);
 #endif
 
+extern struct grub_serial_port *grub_serial_ports;
+#define FOR_SERIAL_PORTS(var) FOR_LIST_ELEMENTS((var), (grub_serial_ports))
+
+
 #endif
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 3/3] serial: Improve detection of duplicate serial ports

Quoted 2 lines · Benjamin Herrenschmidt

We currently rely on some pretty fragile comparison by name to identify wether a serial port being configured is identical

---

(Sorry about that ... blame christmas :-)

Quoted 223 lines

---
 grub-core/term/arc/serial.c      |  4 +++ grub-core/term/ieee1275/serial.c |  4 +++ grub-core/term/ns8250.c          | 16 +++++++++ grub-core/term/serial.c          | 57 +++++++++++++----------------- --
 include/grub/serial.h            |  4 +++

 5 files changed, 51 insertions(+), 34 deletions(-)
grub- renamed from grub-core/term/arc/serial.c +0 -0

core/term/arc/serial.c
index 651f814ee..487aa1b30 100644

grub-core/term/arc/serial.c +0 -0
@@ -106,6 +106,10 @@ grub_arcserial_add_port (const char *path)
    struct grub_serial_port *port;
    grub_err_t err;


+  FOR_SERIAL_PORTS (port)
+    if (grub_strcmp(path, port->name) == 0)
+      return port;
+
   port = grub_zalloc (sizeof (*port));
   if (!port)
     return NULL;

grub- renamed from grub-core/term/ieee1275/serial.c +0 -0

core/term/ieee1275/serial.c
index b4aa9d5c5..afbe8dcda 100644

grub-core/term/ieee1275/serial.c +0 -0
@@ -227,6 +227,10 @@ add_port (struct ofserial_hash_ent *ent)
    if (!ent->shortest)
      return NULL;


+  FOR_SERIAL_PORTS (port)
+    if (port->ent == ent)
+      return port;
+
   port = grub_zalloc (sizeof (*port));
   if (!port)
     return NULL;

grub-core/term/ns8250.c +0 -0
index 1eb6a30b6..074bd08ca 100644
@@ -364,6 +364,14 @@ grub_serial_ns8250_add_port (grub_port_t port,

struct grub_serial_config *config
         return &com_ports[i];
       }

+  FOR_SERIAL_PORTS (p)
+    if (!p->mmio && p->port == port)
+      {
+        if (config != NULL)
+          grub_serial_port_configure (p, config); +        return p;
+      }
+
   grub_outb (0x5a, port + UART_SR);
   if (grub_inb (port + UART_SR) != 0x5a) return NULL;

grub-core/term/ns8250.c +0 -0
@@ -409,6 +417,14 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr,
 unsigned int acc_size,
          return &com_ports[i];
        }


+  FOR_SERIAL_PORTS (p)
+    if (p->mmio && p->mmio_base == addr) +      {
+        if (config != NULL)
+          grub_serial_port_configure (p, config); +        return p;
+      }
+
   p = grub_malloc (sizeof (*p));
   if (p == NULL)
     return NULL;

grub-core/term/serial.c +0 -0
index 275dd61af..e214d773b 100644
@@ -37,8 +37,6 @@


 GRUB_MOD_LICENSE ("GPLv3+");

-#define FOR_SERIAL_PORTS(var) FOR_LIST_ELEMENTS((var), (grub_serial_ports))
-
 enum
   {
     OPTION_UNIT,

grub-core/term/serial.c +0 -0
@@ -65,7 +63,7 @@ static const struct grub_arg_option options[] =
    {0, 0, 0, 0, 0, 0}
  };

grub-core/term/serial.c +1 -1
-static struct grub_serial_port *grub_serial_ports;
+struct grub_serial_port *grub_serial_ports;


 struct grub_serial_output_state
 {

grub-core/term/serial.c +0 -0
@@ -147,26 +145,30 @@ grub_serial_find (const char *name)
  {
    struct grub_serial_port *port;


+  /*
+   * First look for an exact match by name, this will take care of +   * things like "com0" which have already been created and that +   * this function cannot re-create.
+   */
   FOR_SERIAL_PORTS (port)
     if (grub_strcmp (port->name, name) == 0)
-      break;
+      return port;

 #if (defined(__mips__) || defined (__i386__) || defined (__x86_64__)) && !defined(GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC)
-  if (!port && grub_strncmp (name, "port", sizeof ("port") - 1) == 0 +  if (grub_strncmp (name, "port", sizeof ("port") - 1) == 0 && grub_isxdigit (name [sizeof ("port") - 1]))
     {
       port = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
                                                        0, 16), NULL);
-      if (port == NULL)
-        return NULL;
+      if (port != NULL)
+        return port;
     }
-  if (!port && grub_strncmp (name, "mmio,", sizeof ("mmio,") - 1) == 0
+  if (grub_strncmp (name, "mmio,", sizeof ("mmio,") - 1) == 0 && grub_isxdigit (name [sizeof ("mmio,") - 1]))
     {
       const char *p1, *p = &name[sizeof ("mmio,") - 1]; grub_addr_t addr = grub_strtoul (p, &p1, 16); unsigned int acc_size = 1;
-      unsigned int nlen = p1 - p;

       /*
        * If we reach here, we know there's a digit after "mmio,", so

grub-core/term/serial.c +0 -0
@@ -203,45 +205,32 @@ grub_serial_find (const char *name)

             grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Incorrect MMIO access size"));
           }

-      /*
-       * Specifying the access size is optional an grub_serial_ns8250_add_mmio()
-       * will not add it to the name. So the original loop trying to match an
-       * existing port above might have missed this one. Let's do another
-       * search ignoring the access size part of the string. At this point
-       * nlen contains the length of the name up to the end of the address.
-       */
-      FOR_SERIAL_PORTS (port)
-        if (grub_strncmp (port->name, name, nlen) == 0) { -          break;
-        }
-
       port = grub_serial_ns8250_add_mmio (addr, acc_size, NULL); -      if (port == NULL)
-        return NULL;
+      if (port != NULL)
+        return port;
     }
-  if (!port && grub_strcmp (name, "auto") == 0)
+  if (grub_strcmp (name, "auto") == 0)
     {
       /* Look for an SPCR if any. If not, default to com0 */ port = grub_ns8250_spcr_init ();
-      if (port == NULL)
-        {
-          FOR_SERIAL_PORTS (port)
-            if (grub_strcmp (port->name, "com0") == 0)
-              break;
-       }
+      if (port != NULL)
+        return port;
+      FOR_SERIAL_PORTS (port)
+        if (grub_strcmp (port->name, "com0") == 0)
+          return port;
     }
 #endif

 #ifdef GRUB_MACHINE_IEEE1275
-  if (!port && grub_strncmp (name, "ieee1275/", sizeof ("ieee1275/")
- 1) == 0)
+  if (grub_strncmp (name, "ieee1275/", sizeof ("ieee1275/") - 1) ==
0)
     {
       port = grub_ofserial_add_port (&name[sizeof ("ieee1275/") - 1]);
-      if (port == NULL)
-        return NULL;
+      if (port != NULL)
+        return port;
     }
 #endif

-  return port;
+  return NULL;
 }

 static grub_err_t

include/grub/serial.h +0 -0
index 17ee0f08b..ce3ec10ec 100644
@@ -219,4 +219,8 @@ extern void grub_serial_init (void);
  extern void grub_serial_fini (void);
  #endif

include/grub/serial.h +2 -0
+extern struct grub_serial_port *grub_serial_ports;
+#define FOR_SERIAL_PORTS(var) FOR_LIST_ELEMENTS((var),

(grub_serial_ports))

include/grub/serial.h +2 -0
+
+

 #endif

#5 lore raw
In reply to Benjamin Herrenschmidt
Quoted 9 lines · Benjamin Herrenschmidt

This series cleans up a few things in serial port discovery and fixes detection of duplicates in some cases:

- Existing usage of memcmp to compare strings is replaces with strncmp

- Don't lookup ports by name right after creating them, just return the pointer instead.

Quoted 6 lines

- Move the detection of duplicates *into* the various port creation functions. At this point, the various attributes such as port number and address have been properly parsed and there is no confusion between things like wether a 0x prefix is present or not for example, or wether hex digits are upper or lower case.

For all the patches Reviewed-by: Daniel Kiper <[email protected]>...

Thank you for fixing the issues!

Daniel

PS I will add missing SOB on your behalf to the last patch. I hope this
is not big deal...

#6 lore raw
Quoted 10 lines · Daniel Kiper

For all the patches Reviewed-by: Daniel Kiper
<[email protected]>...

Thank you for fixing the issues!

Daniel

PS I will add missing SOB on your behalf to the last patch. I hope this
   is not big deal...

ooops ! Let me reply to the patch with a s-o-b to make this more formal (And archived).

Cheers,
Ben.