[PATCH v2] soc: aspeed: move switch statements before resource allocation and free kfifo on error

Joe Hattori joe at pf.is.s.u-tokyo.ac.jp
Wed Dec 11 14:49:08 AEDT 2024


The switch statement in aspeed_lpc_enable_snoop() only depends on the
function argument channel, thus move it prior to any resource
acquisition. Also free the kfifo when misc_register() fails.

Fixes: 524feb799408 ("soc: add aspeed folder and misc drivers")
Signed-off-by: Joe Hattori <joe at pf.is.s.u-tokyo.ac.jp>
---
Changes in V2:
- Move the switch statement before any resource allocation happens.
- Free the kfifo before returning an error.
---
 drivers/soc/aspeed/aspeed-lpc-snoop.c | 34 ++++++++++++++-------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
index 9ab5ba9cf1d6..de308bc789ed 100644
--- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
+++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
@@ -190,22 +190,6 @@ static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
 	const struct aspeed_lpc_snoop_model_data *model_data =
 		of_device_get_match_data(dev);
 
-	init_waitqueue_head(&lpc_snoop->chan[channel].wq);
-	/* Create FIFO datastructure */
-	rc = kfifo_alloc(&lpc_snoop->chan[channel].fifo,
-			 SNOOP_FIFO_SIZE, GFP_KERNEL);
-	if (rc)
-		return rc;
-
-	lpc_snoop->chan[channel].miscdev.minor = MISC_DYNAMIC_MINOR;
-	lpc_snoop->chan[channel].miscdev.name =
-		devm_kasprintf(dev, GFP_KERNEL, "%s%d", DEVICE_NAME, channel);
-	lpc_snoop->chan[channel].miscdev.fops = &snoop_fops;
-	lpc_snoop->chan[channel].miscdev.parent = dev;
-	rc = misc_register(&lpc_snoop->chan[channel].miscdev);
-	if (rc)
-		return rc;
-
 	/* Enable LPC snoop channel at requested port */
 	switch (channel) {
 	case 0:
@@ -224,6 +208,24 @@ static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
 		return -EINVAL;
 	}
 
+	init_waitqueue_head(&lpc_snoop->chan[channel].wq);
+	/* Create FIFO datastructure */
+	rc = kfifo_alloc(&lpc_snoop->chan[channel].fifo,
+			 SNOOP_FIFO_SIZE, GFP_KERNEL);
+	if (rc)
+		return rc;
+
+	lpc_snoop->chan[channel].miscdev.minor = MISC_DYNAMIC_MINOR;
+	lpc_snoop->chan[channel].miscdev.name =
+		devm_kasprintf(dev, GFP_KERNEL, "%s%d", DEVICE_NAME, channel);
+	lpc_snoop->chan[channel].miscdev.fops = &snoop_fops;
+	lpc_snoop->chan[channel].miscdev.parent = dev;
+	rc = misc_register(&lpc_snoop->chan[channel].miscdev);
+	if (rc) {
+		kfifo_free(&lpc_snoop->chan[channel].fifo);
+		return rc;
+	}
+
 	regmap_update_bits(lpc_snoop->regmap, HICR5, hicr5_en, hicr5_en);
 	regmap_update_bits(lpc_snoop->regmap, SNPWADR, snpwadr_mask,
 			   lpc_port << snpwadr_shift);
-- 
2.34.1



More information about the Linux-aspeed mailing list