Skip to contentSkip to conversation

vdpa/mlx5: Remove debugfs file after device unregister

PATCH Jason Wang 1 participant 4 messages Mar 17, 2023 – Mar 22, 2023 4 messages not in the archive virtio core

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Jason Wang

author
#1 lore raw
Quoted 27 lines · Eli Cohen

When deleting the vdpa device, the debugfs files need to be removed so need to remove debugfs after the device has been unregistered.

This fixes null pointer dereference when someone deletes the device after debugfs has been populated.

Fixes 294221004322 ("vdpa/mlx5: Add debugfs subtree")
---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -2
index 3858ba1e8975..3f6149f2ffd4 100644
@@ -3322,8 +3322,6 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
        struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
        struct workqueue_struct *wq;
 
-       mlx5_vdpa_remove_debugfs(ndev->debugfs);
-       ndev->debugfs = NULL;
        if (ndev->nb_registered) {
                ndev->nb_registered = false;
                mlx5_notifier_unregister(mvdev->mdev, &ndev->nb);

drivers/vdpa/mlx5/net/mlx5_vnet.c static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device * · line 3332

Quoted 3 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -0
@@ -3332,6 +3330,8 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
        mvdev->wq = NULL;
        destroy_workqueue(wq);
        _vdpa_unregister_device(dev);

What if the user tries to access debugfs after _vdpa_unregister_device()?

Thanks

Quoted 8 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +2 -1
+       mlx5_vdpa_remove_debugfs(ndev->debugfs);
+       ndev->debugfs = NULL;
        mgtdev->ndev = NULL;
 }
 
--

2.38.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#3 lore raw
Quoted 33 lines · Eli Cohen
Quoted 21 lines · Jason Wang
Quoted 20 lines · Eli Cohen

When deleting the vdpa device, the debugfs files need to be removed so need to remove debugfs after the device has been unregistered.

This fixes null pointer dereference when someone deletes the device after debugfs has been populated.

Fixes 294221004322 ("vdpa/mlx5: Add debugfs subtree")
---
  drivers/vdpa/mlx5/net/mlx5_vnet.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -0
index 3858ba1e8975..3f6149f2ffd4 100644
@@ -3322,8 +3322,6 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
         struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
         struct workqueue_struct *wq;
 
Quoted 10 lines

drivers/vdpa/mlx5/net/mlx5_vnet.c static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device * · line 3332

Quoted 9 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -2
-       mlx5_vdpa_remove_debugfs(ndev->debugfs);
-       ndev->debugfs = NULL;
         if (ndev->nb_registered) {
                 ndev->nb_registered = false;
                 mlx5_notifier_unregister(mvdev->mdev, &ndev->nb);
@@ -3332,6 +3330,8 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
         mvdev->wq = NULL;
         destroy_workqueue(wq);
         _vdpa_unregister_device(dev);

What if the user tries to access debugfs after _vdpa_unregister_device()?

Quoted 3 lines

I don't see an issue with that. During the process of deleting a device, the resources are destroyed and their corresponding debugfs files are also destroyed just prior to destroying the resource.

For example, rx_flow_table_show() requires the structure mlx5_vdpa_net alive, but it seems the structure has been freed after _vdpa_unregister_device().

If a user tries to access that file after _vdpa_unregister_device() but before mlx5_vdpa_remove_debugfs(), will we end up with use-after-free?

Thanks

Quoted 12 lines
Quoted 11 lines

Thanks

Quoted 8 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +2 -1
+       mlx5_vdpa_remove_debugfs(ndev->debugfs);
+       ndev->debugfs = NULL;
         mgtdev->ndev = NULL;
  }
 
--

2.38.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#5 lore raw
Quoted 39 lines · Eli Cohen
Quoted 36 lines · Jason Wang
Quoted 35 lines · Eli Cohen
Quoted 21 lines · Jason Wang
Quoted 20 lines · Eli Cohen

When deleting the vdpa device, the debugfs files need to be removed so need to remove debugfs after the device has been unregistered.

This fixes null pointer dereference when someone deletes the device after debugfs has been populated.

Fixes 294221004322 ("vdpa/mlx5: Add debugfs subtree")
---
   drivers/vdpa/mlx5/net/mlx5_vnet.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -0
index 3858ba1e8975..3f6149f2ffd4 100644
@@ -3322,8 +3322,6 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
          struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
          struct workqueue_struct *wq;
 
Quoted 10 lines

drivers/vdpa/mlx5/net/mlx5_vnet.c static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device * · line 3332

Quoted 9 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -2
-       mlx5_vdpa_remove_debugfs(ndev->debugfs);
-       ndev->debugfs = NULL;
          if (ndev->nb_registered) {
                  ndev->nb_registered = false;
                  mlx5_notifier_unregister(mvdev->mdev, &ndev->nb);
@@ -3332,6 +3330,8 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
          mvdev->wq = NULL;
          destroy_workqueue(wq);
          _vdpa_unregister_device(dev);

What if the user tries to access debugfs after _vdpa_unregister_device()?

I don't see an issue with that. During the process of deleting a device, the resources are destroyed and their corresponding debugfs files are also destroyed just prior to destroying the resource.

Quoted 7 lines
Quoted 3 lines

For example, rx_flow_table_show() requires the structure mlx5_vdpa_net alive, but it seems the structure has been freed after _vdpa_unregister_device().

But in this case, rx_flow_table_show() gets called first, so the file will be removed from the debugfs before

mlx5_vdpa_net gets released.

Just to make sure we are at the same page. I meant:

[CPU 0] _vdpa_unregister_device(dev);
[CPU 1] rx_flow_table_show()
[CPU 0] mlx5_vdpa_remove_debugfs()

So when CPU 1 tries to access the flow table, the ndev has been released by CPU0 in _vdpa_unregister_device()?

Thanks

Quoted 11 lines
Quoted 11 lines

If a user tries to access that file after _vdpa_unregister_device() but before mlx5_vdpa_remove_debugfs(), will we end up with use-after-free?

Thanks

Quoted 2 lines
Quoted 2 lines

Thanks

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +2 -1
+       mlx5_vdpa_remove_debugfs(ndev->debugfs);
+       ndev->debugfs = NULL;
          mgtdev->ndev = NULL;
   }
 
--

2.38.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

A message that is not in the archive (sent off-list or not yet received): DM8PR12MB540045D5103137CD61BF8632AB819@DM8PR12MB5400.namprd12.prod.outlook.com

author
#7 lore raw
Quoted 61 lines · Eli Cohen
Quoted 61 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, 21 March 2023 5:23
To: Eli Cohen <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Parav Pandit <[email protected]>
Subject: Re: [PATCH] vdpa/mlx5: Remove debugfs file after device unregister

Quoted 7 lines · Eli Cohen
Quoted 4 lines · Jason Wang
Quoted 3 lines · Eli Cohen
Quoted 2 lines · Jason Wang
Quoted 1 line · Eli Cohen

When deleting the vdpa device, the debugfs files need to be removed

so

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

need to remove debugfs after the device has been unregistered.

This fixes null pointer dereference when someone deletes the device after debugfs has been populated.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 7 lines
Fixes 294221004322 ("vdpa/mlx5: Add debugfs subtree")
---
   drivers/vdpa/mlx5/net/mlx5_vnet.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/vdpa/mlx5/net/mlx5_vnet.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/vdpa/mlx5/net/mlx5_vnet.c static void mlx5_vdpa_dev_del(struct

Quoted 4 lines

index 3858ba1e8975..3f6149f2ffd4 100644

drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -0
@@ -3322,8 +3322,6 @@ static void mlx5_vdpa_dev_del(struct

vdpa_mgmt_dev *v_mdev, struct vdpa_device *

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/vdpa/mlx5/net/mlx5_vnet.c static void mlx5_vdpa_dev_del(struct

Quoted 9 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -2
          struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
          struct workqueue_struct *wq;
 
-       mlx5_vdpa_remove_debugfs(ndev->debugfs);
-       ndev->debugfs = NULL;
          if (ndev->nb_registered) {
                  ndev->nb_registered = false;
                  mlx5_notifier_unregister(mvdev->mdev, &ndev->nb);
@@ -3332,6 +3330,8 @@ static void mlx5_vdpa_dev_del(struct

vdpa_mgmt_dev *v_mdev, struct vdpa_device *

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/vdpa/mlx5/net/mlx5_vnet.c static void mlx5_vdpa_dev_del(struct · line 3332

Quoted 3 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -0
          mvdev->wq = NULL;
          destroy_workqueue(wq);
          _vdpa_unregister_device(dev);

What if the user tries to access debugfs after

_vdpa_unregister_device()?

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

I don't see an issue with that. During the process of deleting a device, the resources are destroyed and their corresponding debugfs files are also destroyed just prior to destroying the resource.

For example, rx_flow_table_show() requires the structure mlx5_vdpa_net alive, but it seems the structure has been freed after _vdpa_unregister_device().

But in this case, rx_flow_table_show() gets called first, so the file will be removed from the debugfs before

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

mlx5_vdpa_net gets released.

Just to make sure we are at the same page. I meant:

[CPU 0] _vdpa_unregister_device(dev);

If the vdpa is still in use, e.g a VM is using it, this call will hang.

Ture since it will wait for the VM to release the fd. But this is not the case when the device is bound to the virtio-vdpa driver. In this case, do we need to synchronize here?

Quoted 2 lines

For this call to conclude, the device needs to be reset. During the reset process, all the resources are destroyed, including (for example) the TIR. Just before destroying the TIR, the debugfs entry is removed by calling mlx5_vdpa_remove_rx_flow_table(). So at the time _vdpa_unregister_device() is completed, the debugfs file is not there anymore.

Ok, so in this case we probably don't need a second call to mlx5_vdpa_remove_debugfs()?

Thanks

Quoted 25 lines
Quoted 25 lines

[CPU 1] rx_flow_table_show()
[CPU 0] mlx5_vdpa_remove_debugfs()

So when CPU 1 tries to access the flow table, the ndev has been released by CPU0 in _vdpa_unregister_device()?

Thanks

Quoted 16 lines
Quoted 16 lines

If a user tries to access that file after _vdpa_unregister_device() but before mlx5_vdpa_remove_debugfs(), will we end up with use-after-free?

Thanks

Quoted 7 lines
Quoted 7 lines

Thanks

Quoted 5 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +2 -0
+       mlx5_vdpa_remove_debugfs(ndev->debugfs);
+       ndev->debugfs = NULL;
          mgtdev->ndev = NULL;
   }
 
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

--
2.38.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization