From mboxrd@z Thu Jan 1 00:00:00 1970 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 07E37C27C7B for ; Thu, 13 Jun 2024 22:13:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6DCC6B0099; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D1D6C6B0098; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE4806B0099; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A440A6B0096 for ; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 210E440359 for ; Thu, 13 Jun 2024 22:13:41 +0000 (UTC) X-FDA: 82227268242.09.E668396 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf16.hostedemail.com (Postfix) with ESMTP id EE64A180003 for ; Thu, 13 Jun 2024 22:13:38 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Vbv8+vbN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718316817; a=rsa-sha256; cv=none; b=NudYP6wDR8Noqimeqsqw5TZ+iEpm/9hb1ow/drI0VLGnuEpNmRitpdgAVUhFiyymoDh/cB hUCYLymCj+fraNYc5oJqU4C4Q3IUqyn5pEczo4ISRaIQNd0VDCf0d9QRFzGn64Nqtjfp/8 i4WWYu5Y43dRu+8nLRC/A0eJfnSj978= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Vbv8+vbN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718316817; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WdfJs2fGTcT6wyOt02CtufQGS6XpiVjlndTuH5LRebI=; b=qyZgdjL0hTpmJnL81++zVT2ZM1AuoH9UwQLQ1VWhi7x3fYMsgFA2QMmsmor5gj+lNEg8T1 kTCSxmXiz9Nv1ORYGP41I6QZbHzV1Q1J1KnJLxgN1TiK0zI6Z3WDJwALO8tAx7uxa82sAg f+XaO2dByR0PU0svfYWSFQeGqns1Zdg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316819; x=1749852819; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=A1Q4hK2VF6Kkq0SWMg39yfc5/Z639gK4zvJ914UtXkE=; b=Vbv8+vbNfKJLySDYKmpIIBqGrWWt0BnqcjXe3THkxYn4BUYNKYd8eCsH HPO05lKgSIk+byjsw5Ts5NlnwPiyPk1kECl18Xsv62g1mulMLHZEJR41J hKwXoFsKBPPoSCVj6rFoMcR1OFdYgR8OtgzKUijaOMmKs6ipOhBBA3oXG S05go1CTUmrP2boW0mGOz0NyKj+iaTEYy4sdkY7SPWVEw7lATK4d61eKr 8MFsKFF1PfQhCqWyZg1oOGbbhE9RUrQCNcZC32iKt9+fXwU5Y/QwHkAoG +e4a4vwqJDl9qVVRvmvYXm3S/sybhj7K53mMs+c1l9PdkgHOo7lvD402A w==; X-CSE-ConnectionGUID: LBkV5OrcQFC0NDYFtC/98g== X-CSE-MsgGUID: nwfelcPGQUq5d2Cpb2DtkQ== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720537" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720537" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 X-CSE-ConnectionGUID: NKbLsMRUT26Jf1IKfP3I9A== X-CSE-MsgGUID: LisDwqOGR3K43NzJBlu6Eg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214094" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Jason Gunthorpe , Peter Xu , Dave Airlie , Gerd Hoffmann Subject: [PATCH v15 2/9] mm/gup: Introduce check_and_migrate_movable_folios() Date: Thu, 13 Jun 2024 14:42:04 -0700 Message-ID: <20240613214741.1029446-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: EE64A180003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 98jjcmbwg8tbxr8si41tqn599q73jb8a X-HE-T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aPpUzsdZ7A5C8OdfXWCt+IhMXug+hUr+/i+fFilW5BjJXgDPVP0AFOcZGUSmAfSObRdOXdNYh28TfBqvK7n2latEPRECq3eXpLOL3kGbjXcGlq+pTUv3OEQ0ypaseISiVdXmXthwSQ+iCwGJexZ7GaDQ4qQMhf1qXW2NEDLmYMNa66QEOYx2i9ONg/v/gYbnKhj5D0AX7Id9YtPeo6J9MBl4gxVGejtYCQwUeKAqu/6kmoGWXd56C9KT1Mqs5734ew== 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: List-Subscribe: List-Unsubscribe: This helper is the folio equivalent of check_and_migrate_movable_pages(). Therefore, all the rules that apply to check_and_migrate_movable_pages() also apply to this one as well. Currently, this helper is only used by memfd_pin_folios(). This patch also includes changes to rename and convert the internal functions collect_longterm_unpinnable_pages() and migrate_longterm_unpinnable_pages() to work on folios. As a result, check_and_migrate_movable_pages() is now a wrapper around check_and_migrate_movable_folios(). Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Peter Xu Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Acked-by: Dave Airlie Acked-by: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- mm/gup.c | 124 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 47 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index d9ea60621628..a88e19c78730 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2427,19 +2427,19 @@ struct page *get_dump_page(unsigned long addr) #ifdef CONFIG_MIGRATION /* - * Returns the number of collected pages. Return value is always >= 0. + * Returns the number of collected folios. Return value is always >= 0. */ -static unsigned long collect_longterm_unpinnable_pages( - struct list_head *movable_page_list, - unsigned long nr_pages, - struct page **pages) +static unsigned long collect_longterm_unpinnable_folios( + struct list_head *movable_folio_list, + unsigned long nr_folios, + struct folio **folios) { unsigned long i, collected = 0; struct folio *prev_folio = NULL; bool drain_allow = true; - for (i = 0; i < nr_pages; i++) { - struct folio *folio = page_folio(pages[i]); + for (i = 0; i < nr_folios; i++) { + struct folio *folio = folios[i]; if (folio == prev_folio) continue; @@ -2454,7 +2454,7 @@ static unsigned long collect_longterm_unpinnable_pages( continue; if (folio_test_hugetlb(folio)) { - isolate_hugetlb(folio, movable_page_list); + isolate_hugetlb(folio, movable_folio_list); continue; } @@ -2466,7 +2466,7 @@ static unsigned long collect_longterm_unpinnable_pages( if (!folio_isolate_lru(folio)) continue; - list_add_tail(&folio->lru, movable_page_list); + list_add_tail(&folio->lru, movable_folio_list); node_stat_mod_folio(folio, NR_ISOLATED_ANON + folio_is_file_lru(folio), folio_nr_pages(folio)); @@ -2476,27 +2476,28 @@ static unsigned long collect_longterm_unpinnable_pages( } /* - * Unpins all pages and migrates device coherent pages and movable_page_list. - * Returns -EAGAIN if all pages were successfully migrated or -errno for failure - * (or partial success). + * Unpins all folios and migrates device coherent folios and movable_folio_list. + * Returns -EAGAIN if all folios were successfully migrated or -errno for + * failure (or partial success). */ -static int migrate_longterm_unpinnable_pages( - struct list_head *movable_page_list, - unsigned long nr_pages, - struct page **pages) +static int migrate_longterm_unpinnable_folios( + struct list_head *movable_folio_list, + unsigned long nr_folios, + struct folio **folios) { int ret; unsigned long i; - for (i = 0; i < nr_pages; i++) { - struct folio *folio = page_folio(pages[i]); + for (i = 0; i < nr_folios; i++) { + struct folio *folio = folios[i]; if (folio_is_device_coherent(folio)) { /* - * Migration will fail if the page is pinned, so convert - * the pin on the source page to a normal reference. + * Migration will fail if the folio is pinned, so + * convert the pin on the source folio to a normal + * reference. */ - pages[i] = NULL; + folios[i] = NULL; folio_get(folio); gup_put_folio(folio, 1, FOLL_PIN); @@ -2509,24 +2510,24 @@ static int migrate_longterm_unpinnable_pages( } /* - * We can't migrate pages with unexpected references, so drop + * We can't migrate folios with unexpected references, so drop * the reference obtained by __get_user_pages_locked(). - * Migrating pages have been added to movable_page_list after + * Migrating folios have been added to movable_folio_list after * calling folio_isolate_lru() which takes a reference so the - * page won't be freed if it's migrating. + * folio won't be freed if it's migrating. */ - unpin_user_page(pages[i]); - pages[i] = NULL; + unpin_folio(folios[i]); + folios[i] = NULL; } - if (!list_empty(movable_page_list)) { + if (!list_empty(movable_folio_list)) { struct migration_target_control mtc = { .nid = NUMA_NO_NODE, .gfp_mask = GFP_USER | __GFP_NOWARN, .reason = MR_LONGTERM_PIN, }; - if (migrate_pages(movable_page_list, alloc_migration_target, + if (migrate_pages(movable_folio_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC, MR_LONGTERM_PIN, NULL)) { ret = -ENOMEM; @@ -2534,48 +2535,71 @@ static int migrate_longterm_unpinnable_pages( } } - putback_movable_pages(movable_page_list); + putback_movable_pages(movable_folio_list); return -EAGAIN; err: - for (i = 0; i < nr_pages; i++) - if (pages[i]) - unpin_user_page(pages[i]); - putback_movable_pages(movable_page_list); + unpin_folios(folios, nr_folios); + putback_movable_pages(movable_folio_list); return ret; } /* - * Check whether all pages are *allowed* to be pinned. Rather confusingly, all - * pages in the range are required to be pinned via FOLL_PIN, before calling - * this routine. + * Check whether all folios are *allowed* to be pinned indefinitely (longterm). + * Rather confusingly, all folios in the range are required to be pinned via + * FOLL_PIN, before calling this routine. * - * If any pages in the range are not allowed to be pinned, then this routine - * will migrate those pages away, unpin all the pages in the range and return + * If any folios in the range are not allowed to be pinned, then this routine + * will migrate those folios away, unpin all the folios in the range and return * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then * call this routine again. * * If an error other than -EAGAIN occurs, this indicates a migration failure. * The caller should give up, and propagate the error back up the call stack. * - * If everything is OK and all pages in the range are allowed to be pinned, then - * this routine leaves all pages pinned and returns zero for success. + * If everything is OK and all folios in the range are allowed to be pinned, + * then this routine leaves all folios pinned and returns zero for success. */ -static long check_and_migrate_movable_pages(unsigned long nr_pages, - struct page **pages) +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) { unsigned long collected; - LIST_HEAD(movable_page_list); + LIST_HEAD(movable_folio_list); - collected = collect_longterm_unpinnable_pages(&movable_page_list, - nr_pages, pages); + collected = collect_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); if (!collected) return 0; - return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages, - pages); + return migrate_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); +} + +/* + * This routine just converts all the pages in the @pages array to folios and + * calls check_and_migrate_movable_folios() to do the heavy lifting. + * + * Please see the check_and_migrate_movable_folios() documentation for details. + */ +static long check_and_migrate_movable_pages(unsigned long nr_pages, + struct page **pages) +{ + struct folio **folios; + long i, ret; + + folios = kmalloc_array(nr_pages, sizeof(*folios), GFP_KERNEL); + if (!folios) + return -ENOMEM; + + for (i = 0; i < nr_pages; i++) + folios[i] = page_folio(pages[i]); + + ret = check_and_migrate_movable_folios(nr_pages, folios); + + kfree(folios); + return ret; } #else static long check_and_migrate_movable_pages(unsigned long nr_pages, @@ -2583,6 +2607,12 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, { return 0; } + +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) +{ + return 0; +} #endif /* CONFIG_MIGRATION */ /* -- 2.45.1