mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add()
@ 2026-05-20 12:14 Li Ming
  2026-05-21  7:25 ` Alison Schofield
  2026-05-26 21:36 ` Dave Jiang
  0 siblings, 2 replies; 6+ messages in thread
From: Li Ming @ 2026-05-20 12:14 UTC (permalink / raw)
  To: dave, jic23, dave.jiang, alison.schofield, vishal.l.verma,
	ira.weiny, djbw
  Cc: linux-cxl, linux-kernel

CXL test environment hits the following error sometimes.

 cxl_mem mem9: endpoint7 failed probe

All mock memdevs are platform firmware devices added by cxl_test module,
and cxl_test module also provides a platform device driver for them to
create a memdev device to CXL subsystem. cxl_test module uses
cxl_rcd/mem_single/mem arrays to store different types of mock memdevs.
CXL drivers calls registered mock functions for a mock memdev by
checking if a given memdev is in these arrays.

When cxl_test module adds these mock memdevs, it always calls
platform_device_add() before adding them to a suitable mock memdev
array. However, there is a small window where CXL drivers calls mock
function for a added memdev before it added to a mock memdev array. In
above case, cxl endpoint driver considers a added memdev was not a mock
memdev, then calling devm_cxl_endpoint_decoders_setup() for it rather
than mock_endpoint_decoders_setup().

An appropriate solution is that adding a new mock device to a mock
device array before calling platform_device_add() for it. It can
guarantee the new mock device is visible to CXL subsystem.

This patch introduces a new helped called cxl_mock_platform_device_add()
to handle the issue, and uses the function for all mock devices addition.

Fixes: 3a2b97b3210b ("cxl/test: Improve init-order fidelity relative to real-world systems")
Signed-off-by: Li Ming <ming.li@zohomail.com>
---
Changes from v1:
- Rebase to v7.1-rc4.
- Introduce a new helper to handle the issue. (Alison)
- Use new helper for all mock device addition.
---
 tools/testing/cxl/test/cxl.c | 105 ++++++++++++++---------------------
 1 file changed, 43 insertions(+), 62 deletions(-)

diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
index 418669927fb0..296516eecfd6 100644
--- a/tools/testing/cxl/test/cxl.c
+++ b/tools/testing/cxl/test/cxl.c
@@ -1523,6 +1523,23 @@ static void mock_companion(struct acpi_device *adev, struct device *dev)
 #define SZ_64G (SZ_32G * 2)
 #endif
 
+static int cxl_mock_platform_device_add(struct platform_device *pdev,
+					struct platform_device **ppdev)
+{
+	int rc;
+
+	if (ppdev)
+		*ppdev = pdev;
+	rc = platform_device_add(pdev);
+	if (rc) {
+		platform_device_put(pdev);
+		if (ppdev)
+			*ppdev = NULL;
+	}
+
+	return rc;
+}
+
 static __init int cxl_rch_topo_init(void)
 {
 	int rc, i;
@@ -1537,13 +1554,10 @@ static __init int cxl_rch_topo_init(void)
 			goto err_bridge;
 
 		mock_companion(adev, &pdev->dev);
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_rch[i]);
+		if (rc)
 			goto err_bridge;
-		}
 
-		cxl_rch[i] = pdev;
 		mock_pci_bus[idx].bridge = &pdev->dev;
 		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
 				       "firmware_node");
@@ -1595,13 +1609,10 @@ static __init int cxl_single_topo_init(void)
 			goto err_bridge;
 
 		mock_companion(adev, &pdev->dev);
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_hb_single[i]);
+		if (rc)
 			goto err_bridge;
-		}
 
-		cxl_hb_single[i] = pdev;
 		mock_pci_bus[i + NR_CXL_HOST_BRIDGES].bridge = &pdev->dev;
 		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
 				       "physical_node");
@@ -1620,12 +1631,9 @@ static __init int cxl_single_topo_init(void)
 			goto err_port;
 		pdev->dev.parent = &bridge->dev;
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_root_single[i]);
+		if (rc)
 			goto err_port;
-		}
-		cxl_root_single[i] = pdev;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++) {
@@ -1638,12 +1646,9 @@ static __init int cxl_single_topo_init(void)
 			goto err_uport;
 		pdev->dev.parent = &root_port->dev;
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_swu_single[i]);
+		if (rc)
 			goto err_uport;
-		}
-		cxl_swu_single[i] = pdev;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++) {
@@ -1657,12 +1662,9 @@ static __init int cxl_single_topo_init(void)
 			goto err_dport;
 		pdev->dev.parent = &uport->dev;
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_swd_single[i]);
+		if (rc)
 			goto err_dport;
-		}
-		cxl_swd_single[i] = pdev;
 	}
 
 	return 0;
@@ -1735,12 +1737,9 @@ static int cxl_mem_init(void)
 		pdev->dev.parent = &dport->dev;
 		set_dev_node(&pdev->dev, i % 2);
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_mem[i]);
+		if (rc)
 			goto err_mem;
-		}
-		cxl_mem[i] = pdev;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++) {
@@ -1753,12 +1752,9 @@ static int cxl_mem_init(void)
 		pdev->dev.parent = &dport->dev;
 		set_dev_node(&pdev->dev, i % 2);
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_mem_single[i]);
+		if (rc)
 			goto err_single;
-		}
-		cxl_mem_single[i] = pdev;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++) {
@@ -1772,12 +1768,9 @@ static int cxl_mem_init(void)
 		pdev->dev.parent = &rch->dev;
 		set_dev_node(&pdev->dev, i % 2);
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_rcd[i]);
+		if (rc)
 			goto err_rcd;
-		}
-		cxl_rcd[i] = pdev;
 	}
 
 	return 0;
@@ -1869,13 +1862,10 @@ static __init int cxl_test_init(void)
 			goto err_bridge;
 
 		mock_companion(adev, &pdev->dev);
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_host_bridge[i]);
+		if (rc)
 			goto err_bridge;
-		}
 
-		cxl_host_bridge[i] = pdev;
 		mock_pci_bus[i].bridge = &pdev->dev;
 		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
 				       "physical_node");
@@ -1893,12 +1883,9 @@ static __init int cxl_test_init(void)
 			goto err_port;
 		pdev->dev.parent = &bridge->dev;
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_root_port[i]);
+		if (rc)
 			goto err_port;
-		}
-		cxl_root_port[i] = pdev;
 	}
 
 	BUILD_BUG_ON(ARRAY_SIZE(cxl_switch_uport) != ARRAY_SIZE(cxl_root_port));
@@ -1911,12 +1898,9 @@ static __init int cxl_test_init(void)
 			goto err_uport;
 		pdev->dev.parent = &root_port->dev;
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_switch_uport[i]);
+		if (rc)
 			goto err_uport;
-		}
-		cxl_switch_uport[i] = pdev;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++) {
@@ -1929,12 +1913,9 @@ static __init int cxl_test_init(void)
 			goto err_dport;
 		pdev->dev.parent = &uport->dev;
 
-		rc = platform_device_add(pdev);
-		if (rc) {
-			platform_device_put(pdev);
+		rc = cxl_mock_platform_device_add(pdev, &cxl_switch_dport[i]);
+		if (rc)
 			goto err_dport;
-		}
-		cxl_switch_dport[i] = pdev;
 	}
 
 	rc = cxl_single_topo_init();
@@ -1953,9 +1934,9 @@ static __init int cxl_test_init(void)
 	acpi0017_mock.dev.bus = &platform_bus_type;
 	cxl_acpi->dev.groups = cxl_acpi_groups;
 
-	rc = platform_device_add(cxl_acpi);
+	rc = cxl_mock_platform_device_add(cxl_acpi, NULL);
 	if (rc)
-		goto err_root;
+		goto err_rch;
 
 	rc = cxl_mem_init();
 	if (rc)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add()
  2026-05-20 12:14 [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add() Li Ming
@ 2026-05-21  7:25 ` Alison Schofield
  2026-05-21 12:25   ` Li Ming
  2026-05-26 21:36 ` Dave Jiang
  1 sibling, 1 reply; 6+ messages in thread
From: Alison Schofield @ 2026-05-21  7:25 UTC (permalink / raw)
  To: Li Ming
  Cc: dave, jic23, dave.jiang, vishal.l.verma, ira.weiny, djbw,
	linux-cxl, linux-kernel

On Wed, May 20, 2026 at 08:14:57PM +0800, Li Ming wrote:
> CXL test environment hits the following error sometimes.
> 
>  cxl_mem mem9: endpoint7 failed probe
> 
> All mock memdevs are platform firmware devices added by cxl_test module,
> and cxl_test module also provides a platform device driver for them to
> create a memdev device to CXL subsystem. cxl_test module uses
> cxl_rcd/mem_single/mem arrays to store different types of mock memdevs.
> CXL drivers calls registered mock functions for a mock memdev by
> checking if a given memdev is in these arrays.
> 
> When cxl_test module adds these mock memdevs, it always calls
> platform_device_add() before adding them to a suitable mock memdev
> array. However, there is a small window where CXL drivers calls mock
> function for a added memdev before it added to a mock memdev array. In
> above case, cxl endpoint driver considers a added memdev was not a mock
> memdev, then calling devm_cxl_endpoint_decoders_setup() for it rather
> than mock_endpoint_decoders_setup().
> 
> An appropriate solution is that adding a new mock device to a mock
> device array before calling platform_device_add() for it. It can
> guarantee the new mock device is visible to CXL subsystem.
> 
> This patch introduces a new helped called cxl_mock_platform_device_add()
> to handle the issue, and uses the function for all mock devices addition.
> 

Reviewed-by: Alison Schofield <alison.schofield@intel.com>



> Fixes: 3a2b97b3210b ("cxl/test: Improve init-order fidelity relative to real-world systems")
> Signed-off-by: Li Ming <ming.li@zohomail.com>
> ---
> Changes from v1:
> - Rebase to v7.1-rc4.
> - Introduce a new helper to handle the issue. (Alison)
> - Use new helper for all mock device addition.
> ---
>  tools/testing/cxl/test/cxl.c | 105 ++++++++++++++---------------------
>  1 file changed, 43 insertions(+), 62 deletions(-)
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add()
  2026-05-21  7:25 ` Alison Schofield
@ 2026-05-21 12:25   ` Li Ming
  2026-05-22 11:50     ` Li Ming
  0 siblings, 1 reply; 6+ messages in thread
From: Li Ming @ 2026-05-21 12:25 UTC (permalink / raw)
  To: Alison Schofield
  Cc: dave, jic23, dave.jiang, vishal.l.verma, ira.weiny, djbw,
	linux-cxl, linux-kernel


在 2026/5/21 15:25, Alison Schofield 写道:
> On Wed, May 20, 2026 at 08:14:57PM +0800, Li Ming wrote:
>> CXL test environment hits the following error sometimes.
>>
>>   cxl_mem mem9: endpoint7 failed probe
>>
>> All mock memdevs are platform firmware devices added by cxl_test module,
>> and cxl_test module also provides a platform device driver for them to
>> create a memdev device to CXL subsystem. cxl_test module uses
>> cxl_rcd/mem_single/mem arrays to store different types of mock memdevs.
>> CXL drivers calls registered mock functions for a mock memdev by
>> checking if a given memdev is in these arrays.
>>
>> When cxl_test module adds these mock memdevs, it always calls
>> platform_device_add() before adding them to a suitable mock memdev
>> array. However, there is a small window where CXL drivers calls mock
>> function for a added memdev before it added to a mock memdev array. In
>> above case, cxl endpoint driver considers a added memdev was not a mock
>> memdev, then calling devm_cxl_endpoint_decoders_setup() for it rather
>> than mock_endpoint_decoders_setup().
>>
>> An appropriate solution is that adding a new mock device to a mock
>> device array before calling platform_device_add() for it. It can
>> guarantee the new mock device is visible to CXL subsystem.
>>
>> This patch introduces a new helped called cxl_mock_platform_device_add()
>> to handle the issue, and uses the function for all mock devices addition.
>>
> Reviewed-by: Alison Schofield <alison.schofield@intel.com>

Hi Alison,


I missed that you mentioned putting platform_device_alloc() into the 
helper. I will send out v3 for that.


Ming

>
>
>> Fixes: 3a2b97b3210b ("cxl/test: Improve init-order fidelity relative to real-world systems")
>> Signed-off-by: Li Ming <ming.li@zohomail.com>
>> ---
>> Changes from v1:
>> - Rebase to v7.1-rc4.
>> - Introduce a new helper to handle the issue. (Alison)
>> - Use new helper for all mock device addition.
>> ---
>>   tools/testing/cxl/test/cxl.c | 105 ++++++++++++++---------------------
>>   1 file changed, 43 insertions(+), 62 deletions(-)
>>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add()
  2026-05-21 12:25   ` Li Ming
@ 2026-05-22 11:50     ` Li Ming
  2026-05-26 20:49       ` Alison Schofield
  0 siblings, 1 reply; 6+ messages in thread
From: Li Ming @ 2026-05-22 11:50 UTC (permalink / raw)
  To: Alison Schofield
  Cc: dave, jic23, dave.jiang, vishal.l.verma, ira.weiny, djbw,
	linux-cxl, linux-kernel


在 2026/5/21 20:25, Li Ming 写道:
>
> 在 2026/5/21 15:25, Alison Schofield 写道:
>> On Wed, May 20, 2026 at 08:14:57PM +0800, Li Ming wrote:
>>> CXL test environment hits the following error sometimes.
>>>
>>>   cxl_mem mem9: endpoint7 failed probe
>>>
>>> All mock memdevs are platform firmware devices added by cxl_test 
>>> module,
>>> and cxl_test module also provides a platform device driver for them to
>>> create a memdev device to CXL subsystem. cxl_test module uses
>>> cxl_rcd/mem_single/mem arrays to store different types of mock memdevs.
>>> CXL drivers calls registered mock functions for a mock memdev by
>>> checking if a given memdev is in these arrays.
>>>
>>> When cxl_test module adds these mock memdevs, it always calls
>>> platform_device_add() before adding them to a suitable mock memdev
>>> array. However, there is a small window where CXL drivers calls mock
>>> function for a added memdev before it added to a mock memdev array. In
>>> above case, cxl endpoint driver considers a added memdev was not a mock
>>> memdev, then calling devm_cxl_endpoint_decoders_setup() for it rather
>>> than mock_endpoint_decoders_setup().
>>>
>>> An appropriate solution is that adding a new mock device to a mock
>>> device array before calling platform_device_add() for it. It can
>>> guarantee the new mock device is visible to CXL subsystem.
>>>
>>> This patch introduces a new helped called 
>>> cxl_mock_platform_device_add()
>>> to handle the issue, and uses the function for all mock devices 
>>> addition.
>>>
>> Reviewed-by: Alison Schofield <alison.schofield@intel.com>
>
> Hi Alison,
>
>
> I missed that you mentioned putting platform_device_alloc() into the 
> helper. I will send out v3 for that.
>
>
After taking a look into the implementation, maybe keep current 
implementation would be better.

This helper is responsible for performing the platform_device_add() 
operation for all mock devices. However, each type of mock device has 
its own specific logic between platform_device_alloc() and 
platform_device_add().

If I move platform_device_alloc() into this helper,I will also have to 
incorporate all these device-specific implementations.

Ming

> Ming
>
>>
>>
>>> Fixes: 3a2b97b3210b ("cxl/test: Improve init-order fidelity relative 
>>> to real-world systems")
>>> Signed-off-by: Li Ming <ming.li@zohomail.com>
>>> ---
>>> Changes from v1:
>>> - Rebase to v7.1-rc4.
>>> - Introduce a new helper to handle the issue. (Alison)
>>> - Use new helper for all mock device addition.
>>> ---
>>>   tools/testing/cxl/test/cxl.c | 105 
>>> ++++++++++++++---------------------
>>>   1 file changed, 43 insertions(+), 62 deletions(-)
>>>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add()
  2026-05-22 11:50     ` Li Ming
@ 2026-05-26 20:49       ` Alison Schofield
  0 siblings, 0 replies; 6+ messages in thread
From: Alison Schofield @ 2026-05-26 20:49 UTC (permalink / raw)
  To: Li Ming
  Cc: dave, jic23, dave.jiang, vishal.l.verma, ira.weiny, djbw,
	linux-cxl, linux-kernel

On Fri, May 22, 2026 at 07:50:59PM +0800, Li Ming wrote:
> 
> 在 2026/5/21 20:25, Li Ming 写道:
> > 
> > 在 2026/5/21 15:25, Alison Schofield 写道:
> > > On Wed, May 20, 2026 at 08:14:57PM +0800, Li Ming wrote:
> > > > CXL test environment hits the following error sometimes.
> > > > 
> > > >   cxl_mem mem9: endpoint7 failed probe
> > > > 
> > > > All mock memdevs are platform firmware devices added by cxl_test
> > > > module,
> > > > and cxl_test module also provides a platform device driver for them to
> > > > create a memdev device to CXL subsystem. cxl_test module uses
> > > > cxl_rcd/mem_single/mem arrays to store different types of mock memdevs.
> > > > CXL drivers calls registered mock functions for a mock memdev by
> > > > checking if a given memdev is in these arrays.
> > > > 
> > > > When cxl_test module adds these mock memdevs, it always calls
> > > > platform_device_add() before adding them to a suitable mock memdev
> > > > array. However, there is a small window where CXL drivers calls mock
> > > > function for a added memdev before it added to a mock memdev array. In
> > > > above case, cxl endpoint driver considers a added memdev was not a mock
> > > > memdev, then calling devm_cxl_endpoint_decoders_setup() for it rather
> > > > than mock_endpoint_decoders_setup().
> > > > 
> > > > An appropriate solution is that adding a new mock device to a mock
> > > > device array before calling platform_device_add() for it. It can
> > > > guarantee the new mock device is visible to CXL subsystem.
> > > > 
> > > > This patch introduces a new helped called
> > > > cxl_mock_platform_device_add()
> > > > to handle the issue, and uses the function for all mock devices
> > > > addition.
> > > > 
> > > Reviewed-by: Alison Schofield <alison.schofield@intel.com>
> > 
> > Hi Alison,
> > 
> > 
> > I missed that you mentioned putting platform_device_alloc() into the
> > helper. I will send out v3 for that.
> > 
> > 
> After taking a look into the implementation, maybe keep current
> implementation would be better.
> 
> This helper is responsible for performing the platform_device_add()
> operation for all mock devices. However, each type of mock device has its
> own specific logic between platform_device_alloc() and
> platform_device_add().
> 
> If I move platform_device_alloc() into this helper,I will also have to
> incorporate all these device-specific implementations.

That makes sense Ming. Thanks for trying it out.

I'll add to my previous tag,

Tested-by: Alison Schofield <alison.schofield@intel.com>

> 
> Ming
> 
> > Ming
> > 
snip

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add()
  2026-05-20 12:14 [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add() Li Ming
  2026-05-21  7:25 ` Alison Schofield
@ 2026-05-26 21:36 ` Dave Jiang
  1 sibling, 0 replies; 6+ messages in thread
From: Dave Jiang @ 2026-05-26 21:36 UTC (permalink / raw)
  To: Li Ming, dave, jic23, alison.schofield, vishal.l.verma, ira.weiny, djbw
  Cc: linux-cxl, linux-kernel



On 5/20/26 5:14 AM, Li Ming wrote:
> CXL test environment hits the following error sometimes.
> 
>  cxl_mem mem9: endpoint7 failed probe
> 
> All mock memdevs are platform firmware devices added by cxl_test module,
> and cxl_test module also provides a platform device driver for them to
> create a memdev device to CXL subsystem. cxl_test module uses
> cxl_rcd/mem_single/mem arrays to store different types of mock memdevs.
> CXL drivers calls registered mock functions for a mock memdev by
> checking if a given memdev is in these arrays.
> 
> When cxl_test module adds these mock memdevs, it always calls
> platform_device_add() before adding them to a suitable mock memdev
> array. However, there is a small window where CXL drivers calls mock
> function for a added memdev before it added to a mock memdev array. In
> above case, cxl endpoint driver considers a added memdev was not a mock
> memdev, then calling devm_cxl_endpoint_decoders_setup() for it rather
> than mock_endpoint_decoders_setup().
> 
> An appropriate solution is that adding a new mock device to a mock
> device array before calling platform_device_add() for it. It can
> guarantee the new mock device is visible to CXL subsystem.
> 
> This patch introduces a new helped called cxl_mock_platform_device_add()
> to handle the issue, and uses the function for all mock devices addition.
> 
> Fixes: 3a2b97b3210b ("cxl/test: Improve init-order fidelity relative to real-world systems")
> Signed-off-by: Li Ming <ming.li@zohomail.com>

Applied to cxl/fixes
d90f236f8b9e354848bd226f581db27755ab901d

> ---
> Changes from v1:
> - Rebase to v7.1-rc4.
> - Introduce a new helper to handle the issue. (Alison)
> - Use new helper for all mock device addition.
> ---
>  tools/testing/cxl/test/cxl.c | 105 ++++++++++++++---------------------
>  1 file changed, 43 insertions(+), 62 deletions(-)
> 
> diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
> index 418669927fb0..296516eecfd6 100644
> --- a/tools/testing/cxl/test/cxl.c
> +++ b/tools/testing/cxl/test/cxl.c
> @@ -1523,6 +1523,23 @@ static void mock_companion(struct acpi_device *adev, struct device *dev)
>  #define SZ_64G (SZ_32G * 2)
>  #endif
>  
> +static int cxl_mock_platform_device_add(struct platform_device *pdev,
> +					struct platform_device **ppdev)
> +{
> +	int rc;
> +
> +	if (ppdev)
> +		*ppdev = pdev;
> +	rc = platform_device_add(pdev);
> +	if (rc) {
> +		platform_device_put(pdev);
> +		if (ppdev)
> +			*ppdev = NULL;
> +	}
> +
> +	return rc;
> +}
> +
>  static __init int cxl_rch_topo_init(void)
>  {
>  	int rc, i;
> @@ -1537,13 +1554,10 @@ static __init int cxl_rch_topo_init(void)
>  			goto err_bridge;
>  
>  		mock_companion(adev, &pdev->dev);
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_rch[i]);
> +		if (rc)
>  			goto err_bridge;
> -		}
>  
> -		cxl_rch[i] = pdev;
>  		mock_pci_bus[idx].bridge = &pdev->dev;
>  		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
>  				       "firmware_node");
> @@ -1595,13 +1609,10 @@ static __init int cxl_single_topo_init(void)
>  			goto err_bridge;
>  
>  		mock_companion(adev, &pdev->dev);
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_hb_single[i]);
> +		if (rc)
>  			goto err_bridge;
> -		}
>  
> -		cxl_hb_single[i] = pdev;
>  		mock_pci_bus[i + NR_CXL_HOST_BRIDGES].bridge = &pdev->dev;
>  		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
>  				       "physical_node");
> @@ -1620,12 +1631,9 @@ static __init int cxl_single_topo_init(void)
>  			goto err_port;
>  		pdev->dev.parent = &bridge->dev;
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_root_single[i]);
> +		if (rc)
>  			goto err_port;
> -		}
> -		cxl_root_single[i] = pdev;
>  	}
>  
>  	for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++) {
> @@ -1638,12 +1646,9 @@ static __init int cxl_single_topo_init(void)
>  			goto err_uport;
>  		pdev->dev.parent = &root_port->dev;
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_swu_single[i]);
> +		if (rc)
>  			goto err_uport;
> -		}
> -		cxl_swu_single[i] = pdev;
>  	}
>  
>  	for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++) {
> @@ -1657,12 +1662,9 @@ static __init int cxl_single_topo_init(void)
>  			goto err_dport;
>  		pdev->dev.parent = &uport->dev;
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_swd_single[i]);
> +		if (rc)
>  			goto err_dport;
> -		}
> -		cxl_swd_single[i] = pdev;
>  	}
>  
>  	return 0;
> @@ -1735,12 +1737,9 @@ static int cxl_mem_init(void)
>  		pdev->dev.parent = &dport->dev;
>  		set_dev_node(&pdev->dev, i % 2);
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_mem[i]);
> +		if (rc)
>  			goto err_mem;
> -		}
> -		cxl_mem[i] = pdev;
>  	}
>  
>  	for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++) {
> @@ -1753,12 +1752,9 @@ static int cxl_mem_init(void)
>  		pdev->dev.parent = &dport->dev;
>  		set_dev_node(&pdev->dev, i % 2);
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_mem_single[i]);
> +		if (rc)
>  			goto err_single;
> -		}
> -		cxl_mem_single[i] = pdev;
>  	}
>  
>  	for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++) {
> @@ -1772,12 +1768,9 @@ static int cxl_mem_init(void)
>  		pdev->dev.parent = &rch->dev;
>  		set_dev_node(&pdev->dev, i % 2);
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_rcd[i]);
> +		if (rc)
>  			goto err_rcd;
> -		}
> -		cxl_rcd[i] = pdev;
>  	}
>  
>  	return 0;
> @@ -1869,13 +1862,10 @@ static __init int cxl_test_init(void)
>  			goto err_bridge;
>  
>  		mock_companion(adev, &pdev->dev);
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_host_bridge[i]);
> +		if (rc)
>  			goto err_bridge;
> -		}
>  
> -		cxl_host_bridge[i] = pdev;
>  		mock_pci_bus[i].bridge = &pdev->dev;
>  		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
>  				       "physical_node");
> @@ -1893,12 +1883,9 @@ static __init int cxl_test_init(void)
>  			goto err_port;
>  		pdev->dev.parent = &bridge->dev;
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_root_port[i]);
> +		if (rc)
>  			goto err_port;
> -		}
> -		cxl_root_port[i] = pdev;
>  	}
>  
>  	BUILD_BUG_ON(ARRAY_SIZE(cxl_switch_uport) != ARRAY_SIZE(cxl_root_port));
> @@ -1911,12 +1898,9 @@ static __init int cxl_test_init(void)
>  			goto err_uport;
>  		pdev->dev.parent = &root_port->dev;
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_switch_uport[i]);
> +		if (rc)
>  			goto err_uport;
> -		}
> -		cxl_switch_uport[i] = pdev;
>  	}
>  
>  	for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++) {
> @@ -1929,12 +1913,9 @@ static __init int cxl_test_init(void)
>  			goto err_dport;
>  		pdev->dev.parent = &uport->dev;
>  
> -		rc = platform_device_add(pdev);
> -		if (rc) {
> -			platform_device_put(pdev);
> +		rc = cxl_mock_platform_device_add(pdev, &cxl_switch_dport[i]);
> +		if (rc)
>  			goto err_dport;
> -		}
> -		cxl_switch_dport[i] = pdev;
>  	}
>  
>  	rc = cxl_single_topo_init();
> @@ -1953,9 +1934,9 @@ static __init int cxl_test_init(void)
>  	acpi0017_mock.dev.bus = &platform_bus_type;
>  	cxl_acpi->dev.groups = cxl_acpi_groups;
>  
> -	rc = platform_device_add(cxl_acpi);
> +	rc = cxl_mock_platform_device_add(cxl_acpi, NULL);
>  	if (rc)
> -		goto err_root;
> +		goto err_rch;
>  
>  	rc = cxl_mem_init();
>  	if (rc)


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-05-26 21:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-20 12:14 [PATCH v2 1/1] cxl/test: Update mock dev array before calling platform_device_add() Li Ming
2026-05-21  7:25 ` Alison Schofield
2026-05-21 12:25   ` Li Ming
2026-05-22 11:50     ` Li Ming
2026-05-26 20:49       ` Alison Schofield
2026-05-26 21:36 ` Dave Jiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome