Skip to contentSkip to conversation

vfio: type1: fix kthread use case

RFC PATCH In review Hillf Danton 3 participants 10 messages Jul 6, 2020 – Jul 7, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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.

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]>

---

drivers/vfio/vfio_iommu_type1.c +4 -4
@@ -2798,7 +2798,8 @@ static int vfio_iommu_type1_dma_rw_chunk
 	struct 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_chunk
 		return -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

#1 lore raw

…

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

#2 lore raw
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_chunk
 	struct 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_chunk
 		return -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

#3 lore raw
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 -0
 	size_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 -0
 	if (!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

#4 lore raw
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 -0
 	size_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 -0
 	if (!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_chunk
 	struct 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_chunk
 		return -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

#5 lore raw
In reply to Hillf Danton

…

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 -1
 	if (!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

#6 lore raw

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_chunk
 	struct 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_chunk
 		return -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

#7 lore raw

…

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 -0
 	if (!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

#8 lore raw
In reply to Hillf Danton
Quoted 19 lines · Hillf Danton

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.

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]>

---

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_chunk
 	struct 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 -4
 	size_t offset;
 
 	*copied = 0;
@@ -2812,11 +2813,10 @@ static int vfio_iommu_type1_dma_rw_chunk
 		return -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);
--
#9 lore raw
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 not use_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