Subject: Re: [PATCH v2 03/18] KVM: PPC: Account TCE pages in locked_vm

From: Gavin Shan <[email-masked]>
Date: Wed, 23 Jul 2014 13:36:52 +1000

On Wed, Jul 23, 2014 at 01:05:49PM +1000, Alexey Kardashevskiy wrote:
>Signed-off-by: Alexey Kardashevskiy <[email-masked]>
>---
> arch/powerpc/kvm/book3s_64_vio.c | 35 ++++++++++++++++++++++++++++++++++-
> 1 file changed, 34 insertions(+), 1 deletion(-)
>
>diff --git a/arch/powerpc/kvm/book3s_64_vio.c b/arch/powerpc/kvm/book3s_64_vio.c
>index 516f2ee..48b7ed4 100644
>--- a/arch/powerpc/kvm/book3s_64_vio.c
>+++ b/arch/powerpc/kvm/book3s_64_vio.c
>@@ -45,18 +45,48 @@ static long kvmppc_stt_npages(unsigned long window_size)
> 		     * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
> }
>
>+/*
>+ * Checks ulimit in order not to let the user space to pin all
>+ * available memory for TCE tables.
>+ */
>+static long kvmppc_account_memlimit(long npages)
>+{
>+	unsigned long ret = 0, locked, lock_limit;
>+
>+	if (!current->mm)
>+		return -ESRCH; /* process exited */
>+
>+	down_write(&current->mm->mmap_sem);
>+	locked = current->mm->locked_vm + npages;
>+	lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
>+	if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {

I took a look on mm/mlock.c::mlock() where the mlock operation
is allowd while we have one of following conditions (not &&) :-)

        /* check against resource limits */
        if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
                error = do_mlock(start, len, 1);

Thanks,
Gavin

>+		pr_warn("RLIMIT_MEMLOCK (%ld) exceeded\n",
>+				rlimit(RLIMIT_MEMLOCK));
>+		ret = -ENOMEM;
>+	} else {
>+		current->mm->locked_vm += npages;
>+	}
>+	up_write(&current->mm->mmap_sem);
>+
>+	return ret;
>+}
>+
> static void release_spapr_tce_table(struct kvmppc_spapr_tce_table *stt)
> {
> 	struct kvm *kvm = stt->kvm;
> 	int i;
>+	long npages = kvmppc_stt_npages(stt->window_size);
>
> 	mutex_lock(&kvm->lock);
> 	list_del(&stt->list);
>-	for (i = 0; i < kvmppc_stt_npages(stt->window_size); i++)
>+	for (i = 0; i < npages; i++)
> 		__free_page(stt->pages[i]);
>+
> 	kfree(stt);
> 	mutex_unlock(&kvm->lock);
>
>+	kvmppc_account_memlimit(-(npages + 1));
>+
> 	kvm_put_kvm(kvm);
> }
>
>@@ -112,6 +142,9 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
> 	}
>
> 	npages = kvmppc_stt_npages(args->window_size);
>+	ret = kvmppc_account_memlimit(npages + 1);
>+	if (ret)
>+		goto fail;
>
> 	stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
> 		      GFP_KERNEL);
>-- 
>2.0.0
>