Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp199214ybe; Thu, 12 Sep 2019 18:18:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqw4Ryr2LJtgHZWhoI55aPpPpqCLTy3pONRylQlCLjc2x56q3ONeKKCrAuQocynmc9ghsiZs X-Received: by 2002:a17:906:498c:: with SMTP id p12mr4843721eju.289.1568337533191; Thu, 12 Sep 2019 18:18:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568337533; cv=none; d=google.com; s=arc-20160816; b=F9QJ6UYIsgKIqbQeJc9wbXKknqen/8YSkdp0nSADV00taO9YbQ0FZASzmLwcxkyA6B wfaU6lvxFpseafsg4ni+EQKFBcqeAQ1vlTDo5jhnPRIRLsbp1URTwLxaeT934azfh4dL wRZwLlSNzFF80NXsukSqChSQX53APe57lZEiQUzM5wkfjq+IcLntW0mE2eWXDRhWTa3d WH5KsJvkpha5WxNOfb2Of3Euc48I0o/D2K3ZZbrxHZqYB8sorvLdzaPip86jdh5m9Cwl rsHDSyeI7/dO+ERHu5z3YH8lENnKoX9hmWF77X1yOUi5SRLapTvAbd7rdqG7yrVbrWeH fV+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject :smtp-origin-cluster:cc:to:smtp-origin-hostname:from :smtp-origin-hostprefix:dkim-signature; bh=eJAkWK9ab3KEK9eiIEM2hNTnrj5bd98d2PXl3/7B5kA=; b=KN4M/WYwQZJqOoHyp1M0RwFBU0KB+wnlKuoLZ2a7c9/dF6XaI5+MywpgTVVxK0/YsU guSHUuvyyVJo6aVVUQPwR3BGNGV+7mTnNfc0PXh+tXxNHa2cMhQFXy5C9vSuQ+wjC3Iy z71X5S6OeIEey0wnR4AZBjZI6pL/fMY4h4z/zsh5aCX/Pr9aJdaq9+JPy4JBMjNjDK8n SGl5j2Zmc7nlwWCFFCne+a7PgrVTBRfT9zxI7z0sa8P+Hx/bf1xboDcTDiO2rgrr/w/Q 2PVZI9EJFHcpQ2mTbvjO278ve+vlkuRuILasYF+RhUgIgYgIDgis+D3xNj1BgGkOt8KL 1FdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b="KEE3nZG/"; 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=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g14si13550288ejh.172.2019.09.12.18.18.29; Thu, 12 Sep 2019 18:18:53 -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=@fb.com header.s=facebook header.b="KEE3nZG/"; 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=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726509AbfILXSp (ORCPT + 99 others); Thu, 12 Sep 2019 19:18:45 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:21986 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726032AbfILXSo (ORCPT ); Thu, 12 Sep 2019 19:18:44 -0400 Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id x8CNIdAq027007 for ; Thu, 12 Sep 2019 16:18:43 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=eJAkWK9ab3KEK9eiIEM2hNTnrj5bd98d2PXl3/7B5kA=; b=KEE3nZG/JOQDnNQzUYlH+IEZLdLgjHRmdAJPx87Fqvg3zAPIbhFkY0U06zvqcGBJlfoF dIdw22fCV29IeqB4y5BW9T4sn0gK0QOrl1DKttB/zx9vNdOOomaj3MSWTvHjsgXNK2dU zEpjdARG7yxKkIHUxb8kh5Grh47kTxdNKEs= Received: from mail.thefacebook.com (mailout.thefacebook.com [199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2uytcssjcx-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 12 Sep 2019 16:18:43 -0700 Received: from mx-out.facebook.com (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1713.5; Thu, 12 Sep 2019 16:18:31 -0700 Received: by devbig059.prn3.facebook.com (Postfix, from userid 4924) id 1E37017419B8; Thu, 12 Sep 2019 16:18:31 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Lucian Adrian Grijincu Smtp-Origin-Hostname: devbig059.prn3.facebook.com To: Lucian Adrian Grijincu , CC: , Michal Hocko , Andrew Morton , Rik van Riel , Roman Gushchin Smtp-Origin-Cluster: prn3c05 Subject: [PATCH] mm: memory: fix /proc/meminfo reporting for MLOCK_ONFAULT Date: Thu, 12 Sep 2019 16:18:20 -0700 Message-ID: <20190912231820.590276-1-lucian@fb.com> X-Mailer: git-send-email 2.17.1 X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.70,1.0.8 definitions=2019-09-12_12:2019-09-11,2019-09-12 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 mlxscore=0 lowpriorityscore=0 spamscore=0 impostorscore=0 phishscore=0 clxscore=1011 adultscore=0 malwarescore=0 mlxlogscore=639 suspectscore=2 priorityscore=1501 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1908290000 definitions=main-1909120238 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- mm/memory.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index e0c232fe81d9..7e8dc3ed4e89 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3311,6 +3311,9 @@ 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 | VM_SPECIAL)) == VM_LOCKED && + !PageTransCompound(page)) + mlock_vma_page(page); } set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); -- 2.17.1