Skip to contentSkip to conversation

EDAC/igen6: Avoid segmentation fault and add polling support

PATCH In review Orange Kao 2 participants 6 messages Nov 1, 2024 – Nov 2, 2024

Subsystems (2)
lore
Share & tools
lore

Orange Kao

author
#0 lore raw

cover EDAC/igen6: Avoid segmentation fault and add polling support

Hello. This is Orange from Aiven Australia. I want to propose two patches to fix a bug that affects my machine with Intel N100.

Patch 1: Avoid segmentation fault during rmmod
Patch 2: Add polling support

The detailed reproduce steps has been documented in kernel bugzilla 219360. Summary below

I have a PC with Intel N100 (with PCI device 8086:461c, DID_ADL_N_SKU4 in igen6_edac.c) with a BIOS/UEFI that allows me to enable IBECC and error injection.

Interrupt seems not working in Linux, but it seems able to detect the error when "modprobe igen6_edac". I am not sure if this is a BIOS bug or not, but I have no access to BIOS source code. So I tried to implement polling, and it seems to work. My proposal in patch 2.

Also "rmmod igen6_edac" can trigger segmentation fault. It seems caused by double kfree on the same memory address. I tried to fix it in patch 1.

Thanks for considering this. I am new to this area so I could be wrong, and I might need extra help and guidance.

author
#1 lore raw

PATCH 1/2 EDAC/igen6: Avoid segmentation fault when rmmod

The segmentation fault happens because

During modprobe:
1. In igen6_probe(), igen6_pvt will be allocated with kzalloc()
2. In igen6_register_mci(), mci->pvt_info will point to &igen6_pvt->imc[mc]

During rmmod:
1. In mci_release() in edac_mc.c, it will kfree(mci->pvt_info)
2. In igen6_remove(), it will kfree(igen6_pvt);

And that caused double kfree on the same memory address. My proposal is to set mci->pvt_info to NULL to avoid double-kfree.

---
 drivers/edac/igen6_edac.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/edac/igen6_edac.c +2 -0
index 189a2fc29e74..07dacf8c10be 100644
@@ -1245,6 +1245,7 @@ static int igen6_register_mci(int mc, u64 mchbar, struct pci_dev *pdev)
 	imc->mci = mci;
 	return 0;
 fail3:
+	mci->pvt_info = NULL;
 	kfree(mci->ctl_name);
 fail2:
 	edac_mc_free(mci);
@@ -1269,6 +1270,7 @@ static void igen6_unregister_mcis(void)
 
 		edac_mc_del_mc(mci->pdev);
 		kfree(mci->ctl_name);
+		mci->pvt_info = NULL;
 		edac_mc_free(mci);
 		iounmap(imc->window);
 	}
Signature · 1 line
2.47.0
#2 lore raw

RE: [PATCH 1/2] EDAC/igen6: Avoid segmentation fault when rmmod

Quoted 3 lines

From: Orange Kao <[email protected]>
[...]
Subject: [PATCH 1/2] EDAC/igen6: Avoid segmentation fault when rmmod

This is a bug. Let's fix it. Suggested subject as fellow:

EDAC/igen6: Fix segmentation fault when rmmod

Quoted 6 lines

The segmentation fault happens because
During modprobe:
1. In igen6_probe(), igen6_pvt will be allocated with kzalloc() 2. In igen6_register_mci(), mci->pvt_info will point to &igen6_pvt->imc[mc]

Quoted 7 lines

During rmmod:
1. In mci_release() in edac_mc.c, it will kfree(mci->pvt_info) 2. In igen6_remove(), it will kfree(igen6_pvt);

And that caused double kfree on the same memory address. My proposal is to set
mci->pvt_info to NULL to avoid double-kfree.

Please change the last sentence to:

Fix this issue by setting mci->pvt_info to NULL to avoid the double kfree.

And append the following tags here:

Fixes 10590a9d4f23 ("EDAC/igen6: Add EDAC driver for Intel client SoCs using IBECC")

Thanks!
-Qiuxu

author
#3 lore raw

PATCH 2/2 EDAC/igen6: Add polling support

In reply to Orange Kao

I have a PC with Intel N100 (with PCI device 8086:461c, DID_ADL_N_SKU4) but interrupt does not seems to work, even with the following configuration in BIOS. I am not sure if this is caused by a BIOS bug or not.

In-Band ECC Support: Enabled
In-Band ECC Operation Mode: 2 (make all requests protected and
ignore range checks)
IBECC Error Injection Control: Inject Correctable Error on insertion
counter
Error Injaction Insertion Count: 251658240 (0xf000000)

This commit tried to add polling support.

---
 drivers/edac/igen6_edac.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/edac/igen6_edac.c +22 -1
index 07dacf8c10be..5027070410a5 100644
@@ -1170,6 +1170,19 @@ static int igen6_pci_setup(struct pci_dev *pdev, u64 *mchbar)
 	return -ENODEV;
 }
 
+static void igen6_check(struct mem_ctl_info *mci)
+{
+	struct igen6_imc *imc = mci->pvt_info;
+
+	/* errsts_clear() isn't NMI-safe. Delay it in the IRQ context */
+	u64 ecclog = ecclog_read_and_clear(imc);
+	if (!ecclog)
+		return;
+	if (!ecclog_gen_pool_add(imc->mc, ecclog))
+		irq_work_queue(&ecclog_irq_work);
+
+}
+
 static int igen6_register_mci(int mc, u64 mchbar, struct pci_dev *pdev)
 {
 	struct edac_mc_layer layers[2];
@@ -1211,6 +1224,9 @@ static int igen6_register_mci(int mc, u64 mchbar, struct pci_dev *pdev)
 	mci->edac_cap = EDAC_FLAG_SECDED;
 	mci->mod_name = EDAC_MOD_STR;
 	mci->dev_name = pci_name(pdev);
+	if (edac_op_state == EDAC_OPSTATE_POLL) {
+		mci->edac_check = igen6_check;
+	}
 	mci->pvt_info = &igen6_pvt->imc[mc];
 
 	imc = mci->pvt_info;
@@ -1450,7 +1466,9 @@ static int __init igen6_init(void)
 	if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
 		return -EBUSY;
 
-	edac_op_state = EDAC_OPSTATE_NMI;
+	if (edac_op_state == EDAC_OPSTATE_INVAL) {
+		edac_op_state = EDAC_OPSTATE_NMI;
+	}
 
 	rc = pci_register_driver(&igen6_driver);
 	if (rc)
@@ -1474,3 +1492,6 @@ module_exit(igen6_exit);
 MODULE_LICENSE("GPL v2");
 MODULE_AUTHOR("Qiuxu Zhuo");
 MODULE_DESCRIPTION("MC Driver for Intel client SoC using In-Band ECC");
+
+module_param(edac_op_state, int, 0444);
+MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI. Default=1");
Signature · 1 line
2.47.0
#4 lore raw

RE: [PATCH 2/2] EDAC/igen6: Add polling support

Hi Orange,

There are two things to do:

1) Set ' edac_op_state' correctly (preparation work). This is provided as a separate patch in [1].

2) Add polling mode support (your current patch). Please incorporate [2] into your current patch and update your current patch on top of [1] with the comments below.

Eventually, there will be 3 patches in your series. Could you please re-send all these 3 patches after you resolve the comments below?

If any questions, feel free to let me know. Thanks!

TIP: the command below can quickly provide you with useful comments for your patch.

    ${LINUX}/scripts/checkpatch.pl --strict   <your patch>
Quoted 6 lines

From: Orange Kao <[email protected]>
[...]

I have a PC with Intel N100 (with PCI device 8086:461c, DID_ADL_N_SKU4) but interrupt does not seems to work, even with the following configuration in BIOS. I am not sure if this is caused by a BIOS bug or not.

Tweak this commit message a bit as below:

Some PCs with Intel N100 (with PCI device 8086:461c, DID_ADL_N_SKU4) experienced issues with error interrupts not working, even with the following configuration in the BIOS.

Quoted 7 lines

In-Band ECC Support: Enabled
In-Band ECC Operation Mode: 2 (make all requests protected and
ignore range checks)
IBECC Error Injection Control: Inject Correctable Error on insertion
counter
Error Injaction Insertion Count: 251658240 (0xf000000)

s/Injaction/Injection

Quoted 2 lines

This commit tried to add polling support.

Tweak this sentence a bit as below:

Add polling mode support for these machines to ensure that memory error events are handled.

Also append your "Signed-off-by:" tag here.

Quoted 10 lines
---
 drivers/edac/igen6_edac.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/edac/igen6_edac.c index renamed from drivers/edac/igen6_edac.c +0 -0

07dacf8c10be..5027070410a5 100644

drivers/edac/igen6_edac.c +0 -0
@@ -1170,6 +1170,19 @@ static int igen6_pci_setup(struct pci_dev *pdev,
 u64 *mchbar)

drivers/edac/igen6_edac.c static int igen6_pci_setup(struct pci_dev *pdev, · line 1180

Quoted 10 lines
drivers/edac/igen6_edac.c +7 -0
 	return -ENODEV;
 }
 
+static void igen6_check(struct mem_ctl_info *mci) {
+	struct igen6_imc *imc = mci->pvt_info;
+
+	/* errsts_clear() isn't NMI-safe. Delay it in the IRQ context */
+	u64 ecclog = ecclog_read_and_clear(imc);
+	if (!ecclog)
+		return;

Add a blank line here.

Quoted 19 lines
drivers/edac/igen6_edac.c +8 -0
+	if (!ecclog_gen_pool_add(imc->mc, ecclog))
+		irq_work_queue(&ecclog_irq_work);
+
+}
+
 static int igen6_register_mci(int mc, u64 mchbar, struct pci_dev *pdev)  {
 	struct edac_mc_layer layers[2];
@@ -1211,6 +1224,9 @@ static int igen6_register_mci(int mc, u64 mchbar,
 struct pci_dev *pdev)
 	mci->edac_cap = EDAC_FLAG_SECDED;
 	mci->mod_name = EDAC_MOD_STR;
 	mci->dev_name = pci_name(pdev);
+	if (edac_op_state == EDAC_OPSTATE_POLL) {
+		mci->edac_check = igen6_check;
+	}
 	mci->pvt_info = &igen6_pvt->imc[mc];
 
drivers/edac/igen6_edac.c +0 -0
 	imc = mci->pvt_info;

drivers/edac/igen6_edac.c static int __init igen6_init(void) · line 1472

Quoted 8 lines
drivers/edac/igen6_edac.c +3 -1
@@ -1450,7 +1466,9 @@ static int __init igen6_init(void)
 	if (owner && strncmp(owner, EDAC_MOD_STR,
 sizeof(EDAC_MOD_STR)))
 		return -EBUSY;
 
-	edac_op_state = EDAC_OPSTATE_NMI;
+	if (edac_op_state == EDAC_OPSTATE_INVAL) {
+		edac_op_state = EDAC_OPSTATE_NMI;
+	}

These 4 diffs are not needed after the separate patch [1].

Quoted 9 lines
drivers/edac/igen6_edac.c +0 -0
 	rc = pci_register_driver(&igen6_driver);
 	if (rc)
@@ -1474,3 +1492,6 @@ module_exit(igen6_exit);  MODULE_LICENSE("GPL

v2"); MODULE_AUTHOR("Qiuxu Zhuo"); MODULE_DESCRIPTION("MC Driver for Intel client SoC using In-Band ECC");

drivers/edac/igen6_edac.c +2 -0
+
+module_param(edac_op_state, int, 0444);

MODULE_PARM_DESC(edac_op_state,
+"EDAC Error Reporting state: 0=Poll,1=NMI. Default=1");

The driver now supports polling mode or detects it based on the configuration mode. Please replace this line with the one in [2] accordingly.

Quoted 2 lines

--
2.47.0

=======

[1]
From 182c717f9f46a9dc0711a8d9a1c42be6deb2cdb3 Mon Sep 17 00:00:00 2001
From: Qiuxu Zhuo <[email protected]>
Date: Sat, 2 Nov 2024 16:15:09 +0800
Subject: [PATCH 1/1] EDAC/igen6: Initialize edac_op_state according to the
configuration data

Currently, igen6_edac sets edac_op_state to EDAC_OPSTATE_NMI, while the driver also supports memory errors reported from Machine Check. Initialize edac_op_state to the correct value according to the configuration data that the driver probed.

---
 drivers/edac/igen6_edac.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/edac/igen6_edac.c +11 -2
index 189a2fc29e74..b6ac14b69358 100644
@@ -1348,6 +1348,15 @@ static void unregister_err_handler(void)
        unregister_nmi_handler(NMI_SERR, IGEN6_NMI_NAME);
 }
 
+static void opstate_set(struct res_config *cfg)
+{
+       /* Set the mode according to the configuration data. */
+       if (cfg->machine_check)
+               edac_op_state = EDAC_OPSTATE_INT;
+       else
+               edac_op_state = EDAC_OPSTATE_NMI;
+}
+
 static int igen6_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
        u64 mchbar;
@@ -1365,6 +1374,8 @@ static int igen6_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
        if (rc)
                goto fail;
 
+       opstate_set(res_cfg);
+
        for (i = 0; i < res_cfg->num_imc; i++) {
                rc = igen6_register_mci(i, mchbar, pdev);
                if (rc)
@@ -1448,8 +1459,6 @@ static int __init igen6_init(void)
        if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
                return -EBUSY;
 
-       edac_op_state = EDAC_OPSTATE_NMI;
-
        rc = pci_register_driver(&igen6_driver);
        if (rc)
                return rc;

--
2.17.1

[2]

drivers/edac/igen6_edac.c +5 -1
index 4f38bb1c1fc3..c8b6e41a12b5 100644
@@ -1368,6 +1368,10 @@ static void unregister_err_handler(void)
 
 static void opstate_set(struct res_config *cfg)
 {
+       /* Only the polling mode can be set via the module parameter. */
+       if (edac_op_state == EDAC_OPSTATE_POLL)
+               return;
+
        /* Set the mode according to the configuration data. */
        if (cfg->machine_check)
                edac_op_state = EDAC_OPSTATE_INT;
@@ -1505,4 +1509,4 @@ MODULE_AUTHOR("Qiuxu Zhuo");
 MODULE_DESCRIPTION("MC Driver for Intel client SoC using In-Band ECC");
 
 module_param(edac_op_state, int, 0444);
-MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI. Default=1");
+MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll, Others or default=Auto detect");
#5 lore raw
In reply to Orange Kao
Quoted 7 lines

From: Orange Kao <[email protected]>
[...]
Subject: [PATCH 0/2] EDAC/igen6: Avoid segmentation fault and add polling support

Hello. This is Orange from Aiven Australia. I want to propose two patches to fix a bug that affects my machine with Intel N100.

Hi Orange, welcome!
Feel free to propose your patches. :-)

Quoted 5 lines

Patch 1: Avoid segmentation fault during rmmod Patch 2: Add polling support

The detailed reproduce steps has been documented in kernel bugzilla 219360. Summary below

Quoted 8 lines

I have a PC with Intel N100 (with PCI device 8086:461c, DID_ADL_N_SKU4 in igen6_edac.c) with a BIOS/UEFI that allows me to enable IBECC and error injection.

Interrupt seems not working in Linux, but it seems able to detect the error when "modprobe igen6_edac". I am not sure if this is a BIOS bug or not, but I have no access to BIOS source code. So I tried to implement polling, and it seems to work. My proposal in patch 2.

Good proposal.

Quoted 2 lines

Also "rmmod igen6_edac" can trigger segmentation fault. It seems caused by double kfree on the same memory address. I tried to fix it in patch 1.

Good finding.

Quoted 2 lines

Thanks for considering this. I am new to this area so I could be wrong, and I might need extra help and guidance.

You've already done well in this analysis and in providing patches. :-) I'll review and add some minor comments to your patches.
If they're OK with you, please re-send the patches after resolving the comments.

Thank you for your cooperation.

-Qiuxu