EDAC/versalnet: Fix error handling, teardown, and robustness
Shubhrajyoti Datta
maintainerv2 cover EDAC/versalnet: Fix error handling, teardown, and robustness
This series addresses several correctness and robustness issues in the VersalNet EDAC driver, including NULL pointer dereferences, resource lifetime problems, shutdown handling, and probe/remove path cleanup.
The main fixes are:
* Robustness fixes
- Add bounds validation in rpmsg_cb().
* Probe and initialization cleanup
- Move platform_set_drvdata() from init_one_mc() to mc_probe().
- Reorder probe flow to initialize MCDI before RPMsg registration.
- Remove unused pdev parameters from internal initialization functions.
* Resource management fixes
- Fix error handling in init_one_mc() to avoid a potential double-free by reordering device_register() and edac_mc_alloc().
- Replace sprintf() with init_name by dev_set_name() to avoid use-after-free issues caused by deferred kobject cleanup.
* Code cleanup
- Use a designated initializer for rpmsg_channel_info.
Changes in v2:
Add the codeveloped tag and remove the non standard signature fix the name stack warning
Use a macro for channel name
Add reviewed by tag
Sashiko comment
"This is a pre-existing issue, but are offset and length validated before being used as indices?"
New patch to fix shashiko review comment "this is a pre-existing issue, but is the per-controller width overwriting the shared state for all controllers?".
Patch addition for the rpmsg endpoint race
Update the labels
Address the sashiko comment
"register_rpmsg_driver() exposes the callback endpoint before setup_mcdi() initializes the mcdi pointer. Could this lead to a NULL pointer dereference if a response arrives early?"
Patch addition for the rpmsg drvdata race
Add reviewed-by tag
Add amd,num-ctrl property to device tree
The property was requested in the discussion below
https://lore.kernel.org/all/[email protected]/
Adding the device tree parameter was suggested in the review in v1
https://lore.kernel.org/all/[email protected]/
New patch added to bailout if the cdx_mcdi_rpc fails.
Hayden Rinn (1):
EDAC/versalnet: Wait for RPMsg endpoint initialization
Prasanna Kumar T S M (1):
EDAC/versalnet: Fix device_register() error handling in init_one_mc()
Shubhrajyoti Datta (10):
EDAC/versalnet: Use designated initializer for rpmsg_channel_info EDAC/versalnet: Fix use-after-free in remove_one_mc() EDAC/versalnet: Add bounds validation in rpmsg_cb() EDAC/versalnet: Make dwidth per-controller
EDAC/versalnet: Initialize MCDI before RPMsg registration EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb() EDAC/versalnet: Move platform_set_drvdata() to mc_probe()
dt-bindings: memory: xlnx,versal-net-ddrmc5: Add amd,num-ctrl property EDAC/versalnet: use device tree property for number of controllers EDAC/versalnet: Make get_ddr_config() return errors
.../xlnx,versal-net-ddrmc5.yaml | 9 + drivers/edac/versalnet_edac.c | 283 +++++++++++++----- 2 files changed, 220 insertions(+), 72 deletions(-)
Signature · 1 line
2.44.4
Shubhrajyoti Datta
maintainerPATCH v2 1/12 EDAC/versalnet: Fix device_register() error handling in init_one_mc()
From: Prasanna Kumar T S M <[email protected]>
When device_register() fails, it must be followed by put_device() rather than kfree(), because device_register() calls device_initialize() which sets up the device refcount. The matching release function versal_edac_release() handles the actual kfree().
To simplify error handling and avoid complex unwinding, split device_register() into device_initialize() and device_add(). Initialize the device early so put_device() can be used in all error paths.
---
Changes in v2:
Add the codeveloped tag and remove the non standard signature fix the name stack warning
drivers/edac/versalnet_edac.c | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/edac/versalnet_edac.c +18 -18
@@ -70,8 +70,6 @@#define XDDR5_BUS_WIDTH_32 1#define XDDR5_BUS_WIDTH_16 2 -#define MC_NAME_LEN 32 -/** * struct ecc_error_info - ECC error log information. * @burstpos: Burst position.
@@ -777,7 +775,6 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int iu32 num_chans, rank, dwidth, config;struct edac_mc_layer layers[2 ];struct mem_ctl_info *mci; -char name[MC_NAME_LEN ];struct device *dev;enum dev_type dt;int rc;
@@ -817,28 +814,31 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int iif (!dev)return rc; + dev->release = versal_edac_release; +device_initialize (dev); + + rc =dev_set_name (dev,"versal-net-ddrmc5-edac-%d" , i); +if (rc) + goto err_put_dev; + + rc =device_add (dev); +if (rc) + goto err_put_dev; + mci =edac_mc_alloc (i,ARRAY_SIZE (layers), layers,sizeof (struct mc_priv ));if (!mci) {edac_printk (KERN_ERR ,EDAC_MC ,"Failed memory allocation for MC%d\n" , i); - goto err_dev_free; + rc = -ENOMEM ; + goto err_dev_del; } -sprintf (name,"versal-net-ddrmc5-edac-%d" , i); - - dev->init_name = name; - dev->release = versal_edac_release; - - rc =device_register (dev); -if (rc) - goto err_mc_free; - mci->pdev = dev;mc_init (mci, dev); rc =edac_mc_add_mc (mci);if (rc) {edac_printk (KERN_ERR ,EDAC_MC ,"Failed to register MC%d with EDAC core\n" , i); - goto err_unreg; + goto err_mc_free; } priv->mci[i] = mci;
@@ -848,12 +848,12 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int ireturn 0 ; -err_unreg: -device_unregister (mci->pdev); err_mc_free:edac_mc_free (mci); -err_dev_free: -kfree (dev); +err_dev_del: +device_del (dev); +err_put_dev: +put_device (dev);return rc; }
Signature · 1 line
2.44.4
Re: [PATCH v2 01/12] EDAC/versalnet: Fix device_register() error handling in init_one_mc()
Quoted 12 lines · Shubhrajyoti Datta
From: Prasanna Kumar T S M <[email protected]>
When
device_register()fails, it must be followed byput_device()rather thankfree(), becausedevice_register()callsdevice_initialize()which sets up the device refcount. The matching release functionversal_edac_release()handles the actualkfree().To simplify error handling and avoid complex unwinding, split
device_register()intodevice_initialize()anddevice_add(). Initialize the device early soput_device()can be used in all error paths.
Quoted 5 lines
Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Prasanna Kumar T S M <[email protected]>Co-developed-by Shubhrajyoti Datta <[email protected]>Signed-off-by Shubhrajyoti Datta <[email protected]>
Thanks!
Quoted 90 lines
---
Changes in v2:
Add the codeveloped tag and remove the non standard signature fix the name stack warningdrivers/edac/versalnet_edac.c | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)drivers/edac/versalnet_edac.c +18
-18@@ -70,8 +70,6 @@#define XDDR5_BUS_WIDTH_32 1#define XDDR5_BUS_WIDTH_16 2 -#define MC_NAME_LEN 32 -/** * struct ecc_error_info - ECC error log information. * @burstpos: Burst position. @@ -777,7 +775,6 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int iu32 num_chans, rank, dwidth, config;struct edac_mc_layer layers[2 ];struct mem_ctl_info *mci; -char name[MC_NAME_LEN ];struct device *dev;enum dev_type dt;int rc;@@ -817,28 +814,31 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int iif (!dev)return rc; + dev->release = versal_edac_release; +device_initialize (dev); + + rc =dev_set_name (dev,"versal-net-ddrmc5-edac-%d" , i); +if (rc) + goto err_put_dev; + + rc =device_add (dev); +if (rc) + goto err_put_dev; + mci =edac_mc_alloc (i,ARRAY_SIZE (layers), layers,sizeof (struct mc_priv ));if (!mci) {edac_printk (KERN_ERR ,EDAC_MC ,"Failed memory allocation for MC%d\n" , i); - goto err_dev_free; + rc = -ENOMEM ; + goto err_dev_del; } -sprintf (name,"versal-net-ddrmc5-edac-%d" , i); - - dev->init_name = name; - dev->release = versal_edac_release; - - rc =device_register (dev); -if (rc) - goto err_mc_free; - mci->pdev = dev;mc_init (mci, dev); rc =edac_mc_add_mc (mci);if (rc) {edac_printk (KERN_ERR ,EDAC_MC ,"Failed to register MC%d with EDAC core\n" , i); - goto err_unreg; + goto err_mc_free; } priv->mci[i] = mci;@@ -848,12 +848,12 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int ireturn 0 ; -err_unreg: -device_unregister (mci->pdev); err_mc_free:edac_mc_free (mci); -err_dev_free: -kfree (dev); +err_dev_del: +device_del (dev); +err_put_dev: +put_device (dev);return rc; }
Re: [PATCH v2 01/12] EDAC/versalnet: Fix device_register() error handling in init_one_mc()
In reply to Shubhrajyoti DattaQuoted 17 lines · Shubhrajyoti Datta
From: Prasanna Kumar T S M <[email protected]>
When
device_register()fails, it must be followed byput_device()rather thankfree(), becausedevice_register()callsdevice_initialize()which sets up the device refcount. The matching release functionversal_edac_release()handles the actualkfree().To simplify error handling and avoid complex unwinding, split
device_register()intodevice_initialize()anddevice_add(). Initialize the device early soput_device()can be used in all error paths.Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Prasanna Kumar T S M <[email protected]>Co-developed-by Shubhrajyoti Datta <[email protected]>Signed-off-by Shubhrajyoti Datta <[email protected]>
Quoted 8 lines
---
Changes in v2:
Add the codeveloped tag and remove the non standard signature fix the name stack warningdrivers/edac/versalnet_edac.c | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)
Applied, thanks.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteShubhrajyoti Datta
maintainerPATCH v2 2/12 EDAC/versalnet: Use designated initializer for rpmsg_channel_info
In reply to Shubhrajyoti DattaReplace field-by-field assignment of struct rpmsg_channel_info with a designated initializer. The old strscpy() call used strlen() as the destination size, which truncated the channel name. Use the string literal (via EDAC_RPMSG_CHAN_NAME) in the initializer so the full name is passed to rpmsg_create_ept().
---
Changes in v2:
Use a macro for channel name
drivers/edac/versalnet_edac.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/edac/versalnet_edac.c +8 -6
@@ -70,6 +70,8 @@#define XDDR5_BUS_WIDTH_32 1#define XDDR5_BUS_WIDTH_16 2 +#define EDAC_RPMSG_CHAN_NAME "error_ipc" +/** * struct ecc_error_info - ECC error log information. * @burstpos: Burst position.
@@ -713,21 +715,21 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, }static struct rpmsg_device_id amd_rpmsg_id_table[] = { - { .name ="error_ipc" }, + { .name =EDAC_RPMSG_CHAN_NAME }, { }, };MODULE_DEVICE_TABLE (rpmsg, amd_rpmsg_id_table);static int rpmsg_probe (struct rpmsg_device *rpdev) { -struct rpmsg_channel_info chinfo; +struct rpmsg_channel_info chinfo = { + .src =RPMSG_ADDR_ANY , + .dst = rpdev->dst, + .name =EDAC_RPMSG_CHAN_NAME , + };struct mc_priv *pg; pg = (struct mc_priv *)amd_rpmsg_id_table[0 ].driver_data; - chinfo.src =RPMSG_ADDR_ANY ; - chinfo.dst = rpdev->dst; -strscpy (chinfo.name, amd_rpmsg_id_table[0 ].name, -strlen (amd_rpmsg_id_table[0 ].name)); pg->ept =rpmsg_create_ept (rpdev, rpmsg_cb,NULL , chinfo);if (!pg->ept)
Signature · 1 line
2.44.4
Re: [PATCH v2 02/12] EDAC/versalnet: Use designated initializer for rpmsg_channel_info
Quoted 8 lines · Shubhrajyoti Datta
Replace field-by-field assignment of struct
rpmsg_channel_infowith a designated initializer. The oldstrscpy()call usedstrlen()as the destination size, which truncated the channel name. Use the string literal (viaEDAC_RPMSG_CHAN_NAME) in the initializer so the full name is passed torpmsg_create_ept().Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>
Thanks!
Quoted 49 lines
---
Changes in v2:
Use a macro for channel namedrivers/edac/versalnet_edac.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)drivers/edac/versalnet_edac.c +8
-6@@ -70,6 +70,8 @@#define XDDR5_BUS_WIDTH_32 1#define XDDR5_BUS_WIDTH_16 2 +#define EDAC_RPMSG_CHAN_NAME "error_ipc" +/** * struct ecc_error_info - ECC error log information. * @burstpos: Burst position. @@ -713,21 +715,21 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, }static struct rpmsg_device_id amd_rpmsg_id_table[] = { - { .name ="error_ipc" }, + { .name =EDAC_RPMSG_CHAN_NAME }, { }, };MODULE_DEVICE_TABLE (rpmsg, amd_rpmsg_id_table);static int rpmsg_probe (struct rpmsg_device *rpdev) { -struct rpmsg_channel_info chinfo; +struct rpmsg_channel_info chinfo = { + .src =RPMSG_ADDR_ANY , + .dst = rpdev->dst, + .name =EDAC_RPMSG_CHAN_NAME , + };struct mc_priv *pg; pg = (struct mc_priv *)amd_rpmsg_id_table[0 ].driver_data; - chinfo.src =RPMSG_ADDR_ANY ; - chinfo.dst = rpdev->dst; -strscpy (chinfo.name, amd_rpmsg_id_table[0 ].name, -strlen (amd_rpmsg_id_table[0 ].name)); pg->ept =rpmsg_create_ept (rpdev, rpmsg_cb,NULL , chinfo);if (!pg->ept)
Borislav Petkov
maintainerRe: [PATCH v2 02/12] EDAC/versalnet: Use designated initializer for rpmsg_channel_info
In reply to Shubhrajyoti DattaQuoted 8 lines · Shubhrajyoti Datta
Replace field-by-field assignment of struct
rpmsg_channel_infowith a designated initializer. The oldstrscpy()call usedstrlen()as the destination size, which truncated the channel name. Use the string literal (viaEDAC_RPMSG_CHAN_NAME) in the initializer so the full name is passed torpmsg_create_ept().Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>
Quoted 7 lines
---
Changes in v2:
Use a macro for channel namedrivers/edac/versalnet_edac.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
Changes ontop:
drivers/edac/versalnet_edac.c +5 -5
@@ -70,7 +70,7 @@#define XDDR5_BUS_WIDTH_32 1#define XDDR5_BUS_WIDTH_16 2 -#define EDAC_RPMSG_CHAN_NAME "error_ipc" +#define RPMSG_CHAN_NAME "error_ipc"/** * struct ecc_error_info - ECC error log information.
@@ -715,7 +715,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, }static struct rpmsg_device_id amd_rpmsg_id_table[] = { - { .name =EDAC_RPMSG_CHAN_NAME }, + { .name =RPMSG_CHAN_NAME }, { }, };MODULE_DEVICE_TABLE (rpmsg, amd_rpmsg_id_table);
@@ -723,9 +723,9 @@ MODULE_DEVICE_TABLE(rpmsg, amd_rpmsg_id_table);static int rpmsg_probe (struct rpmsg_device *rpdev) {struct rpmsg_channel_info chinfo = { - .src =RPMSG_ADDR_ANY , - .dst = rpdev->dst, - .name =EDAC_RPMSG_CHAN_NAME , + .src =RPMSG_ADDR_ANY , + .dst = rpdev->dst, + .name =RPMSG_CHAN_NAME , };struct mc_priv *pg;
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteShubhrajyoti Datta
maintainerPATCH v2 3/12 EDAC/versalnet: Fix use-after-free in remove_one_mc()
In reply to Shubhrajyoti Dattadevice_unregister() drops the last reference on the device and invokes versal_edac_release() which calls kfree(dev). The subsequent call to edac_mc_del_mc(mci->pdev) then dereferences the freed pointer.
Fix by saving the device pointer, calling edac_mc_del_mc() and edac_mc_free() first, then device_unregister() last so the device is freed only after all users are done with it.
---
Changes in v2:
Add reviewed by tag
drivers/edac/versalnet_edac.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/edac/versalnet_edac.c +4 -2
@@ -765,11 +765,13 @@ static void versal_edac_release(struct device *dev)static void remove_one_mc (struct mc_priv *priv,int i) {struct mem_ctl_info *mci; +struct device *dev; mci = priv->mci[i]; -device_unregister (mci->pdev); -edac_mc_del_mc (mci->pdev); + dev = mci->pdev; +edac_mc_del_mc (dev);edac_mc_free (mci); +device_unregister (dev); }static int init_one_mc (struct mc_priv *priv,struct platform_device *pdev,int i)
Signature · 1 line
2.44.4
Borislav Petkov
maintainerRe: [PATCH v2 03/12] EDAC/versalnet: Fix use-after-free in remove_one_mc()
Quoted 7 lines · Shubhrajyoti Datta
drivers/edac/versalnet_edac.c +0
-0@@ -765,11 +765,13 @@ static void versal_edac_release(struct device *dev)static void remove_one_mc (struct mc_priv *priv,int i) {
drivers/edac/versalnet_edac.c static void versal_edac_release(struct device *dev) · line 774
Quoted 10 lines
drivers/edac/versalnet_edac.c +4
-2struct mem_ctl_info *mci; +struct device *dev; mci = priv->mci[i]; -device_unregister (mci->pdev); -edac_mc_del_mc (mci->pdev); + dev = mci->pdev; +edac_mc_del_mc (dev);edac_mc_free (mci); +device_unregister (dev);
Btw, if this is using device_unregister(), you probably should simplify init_one_mc() by using device_register() there instead of the two initialize and add steps you're using now.
In any case, first three queued.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteShubhrajyoti Datta
maintainerPATCH v2 4/12 EDAC/versalnet: Add bounds validation in rpmsg_cb()
In reply to Shubhrajyoti DattaThe firmware-supplied offset and length values from the RPMsg payload are used without validation to index mc_priv->regs[] (REG_MAX entries). A malformed or buggy firmware message could access past the end of the array, corrupting adjacent structure members or kernel memory.
Add bounds checks for the offset and length before accessing the array.
rpmsg_cb() supports messages split across two RPMsg callbacks. If validation fails for either part of a split message, discard the entire message.
---
Changes in v2:
Sashiko comment
"This is a pre-existing issue, but are offset and length validated before being used as indices?"
drivers/edac/versalnet_edac.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/edac/versalnet_edac.c +7 -0
@@ -607,6 +607,13 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, length = result[MSG_ERR_LENGTH ]; offset = result[MSG_ERR_OFFSET ]; +if (length >REG_MAX || offset >REG_MAX - length || + result[TOTAL_ERR_LENGTH ] >REG_MAX ) { +dev_warn_ratelimited (&rpdev->dev, +"Rejecting the faulty message\n" ); +return -EINVAL ; + } +/* * The data can come in two stretches. Construct the regs from two * messages. The offset indicates the offset from which the data is to
Signature · 1 line
2.44.4
[email protected]
AI reviewRe: [PATCH v2 04/12] EDAC/versalnet: Add bounds validation in rpmsg_cb()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] EDAC/versalnet: stale part_len on split-message failure in rpmsg_cb() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] EDAC/versalnet: stale part_len on split-message failure in rpmsg_cb()
--
commit a43cb655a4c7983a62adb71672a8f139d96e6c3c
Author: Shubhrajyoti Datta <[email protected]>
EDAC/versalnet: Add bounds validation in rpmsg_cb()
This commit adds bounds checks on the RPMsg payload's offset, length, and total error length before indexing mc_priv->regs[] in rpmsg_cb().
Quoted 8 lines
drivers/edac/versalnet_edac.c +0
-0@@ -607,6 +607,13 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, length = result[MSG_ERR_LENGTH ]; offset = result[MSG_ERR_OFFSET ];
drivers/edac/versalnet_edac.c static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, · line 619
Quoted 10 lines
drivers/edac/versalnet_edac.c +7
-0+if (length >REG_MAX || offset >REG_MAX - length || + result[TOTAL_ERR_LENGTH ] >REG_MAX ) { +dev_warn_ratelimited (&rpdev->dev, +"Rejecting the faulty message\n" ); +return -EINVAL ; + } +/* * The data can come in two stretches. Construct the regs from two * messages. The offset indicates the offset from which the data is to
[Severity: Medium]
Does rejecting one part of a split message leave mc_priv->part_len out of sync instead of discarding the entire message?
When the first part of a split message succeeds, rpmsg_cb() stores its length in mc_priv->part_len and returns 0:
drivers/edac/versalnet_edac.c:rpmsg_cb() {
...
if (result[TOTAL_ERR_LENGTH] > length) {
if (!mc_priv->part_len)
mc_priv->part_len = length;
else
mc_priv->part_len += length;
if (mc_priv->part_len < result[TOTAL_ERR_LENGTH])
return 0;
mc_priv->part_len = 0;
}
...
}If the second part then fails the bounds check and returns -EINVAL, mc_priv->part_len is not reset to 0. When the first part of the next valid split message arrives, its length is added to the stale mc_priv->part_len, causing rpmsg_cb() to report an error prematurely from incomplete and stale data in mc_priv->regs[] and leaving mc_priv->part_len desynchronized for subsequent split messages.
Conversely, if the first part of a split message is rejected (leaving mc_priv->part_len at 0) and the second part arrives with a valid non-zero offset, rpmsg_cb() does not check that offset matches mc_priv->part_len and accepts the orphaned second fragment.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Datta, Shubhrajyoti
maintainerRE: [PATCH v2 04/12] EDAC/versalnet: Add bounds validation in rpmsg_cb()
Public
Quoted 12 lines
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Wednesday, November 18, 2026 8:03 PM
To: Datta, Shubhrajyoti <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2 04/12] EDAC/versalnet: Add bounds validation inrpmsg_cb()Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 5 lines
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] EDAC/versalnet: stalepart_lenon split-message failure inrpmsg_cb()
--
Agree will fix in the next version
Re: [PATCH v2 04/12] EDAC/versalnet: Add bounds validation in rpmsg_cb()
In reply to Shubhrajyoti DattaQuoted 7 lines · Shubhrajyoti Datta
The firmware-supplied offset and length values from the RPMsg payload are used without validation to index mc_priv->regs[] (
REG_MAXentries). A malformed or buggy firmware message could access past the end of the array, corrupting adjacent structure members or kernel memory.Add bounds checks for the offset and length before accessing the array.
Quoted 7 lines
rpmsg_cb()supports messages split across two RPMsg callbacks. If validation fails for either part of a split message, discard the entire message.Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>---
Let's fix sashiko reported bug.
- [Medium] EDAC/versalnet: stale part_len on split-message failure in rpmsg_cb()
Quoted 27 lines
Changes in v2:
Sashiko comment
"This is a pre-existing issue, but are offset and length validated before being used as indices?"drivers/edac/versalnet_edac.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/edac/versalnet_edac.c +7
-0@@ -607,6 +607,13 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, length = result[MSG_ERR_LENGTH ]; offset = result[MSG_ERR_OFFSET ]; +if (length >REG_MAX || offset >REG_MAX - length || + result[TOTAL_ERR_LENGTH ] >REG_MAX ) { +dev_warn_ratelimited (&rpdev->dev, +"Rejecting the faulty message\n" ); +return -EINVAL ; + } +/* * The data can come in two stretches. Construct the regs from two * messages. The offset indicates the offset from which the data is to
Shubhrajyoti Datta
maintainerPATCH v2 5/12 EDAC/versalnet: Make dwidth per-controller
In reply to Shubhrajyoti DattaCurrently dwidth is a single scalar in mc_priv, so each call to init_one_mc() overwrites the previous controller's value.
Fix this by converting dwidth to a per-controller array and passing the device type directly to mc_init() from the detected dt. This also updates the DIMM device type reported in EDAC.
---
Changes in v2:
https://sashiko.dev/#/patchset/20260724171945.2812749-1-shubhrajyoti.datta%40amd.com
New patch to fix shashiko review comment "this is a pre-existing issue, but is the per-controller width overwriting the shared state for all controllers?".
drivers/edac/versalnet_edac.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/edac/versalnet_edac.c +8 -8
@@ -140,7 +140,7 @@ struct ecc_status {* @stat: ECC status information. * @error_id: The error id. * @error_level: The error level. -* @dwidth: Width of data bus excluding ECC bits. +* @dwidth: Per-controller width of data bus excluding ECC bits. * @part_len: The support of the message received. * @regs: The registers sent on the rpmsg. * @adec: Address decode registers.
@@ -153,7 +153,7 @@ struct mc_priv {struct ecc_status stat;u32 error_id;u32 error_level; -u32 dwidth; +enum dev_type dwidth[NUM_CONTROLLERS ];u32 part_len;u32 regs[REG_MAX ];u32 adec[ADEC_MAX ];
@@ -399,7 +399,7 @@ static unsigned long convert_to_physical(struct mc_priv *priv,else ilc_base_ctrl_add = ilcmem_base - low_mem_offset; -if (priv->dwidth ==DEV_X16 ) { +if (priv->dwidth[controller] ==DEV_X16 ) { blk = err_addr /MC5_X16_SIZE ; rsh_req_addr = (blk <<8 ) + ilc_base_ctrl_add; err_addr = rsh_req_addr * interleave *2 ;
@@ -473,9 +473,9 @@ static void handle_error(struct mc_priv *priv, struct ecc_status *stat, } } -static void mc_init (struct mem_ctl_info *mci,struct device *dev) +static void mc_init (struct mem_ctl_info *mci,struct device *dev, +enum dev_type dtype) { -struct mc_priv *priv = mci->pvt_info;struct csrow_info *csi;struct dimm_info *dimm;u32 row;
@@ -501,7 +501,7 @@ static void mc_init(struct mem_ctl_info *mci, struct device *dev) dimm->edac_mode =EDAC_SECDED ; dimm->mtype =MEM_DDR5 ; dimm->grain =MC5_ERR_GRAIN ; - dimm->dtype = priv->dwidth; + dimm->dtype = dtype; } } }
@@ -844,7 +844,7 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i } mci->pdev = dev; -mc_init (mci, dev); +mc_init (mci, dev, dt); rc =edac_mc_add_mc (mci);if (rc) {
@@ -853,7 +853,7 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i } priv->mci[i] = mci; - priv->dwidth = dt; + priv->dwidth[i] = dt;platform_set_drvdata (pdev, priv);
Signature · 1 line
2.44.4
Re: [PATCH v2 05/12] EDAC/versalnet: Make dwidth per-controller
Quoted 2 lines · Shubhrajyoti Datta
Currently dwidth is a single scalar in
mc_priv, so each call toinit_one_mc()overwrites the previous controller's value.
Let's also explain side effect of it i.e convert_to_physical() indexes the address decode registers per controller but reads that shared value, so on a part whose controllers are not identically configured it computes the wrong physical address - and for an uncorrectable error that address is passed to memory_failure().
Quoted 3 lines
Fix this by converting dwidth to a per-controller array and passing the device type directly to
mc_init()from the detected dt.
dt? rephrase the variable as dtype.
Quoted 20 lines
This also updates the DIMM device type reported in EDAC.
Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>---
Changes in v2:
https://sashiko.dev/#/patchset/20260724171945.2812749-1-shubhrajyoti.datta%40amd.com
New patch to fix shashiko review comment "this is a pre-existing issue, but is the per-controller width overwriting the shared state for all controllers?".drivers/edac/versalnet_edac.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)drivers/edac/versalnet_edac.c +0
-0
drivers/edac/versalnet_edac.c struct ecc_status { · line 143
Quoted 5 lines
drivers/edac/versalnet_edac.c +1
-1@@ -140,7 +140,7 @@ struct ecc_status {* @stat: ECC status information. * @error_id: The error id. * @error_level: The error level. -* @dwidth: Width of data bus excluding ECC bits. +* @dwidth: Per-controller width of data bus excluding ECC bits.
Nit - data bus width
Quoted 60 lines
drivers/edac/versalnet_edac.c +7
-7* @part_len: The support of the message received. * @regs: The registers sent on the rpmsg. * @adec: Address decode registers. @@ -153,7 +153,7 @@ struct mc_priv {struct ecc_status stat;u32 error_id;u32 error_level; -u32 dwidth; +enum dev_type dwidth[NUM_CONTROLLERS ];u32 part_len;u32 regs[REG_MAX ];u32 adec[ADEC_MAX ];@@ -399,7 +399,7 @@ static unsigned long convert_to_physical(struct mc_priv *priv,else ilc_base_ctrl_add = ilcmem_base - low_mem_offset; -if (priv->dwidth ==DEV_X16 ) { +if (priv->dwidth[controller] ==DEV_X16 ) { blk = err_addr /MC5_X16_SIZE ; rsh_req_addr = (blk <<8 ) + ilc_base_ctrl_add; err_addr = rsh_req_addr * interleave *2 ;@@ -473,9 +473,9 @@ static void handle_error(struct mc_priv *priv, struct ecc_status *stat, } } -static void mc_init (struct mem_ctl_info *mci,struct device *dev) +static void mc_init (struct mem_ctl_info *mci,struct device *dev, +enum dev_type dtype) { -struct mc_priv *priv = mci->pvt_info;struct csrow_info *csi;struct dimm_info *dimm;u32 row;@@ -501,7 +501,7 @@ static void mc_init(struct mem_ctl_info *mci, struct device *dev) dimm->edac_mode =EDAC_SECDED ; dimm->mtype =MEM_DDR5 ; dimm->grain =MC5_ERR_GRAIN ; - dimm->dtype = priv->dwidth; + dimm->dtype = dtype; } } }@@ -844,7 +844,7 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i } mci->pdev = dev; -mc_init (mci, dev); +mc_init (mci, dev, dt); rc =edac_mc_add_mc (mci);if (rc) {@@ -853,7 +853,7 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i } priv->mci[i] = mci; - priv->dwidth = dt; + priv->dwidth[i] = dt;platform_set_drvdata (pdev, priv);
Re: [PATCH v2 05/12] EDAC/versalnet: Make dwidth per-controller
In reply to Shubhrajyoti DattaQuoted 52 lines · Shubhrajyoti Datta
Currently dwidth is a single scalar in
mc_priv, so each call toinit_one_mc()overwrites the previous controller's value.Fix this by converting dwidth to a per-controller array and passing the device type directly to
mc_init()from the detected dt. This also updates the DIMM device type reported in EDAC.Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>---
Changes in v2:
https://sashiko.dev/#/patchset/20260724171945.2812749-1-shubhrajyoti.datta%40amd.com
New patch to fix shashiko review comment "this is a pre-existing issue, but is the per-controller width overwriting the shared state for all controllers?".drivers/edac/versalnet_edac.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)drivers/edac/versalnet_edac.c +3
-3@@ -140,7 +140,7 @@ struct ecc_status {* @stat: ECC status information. * @error_id: The error id. * @error_level: The error level. -* @dwidth: Width of data bus excluding ECC bits. +* @dwidth: Per-controller width of data bus excluding ECC bits. * @part_len: The support of the message received. * @regs: The registers sent on the rpmsg. * @adec: Address decode registers. @@ -153,7 +153,7 @@ struct mc_priv {struct ecc_status stat;u32 error_id;u32 error_level; -u32 dwidth; +enum dev_type dwidth[NUM_CONTROLLERS ];u32 part_len;u32 regs[REG_MAX ];u32 adec[ADEC_MAX ];@@ -399,7 +399,7 @@ static unsigned long convert_to_physical(struct mc_priv *priv,else ilc_base_ctrl_add = ilcmem_base - low_mem_offset; -if (priv->dwidth ==DEV_X16 ) { +if (priv->dwidth[controller] ==DEV_X16 ) { blk = err_addr /MC5_X16_SIZE ; rsh_req_addr = (blk <<8 ) + ilc_base_ctrl_add; err_addr = rsh_req_addr * interleave *2 ;
drivers/edac/versalnet_edac.c static void handle_error(struct mc_priv *priv, struct ecc_status *stat, · line 478
Quoted 8 lines
drivers/edac/versalnet_edac.c +2
-2@@ -473,9 +473,9 @@ static void handle_error(struct mc_priv *priv, struct ecc_status *stat, } } -static void mc_init (struct mem_ctl_info *mci,struct device *dev) +static void mc_init (struct mem_ctl_info *mci,struct device *dev, +enum dev_type dtype) { -struct mc_priv *priv = mci->pvt_info;
With this change, now there are no users of mci->pvt_info.
So you could save some memory by not allocating it.
mci = edac_mc_alloc(i, ARRAY_SIZE(layers), layers, 0);
You set the last parameter to '0' ^^^
We have the same idea in amd64_edac.
Thanks,
Yazen
Shubhrajyoti Datta
maintainerPATCH v2 6/12 EDAC/versalnet: Wait for RPMsg endpoint initialization
In reply to Shubhrajyoti DattaFrom: Hayden Rinn <[email protected]>
The rpmsg endpoint can be used before rpmsg_probe() completes, and can race with asynchronous rpmsg_remove() teardown (e.g. R5 crash or shutdown), both leading to use of an uninitialized or freed endpoint.
Add a completion so mc_probe() waits for the endpoint, and a "ready" flag so rpmsg_cb() drops events until mci/adec are set up. Add ept_lock to serialize priv->ept and priv->mcdi->ept across mcdi_request(), rpmsg_probe(), and rpmsg_remove(), clearing them on removal and failing mcdi_request() if the channel is down.
---
Changes in v2:
Patch addition for the rpmsg endpoint race
drivers/edac/versalnet_edac.c | 123 ++++++++++++++++++++++++++++++---- 1 file changed, 111 insertions(+), 12 deletions(-)
drivers/edac/versalnet_edac.c +111 -12
@@ -5,8 +5,10 @@*/ #include <linux/cdx/edac_cdx_pcol.h> +#include <linux/completion.h> #include <linux/edac.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/of_device.h> #include <linux/ras.h> #include <linux/remoteproc.h>
@@ -146,7 +148,22 @@ struct ecc_status {* @adec: Address decode registers. * @mci: Memory controller interface. * @ept: rpmsg endpoint. +* @ept_lock: Protects @ept and @mcdi->ept against concurrent teardown +* by rpmsg_remove() (e.g. remote processor crash/shutdown) +* while mcdi_request() is sending on them. +* @rpmsg_ready: Completion signaled once rpmsg_probe() has run to +* completion success. @ept is only valid to check +* once this is signaled. * @mcdi: The mcdi handle. +* @dev: Platform device backing this driver instance. Stays +* valid for as long as mcdi_request() can be called, +* unlike @mcdi->rpdev, which is torn down and cleared +* by rpmsg_remove(). Used for logging when there is no +* live rpmsg channel to hang the message off of. +* @ready: Set once edac priv data is initialised. Published with a +* release store and read with an acquire load so that rpmsg_cb +* never calls handle_error() against a @mci entry that hasn't +* been allocated yet. */ struct mc_priv {char message[256 ];
@@ -159,7 +176,11 @@ struct mc_priv {u32 adec[ADEC_MAX ];struct mem_ctl_info *mci[NUM_CONTROLLERS ];struct rpmsg_endpoint *ept; +struct mutex ept_lock;/* Protects endpoint */ +struct completion rpmsg_ready;struct cdx_mcdi *mcdi; +struct device *dev; +bool ready; };/*
@@ -508,6 +529,12 @@ static void mc_init(struct mem_ctl_info *mci, struct device *dev,#define to_mci (k) container_of(k, struct mem_ctl_info, dev) +static struct rpmsg_device_id amd_rpmsg_id_table[] = { + { .name =EDAC_RPMSG_CHAN_NAME }, + { }, +}; +MODULE_DEVICE_TABLE (rpmsg, amd_rpmsg_id_table); +static unsigned int mcdi_rpc_timeout (struct cdx_mcdi *cdx,unsigned int cmd) {return MCDI_RPC_TIMEOUT ;
@@ -517,6 +544,7 @@ static void mcdi_request(struct cdx_mcdi *cdx, const struct cdx_dword *hdr, size_t hdr_len, const struct cdx_dword *sdu, size_t sdu_len) { + struct mc_priv *mc_priv = (struct mc_priv *)amd_rpmsg_id_table[0].driver_data;void *send_buf;int ret;
@@ -527,6 +555,24 @@ static void mcdi_request(struct cdx_mcdi *cdx,memcpy (send_buf, hdr, hdr_len);memcpy (send_buf + hdr_len, sdu, sdu_len); +guard (mutex)(&mc_priv->ept_lock); + +/* +* The rpmsg channel can be torn down asynchronously (e.g. the R5 +* remote processor crashing or being shut down) by rpmsg_remove(), +* which clears both @cdx->ept and @mc_priv->ept under this same +* lock. Bail out instead of sending on a destroyed endpoint. +* +* @cdx->rpdev is cleared by rpmsg_remove() together with @cdx->ept, +* so it cannot be used for logging here; use the stable platform +* device instead. +*/ +if (!cdx->ept) { +dev_err (mc_priv->dev,"Dropping rpmsg send, channel is down\n" ); +kfree (send_buf); +return ; + } + ret =rpmsg_send (cdx->ept, send_buf, hdr_len + sdu_len);if (ret)dev_err (&cdx->rpdev->dev,"Failed to send rpmsg data: %d\n" , ret);
@@ -601,6 +647,18 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,cdx_mcdi_process_cmd (mc_priv->mcdi, (struct cdx_dword *)data, len);return 0 ; } +/* +* The rpmsg channel (and thus this callback) can become live +* before init_versalnet() has allocated priv->mci[] and +* populated priv->adec[]. Drop reports that race ahead +* of it instead of calling handle_error() against incomplete +* state. +*/ +if (!smp_load_acquire (&mc_priv->ready)) { +dev_warn_ratelimited (&rpdev->dev, +"Dropping event, driver not ready yet\n" ); +return 0 ; + } sec_sev = result[ERROR_LEVEL ]; error_id = result[ERROR_ID ];
@@ -721,12 +779,6 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,return 0 ; } -static struct rpmsg_device_id amd_rpmsg_id_table[] = { - { .name =EDAC_RPMSG_CHAN_NAME }, - { }, -}; -MODULE_DEVICE_TABLE (rpmsg, amd_rpmsg_id_table); -static int rpmsg_probe (struct rpmsg_device *rpdev) {struct rpmsg_channel_info chinfo = {
@@ -734,25 +786,56 @@ static int rpmsg_probe(struct rpmsg_device *rpdev) .dst = rpdev->dst, .name = EDAC_RPMSG_CHAN_NAME, }; +struct rpmsg_endpoint *ept;struct mc_priv *pg; pg = (struct mc_priv *)amd_rpmsg_id_table[0 ].driver_data; - pg->ept =rpmsg_create_ept (rpdev, rpmsg_cb,NULL , chinfo); -if (!pg->ept) -return dev_err_probe (&rpdev->dev, -ENXIO ,"Failed to create ept for channel %s\n" , - chinfo.name); -dev_set_drvdata (&rpdev->dev, pg); + ept =rpmsg_create_ept (rpdev, rpmsg_cb,NULL , chinfo); +if (!ept) { +return dev_err_probe (&rpdev->dev, -ENXIO , +"Failed to create ept for channel %s\n" , + chinfo.name); + } + +/* +* Link the new endpoint in under the lock. This also re-syncs +* mcdi->ept on reconnection after the remote processor recovers +* from a crash/shutdown, which previously only happened once, +* during the initial mc_probe(). +*/ + scoped_guard(mutex, &pg->ept_lock) { + pg->ept = ept; + pg->mcdi->ept = ept; + pg->mcdi->rpdev = rpdev; + } + +complete_all (&pg->rpmsg_ready); +return 0 ; }static void rpmsg_remove (struct rpmsg_device *rpdev) {struct mc_priv *mc_priv =dev_get_drvdata (&rpdev->dev); +struct rpmsg_endpoint *ept; + +/* +* Clear the shared endpoint pointers before destroying the +* endpoint, so that mcdi_request() (serialised on the same lock) +* cannot observe or send on a stale/freed endpoint if the remote +* processor goes down concurrently. +*/ + scoped_guard(mutex, &mc_priv->ept_lock) { + ept = mc_priv->ept; + mc_priv->ept =NULL ; + mc_priv->mcdi->ept =NULL ; + mc_priv->mcdi->rpdev =NULL ; + } -rpmsg_destroy_ept (mc_priv->ept); +rpmsg_destroy_ept (ept);dev_set_drvdata (&rpdev->dev,NULL ); }
@@ -920,6 +1003,9 @@ static int mc_probe(struct platform_device *pdev) goto err_alloc; } + priv->dev = &pdev->dev; +mutex_init (&priv->ept_lock); +init_completion (&priv->rpmsg_ready); amd_rpmsg_id_table[0 ].driver_data = (kernel_ulong_t )priv; rc =register_rpmsg_driver (&amd_rpmsg_driver);
@@ -932,12 +1018,25 @@ static int mc_probe(struct platform_device *pdev)if (rc) goto err_unreg; +if (!wait_for_completion_timeout (&priv->rpmsg_ready,MCDI_RPC_TIMEOUT )) { +dev_err (&pdev->dev,"Timed out waiting for RPMsg endpoint\n" ); + rc = -ENODEV ; + goto err_init; + } + priv->mcdi->r5_rproc = rp; rc =init_versalnet (priv, pdev);if (rc) goto err_init; +/* +* edac is initialised. Publish this with a release store so rpmsg_cb +* running concurrently on the rpmsg worker thread, only calls +* handle_error() once it observes @ready via a matching acquire load. +*/ +smp_store_release (&priv->ready, true); +return 0 ; err_init:
Signature · 1 line
2.44.4
Re: [PATCH v2 06/12] EDAC/versalnet: Wait for RPMsg endpoint initialization
Quoted 7 lines · Shubhrajyoti Datta
From: Hayden Rinn <[email protected]>
The rpmsg endpoint can be used before
rpmsg_probe()completes, and can race with asynchronousrpmsg_remove()teardown (e.g. R5 crash or shutdown), both leading to use of an uninitialized or freed endpoint.
Quoted 8 lines
Add a completion so
mc_probe()waits for the endpoint, and a "ready" flag sorpmsg_cb()drops events until mci/adec are set up. Addept_lockto serializepriv->eptandpriv->mcdi->eptacrossmcdi_request(),rpmsg_probe(), andrpmsg_remove(), clearing them on removal and failingmcdi_request()if the channel is down.Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Hayden Rinn <[email protected]>
Why Hayden is missed out in to/cc list?
Quoted 31 lines
Co-developed-by Shubhrajyoti Datta <[email protected]>Signed-off-by Shubhrajyoti Datta <[email protected]>---
Changes in v2:
Patch addition for the rpmsg endpoint racedrivers/edac/versalnet_edac.c | 123 ++++++++++++++++++++++++++++++---- 1 file changed, 111 insertions(+), 12 deletions(-)drivers/edac/versalnet_edac.c +4
-0@@ -5,8 +5,10 @@*/ #include <linux/cdx/edac_cdx_pcol.h> +#include <linux/completion.h> #include <linux/edac.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/of_device.h> #include <linux/ras.h> #include <linux/remoteproc.h> @@ -146,7 +148,22 @@ struct ecc_status {* @adec: Address decode registers. * @mci: Memory controller interface. * @ept: rpmsg endpoint. +* @ept_lock: Protects @ept and @mcdi->ept against concurrent teardown +* by rpmsg_remove() (e.g. remote processor crash/shutdown)
drivers/edac/versalnet_edac.c struct ecc_status { · line 162
Quoted 10 lines
drivers/edac/versalnet_edac.c +9
-0+* while mcdi_request() is sending on them. +* @rpmsg_ready: Completion signaled once rpmsg_probe() has run to +* completion success. @ept is only valid to check +* once this is signaled. * @mcdi: The mcdi handle. +* @dev: Platform device backing this driver instance. Stays +* valid for as long as mcdi_request() can be called, +* unlike @mcdi->rpdev, which is torn down and cleared +* by rpmsg_remove(). Used for logging when there is no +* live rpmsg channel to hang the message off of.
To big description. Lets trim all this AI generated text.
drivers/edac/versalnet_edac.c struct ecc_status { · line 166
Quoted 4 lines
drivers/edac/versalnet_edac.c +4
-0+* @ready: Set once edac priv data is initialised. Published with a +* release store and read with an acquire load so that rpmsg_cb +* never calls handle_error() against a @mci entry that hasn't +* been allocated yet.
Same here.
Quoted 45 lines
drivers/edac/versalnet_edac.c +16
-0*/ struct mc_priv {char message[256 ];@@ -159,7 +176,11 @@ struct mc_priv {u32 adec[ADEC_MAX ];struct mem_ctl_info *mci[NUM_CONTROLLERS ];struct rpmsg_endpoint *ept; +struct mutex ept_lock;/* Protects endpoint */ +struct completion rpmsg_ready;struct cdx_mcdi *mcdi; +struct device *dev; +bool ready; };/* @@ -508,6 +529,12 @@ static void mc_init(struct mem_ctl_info *mci, struct device *dev,#define to_mci (k) container_of(k, struct mem_ctl_info, dev) +static struct rpmsg_device_id amd_rpmsg_id_table[] = { + { .name =EDAC_RPMSG_CHAN_NAME }, + { }, +}; +MODULE_DEVICE_TABLE (rpmsg, amd_rpmsg_id_table); +static unsigned int mcdi_rpc_timeout (struct cdx_mcdi *cdx,unsigned int cmd) {return MCDI_RPC_TIMEOUT ;@@ -517,6 +544,7 @@ static void mcdi_request(struct cdx_mcdi *cdx, const struct cdx_dword *hdr, size_t hdr_len, const struct cdx_dword *sdu, size_t sdu_len) { + struct mc_priv *mc_priv = (struct mc_priv *)amd_rpmsg_id_table[0].driver_data;void *send_buf;int ret;@@ -527,6 +555,24 @@ static void mcdi_request(struct cdx_mcdi *cdx,memcpy (send_buf, hdr, hdr_len);memcpy (send_buf + hdr_len, sdu, sdu_len); +guard (mutex)(&mc_priv->ept_lock); + +/* +* The rpmsg channel can be torn down asynchronously (e.g. the R5 +* remote processor crashing or being shut down) by rpmsg_remove(),
drivers/edac/versalnet_edac.c static void mcdi_request(struct cdx_mcdi *cdx, · line 572
Quoted 10 lines
drivers/edac/versalnet_edac.c +10
-0+* which clears both @cdx->ept and @mc_priv->ept under this same +* lock. Bail out instead of sending on a destroyed endpoint. +* +* @cdx->rpdev is cleared by rpmsg_remove() together with @cdx->ept, +* so it cannot be used for logging here; use the stable platform +* device instead. +*/ +if (!cdx->ept) { +dev_err (mc_priv->dev,"Dropping rpmsg send, channel is down\n" ); +kfree (send_buf);
Better to do in start and avoid this kfree.
Quoted 62 lines
drivers/edac/versalnet_edac.c +24
-11+return ; + } + ret =rpmsg_send (cdx->ept, send_buf, hdr_len + sdu_len);if (ret)dev_err (&cdx->rpdev->dev,"Failed to send rpmsg data: %d\n" , ret);@@ -601,6 +647,18 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,cdx_mcdi_process_cmd (mc_priv->mcdi, (struct cdx_dword *)data, len);return 0 ; } +/* +* The rpmsg channel (and thus this callback) can become live +* before init_versalnet() has allocated priv->mci[] and +* populated priv->adec[]. Drop reports that race ahead +* of it instead of calling handle_error() against incomplete +* state. +*/ +if (!smp_load_acquire (&mc_priv->ready)) { +dev_warn_ratelimited (&rpdev->dev, +"Dropping event, driver not ready yet\n" ); +return 0 ; + } sec_sev = result[ERROR_LEVEL ]; error_id = result[ERROR_ID ];@@ -721,12 +779,6 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,return 0 ; } -static struct rpmsg_device_id amd_rpmsg_id_table[] = { - { .name =EDAC_RPMSG_CHAN_NAME }, - { }, -}; -MODULE_DEVICE_TABLE (rpmsg, amd_rpmsg_id_table); -static int rpmsg_probe (struct rpmsg_device *rpdev) {struct rpmsg_channel_info chinfo = {@@ -734,25 +786,56 @@ static int rpmsg_probe(struct rpmsg_device *rpdev) .dst = rpdev->dst, .name = EDAC_RPMSG_CHAN_NAME, }; +struct rpmsg_endpoint *ept;struct mc_priv *pg; pg = (struct mc_priv *)amd_rpmsg_id_table[0 ].driver_data; - pg->ept =rpmsg_create_ept (rpdev, rpmsg_cb,NULL , chinfo); -if (!pg->ept) -return dev_err_probe (&rpdev->dev, -ENXIO ,"Failed to create ept for channel %s\n" , - chinfo.name); -dev_set_drvdata (&rpdev->dev, pg); + ept =rpmsg_create_ept (rpdev, rpmsg_cb,NULL , chinfo); +if (!ept) { +return dev_err_probe (&rpdev->dev, -ENXIO , +"Failed to create ept for channel %s\n" , + chinfo.name); + } + +/*
drivers/edac/versalnet_edac.c static int rpmsg_probe(struct rpmsg_device *rpdev) · line 813
Quoted 10 lines
drivers/edac/versalnet_edac.c +10
-0+* Link the new endpoint in under the lock. This also re-syncs +* mcdi->ept on reconnection after the remote processor recovers +* from a crash/shutdown, which previously only happened once, +* during the initial mc_probe(). +*/ + scoped_guard(mutex, &pg->ept_lock) { + pg->ept = ept; + pg->mcdi->ept = ept; + pg->mcdi->rpdev = rpdev; + }
[1] Critical: rpmsg_probe() dereferences priv->mcdi before mc_probe() assigns it.
priv->mcdi is assigned inside setup_mcdi(), which mc_probe() calls after register_rpmsg_driver(). If the channel binds in that window - only a kzalloc_obj() and cdx_mcdi_init() wide, but the NS callback runs in an unrelated context - pg->mcdi is NULL here.
Pre-patch rpmsg_probe() only touched pg->ept, which lives in the devm-allocated priv, so this is newly introduced.
There is a second, wider instance: if setup_mcdi() fails in cdx_mcdi_init() it returns with mc_priv->mcdi still NULL, and "goto err_unreg" runs unregister_rpmsg_driver() -> rpmsg_remove(), which does mc_priv->mcdi->ept = NULL on NULL.
Quoted 12 lines
drivers/edac/versalnet_edac.c +6
-0+ +complete_all (&pg->rpmsg_ready); +return 0 ; }static void rpmsg_remove (struct rpmsg_device *rpdev) {struct mc_priv *mc_priv =dev_get_drvdata (&rpdev->dev); +struct rpmsg_endpoint *ept; + +/*
drivers/edac/versalnet_edac.c static int rpmsg_probe(struct rpmsg_device *rpdev) · line 835
Quoted 10 lines
drivers/edac/versalnet_edac.c +10
-0+* Clear the shared endpoint pointers before destroying the +* endpoint, so that mcdi_request() (serialised on the same lock) +* cannot observe or send on a stale/freed endpoint if the remote +* processor goes down concurrently. +*/ + scoped_guard(mutex, &mc_priv->ept_lock) { + ept = mc_priv->ept; + mc_priv->ept =NULL ; + mc_priv->mcdi->ept =NULL ; + mc_priv->mcdi->rpdev =NULL ;
[2] Critical: use-after-free on the probe error path.
drivers/edac/versalnet_edac.c static void rpmsg_remove(struct rpmsg_device *rpdev) · line 791
Quoted 7 lines
drivers/edac/versalnet_edac.c +6
-0@@ -745,25 +786,56 @@ static void rpmsg_remove(struct rpmsg_device *rpdev) + scoped_guard(mutex, &mc_priv->ept_lock) { + ept = mc_priv->ept; + mc_priv->ept =NULL ; + mc_priv->mcdi->ept =NULL ; + mc_priv->mcdi->rpdev =NULL ; + }
Combined with the unwind in mc_probe(), which is unchanged by this patch but newly matters because rpmsg_remove() now writes through priv->mcdi:
err_init:
cdx_mcdi_finish(priv->mcdi);
kfree(priv->mcdi);
err_unreg:
unregister_rpmsg_driver(&amd_rpmsg_driver);err_init frees priv->mcdi and then falls through to unregister_rpmsg_driver(), which calls rpmsg_remove() on the still bound channel, which writes to the freed object. Reachable whenever init_versalnet() fails, since the channel is bound by then.
mc_remove() has the right order (unregister, then free); only the error path is inverted.
Quoted 18 lines
drivers/edac/versalnet_edac.c +5
-1+ } -rpmsg_destroy_ept (mc_priv->ept); +rpmsg_destroy_ept (ept);dev_set_drvdata (&rpdev->dev,NULL ); }@@ -920,6 +1003,9 @@ static int mc_probe(struct platform_device *pdev) goto err_alloc; } + priv->dev = &pdev->dev; +mutex_init (&priv->ept_lock); +init_completion (&priv->rpmsg_ready); amd_rpmsg_id_table[0 ].driver_data = (kernel_ulong_t )priv; rc =register_rpmsg_driver (&amd_rpmsg_driver);
drivers/edac/versalnet_edac.c static int mc_probe(struct platform_device *pdev) · line 1020
Quoted 3 lines
drivers/edac/versalnet_edac.c +0
-0@@ -932,12 +1018,25 @@ static int mc_probe(struct platform_device *pdev)if (rc) goto err_unreg;
[3] Critical: the completion is waited on after the endpoint's only pre-init user, so the patch does not fix the bug it describes.
The wait lands after setup_mcdi(), but setup_mcdi() is itself the first and only user of the endpoint during probe:
mcdi_request() takes the new !cdx->ept exit and returns void, so the CDX core never learns the send failed and waits out cdx_mcdi_rpc_timeout(), 10 * HZ. get_ddr_config() only copies on success and setup_mcdi() returns 0 regardless, so adec[] stays zero and probe continues: init_one_mc() reads FIELD_GET(MC5_BUS_WIDTH_MASK, 0) == XDDR5_BUS_WIDTH_64 and registers MC0 with fabricated geometry. (The announcement normally lands during that stall, so in practice only MC0 is affected.)
Pre-patch this at least produced a WARN_ON(!ept) from rpmsg_send(). The guard is correct, but without moving the wait above the loop it only silences the symptom.
Quoted 22 lines
drivers/edac/versalnet_edac.c +13
-0+if (!wait_for_completion_timeout (&priv->rpmsg_ready,MCDI_RPC_TIMEOUT )) { +dev_err (&pdev->dev,"Timed out waiting for RPMsg endpoint\n" ); + rc = -ENODEV ; + goto err_init; + } + priv->mcdi->r5_rproc = rp; rc =init_versalnet (priv, pdev);if (rc) goto err_init; +/* +* edac is initialised. Publish this with a release store so rpmsg_cb +* running concurrently on the rpmsg worker thread, only calls +* handle_error() once it observes @ready via a matching acquire load. +*/ +smp_store_release (&priv->ready, true); +return 0 ; err_init:
Shubhrajyoti Datta
maintainerPATCH v2 7/12 EDAC/versalnet: Initialize MCDI before RPMsg registration
In reply to Shubhrajyoti Dattasetup_mcdi() currently allocates the MCDI instance, assigns the RPMsg endpoint, and retrieves DDR configuration data. The DDR configuration path invokes cdx_mcdi_rpc(), which requires a functional RPMsg endpoint.
Split setup_mcdi() so that only MCDI allocation and initialization are performed before RPMsg registration. Update the error paths to match the new initialization order, ensuring resources are released in reverse order of acquisition.
---
Changes in v2:
Update the labels
this fixes the sashiko comment
"register_rpmsg_driver() exposes the callback endpoint before setup_mcdi() initializes the mcdi pointer. Could this lead to a NULL pointer dereference if a response arrives early?"
drivers/edac/versalnet_edac.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)
drivers/edac/versalnet_edac.c +13 -14
@@ -605,7 +605,7 @@ static void get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi)static int setup_mcdi (struct mc_priv *mc_priv) {struct cdx_mcdi *amd_mcdi; -int ret, i; +int ret; amd_mcdi =kzalloc_obj (*amd_mcdi);if (!amd_mcdi)
@@ -618,12 +618,8 @@ static int setup_mcdi(struct mc_priv *mc_priv)return ret; } - amd_mcdi->ept = mc_priv->ept; mc_priv->mcdi = amd_mcdi; -for (i =0 ; i <NUM_CONTROLLERS ; i++) -get_ddr_config (i, &mc_priv->adec[ADEC_NUM * i], amd_mcdi); -return 0 ; }
@@ -978,7 +974,7 @@ static int mc_probe(struct platform_device *pdev) {struct mc_priv *priv;struct rproc *rp; -int rc; +int rc, i; struct device_node *r5_core_node __free(device_node) =of_parse_phandle (pdev->dev.of_node,"amd,rproc" ,0 );
@@ -1008,16 +1004,16 @@ static int mc_probe(struct platform_device *pdev)init_completion (&priv->rpmsg_ready); amd_rpmsg_id_table[0 ].driver_data = (kernel_ulong_t )priv; + rc =setup_mcdi (priv); +if (rc) + goto err_alloc; + rc =register_rpmsg_driver (&amd_rpmsg_driver);if (rc) {edac_printk (KERN_ERR ,EDAC_MC ,"Failed to register RPMsg driver: %d\n" , rc); - goto err_alloc; + goto err_reg; } - rc =setup_mcdi (priv); -if (rc) - goto err_unreg; -if (!wait_for_completion_timeout (&priv->rpmsg_ready,MCDI_RPC_TIMEOUT )) {dev_err (&pdev->dev,"Timed out waiting for RPMsg endpoint\n" ); rc = -ENODEV ;
@@ -1026,6 +1022,9 @@ static int mc_probe(struct platform_device *pdev) priv->mcdi->r5_rproc = rp; +for (i =0 ; i <NUM_CONTROLLERS ; i++) +get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi); + rc =init_versalnet (priv, pdev);if (rc) goto err_init;
@@ -1040,12 +1039,12 @@ static int mc_probe(struct platform_device *pdev)return 0 ; err_init: +unregister_rpmsg_driver (&amd_rpmsg_driver); + +err_reg:cdx_mcdi_finish (priv->mcdi);kfree (priv->mcdi); -err_unreg: -unregister_rpmsg_driver (&amd_rpmsg_driver); - err_alloc:rproc_shutdown (rp);
Signature · 1 line
2.44.4
Re: [PATCH v2 07/12] EDAC/versalnet: Initialize MCDI before RPMsg registration
Quoted 9 lines · Shubhrajyoti Datta
setup_mcdi()currently allocates the MCDI instance, assigns the RPMsg endpoint, and retrieves DDR configuration data. The DDR configuration path invokescdx_mcdi_rpc(), which requires a functional RPMsg endpoint.Split
setup_mcdi()so that only MCDI allocation and initialization are performed before RPMsg registration. Update the error paths to match the new initialization order, ensuring resources are released in reverse order of acquisition.
All three from the earlier review. setup_mcdi() now precedes register_rpmsg_driver(), so rpmsg_probe() can't deref a NULL priv->mcdi. The get_ddr_config() loop moved below the completion wait, so the endpoint is live before first use. err_init unregisters the rpmsg driver before cdx_mcdi_finish()/kfree(), so rpmsg_remove() can't write through freed memory.
I suggest squashing this into the previous patch so that the series does not introduce a bug that is fixed by a subsequent patch.
Quoted 97 lines
Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>---
Changes in v2:
Update the labels
this fixes the sashiko comment
"register_rpmsg_driver()exposes the callback endpoint beforesetup_mcdi()initializes the mcdi pointer. Could this lead to a NULL pointer dereference if a response arrives early?"drivers/edac/versalnet_edac.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)drivers/edac/versalnet_edac.c +13
-14@@ -605,7 +605,7 @@ static void get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi)static int setup_mcdi (struct mc_priv *mc_priv) {struct cdx_mcdi *amd_mcdi; -int ret, i; +int ret; amd_mcdi =kzalloc_obj (*amd_mcdi);if (!amd_mcdi)@@ -618,12 +618,8 @@ static int setup_mcdi(struct mc_priv *mc_priv)return ret; } - amd_mcdi->ept = mc_priv->ept; mc_priv->mcdi = amd_mcdi; -for (i =0 ; i <NUM_CONTROLLERS ; i++) -get_ddr_config (i, &mc_priv->adec[ADEC_NUM * i], amd_mcdi); -return 0 ; }@@ -978,7 +974,7 @@ static int mc_probe(struct platform_device *pdev) {struct mc_priv *priv;struct rproc *rp; -int rc; +int rc, i; struct device_node *r5_core_node __free(device_node) =of_parse_phandle (pdev->dev.of_node,"amd,rproc" ,0 );@@ -1008,16 +1004,16 @@ static int mc_probe(struct platform_device *pdev)init_completion (&priv->rpmsg_ready); amd_rpmsg_id_table[0 ].driver_data = (kernel_ulong_t )priv; + rc =setup_mcdi (priv); +if (rc) + goto err_alloc; + rc =register_rpmsg_driver (&amd_rpmsg_driver);if (rc) {edac_printk (KERN_ERR ,EDAC_MC ,"Failed to register RPMsg driver: %d\n" , rc); - goto err_alloc; + goto err_reg; } - rc =setup_mcdi (priv); -if (rc) - goto err_unreg; -if (!wait_for_completion_timeout (&priv->rpmsg_ready,MCDI_RPC_TIMEOUT )) {dev_err (&pdev->dev,"Timed out waiting for RPMsg endpoint\n" ); rc = -ENODEV ;@@ -1026,6 +1022,9 @@ static int mc_probe(struct platform_device *pdev) priv->mcdi->r5_rproc = rp; +for (i =0 ; i <NUM_CONTROLLERS ; i++) +get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi); + rc =init_versalnet (priv, pdev);if (rc) goto err_init;@@ -1040,12 +1039,12 @@ static int mc_probe(struct platform_device *pdev)return 0 ; err_init: +unregister_rpmsg_driver (&amd_rpmsg_driver); + +err_reg:cdx_mcdi_finish (priv->mcdi);kfree (priv->mcdi); -err_unreg: -unregister_rpmsg_driver (&amd_rpmsg_driver); - err_alloc:rproc_shutdown (rp);
Shubhrajyoti Datta
maintainerPATCH v2 8/12 EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb()
In reply to Shubhrajyoti DattaThe rpmsg core creates its own endpoint bound to rpmsg_cb() in rpmsg_dev_probe(), before rpmsg_probe() runs and attaches mc_priv via dev_set_drvdata(). A message dispatched to that endpoint in that window would dereference a NULL mc_priv and crash.
The same race exists on the teardown side: rpmsg_dev_remove() calls rpmsg_remove() first, which clears drvdata back to NULL, and only destroys the core's default endpoint afterwards. A message arriving in that window reaches rpmsg_cb() with drvdata already NULL.
Check for a NULL mc_priv in rpmsg_cb() and drop the message instead of crashing, in either window.
---
Changes in v2:
Patch addition for the rpmsg drvdata race
drivers/edac/versalnet_edac.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/edac/versalnet_edac.c +6 -0
@@ -639,6 +639,12 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,const char *err_str;u32 *adec_data; +if (!mc_priv) { +dev_warn_ratelimited (&rpdev->dev, +"Dropping rpmsg message, driver not ready or being removed\n" ); +return 0 ; + } +if (*(u8 *)data ==MCDI_RESPONSE ) {cdx_mcdi_process_cmd (mc_priv->mcdi, (struct cdx_dword *)data, len);return 0 ;
Signature · 1 line
2.44.4
Re: [PATCH v2 08/12] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb()
Quoted 10 lines · Shubhrajyoti Datta
The rpmsg core creates its own endpoint bound to
rpmsg_cb()inrpmsg_dev_probe(), beforerpmsg_probe()runs and attachesmc_privviadev_set_drvdata(). A message dispatched to that endpoint in that window would dereference a NULLmc_privand crash.The same race exists on the teardown side:
rpmsg_dev_remove()callsrpmsg_remove()first, which clears drvdata back to NULL, and only destroys the core's default endpoint afterwards. A message arriving in that window reachesrpmsg_cb()with drvdata already NULL.
Quoted 5 lines
Check for a NULL
mc_privinrpmsg_cb()and drop the message instead of crashing, in either window.Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>
Thanks!
Quoted 25 lines
---
Changes in v2:
Patch addition for the rpmsg drvdata racedrivers/edac/versalnet_edac.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/edac/versalnet_edac.c +6
-0@@ -639,6 +639,12 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,const char *err_str;u32 *adec_data; +if (!mc_priv) { +dev_warn_ratelimited (&rpdev->dev, +"Dropping rpmsg message, driver not ready or being removed\n" ); +return 0 ; + } +if (*(u8 *)data ==MCDI_RESPONSE ) {cdx_mcdi_process_cmd (mc_priv->mcdi, (struct cdx_dword *)data, len);return 0 ;
Shubhrajyoti Datta
maintainerPATCH v2 9/12 EDAC/versalnet: Move platform_set_drvdata() to mc_probe()
In reply to Shubhrajyoti DattaMove platform_set_drvdata() out of init_one_mc() and into mc_probe() so that the driver data is set once during probe rather than being redundantly set on each memory controller initialization.
The pdev parameter in init_one_mc() and init_versalnet() is no longer referenced. Remove it from both function signatures.
---
Changes in v2:
Add reviewed-by tag
drivers/edac/versalnet_edac.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/edac/versalnet_edac.c +5 -6
@@ -866,7 +866,7 @@ static void remove_one_mc(struct mc_priv *priv, int i)device_unregister (dev); } -static int init_one_mc (struct mc_priv *priv,struct platform_device *pdev,int i) +static int init_one_mc (struct mc_priv *priv,int i) {u32 num_chans, rank, dwidth, config;struct edac_mc_layer layers[2 ];
@@ -940,8 +940,6 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i priv->mci[i] = mci; priv->dwidth[i] = dt; -platform_set_drvdata (pdev, priv); -return 0 ; err_mc_free:
@@ -954,12 +952,12 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int ireturn rc; } -static int init_versalnet (struct mc_priv *priv,struct platform_device *pdev) +static int init_versalnet (struct mc_priv *priv) {int rc, i;for (i =0 ; i <NUM_CONTROLLERS ; i++) { - rc =init_one_mc (priv, pdev, i); + rc =init_one_mc (priv, i);if (rc) {while (i--)remove_one_mc (priv, i);
@@ -1006,6 +1004,7 @@ static int mc_probe(struct platform_device *pdev) } priv->dev = &pdev->dev; +platform_set_drvdata (pdev, priv);mutex_init (&priv->ept_lock);init_completion (&priv->rpmsg_ready); amd_rpmsg_id_table[0 ].driver_data = (kernel_ulong_t )priv;
@@ -1031,7 +1030,7 @@ static int mc_probe(struct platform_device *pdev)for (i =0 ; i <NUM_CONTROLLERS ; i++)get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi); - rc =init_versalnet (priv, pdev); + rc =init_versalnet (priv);if (rc) goto err_init;
Signature · 1 line
2.44.4
Shubhrajyoti Datta
maintainerPATCH v2 10/12 dt-bindings: memory: xlnx,versal-net-ddrmc5: Add amd,num-ctrl property
In reply to Shubhrajyoti DattaAdd the amd,num-ctrl property to describe the number of DDR memory controllers enabled in the design. Valid range is 1 to 8. The driver then can enumerate only the enabled controllers.
---
Changes in v2:
Add amd,num-ctrl property to device tree The property was requested in the discussion below
https://lore.kernel.org/all/[email protected]/
.../memory-controllers/xlnx,versal-net-ddrmc5.yaml | 9 +++++++++ 1 file changed, 9 insertions(+)
Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml +9 -0
@@ -27,6 +27,15 @@ properties: with remote processor. APU primarily communicates with the RPU for accessing the DDRMC address space and getting error notification. + amd,num-ctrl: + description: + Number of DDR memory controller (DDRMC) instances enabled in the + design. + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 8 + default: 8 + required: - compatible - amd,rproc
Signature · 1 line
2.44.4
Re: [PATCH v2 10/12] dt-bindings: memory: xlnx,versal-net-ddrmc5: Add amd,num-ctrl property
Quoted 3 lines · Shubhrajyoti Datta
Add the amd,num-ctrl property to describe the number of DDR memory controllers enabled in the design. Valid range is 1 to 8. The driver then can enumerate only the enabled controllers.
Nit - avoid "driver" usage in binding commit description.
Quoted 3 lines
Signed-off-by Shubhrajyoti Datta <[email protected]>---
we should also explain why it need to be a DT property and can't be queried/derived from firmware response.
Quoted 14 lines
Changes in v2:
Add amd,num-ctrl property to device tree The property was requested in the discussion below
https://lore.kernel.org/all/[email protected]/.../memory-controllers/xlnx,versal-net-ddrmc5.yaml | 9 +++++++++ 1 file changed, 9 insertions(+)Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml +0
-0
Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml properties: · line 33
Quoted 7 lines
Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml +4
-0@@ -27,6 +27,15 @@ properties: with remote processor. APU primarily communicates with the RPU for accessing the DDRMC address space and getting error notification. + amd,num-ctrl: + description: + Number of DDR memory controller (DDRMC) instances enabled in the + design.
Mention that : The enabled instances are contiguous.
Quoted 8 lines
Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml +5
-0+ $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 8 + default: 8 + required: - compatible - amd,rproc
Krzysztof Kozlowski
maintainerRe: [PATCH v2 10/12] dt-bindings: memory: xlnx,versal-net-ddrmc5: Add amd,num-ctrl property
In reply to Shubhrajyoti DattaQuoted 3 lines · Shubhrajyoti Datta
Add the amd,num-ctrl property to describe the number of DDR memory controllers enabled in the design. Valid range is 1 to 8. The driver then can enumerate only the enabled controllers.
You still fail to explain why this is needed in the first place, IOW, why this is not deducible from the compatible.
Best regards,
Krzysztof
Shubhrajyoti Datta
maintainerPATCH v2 11/12 EDAC/versalnet: use device tree property for number of controllers
In reply to Shubhrajyoti DattaUse the number of enabled controllers read from the "amd,num-ctrl" device tree property for registering instead of all the controllers. The enabled controllers are contiguous. Add anum_ctrl field to mc_priv and use it in probe, init, remove, error handling, and the rpmsg callback, allowing the driver to support platforms with varying numbers of memory controllers enabled.
This keeps the old behaviour for older device trees. On a platform with whose device tree lacks the property, the old iteration over all 8 indices, remains in effect.
---
Changes in v2:
Adding the device tree parameter was suggested in the review in v1
https://lore.kernel.org/all/[email protected]/
drivers/edac/versalnet_edac.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
drivers/edac/versalnet_edac.c +22 -6
@@ -140,6 +140,7 @@ struct ecc_status {* struct mc_priv - DDR memory controller private instance data. * @message: Buffer for framing the event specific info. * @stat: ECC status information. +* @num_ctrl: Number of DDR memory controllers. * @error_id: The error id. * @error_level: The error level. * @dwidth: Per-controller width of data bus excluding ECC bits.
@@ -168,6 +169,7 @@ struct ecc_status {struct mc_priv {char message[256 ];struct ecc_status stat; +u32 num_ctrl;u32 error_id;u32 error_level;enum dev_type dwidth[NUM_CONTROLLERS ];
@@ -456,7 +458,7 @@ static void handle_error(struct mc_priv *priv, struct ecc_status *stat,phys_addr_t pfn;int err; -if (WARN_ON_ONCE (ctl_num >=NUM_CONTROLLERS )) +if (WARN_ON_ONCE (ctl_num >= priv->num_ctrl))return ; mci = priv->mci[ctl_num];
@@ -713,7 +715,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, p = &mc_priv->stat;memset (p,0 ,sizeof (struct ecc_status )); p->error_type =MC5_ERR_TYPE_CE ; -for (i =0 ; i <NUM_CONTROLLERS ; i++) { +for (i =0 ; i < mc_priv->num_ctrl; i++) {if (get_ddr_info (&mc_priv->regs[i *REGS_PER_CONTROLLER ], mc_priv)) { adec_data = mc_priv->adec +ADEC_NUM * i;handle_error (mc_priv, &mc_priv->stat, i, adec_data);
@@ -724,7 +726,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, p = &mc_priv->stat;memset (p,0 ,sizeof (struct ecc_status )); p->error_type =MC5_ERR_TYPE_UE ; -for (i =0 ; i <NUM_CONTROLLERS ; i++) { +for (i =0 ; i < mc_priv->num_ctrl; i++) {if (get_ddr_info (&mc_priv->regs[i *REGS_PER_CONTROLLER ], mc_priv)) { adec_data = mc_priv->adec +ADEC_NUM * i;handle_error (mc_priv, &mc_priv->stat, i, adec_data);
@@ -956,7 +958,7 @@ static int init_versalnet(struct mc_priv *priv) {int rc, i; -for (i =0 ; i <NUM_CONTROLLERS ; i++) { +for (i =0 ; i < priv->num_ctrl; i++) { rc =init_one_mc (priv, i);if (rc) {while (i--)
@@ -970,7 +972,7 @@ static int init_versalnet(struct mc_priv *priv)static void remove_versalnet (struct mc_priv *priv) { -for (int i =0 ; i <NUM_CONTROLLERS ; i++) +for (int i =0 ; i < priv->num_ctrl; i++)remove_one_mc (priv, i); }
@@ -978,6 +980,7 @@ static int mc_probe(struct platform_device *pdev) {struct mc_priv *priv;struct rproc *rp; +u32 num_ctrl;int rc, i; struct device_node *r5_core_node __free(device_node) =
@@ -987,6 +990,18 @@ static int mc_probe(struct platform_device *pdev)return -EINVAL ; } + rc =of_property_read_u32 (pdev->dev.of_node,"amd,num-ctrl" , &num_ctrl); +if (rc == -EINVAL ) { + num_ctrl =NUM_CONTROLLERS ; + }else if (rc) { +dev_err (&pdev->dev,"amd,num-ctrl: failed to read property: %d\n" , rc); +return rc; + } + +if (!num_ctrl || num_ctrl >NUM_CONTROLLERS ) +return dev_err_probe (&pdev->dev, -EINVAL , +"amd,num-ctrl: invalid value %u\n" , num_ctrl); + rp =rproc_get_by_phandle (r5_core_node->phandle);if (!rp)return -EPROBE_DEFER ;
@@ -1003,6 +1018,7 @@ static int mc_probe(struct platform_device *pdev) goto err_alloc; } + priv->num_ctrl = num_ctrl; priv->dev = &pdev->dev;platform_set_drvdata (pdev, priv);mutex_init (&priv->ept_lock);
@@ -1027,7 +1043,7 @@ static int mc_probe(struct platform_device *pdev) priv->mcdi->r5_rproc = rp; -for (i =0 ; i <NUM_CONTROLLERS ; i++) +for (i =0 ; i < priv->num_ctrl; i++)get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi); rc =init_versalnet (priv);
Signature · 1 line
2.44.4
Re: [PATCH v2 11/12] EDAC/versalnet: use device tree property for number of controllers
Quoted 10 lines · Shubhrajyoti Datta
Use the number of enabled controllers read from the "amd,num-ctrl" device tree property for registering instead of all the controllers. The enabled controllers are contiguous. Add a
num_ctrlfield tomc_privand use it in probe, init, remove, error handling, and the rpmsg callback, allowing the driver to support platforms with varying numbers of memory controllers enabled.This keeps the old behaviour for older device trees. On a platform with whose device tree lacks the property, the old iteration over all 8 indices, remains in effect.
Trim/simplify above commit description.
Default to eight controllers when the property is absent to maintain compatibility with existing device trees.
Quoted 90 lines
Signed-off-by Shubhrajyoti Datta <[email protected]>---
Changes in v2:
Adding the device tree parameter was suggested in the review in v1
https://lore.kernel.org/all/[email protected]/drivers/edac/versalnet_edac.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)drivers/edac/versalnet_edac.c +10
-5@@ -140,6 +140,7 @@ struct ecc_status {* struct mc_priv - DDR memory controller private instance data. * @message: Buffer for framing the event specific info. * @stat: ECC status information. +* @num_ctrl: Number of DDR memory controllers. * @error_id: The error id. * @error_level: The error level. * @dwidth: Per-controller width of data bus excluding ECC bits. @@ -168,6 +169,7 @@ struct ecc_status {struct mc_priv {char message[256 ];struct ecc_status stat; +u32 num_ctrl;u32 error_id;u32 error_level;enum dev_type dwidth[NUM_CONTROLLERS ];@@ -456,7 +458,7 @@ static void handle_error(struct mc_priv *priv, struct ecc_status *stat,phys_addr_t pfn;int err; -if (WARN_ON_ONCE (ctl_num >=NUM_CONTROLLERS )) +if (WARN_ON_ONCE (ctl_num >= priv->num_ctrl))return ; mci = priv->mci[ctl_num];@@ -713,7 +715,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, p = &mc_priv->stat;memset (p,0 ,sizeof (struct ecc_status )); p->error_type =MC5_ERR_TYPE_CE ; -for (i =0 ; i <NUM_CONTROLLERS ; i++) { +for (i =0 ; i < mc_priv->num_ctrl; i++) {if (get_ddr_info (&mc_priv->regs[i *REGS_PER_CONTROLLER ], mc_priv)) { adec_data = mc_priv->adec +ADEC_NUM * i;handle_error (mc_priv, &mc_priv->stat, i, adec_data);@@ -724,7 +726,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data, p = &mc_priv->stat;memset (p,0 ,sizeof (struct ecc_status )); p->error_type =MC5_ERR_TYPE_UE ; -for (i =0 ; i <NUM_CONTROLLERS ; i++) { +for (i =0 ; i < mc_priv->num_ctrl; i++) {if (get_ddr_info (&mc_priv->regs[i *REGS_PER_CONTROLLER ], mc_priv)) { adec_data = mc_priv->adec +ADEC_NUM * i;handle_error (mc_priv, &mc_priv->stat, i, adec_data);@@ -956,7 +958,7 @@ static int init_versalnet(struct mc_priv *priv) {int rc, i; -for (i =0 ; i <NUM_CONTROLLERS ; i++) { +for (i =0 ; i < priv->num_ctrl; i++) { rc =init_one_mc (priv, i);if (rc) {while (i--)@@ -970,7 +972,7 @@ static int init_versalnet(struct mc_priv *priv)static void remove_versalnet (struct mc_priv *priv) { -for (int i =0 ; i <NUM_CONTROLLERS ; i++) +for (int i =0 ; i < priv->num_ctrl; i++)remove_one_mc (priv, i); }@@ -978,6 +980,7 @@ static int mc_probe(struct platform_device *pdev) {struct mc_priv *priv;struct rproc *rp; +u32 num_ctrl;int rc, i; struct device_node *r5_core_node __free(device_node) =@@ -987,6 +990,18 @@ static int mc_probe(struct platform_device *pdev)return -EINVAL ; } + rc =of_property_read_u32 (pdev->dev.of_node,"amd,num-ctrl" , &num_ctrl); +if (rc == -EINVAL ) {
drivers/edac/versalnet_edac.c static int mc_probe(struct platform_device *pdev) · line 1004
Quoted 10 lines
drivers/edac/versalnet_edac.c +10
-0+ num_ctrl =NUM_CONTROLLERS ; + }else if (rc) { +dev_err (&pdev->dev,"amd,num-ctrl: failed to read property: %d\n" , rc); +return rc; + } + +if (!num_ctrl || num_ctrl >NUM_CONTROLLERS ) +return dev_err_probe (&pdev->dev, -EINVAL , +"amd,num-ctrl: invalid value %u\n" , num_ctrl); +
Nit - consistent use of dev_err/dev_err_probe.
How about something like below.
u32 num_ctrl = NUM_CONTROLLERS;
...
rc = of_property_read_u32(pdev->dev.of_node, "amd,num-ctrl",
&num_ctrl); if (rc && rc != -EINVAL) return dev_err_probe(&pdev->dev, rc, "amd,num-ctrl: failed to read property\n");
if (!num_ctrl || num_ctrl > NUM_CONTROLLERS)
return dev_err_probe(&pdev->dev, -EINVAL, "amd,num-ctrl: invalid value %u\n", num_ctrl);
Quoted 20 lines
drivers/edac/versalnet_edac.c +2
-1rp =rproc_get_by_phandle (r5_core_node->phandle);if (!rp)return -EPROBE_DEFER ;@@ -1003,6 +1018,7 @@ static int mc_probe(struct platform_device *pdev) goto err_alloc; } + priv->num_ctrl = num_ctrl; priv->dev = &pdev->dev;platform_set_drvdata (pdev, priv);mutex_init (&priv->ept_lock);@@ -1027,7 +1043,7 @@ static int mc_probe(struct platform_device *pdev) priv->mcdi->r5_rproc = rp; -for (i =0 ; i <NUM_CONTROLLERS ; i++) +for (i =0 ; i < priv->num_ctrl; i++)get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi); rc =init_versalnet (priv);
Shubhrajyoti Datta
maintainerPATCH v2 12/12 EDAC/versalnet: Make get_ddr_config() return errors
In reply to Shubhrajyoti Dattaget_ddr_config() invokes cdx_mcdi_rpc() but ignores its return value, and proceeds even if the cdx_mcdi_rpc returned errors. Also it copies the data out of the response buffer without checking that the firmware actually returned that much data.
Make get_ddr_config() return an int: propagate cdx_mcdi_rpc()'s return code on failure, and return -EIO if the response is shorter than expected. Update mc_probe() to check the return value and bail out through the existing error path on failure.
---
Changes in v2:
New patch added to bailout if the cdx_mcdi_rpc fails.
drivers/edac/versalnet_edac.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/edac/versalnet_edac.c +15 -6
@@ -587,7 +587,7 @@ static const struct cdx_mcdi_ops mcdi_ops = { .mcdi_request = mcdi_request, }; -static void get_ddr_config (u32 index,u32 *buffer,struct cdx_mcdi *amd_mcdi) +static int get_ddr_config (u32 index,u32 *buffer,struct cdx_mcdi *amd_mcdi) {size_t outlen;int ret;
@@ -599,9 +599,15 @@ static void get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi) ret =cdx_mcdi_rpc (amd_mcdi,MC_CMD_EDAC_GET_DDR_CONFIG , inbuf,sizeof (inbuf), outbuf,sizeof (outbuf), &outlen); -if (!ret) -memcpy (buffer,MCDI_PTR (outbuf,GET_DDR_CONFIG ), - (ADEC_NUM *4 )); +if (ret) +return ret; + +if (outlen <MC_CMD_GET_DDR_CONFIG_OFST +ADEC_NUM *4 ) +return -EIO ; + +memcpy (buffer,MCDI_PTR (outbuf,GET_DDR_CONFIG ), (ADEC_NUM *4 )); + +return 0 ; }static int setup_mcdi (struct mc_priv *mc_priv)
@@ -1043,8 +1049,11 @@ static int mc_probe(struct platform_device *pdev) priv->mcdi->r5_rproc = rp; -for (i =0 ; i < priv->num_ctrl; i++) -get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi); +for (i =0 ; i < priv->num_ctrl; i++) { + rc =get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi); +if (rc) + goto err_init; + } rc =init_versalnet (priv);if (rc)
Signature · 1 line
2.44.4
Re: [PATCH v2 12/12] EDAC/versalnet: Make get_ddr_config() return errors
Quoted 2 lines · Shubhrajyoti Datta
get_ddr_config()invokescdx_mcdi_rpc()but ignores its return value, and proceeds even if thecdx_mcdi_rpcreturned errors. Also it
Nit - cdx_mcdi_rpc()
Quoted 32 lines
copies the data out of the response buffer without checking that the firmware actually returned that much data.
Make
get_ddr_config()return an int: propagate cdx_mcdi_rpc()'s return code on failure, and return-EIOif the response is shorter than expected. Updatemc_probe()to check the return value and bail out through the existing error path on failure.Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>---
Changes in v2:
New patch added to bailout if thecdx_mcdi_rpcfails.drivers/edac/versalnet_edac.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)drivers/edac/versalnet_edac.c +1
-1@@ -587,7 +587,7 @@ static const struct cdx_mcdi_ops mcdi_ops = { .mcdi_request = mcdi_request, }; -static void get_ddr_config (u32 index,u32 *buffer,struct cdx_mcdi *amd_mcdi) +static int get_ddr_config (u32 index,u32 *buffer,struct cdx_mcdi *amd_mcdi) {size_t outlen;int ret;
drivers/edac/versalnet_edac.c static void get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi) · line 605
Quoted 10 lines
drivers/edac/versalnet_edac.c +4
-3@@ -599,9 +599,15 @@ static void get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi) ret =cdx_mcdi_rpc (amd_mcdi,MC_CMD_EDAC_GET_DDR_CONFIG , inbuf,sizeof (inbuf), outbuf,sizeof (outbuf), &outlen); -if (!ret) -memcpy (buffer,MCDI_PTR (outbuf,GET_DDR_CONFIG ), - (ADEC_NUM *4 )); +if (ret) +return ret; + +if (outlen <MC_CMD_GET_DDR_CONFIG_OFST +ADEC_NUM *4 )
I think better to use sizeof(outbuf).
Quoted 9 lines
drivers/edac/versalnet_edac.c +5
-0+return -EIO ; + +memcpy (buffer,MCDI_PTR (outbuf,GET_DDR_CONFIG ), (ADEC_NUM *4 )); + +return 0 ; }static int setup_mcdi (struct mc_priv *mc_priv)
drivers/edac/versalnet_edac.c static int mc_probe(struct platform_device *pdev) · line 1053
Quoted 7 lines
drivers/edac/versalnet_edac.c +2
-2@@ -1043,8 +1049,11 @@ static int mc_probe(struct platform_device *pdev) priv->mcdi->r5_rproc = rp; -for (i =0 ; i < priv->num_ctrl; i++) -get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi); +for (i =0 ; i < priv->num_ctrl; i++) { + rc =get_ddr_config (i, &priv->adec[ADEC_NUM * i], priv->mcdi);
To be consistent: Every other failure in mc_probe() logs error message. Inform user this failure as well.
Quoted 6 lines
drivers/edac/versalnet_edac.c +3
-0+if (rc) + goto err_init; + } rc =init_versalnet (priv);if (rc)
Re: [PATCH v2 12/12] EDAC/versalnet: Make get_ddr_config() return errors
In reply to Shubhrajyoti DattaQuoted 5 lines · Shubhrajyoti Datta
get_ddr_config()invokescdx_mcdi_rpc()but ignores its return value, and proceeds even if thecdx_mcdi_rpcreturned errors. Also it copies the data out of the response buffer without checking that the firmware actually returned that much data.
Quoted 8 lines
Make
get_ddr_config()return an int: propagate cdx_mcdi_rpc()'s return code on failure, and return-EIOif the response is shorter than expected. Updatemc_probe()to check the return value and bail out through the existing error path on failure.Fixes d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")Signed-off-by Shubhrajyoti Datta <[email protected]>---
Fixes should generally go first in a series. Any reason to leave this until the end?
Thanks,
Yazen