From patchwork Wed Dec 11 23:51:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 11286549 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EFF3514BD for ; Wed, 11 Dec 2019 23:51:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B22E8222C4 for ; Wed, 11 Dec 2019 23:51:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b="dvW77Nrm" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B22E8222C4 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BCC506B3492; Wed, 11 Dec 2019 18:51:12 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B56526B3493; Wed, 11 Dec 2019 18:51:12 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A1D616B3494; Wed, 11 Dec 2019 18:51:12 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0085.hostedemail.com [216.40.44.85]) by kanga.kvack.org (Postfix) with ESMTP id 88C016B3492 for ; Wed, 11 Dec 2019 18:51:12 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id 3F914513 for ; Wed, 11 Dec 2019 23:51:12 +0000 (UTC) X-FDA: 76254509184.20.seat44_278e7df6a7a2a X-Spam-Summary: 2,0,0,ac9abe18231afc8e,d41d8cd98f00b204,joel@joelfernandes.org,::akpm@linux-foundation.org:mhocko@suse.com:riel@redhat.com:minchan@google.com:hannes@cmpxchg.org:kirill.shutemov@linux.intel.com,RULES_HIT:41:69:355:379:960:968:973:988:989:1260:1277:1312:1313:1314:1345:1381:1437:1516:1518:1519:1535:1543:1593:1594:1595:1596:1711:1730:1747:1777:1792:2393:2559:2562:2689:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:3873:3874:4184:4250:4321:5007:6119:6261:6653:6691:7903:8957:9000:9969:10004:10226:10400:11026:11658:11914:12043:12048:12295:12296:12297:12438:12517:12519:12555:12663:12895:12986:13071:13146:13161:13229:13230:13439:13895:14096:14097:14180:14181:14721:21060:21063:21080:21324:21433:21444:21450:21451:21611:21627:21740:21990:30003:30012:30046:30054:30070,0,RBL:209.85.214.176:@joelfernandes.org:.lbl8.mailshell.net-62.14.0.100 66.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral, Custom_r X-HE-Tag: seat44_278e7df6a7a2a X-Filterd-Recvd-Size: 5892 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Wed, 11 Dec 2019 23:51:11 +0000 (UTC) Received: by mail-pl1-f176.google.com with SMTP id g6so221195plp.7 for ; Wed, 11 Dec 2019 15:51:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=VWBjbIAlhb3eHCJoX68dSikocrqnH8MT20KqZSrL1l0=; b=dvW77NrmLj2zsPWuXc1gFzy9hRk+VUPmFPswwieujOOdufw9b2kj3+Xb/Tqrs3PWG8 HxlHjEpibtuhABedesa6MSbPl9UMOG1FnMQGpQVUxXNX7Mf03xgfwz4/0QYPXT76dtwn 4Ia6aCqoO9zNpHblK1qtsOATQCgJ+ZjkhqkiE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=VWBjbIAlhb3eHCJoX68dSikocrqnH8MT20KqZSrL1l0=; b=Pb/ZMdQPjlgcs1E94FR7fAsduQykgMrG02YpkDOLxoQHFa242A54QS3k7wX3U4hxQc 2yTCetBNcnpnlw8d7RYTxLFkXPy29W2nyn5bZrk1/Wwfrs2XHU1P2qDGbNUb0HTocg6X e2JSGPkovyqSgnT4ff6FSeMM5DlDXmadUrp9iTgYlGVh7qIgn5l7HzsOOm1mVAjSKoSg Y5w+zmoSbGZhFDk48taJar/mM/BTrK1mrtIziVaA55QumX02fawrVyAag5B8bB45oqIe hprAXLS4amvi1EooBTfOOU1uq3SK6M6vLW/Ph21rAutt1oZc3jniX/G/tY1MVSfDbdIA hQGw== X-Gm-Message-State: APjAAAUxywxK9pbQpjQ1d9aWdXYmBTsPEdZKMpkE+HE0bWKxY2Iab0Gj 8O3WLAwhna2MfOJ52cG0fVxE5axWl1o= X-Google-Smtp-Source: APXvYqzxuFJhP2rPsqB0ZmGTRdifoCKrLMRvjSUVHcHFw+VcArGxtD3fAQprtDiRBqjLPQmKhBjTzA== X-Received: by 2002:a17:90a:868b:: with SMTP id p11mr6557837pjn.60.1576108270129; Wed, 11 Dec 2019 15:51:10 -0800 (PST) Received: from localhost ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id g7sm4346867pfq.33.2019.12.11.15.51.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Dec 2019 15:51:09 -0800 (PST) Date: Wed, 11 Dec 2019 18:51:08 -0500 From: Joel Fernandes To: linux-mm@kvack.org, akpm@linux-foundation.org, mhocko@suse.com, riel@redhat.com, minchan@google.com, hannes@cmpxchg.org, kirill.shutemov@linux.intel.com Subject: rmap for shmem pages Message-ID: <20191211235108.GA85068@google.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hi, I am looking into a weird behavior of the VmRSS counter in /proc/pid/status which I think is a bug. The issue is if a memfd is mapped into the same process twice as MAP_SHARED, and you write to both maps, then the RSS doubles even though the pages were mapped elsewhere within the same address space before. We expect RSS to increase only once in this case, not twice. Digging further, I see the RssShmem is contributing to this increase and this happens in alloc_set_pte() during a page fault. I tried to write a patch to check if the page is mapped within the same process already, and if not, don't do the increment as the accounting was already previously done. However for shmem pages, it appears rmap has no effect and cannot detect the case I am interested in. The patch below is obviously incomplete since I didn't handle the unmap case, but could anyone let me know why rmap does not seem to be working for shmem, and is this expected? I was expecting the RSS increment to be skipped since the code should find that the page was mapped locally within same address space already. ---8<----------------------- diff --git a/mm/memory.c b/mm/memory.c index 606da187d1de..4fca5d7e9d66 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3360,6 +3361,8 @@ static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) * * Return: %0 on success, %VM_FAULT_ code in case of error. */ +bool page_has_single_mm(struct page *page, struct mm_struct *mm); + vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, struct page *page) { @@ -3399,8 +3402,14 @@ vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, mem_cgroup_commit_charge(page, memcg, false, false); lru_cache_add_active_or_unevictable(page, vma); } else { - inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); page_add_file_rmap(page, false); + + if (page_has_single_mm(page, vma->vm_mm)) { + trace_printk("inc 2 mm_counter_file counter=%d\n", mm_counter_file(page)); + inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); + } else { + trace_printk("inc 2 mm_counter_file counter=%d skipped\n", mm_counter_file(page)); + } } set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); diff --git a/mm/rmap.c b/mm/rmap.c index b3e381919835..580da8469f0d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1273,6 +1273,39 @@ static void page_remove_file_rmap(struct page *page, bool compound) unlock_page_memcg(page); } +struct page_single_mm_ctx { + struct mm_struct *mm; + bool single; +}; + +static bool __page_has_single_mm(struct page *page, struct vm_area_struct *vma, + unsigned long addr, void *ctx_in) +{ + struct page_single_mm_ctx *ctx = ctx_in; + + if (vma->vm_mm == ctx->mm) + return true; + + ctx->single = false; + return false; +} + +bool page_has_single_mm(struct page *page, struct mm_struct *mm) +{ + struct page_single_mm_ctx ctx; + + ctx.mm = mm; + ctx.single = true; + + struct rmap_walk_control rwc = { + .rmap_one = __page_has_single_mm, + .arg = &ctx, + }; + + rmap_walk(page, &rwc); + return ctx.single; +} + static void page_remove_anon_compound_rmap(struct page *page) { int i, nr;