ptp: vmclock: bugfixes and cleanups for error handling
Subsystems (5)
v2 cover ptp: vmclock: bugfixes and cleanups for error handling
Some error handling issues I noticed while looking at the code.
Only compile-tested.
---
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/[email protected]
---
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,
Signature · 1 line
Thomas Weißschuh <[email protected]>
PATCH v2 1/5 ptp: vmclock: Add .owner to vmclock_miscdev_fops
From: David Woodhouse <[email protected]>
Without the .owner field, the module can be unloaded while /dev/vmclock0 is open, leading to an oops.
--- drivers/ptp/ptp_vmclock.c | 1 + 1 file changed, 1 insertion(+)
drivers/ptp/ptp_vmclock.c +1 -0
@@ -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, };
Signature · 1 line
2.48.1
PATCH v2 2/5 ptp: vmclock: Set driver data before its usage
In reply to Thomas WeißschuhIf 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.
--- drivers/ptp/ptp_vmclock.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/ptp/ptp_vmclock.c +2 -2
@@ -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; }
Signature · 1 line
2.48.1
PATCH v2 3/5 ptp: vmclock: Don't unregister misc device if it was not registered
In reply to Thomas Weißschuhvmclock_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.
--- drivers/ptp/ptp_vmclock.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/ptp/ptp_vmclock.c +2 -1
@@ -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;
Signature · 1 line
2.48.1
PATCH v2 4/5 ptp: vmclock: Clean up miscdev and ptp clock through devres
In reply to Thomas WeißschuhMost 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.
--- drivers/ptp/ptp_vmclock.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/ptp/ptp_vmclock.c +6 -7
@@ -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,
Signature · 1 line
2.48.1
PATCH v2 5/5 ptp: vmclock: Remove goto-based cleanup logic
In reply to Thomas Weißschuhvmclock_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.
--- drivers/ptp/ptp_vmclock.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-)
drivers/ptp/ptp_vmclock.c +13 -19
@@ -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[] = {
Signature · 1 line
2.48.1
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 15 lines · 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 <[email protected]>---
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/[email protected]
Quoted 1 line
[...]
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!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html