From patchwork Thu Mar 10 17:26:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12776794 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 056AAC433EF for ; Thu, 10 Mar 2022 17:26:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E01F38D0001; Thu, 10 Mar 2022 12:26:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D7ABE8D0007; Thu, 10 Mar 2022 12:26:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3D6E8D0001; Thu, 10 Mar 2022 12:26:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 4C6C08D0006 for ; Thu, 10 Mar 2022 12:26:50 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 11F15243C1 for ; Thu, 10 Mar 2022 17:26:50 +0000 (UTC) X-FDA: 79229156580.12.75E96C6 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2074.outbound.protection.outlook.com [40.107.244.74]) by imf19.hostedemail.com (Postfix) with ESMTP id 347CA1A001A for ; Thu, 10 Mar 2022 17:26:49 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gfhHLVfWR0Wgxu/H3Vh01Jes+HnHtg1xLBa/iD7wH2AuTKIfp/CwjrH+LPKX7nE7FDSAvp0QnueNQUsQ6QrXgrpfofsLBnu0QO9NRhgiPIPVgkKl+BhhWi0+rMZFmqKS5SdEI7hJNF/M0vI9uHMDMpFuWXYtuAd02b34rCPTImJG5PvWRGZCQOjiFwWgLrhmJv13pvreB2ydB3WlQ+NBtY7I9DaFZ1nw4irzyGBy6pEvgrZ3ErC/4ocpDbtExWvT/A6uhp9oy7h3sW80jiMWWady98xXkB8A2L5pZ4eg7WAOz8Oz67hPMAu6Sk2OtGj4IqYOSCS/nXdivl8YIX66Xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=AVq79cS0d6V/NkPMYLcAbkSJpde0OPYFucZGLHE3tFc=; b=O5+vCNCTkORBshDOxPv3aJS67JGlkmdXaXbUGuOy5jAM91cZY7AyYYXGEUdls2RVFDWEYg96FhfZ/7jHWUle6pdFqIfeJdcS+WUScdsyasKnz0EvYO6kttD+XIF/y/vKHTNXrzJ42sJFcKobciP9AgR/gnn+XhCp297dsiBVNe9PwyKf+xcbG2GwmS46F7KCbh7jjS7NllF7Md9K6hqLHuWUC4+BLsFrD/2d/41NlyH43H9brNJZran4QGb/gfcI6IMx4Intdu9yDLG3ml1OCgFqylgKT9H8kL0UbBE8Cqs/ewlK5W+8b3L8nfPaYM+gSxsrTsb67bdo7ODlDDPCMw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AVq79cS0d6V/NkPMYLcAbkSJpde0OPYFucZGLHE3tFc=; b=4yf0XY8qlpJzSsKPFUCxkbf9vHUJT4LXu9AL5Xo8IFYumzf16ZNSPJyfZIBCUNHLaIV2UnGl95Ny8+OcnKWk655vHlsPbOOWOy0we8okdpMDChYZ1kCwMqd3b34ras1wOifZRQbJSOsJ8LMQjje96u1MyUGNgDNjHEvt9Hk0Zug= Received: from DM3PR11CA0014.namprd11.prod.outlook.com (2603:10b6:0:54::24) by MW2PR12MB2507.namprd12.prod.outlook.com (2603:10b6:907:c::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.22; Thu, 10 Mar 2022 17:26:46 +0000 Received: from DM6NAM11FT010.eop-nam11.prod.protection.outlook.com (2603:10b6:0:54:cafe::e6) by DM3PR11CA0014.outlook.office365.com (2603:10b6:0:54::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.27 via Frontend Transport; Thu, 10 Mar 2022 17:26:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT010.mail.protection.outlook.com (10.13.172.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5061.22 via Frontend Transport; Thu, 10 Mar 2022 17:26:45 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Thu, 10 Mar 2022 11:26:44 -0600 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v1 1/3] mm: split vm_normal_pages for LRU and non-LRU handling Date: Thu, 10 Mar 2022 11:26:31 -0600 Message-ID: <20220310172633.9151-2-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220310172633.9151-1-alex.sierra@amd.com> References: <20220310172633.9151-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9949dd69-f48a-4bbc-c73e-08da02bb26a8 X-MS-TrafficTypeDiagnostic: MW2PR12MB2507:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CmaBAsRYPgZ7xi3MGYAKlDbisaaV2m8oZioMUL3DK+AoRwe0UV/yP+4bQzkGZ2XoB/iG7oAUPeCRt/+cB/nh4vMWB2qZc/0J8aNocUYUSpQAeEZ+E+P1vtaSnf8q3Fhi6Jk6nuHtOx4+1wbcYQtMUeeI9ImzHPSl5Us/wXUcOeFmLaZ5/7FpF8Z3OafICAfhLW6nSCP+V8ibMCnwCMNgEx0eSAEAXcAtDop6qV8E2z6qtd8DBW8RDUCm6B5KVhqSyIbCjijXiHNqjv3bRi5AssZy3gNP6GtGIJ7LCVN5aR8jWKet/kHZpo0ZL7+K2DhupS5GCMygOe6yC7ksSnRuHim+WI+xdWJx8nBfq7DlIEIszGxhBdfKmDFD2Y3u7ffQ87RwlKbd1mQJ3Aei4qS6k+PPJ+AnTjxt9X2jKBG0Lc8umfGNyPemUdzZiWsx9sGf4ExWloot/E9Imr3+bIS6voWcU0e6ZqlAjVQbqrTWIITaxUA+NVXUt1ooLwN2OUX+PWEfqCNT9p8/pAE5NVUyqWsQv39LfpO/XE+H5SHv8ZHZYRp0A2zX5b2yc1ZOybzsjpwE4kY4fzZPGvf4y0eFxrY9f+mwXYryLMQMVIgog+iZdOyXQmZsnSP08YgwxMWaKH/1MCyBG/6gv1RP0bEjTg2AWosKuxFpDI4lNcaUYHPuyC0geJtKMmRdzMzZFesK1DznmOkePQgDxA1v0KBHcQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(40470700004)(36840700001)(47076005)(16526019)(186003)(7416002)(2616005)(36860700001)(70586007)(36756003)(26005)(426003)(8936002)(4326008)(6916009)(86362001)(8676002)(70206006)(30864003)(316002)(336012)(5660300002)(54906003)(40460700003)(82310400004)(83380400001)(1076003)(508600001)(7696005)(2906002)(6666004)(356005)(44832011)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Mar 2022 17:26:45.5675 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9949dd69-f48a-4bbc-c73e-08da02bb26a8 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT010.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR12MB2507 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 347CA1A001A X-Stat-Signature: hnucyxje5acsb8rh8qryzb1r8skskfxm Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=4yf0XY8q; spf=pass (imf19.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.244.74 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-HE-Tag: 1646933209-668958 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: DEVICE_COHERENT pages introduce a subtle distinction in the way "normal" pages can be used by various callers throughout the kernel. They behave like normal pages for purposes of mapping in CPU page tables, and for COW. But they do not support LRU lists, NUMA migration or THP. Therefore we split vm_normal_page into two functions vm_normal_any_page and vm_normal_lru_page. The latter will only return pages that can be put on an LRU list and that support NUMA migration, KSM and THP. We also introduced a FOLL_LRU flag that adds the same behaviour to follow_page and related APIs, to allow callers to specify that they expect to put pages on an LRU list. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling --- fs/proc/task_mmu.c | 12 ++++++------ include/linux/mm.h | 11 +++++++---- mm/gup.c | 10 ++++++---- mm/hmm.c | 2 +- mm/huge_memory.c | 2 +- mm/khugepaged.c | 8 ++++---- mm/ksm.c | 4 ++-- mm/madvise.c | 4 ++-- mm/memcontrol.c | 2 +- mm/memory.c | 38 ++++++++++++++++++++++++++------------ mm/mempolicy.c | 4 ++-- mm/migrate.c | 2 +- mm/migrate_device.c | 2 +- mm/mlock.c | 6 +++--- mm/mprotect.c | 2 +- 15 files changed, 64 insertions(+), 45 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index f46060eb91b5..cada3e702693 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -528,7 +528,7 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr, bool migration = false; if (pte_present(*pte)) { - page = vm_normal_page(vma, addr, *pte); + page = vm_normal_any_page(vma, addr, *pte); } else if (is_swap_pte(*pte)) { swp_entry_t swpent = pte_to_swp_entry(*pte); @@ -723,7 +723,7 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, struct page *page = NULL; if (pte_present(*pte)) { - page = vm_normal_page(vma, addr, *pte); + page = vm_normal_any_page(vma, addr, *pte); } else if (is_swap_pte(*pte)) { swp_entry_t swpent = pte_to_swp_entry(*pte); @@ -1077,7 +1077,7 @@ static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, return false; if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) return false; - page = vm_normal_page(vma, addr, pte); + page = vm_normal_any_page(vma, addr, pte); if (!page) return false; return page_maybe_dma_pinned(page); @@ -1190,7 +1190,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, if (!pte_present(ptent)) continue; - page = vm_normal_page(vma, addr, ptent); + page = vm_normal_any_page(vma, addr, ptent); if (!page) continue; @@ -1402,7 +1402,7 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm, if (pm->show_pfn) frame = pte_pfn(pte); flags |= PM_PRESENT; - page = vm_normal_page(vma, addr, pte); + page = vm_normal_any_page(vma, addr, pte); if (pte_soft_dirty(pte)) flags |= PM_SOFT_DIRTY; if (pte_uffd_wp(pte)) @@ -1784,7 +1784,7 @@ static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, if (!pte_present(pte)) return NULL; - page = vm_normal_page(vma, addr, pte); + page = vm_normal_lru_page(vma, addr, pte); if (!page) return NULL; diff --git a/include/linux/mm.h b/include/linux/mm.h index d507c32724c0..46b0bb43cef3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -593,8 +593,8 @@ struct vm_operations_struct { unsigned long addr); #endif /* - * Called by vm_normal_page() for special PTEs to find the - * page for @addr. This is useful if the default behavior + * Called by vm_normal_*_page() for special PTEs to find the + * page for @addr. This is useful if the default behavior * (using pte_page()) would not find the correct page. */ struct page *(*find_special_page)(struct vm_area_struct *vma, @@ -1781,7 +1781,9 @@ static inline bool can_do_mlock(void) { return false; } extern int user_shm_lock(size_t, struct ucounts *); extern void user_shm_unlock(size_t, struct ucounts *); -struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, +struct page *vm_normal_any_page(struct vm_area_struct *vma, unsigned long addr, + pte_t pte); +struct page *vm_normal_lru_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte); struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, pmd_t pmd); @@ -2901,6 +2903,7 @@ struct page *follow_page(struct vm_area_struct *vma, unsigned long address, #define FOLL_SPLIT_PMD 0x20000 /* split huge pmd before returning */ #define FOLL_PIN 0x40000 /* pages must be released via unpin_user_page */ #define FOLL_FAST_ONLY 0x80000 /* gup_fast: prevent fall-back to slow gup */ +#define FOLL_LRU 0x100000 /* return only LRU (anon or page cache) */ /* * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each @@ -3227,7 +3230,7 @@ extern long copy_huge_page_from_user(struct page *dst_page, * @vma: Pointer to the struct vm_area_struct to consider * * Whether transhuge page-table entries are considered "special" following - * the definition in vm_normal_page(). + * the definition in vm_normal_*_page(). * * Return: true if transhuge page-table entries should be considered special, * false otherwise. diff --git a/mm/gup.c b/mm/gup.c index 41349b685eaf..0ed597143d80 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -539,8 +539,10 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, pte_unmap_unlock(ptep, ptl); return NULL; } - - page = vm_normal_page(vma, address, pte); + if (flags & (FOLL_MLOCK | FOLL_LRU)) + page = vm_normal_lru_page(vma, address, pte); + else + page = vm_normal_any_page(vma, address, pte); if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { /* * Only return device mapping pages in the FOLL_GET or FOLL_PIN @@ -824,7 +826,7 @@ static struct page *follow_p4d_mask(struct vm_area_struct *vma, * * Return: the mapped (struct page *), %NULL if no mapping exists, or * an error pointer if there is a mapping to something not represented - * by a page descriptor (see also vm_normal_page()). + * by a page descriptor (see also vm_normal_*_page()). */ static struct page *follow_page_mask(struct vm_area_struct *vma, unsigned long address, unsigned int flags, @@ -917,7 +919,7 @@ static int get_gate_page(struct mm_struct *mm, unsigned long address, *vma = get_gate_vma(mm); if (!page) goto out; - *page = vm_normal_page(*vma, address, *pte); + *page = vm_normal_any_page(*vma, address, *pte); if (!*page) { if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) goto unmap; diff --git a/mm/hmm.c b/mm/hmm.c index bd56641c79d4..90c949d66712 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -300,7 +300,7 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, * Since each architecture defines a struct page for the zero page, just * fall through and treat it like a normal page. */ - if (!vm_normal_page(walk->vma, addr, pte) && + if (!vm_normal_any_page(walk->vma, addr, pte) && !pte_devmap(pte) && !is_zero_pfn(pte_pfn(pte))) { if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 406a3c28c026..ea1efc825774 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2966,7 +2966,7 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, } /* FOLL_DUMP to ignore special (like zero) pages */ - follflags = FOLL_GET | FOLL_DUMP; + follflags = FOLL_GET | FOLL_DUMP | FOLL_LRU; page = follow_page(vma, addr, follflags); if (IS_ERR(page)) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 131492fd1148..a7153db09afa 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -627,7 +627,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, result = SCAN_PTE_NON_PRESENT; goto out; } - page = vm_normal_page(vma, address, pteval); + page = vm_normal_lru_page(vma, address, pteval); if (unlikely(!page)) { result = SCAN_PAGE_NULL; goto out; @@ -1286,7 +1286,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, if (pte_write(pteval)) writable = true; - page = vm_normal_page(vma, _address, pteval); + page = vm_normal_lru_page(vma, _address, pteval); if (unlikely(!page)) { result = SCAN_PAGE_NULL; goto out_unmap; @@ -1494,7 +1494,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) if (!pte_present(*pte)) goto abort; - page = vm_normal_page(vma, addr, *pte); + page = vm_normal_lru_page(vma, addr, *pte); /* * Note that uprobe, debugger, or MAP_PRIVATE may change the @@ -1512,7 +1512,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) if (pte_none(*pte)) continue; - page = vm_normal_page(vma, addr, *pte); + page = vm_normal_lru_page(vma, addr, *pte); page_remove_rmap(page, false); } diff --git a/mm/ksm.c b/mm/ksm.c index c20bd4d9a0d9..352d37e44694 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -474,7 +474,7 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr) do { cond_resched(); page = follow_page(vma, addr, - FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE); + FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE | FOLL_LRU); if (IS_ERR_OR_NULL(page)) break; if (PageKsm(page)) @@ -559,7 +559,7 @@ static struct page *get_mergeable_page(struct rmap_item *rmap_item) if (!vma) goto out; - page = follow_page(vma, addr, FOLL_GET); + page = follow_page(vma, addr, FOLL_GET | FOLL_LRU); if (IS_ERR_OR_NULL(page)) goto out; if (PageAnon(page)) { diff --git a/mm/madvise.c b/mm/madvise.c index 38d0f515d548..8dcfb93fe0d5 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -411,7 +411,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, if (!pte_present(ptent)) continue; - page = vm_normal_page(vma, addr, ptent); + page = vm_normal_lru_page(vma, addr, ptent); if (!page) continue; @@ -621,7 +621,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, continue; } - page = vm_normal_page(vma, addr, ptent); + page = vm_normal_lru_page(vma, addr, ptent); if (!page) continue; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index d9cd8f2afa3c..1d791760608b 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5476,7 +5476,7 @@ enum mc_target_type { static struct page *mc_handle_present_pte(struct vm_area_struct *vma, unsigned long addr, pte_t ptent) { - struct page *page = vm_normal_page(vma, addr, ptent); + struct page *page = vm_normal_any_page(vma, addr, ptent); if (!page || !page_mapped(page)) return NULL; diff --git a/mm/memory.c b/mm/memory.c index c125c4969913..1422b1f71f0e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -565,7 +565,7 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, } /* - * vm_normal_page -- This function gets the "struct page" associated with a pte. + * vm_normal_any_page -- This function gets the "struct page" associated with a pte. * * "Special" mappings do not wish to be associated with a "struct page" (either * it doesn't exist, or it exists but they don't want to touch it). In this @@ -606,7 +606,7 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, * PFNMAP mappings in order to support COWable mappings. * */ -struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, +struct page *vm_normal_any_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte) { unsigned long pfn = pte_pfn(pte); @@ -620,8 +620,6 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, return NULL; if (is_zero_pfn(pfn)) return NULL; - if (pte_devmap(pte)) - return NULL; print_bad_pte(vma, addr, pte, NULL); return NULL; @@ -661,6 +659,22 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, return pfn_to_page(pfn); } +/* + * vm_normal_lru_page -- This function gets the "struct page" associated + * with a pte only for page cache and anon page. These pages are LRU handled. + */ +struct page *vm_normal_lru_page(struct vm_area_struct *vma, unsigned long addr, + pte_t pte) +{ + struct page *page; + + page = vm_normal_any_page(vma, addr, pte); + if (page && is_zone_device_page(page)) + return NULL; + + return page; +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, pmd_t pmd) @@ -670,7 +684,7 @@ struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, /* * There is no pmd_special() but there may be special pmds, e.g. * in a direct-access (dax) mapping, so let's just replicate the - * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. + * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_any_page() here. */ if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { if (vma->vm_flags & VM_MIXEDMAP) { @@ -946,7 +960,7 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, pte_t pte = *src_pte; struct page *page; - page = vm_normal_page(src_vma, addr, pte); + page = vm_normal_any_page(src_vma, addr, pte); if (page) { int retval; @@ -1358,7 +1372,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, if (pte_present(ptent)) { struct page *page; - page = vm_normal_page(vma, addr, ptent); + page = vm_normal_any_page(vma, addr, ptent); if (unlikely(zap_skip_check_mapping(details, page))) continue; ptent = ptep_get_and_clear_full(mm, addr, pte, @@ -2168,7 +2182,7 @@ EXPORT_SYMBOL(vmf_insert_pfn); static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) { - /* these checks mirror the abort conditions in vm_normal_page */ + /* these checks mirror the abort conditions in vm_normal_lru_page */ if (vma->vm_flags & VM_MIXEDMAP) return true; if (pfn_t_devmap(pfn)) @@ -2198,7 +2212,7 @@ static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, /* * If we don't have pte special, then we have to use the pfn_valid() - * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* + * based VM_MIXEDMAP scheme (see vm_normal_any_page), and thus we *must* * refcount the page if pfn_valid is true (hence insert_page rather * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP * without pte special, it would there be refcounted as a normal page. @@ -2408,7 +2422,7 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, * There's a horrible special case to handle copy-on-write * behaviour that some programs depend on. We mark the "original" * un-COW'ed pages by matching them up with "vma->vm_pgoff". - * See vm_normal_page() for details. + * See vm_normal_any_page() for details. */ if (is_cow_mapping(vma->vm_flags)) { if (addr != vma->vm_start || end != vma->vm_end) @@ -3267,7 +3281,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) mm_tlb_flush_pending(vmf->vma->vm_mm))) flush_tlb_page(vmf->vma, vmf->address); - vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); + vmf->page = vm_normal_any_page(vma, vmf->address, vmf->orig_pte); if (!vmf->page) { /* * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a @@ -4364,7 +4378,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) old_pte = ptep_get(vmf->pte); pte = pte_modify(old_pte, vma->vm_page_prot); - page = vm_normal_page(vma, vmf->address, pte); + page = vm_normal_lru_page(vma, vmf->address, pte); if (!page) goto out_map; diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 69284d3b5e53..651408f14b3e 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -527,11 +527,11 @@ static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr, for (; addr != end; pte++, addr += PAGE_SIZE) { if (!pte_present(*pte)) continue; - page = vm_normal_page(vma, addr, *pte); + page = vm_normal_lru_page(vma, addr, *pte); if (!page) continue; /* - * vm_normal_page() filters out zero pages, but there might + * vm_normal_lru_page() filters out zero pages, but there might * still be PageReserved pages to skip, perhaps in a VDSO. */ if (PageReserved(page)) diff --git a/mm/migrate.c b/mm/migrate.c index c31d04b46a5e..17d049311b78 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1614,7 +1614,7 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, goto out; /* FOLL_DUMP to ignore special (like zero) pages */ - follflags = FOLL_GET | FOLL_DUMP; + follflags = FOLL_GET | FOLL_DUMP | FOLL_LRU; page = follow_page(vma, addr, follflags); err = PTR_ERR(page); diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 3373b535d5c9..fac1b6978361 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -154,7 +154,7 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, migrate->cpages++; goto next; } - page = vm_normal_page(migrate->vma, addr, pte); + page = vm_normal_any_page(migrate->vma, addr, pte); if (page && !is_zone_device_page(page) && !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) goto next; diff --git a/mm/mlock.c b/mm/mlock.c index 25934e7db3e1..bb09926aeee7 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -342,7 +342,7 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone) * a non-TPH page already pinned and in the @pvec, and that it belongs to @zone. * * The rest of @pvec is filled by subsequent pages within the same pmd and same - * zone, as long as the pte's are present and vm_normal_page() succeeds. These + * zone, as long as the pte's are present and vm_normal_lru_page() succeeds. These * pages also get pinned. * * Returns the address of the next page that should be scanned. This equals @@ -373,7 +373,7 @@ static unsigned long __munlock_pagevec_fill(struct pagevec *pvec, struct page *page = NULL; pte++; if (pte_present(*pte)) - page = vm_normal_page(vma, start, *pte); + page = vm_normal_lru_page(vma, start, *pte); /* * Break if page could not be obtained or the page's node+zone does not * match @@ -439,7 +439,7 @@ void munlock_vma_pages_range(struct vm_area_struct *vma, * suits munlock very well (and if somehow an abnormal page * has sneaked into the range, we won't oops here: great). */ - page = follow_page(vma, start, FOLL_GET | FOLL_DUMP); + page = follow_page(vma, start, FOLL_GET | FOLL_DUMP | FOLL_LRU); if (page && !IS_ERR(page)) { if (PageTransTail(page)) { diff --git a/mm/mprotect.c b/mm/mprotect.c index 2887644fd150..bc3e75334aeb 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -88,7 +88,7 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, if (pte_protnone(oldpte)) continue; - page = vm_normal_page(vma, addr, oldpte); + page = vm_normal_lru_page(vma, addr, oldpte); if (!page || PageKsm(page)) continue; From patchwork Thu Mar 10 17:26:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12776792 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7524C433FE for ; Thu, 10 Mar 2022 17:26:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 362498D0003; Thu, 10 Mar 2022 12:26:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2EACF8D0001; Thu, 10 Mar 2022 12:26:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0A0188D0003; Thu, 10 Mar 2022 12:26:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id E27328D0001 for ; Thu, 10 Mar 2022 12:26:49 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id BA60581A4C for ; Thu, 10 Mar 2022 17:26:49 +0000 (UTC) X-FDA: 79229156538.12.AC2B7CB Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2046.outbound.protection.outlook.com [40.107.94.46]) by imf01.hostedemail.com (Postfix) with ESMTP id 1035B4001E for ; Thu, 10 Mar 2022 17:26:48 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RdNV13xn8P6SHj1yUq3vAI8ZTay3rclqFZtAju3PUBwS9kqMdvvL80Mo9wVuL1QHJ19IZ+A2G874cWU7CSTfegf5lTzfcjeNFbWrF9/iQ92Lwksiczu9noFYqfHbxi8TmgwC5XxwHeHxg5t6vpjJ8IvAmulWL1ZENAfm89ToCtkaHUt0Gj0UCrSW7WBoNhA1kmex7gnzugybdbbGU0l2BdXITiVsP6KwUKsiYkYinLrqy+RFQ4RgCiYU6RMbQ+Xc5g3ty1FhJTKkcBo7bzbtqtc+k54dWt3Fj7UgMeoxcSVhjA8dc9F9jwgcw75BAxX0bOXpzLUP371lY1nMAp7GUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=GHU7YhU+SJ2J7kH8Bg29HQQ9+RIQsN1DFo8UaEiYdGg=; b=F2EZQaDGBaGLNZLri1J0MUvtC9yXlUl/rYMh06BNbPyyDvha0qug4ZYWkHDs202mf4PlHQwWghpaExf1VZdglVzkUxF+2qeXnZm2ZpCv2AissqPwPq7vNt1Bv+pVIhEfkR1nODh0IBZkVsNpJVpPGlJ9g0abfVuMiGmNwwyN8o+DvHLfhD1z2jhFllPtQsgvequDED/0wR7tV98qrwXLnOks0poe1TUPgO0ZlFWNCZ9LKOSIkwb9x9Z1rmEzWnvAxc9aRGP/EiYXlex8f9VBMGtJFGyUaErSLAaVqUgsoi/mWs/pGlgfdQebINn0dqbWfILImZIxNfq7mAV19V6xgQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GHU7YhU+SJ2J7kH8Bg29HQQ9+RIQsN1DFo8UaEiYdGg=; b=FDgVa9nGuts7Yztb1A9ktwizeKFQD4b3o6LgUGMrlYougNpKfw+LszcXjPAXFmeYUljvzNBRbPkVCtTFYkooRsKBeqYOm04lREl3EPuKJlndzsgjkYhS5DIO3cwvk1PAnfXcgGhz03HcPnVASpeUj1rwDFzPRQ3Bj7U+HR1FQu8= Received: from DM3PR11CA0012.namprd11.prod.outlook.com (2603:10b6:0:54::22) by BYAPR12MB4982.namprd12.prod.outlook.com (2603:10b6:a03:106::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.17; Thu, 10 Mar 2022 17:26:46 +0000 Received: from DM6NAM11FT010.eop-nam11.prod.protection.outlook.com (2603:10b6:0:54:cafe::db) by DM3PR11CA0012.outlook.office365.com (2603:10b6:0:54::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.22 via Frontend Transport; Thu, 10 Mar 2022 17:26:46 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT010.mail.protection.outlook.com (10.13.172.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5061.22 via Frontend Transport; Thu, 10 Mar 2022 17:26:46 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Thu, 10 Mar 2022 11:26:45 -0600 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v1 2/3] tools: add more gup configs to hmm_gup selftests Date: Thu, 10 Mar 2022 11:26:32 -0600 Message-ID: <20220310172633.9151-3-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220310172633.9151-1-alex.sierra@amd.com> References: <20220310172633.9151-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7edfc9fe-5d1b-481a-0d46-08da02bb2733 X-MS-TrafficTypeDiagnostic: BYAPR12MB4982:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9VHFhXQVTro1PivJPVHcG2Fhdh66kxS4HpoSgNTIv6UAmCHlgMHtX+DcFIjLpYhk0d10CIb+u/DtnqroJszg0kt2ZNrQ2u3zkqPpSM0ZaG/j/MrNJ24NssydSaSAgW6Lb8hb3OM9K7QuagLSKKDdKssHzT77exOj9RX+CqiDQguqy2WhUMpzRlKkQBlf/KrH83QOo6qpKKF5eJabmvqq9W14lBo9rOGML7xZ9guW6hV0inTngnNFEzqdH87ZGrVH1MUdr1lwGo1968tc7UdohXcQZXrGSFfpvKlq+l0qwvH5ODcur3YVMZCiTIqZ7OyewiFyCcPDpiZ6ZhJ54H00nrHtHXYCXSOK1Snwa7SHffxUB3l5RYI2BEQTjOoHVza6lNjwAyIEvMaho+6MOB08NcctafV1QBqQYWKm5ByxlqVTz3hBqYa0O7XGG3ujJ7qfZReWl1aYILwe+AdzKA5NwYZdVwj5kx9n+38kUEyBlssroD779CnM4PzxD+oQeeUYvYu+Xj8dLQpqJmrySFjFVKuz0nwDTQn7ihTmjFkLoXKHXnJnz6CBiR2suXKKVFd8JQjymqSFW2lep59t8KO6JkGVzSE0Odknwh+dVlllFnyav73V8MKfRsSkzDBqrR88CechEJdIBAvv1hICr0Zjpvy5NILHHNQ8DDyGWWAvb24x/OZ7IboV83N3sLJ3E3tZX6gjHbCPT42faL1t7r4PSg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(36840700001)(46966006)(40470700004)(2616005)(82310400004)(81166007)(508600001)(186003)(26005)(8676002)(70206006)(16526019)(70586007)(6666004)(356005)(86362001)(44832011)(5660300002)(47076005)(7696005)(36860700001)(83380400001)(4326008)(36756003)(8936002)(316002)(6916009)(54906003)(7416002)(426003)(336012)(1076003)(2906002)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Mar 2022 17:26:46.4737 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7edfc9fe-5d1b-481a-0d46-08da02bb2733 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT010.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB4982 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 1035B4001E X-Rspam-User: Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=FDgVa9nG; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf01.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.94.46 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Stat-Signature: 9jaefuaxi6qjm3xf4stu69um1c4bc839 X-HE-Tag: 1646933208-799211 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: Test device pages with get_user_pages and get_user_pages_fast. The motivation is to test device coherent type pages in the gup and gup fast paths, after vm_normal_pages was split into LRU and non-LRU handled. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling --- tools/testing/selftests/vm/hmm-tests.c | 65 +++++++++++++++++--------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 11b83a8084fe..65e30ab6494c 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -1769,6 +1769,24 @@ TEST_F(hmm, exclusive_cow) hmm_buffer_free(buffer); } +static int gup_test_exec(int gup_fd, unsigned long addr, + int cmd, int npages, int size) +{ + struct gup_test gup = { + .nr_pages_per_call = npages, + .addr = addr, + .gup_flags = FOLL_WRITE, + .size = size, + }; + + if (ioctl(gup_fd, cmd, &gup)) { + perror("ioctl on error\n"); + return errno; + } + + return 0; +} + /* * Test get user device pages through gup_test. Setting PIN_LONGTERM flag. * This should trigger a migration back to system memory for both, private @@ -1779,7 +1797,6 @@ TEST_F(hmm, exclusive_cow) TEST_F(hmm, hmm_gup_test) { struct hmm_buffer *buffer; - struct gup_test gup; int gup_fd; unsigned long npages; unsigned long size; @@ -1792,8 +1809,7 @@ TEST_F(hmm, hmm_gup_test) if (gup_fd == -1) SKIP(return, "Skipping test, could not find gup_test driver"); - npages = 4; - ASSERT_NE(npages, 0); + npages = 3; size = npages << self->page_shift; buffer = malloc(sizeof(*buffer)); @@ -1822,28 +1838,35 @@ TEST_F(hmm, hmm_gup_test) for (i = 0, ptr = buffer->mirror; i < size / sizeof(*ptr); ++i) ASSERT_EQ(ptr[i], i); - gup.nr_pages_per_call = npages; - gup.addr = (unsigned long)buffer->ptr; - gup.gup_flags = FOLL_WRITE; - gup.size = size; - /* - * Calling gup_test ioctl. It will try to PIN_LONGTERM these device pages - * causing a migration back to system memory for both, private and coherent - * type pages. - */ - if (ioctl(gup_fd, PIN_LONGTERM_BENCHMARK, &gup)) { - perror("ioctl on PIN_LONGTERM_BENCHMARK\n"); - goto out_test; - } - - /* Take snapshot to make sure pages have been migrated to sys memory */ + ASSERT_EQ(gup_test_exec(gup_fd, + (unsigned long)buffer->ptr, + GUP_BASIC_TEST, 1, self->page_size), 0); + ASSERT_EQ(gup_test_exec(gup_fd, + (unsigned long)buffer->ptr + 1 * self->page_size, + GUP_FAST_BENCHMARK, 1, self->page_size), 0); + ASSERT_EQ(gup_test_exec(gup_fd, + (unsigned long)buffer->ptr + 2 * self->page_size, + PIN_LONGTERM_BENCHMARK, 1, self->page_size), 0); + + /* Take snapshot to CPU pagetables */ ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_SNAPSHOT, buffer, npages); ASSERT_EQ(ret, 0); ASSERT_EQ(buffer->cpages, npages); m = buffer->mirror; - for (i = 0; i < npages; i++) - ASSERT_EQ(m[i], HMM_DMIRROR_PROT_WRITE); -out_test: + if (hmm_is_coherent_type(variant->device_number)) { + ASSERT_EQ(HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL | HMM_DMIRROR_PROT_WRITE, m[0]); + ASSERT_EQ(HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL | HMM_DMIRROR_PROT_WRITE, m[1]); + } else { + ASSERT_EQ(HMM_DMIRROR_PROT_WRITE, m[0]); + ASSERT_EQ(HMM_DMIRROR_PROT_WRITE, m[1]); + } + ASSERT_EQ(HMM_DMIRROR_PROT_WRITE, m[2]); + /* Check again the content on the pages. Make sure there's no + * corrupted data. + */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ASSERT_EQ(ptr[i], i); + close(gup_fd); hmm_buffer_free(buffer); } From patchwork Thu Mar 10 17:26:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12776793 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C2C9C433F5 for ; Thu, 10 Mar 2022 17:26:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A423C8D0006; Thu, 10 Mar 2022 12:26:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A0C98D0005; Thu, 10 Mar 2022 12:26:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CC398D0001; Thu, 10 Mar 2022 12:26:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0006.hostedemail.com [216.40.44.6]) by kanga.kvack.org (Postfix) with ESMTP id 40EFE8D0005 for ; Thu, 10 Mar 2022 12:26:50 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id E7CC0181C9906 for ; Thu, 10 Mar 2022 17:26:49 +0000 (UTC) X-FDA: 79229156538.28.80D125C Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2071.outbound.protection.outlook.com [40.107.92.71]) by imf27.hostedemail.com (Postfix) with ESMTP id 4BE084001E for ; Thu, 10 Mar 2022 17:26:49 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OFEBKSQFyqeRHtai7awNAGQ/SGHMOflQITcG1gx7HMlk00BPX/qmMs0LnVSrXh0dFppweNV7/AF9CIh8haJbD9zZONveuFkoPW2kxW69CviKutowrYWHyuGA0xuSeZs6O2ui9gOTl2G1srovSxBWjkmDuuctxdJgbUKmGKvkOoyKS155kOr9RGIQJBY1hT4PZ1nsrkUObQD5M7PaWU3zMNjNrTheX6sVtufFY5zIlHLCFde3dtjWchzWzP/eduNUSW5aXYEs2QSObLsZH93vjDyicQl63JOLDnGNbW5t2BKYm6drBrESypQj8AOuB58dDFEWHWnc2lE6CgUR2LgxfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=+v+1UPaPthHsygapXxuY4sqVDC3cvlVfCyGWQ0h08FA=; b=n1iy540S9M4XzPyVHItLtr5MFznLBd728lGCGmIiIDjXuI/oo/fT9ua4sHUPIN9WU1Ep34iwI12K+8VAbX4AuSoj7xp9fJvgUb0ATfm+6FlyuKSkpQ+SmRVVgdMt1xtTyN8p6o9ZKxlK9mKAMH+XGELMYs+OLi8BuTu8vv1ne9645GkSnBOIOrJZYYyWaXYSS6scsFSNquvZvt2+HtwCFdpigHWJWHmZ4CAiUdH6im0NpV1GiB5RJumleqhfXEK2lL938L/QvwiNDfaQMxGWt7W0aGcBF5EQN1xy3r7qQmMv+82utCJbIzafe3jBMyJTtEzLZLx5J4R7nIZ2Rz20tg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+v+1UPaPthHsygapXxuY4sqVDC3cvlVfCyGWQ0h08FA=; b=NNmB470gb/CYX90FK4yEbhGDNmtUDwjqkl72EgETLlLCQ/6xDMUzK5iY8SW6q5Bi2eUl99mubFJbJdNdoM9w+c7Fat/ZG5++cL0h5oCxuz9HbrisKngmZ/i3IPq0DHF+mbwg4j910rEkooHMTDc+vajAFkQuS9sh6sLGWukc6F8= Received: from DM3PR11CA0019.namprd11.prod.outlook.com (2603:10b6:0:54::29) by BL1PR12MB5141.namprd12.prod.outlook.com (2603:10b6:208:309::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.14; Thu, 10 Mar 2022 17:26:47 +0000 Received: from DM6NAM11FT010.eop-nam11.prod.protection.outlook.com (2603:10b6:0:54:cafe::da) by DM3PR11CA0019.outlook.office365.com (2603:10b6:0:54::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.27 via Frontend Transport; Thu, 10 Mar 2022 17:26:47 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT010.mail.protection.outlook.com (10.13.172.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5061.22 via Frontend Transport; Thu, 10 Mar 2022 17:26:47 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Thu, 10 Mar 2022 11:26:45 -0600 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v1 3/3] tools: add selftests to hmm for COW in device memory Date: Thu, 10 Mar 2022 11:26:33 -0600 Message-ID: <20220310172633.9151-4-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220310172633.9151-1-alex.sierra@amd.com> References: <20220310172633.9151-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 85354b9d-0445-47ff-a513-08da02bb278d X-MS-TrafficTypeDiagnostic: BL1PR12MB5141:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NCbRBKF8uJrOBI8LouOXjRxOdyXZMhchPVjs8D+xtAq+7pBrN92E8HVOh1idHSDDnASLZlwyrx884SnsUFAVb3I2F/wplQtQx/aIQcAVN6g1dZ0iMGoHxxRt0pMjlB2t3IyYscBrb9QjNmr+POap6/+aKa8kD8RUy1HbtZQZC9lhZfde95b8PCz+Kh+Vk1AnLrN2zDRQad6fH9hys/S1Jus+cRzk3j4QQHCl+VsUFElnzS/DmqKt/SBQYAPgZVybdxX5IVjPEfKo8iXIwZA34pE9xVeAEwpIDp74lodgsrz5zS7JBugcJynpHFtqlYpTIr45aFc9Wsalptbt28LZ9L0+jeY89nKj2Semp2eOIykwi3iPq6qG4vOMz/pXLOsH2dDeL9Zp73+jc+/b5sn2/e+cULzj7Ai1UWHYD53QUjNCsKw2WJ68nI5kdOX1UIdmwLNznB3JpJX/7GBYFTpaNvdqbTXL1vYhAma5RNGb9DR722ER+iodnCdsoWgPRcZTxHkMG5/i8y21rcyfD+hnhoWlYpWihdy399WtuUluqYB+dhVUdoRH+comEnnFEpyp8HE6lybrtG94lrMtbpijjS9O4r4HHRJE9ToFEt//p9lnZxnUmiqkTMH/VGB3AmXxclyjOPkcV3vO6KaYOhUH2E0xykJR1wTmgJnBBTsV9J7yUdel5SWAT0sLj2TI98FSMuDc0I83RWDRP2pfcIBXgw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(36840700001)(46966006)(40470700004)(5660300002)(47076005)(7416002)(2616005)(6666004)(40460700003)(83380400001)(1076003)(26005)(2906002)(8936002)(82310400004)(44832011)(7696005)(336012)(426003)(16526019)(186003)(70206006)(36756003)(4326008)(81166007)(6916009)(70586007)(54906003)(356005)(316002)(86362001)(8676002)(508600001)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Mar 2022 17:26:47.0830 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 85354b9d-0445-47ff-a513-08da02bb278d X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT010.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5141 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 4BE084001E X-Stat-Signature: a7xe6tfqdk1jndz64zz6ob6mm5pi5rxo Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=NNmB470g; spf=pass (imf27.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.92.71 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-HE-Tag: 1646933209-119109 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: The objective is to test device migration mechanism in pages marked as COW, for private and coherent device type. In case of writing to COW private page(s), a page fault will migrate pages back to system memory first. Then, these pages will be duplicated. In case of COW device coherent type, pages are duplicated directly from device memory. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling --- tools/testing/selftests/vm/hmm-tests.c | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 65e30ab6494c..d70b780df877 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -1870,4 +1870,84 @@ TEST_F(hmm, hmm_gup_test) close(gup_fd); hmm_buffer_free(buffer); } + +/* + * Test copy-on-write in device pages. + * In case of writing to COW private page(s), a page fault will migrate pages + * back to system memory first. Then, these pages will be duplicated. In case + * of COW device coherent type, pages are duplicated directly from device + * memory. + */ +TEST_F(hmm, hmm_cow_in_device) +{ + struct hmm_buffer *buffer; + unsigned long npages; + unsigned long size; + unsigned long i; + int *ptr; + int ret; + unsigned char *m; + pid_t pid; + int status; + + npages = 4; + size = npages << self->page_shift; + + buffer = malloc(sizeof(*buffer)); + ASSERT_NE(buffer, NULL); + + buffer->fd = -1; + buffer->size = size; + buffer->mirror = malloc(size); + ASSERT_NE(buffer->mirror, NULL); + + buffer->ptr = mmap(NULL, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, + buffer->fd, 0); + ASSERT_NE(buffer->ptr, MAP_FAILED); + + /* Initialize buffer in system memory. */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ptr[i] = i; + + /* Migrate memory to device. */ + + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + + pid = fork(); + if (pid == -1) + ASSERT_EQ(pid, 0); + if (!pid) { + /* Child process waitd for SIGTERM from the parent. */ + while (1) { + } + perror("Should not reach this\n"); + exit(0); + } + /* Parent process writes to COW pages(s) and gets a + * new copy in system. In case of device private pages, + * this write causes a migration to system mem first. + */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ptr[i] = i; + + /* Terminate child and wait */ + EXPECT_EQ(0, kill(pid, SIGTERM)); + EXPECT_EQ(pid, waitpid(pid, &status, 0)); + EXPECT_NE(0, WIFSIGNALED(status)); + EXPECT_EQ(SIGTERM, WTERMSIG(status)); + + /* Take snapshot to CPU pagetables */ + ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_SNAPSHOT, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + m = buffer->mirror; + for (i = 0; i < npages; i++) + ASSERT_EQ(HMM_DMIRROR_PROT_WRITE, m[i]); + + hmm_buffer_free(buffer); +} TEST_HARNESS_MAIN