vfio: type1: fix kthread use case
Subsystems (2)
RFC PATCH 1/1 vfio: type1: fix kthread use case
It's incorrect to tell out if a task is kthread without checking PF_KTHREAD at all.
What's also fixed, if no need to be in a seperate patch, is to invoke kthread_use_mm() without checking the current mm first as the kthread may hold a mm struct atm and it's not the right place to switch mm.
---
drivers/vfio/vfio_iommu_type1.c +4 -4
@@ -2798,7 +2798,8 @@ static int vfio_iommu_type1_dma_rw_chunkstruct mm_struct *mm;unsigned long vaddr;struct vfio_dma *dma; -bool kthread = current->mm ==NULL ; +bool kthread = current->flags &PF_KTHREAD ; +bool use_mm = current->mm ==NULL ;size_t offset; *copied =0 ;
@@ -2812,11 +2813,10 @@ static int vfio_iommu_type1_dma_rw_chunkreturn -EPERM ; mm =get_task_mm (dma->task); -if (!mm)return -EPERM ; -if (kthread) +if (kthread && use_mm)kthread_use_mm (mm);else if (current->mm != mm) goto out;
@@ -2843,7 +2843,7 @@ static int vfio_iommu_type1_dma_rw_chunk }else *copied =copy_from_user (data, (void __user *)vaddr, count) ?0 : count; -if (kthread) +if (kthread && use_mm)kthread_unuse_mm (mm); out:mmput (mm);
--
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
…
Quoted 2 lines
+++
b/drivers/vfio/vfio_iommu_type1.c(quoted diff) +0
-0@@ -2798,7 +2798,8 @@ static int vfio_iommu_type1_dma_rw_chunk
…
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2802
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +2
-1-bool kthread = current->mm ==NULL ; +bool kthread = current->flags &PF_KTHREAD ; +bool use_mm = current->mm ==NULL ;
…
Can it be helpful to convert initialisations for these variables into later assignments?
Regards,
Markus
Quoted 11 lines · On Mon, 6 Jul 2020 13:32:18 +0200 Markus Elfring wrote:
=E2=80=A6
Review static int vfio_iommu_type1_dma_rw_chunk
Quoted 2 lines
+++
b/drivers/vfio/vfio_iommu_type1.c(quoted diff) +0
-0@@ -2798,7 +2798,8 @@ static int vfio_iommu_type1_dma_rw_chunk=E2=80=A6
Review static int vfio_iommu_type1_dma_rw_chunk · line 2799
Quoted 3 lines
(quoted diff) +2
-1- bool kthread =3D current->mm =3D=3D NULL; + bool kthread =3D current->flags & PF_KTHREAD; + bool use_mm =3D current->mm =3D=3D NULL;=E2=80=A6
Can it be helpful to convert initialisations for these variables into later assignments?
Perhaps. Then it looks like the below.
drivers/vfio/vfio_iommu_type1.c +6 -4
@@ -2798,9 +2798,12 @@ static int vfio_iommu_type1_dma_rw_chunkstruct mm_struct *mm;unsigned long vaddr;struct vfio_dma *dma; -bool kthread = current->mm ==NULL ; +bool kthread; +bool use_mm;size_t offset; + kthread = current->flags &PF_KTHREAD ; + use_mm = current->mm ==NULL ; *copied =0 ; dma =vfio_find_dma (iommu, user_iova,1 );
@@ -2812,11 +2815,10 @@ static int vfio_iommu_type1_dma_rw_chunkreturn -EPERM ; mm =get_task_mm (dma->task); -if (!mm)return -EPERM ; -if (kthread) +if (kthread && use_mm)kthread_use_mm (mm);else if (current->mm != mm) goto out;
@@ -2843,7 +2845,7 @@ static int vfio_iommu_type1_dma_rw_chunk }else *copied =copy_from_user (data, (void __user *)vaddr, count) ?0 : count; -if (kthread) +if (kthread && use_mm)kthread_unuse_mm (mm); out:mmput (mm);
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Quoted 4 lines
Quoted 2 lines
Can it be helpful to convert initialisations for these variables into later assignments?
Perhaps. Then it looks like the below.
…
Quoted 2 lines
+++
b/drivers/vfio/vfio_iommu_type1.c(quoted diff) +0
-0@@ -2798,9 +2798,12 @@ static int vfio_iommu_type1_dma_rw_chunk
…
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2802
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +2
-1-bool kthread = current->mm ==NULL ; +bool kthread; +bool use_mm;
I would prefer the following variable declarations then.
+ bool kthread, use_mm;
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2806
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +2
-0size_t offset; + kthread = current->flags &PF_KTHREAD ; + use_mm = current->mm ==NULL ;
I propose to move such assignments directly before the corresponding check.
…
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2819
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +0
-0if (!mm)return -EPERM ;
+ kthread = current->flags & PF_KTHREAD;
+ use_mm = !current->mm;
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2822
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +1
-1-if (kthread) +if (kthread && use_mm)kthread_use_mm (mm);
…
Regards,
Markus
Quoted 6 lines · On Mon, 6 Jul 2020 15:33:58 +0200 Markus Elfring wrote:
I would prefer the following variable declarations then.
(quoted diff) +1
-0+ bool kthread, use_mm;
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +2
-0size_t offset; + kthread =3D current->flags & PF_KTHREAD; + use_mm =3D current->mm =3D=3D NULL;I propose to move such assignments directly before the corresponding check= .
Got it. See below.
Quoted 11 lines
Quoted 3 lines
(quoted diff) +0
-0if (!mm) return -EPERM;(quoted diff) +2
-0+ kthread =3D current->flags & PF_KTHREAD; + use_mm =3D !current->mm;Review
Quoted 4 lines
(quoted diff) +1
-1- if (kthread) + if (kthread && use_mm) kthread_use_mm(mm);
drivers/vfio/vfio_iommu_type1.c +6 -4
@@ -2798,7 +2798,7 @@ static int vfio_iommu_type1_dma_rw_chunkstruct mm_struct *mm;unsigned long vaddr;struct vfio_dma *dma; -bool kthread = current->mm ==NULL ; +bool kthread, use_mm;size_t offset; *copied =0 ;
@@ -2812,11 +2812,13 @@ static int vfio_iommu_type1_dma_rw_chunkreturn -EPERM ; mm =get_task_mm (dma->task); -if (!mm)return -EPERM ; -if (kthread) + kthread = current->flags &PF_KTHREAD ; + use_mm = current->mm ==NULL ; + +if (kthread && use_mm)kthread_use_mm (mm);else if (current->mm != mm) goto out;
@@ -2843,7 +2845,7 @@ static int vfio_iommu_type1_dma_rw_chunk }else *copied =copy_from_user (data, (void __user *)vaddr, count) ?0 : count; -if (kthread) +if (kthread && use_mm)kthread_unuse_mm (mm); out:mmput (mm);
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
…
Quoted 1 line
+++
b/drivers/vfio/vfio_iommu_type1.c
…
Quoted 1 line
(quoted diff) +0
-0@@ -2812,11 +2815,10 @@ static int vfio_iommu_type1_dma_rw_chunk
…
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2821
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +1
-1if (!mm)return -EPERM ; -if (kthread) +if (kthread && use_mm)
Can another design approach make sense here?
+ bool thread_use_mm = ((current->flags & PF_KTHREAD) && !current->mm);
+ if (thread_use_mm)
Quoted 1 line
kthread_use_mm(mm);
…
Regards,
Markus
Review
Quoted 5 lines · On Mon, 6 Jul 2020 16:04:13 +0200 Markus Elfring wrote:
Can another design approach make sense here?
(quoted diff) +2
-0+ bool thread_use_mm = ((current->flags & PF_KTHREAD) && !current->mm); + if (thread_use_mm)
Good. See below.
Quoted 3 lines
Quoted 3 lines
kthread_use_mm(mm);
drivers/vfio/vfio_iommu_type1.c +5 -4
@@ -2798,7 +2798,7 @@ static int vfio_iommu_type1_dma_rw_chunkstruct mm_struct *mm;unsigned long vaddr;struct vfio_dma *dma; -bool kthread = current->mm ==NULL ; +bool kthread_load_mm;size_t offset; *copied =0 ;
@@ -2812,11 +2812,12 @@ static int vfio_iommu_type1_dma_rw_chunkreturn -EPERM ; mm =get_task_mm (dma->task); -if (!mm)return -EPERM ; -if (kthread) + kthread_load_mm = current->flags &PF_KTHREAD && + current->mm ==NULL ; +if (kthread_load_mm)kthread_use_mm (mm);else if (current->mm != mm) goto out;
@@ -2843,7 +2844,7 @@ static int vfio_iommu_type1_dma_rw_chunk }else *copied =copy_from_user (data, (void __user *)vaddr, count) ?0 : count; -if (kthread) +if (kthread_load_mm)kthread_unuse_mm (mm); out:mmput (mm);
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
…
Quoted 2 lines
+++
b/drivers/vfio/vfio_iommu_type1.c(quoted diff) +0
-0@@ -2798,7 +2798,7 @@ static int vfio_iommu_type1_dma_rw_chunk
…
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2802
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +1
-1-bool kthread = current->mm ==NULL ; +bool kthread_load_mm;size_t offset;
How do you think about to reduce the scope for such variables?
https://refactoring.com/catalog/reduceScopeOfVariable.html
…
Quoted 1 line
(quoted diff) +0
-0@@ -2812,11 +2812,12 @@ static int vfio_iommu_type1_dma_rw_chunk
…
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +0
-0if (!mm)return -EPERM ;
…
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2819
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+ kthread_load_mm = current->flags &PF_KTHREAD && + current->mm ==NULL ;
…
Would you like to apply a more succinct code variant?
+ kthread_load_mm = current->flags & PF_KTHREAD && !current->mm;
Regards,
Markus
Quoted 19 lines · Hillf Danton
It's incorrect to tell out if a task is kthread without checking
PF_KTHREADat all.What's also fixed, if no need to be in a seperate patch, is to invoke
kthread_use_mm()without checking the current mm first as the kthread may hold a mm struct atm and it's not the right place to switch mm.Fixes 8d46c0cca5f4 ("vfio: introduce vfio_dma_rw to read/write a range of IOVAs")Cc Yan Zhao <[email protected]>Cc Markus Elfring <[email protected]>Cc Christoph Hellwig <[email protected]>Signed-off-by Hillf Danton <[email protected]>---
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw_chunk · line 2802
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +2
-1@@ -2798,7 +2798,8 @@ static int vfio_iommu_type1_dma_rw_chunkstruct mm_struct *mm;unsigned long vaddr;struct vfio_dma *dma; -bool kthread = current->mm ==NULL ; +bool kthread = current->flags &PF_KTHREAD ; +bool use_mm = current->mm ==NULL ;
is it acceptable to just rename "kthread" to "kthread_no_use_mm"?
I think "current->mm == NULL" in itself implies kthread and not use_mm, as a user thread is not able to have "current->mm == NULL", right?
Thanks
Yan
Quoted 27 lines
drivers/vfio/vfio_iommu_type1.c +2
-4size_t offset; *copied =0 ;@@ -2812,11 +2813,10 @@ static int vfio_iommu_type1_dma_rw_chunkreturn -EPERM ; mm =get_task_mm (dma->task); -if (!mm)return -EPERM ; -if (kthread) +if (kthread && use_mm)kthread_use_mm (mm);else if (current->mm != mm) goto out;@@ -2843,7 +2843,7 @@ static int vfio_iommu_type1_dma_rw_chunk }else *copied =copy_from_user (data, (void __user *)vaddr, count) ?0 : count; -if (kthread) +if (kthread && use_mm)kthread_unuse_mm (mm); out:mmput (mm); --
Quoted 3 lines · On Tue, 7 Jul 2020 08:31:41 +0800 Yan Zhao wrote:
I think "
current->mm== NULL" in itself implies kthread and notuse_mm, as a user thread is not able to have "current->mm== NULL", right?
Nope, it's the fix target as checking the current mm does not make sense without current being kthread.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu