Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1480425pxu; Thu, 17 Dec 2020 10:56:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJxw6uPzssEdsYwQmM0Tk1gEjbFN7ow+OxQkVohy6VFu1OBthVYang4nWckKYOlWUpNTsW7o X-Received: by 2002:a17:906:a3c7:: with SMTP id ca7mr365820ejb.523.1608231378481; Thu, 17 Dec 2020 10:56:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608231378; cv=none; d=google.com; s=arc-20160816; b=nsIxrCxX68EU+TmQSHNVYYEmN551aecTHz1XgsOx/S0vEs6Qblc6KPnv32L8A4OJsN YSjB9bSRsHbha9l82TxvKxWStAwlte1tY+OR3Xr42uOkFp4bD+PCCFvrVPmodEtJUSC2 X/2HNVq/oXKhbfzjdOCrGZTcU2oCh42EUd5ySeFzicaPKSf/865fp8eityIcaaiNIc1/ hTKg4KHLgpGJmOddifbq2XkB6e83oaDVSTI6e5c52QRd1wQiXLJaFyQ+E/H5jrh47Hby Mdz9yoj6oi1vKsLO97Tz2KB6q0PuM0vvpCGA6pcTFiEzntS1w3Nu0BPLkcVJK55yPe4f qhqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=e6Ncu8c/WYeWoMyVk5R0b06plBGbLPujuz6OQkTIMmE=; b=SH/ado1+g8m9H7QKO9Cw4zLZ0m1Tidnx63fmKft0ttAjYd3rGCX/k+49Ybbx0rhdWS 57MCgSAKqulJTxDxmdcfEx/cUWYCZipHZuhvOkgvE0NGHZMODNWAWOrQbw2oSqEewMBH Fgcj3jL5WsWs3bi/P26RYecx+1DyL3kpwjAsfwXtkqqwbuPFxDcTKbhOkdyEWvXwmz0g BGwrmk5QvjB+kq66wI4hweH3hfZxlO1IDHsUu0eXvSjNPWhpuPtGq9Sl/okxr+/24X14 I+bQEyhxXB0WVlvVWmhoZtBy5gaGcoDyB2N7KeyDUVqXG6tD3VzFCNUiz7SGFPpq4MSg nmNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b=eLX1jgZ7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v7si6558743edj.402.2020.12.17.10.55.55; Thu, 17 Dec 2020 10:56:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b=eLX1jgZ7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730872AbgLQSy0 (ORCPT + 99 others); Thu, 17 Dec 2020 13:54:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729613AbgLQSyZ (ORCPT ); Thu, 17 Dec 2020 13:54:25 -0500 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09E12C0611BB for ; Thu, 17 Dec 2020 10:52:57 -0800 (PST) Received: by mail-qv1-xf31.google.com with SMTP id u16so13789579qvl.7 for ; Thu, 17 Dec 2020 10:52:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=e6Ncu8c/WYeWoMyVk5R0b06plBGbLPujuz6OQkTIMmE=; b=eLX1jgZ7IuyDcsgovsbreDFQXA2MaJfYs3hIyomcThfex/wvVW3Z/nmVUoxDR5TyL0 t64YpRkFt3uba3f5F+NwIC0ERE+2Gw7ozN14LqGUNeHrLCMDnWSDA6YpTktNvpTF9K4g vkiaxpQsOTamjUV9fvIwE8wZJE3p+VMDVLxV4/kwJIJb6LaeSUoF5NhEsp/frRaHbNV0 9PGijeoxePmSMQQU0VTKb/ja5SAqjUMdxN06aeaRgeKE9Nx5C+i3HPJNvFub/pKVPzWs VFzdBSXFjFUL2BFh+DoPdA8+Ii1KvTE2a92B0fh+cVnglUzvugVbpqHty2bwO3YLaZzH 8D8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e6Ncu8c/WYeWoMyVk5R0b06plBGbLPujuz6OQkTIMmE=; b=KIbKEvMQUh3vO3gnLKVgQ7v1AAetzdZ4y8Azx6tNCklDIfj1l/4taItyMd9EuZxXnX qd586NcY+7RCIUd8V6L1TQ41bY6B1YilEoidb/n7jGW9HVrwB9+P5u3v8X2d3T1HMmxT 5vr7+SrIzwhgWMMgGFs9hG4OH+TjTBFHX+Y/TnnOnoT3SQ0Du660MCxmepO1/l+zmTv0 uH/e1Yt19rnwwxfEvA2cZDQ2zr/CJy9o7qZF8A7tKMCZbX6fdKPZiqNWnnaY+orsfRKy ODrsshHt3N+GN6Kq05K8G3eVkzVfTxggg/d+t6CgMp6dDv/YuHP7ANvmY0qkQniumMtS s4ww== X-Gm-Message-State: AOAM531C9bFvhW87vdx7iH2EoI0Whudy6RUXIxylTtN6B8T4lSaG7aDT cVwIhBJd0yncmMlQ5JE3J0M/7w== X-Received: by 2002:a0c:a366:: with SMTP id u93mr626563qvu.53.1608231176250; Thu, 17 Dec 2020 10:52:56 -0800 (PST) Received: from localhost.localdomain (c-73-69-118-222.hsd1.nh.comcast.net. [73.69.118.222]) by smtp.gmail.com with ESMTPSA id m8sm4127434qkn.41.2020.12.17.10.52.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Dec 2020 10:52:55 -0800 (PST) From: Pavel Tatashin To: pasha.tatashin@soleen.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, vbabka@suse.cz, mhocko@suse.com, david@redhat.com, osalvador@suse.de, dan.j.williams@intel.com, sashal@kernel.org, tyhicks@linux.microsoft.com, iamjoonsoo.kim@lge.com, mike.kravetz@oracle.com, rostedt@goodmis.org, mingo@redhat.com, jgg@ziepe.ca, peterz@infradead.org, mgorman@suse.de, willy@infradead.org, rientjes@google.com, jhubbard@nvidia.com, linux-doc@vger.kernel.org, ira.weiny@intel.com, linux-kselftest@vger.kernel.org Subject: [PATCH v4 05/10] mm/gup: migrate pinned pages out of movable zone Date: Thu, 17 Dec 2020 13:52:38 -0500 Message-Id: <20201217185243.3288048-6-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201217185243.3288048-1-pasha.tatashin@soleen.com> References: <20201217185243.3288048-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We should not pin pages in ZONE_MOVABLE. Currently, we do not pin only movable CMA pages. Generalize the function that migrates CMA pages to migrate all movable pages. Use is_pinnable_page() to check which pages need to be migrated Signed-off-by: Pavel Tatashin Reviewed-by: John Hubbard --- include/linux/migrate.h | 1 + include/linux/mmzone.h | 11 ++++-- include/trace/events/migrate.h | 3 +- mm/gup.c | 68 ++++++++++++++-------------------- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/include/linux/migrate.h b/include/linux/migrate.h index 4594838a0f7c..aae5ef0b3ba1 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -27,6 +27,7 @@ enum migrate_reason { MR_MEMPOLICY_MBIND, MR_NUMA_MISPLACED, MR_CONTIG_RANGE, + MR_LONGTERM_PIN, MR_TYPES }; diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index b593316bff3d..25c0c13ba4b1 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -386,9 +386,14 @@ enum zone_type { * likely to succeed, and to locally limit unmovable allocations - e.g., * to increase the number of THP/huge pages. Notable special cases are: * - * 1. Pinned pages: (long-term) pinning of movable pages might - * essentially turn such pages unmovable. Memory offlining might - * retry a long time. + * 1. Pinned pages: (long-term) pinning of movable pages is avoided + * when pages are pinned and faulted, but it is still possible that + * address space already has pages in ZONE_MOVABLE at the time when + * pages are pinned (i.e. user has touches that memory before + * pinning). In such case we try to migrate them to a different zone, + * but if migration fails the pages can still end-up pinned in + * ZONE_MOVABLE. In such case, memory offlining might retry a long + * time and will only succeed once user application unpins pages. * 2. memblock allocations: kernelcore/movablecore setups might create * situations where ZONE_MOVABLE contains unmovable allocations * after boot. Memory offlining and allocations fail early. diff --git a/include/trace/events/migrate.h b/include/trace/events/migrate.h index 4d434398d64d..363b54ce104c 100644 --- a/include/trace/events/migrate.h +++ b/include/trace/events/migrate.h @@ -20,7 +20,8 @@ EM( MR_SYSCALL, "syscall_or_cpuset") \ EM( MR_MEMPOLICY_MBIND, "mempolicy_mbind") \ EM( MR_NUMA_MISPLACED, "numa_misplaced") \ - EMe(MR_CONTIG_RANGE, "contig_range") + EM( MR_CONTIG_RANGE, "contig_range") \ + EMe(MR_LONGTERM_PIN, "longterm_pin") /* * First define the enums in the above macros to be exported to userspace diff --git a/mm/gup.c b/mm/gup.c index 04602e94856b..591d8e2dfc70 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -89,11 +89,12 @@ static __maybe_unused struct page *try_grab_compound_head(struct page *page, int orig_refs = refs; /* - * Can't do FOLL_LONGTERM + FOLL_PIN with CMA in the gup fast - * path, so fail and let the caller fall back to the slow path. + * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a + * right zone, so fail and let the caller fall back to the slow + * path. */ - if (unlikely(flags & FOLL_LONGTERM) && - is_migrate_cma_page(page)) + if (unlikely((flags & FOLL_LONGTERM) && + !is_pinnable_page(page))) return NULL; /* @@ -1549,19 +1550,18 @@ struct page *get_dump_page(unsigned long addr) } #endif /* CONFIG_ELF_CORE */ -#ifdef CONFIG_CMA -static long check_and_migrate_cma_pages(struct mm_struct *mm, - unsigned long start, - unsigned long nr_pages, - struct page **pages, - struct vm_area_struct **vmas, - unsigned int gup_flags) +static long check_and_migrate_movable_pages(struct mm_struct *mm, + unsigned long start, + unsigned long nr_pages, + struct page **pages, + struct vm_area_struct **vmas, + unsigned int gup_flags) { unsigned long i; unsigned long step; bool drain_allow = true; bool migrate_allow = true; - LIST_HEAD(cma_page_list); + LIST_HEAD(movable_page_list); long ret = nr_pages; struct migration_target_control mtc = { .nid = NUMA_NO_NODE, @@ -1579,13 +1579,12 @@ static long check_and_migrate_cma_pages(struct mm_struct *mm, */ step = compound_nr(head) - (pages[i] - head); /* - * If we get a page from the CMA zone, since we are going to - * be pinning these entries, we might as well move them out - * of the CMA zone if possible. + * If we get a movable page, since we are going to be pinning + * these entries, try to move them out if possible. */ - if (is_migrate_cma_page(head)) { + if (!is_pinnable_page(head)) { if (PageHuge(head)) - isolate_huge_page(head, &cma_page_list); + isolate_huge_page(head, &movable_page_list); else { if (!PageLRU(head) && drain_allow) { lru_add_drain_all(); @@ -1593,7 +1592,7 @@ static long check_and_migrate_cma_pages(struct mm_struct *mm, } if (!isolate_lru_page(head)) { - list_add_tail(&head->lru, &cma_page_list); + list_add_tail(&head->lru, &movable_page_list); mod_node_page_state(page_pgdat(head), NR_ISOLATED_ANON + page_is_file_lru(head), @@ -1605,7 +1604,7 @@ static long check_and_migrate_cma_pages(struct mm_struct *mm, i += step; } - if (!list_empty(&cma_page_list)) { + if (!list_empty(&movable_page_list)) { /* * drop the above get_user_pages reference. */ @@ -1615,25 +1614,24 @@ static long check_and_migrate_cma_pages(struct mm_struct *mm, for (i = 0; i < nr_pages; i++) put_page(pages[i]); - if (migrate_pages(&cma_page_list, alloc_migration_target, NULL, - (unsigned long)&mtc, MIGRATE_SYNC, MR_CONTIG_RANGE)) { + if (migrate_pages(&movable_page_list, alloc_migration_target, NULL, + (unsigned long)&mtc, MIGRATE_SYNC, MR_LONGTERM_PIN)) { /* * some of the pages failed migration. Do get_user_pages * without migration. */ migrate_allow = false; - if (!list_empty(&cma_page_list)) - putback_movable_pages(&cma_page_list); + if (!list_empty(&movable_page_list)) + putback_movable_pages(&movable_page_list); } /* * We did migrate all the pages, Try to get the page references - * again migrating any new CMA pages which we failed to isolate - * earlier. + * again migrating any pages which we failed to isolate earlier. */ ret = __get_user_pages_locked(mm, start, nr_pages, - pages, vmas, NULL, - gup_flags); + pages, vmas, NULL, + gup_flags); if ((ret > 0) && migrate_allow) { nr_pages = ret; @@ -1644,17 +1642,6 @@ static long check_and_migrate_cma_pages(struct mm_struct *mm, return ret; } -#else -static long check_and_migrate_cma_pages(struct mm_struct *mm, - unsigned long start, - unsigned long nr_pages, - struct page **pages, - struct vm_area_struct **vmas, - unsigned int gup_flags) -{ - return nr_pages; -} -#endif /* CONFIG_CMA */ /* * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which @@ -1678,8 +1665,9 @@ static long __gup_longterm_locked(struct mm_struct *mm, if (gup_flags & FOLL_LONGTERM) { if (rc > 0) - rc = check_and_migrate_cma_pages(mm, start, rc, pages, - vmas, gup_flags); + rc = check_and_migrate_movable_pages(mm, start, rc, + pages, vmas, + gup_flags); memalloc_pin_restore(flags); } return rc; -- 2.25.1