As device_create_file() can return error number,
it should be better to check the return value and
deal with the exception.
Moreover, this driver should be using an attribute group.
Fixes: 0ba002bc4393 ("virt: Add vboxguest driver for Virtual Box Guest integration")
Signed-off-by: Jiasheng Jiang <[email protected]>
---
Change log:
v1 -> v2:
1. Use gdev->dev instead of dev.
2. Remove file when creation failed.
---
drivers/virt/vboxguest/vboxguest_linux.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/virt/vboxguest/vboxguest_linux.c b/drivers/virt/vboxguest/vboxguest_linux.c
index 4ccfd30c2a30..2fb9a6d91b53 100644
--- a/drivers/virt/vboxguest/vboxguest_linux.c
+++ b/drivers/virt/vboxguest/vboxguest_linux.c
@@ -390,8 +390,13 @@ static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
}
pci_set_drvdata(pci, gdev);
- device_create_file(dev, &dev_attr_host_version);
- device_create_file(dev, &dev_attr_host_features);
+
+ ret = device_create_file(gdev->dev, &dev_attr_host_features);
+ if (ret)
+ goto err_unregister_misc_device_user;
+ ret = device_create_file(gdev->dev, &dev_attr_host_version);
+ if (ret)
+ goto err_remove_file_features;
vbg_info("vboxguest: misc device minor %d, IRQ %d, I/O port %x, MMIO at %pap (size %pap)\n",
gdev->misc_device.minor, pci->irq, gdev->io_port,
@@ -399,6 +404,8 @@ static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
return 0;
+err_remove_file_features:
+ device_remove_file(gdev->dev, &dev_attr_host_features);
err_unregister_misc_device_user:
misc_deregister(&gdev->misc_device_user);
err_unregister_misc_device:
--
2.25.1
On Thu, Sep 01, 2022 at 05:17:52PM +0800, Jiasheng Jiang wrote:
> As device_create_file() can return error number,
> it should be better to check the return value and
> deal with the exception.
> Moreover, this driver should be using an attribute group.
>
> Fixes: 0ba002bc4393 ("virt: Add vboxguest driver for Virtual Box Guest integration")
> Signed-off-by: Jiasheng Jiang <[email protected]>
> ---
> Change log:
>
> v1 -> v2:
>
> 1. Use gdev->dev instead of dev.
> 2. Remove file when creation failed.
> ---
> drivers/virt/vboxguest/vboxguest_linux.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/virt/vboxguest/vboxguest_linux.c b/drivers/virt/vboxguest/vboxguest_linux.c
> index 4ccfd30c2a30..2fb9a6d91b53 100644
> --- a/drivers/virt/vboxguest/vboxguest_linux.c
> +++ b/drivers/virt/vboxguest/vboxguest_linux.c
> @@ -390,8 +390,13 @@ static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
> }
>
> pci_set_drvdata(pci, gdev);
> - device_create_file(dev, &dev_attr_host_version);
> - device_create_file(dev, &dev_attr_host_features);
> +
> + ret = device_create_file(gdev->dev, &dev_attr_host_features);
> + if (ret)
> + goto err_unregister_misc_device_user;
> + ret = device_create_file(gdev->dev, &dev_attr_host_version);
> + if (ret)
> + goto err_remove_file_features;
>
> vbg_info("vboxguest: misc device minor %d, IRQ %d, I/O port %x, MMIO at %pap (size %pap)\n",
> gdev->misc_device.minor, pci->irq, gdev->io_port,
> @@ -399,6 +404,8 @@ static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
>
> return 0;
>
> +err_remove_file_features:
> + device_remove_file(gdev->dev, &dev_attr_host_features);
> err_unregister_misc_device_user:
> misc_deregister(&gdev->misc_device_user);
> err_unregister_misc_device:
> --
> 2.25.1
>
Please make a patch series that is linked together, the one you just did
was not properly threaded at all (hint, use git send-email).
And again, this is not the correct way to solve this issue, use a
default attribute group for the driver instead. No driver should ever
be adding or removing files individually like this.
thanks,
greg k-h
On Thu, Sep 01, 2022 at 06:16:49PM +0800, Jiasheng Jiang wrote:
> On Thu, Sep 01, 2022 at 05:39:46PM +0800, Greg KH wrote:
> >> - device_create_file(dev, &dev_attr_host_version);
> >> - device_create_file(dev, &dev_attr_host_features);
> >> +
> >> + ret = device_create_file(gdev->dev, &dev_attr_host_features);
> >> + if (ret)
> >> + goto err_unregister_misc_device_user;
> >> + ret = device_create_file(gdev->dev, &dev_attr_host_version);
> >> + if (ret)
> >> + goto err_remove_file_features;
> >>
> >> vbg_info("vboxguest: misc device minor %d, IRQ %d, I/O port %x, MMIO at %pap (size %pap)\n",
> >> gdev->misc_device.minor, pci->irq, gdev->io_port,
> >> @@ -399,6 +404,8 @@ static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
> >>
> >> return 0;
> >>
> >> +err_remove_file_features:
> >> + device_remove_file(gdev->dev, &dev_attr_host_features);
> > Please make a patch series that is linked together, the one you just did
> > was not properly threaded at all (hint, use git send-email).
> >
> > And again, this is not the correct way to solve this issue, use a
> > default attribute group for the driver instead. No driver should ever
> > be adding or removing files individually like this.
>
> Need I use the group to replace the individually removal in
> vbg_pci_remove() too?
If you use a group, the driver core handles all of the adding and
removing automatically for you. Look at other commits that set the
default groups attribute for a driver for examples.
thanks,
greg k-h
Hi,
On 9/1/22 12:16, Jiasheng Jiang wrote:
> On Thu, Sep 01, 2022 at 05:39:46PM +0800, Greg KH wrote:
>>> - device_create_file(dev, &dev_attr_host_version);
>>> - device_create_file(dev, &dev_attr_host_features);
>>> +
>>> + ret = device_create_file(gdev->dev, &dev_attr_host_features);
>>> + if (ret)
>>> + goto err_unregister_misc_device_user;
>>> + ret = device_create_file(gdev->dev, &dev_attr_host_version);
>>> + if (ret)
>>> + goto err_remove_file_features;
>>>
>>> vbg_info("vboxguest: misc device minor %d, IRQ %d, I/O port %x, MMIO at %pap (size %pap)\n",
>>> gdev->misc_device.minor, pci->irq, gdev->io_port,
>>> @@ -399,6 +404,8 @@ static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
>>>
>>> return 0;
>>>
>>> +err_remove_file_features:
>>> + device_remove_file(gdev->dev, &dev_attr_host_features);
>> Please make a patch series that is linked together, the one you just did
>> was not properly threaded at all (hint, use git send-email).
>>
>> And again, this is not the correct way to solve this issue, use a
>> default attribute group for the driver instead. No driver should ever
>> be adding or removing files individually like this.
>
> Need I use the group to replace the individually removal in
> vbg_pci_remove() too?
The idea is to do a change similar to this one:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/commit/?id=4b0133533e82af25d6aaf9d0c7b2a5af388feb74
Setting the driver's .dev_groups field will automatically
add the attributes when probe() succeeds and they will
also automatically be removed before the driver's remove()
function gets called.
So after this change you can remove both the manual add
calls from probe() as well as remove the remove calls
from vbg_pci_remove().
Regards,
Hans
On Thu, Sep 01, 2022 at 05:39:46PM +0800, Greg KH wrote:
>> - device_create_file(dev, &dev_attr_host_version);
>> - device_create_file(dev, &dev_attr_host_features);
>> +
>> + ret = device_create_file(gdev->dev, &dev_attr_host_features);
>> + if (ret)
>> + goto err_unregister_misc_device_user;
>> + ret = device_create_file(gdev->dev, &dev_attr_host_version);
>> + if (ret)
>> + goto err_remove_file_features;
>>
>> vbg_info("vboxguest: misc device minor %d, IRQ %d, I/O port %x, MMIO at %pap (size %pap)\n",
>> gdev->misc_device.minor, pci->irq, gdev->io_port,
>> @@ -399,6 +404,8 @@ static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
>>
>> return 0;
>>
>> +err_remove_file_features:
>> + device_remove_file(gdev->dev, &dev_attr_host_features);
> Please make a patch series that is linked together, the one you just did
> was not properly threaded at all (hint, use git send-email).
>
> And again, this is not the correct way to solve this issue, use a
> default attribute group for the driver instead. No driver should ever
> be adding or removing files individually like this.
Need I use the group to replace the individually removal in
vbg_pci_remove() too?
Thanks,
Jiang