[PATCH 2/3] serial: 8250_mxpcie: fail the probe when a port cannot be registered

Linmao Li <[email protected]>
Newsgroups gmane.linux.serial,gmane.linux.kernel
Message-ID <[email protected]>
When serial8250_register_8250_port() fails the loop only breaks out and
the probe still returns success.  The entries it did not reach keep the
zero devm_kzalloc() left there, and the entry that failed keeps a
negative error code.

mxpcie8250_remove() then feeds all of them to
serial8250_unregister_port(), which checks neither: line 0 unregisters a
port this driver does not own, and the negative line indexes
serial8250_ports[] out of bounds.

Fail the probe instead, unregistering the ports registered so far.

Fixes: 0481a041e956 ("serial: 8250: split Moxa PCIe serial board support out of 8250_pci")
Signed-off-by: Linmao Li <[email protected]>
---
 drivers/tty/serial/8250/8250_mxpcie.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_mxpcie.c b/drivers/tty/serial/8250/8250_mxpcie.c
index c0e3517d4e4ec..ddd02d5622ec4 100644
--- a/drivers/tty/serial/8250/8250_mxpcie.c
+++ b/drivers/tty/serial/8250/8250_mxpcie.c
@@ -570,14 +570,18 @@ static int mxpcie8250_probe(struct pci_dev *pdev, const struct pci_device_id *id
 		dev_dbg(dev, "Setup PCI port: port %lx, irq %d, type %d\n",
 			up.port.iobase, up.port.irq, up.port.iotype);
 
-		priv->port[i].line = serial8250_register_8250_port(&up);
-		if (priv->port[i].line < 0) {
+		ret = serial8250_register_8250_port(&up);
+		if (ret < 0) {
 			dev_err(dev,
 				"Couldn't register serial port %lx, irq %d, type %d, error %d\n",
 				up.port.iobase, up.port.irq,
-				up.port.iotype, priv->port[i].line);
-			break;
+				up.port.iotype, ret);
+			while (i--)
+				serial8250_unregister_port(priv->port[i].line);
+
+			return ret;
 		}
+		priv->port[i].line = ret;
 		priv->port[i].rx_trig_level = MOXA_PUART_RX_TRIG_DEFAULT;
 	}
 
-- 
2.25.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.