Skip to contentSkip to conversation

Replace current->mm by kvm->mm on powerpc/kvm

PATCH Superseded Leonardo Bras 2 participants 9 messages Sep 23, 2019 – Oct 22, 2019

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Replace current->mm by kvm->mm on powerpc/kvm

By replacing, we would reduce the use of 'global' current on code, relying more in the contents of kvm struct.

On code, I found that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have tests like that:
if (kvm->mm != current->mm)

        return -EIO;

So this change would be safe.

I split the changes in 3 patches, so it would be easier to read and reject separated parts. If decided that squashing is better, I see no problem doing that.

Best regards,

Leonardo Bras (3):
powerpc/kvm/book3s: Replace current->mm by kvm->mm powerpc/kvm/book3e: Replace current->mm by kvm->mm powerpc/kvm/e500: Replace current->mm by kvm->mm

 arch/powerpc/kvm/book3s_64_mmu_hv.c |  4 ++--
 arch/powerpc/kvm/book3s_64_vio.c    |  6 +++---
 arch/powerpc/kvm/book3s_hv.c        | 10 +++++-----
 arch/powerpc/kvm/booke.c            |  2 +-
 arch/powerpc/kvm/e500_mmu_host.c    |  6 +++---
 5 files changed, 14 insertions(+), 14 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/3 powerpc/kvm/book3s: Replace current->mm by kvm->mm

Given that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have:
if (kvm->mm != current->mm)

	return -EIO;

I see no reason to keep using current->mm instead of kvm->mm.

By doing so, we would reduce the use of 'global' variables on code, relying more in the contents of kvm struct.

---
 arch/powerpc/kvm/book3s_64_mmu_hv.c |  4 ++--
 arch/powerpc/kvm/book3s_64_vio.c    |  6 +++---
 arch/powerpc/kvm/book3s_hv.c        | 10 +++++-----
 3 files changed, 10 insertions(+), 10 deletions(-)
arch/powerpc/kvm/book3s_64_mmu_hv.c +2 -2
index f2b9aea43216..2f23cbc6c23e 100644
@@ -296,7 +296,7 @@ static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
 	/* Protect linux PTE lookup from page table destruction */
 	rcu_read_lock_sched();	/* this disables preemption too */
 	ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
-				current->mm->pgd, false, pte_idx_ret);
+				kvm->mm->pgd, false, pte_idx_ret);
 	rcu_read_unlock_sched();
 	if (ret = H_TOO_HARD) {
 		/* this can't happen */
@@ -585,7 +585,7 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
 	is_ci = false;
 	pfn = 0;
 	page = NULL;
-	mm = current->mm;
+	mm = kvm->mm;
 	pte_size = PAGE_SIZE;
 	writing = (dsisr & DSISR_ISSTORE) != 0;
 	/* If writing != 0, then the HPTE must allow writing, if we get here */
arch/powerpc/kvm/book3s_64_vio.c +3 -3
index c4b606fe73eb..8069b35f2905 100644
@@ -255,7 +255,7 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
 
 	kvm_put_kvm(stt->kvm);
 
-	account_locked_vm(current->mm,
+	account_locked_vm(kvm->mm,
 		kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);
 	call_rcu(&stt->rcu, release_spapr_tce_table);
 
@@ -280,7 +280,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
 		return -EINVAL;
 
 	npages = kvmppc_tce_pages(size);
-	ret = account_locked_vm(current->mm, kvmppc_stt_pages(npages), true);
+	ret = account_locked_vm(kvm->mm, kvmppc_stt_pages(npages), true);
 	if (ret)
 		return ret;
 
@@ -326,7 +326,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
 
 	kfree(stt);
  fail_acct:
-	account_locked_vm(current->mm, kvmppc_stt_pages(npages), false);
+	account_locked_vm(kvm->mm, kvmppc_stt_pages(npages), false);
 	return ret;
 }
 
arch/powerpc/kvm/book3s_hv.c +5 -5
index cde3f5a4b3e4..c5b6081b6de0 100644
@@ -4265,7 +4265,7 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 	user_vrsave = mfspr(SPRN_VRSAVE);
 
 	vcpu->arch.wqp = &vcpu->arch.vcore->wq;
-	vcpu->arch.pgdir = current->mm->pgd;
+	vcpu->arch.pgdir = kvm->mm->pgd;
 	vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
 
 	do {
@@ -4597,14 +4597,14 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
 
 	/* Look up the VMA for the start of this memory slot */
 	hva = memslot->userspace_addr;
-	down_read(&current->mm->mmap_sem);
-	vma = find_vma(current->mm, hva);
+	down_read(&kvm->mm->mmap_sem);
+	vma = find_vma(kvm->mm, hva);
 	if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
 		goto up_out;
 
 	psize = vma_kernel_pagesize(vma);
 
-	up_read(&current->mm->mmap_sem);
+	up_read(&kvm->mm->mmap_sem);
 
 	/* We can handle 4k, 64k or 16M pages in the VRMA */
 	if (psize >= 0x1000000)
@@ -4637,7 +4637,7 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
 	return err;
 
  up_out:
-	up_read(&current->mm->mmap_sem);
+	up_read(&kvm->mm->mmap_sem);
 	goto out_srcu;
 }
 
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH 1/3] powerpc/kvm/book3s: Replace current->mm by kvm->mm

Quoted 11 lines · Leonardo Bras

Given that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have:
if (kvm->mm != current->mm)

	return -EIO;

I see no reason to keep using current->mm instead of kvm->mm.

By doing so, we would reduce the use of 'global' variables on code, relying more in the contents of kvm struct.

This patch led to a crash on shutting down a VM, because of this hunk:

arch/powerpc/kvm/book3s_64_vio.c static int kvm_spapr_tce_release(struct inode *inode, struct file *filp) · line 259

Quoted 11 lines
arch/powerpc/kvm/book3s_64_vio.c +1 -1
index c4b606fe73eb..8069b35f2905 100644
@@ -255,7 +255,7 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
 
 	kvm_put_kvm(stt->kvm);
 
-	account_locked_vm(current->mm,
+	account_locked_vm(kvm->mm,
 		kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);

You are referencing kvm->mm after having done kvm_put_kvm a couple of lines earlier, which means that *kvm can be freed at the point where you use kvm->mm. If you want to make this change you will need to move the kvm_put_kvm call to after the last use of it.

I have dropped this patch for now.

Paul.

#3 lore raw

PATCH 2/3 powerpc/kvm/book3e: Replace current->mm by kvm->mm

In reply to Leonardo Bras

Given that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have:
if (kvm->mm != current->mm)

	return -EIO;

I see no reason to keep using current->mm instead of kvm->mm.

By doing so, we would reduce the use of 'global' variables on code, relying more in the contents of kvm struct.

---
 arch/powerpc/kvm/booke.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/booke.c +1 -1
index be9a45874194..383108263af5 100644
@@ -775,7 +775,7 @@ int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 	debug = current->thread.debug;
 	current->thread.debug = vcpu->arch.dbg_reg;
 
-	vcpu->arch.pgdir = current->mm->pgd;
+	vcpu->arch.pgdir = kvm->mm->pgd;
 	kvmppc_fix_ee_before_entry();
 
 	ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Signature · 1 line
2.20.1
#4 lore raw

Re: [PATCH 2/3] powerpc/kvm/book3e: Replace current->mm by kvm->mm

Quoted 22 lines · Leonardo Bras

Given that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have:
if (kvm->mm != current->mm)

	return -EIO;

I see no reason to keep using current->mm instead of kvm->mm.

By doing so, we would reduce the use of 'global' variables on code, relying more in the contents of kvm struct.

---
 arch/powerpc/kvm/booke.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/booke.c +0 -0
index be9a45874194..383108263af5 100644

arch/powerpc/kvm/booke.c int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) · line 781

Quoted 8 lines
arch/powerpc/kvm/booke.c +1 -1
@@ -775,7 +775,7 @@ int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 	debug = current->thread.debug;
 	current->thread.debug = vcpu->arch.dbg_reg;
 
-	vcpu->arch.pgdir = current->mm->pgd;
+	vcpu->arch.pgdir = kvm->mm->pgd;
 	kvmppc_fix_ee_before_entry();
 
 	ret = __kvmppc_vcpu_run(kvm_run, vcpu);

With this patch, I get compile errors for Book E configs:

CC arch/powerpc/kvm/booke.o
/home/paulus/kernel/kvm/arch/powerpc/kvm/booke.c: In function ‘kvmppc_vcpu_run’:
/home/paulus/kernel/kvm/arch/powerpc/kvm/booke.c:778:21: error: ‘kvm’ undeclared (first use in this function)

  vcpu->arch.pgdir = kvm->mm->pgd;
                     ^

/home/paulus/kernel/kvm/arch/powerpc/kvm/booke.c:778:21: note: each undeclared identifier is reported only once for each function it appears in make[3]: *** [/home/paulus/kernel/kvm/scripts/Makefile.build:266: arch/powerpc/kvm/booke.o] Error 1
make[2]: *** [/home/paulus/kernel/kvm/scripts/Makefile.build:509: arch/powerpc/kvm] Error 2
make[1]: *** [/home/paulus/kernel/kvm/Makefile:1649: arch/powerpc] Error 2
make: *** [/home/paulus/kernel/kvm/Makefile:179: sub-make] Error 2

It seems that you didn't compile-test this patch.

Paul.

#5 lore raw

Re: [PATCH 2/3] powerpc/kvm/book3e: Replace current->mm by kvm->mm

Quoted 45 lines · Paul Mackerras
Quoted 22 lines · Leonardo Bras

Given that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have:
if (kvm->mm != current->mm)

	return -EIO;

I see no reason to keep using current->mm instead of kvm->mm.

By doing so, we would reduce the use of 'global' variables on code, relying more in the contents of kvm struct.

---
 arch/powerpc/kvm/booke.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/booke.c +0 -0
index be9a45874194..383108263af5 100644

arch/powerpc/kvm/booke.c int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) · line 781

Quoted 8 lines
arch/powerpc/kvm/booke.c +1 -1
@@ -775,7 +775,7 @@ int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 	debug = current->thread.debug;
 	current->thread.debug = vcpu->arch.dbg_reg;
 
-	vcpu->arch.pgdir = current->mm->pgd;
+	vcpu->arch.pgdir = kvm->mm->pgd;
 	kvmppc_fix_ee_before_entry();
 
 	ret = __kvmppc_vcpu_run(kvm_run, vcpu);

With this patch, I get compile errors for Book E configs:

CC arch/powerpc/kvm/booke.o
/home/paulus/kernel/kvm/arch/powerpc/kvm/booke.c: In function ‘kvmppc_vcpu_run’:
/home/paulus/kernel/kvm/arch/powerpc/kvm/booke.c:778:21: error: ‘kvm’ undeclared (first use in this function)

  vcpu->arch.pgdir = kvm->mm->pgd;
                     ^

/home/paulus/kernel/kvm/arch/powerpc/kvm/booke.c:778:21: note: each undeclared identifier is reported only once for each function it appears in make[3]: *** [/home/paulus/kernel/kvm/scripts/Makefile.build:266: arch/powerpc/kvm/booke.o] Error 1
make[2]: *** [/home/paulus/kernel/kvm/scripts/Makefile.build:509: arch/powerpc/kvm] Error 2
make[1]: *** [/home/paulus/kernel/kvm/Makefile:1649: arch/powerpc] Error 2
make: *** [/home/paulus/kernel/kvm/Makefile:179: sub-make] Error 2

Quoted 3 lines

It seems that you didn't compile-test this patch.

Paul.

Sorry Paul,
I remember dealing with this before, but it looks like it was lost during git context/branch switching.

I will make sure that doesn't happen again.
A v2 will come soon, I will test it on travis.

Best regards,
Leonardo Bras

#6 lore raw

PATCH 3/3 powerpc/kvm/e500: Replace current->mm by kvm->mm

In reply to Leonardo Bras

Given that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have:
if (kvm->mm != current->mm)

        return -EIO;

I see no reason to keep using current->mm instead of kvm->mm.

By doing so, we would reduce the use of 'global' variables on code, relying more in the contents of kvm struct.

---
 arch/powerpc/kvm/e500_mmu_host.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/powerpc/kvm/e500_mmu_host.c +3 -3
index 321db0fdb9db..425d13806645 100644
@@ -355,9 +355,9 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 
 	if (tlbsel = 1) {
 		struct vm_area_struct *vma;
-		down_read(&current->mm->mmap_sem);
+		down_read(&kvm->mm->mmap_sem);
 
-		vma = find_vma(current->mm, hva);
+		vma = find_vma(kvm->mm, hva);
 		if (vma && hva >= vma->vm_start &&
 		    (vma->vm_flags & VM_PFNMAP)) {
 			/*
@@ -441,7 +441,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 			tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
 		}
 
-		up_read(&current->mm->mmap_sem);
+		up_read(&kvm->mm->mmap_sem);
 	}
 
 	if (likely(!pfnmap)) {
Signature · 1 line
2.20.1
#7 lore raw
In reply to Leonardo Bras
Quoted 8 lines · Leonardo Bras

By replacing, we would reduce the use of 'global' current on code, relying more in the contents of kvm struct.

On code, I found that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have tests like that:
if (kvm->mm != current->mm)

Quoted 7 lines
        return -EIO;

So this change would be safe.

I split the changes in 3 patches, so it would be easier to read and reject separated parts. If decided that squashing is better, I see no problem doing that.

The patch series looks fine. It has missed the 5.4 merge window, and it doesn't fix any bugs, so I will queue it up for the 5.5 merge window, meaning that I will put it into my kvm-ppc-next branch when I prepare it for the 5.5 merge window, probably in about a month from now.

This remark also applies to your other patch "Reduce calls to get current->mm by storing the value locally".

Thanks,
Paul.

#8 lore raw
Quoted 23 lines · Paul Mackerras
Quoted 8 lines · Leonardo Bras

By replacing, we would reduce the use of 'global' current on code, relying more in the contents of kvm struct.

On code, I found that in kvm_create_vm() there is:
kvm->mm = current->mm;

And that on every kvm_*_ioctl we have tests like that:
if (kvm->mm != current->mm)

Quoted 7 lines
        return -EIO;

So this change would be safe.

I split the changes in 3 patches, so it would be easier to read and reject separated parts. If decided that squashing is better, I see no problem doing that.

The patch series looks fine. It has missed the 5.4 merge window, and it doesn't fix any bugs, so I will queue it up for the 5.5 merge window, meaning that I will put it into my kvm-ppc-next branch when I prepare it for the 5.5 merge window, probably in about a month from now.

Quoted 5 lines

This remark also applies to your other patch "Reduce calls to get current->mm by storing the value locally".

Thanks,
Paul.

Thanks!
Leonardo Bras