Skip to contentSkip to conversation

hwmon: xgene: Fix crash when alarm occurs before driver probe

PATCH Superseded Hoan Tran 2 participants 9 messages Sep 7, 2016 Hardware monitoring

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hoan Tran

author
#0 lore raw

PATCH 1/1 hwmon: xgene: Fix crash when alarm occurs before driver probe

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes when accesses
into invalid resource as unmapped PCC shared memory.

This patch fixes this issue by saving this alarm message and scheduling a bottom handler after xgene_hwmon_probe() finish.

---
 drivers/hwmon/xgene-hwmon.c | 75 +++++++++++++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 19 deletions(-)
drivers/hwmon/xgene-hwmon.c +56 -19
index bc78a5d..e3b4e84 100644
@@ -107,6 +107,8 @@ struct xgene_hwmon_dev {
 	struct completion	rd_complete;
 	int			resp_pending;
 	struct slimpro_resp_msg sync_msg;
+	bool			init_flag;
+	bool			rx_pending;
 
 	struct work_struct	workq;
 	struct kfifo_rec_ptr_1	async_msg_fifo;
@@ -465,13 +467,35 @@ static void xgene_hwmon_evt_work(struct work_struct *work)
 	}
 }
 
+static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
+{
+	if (!ctx->init_flag) {
+		ctx->rx_pending = true;
+		/* Enqueue to the FIFO */
+		kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
+				    sizeof(struct slimpro_resp_msg),
+				    &ctx->kfifo_lock);
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
 /*
  * This function is called when the SLIMpro Mailbox received a message
  */
 static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
 {
 	struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
-	struct slimpro_resp_msg amsg;
+
+	/*
+	 * While the driver registers with the mailbox framework, an interrupt
+	 * can be pending before the probe function completes its
+	 * initialization. If such condition occurs, just queue up the message
+	 * as the driver is not ready for servicing the callback.
+	 */
+	if (xgene_hwmon_rx_ready(ctx, msg) < 0)
+		return;
 
 	/*
 	 * Response message format:
@@ -500,12 +524,8 @@ static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
 		return;
 	}
 
-	amsg.msg   = ((u32 *)msg)[0];
-	amsg.param1 = ((u32 *)msg)[1];
-	amsg.param2 = ((u32 *)msg)[2];
-
 	/* Enqueue to the FIFO */
-	kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
+	kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
 			    sizeof(struct slimpro_resp_msg), &ctx->kfifo_lock);
 	/* Schedule the bottom handler */
 	schedule_work(&ctx->workq);
@@ -520,6 +540,15 @@ static void xgene_hwmon_pcc_rx_cb(struct mbox_client *cl, void *msg)
 	struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
 	struct slimpro_resp_msg amsg;
 
+	/*
+	 * While the driver registers with the mailbox framework, an interrupt
+	 * can be pending before the probe function completes its
+	 * initialization. If such condition occurs, just queue up the message
+	 * as the driver is not ready for servicing the callback.
+	 */
+	if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
+		return;
+
 	msg = generic_comm_base + 1;
 	/* Check if platform sends interrupt */
 	if (!xgene_word_tst_and_clr(&generic_comm_base->status,
@@ -596,6 +625,17 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, ctx);
 	cl = &ctx->mbox_client;
 
+	spin_lock_init(&ctx->kfifo_lock);
+	mutex_init(&ctx->rd_mutex);
+
+	rc = kfifo_alloc(&ctx->async_msg_fifo,
+			 sizeof(struct slimpro_resp_msg) * ASYNC_MSG_FIFO_SIZE,
+			 GFP_KERNEL);
+	if (rc)
+		goto out_mbox_free;
+
+	INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
+
 	/* Request mailbox channel */
 	cl->dev = &pdev->dev;
 	cl->tx_done = xgene_hwmon_tx_done;
@@ -676,17 +716,6 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 		ctx->usecs_lat = PCC_NUM_RETRIES * cppc_ss->latency;
 	}
 
-	spin_lock_init(&ctx->kfifo_lock);
-	mutex_init(&ctx->rd_mutex);
-
-	rc = kfifo_alloc(&ctx->async_msg_fifo,
-			 sizeof(struct slimpro_resp_msg) * ASYNC_MSG_FIFO_SIZE,
-			 GFP_KERNEL);
-	if (rc)
-		goto out_mbox_free;
-
-	INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
-
 	ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
 							   "apm_xgene",
 							   ctx,
@@ -697,17 +726,25 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 		goto out;
 	}
 
+	ctx->init_flag = true;
+	if (ctx->rx_pending) {
+		/*
+		 * If there is a pending message, schedule the bottom handler
+		 */
+		schedule_work(&ctx->workq);
+	}
+
 	dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver registered\n");
 
 	return 0;
 
 out:
-	kfifo_free(&ctx->async_msg_fifo);
-out_mbox_free:
 	if (acpi_disabled)
 		mbox_free_channel(ctx->mbox_chan);
 	else
 		pcc_mbox_free_channel(ctx->mbox_chan);
+out_mbox_free:
+	kfifo_free(&ctx->async_msg_fifo);
 
 	return rc;
 }
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 11 lines · Hoan Tran

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes when accesses
into invalid resource as unmapped PCC shared memory.

This patch fixes this issue by saving this alarm message and scheduling a bottom handler after xgene_hwmon_probe() finish.

I am not completely happy with this fix. Main problem I have is that the processing associated with resp_pending doesn't happen until init_flag is set. Since the hwmon functions can be called right after hwmon_device_register_with_groups(), there is now a new race condition between that call and setting init_flag.

I am also a bit concerned with init_flag and rx_pending not being atomic and protected. What happens if a second interrupt occurs right after init_flag is set but before (or while) rx_pending is evaluated ?

On top of that, init_flag and thus the added complexity is (unless I am missing something) only needed if acpi is enabled. Penaltizing non-acpi code doesn't seem to be optimal.

How do other drivers handle this situation ? This must be a common problem with all mbox users.

Thanks,
Guenter

Quoted 151 lines
---
 drivers/hwmon/xgene-hwmon.c | 75 +++++++++++++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 19 deletions(-)
drivers/hwmon/xgene-hwmon.c +56 -19
index bc78a5d..e3b4e84 100644
@@ -107,6 +107,8 @@ struct xgene_hwmon_dev {
 	struct completion	rd_complete;
 	int			resp_pending;
 	struct slimpro_resp_msg sync_msg;
+	bool			init_flag;
+	bool			rx_pending;
 
 	struct work_struct	workq;
 	struct kfifo_rec_ptr_1	async_msg_fifo;
@@ -465,13 +467,35 @@ static void xgene_hwmon_evt_work(struct work_struct *work)
 	}
 }
 
+static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
+{
+	if (!ctx->init_flag) {
+		ctx->rx_pending = true;
+		/* Enqueue to the FIFO */
+		kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
+				    sizeof(struct slimpro_resp_msg),
+				    &ctx->kfifo_lock);
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
 /*
  * This function is called when the SLIMpro Mailbox received a message
  */
 static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
 {
 	struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
-	struct slimpro_resp_msg amsg;
+
+	/*
+	 * While the driver registers with the mailbox framework, an interrupt
+	 * can be pending before the probe function completes its
+	 * initialization. If such condition occurs, just queue up the message
+	 * as the driver is not ready for servicing the callback.
+	 */
+	if (xgene_hwmon_rx_ready(ctx, msg) < 0)
+		return;
 
 	/*
 	 * Response message format:
@@ -500,12 +524,8 @@ static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
 		return;
 	}
 
-	amsg.msg   = ((u32 *)msg)[0];
-	amsg.param1 = ((u32 *)msg)[1];
-	amsg.param2 = ((u32 *)msg)[2];
-
 	/* Enqueue to the FIFO */
-	kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
+	kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
 			    sizeof(struct slimpro_resp_msg), &ctx->kfifo_lock);
 	/* Schedule the bottom handler */
 	schedule_work(&ctx->workq);
@@ -520,6 +540,15 @@ static void xgene_hwmon_pcc_rx_cb(struct mbox_client *cl, void *msg)
 	struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
 	struct slimpro_resp_msg amsg;
 
+	/*
+	 * While the driver registers with the mailbox framework, an interrupt
+	 * can be pending before the probe function completes its
+	 * initialization. If such condition occurs, just queue up the message
+	 * as the driver is not ready for servicing the callback.
+	 */
+	if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
+		return;
+
 	msg = generic_comm_base + 1;
 	/* Check if platform sends interrupt */
 	if (!xgene_word_tst_and_clr(&generic_comm_base->status,
@@ -596,6 +625,17 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, ctx);
 	cl = &ctx->mbox_client;
 
+	spin_lock_init(&ctx->kfifo_lock);
+	mutex_init(&ctx->rd_mutex);
+
+	rc = kfifo_alloc(&ctx->async_msg_fifo,
+			 sizeof(struct slimpro_resp_msg) * ASYNC_MSG_FIFO_SIZE,
+			 GFP_KERNEL);
+	if (rc)
+		goto out_mbox_free;
+
+	INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
+
 	/* Request mailbox channel */
 	cl->dev = &pdev->dev;
 	cl->tx_done = xgene_hwmon_tx_done;
@@ -676,17 +716,6 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 		ctx->usecs_lat = PCC_NUM_RETRIES * cppc_ss->latency;
 	}
 
-	spin_lock_init(&ctx->kfifo_lock);
-	mutex_init(&ctx->rd_mutex);
-
-	rc = kfifo_alloc(&ctx->async_msg_fifo,
-			 sizeof(struct slimpro_resp_msg) * ASYNC_MSG_FIFO_SIZE,
-			 GFP_KERNEL);
-	if (rc)
-		goto out_mbox_free;
-
-	INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
-
 	ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
 							   "apm_xgene",
 							   ctx,
@@ -697,17 +726,25 @@ static int xgene_hwmon_probe(struct platform_device *pdev)
 		goto out;
 	}
 
+	ctx->init_flag = true;
+	if (ctx->rx_pending) {
+		/*
+		 * If there is a pending message, schedule the bottom handler
+		 */
+		schedule_work(&ctx->workq);
+	}
+
 	dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver registered\n");
 
 	return 0;
 
 out:
-	kfifo_free(&ctx->async_msg_fifo);
-out_mbox_free:
 	if (acpi_disabled)
 		mbox_free_channel(ctx->mbox_chan);
 	else
 		pcc_mbox_free_channel(ctx->mbox_chan);
+out_mbox_free:
+	kfifo_free(&ctx->async_msg_fifo);
 
 	return rc;
 }
author
#2 lore raw

Hi Guenter,

Thank for your quick review !

Quoted 15 lines · Guenter Roeck
Quoted 10 lines · Hoan Tran

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes when
accesses
into invalid resource as unmapped PCC shared memory.

Quoted 3 lines

This patch fixes this issue by saving this alarm message and scheduling a bottom handler after xgene_hwmon_probe() finish.

Quoted 6 lines

I am not completely happy with this fix. Main problem I have is that the processing associated with resp_pending doesn't happen until init_flag is set.
Since the hwmon functions can be called right after hwmon_device_register_with_groups(),
there is now a new race condition between that call and setting init_flag.

I think it's still good if hwmon functions are called right after hwmon_device_register_with_groups().
The response message will be queued into FIFO and be processed later.

Quoted 6 lines

I am also a bit concerned with init_flag and rx_pending not being atomic and protected.
What happens if a second interrupt occurs right after init_flag is set but before
(or while) rx_pending is evaluated ?

Yah, that's a good catch. I can re-use the "kfifo_lock" spinlock for this atomic protection.

Quoted 6 lines

On top of that, init_flag and thus the added complexity is (unless I am missing
something) only needed if acpi is enabled. Penaltizing non-acpi code doesn't seem
to be optimal.

I think, with DT, we still need this flag. In a case of temperature alarm, the driver needs to set "temp1_critical_alarm" sysfs. This "temp1_critical_alarm" should be created before "init_flag" = true.

Thanks
Hoan

Quoted 177 lines

How do other drivers handle this situation ? This must be a common problem with all mbox users.

Thanks,
Guenter

Quoted 168 lines
---
 drivers/hwmon/xgene-hwmon.c | 75
+++++++++++++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 19 deletions(-)
drivers/hwmon/xgene-hwmon.c +19 -1
index bc78a5d..e3b4e84 100644
@@ -107,6 +107,8 @@ struct xgene_hwmon_dev {
        struct completion       rd_complete;
        int                     resp_pending;
        struct slimpro_resp_msg sync_msg;
+       bool                    init_flag;
+       bool                    rx_pending;
 
        struct work_struct      workq;
        struct kfifo_rec_ptr_1  async_msg_fifo;
@@ -465,13 +467,35 @@ static void xgene_hwmon_evt_work(struct work_struct
 *work)
        }
 }
 
+static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
+{
+       if (!ctx->init_flag) {
+               ctx->rx_pending = true;
+               /* Enqueue to the FIFO */
+               kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
+                                   sizeof(struct slimpro_resp_msg),
+                                   &ctx->kfifo_lock);
+               return -EBUSY;
+       }
+
+       return 0;
+}
+
 /*
  * This function is called when the SLIMpro Mailbox received a message
  */
 static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
 {
        struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
-       struct slimpro_resp_msg amsg;
+
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +5 -5
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, msg) < 0)
+               return;
 
        /*
         * Response message format:
@@ -500,12 +524,8 @@ static void xgene_hwmon_rx_cb(struct mbox_client *cl,
 void *msg)
                return;
        }
 
-       amsg.msg   = ((u32 *)msg)[0];
-       amsg.param1 = ((u32 *)msg)[1];
-       amsg.param2 = ((u32 *)msg)[2];
-
        /* Enqueue to the FIFO */
-       kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
+       kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
                            sizeof(struct slimpro_resp_msg),
 &ctx->kfifo_lock);
        /* Schedule the bottom handler */
        schedule_work(&ctx->workq);
drivers/hwmon/xgene-hwmon.c +2 -0
@@ -520,6 +540,15 @@ static void xgene_hwmon_pcc_rx_cb(struct mbox_client
 *cl, void *msg)
        struct acpi_pcct_shared_memory *generic_comm_base =
 ctx->pcc_comm_addr;
        struct slimpro_resp_msg amsg;
 
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +16 -0
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
+               return;
+
        msg = generic_comm_base + 1;
        /* Check if platform sends interrupt */
        if (!xgene_word_tst_and_clr(&generic_comm_base->status,
@@ -596,6 +625,17 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
        platform_set_drvdata(pdev, ctx);
        cl = &ctx->mbox_client;
 
+       spin_lock_init(&ctx->kfifo_lock);
+       mutex_init(&ctx->rd_mutex);
+
+       rc = kfifo_alloc(&ctx->async_msg_fifo,
+                        sizeof(struct slimpro_resp_msg) *
 ASYNC_MSG_FIFO_SIZE,
+                        GFP_KERNEL);
+       if (rc)
+               goto out_mbox_free;
+
+       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
+
        /* Request mailbox channel */
        cl->dev = &pdev->dev;
        cl->tx_done = xgene_hwmon_tx_done;
drivers/hwmon/xgene-hwmon.c +0 -11
@@ -676,17 +716,6 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
                ctx->usecs_lat = PCC_NUM_RETRIES * cppc_ss->latency;
        }
 
-       spin_lock_init(&ctx->kfifo_lock);
-       mutex_init(&ctx->rd_mutex);
-
-       rc = kfifo_alloc(&ctx->async_msg_fifo,
-                        sizeof(struct slimpro_resp_msg) *
 ASYNC_MSG_FIFO_SIZE,
-                        GFP_KERNEL);
-       if (rc)
-               goto out_mbox_free;
-
-       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
-
        ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
                                                           "apm_xgene",
                                                           ctx,
drivers/hwmon/xgene-hwmon.c +4 -0
@@ -697,17 +726,25 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
                goto out;
        }
 
+       ctx->init_flag = true;
+       if (ctx->rx_pending) {
+               /*
+                * If there is a pending message, schedule the bottom

handler

drivers/hwmon/xgene-hwmon.c +4 -0
+                */
+               schedule_work(&ctx->workq);
+       }
+
        dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver

registered\n");

        return 0;

out:

drivers/hwmon/xgene-hwmon.c +2 -2
-       kfifo_free(&ctx->async_msg_fifo);
-out_mbox_free:
        if (acpi_disabled)
                mbox_free_channel(ctx->mbox_chan);
        else
                pcc_mbox_free_channel(ctx->mbox_chan);
+out_mbox_free:
+       kfifo_free(&ctx->async_msg_fifo);
 
        return rc;
 }
maintainer
#3 lore raw
Quoted 27 lines · Hoan Tran

Hi Guenter,

Thank for your quick review !

Quoted 15 lines · Guenter Roeck
Quoted 10 lines · Hoan Tran

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes when
accesses
into invalid resource as unmapped PCC shared memory.

Quoted 3 lines

This patch fixes this issue by saving this alarm message and scheduling a bottom handler after xgene_hwmon_probe() finish.

Quoted 6 lines

I am not completely happy with this fix. Main problem I have is that the processing associated with resp_pending doesn't happen until init_flag is set.
Since the hwmon functions can be called right after hwmon_device_register_with_groups(),
there is now a new race condition between that call and setting init_flag.

Quoted 4 lines

I think it's still good if hwmon functions are called right after hwmon_device_register_with_groups().
The response message will be queued into FIFO and be processed later.

Yes, but the call to complete() won't happen in this case, or am I missing something ?

Guenter

Quoted 202 lines
Quoted 6 lines

I am also a bit concerned with init_flag and rx_pending not being atomic and protected.
What happens if a second interrupt occurs right after init_flag is set but before
(or while) rx_pending is evaluated ?

Yah, that's a good catch. I can re-use the "kfifo_lock" spinlock for this atomic protection.

Quoted 6 lines

On top of that, init_flag and thus the added complexity is (unless I am missing
something) only needed if acpi is enabled. Penaltizing non-acpi code doesn't seem
to be optimal.

I think, with DT, we still need this flag. In a case of temperature alarm, the driver needs to set "temp1_critical_alarm" sysfs. This "temp1_critical_alarm" should be created before "init_flag" = true.

Thanks
Hoan

Quoted 177 lines

How do other drivers handle this situation ? This must be a common problem with all mbox users.

Thanks,
Guenter

Quoted 168 lines
---
 drivers/hwmon/xgene-hwmon.c | 75
+++++++++++++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 19 deletions(-)
drivers/hwmon/xgene-hwmon.c +19 -1
index bc78a5d..e3b4e84 100644
@@ -107,6 +107,8 @@ struct xgene_hwmon_dev {
        struct completion       rd_complete;
        int                     resp_pending;
        struct slimpro_resp_msg sync_msg;
+       bool                    init_flag;
+       bool                    rx_pending;
 
        struct work_struct      workq;
        struct kfifo_rec_ptr_1  async_msg_fifo;
@@ -465,13 +467,35 @@ static void xgene_hwmon_evt_work(struct work_struct
 *work)
        }
 }
 
+static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
+{
+       if (!ctx->init_flag) {
+               ctx->rx_pending = true;
+               /* Enqueue to the FIFO */
+               kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
+                                   sizeof(struct slimpro_resp_msg),
+                                   &ctx->kfifo_lock);
+               return -EBUSY;
+       }
+
+       return 0;
+}
+
 /*
  * This function is called when the SLIMpro Mailbox received a message
  */
 static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
 {
        struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
-       struct slimpro_resp_msg amsg;
+
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +5 -5
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, msg) < 0)
+               return;
 
        /*
         * Response message format:
@@ -500,12 +524,8 @@ static void xgene_hwmon_rx_cb(struct mbox_client *cl,
 void *msg)
                return;
        }
 
-       amsg.msg   = ((u32 *)msg)[0];
-       amsg.param1 = ((u32 *)msg)[1];
-       amsg.param2 = ((u32 *)msg)[2];
-
        /* Enqueue to the FIFO */
-       kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
+       kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
                            sizeof(struct slimpro_resp_msg),
 &ctx->kfifo_lock);
        /* Schedule the bottom handler */
        schedule_work(&ctx->workq);
drivers/hwmon/xgene-hwmon.c +2 -0
@@ -520,6 +540,15 @@ static void xgene_hwmon_pcc_rx_cb(struct mbox_client
 *cl, void *msg)
        struct acpi_pcct_shared_memory *generic_comm_base =
 ctx->pcc_comm_addr;
        struct slimpro_resp_msg amsg;
 
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +16 -0
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
+               return;
+
        msg = generic_comm_base + 1;
        /* Check if platform sends interrupt */
        if (!xgene_word_tst_and_clr(&generic_comm_base->status,
@@ -596,6 +625,17 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
        platform_set_drvdata(pdev, ctx);
        cl = &ctx->mbox_client;
 
+       spin_lock_init(&ctx->kfifo_lock);
+       mutex_init(&ctx->rd_mutex);
+
+       rc = kfifo_alloc(&ctx->async_msg_fifo,
+                        sizeof(struct slimpro_resp_msg) *
 ASYNC_MSG_FIFO_SIZE,
+                        GFP_KERNEL);
+       if (rc)
+               goto out_mbox_free;
+
+       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
+
        /* Request mailbox channel */
        cl->dev = &pdev->dev;
        cl->tx_done = xgene_hwmon_tx_done;
drivers/hwmon/xgene-hwmon.c +0 -11
@@ -676,17 +716,6 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
                ctx->usecs_lat = PCC_NUM_RETRIES * cppc_ss->latency;
        }
 
-       spin_lock_init(&ctx->kfifo_lock);
-       mutex_init(&ctx->rd_mutex);
-
-       rc = kfifo_alloc(&ctx->async_msg_fifo,
-                        sizeof(struct slimpro_resp_msg) *
 ASYNC_MSG_FIFO_SIZE,
-                        GFP_KERNEL);
-       if (rc)
-               goto out_mbox_free;
-
-       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
-
        ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
                                                           "apm_xgene",
                                                           ctx,
drivers/hwmon/xgene-hwmon.c +4 -0
@@ -697,17 +726,25 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
                goto out;
        }
 
+       ctx->init_flag = true;
+       if (ctx->rx_pending) {
+               /*
+                * If there is a pending message, schedule the bottom

handler

drivers/hwmon/xgene-hwmon.c +4 -0
+                */
+               schedule_work(&ctx->workq);
+       }
+
        dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver

registered\n");

        return 0;

out:

drivers/hwmon/xgene-hwmon.c +2 -2
-       kfifo_free(&ctx->async_msg_fifo);
-out_mbox_free:
        if (acpi_disabled)
                mbox_free_channel(ctx->mbox_chan);
        else
                pcc_mbox_free_channel(ctx->mbox_chan);
+out_mbox_free:
+       kfifo_free(&ctx->async_msg_fifo);
 
        return rc;
 }
author
#4 lore raw

Hi Guenter,

Quoted 34 lines · Guenter Roeck
Quoted 33 lines · Hoan Tran

Hi Guenter,

Thank for your quick review !

Quoted 25 lines · Guenter Roeck
Quoted 11 lines · Hoan Tran

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes when
accesses
into invalid resource as unmapped PCC shared memory.

Quoted 4 lines

This patch fixes this issue by saving this alarm message and scheduling a
bottom handler after xgene_hwmon_probe() finish.

I am not completely happy with this fix. Main problem I have is that the processing associated with resp_pending doesn't happen until init_flag is set.
Since the hwmon functions can be called right after hwmon_device_register_with_groups(),
there is now a new race condition between that call and setting init_flag.

Quoted 6 lines
Quoted 4 lines

I think it's still good if hwmon functions are called right after hwmon_device_register_with_groups().
The response message will be queued into FIFO and be processed later.

Yes, but the call to complete() won't happen in this case, or am I missing something ?

Yes, I think xgene_hwmon_rd() and xgene_hwmon_pcc_rd() functions have to check "init_flag == true" before issue the read command.

Thanks
Hoan

Quoted 217 lines

Guenter

Quoted 212 lines
Quoted 8 lines

I am also a bit concerned with init_flag and rx_pending not being atomic and
protected.
What happens if a second interrupt occurs right after init_flag is set but
before
(or while) rx_pending is evaluated ?

Yah, that's a good catch. I can re-use the "kfifo_lock" spinlock for this atomic protection.

Quoted 7 lines

On top of that, init_flag and thus the added complexity is (unless I am missing
something) only needed if acpi is enabled. Penaltizing non-acpi code doesn't
seem
to be optimal.

I think, with DT, we still need this flag. In a case of temperature alarm, the driver needs to set "temp1_critical_alarm" sysfs. This "temp1_critical_alarm" should be created before "init_flag" = true.

Thanks
Hoan

Quoted 182 lines

How do other drivers handle this situation ? This must be a common problem
with all mbox users.

Thanks,
Guenter

Quoted 172 lines
---
 drivers/hwmon/xgene-hwmon.c | 75
+++++++++++++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 19 deletions(-)
drivers/hwmon/xgene-hwmon.c +2 -0
index bc78a5d..e3b4e84 100644
@@ -107,6 +107,8 @@ struct xgene_hwmon_dev {
        struct completion       rd_complete;
        int                     resp_pending;
        struct slimpro_resp_msg sync_msg;
+       bool                    init_flag;
+       bool                    rx_pending;
 
        struct work_struct      workq;
        struct kfifo_rec_ptr_1  async_msg_fifo;
@@ -465,13 +467,35 @@ static void xgene_hwmon_evt_work(struct

work_struct
*work)

        }

}

drivers/hwmon/xgene-hwmon.c +17 -1
+static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
+{
+       if (!ctx->init_flag) {
+               ctx->rx_pending = true;
+               /* Enqueue to the FIFO */
+               kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
+                                   sizeof(struct slimpro_resp_msg),
+                                   &ctx->kfifo_lock);
+               return -EBUSY;
+       }
+
+       return 0;
+}
+
 /*
  * This function is called when the SLIMpro Mailbox received a message
  */
 static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
 {
        struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
-       struct slimpro_resp_msg amsg;
+
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +5 -5
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, msg) < 0)
+               return;
 
        /*
         * Response message format:
@@ -500,12 +524,8 @@ static void xgene_hwmon_rx_cb(struct mbox_client
 *cl,
 void *msg)
                return;
        }
 
-       amsg.msg   = ((u32 *)msg)[0];
-       amsg.param1 = ((u32 *)msg)[1];
-       amsg.param2 = ((u32 *)msg)[2];
-
        /* Enqueue to the FIFO */
-       kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
+       kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
                            sizeof(struct slimpro_resp_msg),
&ctx->kfifo_lock);
        /* Schedule the bottom handler */
        schedule_work(&ctx->workq);
drivers/hwmon/xgene-hwmon.c +0 -0
@@ -520,6 +540,15 @@ static void xgene_hwmon_pcc_rx_cb(struct

mbox_client
*cl, void *msg)
struct acpi_pcct_shared_memory *generic_comm_base =

ctx->pcc_comm_addr;
        struct slimpro_resp_msg amsg;
drivers/hwmon/xgene-hwmon.c +2 -0
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +16 -0
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
+               return;
+
        msg = generic_comm_base + 1;
        /* Check if platform sends interrupt */
        if (!xgene_word_tst_and_clr(&generic_comm_base->status,
@@ -596,6 +625,17 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
        platform_set_drvdata(pdev, ctx);
        cl = &ctx->mbox_client;
 
+       spin_lock_init(&ctx->kfifo_lock);
+       mutex_init(&ctx->rd_mutex);
+
+       rc = kfifo_alloc(&ctx->async_msg_fifo,
+                        sizeof(struct slimpro_resp_msg) *
 ASYNC_MSG_FIFO_SIZE,
+                        GFP_KERNEL);
+       if (rc)
+               goto out_mbox_free;
+
+       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
+
        /* Request mailbox channel */
        cl->dev = &pdev->dev;
        cl->tx_done = xgene_hwmon_tx_done;
drivers/hwmon/xgene-hwmon.c +0 -11
@@ -676,17 +716,6 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
                ctx->usecs_lat = PCC_NUM_RETRIES * cppc_ss->latency;
        }
 
-       spin_lock_init(&ctx->kfifo_lock);
-       mutex_init(&ctx->rd_mutex);
-
-       rc = kfifo_alloc(&ctx->async_msg_fifo,
-                        sizeof(struct slimpro_resp_msg) *
 ASYNC_MSG_FIFO_SIZE,
-                        GFP_KERNEL);
-       if (rc)
-               goto out_mbox_free;
-
-       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
-
        ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
                                                           "apm_xgene",
                                                           ctx,
drivers/hwmon/xgene-hwmon.c +0 -0
@@ -697,17 +726,25 @@ static int xgene_hwmon_probe(struct

platform_device
*pdev)

                goto out;
        }
drivers/hwmon/xgene-hwmon.c +4 -0
+       ctx->init_flag = true;
+       if (ctx->rx_pending) {
+               /*
+                * If there is a pending message, schedule the bottom

handler

drivers/hwmon/xgene-hwmon.c +4 -0
+                */
+               schedule_work(&ctx->workq);
+       }
+
        dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver

registered\n");

        return 0;

out:

drivers/hwmon/xgene-hwmon.c +2 -2
-       kfifo_free(&ctx->async_msg_fifo);
-out_mbox_free:
        if (acpi_disabled)
                mbox_free_channel(ctx->mbox_chan);
        else
                pcc_mbox_free_channel(ctx->mbox_chan);
+out_mbox_free:
+       kfifo_free(&ctx->async_msg_fifo);
 
        return rc;
 }
maintainer
#5 lore raw
Quoted 41 lines · Hoan Tran

Hi Guenter,

Quoted 38 lines · Guenter Roeck
Quoted 37 lines · Hoan Tran

Hi Guenter,

Thank for your quick review !

Quoted 18 lines · Guenter Roeck
Quoted 11 lines · Hoan Tran

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes when
accesses
into invalid resource as unmapped PCC shared memory.

Quoted 4 lines

This patch fixes this issue by saving this alarm message and scheduling a
bottom handler after xgene_hwmon_probe() finish.

Quoted 7 lines

I am not completely happy with this fix. Main problem I have is that the processing associated with resp_pending doesn't happen until init_flag is set.
Since the hwmon functions can be called right after hwmon_device_register_with_groups(),
there is now a new race condition between that call and setting init_flag.

I think it's still good if hwmon functions are called right after hwmon_device_register_with_groups().
The response message will be queued into FIFO and be processed later.

Quoted 6 lines
Quoted 2 lines

Yes, but the call to complete() won't happen in this case, or am I missing something ?

Yes, I think xgene_hwmon_rd() and xgene_hwmon_pcc_rd() functions have to check "init_flag == true" before issue the read command.

This is getting more and more messy :-(

Quoted 33 lines

Thanks
Hoan

Quoted 30 lines

Guenter

Quoted 26 lines
Quoted 8 lines

I am also a bit concerned with init_flag and rx_pending not being atomic and
protected.
What happens if a second interrupt occurs right after init_flag is set but
before
(or while) rx_pending is evaluated ?

Yah, that's a good catch. I can re-use the "kfifo_lock" spinlock for this atomic protection.

Quoted 7 lines

On top of that, init_flag and thus the added complexity is (unless I am missing
something) only needed if acpi is enabled. Penaltizing non-acpi code doesn't
seem
to be optimal.

I think, with DT, we still need this flag. In a case of temperature alarm, the driver needs to set "temp1_critical_alarm" sysfs. This "temp1_critical_alarm" should be created before "init_flag" = true.

I don't know. After all, any such alarm would be lost if it occurred before the driver was loaded, no ? Those mailboxes should really have a means to be informed that the initiator is ready to handle interrupts/callbacks.

Guenter

Quoted 188 lines
Quoted 187 lines
Quoted 186 lines

Thanks
Hoan

Quoted 182 lines

How do other drivers handle this situation ? This must be a common problem
with all mbox users.

Thanks,
Guenter

Quoted 172 lines
---
 drivers/hwmon/xgene-hwmon.c | 75
+++++++++++++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 19 deletions(-)
drivers/hwmon/xgene-hwmon.c +2 -0
index bc78a5d..e3b4e84 100644
@@ -107,6 +107,8 @@ struct xgene_hwmon_dev {
        struct completion       rd_complete;
        int                     resp_pending;
        struct slimpro_resp_msg sync_msg;
+       bool                    init_flag;
+       bool                    rx_pending;
 
        struct work_struct      workq;
        struct kfifo_rec_ptr_1  async_msg_fifo;
@@ -465,13 +467,35 @@ static void xgene_hwmon_evt_work(struct

work_struct
*work)

        }

}

drivers/hwmon/xgene-hwmon.c +17 -1
+static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
+{
+       if (!ctx->init_flag) {
+               ctx->rx_pending = true;
+               /* Enqueue to the FIFO */
+               kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
+                                   sizeof(struct slimpro_resp_msg),
+                                   &ctx->kfifo_lock);
+               return -EBUSY;
+       }
+
+       return 0;
+}
+
 /*
  * This function is called when the SLIMpro Mailbox received a message
  */
 static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
 {
        struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
-       struct slimpro_resp_msg amsg;
+
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +5 -5
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, msg) < 0)
+               return;
 
        /*
         * Response message format:
@@ -500,12 +524,8 @@ static void xgene_hwmon_rx_cb(struct mbox_client
 *cl,
 void *msg)
                return;
        }
 
-       amsg.msg   = ((u32 *)msg)[0];
-       amsg.param1 = ((u32 *)msg)[1];
-       amsg.param2 = ((u32 *)msg)[2];
-
        /* Enqueue to the FIFO */
-       kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
+       kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
                            sizeof(struct slimpro_resp_msg),
&ctx->kfifo_lock);
        /* Schedule the bottom handler */
        schedule_work(&ctx->workq);
drivers/hwmon/xgene-hwmon.c +0 -0
@@ -520,6 +540,15 @@ static void xgene_hwmon_pcc_rx_cb(struct

mbox_client
*cl, void *msg)
struct acpi_pcct_shared_memory *generic_comm_base =

ctx->pcc_comm_addr;
        struct slimpro_resp_msg amsg;
drivers/hwmon/xgene-hwmon.c +2 -0
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +16 -0
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
+               return;
+
        msg = generic_comm_base + 1;
        /* Check if platform sends interrupt */
        if (!xgene_word_tst_and_clr(&generic_comm_base->status,
@@ -596,6 +625,17 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
        platform_set_drvdata(pdev, ctx);
        cl = &ctx->mbox_client;
 
+       spin_lock_init(&ctx->kfifo_lock);
+       mutex_init(&ctx->rd_mutex);
+
+       rc = kfifo_alloc(&ctx->async_msg_fifo,
+                        sizeof(struct slimpro_resp_msg) *
 ASYNC_MSG_FIFO_SIZE,
+                        GFP_KERNEL);
+       if (rc)
+               goto out_mbox_free;
+
+       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
+
        /* Request mailbox channel */
        cl->dev = &pdev->dev;
        cl->tx_done = xgene_hwmon_tx_done;
drivers/hwmon/xgene-hwmon.c +0 -11
@@ -676,17 +716,6 @@ static int xgene_hwmon_probe(struct platform_device
 *pdev)
                ctx->usecs_lat = PCC_NUM_RETRIES * cppc_ss->latency;
        }
 
-       spin_lock_init(&ctx->kfifo_lock);
-       mutex_init(&ctx->rd_mutex);
-
-       rc = kfifo_alloc(&ctx->async_msg_fifo,
-                        sizeof(struct slimpro_resp_msg) *
 ASYNC_MSG_FIFO_SIZE,
-                        GFP_KERNEL);
-       if (rc)
-               goto out_mbox_free;
-
-       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
-
        ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
                                                           "apm_xgene",
                                                           ctx,
drivers/hwmon/xgene-hwmon.c +0 -0
@@ -697,17 +726,25 @@ static int xgene_hwmon_probe(struct

platform_device
*pdev)

                goto out;
        }
drivers/hwmon/xgene-hwmon.c +4 -0
+       ctx->init_flag = true;
+       if (ctx->rx_pending) {
+               /*
+                * If there is a pending message, schedule the bottom

handler

drivers/hwmon/xgene-hwmon.c +4 -0
+                */
+               schedule_work(&ctx->workq);
+       }
+
        dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver

registered\n");

        return 0;

out:

drivers/hwmon/xgene-hwmon.c +2 -2
-       kfifo_free(&ctx->async_msg_fifo);
-out_mbox_free:
        if (acpi_disabled)
                mbox_free_channel(ctx->mbox_chan);
        else
                pcc_mbox_free_channel(ctx->mbox_chan);
+out_mbox_free:
+       kfifo_free(&ctx->async_msg_fifo);
 
        return rc;
 }
author
#6 lore raw

Hi Guenter,

Quoted 101 lines · Guenter Roeck
Quoted 52 lines · Hoan Tran

Hi Guenter,

Quoted 48 lines · Guenter Roeck
Quoted 46 lines · Hoan Tran

Hi Guenter,

Thank for your quick review !

Quoted 22 lines · Guenter Roeck
Quoted 13 lines · Hoan Tran

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes
when
accesses
into invalid resource as unmapped PCC shared memory.

Quoted 5 lines

This patch fixes this issue by saving this alarm message and scheduling
a
bottom handler after xgene_hwmon_probe() finish.

Quoted 9 lines

I am not completely happy with this fix. Main problem I have is that the
processing associated with resp_pending doesn't happen until init_flag is
set.
Since the hwmon functions can be called right after hwmon_device_register_with_groups(),
there is now a new race condition between that call and setting init_flag.

I think it's still good if hwmon functions are called right after hwmon_device_register_with_groups().
The response message will be queued into FIFO and be processed later.

Quoted 8 lines
Quoted 3 lines

Yes, but the call to complete() won't happen in this case, or am I missing
something ?

Yes, I think xgene_hwmon_rd() and xgene_hwmon_pcc_rd() functions have to check "init_flag == true" before issue the read command.

This is getting more and more messy :-(

Quoted 36 lines

Thanks
Hoan

Quoted 33 lines

Guenter

Quoted 29 lines
Quoted 9 lines

I am also a bit concerned with init_flag and rx_pending not being atomic
and
protected.
What happens if a second interrupt occurs right after init_flag is set but
before
(or while) rx_pending is evaluated ?

Yah, that's a good catch. I can re-use the "kfifo_lock" spinlock for this atomic protection.

Quoted 7 lines

On top of that, init_flag and thus the added complexity is (unless I am missing
something) only needed if acpi is enabled. Penaltizing non-acpi code doesn't
seem
to be optimal.

I think, with DT, we still need this flag. In a case of temperature alarm, the driver needs to set "temp1_critical_alarm" sysfs. This "temp1_critical_alarm" should be created before "init_flag" = true.

Quoted 4 lines

I don't know. After all, any such alarm would be lost if it occurred before the driver was loaded, no ? Those mailboxes should really have a means to be informed that the initiator is ready to handle interrupts/callbacks.

We don't want to miss an alarm message. User should be warned.

As
ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,

"apm_xgene",

                                                           ctx,
                                                           xgene_hwmon_groups);

How about, callback functions check the ctx->hwmon_dev validation. If not, they just save msg into FIFO.
Beside of that, as hwmon functions can be called before ctx->hwmon_dev is assigned. Callback functions check if there is a mailbox response pending before saving msg into FIFO as below

static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)

{
        if (IS_ERR(ctx->hwmon_dev) && !ctx->resp_pending) {
                /* Enqueue to the FIFO */
                kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
                                    sizeof(struct slimpro_resp_msg),
                                    &ctx->kfifo_lock);

                return -EBUSY;
        }

        return 0;
}

At the end of probe function, driver always schedules a bottom handler to handle FIFO msg.

Then we can remove the init_flag and rx_pending.

Thanks
Hoan

Quoted 198 lines

Guenter

Quoted 193 lines
Quoted 192 lines
Quoted 191 lines

Thanks
Hoan

Quoted 187 lines

How do other drivers handle this situation ? This must be a common problem
with all mbox users.

Thanks,
Guenter

Quoted 177 lines
---
 drivers/hwmon/xgene-hwmon.c | 75
+++++++++++++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 19 deletions(-)
drivers/hwmon/xgene-hwmon.c +2 -0
index bc78a5d..e3b4e84 100644
@@ -107,6 +107,8 @@ struct xgene_hwmon_dev {
        struct completion       rd_complete;
        int                     resp_pending;
        struct slimpro_resp_msg sync_msg;
+       bool                    init_flag;
+       bool                    rx_pending;
 
        struct work_struct      workq;
        struct kfifo_rec_ptr_1  async_msg_fifo;
@@ -465,13 +467,35 @@ static void xgene_hwmon_evt_work(struct

work_struct
*work)

        }

}

+static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)

drivers/hwmon/xgene-hwmon.c +13 -0
+{
+       if (!ctx->init_flag) {
+               ctx->rx_pending = true;
+               /* Enqueue to the FIFO */
+               kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
+                                   sizeof(struct slimpro_resp_msg),
+                                   &ctx->kfifo_lock);
+               return -EBUSY;
+       }
+
+       return 0;
+}
+
 /*
  * This function is called when the SLIMpro Mailbox received a

message
*/
static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)

 {
        struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
drivers/hwmon/xgene-hwmon.c +3 -1
-       struct slimpro_resp_msg amsg;
+
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +5 -5
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, msg) < 0)
+               return;
 
        /*
         * Response message format:
@@ -500,12 +524,8 @@ static void xgene_hwmon_rx_cb(struct mbox_client
 *cl,
 void *msg)
                return;
        }
 
-       amsg.msg   = ((u32 *)msg)[0];
-       amsg.param1 = ((u32 *)msg)[1];
-       amsg.param2 = ((u32 *)msg)[2];
-
        /* Enqueue to the FIFO */
-       kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
+       kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
                            sizeof(struct slimpro_resp_msg),
&ctx->kfifo_lock);
        /* Schedule the bottom handler */
        schedule_work(&ctx->workq);
drivers/hwmon/xgene-hwmon.c +0 -0
@@ -520,6 +540,15 @@ static void xgene_hwmon_pcc_rx_cb(struct

mbox_client
*cl, void *msg)
struct acpi_pcct_shared_memory *generic_comm_base =

ctx->pcc_comm_addr;
        struct slimpro_resp_msg amsg;
drivers/hwmon/xgene-hwmon.c +2 -0
+       /*
+        * While the driver registers with the mailbox framework, an

interrupt

drivers/hwmon/xgene-hwmon.c +2 -0
+        * can be pending before the probe function completes its
+        * initialization. If such condition occurs, just queue up the

message

drivers/hwmon/xgene-hwmon.c +5 -0
+        * as the driver is not ready for servicing the callback.
+        */
+       if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
+               return;
+
        msg = generic_comm_base + 1;
        /* Check if platform sends interrupt */
        if (!xgene_word_tst_and_clr(&generic_comm_base->status,
@@ -596,6 +625,17 @@ static int xgene_hwmon_probe(struct

platform_device
*pdev)

        platform_set_drvdata(pdev, ctx);
        cl = &ctx->mbox_client;
drivers/hwmon/xgene-hwmon.c +5 -0
+       spin_lock_init(&ctx->kfifo_lock);
+       mutex_init(&ctx->rd_mutex);
+
+       rc = kfifo_alloc(&ctx->async_msg_fifo,
+                        sizeof(struct slimpro_resp_msg) *

ASYNC_MSG_FIFO_SIZE,

drivers/hwmon/xgene-hwmon.c +6 -0
+                        GFP_KERNEL);
+       if (rc)
+               goto out_mbox_free;
+
+       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
+
        /* Request mailbox channel */
        cl->dev = &pdev->dev;
        cl->tx_done = xgene_hwmon_tx_done;
@@ -676,17 +716,6 @@ static int xgene_hwmon_probe(struct

platform_device
*pdev)

                ctx->usecs_lat = PCC_NUM_RETRIES * cppc_ss->latency;
        }
drivers/hwmon/xgene-hwmon.c +0 -5
-       spin_lock_init(&ctx->kfifo_lock);
-       mutex_init(&ctx->rd_mutex);
-
-       rc = kfifo_alloc(&ctx->async_msg_fifo,
-                        sizeof(struct slimpro_resp_msg) *

ASYNC_MSG_FIFO_SIZE,

drivers/hwmon/xgene-hwmon.c +0 -6
-                        GFP_KERNEL);
-       if (rc)
-               goto out_mbox_free;
-
-       INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
-
        ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,

"apm_xgene",
ctx,

drivers/hwmon/xgene-hwmon.c +0 -0
@@ -697,17 +726,25 @@ static int xgene_hwmon_probe(struct

platform_device
*pdev)

                goto out;
        }
drivers/hwmon/xgene-hwmon.c +4 -0
+       ctx->init_flag = true;
+       if (ctx->rx_pending) {
+               /*
+                * If there is a pending message, schedule the bottom

handler

drivers/hwmon/xgene-hwmon.c +4 -0
+                */
+               schedule_work(&ctx->workq);
+       }
+
        dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver

registered\n");

        return 0;

out:

drivers/hwmon/xgene-hwmon.c +2 -2
-       kfifo_free(&ctx->async_msg_fifo);
-out_mbox_free:
        if (acpi_disabled)
                mbox_free_channel(ctx->mbox_chan);
        else
                pcc_mbox_free_channel(ctx->mbox_chan);
+out_mbox_free:
+       kfifo_free(&ctx->async_msg_fifo);
 
        return rc;
 }
maintainer
#7 lore raw
Quoted 123 lines · Hoan Tran

Hi Guenter,

Quoted 101 lines · Guenter Roeck
Quoted 52 lines · Hoan Tran

Hi Guenter,

Quoted 48 lines · Guenter Roeck
Quoted 46 lines · Hoan Tran

Hi Guenter,

Thank for your quick review !

Quoted 22 lines · Guenter Roeck
Quoted 13 lines · Hoan Tran

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes
when
accesses
into invalid resource as unmapped PCC shared memory.

Quoted 5 lines

This patch fixes this issue by saving this alarm message and scheduling
a
bottom handler after xgene_hwmon_probe() finish.

Quoted 9 lines

I am not completely happy with this fix. Main problem I have is that the
processing associated with resp_pending doesn't happen until init_flag is
set.
Since the hwmon functions can be called right after hwmon_device_register_with_groups(),
there is now a new race condition between that call and setting init_flag.

I think it's still good if hwmon functions are called right after hwmon_device_register_with_groups().
The response message will be queued into FIFO and be processed later.

Quoted 8 lines
Quoted 3 lines

Yes, but the call to complete() won't happen in this case, or am I missing
something ?

Yes, I think xgene_hwmon_rd() and xgene_hwmon_pcc_rd() functions have to check "init_flag == true" before issue the read command.

This is getting more and more messy :-(

Quoted 36 lines

Thanks
Hoan

Quoted 33 lines

Guenter

Quoted 29 lines
Quoted 9 lines

I am also a bit concerned with init_flag and rx_pending not being atomic
and
protected.
What happens if a second interrupt occurs right after init_flag is set but
before
(or while) rx_pending is evaluated ?

Yah, that's a good catch. I can re-use the "kfifo_lock" spinlock for this atomic protection.

Quoted 7 lines

On top of that, init_flag and thus the added complexity is (unless I am missing
something) only needed if acpi is enabled. Penaltizing non-acpi code doesn't
seem
to be optimal.

I think, with DT, we still need this flag. In a case of temperature alarm, the driver needs to set "temp1_critical_alarm" sysfs. This "temp1_critical_alarm" should be created before "init_flag" = true.

Quoted 4 lines

I don't know. After all, any such alarm would be lost if it occurred before the driver was loaded, no ? Those mailboxes should really have a means to be informed that the initiator is ready to handle interrupts/callbacks.

We don't want to miss an alarm message. User should be warned.

As
ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,

"apm_xgene",

                                                           ctx,
                                                           xgene_hwmon_groups);

How about, callback functions check the ctx->hwmon_dev validation. If not, they just save msg into FIFO.
Beside of that, as hwmon functions can be called before ctx->hwmon_dev is assigned. Callback functions check if there is a mailbox response pending before saving msg into FIFO as below

Quoted 3 lines

static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)

{
        if (IS_ERR(ctx->hwmon_dev) && !ctx->resp_pending) {

Probably IS_ERR_OR_ZERO() ?

Guenter

Quoted 6 lines
                /* Enqueue to the FIFO */
                kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
                                    sizeof(struct slimpro_resp_msg),
                                    &ctx->kfifo_lock);

                return -EBUSY;

Need to find something else. EBUSY isn't correct.

Quoted 11 lines
        }

        return 0;
}

At the end of probe function, driver always schedules a bottom handler to handle FIFO msg.

Then we can remove the init_flag and rx_pending.

At least better than before, though I think it is still racy. It might be worthwhile checking by adding a large msleep() after hwmon registration and before ctx->hwmon_dev is written, and have a user space program access sysfs attributes immediately after they have been created.

Thanks,
Guenter

author
#8 lore raw

Hi Guenter,

Quoted 124 lines · Guenter Roeck
Quoted 123 lines · Hoan Tran

Hi Guenter,

Quoted 101 lines · Guenter Roeck
Quoted 52 lines · Hoan Tran

Hi Guenter,

Quoted 48 lines · Guenter Roeck
Quoted 46 lines · Hoan Tran

Hi Guenter,

Thank for your quick review !

Quoted 22 lines · Guenter Roeck
Quoted 13 lines · Hoan Tran

The system crashes during probing xgene-hwmon driver when temperature alarm interrupt occurs before.
It's because
- xgene_hwmon_probe() requests PCC mailbox channel which also enables
the mailbox interrupt.
- As temperature alarm interrupt is pending, ISR runs and crashes
when
accesses
into invalid resource as unmapped PCC shared memory.

Quoted 5 lines

This patch fixes this issue by saving this alarm message and scheduling
a
bottom handler after xgene_hwmon_probe() finish.

Quoted 9 lines

I am not completely happy with this fix. Main problem I have is that the
processing associated with resp_pending doesn't happen until init_flag is
set.
Since the hwmon functions can be called right after hwmon_device_register_with_groups(),
there is now a new race condition between that call and setting init_flag.

I think it's still good if hwmon functions are called right after hwmon_device_register_with_groups().
The response message will be queued into FIFO and be processed later.

Quoted 8 lines
Quoted 3 lines

Yes, but the call to complete() won't happen in this case, or am I missing
something ?

Yes, I think xgene_hwmon_rd() and xgene_hwmon_pcc_rd() functions have to check "init_flag == true" before issue the read command.

This is getting more and more messy :-(

Quoted 36 lines

Thanks
Hoan

Quoted 33 lines

Guenter

Quoted 29 lines
Quoted 9 lines

I am also a bit concerned with init_flag and rx_pending not being atomic
and
protected.
What happens if a second interrupt occurs right after init_flag is set but
before
(or while) rx_pending is evaluated ?

Yah, that's a good catch. I can re-use the "kfifo_lock" spinlock for this atomic protection.

Quoted 7 lines

On top of that, init_flag and thus the added complexity is (unless I am missing
something) only needed if acpi is enabled. Penaltizing non-acpi code doesn't
seem
to be optimal.

I think, with DT, we still need this flag. In a case of temperature alarm, the driver needs to set "temp1_critical_alarm" sysfs. This "temp1_critical_alarm" should be created before "init_flag" = true.

Quoted 4 lines

I don't know. After all, any such alarm would be lost if it occurred before the driver was loaded, no ? Those mailboxes should really have a means to be informed that the initiator is ready to handle interrupts/callbacks.

We don't want to miss an alarm message. User should be warned.

As
ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,

"apm_xgene",

                                                           ctx,
                                                           xgene_hwmon_groups);

How about, callback functions check the ctx->hwmon_dev validation. If not, they just save msg into FIFO.
Beside of that, as hwmon functions can be called before ctx->hwmon_dev is assigned. Callback functions check if there is a mailbox response pending before saving msg into FIFO as below

Quoted 5 lines
Quoted 3 lines

static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)

{
        if (IS_ERR(ctx->hwmon_dev) && !ctx->resp_pending) {

Probably IS_ERR_OR_ZERO() ?

Yes, I'll use IS_ERR_OR_NULL().

Quoted 11 lines

Guenter

Quoted 6 lines
                /* Enqueue to the FIFO */
                kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
                                    sizeof(struct slimpro_resp_msg),
                                    &ctx->kfifo_lock);

                return -EBUSY;

Need to find something else. EBUSY isn't correct.

Change to ENODEV.

Quoted 10 lines
Quoted 10 lines
        }

        return 0;
}

At the end of probe function, driver always schedules a bottom handler to handle FIFO msg.

Quoted 8 lines
Quoted 2 lines

Then we can remove the init_flag and rx_pending.

At least better than before, though I think it is still racy. It might be worthwhile checking by adding a large msleep() after hwmon registration and before ctx->hwmon_dev is written, and have a user space program access sysfs attributes immediately after they have been created.

Yes, I'll test it out by adding msleep() right before __hwmon_device_register() returns.

Thanks
Hoan

Quoted 3 lines

Thanks,
Guenter