public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling
@ 2025-02-07  9:39 Thomas Weißschuh
  2025-02-07  9:39 ` [PATCH net v2 1/5] ptp: vmclock: Add .owner to vmclock_miscdev_fops Thomas Weißschuh
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Thomas Weißschuh @ 2025-02-07  9:39 UTC (permalink / raw)
  To: David Woodhouse, Richard Cochran, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Mateusz Polchlopek, David Woodhouse, netdev, linux-kernel,
	Thomas Weißschuh, stable

Some error handling issues I noticed while looking at the code.

Only compile-tested.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
Changes in v2:
- Fix typo in commit message: vmclock_ptp_register()
- Retarget against net tree
- Include patch from David. It's at the start of the series so that all
  bugfixes are at the beginning and the logical ordering of my patches
  is not disrupted.
- Pick up tags from LKML
- Link to v1: https://lore.kernel.org/r/20250206-vmclock-probe-v1-0-17a3ea07be34@linutronix.de

---
David Woodhouse (1):
      ptp: vmclock: Add .owner to vmclock_miscdev_fops

Thomas Weißschuh (4):
      ptp: vmclock: Set driver data before its usage
      ptp: vmclock: Don't unregister misc device if it was not registered
      ptp: vmclock: Clean up miscdev and ptp clock through devres
      ptp: vmclock: Remove goto-based cleanup logic

 drivers/ptp/ptp_vmclock.c | 47 +++++++++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 26 deletions(-)
---
base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b
change-id: 20250206-vmclock-probe-57cbcb770925

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh@linutronix.de>


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

* [PATCH net v2 1/5] ptp: vmclock: Add .owner to vmclock_miscdev_fops
  2025-02-07  9:39 [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling Thomas Weißschuh
@ 2025-02-07  9:39 ` Thomas Weißschuh
  2025-02-07  9:39 ` [PATCH net v2 2/5] ptp: vmclock: Set driver data before its usage Thomas Weißschuh
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Thomas Weißschuh @ 2025-02-07  9:39 UTC (permalink / raw)
  To: David Woodhouse, Richard Cochran, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Mateusz Polchlopek, David Woodhouse, netdev, linux-kernel,
	Thomas Weißschuh, stable

From: David Woodhouse <dwmw@amazon.co.uk>

Without the .owner field, the module can be unloaded while /dev/vmclock0
is open, leading to an oops.

Fixes: 205032724226 ("ptp: Add support for the AMZNC10C 'vmclock' device")
Cc: stable@vger.kernel.org
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
 drivers/ptp/ptp_vmclock.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/ptp/ptp_vmclock.c b/drivers/ptp/ptp_vmclock.c
index 0a2cfc8ad3c5408a87fd8fedeff274ab895de3dd..dbc73e5382935dcbc799ea608b87f5ff51044ebc 100644
--- a/drivers/ptp/ptp_vmclock.c
+++ b/drivers/ptp/ptp_vmclock.c
@@ -414,6 +414,7 @@ static ssize_t vmclock_miscdev_read(struct file *fp, char __user *buf,
 }
 
 static const struct file_operations vmclock_miscdev_fops = {
+	.owner = THIS_MODULE,
 	.mmap = vmclock_miscdev_mmap,
 	.read = vmclock_miscdev_read,
 };

-- 
2.48.1


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

* [PATCH net v2 2/5] ptp: vmclock: Set driver data before its usage
  2025-02-07  9:39 [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling Thomas Weißschuh
  2025-02-07  9:39 ` [PATCH net v2 1/5] ptp: vmclock: Add .owner to vmclock_miscdev_fops Thomas Weißschuh
@ 2025-02-07  9:39 ` Thomas Weißschuh
  2025-02-07  9:39 ` [PATCH net v2 3/5] ptp: vmclock: Don't unregister misc device if it was not registered Thomas Weißschuh
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Thomas Weißschuh @ 2025-02-07  9:39 UTC (permalink / raw)
  To: David Woodhouse, Richard Cochran, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Mateusz Polchlopek, David Woodhouse, netdev, linux-kernel,
	Thomas Weißschuh, stable

If vmclock_ptp_register() fails during probing, vmclock_remove() is
called to clean up the ptp clock and misc device.
It uses dev_get_drvdata() to access the vmclock state.
However the driver data is not yet set at this point.

Assign the driver data earlier.

Fixes: 205032724226 ("ptp: Add support for the AMZNC10C 'vmclock' device")
Cc: stable@vger.kernel.org
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Reviewed-by: Mateusz Polchlopek <mateusz.polchlopek@intel.com>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Reviewed-by: David Woodhouse <dwmw@amazon.co.uk>
---
 drivers/ptp/ptp_vmclock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/ptp/ptp_vmclock.c b/drivers/ptp/ptp_vmclock.c
index dbc73e5382935dcbc799ea608b87f5ff51044ebc..1ba30a2da570fb4d1ec9db72820bf1781dfa9655 100644
--- a/drivers/ptp/ptp_vmclock.c
+++ b/drivers/ptp/ptp_vmclock.c
@@ -525,6 +525,8 @@ static int vmclock_probe(struct platform_device *pdev)
 		goto out;
 	}
 
+	dev_set_drvdata(dev, st);
+
 	if (le32_to_cpu(st->clk->magic) != VMCLOCK_MAGIC ||
 	    le32_to_cpu(st->clk->size) > resource_size(&st->res) ||
 	    le16_to_cpu(st->clk->version) != 1) {
@@ -588,8 +590,6 @@ static int vmclock_probe(struct platform_device *pdev)
 		 (st->miscdev.minor && st->ptp_clock) ? ", " : "",
 		 st->ptp_clock ? "PTP" : "");
 
-	dev_set_drvdata(dev, st);
-
  out:
 	return ret;
 }

-- 
2.48.1


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

* [PATCH net v2 3/5] ptp: vmclock: Don't unregister misc device if it was not registered
  2025-02-07  9:39 [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling Thomas Weißschuh
  2025-02-07  9:39 ` [PATCH net v2 1/5] ptp: vmclock: Add .owner to vmclock_miscdev_fops Thomas Weißschuh
  2025-02-07  9:39 ` [PATCH net v2 2/5] ptp: vmclock: Set driver data before its usage Thomas Weißschuh
@ 2025-02-07  9:39 ` Thomas Weißschuh
  2025-02-07  9:39 ` [PATCH net v2 4/5] ptp: vmclock: Clean up miscdev and ptp clock through devres Thomas Weißschuh
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Thomas Weißschuh @ 2025-02-07  9:39 UTC (permalink / raw)
  To: David Woodhouse, Richard Cochran, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Mateusz Polchlopek, David Woodhouse, netdev, linux-kernel,
	Thomas Weißschuh, stable

vmclock_remove() tries to detect the successful registration of the misc
device based on the value of its minor value.
However that check is incorrect if the misc device registration was not
attempted in the first place.

Always initialize the minor number, so the check works properly.

Fixes: 205032724226 ("ptp: Add support for the AMZNC10C 'vmclock' device")
Cc: stable@vger.kernel.org
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Reviewed-by: David Woodhouse <dwmw@amazon.co.uk>
---
 drivers/ptp/ptp_vmclock.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/ptp/ptp_vmclock.c b/drivers/ptp/ptp_vmclock.c
index 1ba30a2da570fb4d1ec9db72820bf1781dfa9655..9b8bd626a397313433908fcc838edf8ffc3ecc98 100644
--- a/drivers/ptp/ptp_vmclock.c
+++ b/drivers/ptp/ptp_vmclock.c
@@ -550,6 +550,8 @@ static int vmclock_probe(struct platform_device *pdev)
 		goto out;
 	}
 
+	st->miscdev.minor = MISC_DYNAMIC_MINOR;
+
 	/*
 	 * If the structure is big enough, it can be mapped to userspace.
 	 * Theoretically a guest OS even using larger pages could still
@@ -557,7 +559,6 @@ static int vmclock_probe(struct platform_device *pdev)
 	 * cross that bridge if/when we come to it.
 	 */
 	if (le32_to_cpu(st->clk->size) >= PAGE_SIZE) {
-		st->miscdev.minor = MISC_DYNAMIC_MINOR;
 		st->miscdev.fops = &vmclock_miscdev_fops;
 		st->miscdev.name = st->name;
 

-- 
2.48.1


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

* [PATCH net v2 4/5] ptp: vmclock: Clean up miscdev and ptp clock through devres
  2025-02-07  9:39 [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling Thomas Weißschuh
                   ` (2 preceding siblings ...)
  2025-02-07  9:39 ` [PATCH net v2 3/5] ptp: vmclock: Don't unregister misc device if it was not registered Thomas Weißschuh
@ 2025-02-07  9:39 ` Thomas Weißschuh
  2025-02-07  9:39 ` [PATCH net v2 5/5] ptp: vmclock: Remove goto-based cleanup logic Thomas Weißschuh
  2025-02-11  9:40 ` [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling patchwork-bot+netdevbpf
  5 siblings, 0 replies; 7+ messages in thread
From: Thomas Weißschuh @ 2025-02-07  9:39 UTC (permalink / raw)
  To: David Woodhouse, Richard Cochran, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Mateusz Polchlopek, David Woodhouse, netdev, linux-kernel,
	Thomas Weißschuh

Most resources owned by the vmclock device are managed through devres.
Only the miscdev and ptp clock are managed manually.
This makes the code slightly harder to understand than necessary.

Switch them over to devres and remove the now unnecessary drvdata.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Reviewed-by: David Woodhouse <dwmw@amazon.co.uk>
---
 drivers/ptp/ptp_vmclock.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/ptp/ptp_vmclock.c b/drivers/ptp/ptp_vmclock.c
index 9b8bd626a397313433908fcc838edf8ffc3ecc98..09c023fb94b7e97137433bf18c3a065e26a36c6c 100644
--- a/drivers/ptp/ptp_vmclock.c
+++ b/drivers/ptp/ptp_vmclock.c
@@ -421,10 +421,9 @@ static const struct file_operations vmclock_miscdev_fops = {
 
 /* module operations */
 
-static void vmclock_remove(struct platform_device *pdev)
+static void vmclock_remove(void *data)
 {
-	struct device *dev = &pdev->dev;
-	struct vmclock_state *st = dev_get_drvdata(dev);
+	struct vmclock_state *st = data;
 
 	if (st->ptp_clock)
 		ptp_clock_unregister(st->ptp_clock);
@@ -525,8 +524,6 @@ static int vmclock_probe(struct platform_device *pdev)
 		goto out;
 	}
 
-	dev_set_drvdata(dev, st);
-
 	if (le32_to_cpu(st->clk->magic) != VMCLOCK_MAGIC ||
 	    le32_to_cpu(st->clk->size) > resource_size(&st->res) ||
 	    le16_to_cpu(st->clk->version) != 1) {
@@ -552,6 +549,10 @@ static int vmclock_probe(struct platform_device *pdev)
 
 	st->miscdev.minor = MISC_DYNAMIC_MINOR;
 
+	ret = devm_add_action_or_reset(&pdev->dev, vmclock_remove, st);
+	if (ret)
+		goto out;
+
 	/*
 	 * If the structure is big enough, it can be mapped to userspace.
 	 * Theoretically a guest OS even using larger pages could still
@@ -574,7 +575,6 @@ static int vmclock_probe(struct platform_device *pdev)
 		if (IS_ERR(st->ptp_clock)) {
 			ret = PTR_ERR(st->ptp_clock);
 			st->ptp_clock = NULL;
-			vmclock_remove(pdev);
 			goto out;
 		}
 	}
@@ -603,7 +603,6 @@ MODULE_DEVICE_TABLE(acpi, vmclock_acpi_ids);
 
 static struct platform_driver vmclock_platform_driver = {
 	.probe		= vmclock_probe,
-	.remove		= vmclock_remove,
 	.driver	= {
 		.name	= "vmclock",
 		.acpi_match_table = vmclock_acpi_ids,

-- 
2.48.1


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

* [PATCH net v2 5/5] ptp: vmclock: Remove goto-based cleanup logic
  2025-02-07  9:39 [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling Thomas Weißschuh
                   ` (3 preceding siblings ...)
  2025-02-07  9:39 ` [PATCH net v2 4/5] ptp: vmclock: Clean up miscdev and ptp clock through devres Thomas Weißschuh
@ 2025-02-07  9:39 ` Thomas Weißschuh
  2025-02-11  9:40 ` [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling patchwork-bot+netdevbpf
  5 siblings, 0 replies; 7+ messages in thread
From: Thomas Weißschuh @ 2025-02-07  9:39 UTC (permalink / raw)
  To: David Woodhouse, Richard Cochran, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Mateusz Polchlopek, David Woodhouse, netdev, linux-kernel,
	Thomas Weißschuh

vmclock_probe() uses an "out:" label to return from the function on
error. This indicates that some cleanup operation is necessary.
However the label does not do anything as all resources are managed
through devres, making the code slightly harder to read.

Remove the label and just return directly.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Reviewed-by: David Woodhouse <dwmw@amazon.co.uk>
---
 drivers/ptp/ptp_vmclock.c | 32 +++++++++++++-------------------
 1 file changed, 13 insertions(+), 19 deletions(-)

diff --git a/drivers/ptp/ptp_vmclock.c b/drivers/ptp/ptp_vmclock.c
index 09c023fb94b7e97137433bf18c3a065e26a36c6c..b3a83b03d9c14e10213c6cb94e6a42b38c59546f 100644
--- a/drivers/ptp/ptp_vmclock.c
+++ b/drivers/ptp/ptp_vmclock.c
@@ -506,14 +506,13 @@ static int vmclock_probe(struct platform_device *pdev)
 
 	if (ret) {
 		dev_info(dev, "Failed to obtain physical address: %d\n", ret);
-		goto out;
+		return ret;
 	}
 
 	if (resource_size(&st->res) < VMCLOCK_MIN_SIZE) {
 		dev_info(dev, "Region too small (0x%llx)\n",
 			 resource_size(&st->res));
-		ret = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 	st->clk = devm_memremap(dev, st->res.start, resource_size(&st->res),
 				MEMREMAP_WB | MEMREMAP_DEC);
@@ -521,37 +520,34 @@ static int vmclock_probe(struct platform_device *pdev)
 		ret = PTR_ERR(st->clk);
 		dev_info(dev, "failed to map shared memory\n");
 		st->clk = NULL;
-		goto out;
+		return ret;
 	}
 
 	if (le32_to_cpu(st->clk->magic) != VMCLOCK_MAGIC ||
 	    le32_to_cpu(st->clk->size) > resource_size(&st->res) ||
 	    le16_to_cpu(st->clk->version) != 1) {
 		dev_info(dev, "vmclock magic fields invalid\n");
-		ret = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 
 	ret = ida_alloc(&vmclock_ida, GFP_KERNEL);
 	if (ret < 0)
-		goto out;
+		return ret;
 
 	st->index = ret;
 	ret = devm_add_action_or_reset(&pdev->dev, vmclock_put_idx, st);
 	if (ret)
-		goto out;
+		return ret;
 
 	st->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "vmclock%d", st->index);
-	if (!st->name) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!st->name)
+		return -ENOMEM;
 
 	st->miscdev.minor = MISC_DYNAMIC_MINOR;
 
 	ret = devm_add_action_or_reset(&pdev->dev, vmclock_remove, st);
 	if (ret)
-		goto out;
+		return ret;
 
 	/*
 	 * If the structure is big enough, it can be mapped to userspace.
@@ -565,7 +561,7 @@ static int vmclock_probe(struct platform_device *pdev)
 
 		ret = misc_register(&st->miscdev);
 		if (ret)
-			goto out;
+			return ret;
 	}
 
 	/* If there is valid clock information, register a PTP clock */
@@ -575,15 +571,14 @@ static int vmclock_probe(struct platform_device *pdev)
 		if (IS_ERR(st->ptp_clock)) {
 			ret = PTR_ERR(st->ptp_clock);
 			st->ptp_clock = NULL;
-			goto out;
+			return ret;
 		}
 	}
 
 	if (!st->miscdev.minor && !st->ptp_clock) {
 		/* Neither miscdev nor PTP registered */
 		dev_info(dev, "vmclock: Neither miscdev nor PTP available; not registering\n");
-		ret = -ENODEV;
-		goto out;
+		return -ENODEV;
 	}
 
 	dev_info(dev, "%s: registered %s%s%s\n", st->name,
@@ -591,8 +586,7 @@ static int vmclock_probe(struct platform_device *pdev)
 		 (st->miscdev.minor && st->ptp_clock) ? ", " : "",
 		 st->ptp_clock ? "PTP" : "");
 
- out:
-	return ret;
+	return 0;
 }
 
 static const struct acpi_device_id vmclock_acpi_ids[] = {

-- 
2.48.1


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

* Re: [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling
  2025-02-07  9:39 [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling Thomas Weißschuh
                   ` (4 preceding siblings ...)
  2025-02-07  9:39 ` [PATCH net v2 5/5] ptp: vmclock: Remove goto-based cleanup logic Thomas Weißschuh
@ 2025-02-11  9:40 ` patchwork-bot+netdevbpf
  5 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-02-11  9:40 UTC (permalink / raw)
  To: =?utf-8?q?Thomas_Wei=C3=9Fschuh_=3Cthomas=2Eweissschuh=40linutronix=2Ede=3E?=
  Cc: dwmw2, richardcochran, andrew+netdev, davem, edumazet, kuba,
	pabeni, mateusz.polchlopek, dwmw, netdev, linux-kernel, stable

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <pabeni@redhat.com>:

On Fri, 07 Feb 2025 10:39:01 +0100 you wrote:
> Some error handling issues I noticed while looking at the code.
> 
> Only compile-tested.
> 
> Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
> ---
> Changes in v2:
> - Fix typo in commit message: vmclock_ptp_register()
> - Retarget against net tree
> - Include patch from David. It's at the start of the series so that all
>   bugfixes are at the beginning and the logical ordering of my patches
>   is not disrupted.
> - Pick up tags from LKML
> - Link to v1: https://lore.kernel.org/r/20250206-vmclock-probe-v1-0-17a3ea07be34@linutronix.de
> 
> [...]

Here is the summary with links:
  - [net,v2,1/5] ptp: vmclock: Add .owner to vmclock_miscdev_fops
    https://git.kernel.org/netdev/net/c/7b07b040257c
  - [net,v2,2/5] ptp: vmclock: Set driver data before its usage
    https://git.kernel.org/netdev/net/c/f7d07cd4f77d
  - [net,v2,3/5] ptp: vmclock: Don't unregister misc device if it was not registered
    https://git.kernel.org/netdev/net/c/39e926c3a21b
  - [net,v2,4/5] ptp: vmclock: Clean up miscdev and ptp clock through devres
    https://git.kernel.org/netdev/net/c/9a884c3800b2
  - [net,v2,5/5] ptp: vmclock: Remove goto-based cleanup logic
    https://git.kernel.org/netdev/net/c/b4c1fde5ced9

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2025-02-11  9:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-07  9:39 [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling Thomas Weißschuh
2025-02-07  9:39 ` [PATCH net v2 1/5] ptp: vmclock: Add .owner to vmclock_miscdev_fops Thomas Weißschuh
2025-02-07  9:39 ` [PATCH net v2 2/5] ptp: vmclock: Set driver data before its usage Thomas Weißschuh
2025-02-07  9:39 ` [PATCH net v2 3/5] ptp: vmclock: Don't unregister misc device if it was not registered Thomas Weißschuh
2025-02-07  9:39 ` [PATCH net v2 4/5] ptp: vmclock: Clean up miscdev and ptp clock through devres Thomas Weißschuh
2025-02-07  9:39 ` [PATCH net v2 5/5] ptp: vmclock: Remove goto-based cleanup logic Thomas Weißschuh
2025-02-11  9:40 ` [PATCH net v2 0/5] ptp: vmclock: bugfixes and cleanups for error handling patchwork-bot+netdevbpf

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