Skip to contentSkip to conversation

xen/drivers: ns16550: Fix the return logic for pci_uart_config()

PATCH In review Ayan Kumar Halder 3 participants 4 messages Jul 7, 2023 – Jul 11, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/drivers: ns16550: Fix the return logic for pci_uart_config()

pci_uart_config() should return 0 when it probes the correct uart device and sets the properties of "struct ns16550". Else, it should return -ENODEV. Also before returning -ENODEV, it should restore the value of uart->io_base.

The callers should check if pci_uart_config() has returned 0 (for success) or not.

---
This was based on a discussion "[XEN v4 04/11] xen/drivers: ns16550: Use paddr_t for io_base/io_size"

https://patchew.org/Xen/[email protected]/[email protected]/

 xen/drivers/char/ns16550.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
xen/drivers/char/ns16550.c +11 -11
index 212a9c49ae..5a35498a06 100644
@@ -1342,13 +1342,9 @@ pci_uart_config(struct ns16550 *uart, bool_t skip_amt, unsigned int idx)
         }
     }
 
-    if ( !skip_amt )
-        return -1;
-
-    /* No AMT found, fallback to the defaults. */
     uart->io_base = orig_base;
 
-    return 0;
+    return -ENODEV;
 }
 
 static void enable_exar_enhanced_bits(const struct ns16550 *uart)
@@ -1527,13 +1523,13 @@ static bool __init parse_positional(struct ns16550 *uart, char **str)
 #ifdef CONFIG_HAS_PCI
         if ( strncmp(conf, "pci", 3) == 0 )
         {
-            if ( pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
+            if ( !pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
                 return true;
             conf += 3;
         }
         else if ( strncmp(conf, "amt", 3) == 0 )
         {
-            if ( pci_uart_config(uart, 0, uart - ns16550_com) )
+            if ( !pci_uart_config(uart, 0, uart - ns16550_com) )
                 return true;
             conf += 3;
         }
@@ -1642,13 +1638,17 @@ static bool __init parse_namevalue_pairs(char *str, struct ns16550 *uart)
         case device:
             if ( strncmp(param_value, "pci", 3) == 0 )
             {
-                pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com);
-                dev_set = true;
+                if ( !pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
+                    dev_set = true;
+                else
+                    return false;
             }
             else if ( strncmp(param_value, "amt", 3) == 0 )
             {
-                pci_uart_config(uart, 0, uart - ns16550_com);
-                dev_set = true;
+                if ( !pci_uart_config(uart, 0, uart - ns16550_com) )
+                    dev_set = true;
+                else
+                    return false;
             }
             break;
 
Signature · 1 line
2.25.1
#1 lore raw

xen/drivers/char/ns16550.c pci_uart_config(struct ns16550 *uart, bool_t skip_amt, unsigned int idx) · line 1346

Quoted 8 lines · Ayan Kumar Halder
xen/drivers/char/ns16550.c +0 -2
@@ -1342,13 +1342,9 @@ pci_uart_config(struct ns16550 *uart, bool_t skip_amt, unsigned int idx)
         }
     }
 
-    if ( !skip_amt )
-        return -1;

This special case probably needs retaining in the new model (with an altered return value), such that ...

Quoted 14 lines
xen/drivers/char/ns16550.c +2 -3
-    /* No AMT found, fallback to the defaults. */
     uart->io_base = orig_base;
 
-    return 0;
+    return -ENODEV;
 }
 
 static void enable_exar_enhanced_bits(const struct ns16550 *uart)
@@ -1527,13 +1523,13 @@ static bool __init parse_positional(struct ns16550 *uart, char **str)
 #ifdef CONFIG_HAS_PCI
         if ( strncmp(conf, "pci", 3) == 0 )
         {
-            if ( pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
+            if ( !pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )

xen/drivers/char/ns16550.c static bool __init parse_positional(struct ns16550 *uart, char **str) · line 1535

Quoted 10 lines
xen/drivers/char/ns16550.c +1 -1
                 return true;
             conf += 3;
         }
         else if ( strncmp(conf, "amt", 3) == 0 )
         {
-            if ( pci_uart_config(uart, 0, uart - ns16550_com) )
+            if ( !pci_uart_config(uart, 0, uart - ns16550_com) )
                 return true;
             conf += 3;
         }

... e.g. here you don't suddenly change behavior in unintended ways. Prior to your change the earlier of the return paths was impossible to be taken. That's likely wrong, but you now returning in the success case can't be correct either: Further items may need parsing, first and foremost the IRQ to use.

Jan

Quoted 22 lines
xen/drivers/char/ns16550.c +8 -4
@@ -1642,13 +1638,17 @@ static bool __init parse_namevalue_pairs(char *str, struct ns16550 *uart)
         case device:
             if ( strncmp(param_value, "pci", 3) == 0 )
             {
-                pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com);
-                dev_set = true;
+                if ( !pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
+                    dev_set = true;
+                else
+                    return false;
             }
             else if ( strncmp(param_value, "amt", 3) == 0 )
             {
-                pci_uart_config(uart, 0, uart - ns16550_com);
-                dev_set = true;
+                if ( !pci_uart_config(uart, 0, uart - ns16550_com) )
+                    dev_set = true;
+                else
+                    return false;
             }
             break;
 
#2 lore raw

Hi Jan,

Quoted 11 lines · Jan Beulich

xen/drivers/char/ns16550.c pci_uart_config(struct ns16550 *uart, bool_t skip_amt, unsigned int idx) · line 1346

Quoted 8 lines · Ayan Kumar Halder
xen/drivers/char/ns16550.c +0 -2
@@ -1342,13 +1342,9 @@ pci_uart_config(struct ns16550 *uart, bool_t skip_amt, unsigned int idx)
          }
      }
 
-    if ( !skip_amt )
-        return -1;

This special case probably needs retaining in the new model (with an altered return value), such that ...

Does this look correct ?

     if ( !skip_amt )
-        return -1;
+        return -EINVAL;

Quoted 21 lines
Quoted 21 lines
xen/drivers/char/ns16550.c +2 -4
-    /* No AMT found, fallback to the defaults. */
      uart->io_base = orig_base;
 
-    return 0;
+    return -ENODEV;
  }
 
  static void enable_exar_enhanced_bits(const struct ns16550 *uart)
@@ -1527,13 +1523,13 @@ static bool __init parse_positional(struct ns16550 *uart, char **str)
  #ifdef CONFIG_HAS_PCI
          if ( strncmp(conf, "pci", 3) == 0 )
          {
-            if ( pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
+            if ( !pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
                  return true;
              conf += 3;
          }
          else if ( strncmp(conf, "amt", 3) == 0 )
          {
-            if ( pci_uart_config(uart, 0, uart - ns16550_com) )
Quoted 8 lines

xen/drivers/char/ns16550.c static bool __init parse_positional(struct ns16550 *uart, char **str) · line 1535

Quoted 4 lines
xen/drivers/char/ns16550.c +1 -0
+            if ( !pci_uart_config(uart, 0, uart - ns16550_com) )
                  return true;
              conf += 3;
          }

... e.g. here you don't suddenly change behavior in unintended ways. Prior to your change the earlier of the return paths was impossible to be taken. That's likely wrong, but you now returning in the success case can't be correct either:

I am afraid I don't follow your comments very well.

pci_uart_config() returns 0 for success. So we need to check "!(pci_uart_config(...)" to return true.

Quoted 2 lines

Further items may need parsing, first
and foremost the IRQ to use.

I see the irq is parsed here https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/drivers/char/ns16550.c;h=212a9c49ae8e5c40dc6cd05da07a6652c8405935;hb=refs/heads/master#l1558 .

Do we need to do something more ?

- Ayan

Quoted 25 lines

Jan

Quoted 22 lines
xen/drivers/char/ns16550.c +8 -4
@@ -1642,13 +1638,17 @@ static bool __init parse_namevalue_pairs(char *str, struct ns16550 *uart)
          case device:
              if ( strncmp(param_value, "pci", 3) == 0 )
              {
-                pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com);
-                dev_set = true;
+                if ( !pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
+                    dev_set = true;
+                else
+                    return false;
              }
              else if ( strncmp(param_value, "amt", 3) == 0 )
              {
-                pci_uart_config(uart, 0, uart - ns16550_com);
-                dev_set = true;
+                if ( !pci_uart_config(uart, 0, uart - ns16550_com) )
+                    dev_set = true;
+                else
+                    return false;
              }
              break;
 
#3 lore raw
Quoted 13 lines · Ayan Kumar Halder
Quoted 11 lines · Jan Beulich

xen/drivers/char/ns16550.c pci_uart_config(struct ns16550 *uart, bool_t skip_amt, unsigned int idx) · line 1346

Quoted 8 lines · Ayan Kumar Halder
xen/drivers/char/ns16550.c +0 -2
@@ -1342,13 +1342,9 @@ pci_uart_config(struct ns16550 *uart, bool_t skip_amt, unsigned int idx)
          }
      }
 
-    if ( !skip_amt )
-        return -1;

This special case probably needs retaining in the new model (with an altered return value), such that ...

Quoted 5 lines

Does this look correct ?

     if ( !skip_amt )
-        return -1;
+        return -EINVAL;

It's hard to say without seeing what else changes are done to the patch, but at the first glance this looks wrong. If you change the function along the lines of the initial patch, then likely this wants to be a positive return value (to tell "failure" from "success" as well as from this special case).

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
xen/drivers/char/ns16550.c +1 -2
@@ -1527,13 +1523,13 @@ static bool __init parse_positional(struct ns16550 *uart, char **str)
  #ifdef CONFIG_HAS_PCI
          if ( strncmp(conf, "pci", 3) == 0 )
          {
-            if ( pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
+            if ( !pci_uart_config(uart, 1/* skip AMT */, uart - ns16550_com) )
                  return true;
              conf += 3;
          }
          else if ( strncmp(conf, "amt", 3) == 0 )
          {
-            if ( pci_uart_config(uart, 0, uart - ns16550_com) )
Quoted 8 lines

xen/drivers/char/ns16550.c static bool __init parse_positional(struct ns16550 *uart, char **str) · line 1535

Quoted 4 lines
xen/drivers/char/ns16550.c +1 -0
+            if ( !pci_uart_config(uart, 0, uart - ns16550_com) )
                  return true;
              conf += 3;
          }

... e.g. here you don't suddenly change behavior in unintended ways. Prior to your change the earlier of the return paths was impossible to be taken. That's likely wrong, but you now returning in the success case can't be correct either:

Quoted 3 lines

I am afraid I don't follow your comments very well.

pci_uart_config() returns 0 for success. So we need to check "!(pci_uart_config(...)" to return true.

But you cannot return here in the success case. You need to acknowledge that in the original code a kind-of-error indication from the function is converted to a success return here (which was impossible to happen in one of the two cases, in turn causing extra confusion). So changing this is a two-step process: First it needs to be understood what the behavior ought to be at each of the four call sites. That behavior likely isn't going to be the same in all four cases. And then this needs transforming into the intended code change.

Jan