Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5253153imm; Wed, 12 Sep 2018 03:27:07 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZVF2hZdFeLdl5CmiAPKRkENSSVZ4ggBI7cYCJXXBvYAHyBjDsREyzJQJ+3Q/Grd0kCMWeK X-Received: by 2002:a62:4299:: with SMTP id h25-v6mr1448425pfd.73.1536748027293; Wed, 12 Sep 2018 03:27:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536748027; cv=none; d=google.com; s=arc-20160816; b=j69UHKUAjClMBgYJ8nl2gd4r/DqCO3IkvY1dr1+CN74pjDU0GadO4voKSDX+NexOck /5T5JNYWJ0BMLzliYa+EiSDTkh6PpFKjl5Fxe5+UP4H8io82cSe7x5sqfkNUkdI18Lpg 8TBBn9FhJSMFasxdLGr9lzEzekEX5o/eGPlysD/mxAQWJT62PImnS2jltbJ6Lj3TmJgS d6b6f0UgmfOEDrCsAeP8m0KyyyQPYZ1SHmUbY55w9gQEfEGIigRZkcFVE2TK6FCjNdGT IJbrLpm8i2uPMf6PvoobFey8EMXbVxPExM4M4T5YNwWCRnsd93zrYLlVIz0aQ+iVjtHk HQLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=q8xalLbf3CxPj39DOk0PKZGvlQU6d43iaIbjyORJs5M=; b=wKCYTiqN0bNxVOsnXVivUdSHHg8TZQJvwTdaY58UMUtbFF2gV8U4ys1JliQVa/qzWV BIwWa1bFpMgPZrbwN729rTtfsPdQ+/w9MkBXlj7lQsiaB3BmRgJweH+1QuBd/zszGZqy ia1Seu0vDPW4yoN60BLOZKx19ODnjYmYoO23JUbiHiTLBmf/uOcYEb5rRDKV1eW62pf/ Nug3c6D2IIivWSoVHe5RYlWkw3GLGZEIKC4Jxa5Tgd5vpc9OQ6HSeunFmD5r2g/TdDmd hF6nmp/OhuVHAoVgJf7ju+UHb2pSbT7iGUKSueQ7/Igfub5wnOGvlGsreMr0LgoaXXr0 my4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=mzh450CX; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 7-v6si562012pll.369.2018.09.12.03.26.52; Wed, 12 Sep 2018 03:27:07 -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=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=mzh450CX; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727795AbeILP2z (ORCPT + 99 others); Wed, 12 Sep 2018 11:28:55 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43770 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726552AbeILP2z (ORCPT ); Wed, 12 Sep 2018 11:28:55 -0400 Received: by mail-wr1-f67.google.com with SMTP id k5-v6so1423718wre.10 for ; Wed, 12 Sep 2018 03:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=q8xalLbf3CxPj39DOk0PKZGvlQU6d43iaIbjyORJs5M=; b=mzh450CXyQ+qpAfmXaDBqYM7AJZ1Rf40jyi1oZEm87MEwVDWyW9um35ni057rgLtL8 DIXq0PoXDsOudYAZp3O3BV9iExpV/JK52TY91kJJeLpDi2nfGW6ZEWWhpabvOKGqkwlI VThd5vA2mxqb2ct1YJuRT3zCucW0mbZMAR65PrlQl5jocJ6gYdsv/lrNPo/u9gk6dyTM WREjux3J+2CsBpUncV6L6nRS117TZ9/LUk+CVAwOrFPkXxcNrbyYru+oawsQpNOHzeSb Iq3kTW3Mz/WLzca5bYH+M8g3oF4ntCzbeuYnhqFxRaw35b7wroF3jl2rhokWGApiDv0t DTdA== 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:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=q8xalLbf3CxPj39DOk0PKZGvlQU6d43iaIbjyORJs5M=; b=tkkoOCsyI8xnBlnnVXIeXqWRXdwUsFdfSolnZ9qqtWO4n6f/83M4YrWOLBCQV5E2bV reDXb5t5OXBg6g7XFg4OBkpI9yLqTY0AxPwlWomq0eHtZE634bCtZUB83eIWyVqEh58T 8sGFueQephv/tFlYtUi1IvERfcAWKnCG9FTvTY6bYIPvZVtWipOCBiTSQcfW7uKV6SVx A20C/8DNLFlWTwqWxFEmBXjbL2/f+LUr98MeqcSDzHVE7jXjCa7I7BgDotxJfY0LABSF LclHtVeOhJ3ZQoe5SPWnH7mCR7ET2rt7vqHBAvNwmtcfY1ublKeMqa1KNlFL74jejait Ux4Q== X-Gm-Message-State: APzg51CXaHMQpcfUmk5LMuk3w1N3guQkme5O1kuveqPVNo3xuhat9Ht0 LkXUlrTzoCdpo9LxEBga2czbXg== X-Received: by 2002:adf:b456:: with SMTP id v22-v6mr919571wrd.187.1536747900319; Wed, 12 Sep 2018 03:25:00 -0700 (PDT) Received: from kshutemo-mobl1.localdomain (mm-129-246-122-178.brest.dynamic.pppoe.byfly.by. [178.122.246.129]) by smtp.gmail.com with ESMTPSA id s10-v6sm525330wrw.35.2018.09.12.03.24.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Sep 2018 03:24:59 -0700 (PDT) Received: by kshutemo-mobl1.localdomain (Postfix, from userid 1000) id 3B426300DB6; Wed, 12 Sep 2018 13:24:58 +0300 (+03) Date: Wed, 12 Sep 2018 13:24:58 +0300 From: "Kirill A. Shutemov" To: "Aneesh Kumar K.V" Cc: "Kirill A. Shutemov" , Andrew Morton , Vegard Nossum , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Zi Yan , Naoya Horiguchi , Vlastimil Babka , Andrea Arcangeli Subject: Re: [PATCH] mm, thp: Fix mlocking THP page with migration enabled Message-ID: <20180912102457.deuqtyx2b67zfi7u@kshutemo-mobl1> References: <20180911103403.38086-1-kirill.shutemov@linux.intel.com> <6fcb5b5b-43fa-f1d0-ce78-37fb51b46a75@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6fcb5b5b-43fa-f1d0-ce78-37fb51b46a75@linux.ibm.com> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 12, 2018 at 03:28:24PM +0530, Aneesh Kumar K.V wrote: > On 9/11/18 4:04 PM, Kirill A. Shutemov wrote: > > A transparent huge page is represented by a single entry on an LRU list. > > Therefore, we can only make unevictable an entire compound page, not > > individual subpages. > > > > If a user tries to mlock() part of a huge page, we want the rest of the > > page to be reclaimable. > > > > We handle this by keeping PTE-mapped huge pages on normal LRU lists: the > > PMD on border of VM_LOCKED VMA will be split into PTE table. > > > > Introduction of THP migration breaks the rules around mlocking THP > > pages. If we had a single PMD mapping of the page in mlocked VMA, the > > page will get mlocked, regardless of PTE mappings of the page. > > > > For tmpfs/shmem it's easy to fix by checking PageDoubleMap() in > > remove_migration_pmd(). > > > > Anon THP pages can only be shared between processes via fork(). Mlocked > > page can only be shared if parent mlocked it before forking, otherwise > > CoW will be triggered on mlock(). > > > > For Anon-THP, we can fix the issue by munlocking the page on removing PTE > > migration entry for the page. PTEs for the page will always come after > > mlocked PMD: rmap walks VMAs from oldest to newest. > > > > Test-case: > > > > #include > > #include > > #include > > #include > > #include > > > > int main(void) > > { > > unsigned long nodemask = 4; > > void *addr; > > > > addr = mmap((void *)0x20000000UL, 2UL << 20, PROT_READ | PROT_WRITE, > > MAP_PRIVATE | MAP_ANONYMOUS | MAP_LOCKED, -1, 0); > > > > if (fork()) { > > wait(NULL); > > return 0; > > } > > > > mlock(addr, 4UL << 10); > > mbind(addr, 2UL << 20, MPOL_PREFERRED | MPOL_F_RELATIVE_NODES, > > &nodemask, 4, MPOL_MF_MOVE | MPOL_MF_MOVE_ALL); > > > > return 0; > > } > > > > Signed-off-by: Kirill A. Shutemov > > Reported-by: Vegard Nossum > > Fixes: 616b8371539a ("mm: thp: enable thp migration in generic path") > > Cc: [v4.14+] > > Cc: Zi Yan > > Cc: Naoya Horiguchi > > Cc: Vlastimil Babka > > Cc: Andrea Arcangeli > > --- > > mm/huge_memory.c | 2 +- > > mm/migrate.c | 3 +++ > > 2 files changed, 4 insertions(+), 1 deletion(-) > > > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > > index 533f9b00147d..00704060b7f7 100644 > > --- a/mm/huge_memory.c > > +++ b/mm/huge_memory.c > > @@ -2931,7 +2931,7 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) > > else > > page_add_file_rmap(new, true); > > set_pmd_at(mm, mmun_start, pvmw->pmd, pmde); > > - if (vma->vm_flags & VM_LOCKED) > > + if ((vma->vm_flags & VM_LOCKED) && !PageDoubleMap(new)) > > mlock_vma_page(new); > > update_mmu_cache_pmd(vma, address, pvmw->pmd); > > } > > diff --git a/mm/migrate.c b/mm/migrate.c > > index d6a2e89b086a..01dad96b25b5 100644 > > --- a/mm/migrate.c > > +++ b/mm/migrate.c > > @@ -275,6 +275,9 @@ static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma, > > if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new)) > > mlock_vma_page(new); > > + if (PageTransCompound(new) && PageMlocked(page)) > > + clear_page_mlock(page); > > + > > Can you explain this more? I am confused by the usage of 'new' and 'page' > there. 'new' is the PTE subpage of 'page'. clear_page_mlock() wants to see head page. I guess we can rewrite it more clearly: + if (PageTransHuge(page) && PageMlocked(page)) + clear_page_mlock(page); + > I guess the idea is if we are removing the migration pte at level 4 > table, and if we found the backing page compound don't mark the page > Mlocked? We has to clear mlock, not only don't mark it as such. remove_migration_pmd() for other VMA may mark it as mlock and we want to revert it on the first PTE mapping of the page. -- Kirill A. Shutemov