[5/5] kvm/book3s: use pinned_vm instead of locked_vm to account pinned pages

Message ID 20190211224437.25267-6-daniel.m.jordan@oracle.com
State Not Applicable
Headers show
Series
  • use pinned_vm instead of locked_vm to account pinned pages
Related show

Checks

Context Check Description
snowpatch_ozlabs/checkpatch fail total: 2 errors, 0 warnings, 1 checks, 54 lines checked
snowpatch_ozlabs/build-pmac32 success build succeeded & removed 0 sparse warning(s)
snowpatch_ozlabs/build-ppc64e success build succeeded & removed 0 sparse warning(s)
snowpatch_ozlabs/build-ppc64be fail build failed!
snowpatch_ozlabs/build-ppc64le fail build failed!
snowpatch_ozlabs/apply_patch success next/apply_patch Successfully applied

Commit Message

Daniel Jordan Feb. 11, 2019, 10:44 p.m.
Memory used for TCE tables in kvm_vm_ioctl_create_spapr_tce is currently
accounted to locked_vm because it stays resident and its allocation is
directly triggered from userspace as explained in f8626985c7c2 ("KVM:
PPC: Account TCE-containing pages in locked_vm").

However, since the memory comes straight from the page allocator (and to
a lesser extent unreclaimable slab) and is effectively pinned, it should
be accounted with pinned_vm (see bc3e53f682d9 ("mm: distinguish between
mlocked and pinned pages")).

pinned_vm recently became atomic and so no longer relies on mmap_sem
held as writer: delete.

Signed-off-by: Daniel Jordan <daniel.m.jordan@oracle.com>
---
 arch/powerpc/kvm/book3s_64_vio.c | 35 ++++++++++++++------------------
 1 file changed, 15 insertions(+), 20 deletions(-)

Comments

kbuild test robot Feb. 13, 2019, 1:43 a.m. | #1
Hi Daniel,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on vfio/next]
[also build test ERROR on v5.0-rc4]
[cannot apply to next-20190212]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Daniel-Jordan/use-pinned_vm-instead-of-locked_vm-to-account-pinned-pages/20190213-070458
base:   https://github.com/awilliam/linux-vfio.git next
config: powerpc-defconfig (attached as .config)
compiler: powerpc64-linux-gnu-gcc (Debian 8.2.0-11) 8.2.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        GCC_VERSION=8.2.0 make.cross ARCH=powerpc 

All errors (new ones prefixed by >>):

   In file included from include/linux/llist.h:63,
                    from include/linux/smp.h:15,
                    from include/linux/percpu.h:7,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/vtime.h:5,
                    from include/linux/hardirq.h:8,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
   arch/powerpc/kvm/book3s_64_vio.c: In function 'kvmppc_account_memlimit':
>> arch/powerpc/kvm/book3s_64_vio.c:70:42: error: passing argument 2 of 'atomic64_add_return_relaxed' from incompatible pointer type [-Werror=incompatible-pointer-types]
      pinned_vm = atomic64_add_return(pages, &current->mm->pinned_vm);
                                             ^~~~~~~~~~~~~~~~~~~~~~~
   include/linux/atomic.h:75:22: note: in definition of macro '__atomic_op_fence'
     typeof(op##_relaxed(args)) __ret;    \
                         ^~~~
   arch/powerpc/kvm/book3s_64_vio.c:70:15: note: in expansion of macro 'atomic64_add_return'
      pinned_vm = atomic64_add_return(pages, &current->mm->pinned_vm);
                  ^~~~~~~~~~~~~~~~~~~
   In file included from include/linux/atomic.h:7,
                    from include/linux/llist.h:63,
                    from include/linux/smp.h:15,
                    from include/linux/percpu.h:7,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/vtime.h:5,
                    from include/linux/hardirq.h:8,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
   arch/powerpc/include/asm/atomic.h:331:52: note: expected 'atomic64_t *' {aka 'struct <anonymous> *'} but argument is of type 'long unsigned int *'
    atomic64_##op##_return_relaxed(long a, atomic64_t *v)   \
                                           ~~~~~~~~~~~~^
   arch/powerpc/include/asm/atomic.h:367:2: note: in expansion of macro 'ATOMIC64_OP_RETURN_RELAXED'
     ATOMIC64_OP_RETURN_RELAXED(op, asm_op)    \
     ^~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/powerpc/include/asm/atomic.h:370:1: note: in expansion of macro 'ATOMIC64_OPS'
    ATOMIC64_OPS(add, add)
    ^~~~~~~~~~~~
   In file included from include/linux/llist.h:63,
                    from include/linux/smp.h:15,
                    from include/linux/percpu.h:7,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/vtime.h:5,
                    from include/linux/hardirq.h:8,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
>> arch/powerpc/kvm/book3s_64_vio.c:70:42: error: passing argument 2 of 'atomic64_add_return_relaxed' from incompatible pointer type [-Werror=incompatible-pointer-types]
      pinned_vm = atomic64_add_return(pages, &current->mm->pinned_vm);
                                             ^~~~~~~~~~~~~~~~~~~~~~~
   include/linux/atomic.h:77:23: note: in definition of macro '__atomic_op_fence'
     __ret = op##_relaxed(args);     \
                          ^~~~
   arch/powerpc/kvm/book3s_64_vio.c:70:15: note: in expansion of macro 'atomic64_add_return'
      pinned_vm = atomic64_add_return(pages, &current->mm->pinned_vm);
                  ^~~~~~~~~~~~~~~~~~~
   In file included from include/linux/atomic.h:7,
                    from include/linux/llist.h:63,
                    from include/linux/smp.h:15,
                    from include/linux/percpu.h:7,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/vtime.h:5,
                    from include/linux/hardirq.h:8,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
   arch/powerpc/include/asm/atomic.h:331:52: note: expected 'atomic64_t *' {aka 'struct <anonymous> *'} but argument is of type 'long unsigned int *'
    atomic64_##op##_return_relaxed(long a, atomic64_t *v)   \
                                           ~~~~~~~~~~~~^
   arch/powerpc/include/asm/atomic.h:367:2: note: in expansion of macro 'ATOMIC64_OP_RETURN_RELAXED'
     ATOMIC64_OP_RETURN_RELAXED(op, asm_op)    \
     ^~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/powerpc/include/asm/atomic.h:370:1: note: in expansion of macro 'ATOMIC64_OPS'
    ATOMIC64_OPS(add, add)
    ^~~~~~~~~~~~
>> arch/powerpc/kvm/book3s_64_vio.c:73:24: error: passing argument 2 of 'atomic64_sub' from incompatible pointer type [-Werror=incompatible-pointer-types]
       atomic64_sub(pages, &current->mm->pinned_vm);
                           ^~~~~~~~~~~~~~~~~~~~~~~
   In file included from include/linux/atomic.h:7,
                    from include/linux/llist.h:63,
                    from include/linux/smp.h:15,
                    from include/linux/percpu.h:7,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/vtime.h:5,
                    from include/linux/hardirq.h:8,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
   arch/powerpc/include/asm/atomic.h:315:58: note: expected 'atomic64_t *' {aka 'struct <anonymous> *'} but argument is of type 'long unsigned int *'
    static __inline__ void atomic64_##op(long a, atomic64_t *v)  \
                                                 ~~~~~~~~~~~~^
   arch/powerpc/include/asm/atomic.h:366:2: note: in expansion of macro 'ATOMIC64_OP'
     ATOMIC64_OP(op, asm_op)      \
     ^~~~~~~~~~~
   arch/powerpc/include/asm/atomic.h:371:1: note: in expansion of macro 'ATOMIC64_OPS'
    ATOMIC64_OPS(sub, subf)
    ^~~~~~~~~~~~
>> arch/powerpc/kvm/book3s_64_vio.c:76:29: error: passing argument 1 of 'atomic64_read' from incompatible pointer type [-Werror=incompatible-pointer-types]
      pinned_vm = atomic64_read(&current->mm->pinned_vm);
                                ^~~~~~~~~~~~~~~~~~~~~~~
   In file included from include/linux/atomic.h:7,
                    from include/linux/llist.h:63,
                    from include/linux/smp.h:15,
                    from include/linux/percpu.h:7,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/vtime.h:5,
                    from include/linux/hardirq.h:8,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
   arch/powerpc/include/asm/atomic.h:300:56: note: expected 'const atomic64_t *' {aka 'const struct <anonymous> *'} but argument is of type 'long unsigned int *'
    static __inline__ long atomic64_read(const atomic64_t *v)
                                         ~~~~~~~~~~~~~~~~~~^
   arch/powerpc/kvm/book3s_64_vio.c:80:23: error: passing argument 2 of 'atomic64_sub' from incompatible pointer type [-Werror=incompatible-pointer-types]
      atomic64_sub(pages, &current->mm->pinned_vm);
                          ^~~~~~~~~~~~~~~~~~~~~~~
   In file included from include/linux/atomic.h:7,
                    from include/linux/llist.h:63,
                    from include/linux/smp.h:15,
                    from include/linux/percpu.h:7,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/vtime.h:5,
                    from include/linux/hardirq.h:8,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
   arch/powerpc/include/asm/atomic.h:315:58: note: expected 'atomic64_t *' {aka 'struct <anonymous> *'} but argument is of type 'long unsigned int *'
    static __inline__ void atomic64_##op(long a, atomic64_t *v)  \
                                                 ~~~~~~~~~~~~^
   arch/powerpc/include/asm/atomic.h:366:2: note: in expansion of macro 'ATOMIC64_OP'
     ATOMIC64_OP(op, asm_op)      \
     ^~~~~~~~~~~
   arch/powerpc/include/asm/atomic.h:371:1: note: in expansion of macro 'ATOMIC64_OPS'
    ATOMIC64_OPS(sub, subf)
    ^~~~~~~~~~~~
   In file included from include/linux/kernel.h:14,
                    from include/linux/list.h:9,
                    from include/linux/preempt.h:11,
                    from include/linux/hardirq.h:5,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
   arch/powerpc/kvm/book3s_64_vio.c:85:18: error: passing argument 1 of 'atomic64_read' from incompatible pointer type [-Werror=incompatible-pointer-types]
       atomic64_read(&current->mm->pinned_vm) << PAGE_SHIFT,
                     ^~~~~~~~~~~~~~~~~~~~~~~
   include/linux/printk.h:136:17: note: in definition of macro 'no_printk'
      printk(fmt, ##__VA_ARGS__);  \
                    ^~~~~~~~~~~
   arch/powerpc/kvm/book3s_64_vio.c:83:2: note: in expansion of macro 'pr_debug'
     pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%lu %ld/%lu%s\n", current->pid,
     ^~~~~~~~
   In file included from include/linux/atomic.h:7,
                    from include/linux/llist.h:63,
                    from include/linux/smp.h:15,
                    from include/linux/percpu.h:7,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/vtime.h:5,
                    from include/linux/hardirq.h:8,
                    from include/linux/kvm_host.h:10,
                    from arch/powerpc/kvm/book3s_64_vio.c:23:
   arch/powerpc/include/asm/atomic.h:300:56: note: expected 'const atomic64_t *' {aka 'const struct <anonymous> *'} but argument is of type 'long unsigned int *'
    static __inline__ long atomic64_read(const atomic64_t *v)
                                         ~~~~~~~~~~~~~~~~~~^
   cc1: all warnings being treated as errors

vim +/atomic64_add_return_relaxed +70 arch/powerpc/kvm/book3s_64_vio.c

  > 23	#include <linux/kvm_host.h>
    24	#include <linux/highmem.h>
    25	#include <linux/gfp.h>
    26	#include <linux/slab.h>
    27	#include <linux/sched/signal.h>
    28	#include <linux/hugetlb.h>
    29	#include <linux/list.h>
    30	#include <linux/anon_inodes.h>
    31	#include <linux/iommu.h>
    32	#include <linux/file.h>
    33	
    34	#include <asm/kvm_ppc.h>
    35	#include <asm/kvm_book3s.h>
    36	#include <asm/book3s/64/mmu-hash.h>
    37	#include <asm/hvcall.h>
    38	#include <asm/synch.h>
    39	#include <asm/ppc-opcode.h>
    40	#include <asm/kvm_host.h>
    41	#include <asm/udbg.h>
    42	#include <asm/iommu.h>
    43	#include <asm/tce.h>
    44	#include <asm/mmu_context.h>
    45	
    46	static unsigned long kvmppc_tce_pages(unsigned long iommu_pages)
    47	{
    48		return ALIGN(iommu_pages * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
    49	}
    50	
    51	static unsigned long kvmppc_stt_pages(unsigned long tce_pages)
    52	{
    53		unsigned long stt_bytes = sizeof(struct kvmppc_spapr_tce_table) +
    54				(tce_pages * sizeof(struct page *));
    55	
    56		return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE;
    57	}
    58	
    59	static long kvmppc_account_memlimit(unsigned long pages, bool inc)
    60	{
    61		long ret = 0;
    62		s64 pinned_vm;
    63	
    64		if (!current || !current->mm)
    65			return ret; /* process exited */
    66	
    67		if (inc) {
    68			unsigned long lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
    69	
  > 70			pinned_vm = atomic64_add_return(pages, &current->mm->pinned_vm);
    71			if (pinned_vm > lock_limit && !capable(CAP_IPC_LOCK)) {
    72				ret = -ENOMEM;
  > 73				atomic64_sub(pages, &current->mm->pinned_vm);
    74			}
    75		} else {
  > 76			pinned_vm = atomic64_read(&current->mm->pinned_vm);
    77			if (WARN_ON_ONCE(pages > pinned_vm))
    78				pages = pinned_vm;
    79	
    80			atomic64_sub(pages, &current->mm->pinned_vm);
    81		}
    82	
    83		pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%lu %ld/%lu%s\n", current->pid,
    84				inc ? '+' : '-', pages << PAGE_SHIFT,
    85				atomic64_read(&current->mm->pinned_vm) << PAGE_SHIFT,
    86				rlimit(RLIMIT_MEMLOCK), ret ? " - exceeded" : "");
    87	
    88		return ret;
    89	}
    90	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

Patch

diff --git a/arch/powerpc/kvm/book3s_64_vio.c b/arch/powerpc/kvm/book3s_64_vio.c
index 532ab79734c7..2f8d7c051e4e 100644
--- a/arch/powerpc/kvm/book3s_64_vio.c
+++ b/arch/powerpc/kvm/book3s_64_vio.c
@@ -56,39 +56,34 @@  static unsigned long kvmppc_stt_pages(unsigned long tce_pages)
 	return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE;
 }
 
-static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
+static long kvmppc_account_memlimit(unsigned long pages, bool inc)
 {
 	long ret = 0;
+	s64 pinned_vm;
 
 	if (!current || !current->mm)
 		return ret; /* process exited */
 
-	down_write(&current->mm->mmap_sem);
-
 	if (inc) {
-		unsigned long locked, lock_limit;
+		unsigned long lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
 
-		locked = current->mm->locked_vm + stt_pages;
-		lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
-		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
+		pinned_vm = atomic64_add_return(pages, &current->mm->pinned_vm);
+		if (pinned_vm > lock_limit && !capable(CAP_IPC_LOCK)) {
 			ret = -ENOMEM;
-		else
-			current->mm->locked_vm += stt_pages;
+			atomic64_sub(pages, &current->mm->pinned_vm);
+		}
 	} else {
-		if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm))
-			stt_pages = current->mm->locked_vm;
+		pinned_vm = atomic64_read(&current->mm->pinned_vm);
+		if (WARN_ON_ONCE(pages > pinned_vm))
+			pages = pinned_vm;
 
-		current->mm->locked_vm -= stt_pages;
+		atomic64_sub(pages, &current->mm->pinned_vm);
 	}
 
-	pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%ld %ld/%ld%s\n", current->pid,
-			inc ? '+' : '-',
-			stt_pages << PAGE_SHIFT,
-			current->mm->locked_vm << PAGE_SHIFT,
-			rlimit(RLIMIT_MEMLOCK),
-			ret ? " - exceeded" : "");
-
-	up_write(&current->mm->mmap_sem);
+	pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%lu %ld/%lu%s\n", current->pid,
+			inc ? '+' : '-', pages << PAGE_SHIFT,
+			atomic64_read(&current->mm->pinned_vm) << PAGE_SHIFT,
+			rlimit(RLIMIT_MEMLOCK), ret ? " - exceeded" : "");
 
 	return ret;
 }