Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp2341473imm; Thu, 11 Oct 2018 08:47:33 -0700 (PDT) X-Google-Smtp-Source: ACcGV60OzFk/i4zOs3R99hHSpCwgepTEGWAgWH60aAhVkK3Bik2NpWv1BTDcPMLTzQW6eBT5wbaW X-Received: by 2002:a17:902:b182:: with SMTP id s2-v6mr2077335plr.84.1539272853385; Thu, 11 Oct 2018 08:47:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539272853; cv=none; d=google.com; s=arc-20160816; b=H9IPEfyPobLPIV4qYuhZLHJVUqhUveLQ3/XHrlBywxnd6RRSLl2r89JSA0uz8NZniR ryzurXiEtMxj+qTLPr+hOCkcCcOcPsJgudo01WGLNLFnHJyciuFcxqqPY8WMj2vhOJ8M IHsNcA0Ath5qT9/UsXtr1LVywUbbpTgSHDBZvUNKn7zY+PhB4hpneBgz6QTS1hiyOtsO clYvkx7jR0aWZeeAiN+yE34jL3djwv3HqTRr2GQe2KKDHGejCa6mxzYouhXL0EUIxjzB TIF51X7IyImT0oAxMhd+VpOJtn/YMBrklGeF51NogEukp1NRQBpua+6x+yX5oSP5t+pq efoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/E1dIiyMjZbotlZCNRfJ6SHNeiHssL0IdX30NeeCX34=; b=dIPidqBhtun6M+6XcpJpzN72UjS2egKl0Lzvf+wSVHKsItTiPFL8RueCPozazvqhzq uH85tpM7VCoPBhYsg3PXSsa+eXBmwPeM8J5zEj+pqko2caIcBtL04UvLAQsSOHyEQ81m lMBb/HJ3/EermckOPS3ouU6TV5/142EUjw0xXdehKapspr5Wy/8X11e4+WecK9kAGbYt y+ves+CxHbUAOVLhLfwBuvEvK/zVcF3q/cM9YCPhhRz487KyA698m6HJjrKlZsKkEEun CDXpy+WvBQj2mkzef7cUBtLeopZmgb6RIWAZAC2NnrLZ0KWBcBNxfYfsZ/MJxtRiGvjB qkEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tH+RFqeF; 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 z12-v6si23492878pgo.75.2018.10.11.08.47.18; Thu, 11 Oct 2018 08:47: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=@kernel.org header.s=default header.b=tH+RFqeF; 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 S1731482AbeJKXNs (ORCPT + 99 others); Thu, 11 Oct 2018 19:13:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:46578 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726700AbeJKXNs (ORCPT ); Thu, 11 Oct 2018 19:13:48 -0400 Received: from localhost (ip-213-127-77-176.ip.prioritytelecom.net [213.127.77.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CA4C32098A; Thu, 11 Oct 2018 15:46:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539272763; bh=E3dnPpSYnieZB8xRI+f5z4gXC1anCv4Vi2dr9kkvLhA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tH+RFqeF7n/AsgD24lHW34llNvPnfkuTECD1Z87rt+uVGDoMfmt2eMWK+Pw7XtTQG ErpcqwJDQWH6Cl2Uofj0Z8cWWJG8h/cSNXHhrrQpdlYsjZ20jDRI+T8IV4FoPTluJ+ 575Jm/9Z+BRxoxHvlgXStTWiMP64nDn+tVS2UgSU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Kirill A. Shutemov" , Vegard Nossum , Zi Yan , Naoya Horiguchi , Vlastimil Babka , Andrea Arcangeli , Andrew Morton Subject: [PATCH 4.14 03/45] mm, thp: fix mlocking THP page with migration enabled Date: Thu, 11 Oct 2018 17:39:30 +0200 Message-Id: <20181011152509.012053940@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181011152508.885515042@linuxfoundation.org> References: <20181011152508.885515042@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kirill A. Shutemov commit e125fe405abedc1dc8a5b2229b80ee91c1434015 upstream. 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[1] 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); return 0; } [1] https://lkml.kernel.org/r/CAOMGZ=G52R-30rZvhGxEbkTw7rLLwBGadVYeo--iizcD3upL3A@mail.gmail.com Link: http://lkml.kernel.org/r/20180917133816.43995-1-kirill.shutemov@linux.intel.com Fixes: 616b8371539a ("mm: thp: enable thp migration in generic path") Signed-off-by: Kirill A. Shutemov Reported-by: Vegard Nossum Reviewed-by: Zi Yan Cc: Naoya Horiguchi Cc: Vlastimil Babka Cc: Andrea Arcangeli Cc: [4.14+] Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/huge_memory.c | 2 +- mm/migrate.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2886,7 +2886,7 @@ void remove_migration_pmd(struct page_vm flush_cache_range(vma, mmun_start, mmun_start + HPAGE_PMD_SIZE); page_add_anon_rmap(new, vma, mmun_start, 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); } --- a/mm/migrate.c +++ b/mm/migrate.c @@ -274,6 +274,9 @@ static bool remove_migration_pte(struct if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new)) mlock_vma_page(new); + if (PageTransHuge(page) && PageMlocked(page)) + clear_page_mlock(page); + /* No need to invalidate - it was non-present before */ update_mmu_cache(vma, pvmw.address, pvmw.pte); }