Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1732036ybe; Sat, 14 Sep 2019 00:11:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqzNMaqu54ieKsCiz4M31MjTnwHDe8qWZsHIVD7PvAJv+PqM1uRvtSEw13gMYgZP4Sjplejl X-Received: by 2002:a50:8ad1:: with SMTP id k17mr29101072edk.243.1568445093480; Sat, 14 Sep 2019 00:11:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568445093; cv=none; d=google.com; s=arc-20160816; b=dbj7UZqsawDElHa4k3B8dZQ7kvaswCJ7Rf1TJlNuGE4HjMZLzpKBcxpFgeMWVJ/DMq 6wOtJBfmejJXc5vfMMK4/rF2zzxaXqhRz99iH+WSKfa0sX9qC3vIrluGmC7lmTiJ/irW 5iwUTqYze/278a9OLif7bPNtLFn4Plb+oZHMSBmsqMcEh4qkE0IbpLKj5XfRGllaAmXZ slpjILJ5u3p0RySnqKbxt1TFcWP5zg5IZVH9AWAdhpiCSgViFsXqE3c0r06WMyS/Uccc gTL1JUfR+DpLzfkWVNcYX4+4hH/JUTl0/5ISgb9jR3rbFxYyloisYv1LK1s+5zQbFS35 vxDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=hODFJZyoJjSmmRYWMOJEJfcm53I4w912l6n4+LWHTEE=; b=vb81k0gSa0gE7q1prHT8c7KT/fUvRYAXR43Sxib2kF/uHRFS4W0PADpBcFKh3w0Jiw AUXiVJmI2qt7jgGocF7JUwyD9sv4w9ymbXVrohZKw9g5AiDvEcC+KK9oK0Lq1VildDUi 7t48v2ytW0QRbW6IbMQ03GsfI7q37dOtJB0PcCG92xWt3AnuiXHuyOLyS1jMtUtdgNcF NCm7Mzr0fQqAlyjolZzbSweMikr221hseeZDbAPlez0QbGOxUrGPXe5Okb8Vv+NUZK8B oKfkHGCTIBG80lvsBv1Jt5vpLmBMCmF7Av4BMYuQ2KU26BHYfk8T9FLXaUPp02Sz2ls5 ezGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RcOazIdH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h3si18277771edh.372.2019.09.14.00.11.10; Sat, 14 Sep 2019 00:11:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RcOazIdH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389483AbfIMTq7 (ORCPT + 99 others); Fri, 13 Sep 2019 15:46:59 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:35364 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388211AbfIMTq7 (ORCPT ); Fri, 13 Sep 2019 15:46:59 -0400 Received: by mail-lf1-f68.google.com with SMTP id w6so23025189lfl.2 for ; Fri, 13 Sep 2019 12:46:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=hODFJZyoJjSmmRYWMOJEJfcm53I4w912l6n4+LWHTEE=; b=RcOazIdH9d40Xk6cQVhkhqCPkv5+tWTB7oyy0tbMqmeCtuHLwUhwBEYetiFHLmqNeS N+fvPM7ietagWkoeuNc/klR5GmqrvRS4Y2JPUY56yqoC0MOP4zbB+loU4Iw8Kw6SHYsh +rIN1sUn14h34Dt0/XhbN7eBEAr3LnukmE/4Z+shbyxtLvDeS3Gf0OU/y6a5T/Kx01rt vxN1a/gSxKGGYEIE6W8SUbBdxd+WblMUi7JoQVg46FfRsoc3X+33PhLjmdb2TcZd1hOM Ueke4akyz3ncYUYE0l5i1Otos+1fQZOVhl6dP6pU4nI/jAi9tvaUqBO4bmpQ823zW9Mk 4gzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=hODFJZyoJjSmmRYWMOJEJfcm53I4w912l6n4+LWHTEE=; b=kwUo2ngzO7OM7VHPLXmnl/VLmPlPGyKInjcbxYDAbcWaRKoEDk72im4pGf2/018hDX r1Fz1EXyZoHpfNMNpcvHGEJd8BEqVE0rVRos63eyYhfFiXYWTLEPUKiMLSGXZGbjQ3rJ 4HYxmhBzT9Sk4o6YCHRvlcM/IyGUQ91UKqLhSWgc9Nu6P3OrluvpWy2UhDYXUuZbYHo9 UTcmi6FgVDmhaNhFKv8nR/ilbvEM73NvsExLCSMkabEv4HZ5MVGkJbxJiNR+3mpkg+z8 1Yv6+P3oddbmIyhL+GjUmfw5zOJo6x5nhSFvElEMVlD8524E6F8RPOtx8KpWoSEhMogJ zSBQ== X-Gm-Message-State: APjAAAU1m+gAU03uszXIxvnU1nMugZpvnN9aUt+6wDaFxV0FCjMnhhNS F8E1RvW2AJvQvO9AAyyI62ksAULqub9chzGTVms= X-Received: by 2002:ac2:50c5:: with SMTP id h5mr1679174lfm.105.1568404016517; Fri, 13 Sep 2019 12:46:56 -0700 (PDT) MIME-Version: 1.0 References: <20190913192907.96530-1-lucian@fb.com> In-Reply-To: <20190913192907.96530-1-lucian@fb.com> From: Souptick Joarder Date: Sat, 14 Sep 2019 01:16:44 +0530 Message-ID: Subject: Re: [PATCH] mm: memory: fix /proc/meminfo reporting for MLOCK_ONFAULT To: Lucian Adrian Grijincu Cc: Linux-MM , linux-kernel@vger.kernel.org, Michal Hocko , Andrew Morton , Rik van Riel , Roman Gushchin Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Sep 14, 2019 at 12:59 AM Lucian Adrian Grijincu wrote: > > As pages are faulted in MLOCK_ONFAULT correctly updates > /proc/self/smaps, but doesn't update /proc/meminfo's Mlocked field. > > - Before this /proc/meminfo fields didn't change as pages were faulted in: > > ``` > = Start = > /proc/meminfo > Unevictable: 10128 kB > Mlocked: 10132 kB > = Creating testfile = > > = after mlock2(MLOCK_ONFAULT) = > /proc/meminfo > Unevictable: 10128 kB > Mlocked: 10132 kB > /proc/self/smaps > 7f8714000000-7f8754000000 rw-s 00000000 08:04 50857050 /root/testfile > Locked: 0 kB > > = after reading half of the file = > /proc/meminfo > Unevictable: 10128 kB > Mlocked: 10132 kB > /proc/self/smaps > 7f8714000000-7f8754000000 rw-s 00000000 08:04 50857050 /root/testfile > Locked: 524288 kB > > = after reading the entire the file = > /proc/meminfo > Unevictable: 10128 kB > Mlocked: 10132 kB > /proc/self/smaps > 7f8714000000-7f8754000000 rw-s 00000000 08:04 50857050 /root/testfile > Locked: 1048576 kB > > = after munmap = > /proc/meminfo > Unevictable: 10128 kB > Mlocked: 10132 kB > /proc/self/smaps > ``` > > - After: /proc/meminfo fields are properly updated as pages are touched: > > ``` > = Start = > /proc/meminfo > Unevictable: 60 kB > Mlocked: 60 kB > = Creating testfile = > > = after mlock2(MLOCK_ONFAULT) = > /proc/meminfo > Unevictable: 60 kB > Mlocked: 60 kB > /proc/self/smaps > 7f2b9c600000-7f2bdc600000 rw-s 00000000 08:04 63045798 /root/testfile > Locked: 0 kB > > = after reading half of the file = > /proc/meminfo > Unevictable: 524220 kB > Mlocked: 524220 kB > /proc/self/smaps > 7f2b9c600000-7f2bdc600000 rw-s 00000000 08:04 63045798 /root/testfile > Locked: 524288 kB > > = after reading the entire the file = > /proc/meminfo > Unevictable: 1048496 kB > Mlocked: 1048508 kB > /proc/self/smaps > 7f2b9c600000-7f2bdc600000 rw-s 00000000 08:04 63045798 /root/testfile > Locked: 1048576 kB > > = after munmap = > /proc/meminfo > Unevictable: 176 kB > Mlocked: 60 kB > /proc/self/smaps > ``` > > Repro code. > --- > > int mlock2wrap(const void* addr, size_t len, int flags) { > return syscall(SYS_mlock2, addr, len, flags); > } > > void smaps() { > char smapscmd[1000]; > snprintf( > smapscmd, > sizeof(smapscmd) - 1, > "grep testfile -A 20 /proc/%d/smaps | grep -E '(testfile|Locked)'", > getpid()); > printf("/proc/self/smaps\n"); > fflush(stdout); > system(smapscmd); > } > > void meminfo() { > const char* meminfocmd = "grep -E '(Mlocked|Unevictable)' /proc/meminfo"; > printf("/proc/meminfo\n"); > fflush(stdout); > system(meminfocmd); > } > > { \ > int rc = (call); \ > if (rc != 0) { \ > printf("error %d %s\n", rc, strerror(errno)); \ > exit(1); \ > } \ > } > int main(int argc, char* argv[]) { > printf("= Start =\n"); > meminfo(); > > printf("= Creating testfile =\n"); > size_t size = 1 << 30; // 1 GiB > int fd = open("testfile", O_CREAT | O_RDWR, 0666); > { > void* buf = malloc(size); > write(fd, buf, size); > free(buf); > } > int ret = 0; > void* addr = NULL; > addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); > > if (argc > 1) { > PCHECK(mlock2wrap(addr, size, MLOCK_ONFAULT)); > printf("= after mlock2(MLOCK_ONFAULT) =\n"); > meminfo(); > smaps(); > > for (size_t i = 0; i < size / 2; i += 4096) { > ret += ((char*)addr)[i]; > } > printf("= after reading half of the file =\n"); > meminfo(); > smaps(); > > for (size_t i = 0; i < size; i += 4096) { > ret += ((char*)addr)[i]; > } > printf("= after reading the entire the file =\n"); > meminfo(); > smaps(); > > } else { > PCHECK(mlock(addr, size)); > printf("= after mlock =\n"); > meminfo(); > smaps(); > } > > PCHECK(munmap(addr, size)); > printf("= after munmap =\n"); > meminfo(); > smaps(); > > return ret; > } > > --- > > Signed-off-by: Lucian Adrian Grijincu Acked-by: Souptick Joarder (For the comment on v1) Patch version need to be change to v2. > --- > mm/memory.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/mm/memory.c b/mm/memory.c > index e0c232fe81d9..55da24f33bc4 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -3311,6 +3311,8 @@ vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, > } else { > inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); > page_add_file_rmap(page, false); > + if (vma->vm_flags & VM_LOCKED && !PageTransCompound(page)) > + mlock_vma_page(page); > } > set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); > > -- > 2.17.1 >