From patchwork Sun Feb 25 07:56:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570754 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 C16F7C47DD9 for ; Sun, 25 Feb 2024 08:24:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3BAC6B00F4; Sun, 25 Feb 2024 03:24:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AB6C36B00F8; Sun, 25 Feb 2024 03:24:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83F596B00F6; Sun, 25 Feb 2024 03:24:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 610CE6B00F5 for ; Sun, 25 Feb 2024 03:24:16 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EA6B5A03FE for ; Sun, 25 Feb 2024 08:24:15 +0000 (UTC) X-FDA: 81829638870.27.7EFCCA0 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf09.hostedemail.com (Postfix) with ESMTP id 2EBDF14000D for ; Sun, 25 Feb 2024 08:24:13 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=m++fgHJP; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708849454; 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=0+trpL1ypBsEM42inypymiWldqUMRalG2DWR73Ds48g=; b=nR0fCv/De8QePIw4he+dPoCN+zPjMG21K1sim91SPzIAeUzbhOGQuaGz9UTbiM0CVJ+EPc GrqPNlL/3zbE1bVvCt49WKZ3fxbRxNccX1s04+NElpIlUzRChMIVVJFoKUJ6aSXcZAw/bf kWGg4y7K2OQSb56TCWyfuNlLDDaXmZk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849454; a=rsa-sha256; cv=none; b=yNM/Q1PcgsEFnmHpxvzDXgXaTEir0TMvMD03U9fBEW+lCjJtISmnd+N340lMcqf0ZxJymF V1tsCXXMsVN2LP4PdLkYJDrlKpnVnsZz4MxWwYGTzv0F2oc8ciiG5rn2JoQToQf1CnMjyS z6vOgdeI5qKGLDIyFhnfQ4TA66jUpGM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=m++fgHJP; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849454; x=1740385454; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Lrj5AnfXRm1ue52z3xjuiLSu7Sue1rj/iVQQ5WFlJ/c=; b=m++fgHJPsLMArCkG04juNzJjeAd+kt6oKm6PQp8m0xWACsgsJqNAbcaC cinTSxxSBf5i7Y3XE1aSGd6B/IUsM5RlFOoEwiBWXQnnHyJUhcmUY2LjS 9sAO7H6rrKOuiq6FsBh9eJKdFx43hz/epTlTLYRlzPIld46Dzj6hgt7JZ 9fXtqS8K313G5uWRC9MCdiUhdOkto5LqI/Rgegcjl+9YYgDv8nrySiJ6H mULQF++cZjxuh68a3wG0CUKHBanVxIT+uD6U38WgKLr69BWxUsI1yGaB1 FxfDePnclESF98LtL4nMu+yIU3j6bncygNwRr8UsVQvq5F8neZh1d2fQJ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988346" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988346" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783243" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:11 -0800 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 Subject: [PATCH v12 1/8] mm/gup: Introduce unpin_folio/unpin_folios helpers Date: Sat, 24 Feb 2024 23:56:57 -0800 Message-ID: <20240225080008.1019653-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Stat-Signature: 9tiqmbpyqcopzhw3dea1uc3nfh9axirx X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 2EBDF14000D X-Rspam-User: X-HE-Tag: 1708849453-392826 X-HE-Meta: U2FsdGVkX18+znfYiPLhh028cBZQrr2MZ9a67sgAzJoR7roXJAHMlSnhskRnRlFNz5lZLUC82+yL1AYmWf82dCxsaLfZRGFqAymBz4iJl2CwfKgG5y22dHP+jhYet/3bbzfkVVgzBBmH5QHg5qDFxb+SKondvM+vTXoqC0ftlgO2qgfBfY61V9+ksyFkYLt/Pmqy5AX9c6zENqnXi5evYAE0q+4l6O/6Tby5ktUuOk25upIN210sDsuWfR/4FjpuHukoSA41hIQeRdSNwysop5wy17nXCIQU9Bb8onSH7QGHbmceH6+jLm6i+XxnwsOXfWnFhXRzYIiI4nGjlOh7HZnW7cVkvni15xD2aH6HuFnQ6d790I5slhAkQ/zOImWJZtwGdbJjJ514nF5A5xbXx8mVy+JqXKalPnQ+qTtjcr9Ofp0l88Kc5MFjgNJjnswPHQzJRNhWzp6+rUZakv5Uoq/cqp+A9jgh6yqP4F7tnHcwNped7QTAl4jgtQyrlwhKdXJelTNr7lTfnYSbOAI010vArUUof4KTwe0j6YVWDOyCJ5hNckzOucPNr/1f4R+hGe7uHqIwWfpWVi/Z0922RtdcpMELBsO7M4/PvWi/KEPLqkV2MKbF0RblkgAAbQhSL4MTfX782eIcBuSW3fU30BfcxsBTZT+M+Ms1VTCyZSrkKVvNG71PjX796rVbRkE24zOYFo3DFgnz1zQ/aVuJ5vdPTvL39VSqqEME4KySCUcluaYhagZZEBPJ7ZDLLCLQ1bpKgPgEcEbkyJcIaOiYy9mNYVKT20IhPhk5dwN7G3B3hVLdH5l4pDDaxw7A+hylQHC8SeiHIzIYTiN7mVky9oxuqphTNRcQHybR3sLdvgdQgoE4AfoJBzhSUFwHw44d1cqu9UdMvyPbotpPetSbRsUeX7Ofjs/EBXEl+wzRd/0FjLAv7h4vVA== 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: These helpers are the folio versions of unpin_user_page/unpin_user_pages. They are currently only useful for unpinning folios pinned by memfd_pin_folios() or other associated routines. However, they could find new uses in the future, when more and more folio-only helpers are added to GUP. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Peter Xu Suggested-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- include/linux/mm.h | 2 ++ mm/gup.c | 81 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 6f4825d82965..36e4c2b22600 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1601,11 +1601,13 @@ static inline void put_page(struct page *page) #define GUP_PIN_COUNTING_BIAS (1U << 10) void unpin_user_page(struct page *page); +void unpin_folio(struct folio *folio); void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, bool make_dirty); void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages, bool make_dirty); void unpin_user_pages(struct page **pages, unsigned long npages); +void unpin_folios(struct folio **folios, unsigned long nfolios); static inline bool is_cow_mapping(vm_flags_t flags) { diff --git a/mm/gup.c b/mm/gup.c index df83182ec72d..0a45eda6aaeb 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -30,6 +30,23 @@ struct follow_page_context { unsigned int page_mask; }; +static inline void sanity_check_pinned_folios(struct folio **folios, + unsigned long nfolios) +{ + if (!IS_ENABLED(CONFIG_DEBUG_VM)) + return; + + for (; nfolios; nfolios--, folios++) { + struct folio *folio = *folios; + + if (is_zero_folio(folio) || + !folio_test_anon(folio)) + continue; + + VM_BUG_ON_FOLIO(!PageAnonExclusive(&folio->page), folio); + } +} + static inline void sanity_check_pinned_pages(struct page **pages, unsigned long npages) { @@ -52,15 +69,11 @@ static inline void sanity_check_pinned_pages(struct page **pages, struct page *page = *pages; struct folio *folio = page_folio(page); - if (is_zero_page(page) || - !folio_test_anon(folio)) - continue; - if (!folio_test_large(folio) || folio_test_hugetlb(folio)) - VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page), page); - else - /* Either a PTE-mapped or a PMD-mapped THP. */ - VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page) && - !PageAnonExclusive(page), page); + sanity_check_pinned_folios(&folio, 1); + + /* Either a PTE-mapped or a PMD-mapped THP. */ + if (folio_test_large(folio) && !folio_test_hugetlb(folio)) + VM_BUG_ON_PAGE(!PageAnonExclusive(page), page); } } @@ -276,6 +289,21 @@ void unpin_user_page(struct page *page) } EXPORT_SYMBOL(unpin_user_page); +/** + * unpin_folio() - release a dma-pinned folio + * @folio: pointer to folio to be released + * + * Folios that were pinned via memfd_pin_folios() or other similar routines + * must be released either using unpin_folio() or unpin_folios(). This is so + * that such folios can be separately tracked and uniquely handled. + */ +void unpin_folio(struct folio *folio) +{ + sanity_check_pinned_folios(&folio, 1); + gup_put_folio(folio, 1, FOLL_PIN); +} +EXPORT_SYMBOL(unpin_folio); + /** * folio_add_pin - Try to get an additional pin on a pinned folio * @folio: The folio to be pinned @@ -488,6 +516,41 @@ void unpin_user_pages(struct page **pages, unsigned long npages) } EXPORT_SYMBOL(unpin_user_pages); +/** + * unpin_folios() - release an array of gup-pinned folios. + * @folios: array of folios to be marked dirty and released. + * @nfolios: number of folios in the @folios array. + * + * For each folio in the @folios array, release the folio using unpin_folio(). + * + * Please see the unpin_folio() documentation for details. + */ +void unpin_folios(struct folio **folios, unsigned long nfolios) +{ + unsigned long i = 0, j; + + /* + * If this WARN_ON() fires, then the system *might* be leaking folios + * (by leaving them pinned), but probably not. More likely, gup/pup + * returned a hard -ERRNO error to the caller, who erroneously passed + * it here. + */ + if (WARN_ON(IS_ERR_VALUE(nfolios))) + return; + + sanity_check_pinned_folios(folios, nfolios); + while (i < nfolios) { + for (j = i + 1; j < nfolios; j++) + if (folios[i] != folios[j]) + break; + + if (folios[i]) + gup_put_folio(folios[i], j - i, FOLL_PIN); + i = j; + } +} +EXPORT_SYMBOL(unpin_folios); + /* * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's * lifecycle. Avoid setting the bit unless necessary, or it might cause write From patchwork Sun Feb 25 07:56:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570755 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 0919CC54E41 for ; Sun, 25 Feb 2024 08:24:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 758276B00F8; Sun, 25 Feb 2024 03:24:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 706A16B00F9; Sun, 25 Feb 2024 03:24:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 583006B00FA; Sun, 25 Feb 2024 03:24:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 46FD36B00F8 for ; Sun, 25 Feb 2024 03:24:17 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 17D7FC0289 for ; Sun, 25 Feb 2024 08:24:17 +0000 (UTC) X-FDA: 81829638954.24.EE99E5D Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf21.hostedemail.com (Postfix) with ESMTP id 1CCDD1C000E for ; Sun, 25 Feb 2024 08:24:14 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=nj7L7fBQ; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849455; a=rsa-sha256; cv=none; b=rx2y3BlHX4Uyp59Uj5YV4yBgFSLXxEJfbU/8+w8N8ZTTf7tbJjEoTVmscFcCuZQAbne0v9 z/j8KAxFmPhKkT+I64FMw8N7TXwuxspwFdUUzlgRU69+7V1DQ6WSsq0ih0WHHeifbz2BHA sF3gXl4HQU6YelS2nXNfxoK2Qx5K7zU= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=nj7L7fBQ; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 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=1708849455; 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=M77rke642uG3IUkL73gZsVZhWQrM3CDqVbQFZ5hkz84=; b=Z7pidzljTW6FBN6xzQyH+cvQCz0I7QZyRwjZeCYjrn02qN2dJ6u1uGNOnQf4VXWNJiD6r3 21mRqKAD1lK2ruweLZTg3XH1E68ysozC92gd8qBmL3IGVXb38p5ch6qVqFcnUcLxxxZ6QM PSLV8/2Vlmw9BUWKgPR1oElWnq5QgOs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849455; x=1740385455; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qAWI9OROZ7HrTscI+EJd/d4/as0t64bOlXO/zqAPSsI=; b=nj7L7fBQ7xE+oteS4I0hZjTdOKY/q1hfvgntuSPaSQd/Z/0vuSRqlzRl agp05txi24x1PBrJD+IzJjPpDi3GPL+BN5Nuo4LL2JrIbhyhFJvhPsBEo MoABKwKQ37faOEWFSktkb2pRLcv41o57jH9j2gYeJyqgbhCT6F4zDQusF a4bslBKWPb8jske8z0Kh0G/T1YIxfkUCHPQ+C2YK8LuuiL+DvcaPeftCk jDGlsdEri1fjxqaHK45nHQkMybRhdQzz/b7QobpthiGqnUXerrkaYUsYl /wI4xi2ewPbco0TVWrnLhMC4FgU80CFx0Hh+YO06U7Rx4R9hHTPCTyipo A==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988352" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988352" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783246" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:11 -0800 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 Subject: [PATCH v12 2/8] mm/gup: Introduce check_and_migrate_movable_folios() Date: Sat, 24 Feb 2024 23:56:58 -0800 Message-ID: <20240225080008.1019653-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1CCDD1C000E X-Stat-Signature: 43eo8pi7gyra3ydrnb4ap184rppthuds X-HE-Tag: 1708849454-634706 X-HE-Meta: U2FsdGVkX1866K+DlGXm2vP8P6LoIhC7tb5yK1iUbH0eG639QCLBqKNNXtR8OuK/XYK+zHXJNaM1MZF3FQtAfMWpGju3gAR0BE2VzVPtce7b/C6fQaeP25K3pyKrASBhlVnVm0zY+TKqgKNK/MmgDfCjcYiO4fEqfUin417z2mpI2Iep11BJlM8Yau/xcBPxO6GISMOE9Tqq95HmwlXzQTwH4iGTQcmv+Ko/EIjdgY2XE6TAsAUgsx2KIXejoF3HXOP3vjzh5AIYfQYWlqIjkhBWcSEFbj2DMsoe5IUb0DKhaX+JxEIz4fQfv5A54dwfS8vHw8vTO6nLj4LKVQ1hER7GS49RzlRVKUlWUfgVtKVnhl04wS1j8zEVLy/f48jSNAg9AmEd0Njc91/6fauDBh62cYd/Uhm/BkK9DnaJQFdbJpDMX8yziUEI0YJ0QTzQJt7aBWxQ0rr3QV2Zn0RURsXngnXq6Kz207z1A81w+qvtevj44pK42xp2SXef//KI+GTunEjKFYKoK4y6DDLWZ9M25PviIg12rpzWPkxBoKhn0jhg+N+9iJ+d6dJakTeTmwj6XODTHcaMHZIQOehii5GMLb6H8WCUZ/a5/gRXm9NjspJJsqRDJTgH3CgRprA4fJfGQlZ/0fvdTwthlyS+dD/jWIvTGfV0QNoQsKv78CoDxXqVd135CSebohXnA7T/mT976rFa9BC7HRqoEPAckoFB5eNfwEQdwiniF8C74U7trF8wqVy2lHpkeNBrm4TSHU+0+D2eSTWoNPmmor/17lfyHAq4OK8iEC4RJ5NkRGEDEUqKqvnUZH5bpgY/XscRgIfDzWAYCLc1epNg9dFeSHcGHgBfaP+O9yyrypZM8rhsuDo8sB3mtpULDnhwSQ1aJv7QTAj2Cg27JhM4ga6JHIPA8IH9syXxYzYYvrsnoErWD1c8b3BN6KtZpwYjRycORY2gEbTdK9e2pf+ptU0 ESLr4ZWv DYu9AFsRaUsvIuekLl/0QT2eIKBgJawA+ntUMrcpiel5ijDV2raSEi/kpEej42m3jg0kCFI8cv0zRu+D3Ga9mCfkcQ/n9vxD2t5uTpZCRX8K/OOLQr+4f8RMugL5Hku+pkfcLz4DYvzSEiiGV/ZO0I1l1OY6EWfZtyEitA8dWmMM+P5zf856njNBiViZxc/bNgwwy//UHsdOVlg/7+ySRbD+KJc8pHHU4gkXMkA7sNaSCWKCnkgNi+A0LNHWzN4P5K/Km4nSoe5vTobjCDdQ/x1J0UNtfTIvBEWaoFKVfK9dRVuJ2zuZNAXp9lA== 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. Since they are also used by check_and_migrate_movable_pages(), a temporary array is used to collect and share the folios with these functions. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Peter Xu Suggested-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- mm/gup.c | 129 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 92 insertions(+), 37 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 0a45eda6aaeb..1410af954a4e 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2099,20 +2099,24 @@ 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, +static unsigned long collect_longterm_unpinnable_folios( + struct list_head *movable_folio_list, + unsigned long nr_folios, + struct folio **folios, struct page **pages) { unsigned long i, collected = 0; struct folio *prev_folio = NULL; bool drain_allow = true; + struct folio *folio; - for (i = 0; i < nr_pages; i++) { - struct folio *folio = page_folio(pages[i]); + for (i = 0; i < nr_folios; i++) { + if (pages) + folios[i] = page_folio(pages[i]); + folio = folios[i]; if (folio == prev_folio) continue; prev_folio = folio; @@ -2126,7 +2130,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; } @@ -2138,7 +2142,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)); @@ -2148,27 +2152,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); @@ -2181,23 +2186,23 @@ 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, }; - 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; @@ -2205,15 +2210,15 @@ 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); + for (i = 0; i < nr_folios; i++) + if (folios[i]) + unpin_folio(folios[i]); + putback_movable_pages(movable_folio_list); return ret; } @@ -2237,16 +2242,60 @@ static int migrate_longterm_unpinnable_pages( static long check_and_migrate_movable_pages(unsigned long nr_pages, struct page **pages) { + unsigned long nr_folios = nr_pages; unsigned long collected; - LIST_HEAD(movable_page_list); + LIST_HEAD(movable_folio_list); + struct folio **folios; + long ret; - collected = collect_longterm_unpinnable_pages(&movable_page_list, - nr_pages, pages); + folios = kmalloc_array(nr_folios, sizeof(*folios), GFP_KERNEL); + if (!folios) + return -ENOMEM; + + collected = collect_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios, + pages); + if (!collected) { + kfree(folios); + return 0; + } + + ret = migrate_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); + kfree(folios); + return ret; +} + +/* + * Check whether all folios are *allowed* to be pinned. Rather confusingly, all + * folios in the range are required to be pinned via FOLL_PIN, before calling + * this routine. + * + * 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 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_folios(unsigned long nr_folios, + struct folio **folios) +{ + unsigned long collected; + LIST_HEAD(movable_folio_list); + + collected = collect_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios, + NULL); 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); } #else static long check_and_migrate_movable_pages(unsigned long nr_pages, @@ -2254,6 +2303,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 */ /* From patchwork Sun Feb 25 07:56:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570757 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 1A537C47DD9 for ; Sun, 25 Feb 2024 08:24:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3FEF36B00FB; Sun, 25 Feb 2024 03:24:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 35D936B00FD; Sun, 25 Feb 2024 03:24:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DA806B00FE; Sun, 25 Feb 2024 03:24:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id F01606B00FB for ; Sun, 25 Feb 2024 03:24:18 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C907C1202F6 for ; Sun, 25 Feb 2024 08:24:18 +0000 (UTC) X-FDA: 81829638996.22.54AEF59 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf14.hostedemail.com (Postfix) with ESMTP id CF2EC100005 for ; Sun, 25 Feb 2024 08:24:15 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FXfy7o2p; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 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=1708849456; 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=A9kdWYJrq8df96o0nhlHNPK1LEB8BHvEeLApCKEij8Q=; b=YU/WvNpDh2Ypx2U+m2di472oShsm6vSZz5KUJurr1D/Iq9dGm34GFJYAgN9ogUD+cSd2DS 0eIwpe0b7K1C4aTGpmUNk9nSCq80cHaL25zGJvkjj7/3KSlOZskfOuFuSAOPzhl5zPpKvO VUhye7I+Jc7sHVpXLpSl8TuJXF2uC+Q= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FXfy7o2p; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849456; a=rsa-sha256; cv=none; b=PHDULJzTIZi93aADPXlfLwTmkyIjIyO4ANSqsx52lj4KqAT58bNkKCwdyUCKemFBWtljZ8 XHnVOm4qWkuDuSpuBQvvEv1atL0x643xsNynMd4mKr7NaDGADVwOd4uBnNO88MSKJWF+Z9 G036smq1HBN5pP23IyIkhrgRIbvkOVk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849456; x=1740385456; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Mdt4r6h8BCL+kqp7gjKBQvdvVXgEI5bmUqCpjmEFAmQ=; b=FXfy7o2pcuFYLSp60fAIQEwg/gP11kDCoW1ppmdf9gzkhCGAM3tQIDAj 6bBe7TYLIwHyt96wjH9P6SS/SVIzuKhXjF8x3gSAZL21AIHOKFFFVvqUb 42QUUIZeTYdltEUL7J3ONttsH/S1GF1+EKqzLg8dAFuHSf4lUIBC5fbTl n01EnU6wQ2zJfS84qPcbVMZfkWypksYtOulGylovYKFCEqiWs7PZZWgbC GAxMizuUt8vAyr1YeGrdgJwTceTpvAN0I6j5mLBNBAzyXxLN1+fjZF6H+ m2JwqO6SBRFKvSuwMiY+Eh7Zdn5ETyb0E8EHf/WzK99ilhHcp9CKxyt+p A==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988355" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988355" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783251" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:11 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe , Christoph Hellwig Subject: [PATCH v12 3/8] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Sat, 24 Feb 2024 23:56:59 -0800 Message-ID: <20240225080008.1019653-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: CF2EC100005 X-Stat-Signature: u4nq1gkrr8sdf13a6fr9in5debgks5dy X-Rspam-User: X-HE-Tag: 1708849455-519791 X-HE-Meta: U2FsdGVkX19PhA8BY8IOiblyh0bdhNGjMQm5wu6vR79fuIQQshG5JfRu23+qovGHLXBVFkLSn/s4iM3aa3m7A6QB4GePfd78LqhpBryFOoR/T3kWyWrwF4N8fRuh917GHVdRUFTcdHvWYJ+09bICE1G48w9fQJFkqSG9LXznZLRnmwi5zq0zpphesmyYjXKiRndepAl/AuVtwUr4ai6zKBxCA7g1zV0WLwSZ5eJGQIXWqRLsAsqdlEMySgt6hLOWSZ214E7EqDtH2SrpM1ZYjSvDcyZnf/uAYaPI1yY9PlZpxaLHAgfsefBIzmWoVqGSbpi+Bp96nSyzYgKzXQfjmTNzmqzceuOuYp3o7x75ZB0cV7Owx77al0xNp5u+P36I2rfOf1hX+1jj0s8wF9warb2osDtN4/pgzjsfKTMOBtqXZbhyyWR37O6KgvEPqxhDymQwGvPwZtwOKUMaL+fw4Vda50eQ4WYNVWY8v3hCeWs/Q29B3YXVtrccEvSudu4Z0NjxQCKiz7X1Id9MLH0Iofob+JEhzDUug67rVqSHiSi5Rtuah3qr4KxI57+k41DqUG6AOtnSqwHvibclT3sPr6rkA3jQepLHEi4L4gL/+BqcGxRZMZikRow/VybrMlXcdXMrejSsxC5/bOUCoLfF4Eh8Fl9artgp3uHlmKCSoDMiWSIjGE9o9q73DGEvhBYSI/fsD6NSAOLPktXe3kO8QxGDCeSlS2UP7hAhuo4rchW2CxhlY8wYjpDQ4h8WNrTdMskGsUuzPLFkUQH2fzSdRUzMVLvOgrEWfbLcBJP/acJr2z43KLp9ynfC/xi/dzG2aPHynYyxiTEuQ8qUtHFU5mAJ/m5oQu8Ol+jJI5dRMo/72oIfykmJfMPqV1Vr/72V0xHHQq5PiwNTic4LzwePZvIhLoajNiuEcLJHUG+nVIVjYcC3EKos9O8Tu6B0UTpjlVi7oPLXGhsC26UAk5u dEpdrdOv Q2dsy+C1Ta01rQmTp1uQbLOx1S8OfliERwcMfP5F0MSG7JZbgCwxa0WAYtzkGgtjMqihRvCCIXfCdGt4+UnWyZo0YU1g9vu64Tm1zFJG09/kp3yTtMB5n8jYa6+NacIFHeueRZQcIKPyep56qpsFKAwEobhSz5Uu5kIotpRpOUoAYFZS1354ilANqH3lbqssTsNdRy5cICQBmD0OZyQcj9ftAPTQUnEvYr+RHRF8IKs4YgF0Ymp9pP6EZZksnkdOMyXHk1ssxT64ToNvdGFrDHHZ67kVVkfmT2Mv9+YcKdIzZhzeliZpvqGQUSvkwBd4K5JCUlkohPoepShONH0Yi8KdJ0NUjZgCumnjLKzkmoKCr1Lg= 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: For drivers that would like to longterm-pin the folios associated with a memfd, the memfd_pin_folios() API provides an option to not only pin the folios via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with memfds but it should work with any files that belong to either shmemfs or hugetlbfs. Files belonging to other filesystems are rejected for now. The folios need to be located first before pinning them via FOLL_PIN. If they are found in the page cache, they can be immediately pinned. Otherwise, they need to be allocated using the filesystem specific APIs and then pinned. Cc: David Hildenbrand Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe (v2) Reviewed-by: David Hildenbrand (v3) Reviewed-by: Christoph Hellwig (v6) Signed-off-by: Vivek Kasireddy --- include/linux/memfd.h | 5 ++ include/linux/mm.h | 3 + mm/gup.c | 136 ++++++++++++++++++++++++++++++++++++++++++ mm/memfd.c | 34 +++++++++++ 4 files changed, 178 insertions(+) diff --git a/include/linux/memfd.h b/include/linux/memfd.h index e7abf6fa4c52..3f2cf339ceaf 100644 --- a/include/linux/memfd.h +++ b/include/linux/memfd.h @@ -6,11 +6,16 @@ #ifdef CONFIG_MEMFD_CREATE extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg); +struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx); #else static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a) { return -EINVAL; } +static inline struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) +{ + return ERR_PTR(-EINVAL); +} #endif #endif /* __LINUX_MEMFD_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 36e4c2b22600..54e2775f89be 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2491,6 +2491,9 @@ long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); +long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, + struct folio **folios, unsigned int max_folios, + pgoff_t *offset); int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); diff --git a/mm/gup.c b/mm/gup.c index 1410af954a4e..a72bff363d0c 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include @@ -3530,3 +3532,137 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +/** + * memfd_pin_folios() - pin folios associated with a memfd + * @memfd: the memfd whose folios are to be pinned + * @start: the first memfd offset + * @end: the last memfd offset (inclusive) + * @folios: array that receives pointers to the folios pinned + * @max_folios: maximum number of entries in @folios + * @offset: the offset into the first folio + * + * Attempt to pin folios associated with a memfd in the contiguous range + * [start, end]. Given that a memfd is either backed by shmem or hugetlb, + * the folios can either be found in the page cache or need to be allocated + * if necessary. Once the folios are located, they are all pinned via + * FOLL_PIN and @offset is populatedwith the offset into the first folio. + * And, eventually, these pinned folios must be released either using + * unpin_folios() or unpin_folio(). + * + * It must be noted that the folios may be pinned for an indefinite amount + * of time. And, in most cases, the duration of time they may stay pinned + * would be controlled by the userspace. This behavior is effectively the + * same as using FOLL_LONGTERM with other GUP APIs. + * + * Returns number of folios pinned, which could be less than @max_folios + * as it depends on the folio sizes that cover the range [start, end]. + * If no folios were pinned, it returns -errno. + */ +long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, + struct folio **folios, unsigned int max_folios, + pgoff_t *offset) +{ + unsigned int flags, nr_folios, nr_found; + unsigned int i, pgshift = PAGE_SHIFT; + pgoff_t start_idx, end_idx, next_idx; + struct folio *folio = NULL; + struct folio_batch fbatch; + struct hstate *h; + long ret; + + if (start > end || !max_folios) + return -EINVAL; + + if (!memfd) + return -EINVAL; + + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) + return -EINVAL; + + if (is_file_hugepages(memfd)) { + h = hstate_file(memfd); + pgshift = huge_page_shift(h); + } + + flags = memalloc_pin_save(); + do { + nr_folios = 0; + start_idx = start >> pgshift; + end_idx = end >> pgshift; + if (is_file_hugepages(memfd)) { + start_idx <<= huge_page_order(h); + end_idx <<= huge_page_order(h); + } + + folio_batch_init(&fbatch); + while (start_idx <= end_idx && nr_folios < max_folios) { + /* + * In most cases, we should be able to find the folios + * in the page cache. If we cannot find them for some + * reason, we try to allocate them and add them to the + * page cache. + */ + nr_found = filemap_get_folios_contig(memfd->f_mapping, + &start_idx, + end_idx, + &fbatch); + if (folio) { + folio_put(folio); + folio = NULL; + } + + next_idx = 0; + for (i = 0; i < nr_found; i++) { + /* + * As there can be multiple entries for a + * given folio in the batch returned by + * filemap_get_folios_contig(), the below + * check is to ensure that we pin and return a + * unique set of folios between start and end. + */ + if (next_idx && + next_idx != folio_index(fbatch.folios[i])) + continue; + + folio = try_grab_folio(&fbatch.folios[i]->page, + 1, FOLL_PIN); + if (!folio) { + folio_batch_release(&fbatch); + goto err; + } + + if (nr_folios == 0) + *offset = offset_in_folio(folio, start); + + folios[nr_folios] = folio; + next_idx = folio_next_index(folio); + if (++nr_folios == max_folios) + break; + } + + folio = NULL; + folio_batch_release(&fbatch); + if (!nr_found) { + folio = memfd_alloc_folio(memfd, start_idx); + if (IS_ERR(folio)) { + ret = PTR_ERR(folio); + if (ret != -EEXIST) + goto err; + } + } + } + + ret = check_and_migrate_movable_folios(nr_folios, folios); + } while (ret == -EAGAIN); + + memalloc_pin_restore(flags); + return ret ? ret : nr_folios; +err: + memalloc_pin_restore(flags); + unpin_folios(folios, nr_folios); + + return ret; +} +EXPORT_SYMBOL_GPL(memfd_pin_folios); + diff --git a/mm/memfd.c b/mm/memfd.c index d3a1ba4208c9..36a75e8249f8 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -63,6 +63,40 @@ static void memfd_tag_pins(struct xa_state *xas) xas_unlock_irq(xas); } +/* + * This is a helper function used by memfd_pin_user_pages() in GUP (gup.c). + * It is mainly called to allocate a page in a memfd when the caller + * (memfd_pin_user_pages()) cannot find a page in the page cache at a given + * index in the mapping. + */ +struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) +{ +#ifdef CONFIG_HUGETLB_PAGE + struct folio *folio; + int err; + + if (is_file_hugepages(memfd)) { + folio = alloc_hugetlb_folio_nodemask(hstate_file(memfd), + NUMA_NO_NODE, + NULL, + GFP_USER); + if (folio && folio_try_get(folio)) { + err = hugetlb_add_to_page_cache(folio, + memfd->f_mapping, + idx); + if (err) { + folio_put(folio); + free_huge_folio(folio); + return ERR_PTR(err); + } + return folio; + } + return ERR_PTR(-ENOMEM); + } +#endif + return shmem_read_folio(memfd->f_mapping, idx); +} + /* * Setting SEAL_WRITE requires us to verify there's no pending writer. However, * via get_user_pages(), drivers might have some pending I/O without any active From patchwork Sun Feb 25 07:57:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570756 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 777D5C47DD9 for ; Sun, 25 Feb 2024 08:24:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9062B6B00F9; Sun, 25 Feb 2024 03:24:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 88F156B00FB; Sun, 25 Feb 2024 03:24:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 707946B00FC; Sun, 25 Feb 2024 03:24:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 50EC16B00F9 for ; Sun, 25 Feb 2024 03:24:18 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 209A61A03F2 for ; Sun, 25 Feb 2024 08:24:18 +0000 (UTC) X-FDA: 81829638996.23.6F61469 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf09.hostedemail.com (Postfix) with ESMTP id 5979B140008 for ; Sun, 25 Feb 2024 08:24:16 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=b9+3nzjw; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708849456; 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=G7Bv7jg6ypbCtXkKx3Gshd1i85VTcQErigj+7qKNZ54=; b=Ei1zKuK3iHwFYUsWmBYQ9NCWxWARIwS26P9zNILfIIA5hmT32AL/canpi5zmyh/CvmZzEV jvpsBxNjM3Fhwza59Fha2svTQHYVplRylxGnN5wRR5C8p1y0hxq04zmKQZCGqUGuv677iv JOrCbgHpE8cTJqzYEb0zdPkFti1kGbY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849456; a=rsa-sha256; cv=none; b=geuQhuoMg/gZNiwdZegWeaERFMsevyd0KsPytLDf233ZHVBVSvJ9o9Hcb8bTKKfipXVUG0 mQHmtnyfLyGjbS7T7rw77g1R/aNBRDX6Pz9Efw8eNh8MF6iDvy/PxmSCq4FzegW8RtJH21 lXhI5SyYxfDlbzWpFuBrTJ8P0gQFW4Q= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=b9+3nzjw; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849456; x=1740385456; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tyJeBuptegtLMXafEhsS2DHQW53sLM788gfa3Q0ViXw=; b=b9+3nzjwT2OoBXEhSCbBF7UFxnfY1wHGpxFofBp3aEGHgumqkQ3Tzk9Q TsBPJcDtcpJijuejOiUz69xytwVmizi/suKFxm63xjXCpa/Wez4LvZNXf 26iHlVN7yFz9sKnaLZ26HfudIv8d6m+0GAROxvlInbb8rQQCLGx6uioAU BsnciihX6qGPuXqBcqXxtW9lZQpspJ5C/0E2NYZkua1KsfWhL4iodAWRO 1fRU+6AfU9tWY1NNSFRx9FVDM5eKr9GkOLU0kgMwb6j6D7jxkw0Td8Jay JXKDpFnhUD+PoABDalTLx8HZP90A413hKZzuCZKFJx9E4zPWGSCTgQF+4 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988365" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988365" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783255" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:12 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v12 4/8] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Sat, 24 Feb 2024 23:57:00 -0800 Message-ID: <20240225080008.1019653-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Stat-Signature: krorddu5r163uzzksa9x7odpzr11mary X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 5979B140008 X-Rspam-User: X-HE-Tag: 1708849456-479639 X-HE-Meta: U2FsdGVkX18v/vSC8CDFWUFH3jCMz75WEqp/JNcPfkphFPmQdGyIooWJS6tpnJSDDK9xVD4LtIL3q4AvEdlor/XanDoppb4rUpMdPRbxUGr1oeGDkImEBG16WLYa1mkJ4Rwe7FlmFSGAYaZLGdEK6tsRTPDxw1YjfC5Y7KKDJ5VNeJrXPWgOeFY9LacAvJGOsRl7Gh7ca0vtG7WLEmWW7Rpv/XGCo6wNcsO/J8k5LnE8xJghag56h8IG/n36Xjsl4EGp1i9zdKc0dww7FM1GvdcAk73QqkeCYbLA+15HzQd282bBSKMfa3hgNUulzlY09gwJc5RK8MiibP43bCTgUis4BEIN4ZVmsZVEIyiB69biYAsaFJWXtFclw8XiEvIxmd5c4jDBYDO4Clmf7gr7apkiawc31S4OuWPu0BHwsez4EE5U7QoVPFbG/hEA9PKy2TAnro+Paj4GQe7L+Y9kWUhytEGEgfhvgAHeHqnxkVTMbfbF8RR0qneSaUM6uXEnCdXCrJFh0NBYyug6/jpSxbtbMdV5uvJK4VdcaUjOCwSuAYaF2qLGXaM+v+OVCepkK4sKh8w0dMADLDfGH7VycKoRitBB1vPQp05sSyeSbyZV8ZqOByRfMxIfd59vzxod2LuBa68ne0G+baZN7KQGWM/JLSJRZsj3V0g+Op03eVVIkw66plEOuv2+tDSb/y2q1ZjGTQvgI2jA4iBakRHdDBGwc41PsHRyqWNjdS7CYypPY0tjPwvVMHvC9UX0YeRkjusGlj6SmZYnslnqbxDQQuyRC0FFx6TJCOery08RBIBikdmysZyjtMSPouKvpQNmOI7BrONQuD0EoTtxClEiHAOyIps05FwbwZWwoBQbqdUoNYbARm+KcO22znbqdoFgM6BnxaSXvT46LMS0spmElQx9hMmbDeyMbwz9ARaCaR0+EoWvAMHP6g== 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: Add VM_PFNMAP to vm_flags in the mmap handler to ensure that the mappings would be managed without using struct page. And, in the vm_fault handler, use vmf_insert_pfn to share the page's pfn to userspace instead of directly sharing the page (via struct page *). Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index c40645999648..820c993c8659 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -35,12 +35,13 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) struct vm_area_struct *vma = vmf->vma; struct udmabuf *ubuf = vma->vm_private_data; pgoff_t pgoff = vmf->pgoff; + unsigned long pfn; if (pgoff >= ubuf->pagecount) return VM_FAULT_SIGBUS; - vmf->page = ubuf->pages[pgoff]; - get_page(vmf->page); - return 0; + + pfn = page_to_pfn(ubuf->pages[pgoff]); + return vmf_insert_pfn(vma, vmf->address, pfn); } static const struct vm_operations_struct udmabuf_vm_ops = { @@ -56,6 +57,7 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) vma->vm_ops = &udmabuf_vm_ops; vma->vm_private_data = ubuf; + vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); return 0; } From patchwork Sun Feb 25 07:57:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570758 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 EA35CC54E41 for ; Sun, 25 Feb 2024 08:24:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9537E6B00FD; Sun, 25 Feb 2024 03:24:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 904856B00FF; Sun, 25 Feb 2024 03:24:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 755596B0100; Sun, 25 Feb 2024 03:24:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 60A736B00FF for ; Sun, 25 Feb 2024 03:24:19 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 165EBC022F for ; Sun, 25 Feb 2024 08:24:19 +0000 (UTC) X-FDA: 81829639038.17.E9CEC5A Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf21.hostedemail.com (Postfix) with ESMTP id 338FA1C000C for ; Sun, 25 Feb 2024 08:24:17 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jczufwZQ; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849457; a=rsa-sha256; cv=none; b=0RVvpMp5SCR0BqBlyvbcKYbAyUdRMdiU0trc/1lKh1Woq+r/Ipsjso3vGqjxV5EM9RWj7D YZ8oh+sG9SQZkB1peHrry1QecwvwgovN2HVJUtIpU+wHAQgR3qJUKn3y5ATwtC10mQmL/s D3ybblhbZbXs86XWwRbTwfvK0Arddiw= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jczufwZQ; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 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=1708849457; 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=H5LZX4x0dPh49vNzSTLf7kYd0GJ+7/NAQZVV/ovBC1g=; b=eDTyFtrHY5fm0A6+zH2CclfNUTQWQvDuQ2oRHAqG5VTTR74LunMjdDTisNKygm7UCgKhTU X7un2J43DKqNNkOsf33zk7ZRFH1zVPz/0iPGVUTRJpJ+4V7vU9FIhRWR1uQEozudcJZcoo G6afqWZv8Q84SbbMXw9772Qu5Hzcmek= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849457; x=1740385457; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OFY0cfHR/dm9OGHwjR/w6MaeqHarFwrIQpExmOosGtk=; b=jczufwZQKSCUkUIAU8QmRYaKPrNH5vUvRmixi/tHKb5RG/reNujjrMya Vm/dl5dE90/W4L6XeDMBw/xp0WK8nPxMH8ZZSdfyBC4WtSlE+B/jq6IEp 8fRSk5ScheEuRmH0vXjzNmtEtAYeXO38KTDwceIn4Gg1ekrU1pxnTVK5q ZazJjxYTvf9z5M7LLkobT5b+xLcCa+k3XEI9AHkwpj8y4rmD67zvNnb4u 1vkrwzONuyaV9YaJCi+QlJcQjgxZi2STe/5ZiHVaRPihUCwhQybN2e1A+ hUlPS2GfJ2HVgC0/sgwRkfg5+QHrt32za60ZJlGH+OmLmDYk01uby97xo g==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988374" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988374" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783260" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:13 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v12 5/8] udmabuf: Add back support for mapping hugetlb pages Date: Sat, 24 Feb 2024 23:57:01 -0800 Message-ID: <20240225080008.1019653-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 338FA1C000C X-Stat-Signature: rfsqdmumkb3w96ppxppsfkytima5oxwz X-HE-Tag: 1708849457-73345 X-HE-Meta: U2FsdGVkX18yM2KDOjrmfZCEO/kncF7biMfR68Gdo/RpE6mdTSEPh7nrnFlvkZa+JkXw/WqGxcnv9llBCzWM24WF+sWb6rurQit2tm0eb2V5qzgwsesO/76WfqQeWk/C9eKqAmCzHCV9YPgJkSQMRNmD5GwWypRCN1uNZn71nh5CEQpXIP9ktrBQLg67WkupZbyYNrJxnVgdxTjbjeicyMzWxfxbuXd3iB6mkqDgAlhN/uNCjTeVyxor7mmmUUOcZnAkrufqJfv3VquNH4MI4iIVqlLI52onFJGsDGyF4tFqsDyZLumVQrYUz+x13ymZgxEFNxDz7IIn1pmx4YJ9nJO/IjnVAGkX2i6rXD5cbvEyHTUUhmpsS/jQyJUNR3qWBFCOMnBTDOKzRn342St5zj2ZIMI+rfoD0CTZ7ie55IuY0fVmHxLm3fCePBFuW4ZUMFjPSJlbLJrKIpKpS/srBX5V4av02SUy7OgAZJVibQ4XA9bWdr9uMJNwZXnopEI4RzX7QAlM9cg2wOTDREmio30j+yxlnMqxa7fz0BlWMN8NXJunkepQamK5QF7JIDz6s9JGYLF+eddJxl9ggAnPlQ3ykYYNJWp/Ol9ksSKBmJcgMs5BeSKIxaeMwTsHmf2AD3+giheFZziYYg2uvW3zu8T2ZIj+y6tAFQK4wloZE6rCpeI933beW7ckuzrt6huhmRJsRxMzNpM1+TmWcE1FAcB/uNrg2+bSNpXJseheOr4piS2U8/Gvx/4TUe0MwBFSC6L6HYM7R5N+XQn0ZHsBefUX30brVpFtx4veQjto7/JTq5Wa8dGUKMoUwk+Xy2wHXzU/NVhrR0BraVK++tjPOVjMJmElqXV8ALWQG95L6ijqoboccfUSbPlYQV8FUEMpEqLfZcbVsz387SjdCMl0DMvhyTuwsyMyiZDB0S3TWlDMPJIykKQm7wiWpLJLUJZHlVw5iVYhRHaHFiBa2Ug fHgsUuqQ 6V+va/j02X6VRi0FHcDMpzT1uPraXBYyAV6N5gnks6w3hDsx/sP2cKKaexwAcs2nozgNFXxWFiKAXOsdBUH8C1IJykkNV254zhavaOBpGjQYnLjy7FQ1Ej/mET3dq9NYo0F3dGLcbbhLABVMYBxWpTaQOdYS7HZh0F2u4HyKwf1WWlLVun6t9GA7OkiAZ+cSbQyq5GJU+MbgBGlGb/QSqprvg2SzTrnvFrBxLM93o/BK11/myTb3vBuVoi+3zd7xYlnpq 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: A user or admin can configure a VMM (Qemu) Guest's memory to be backed by hugetlb pages for various reasons. However, a Guest OS would still allocate (and pin) buffers that are backed by regular 4k sized pages. In order to map these buffers and create dma-bufs for them on the Host, we first need to find the hugetlb pages where the buffer allocations are located and then determine the offsets of individual chunks (within those pages) and use this information to eventually populate a scatterlist. Testcase: default_hugepagesz=2M hugepagesz=2M hugepages=2500 options were passed to the Host kernel and Qemu was launched with these relevant options: qemu-system-x86_64 -m 4096m.... -device virtio-gpu-pci,max_outputs=1,blob=true,xres=1920,yres=1080 -display gtk,gl=on -object memory-backend-memfd,hugetlb=on,id=mem1,size=4096M -machine memory-backend=mem1 Replacing -display gtk,gl=on with -display gtk,gl=off above would exercise the mmap handler. Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Acked-by: Mike Kravetz (v2) Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 122 +++++++++++++++++++++++++++++++------- 1 file changed, 101 insertions(+), 21 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 820c993c8659..274defd3fa3e 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,7 @@ struct udmabuf { struct page **pages; struct sg_table *sg; struct miscdevice *device; + pgoff_t *offsets; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -41,6 +43,8 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) return VM_FAULT_SIGBUS; pfn = page_to_pfn(ubuf->pages[pgoff]); + pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT; + return vmf_insert_pfn(vma, vmf->address, pfn); } @@ -90,23 +94,29 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf, { struct udmabuf *ubuf = buf->priv; struct sg_table *sg; + struct scatterlist *sgl; + unsigned int i = 0; int ret; sg = kzalloc(sizeof(*sg), GFP_KERNEL); if (!sg) return ERR_PTR(-ENOMEM); - ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount, - 0, ubuf->pagecount << PAGE_SHIFT, - GFP_KERNEL); + + ret = sg_alloc_table(sg, ubuf->pagecount, GFP_KERNEL); if (ret < 0) - goto err; + goto err_alloc; + + for_each_sg(sg->sgl, sgl, ubuf->pagecount, i) + sg_set_page(sgl, ubuf->pages[i], PAGE_SIZE, ubuf->offsets[i]); + ret = dma_map_sgtable(dev, sg, direction, 0); if (ret < 0) - goto err; + goto err_map; return sg; -err: +err_map: sg_free_table(sg); +err_alloc: kfree(sg); return ERR_PTR(ret); } @@ -143,6 +153,7 @@ static void release_udmabuf(struct dma_buf *buf) for (pg = 0; pg < ubuf->pagecount; pg++) put_page(ubuf->pages[pg]); + kfree(ubuf->offsets); kfree(ubuf->pages); kfree(ubuf); } @@ -196,17 +207,77 @@ static const struct dma_buf_ops udmabuf_ops = { #define SEALS_WANTED (F_SEAL_SHRINK) #define SEALS_DENIED (F_SEAL_WRITE) +static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, + pgoff_t offset, pgoff_t pgcnt, + pgoff_t *pgbuf) +{ + struct hstate *hpstate = hstate_file(memfd); + pgoff_t mapidx = offset >> huge_page_shift(hpstate); + pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; + pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; + struct page *hpage = NULL; + struct folio *folio; + pgoff_t pgidx; + + mapidx <<= huge_page_order(hpstate); + for (pgidx = 0; pgidx < pgcnt; pgidx++) { + if (!hpage) { + folio = __filemap_get_folio(memfd->f_mapping, + mapidx, + FGP_ACCESSED, 0); + if (IS_ERR(folio)) + return PTR_ERR(folio); + + hpage = &folio->page; + } + + get_page(hpage); + ubuf->pages[*pgbuf] = hpage; + ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; + (*pgbuf)++; + if (++subpgoff == maxsubpgs) { + put_page(hpage); + hpage = NULL; + subpgoff = 0; + mapidx += pages_per_huge_page(hpstate); + } + } + + if (hpage) + put_page(hpage); + + return 0; +} + +static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, + pgoff_t offset, pgoff_t pgcnt, + pgoff_t *pgbuf) +{ + pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; + struct page *page; + + for (pgidx = 0; pgidx < pgcnt; pgidx++) { + page = shmem_read_mapping_page(memfd->f_mapping, + pgoff + pgidx); + if (IS_ERR(page)) + return PTR_ERR(page); + + ubuf->pages[*pgbuf] = page; + (*pgbuf)++; + } + + return 0; +} + static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { DEFINE_DMA_BUF_EXPORT_INFO(exp_info); struct file *memfd = NULL; - struct address_space *mapping = NULL; struct udmabuf *ubuf; struct dma_buf *buf; - pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; - struct page *page; + pgoff_t pgcnt, pgbuf = 0, pglimit; int seals, ret = -EINVAL; u32 i, flags; @@ -234,6 +305,12 @@ static long udmabuf_create(struct miscdevice *device, ret = -ENOMEM; goto err; } + ubuf->offsets = kcalloc(ubuf->pagecount, sizeof(*ubuf->offsets), + GFP_KERNEL); + if (!ubuf->offsets) { + ret = -ENOMEM; + goto err; + } pgbuf = 0; for (i = 0; i < head->count; i++) { @@ -241,8 +318,7 @@ static long udmabuf_create(struct miscdevice *device, memfd = fget(list[i].memfd); if (!memfd) goto err; - mapping = memfd->f_mapping; - if (!shmem_mapping(mapping)) + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) goto err; seals = memfd_fcntl(memfd, F_GET_SEALS, 0); if (seals == -EINVAL) @@ -251,16 +327,19 @@ static long udmabuf_create(struct miscdevice *device, if ((seals & SEALS_WANTED) != SEALS_WANTED || (seals & SEALS_DENIED) != 0) goto err; - pgoff = list[i].offset >> PAGE_SHIFT; - pgcnt = list[i].size >> PAGE_SHIFT; - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - page = shmem_read_mapping_page(mapping, pgoff + pgidx); - if (IS_ERR(page)) { - ret = PTR_ERR(page); - goto err; - } - ubuf->pages[pgbuf++] = page; - } + + pgcnt = list[i].size >> PAGE_SHIFT; + if (is_file_hugepages(memfd)) + ret = handle_hugetlb_pages(ubuf, memfd, + list[i].offset, + pgcnt, &pgbuf); + else + ret = handle_shmem_pages(ubuf, memfd, + list[i].offset, + pgcnt, &pgbuf); + if (ret < 0) + goto err; + fput(memfd); memfd = NULL; } @@ -287,6 +366,7 @@ static long udmabuf_create(struct miscdevice *device, put_page(ubuf->pages[--pgbuf]); if (memfd) fput(memfd); + kfree(ubuf->offsets); kfree(ubuf->pages); kfree(ubuf); return ret; From patchwork Sun Feb 25 07:57:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570759 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 741A9C47DD9 for ; Sun, 25 Feb 2024 08:24:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 450576B00FF; Sun, 25 Feb 2024 03:24:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D4576B0103; Sun, 25 Feb 2024 03:24:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 226286B0102; Sun, 25 Feb 2024 03:24:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0CA986B00FF for ; Sun, 25 Feb 2024 03:24:20 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DB57A1602A5 for ; Sun, 25 Feb 2024 08:24:19 +0000 (UTC) X-FDA: 81829639038.18.2196E61 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf14.hostedemail.com (Postfix) with ESMTP id E2744100004 for ; Sun, 25 Feb 2024 08:24:17 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=M5toNqiY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 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=1708849458; 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=LKuz1kue+PFB3PtnGqxy8j+AQVi5RxBcHxsg6i4pFDM=; b=AKwuLJgvcNE465VHnPCEqx6pvu8iL/Zif0WSFc8enRUM5Dc9wx/iQjmiYJsoxicCDPPyeu hmxc4Lq8gaWRillNZDCNZlsbQsVqyysKlsjij5jj44FG7AWZGAXZncxsNj7gGYEscDSv1K LvmkHS2tkjnrPWUBim2csAbe73cn7aA= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=M5toNqiY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849458; a=rsa-sha256; cv=none; b=LDNoI0o06jC+49qnR323q7pebUgfxBSrmclCUWRqHKOacJhNMVozUZTFw7oahcmTjbxKYc 3s+OCt4LoYo8AiUZkQOE9MMKIH4I5HtsnwAkCC2tmEzdKn3wsXqFeG3nPSewlqTCFqRo2A MhK49CBHXfXYv4eA8vQJQkS05EaAZZ4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849458; x=1740385458; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=z2j62I0uVNUXZaGZCUjggQYnU/PyD5X487LNKLaNl5c=; b=M5toNqiYE7JMXKSuAbn5qVtP+eTUi91PUIXdCA99NQXrLuS9XYjKqh3a 4mq+5JoChNkggD8q3ahQ15N3aMCsb/jHsOf1+t0JdDeM3cLybSTM+tO0z oBp/t5oemK2+42lL0JkTQoJ+NeNFNBXJ5EsA9LeY7+KJegQFGhQp7xKRt JCABOaZbsWFyOJ2Ud4F3ioC1R7ze3DFf3nDVVn4DDxD40GodjIhzzjk0N XWy9dCce7egA7tYKztSRnqrSNldE7Ox2w745BwfFc8RLFIaGGpdG+OPKg A5c2F4v7UEvUIp01To93l4UOl1l8xgMq6fqQhN6RqqsdcfWFjhJsLe9qN g==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988386" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988386" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783266" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:13 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v12 6/8] udmabuf: Convert udmabuf driver to use folios Date: Sat, 24 Feb 2024 23:57:02 -0800 Message-ID: <20240225080008.1019653-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: E2744100004 X-Stat-Signature: 31zqr6oqng3b8gajuzgb971fimzxyryx X-Rspam-User: X-HE-Tag: 1708849457-101742 X-HE-Meta: U2FsdGVkX188xebPQBa1V0GCIOg5ppKUJfhCVfMc8iceUVcQp44NFvzHmwgtvA+Lyp9RugSZxlNTL2v1ET+VJU35P+tF4u8UDn3rplDSnIlIgLg0qMgKzPrt0IKN0oYr4iyAuz6pZkbuEgHvxqD74hOd7xG7JcMcU5+88srADTgzA/Zs1IeQ7X2Ey2MobAjV6DvjmxkbAHGptZET5aXobMwySbgM9Iw1ZEXTCth81zWz8DuB7UsKDyEl8KN2jEHOLn3nY/TfbcHhLpr8JZQT0C5ftZ99sdPijp3ZUAzyc1J5oIdhibwL37Y1/21Uaem3a89rA3HTubzMJO6j9Tqzs61DvXGSnVA+WOXHQs4CUarD7MnTByEzyurGbfgc/UC+xy3zcYFi5ZtOGVVNweKifimH01pZmq25QDxarVO3G4JR1yoSNtz4EsPilRYbEGLizGpSkI5ntWmT1FkBVZRZsvagoD1LASJnzN+kkkoX0gDnp7nfzcKpYhTorhG4gMfyTTxqOiFXMZ9YoGwy/GZ4DFlHAEBD0PHJU+FZtR871L4NJyz40X+zWnofxdzd/DwqX+B0Ygrw1DghLAPrp8q8w8SjtKQQ9hoc87H1zcEs4jFPKqcANAomniz5M+aMDKUONR7VF8ZF0OZEaPYtSRespBG56HOYBY1UM+HBkAC9QG11sdjb/LVTcqpybRu3KlpRpmv0rvM153X9sssmPFvyocnXzOrCR6kvgppYkmHIsDq64L4Ax4u5MyXvslTdO5ST9SUBw2H4rdweXOQ6Q1AG9rn6AyNzWro3zeFMraY9dSlbzOpsLSMrDox+jn5cjv3BMZYP8vpbI29H6j2YOdqHsuz9KbBGWlK2ovs9g4VhgZs+oD5H19vK4WpIuqN6BkVmfd8+RQZ2vXj9FzSrswdPv5epmg7OT/uR1ynTS40qHprVXCvuP98oMLhzzDilAsvsX+9E2+yOsINbhn2rRYL Q8nwDIbv rPXGZFfA1J+ThmPJBcLxSyKlrHVdEHd0mViS0ac/4SWENd8k2vtTTLiZoB720+SjDfVyV6meEpn6Jdu4YqE1mJfqxZq7iwkUTPgZ1v8szDNRh/CfylOfqXQG3CT8lRRFqipWEF0SeKQG+n9Sj78e0w/ZN9eMJB514X6QbmiSk0KNM3EqstcwH1LRuq2fS5ZRyYm0B/zyEzj4lv04L5xm1lwifHXKnf1SLGka/4p9m0Xr4kYzy8AjqXeKE/rFZeSksFIzdBbuxSZNRszTMQ5/266f9t/skEjcesChO1Q54n0f0wbJPRwQvsbsT9g== 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 is mainly a preparatory patch to use memfd_pin_folios() API for pinning folios. Using folios instead of pages makes sense as the udmabuf driver needs to handle both shmem and hugetlb cases. However, the function vmap_udmabuf() still needs a list of pages; so, we collect all the head pages into a local array in this case. Other changes in this patch include the addition of helpers for checking the memfd seals and exporting dmabuf. Moving code from udmabuf_create() into these helpers improves readability given that udmabuf_create() is a bit long. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 140 ++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 57 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 274defd3fa3e..a8f3af61f7f2 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -26,7 +26,7 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is struct udmabuf { pgoff_t pagecount; - struct page **pages; + struct folio **folios; struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; @@ -42,7 +42,7 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) if (pgoff >= ubuf->pagecount) return VM_FAULT_SIGBUS; - pfn = page_to_pfn(ubuf->pages[pgoff]); + pfn = folio_pfn(ubuf->folios[pgoff]); pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT; return vmf_insert_pfn(vma, vmf->address, pfn); @@ -68,11 +68,21 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map) { struct udmabuf *ubuf = buf->priv; + struct page **pages; void *vaddr; + pgoff_t pg; dma_resv_assert_held(buf->resv); - vaddr = vm_map_ram(ubuf->pages, ubuf->pagecount, -1); + pages = kmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL); + if (!pages) + return -ENOMEM; + + for (pg = 0; pg < ubuf->pagecount; pg++) + pages[pg] = &ubuf->folios[pg]->page; + + vaddr = vm_map_ram(pages, ubuf->pagecount, -1); + kfree(pages); if (!vaddr) return -EINVAL; @@ -107,7 +117,8 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf, goto err_alloc; for_each_sg(sg->sgl, sgl, ubuf->pagecount, i) - sg_set_page(sgl, ubuf->pages[i], PAGE_SIZE, ubuf->offsets[i]); + sg_set_folio(sgl, ubuf->folios[i], PAGE_SIZE, + ubuf->offsets[i]); ret = dma_map_sgtable(dev, sg, direction, 0); if (ret < 0) @@ -152,9 +163,9 @@ static void release_udmabuf(struct dma_buf *buf) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); for (pg = 0; pg < ubuf->pagecount; pg++) - put_page(ubuf->pages[pg]); + folio_put(ubuf->folios[pg]); kfree(ubuf->offsets); - kfree(ubuf->pages); + kfree(ubuf->folios); kfree(ubuf); } @@ -215,36 +226,33 @@ static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, pgoff_t mapidx = offset >> huge_page_shift(hpstate); pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; - struct page *hpage = NULL; - struct folio *folio; + struct folio *folio = NULL; pgoff_t pgidx; mapidx <<= huge_page_order(hpstate); for (pgidx = 0; pgidx < pgcnt; pgidx++) { - if (!hpage) { + if (!folio) { folio = __filemap_get_folio(memfd->f_mapping, mapidx, FGP_ACCESSED, 0); if (IS_ERR(folio)) return PTR_ERR(folio); - - hpage = &folio->page; } - get_page(hpage); - ubuf->pages[*pgbuf] = hpage; + folio_get(folio); + ubuf->folios[*pgbuf] = folio; ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; (*pgbuf)++; if (++subpgoff == maxsubpgs) { - put_page(hpage); - hpage = NULL; + folio_put(folio); + folio = NULL; subpgoff = 0; mapidx += pages_per_huge_page(hpstate); } } - if (hpage) - put_page(hpage); + if (folio) + folio_put(folio); return 0; } @@ -254,31 +262,69 @@ static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, pgoff_t *pgbuf) { pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; - struct page *page; + struct folio *folio = NULL; for (pgidx = 0; pgidx < pgcnt; pgidx++) { - page = shmem_read_mapping_page(memfd->f_mapping, - pgoff + pgidx); - if (IS_ERR(page)) - return PTR_ERR(page); + folio = shmem_read_folio(memfd->f_mapping, pgoff + pgidx); + if (IS_ERR(folio)) + return PTR_ERR(folio); - ubuf->pages[*pgbuf] = page; + ubuf->folios[*pgbuf] = folio; (*pgbuf)++; } return 0; } +static int check_memfd_seals(struct file *memfd) +{ + int seals; + + if (!memfd) + return -EBADFD; + + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) + return -EBADFD; + + seals = memfd_fcntl(memfd, F_GET_SEALS, 0); + if (seals == -EINVAL) + return -EBADFD; + + if ((seals & SEALS_WANTED) != SEALS_WANTED || + (seals & SEALS_DENIED) != 0) + return -EINVAL; + + return 0; +} + +static int export_udmabuf(struct udmabuf *ubuf, + struct miscdevice *device, + u32 flags) +{ + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct dma_buf *buf; + + ubuf->device = device; + exp_info.ops = &udmabuf_ops; + exp_info.size = ubuf->pagecount << PAGE_SHIFT; + exp_info.priv = ubuf; + exp_info.flags = O_RDWR; + + buf = dma_buf_export(&exp_info); + if (IS_ERR(buf)) + return PTR_ERR(buf); + + return dma_buf_fd(buf, flags); +} + static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { - DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + pgoff_t pgcnt, pgbuf = 0, pglimit; struct file *memfd = NULL; struct udmabuf *ubuf; - struct dma_buf *buf; - pgoff_t pgcnt, pgbuf = 0, pglimit; - int seals, ret = -EINVAL; + int ret = -EINVAL; u32 i, flags; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); @@ -299,9 +345,9 @@ static long udmabuf_create(struct miscdevice *device, if (!ubuf->pagecount) goto err; - ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages), + ubuf->folios = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios), GFP_KERNEL); - if (!ubuf->pages) { + if (!ubuf->folios) { ret = -ENOMEM; goto err; } @@ -314,18 +360,9 @@ static long udmabuf_create(struct miscdevice *device, pgbuf = 0; for (i = 0; i < head->count; i++) { - ret = -EBADFD; memfd = fget(list[i].memfd); - if (!memfd) - goto err; - if (!shmem_file(memfd) && !is_file_hugepages(memfd)) - goto err; - seals = memfd_fcntl(memfd, F_GET_SEALS, 0); - if (seals == -EINVAL) - goto err; - ret = -EINVAL; - if ((seals & SEALS_WANTED) != SEALS_WANTED || - (seals & SEALS_DENIED) != 0) + ret = check_memfd_seals(memfd); + if (ret < 0) goto err; pgcnt = list[i].size >> PAGE_SHIFT; @@ -341,33 +378,22 @@ static long udmabuf_create(struct miscdevice *device, goto err; fput(memfd); - memfd = NULL; } - exp_info.ops = &udmabuf_ops; - exp_info.size = ubuf->pagecount << PAGE_SHIFT; - exp_info.priv = ubuf; - exp_info.flags = O_RDWR; - - ubuf->device = device; - buf = dma_buf_export(&exp_info); - if (IS_ERR(buf)) { - ret = PTR_ERR(buf); + flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0; + ret = export_udmabuf(ubuf, device, flags); + if (ret < 0) goto err; - } - flags = 0; - if (head->flags & UDMABUF_FLAGS_CLOEXEC) - flags |= O_CLOEXEC; - return dma_buf_fd(buf, flags); + return ret; err: while (pgbuf > 0) - put_page(ubuf->pages[--pgbuf]); + folio_put(ubuf->folios[--pgbuf]); if (memfd) fput(memfd); kfree(ubuf->offsets); - kfree(ubuf->pages); + kfree(ubuf->folios); kfree(ubuf); return ret; } From patchwork Sun Feb 25 07:57:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570760 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 E9EE3C54E41 for ; Sun, 25 Feb 2024 08:24:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 97B4C6B0102; Sun, 25 Feb 2024 03:24:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 904876B0103; Sun, 25 Feb 2024 03:24:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66E6A6B0104; Sun, 25 Feb 2024 03:24:20 -0500 (EST) 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 3A47F6B0101 for ; Sun, 25 Feb 2024 03:24:20 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1D9831A036E for ; Sun, 25 Feb 2024 08:24:20 +0000 (UTC) X-FDA: 81829639080.11.C5BAD3B Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf09.hostedemail.com (Postfix) with ESMTP id 4B9E614000F for ; Sun, 25 Feb 2024 08:24:18 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PIDRqP2O; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708849458; 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=S0hncFE+TN2V8h0Vy9nPky+8cKi/00juTYk6MbcQZr4=; b=gpN/URqAG5M0brnkBVSC0SVMcuc6eiEFw86hS/rRX4Iv/1HCho7RxKKKGIVQRp3+xtv1z1 cHkXdqgrUXUOrGIv1juvB1WuuzaHOBjjbQvUlcEfdZbo2lQvyMobzXMOKH+PXDmxrbamJ1 lcXduq3GPpR5hu2LijZgQ2bL5EUT1sU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849458; a=rsa-sha256; cv=none; b=OwNgJoZ0CJYsJju3l7ga+FzOJFlLz9b3D/cMaIiO0hwUDjiYFYYAss67jp4om8HojhAWOi 3GqLYCeAnKTmiF5E5YwFEmIQDIzBowD5F7DDu9ZiMqEgPJQxS8xxkaB7Eib+c0BzrKWZyi cemJqHM5F2mV1xkE05d+HAzYXCJyXv8= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PIDRqP2O; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849458; x=1740385458; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CRNrLA7Zi25moWFEfOmiw/XIaGMGZ6Mt2vHfsiWi2qA=; b=PIDRqP2Oi9bldfA+UPh0WrXcm9FFVNZUiX77hJYI1ji7vuCLAgCBo+O2 84O9xZbBWhKoUG/AoWq+x36hpvyCsgxStK+e/jZUDpc71l8T8uFJRLe1L shWARx/tpbpE9aquoGC5XFO8QbCx7Fwn55TfPj4hot/5ccagL3dDsr90J ON+ZaM9W6Co+quDByeJydzjHXCxSuQ4DfdorZyJy7UrHqIKZG3WT2Pg34 fWIjec5SH9PvhSfMNjDtSyjWFdwQrJXWMZ4Zzugoa1NVXyQlFuoX5BAsx vrMrg2/XpOeQO67Rjh/52Mz5BDp0U9YQGr2bbkxC3uUdLvm6dKLLPmBsl Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988395" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988395" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783274" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:14 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v12 7/8] udmabuf: Pin the pages using memfd_pin_folios() API Date: Sat, 24 Feb 2024 23:57:03 -0800 Message-ID: <20240225080008.1019653-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Stat-Signature: y6s5jdf5c6sr1zdqd4x1oa7daoo47sio X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4B9E614000F X-Rspam-User: X-HE-Tag: 1708849458-936648 X-HE-Meta: U2FsdGVkX1/dA1Hb7/+snUdwU4H+4FoOojkiYJw9le7n2xHGo7pYWhTG7ybEbS9u9lEXmul5v0XA5deORx7aOhzYA9LpO1osSLLL/+Y1OIwAflxWdhxRF+O2orZtMmmnMP1NZ0pVN95CHRu2e2ec47FkyjCnK27UBBhbG7l0U26D681V715zZKBeDCLTQxxs54EIGFAQbgx++hOz3uElLxErRiWkUO+ylEV4rweicq07V/Hv/qOARhAcyJ+i5CIBQaBrqCWTZTJbVB+PhcGZBWt+Xrb1EULB1knv5WPEPPh0E+t1zfhvbxzUr5TS+pufNoWcODa53XDv2YCMRGknp2l02z4DevEenBeg+VbcUCwbphl6+3vjOvurxsDE6K9FL5XyysdVr5eRuIWEBgQLNNeLQxM+zDk9HVDQS6mRgF7ACF8+OUlzrQNstZ8ky1VoZWZe7a1+SkS5hytOEa9MIuuZZAJkAedSuyGvvzOeH+crewM1T3ZFSQoj/i0gJ82KoZ8R251AoifMdoJ65goqYK8x6de4Yd5yCEytYPecfXdohgXL+YqPVATYMPDL2/Bme0mQd+l97HveyZohottsPaidsbDgghbfj9943aRwmIWyiEXB0cu4Fl1qheWGUgrC99Db6X08TbjdzuXj6UZewQJhZoR841dRo6TG2IowbpYPuRQyfzzOZG95Bcp5iW2IPhYn3C2QUT+UvHcTx6f/nZoriMF3ivhV5gWHCOmkbfGDKEiTLVTcvPUmI/xCJeuiYWLp0s0uWM4xKMt3T1M5OVcybA558LfcKgkJbZ0KYkTg2hLjKhyH3ge9PiPtnCNycyZszJyYtd2boh7yzWNVMxpHlyxrJbkXF8gKKEWjCRhNqEmIb9WpCISBI4rsM4m2YXKo3CValIuc3aSswIdsREpasie2DpRr1qcNPFioN1v7l0JQ8hx/7A== 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: Using memfd_pin_folios() will ensure that the pages are pinned correctly using FOLL_PIN. And, this also ensures that we don't accidentally break features such as memory hotunplug as it would not allow pinning pages in the movable zone. Using this new API also simplifies the code as we no longer have to deal with extracting individual pages from their mappings or handle shmem and hugetlb cases separately. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 153 +++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 75 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index a8f3af61f7f2..afa8bfd2a2a9 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -30,6 +30,12 @@ struct udmabuf { struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; + struct list_head unpin_list; +}; + +struct udmabuf_folio { + struct folio *folio; + struct list_head list; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -153,17 +159,43 @@ static void unmap_udmabuf(struct dma_buf_attachment *at, return put_sg_table(at->dev, sg, direction); } +static void unpin_all_folios(struct list_head *unpin_list) +{ + struct udmabuf_folio *ubuf_folio; + + while (!list_empty(unpin_list)) { + ubuf_folio = list_first_entry(unpin_list, + struct udmabuf_folio, list); + unpin_folio(ubuf_folio->folio); + + list_del(&ubuf_folio->list); + kfree(ubuf_folio); + } +} + +static int add_to_unpin_list(struct list_head *unpin_list, + struct folio *folio) +{ + struct udmabuf_folio *ubuf_folio; + + ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL); + if (!ubuf_folio) + return -ENOMEM; + + ubuf_folio->folio = folio; + list_add_tail(&ubuf_folio->list, unpin_list); + return 0; +} + static void release_udmabuf(struct dma_buf *buf) { struct udmabuf *ubuf = buf->priv; struct device *dev = ubuf->device->this_device; - pgoff_t pg; if (ubuf->sg) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); - for (pg = 0; pg < ubuf->pagecount; pg++) - folio_put(ubuf->folios[pg]); + unpin_all_folios(&ubuf->unpin_list); kfree(ubuf->offsets); kfree(ubuf->folios); kfree(ubuf); @@ -218,64 +250,6 @@ static const struct dma_buf_ops udmabuf_ops = { #define SEALS_WANTED (F_SEAL_SHRINK) #define SEALS_DENIED (F_SEAL_WRITE) -static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, - pgoff_t offset, pgoff_t pgcnt, - pgoff_t *pgbuf) -{ - struct hstate *hpstate = hstate_file(memfd); - pgoff_t mapidx = offset >> huge_page_shift(hpstate); - pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; - pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; - struct folio *folio = NULL; - pgoff_t pgidx; - - mapidx <<= huge_page_order(hpstate); - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - if (!folio) { - folio = __filemap_get_folio(memfd->f_mapping, - mapidx, - FGP_ACCESSED, 0); - if (IS_ERR(folio)) - return PTR_ERR(folio); - } - - folio_get(folio); - ubuf->folios[*pgbuf] = folio; - ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; - (*pgbuf)++; - if (++subpgoff == maxsubpgs) { - folio_put(folio); - folio = NULL; - subpgoff = 0; - mapidx += pages_per_huge_page(hpstate); - } - } - - if (folio) - folio_put(folio); - - return 0; -} - -static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, - pgoff_t offset, pgoff_t pgcnt, - pgoff_t *pgbuf) -{ - pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; - struct folio *folio = NULL; - - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - folio = shmem_read_folio(memfd->f_mapping, pgoff + pgidx); - if (IS_ERR(folio)) - return PTR_ERR(folio); - - ubuf->folios[*pgbuf] = folio; - (*pgbuf)++; - } - - return 0; -} - static int check_memfd_seals(struct file *memfd) { int seals; @@ -321,16 +295,19 @@ static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { - pgoff_t pgcnt, pgbuf = 0, pglimit; + pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0; + long nr_folios, ret = -EINVAL; struct file *memfd = NULL; + struct folio **folios; struct udmabuf *ubuf; - int ret = -EINVAL; - u32 i, flags; + u32 i, j, k, flags; + loff_t end; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); if (!ubuf) return -ENOMEM; + INIT_LIST_HEAD(&ubuf->unpin_list); pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { if (!IS_ALIGNED(list[i].offset, PAGE_SIZE)) @@ -366,17 +343,44 @@ static long udmabuf_create(struct miscdevice *device, goto err; pgcnt = list[i].size >> PAGE_SHIFT; - if (is_file_hugepages(memfd)) - ret = handle_hugetlb_pages(ubuf, memfd, - list[i].offset, - pgcnt, &pgbuf); - else - ret = handle_shmem_pages(ubuf, memfd, - list[i].offset, - pgcnt, &pgbuf); - if (ret < 0) + folios = kmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); + if (!folios) { + ret = -ENOMEM; goto err; + } + end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1; + ret = memfd_pin_folios(memfd, list[i].offset, end, + folios, pgcnt, &pgoff); + if (ret < 0) { + kfree(folios); + goto err; + } + + nr_folios = ret; + pgoff >>= PAGE_SHIFT; + for (j = 0, k = 0; j < pgcnt; j++) { + ubuf->folios[pgbuf] = folios[k]; + ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT; + + if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) { + ret = add_to_unpin_list(&ubuf->unpin_list, + folios[k]); + if (ret < 0) { + kfree(folios); + goto err; + } + } + + pgbuf++; + if (++pgoff == folio_nr_pages(folios[k])) { + pgoff = 0; + if (++k == nr_folios) + break; + } + } + + kfree(folios); fput(memfd); } @@ -388,10 +392,9 @@ static long udmabuf_create(struct miscdevice *device, return ret; err: - while (pgbuf > 0) - folio_put(ubuf->folios[--pgbuf]); if (memfd) fput(memfd); + unpin_all_folios(&ubuf->unpin_list); kfree(ubuf->offsets); kfree(ubuf->folios); kfree(ubuf); From patchwork Sun Feb 25 07:57:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570761 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 4AEECC47DD9 for ; Sun, 25 Feb 2024 08:24:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B047D6B0103; Sun, 25 Feb 2024 03:24:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9CC116B0105; Sun, 25 Feb 2024 03:24:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 843926B0106; Sun, 25 Feb 2024 03:24:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5B5986B0103 for ; Sun, 25 Feb 2024 03:24:21 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3D7F3A02FA for ; Sun, 25 Feb 2024 08:24:21 +0000 (UTC) X-FDA: 81829639122.10.3034637 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf21.hostedemail.com (Postfix) with ESMTP id 401001C000C for ; Sun, 25 Feb 2024 08:24:19 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hTXkH1Qw; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849459; a=rsa-sha256; cv=none; b=ifeBJSnTlhSKh4omwe3lV9cTseycx/Mu6QTQRblVuCT6mfCYED9Te+uQPaj7beCPkolkle oFn/VOulNwwX/gdXyW6TT3ig2wXc5F0eru3cYt1I+Qvnb1n91Z0Bt4FtJ8YXEnCuy1y8zB Qn3J2zEOeE7rWsa35IsvUMYTjWze9/w= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=hTXkH1Qw; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 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=1708849459; 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=L3pLh4fLJQF/oufy9DxeNABqnWyH70s/g2oNx/25370=; b=k3QiVVzYK77cKcNlAbSBaa3/ZqMSamcK4r18XXANScrDeqB2piw3b8UTu30MdO7JZHrnDi ryVpbiJz6hYGDB6R2fTKBVAk2wqBODvnndZJcTJrvu2cDS204J40ivTDN9d5QRD0Uzw8I1 xCTlNhl/V077HvBFDlt4KJLAL5QSP0s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849459; x=1740385459; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vDxNOas+i9L0StB2Z+w5S+W+SfphqDLhtnXhh3tvztc=; b=hTXkH1QwqVXHsiimRXz3rzniR1kkeLtRCQVinWixCsMEg12mV0J8KsTJ AWn93ppX6urH8N5DSYC9ttMML0G3vF6yi4jZoP+3k65Xg7qsy2gpHEkzP II/gRetProJKcShi6zpk4iFLDb2qAgsdFuAkQx8OQztvBGHuwSzOiLMV3 3qJzuYfwoquxy2wturkS5cLPDhfsZn4WeeKrR1qvjt84S1ulKV6YZs6FX sJxSLe+7u6Q5L6RGmh3sx7dt3ikIC/+dSWbCntJuTNZaTlkmEcSBpPieJ csAd5CfIynM4jkT3NkSKNk9PDF0Ah1Da4YZ2WlyOtpGZyDJJcGRJ/jtOP w==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988404" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988404" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:15 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783284" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:14 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , Shuah Khan , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v12 8/8] selftests/udmabuf: Add tests to verify data after page migration Date: Sat, 24 Feb 2024 23:57:04 -0800 Message-ID: <20240225080008.1019653-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 401001C000C X-Stat-Signature: 8y1yndk44iwye85zibn36bhxeoi1i6hi X-HE-Tag: 1708849459-31699 X-HE-Meta: U2FsdGVkX1+Q6tFD+8Gt6j41r/0jZYMAyWWQ+9/NLXoDxAA+vLLCrLPFObyBGP/F9Tg1kajXQFfh2KyG8n3mepOAjMSKmdWhq3SRgVpae6UAIG0xmPSu3IjIWQT5OVpRxAXM2wWcl3jI3s39mVvsUTr/WeBoyqJyExELnAE2Wh7GvU+etsh/CWgYOxUFbl2NKVRLN9wBVYZDTt1mABX9JQ3+IDkfbIbZtgZwjWBS+8JKu+QoivuXyPIawtumv1LqYRgXSQP/FYGPpIlDZFEKOfLFshuV5ntXAp8lVEcIb/CGEmGahAHZuDxJFXEwclUXA4SP5/e3Na5iO12K5r9QDuATVTUy3Rb2WHcwStTX4kp/iyPofGluJO0b7eg6G8MMzEQ8q50nlOMqJdoR63GvnZvqlDhccZHWb2jV7gyttTwS2pPkr9t/RXVCzJfgshis1fEcILQP50akGKpLE5CUr4FFouFks5wmqIKurpMqQ0vKDvo3jZFYaK2KKfGbimyV71aFnaZl4ZC+PkGYwwi84XhUM485uP/M62vfgheJ2+PkKKxRmoBYCbBgHbJ9XR/4ligGjQ9xuyP3szI84fY7yEvB7/pdbRcEFRMXDxCrGQ2W3uV6kG7SLUzgBOyNnUyySz1gzNyxPhkyAIGBQrineyIydq8J3jSer9MrWayWKZ/1lkBWqOpFMYi00besDaNOu45ceoMzri3Ot8OatDUHyRyjTG+4TXJJfy8cxPoYeQH/4AafjGNrof6DwZxMAf7hc6V/W2uZ5W6NRpjjVM/6qHhf9TBVcbzimqMZFrjC3CUNUTkGKsLXzPAfIuye6Ku27KDC4kt/X8s9Fl+eQMUJNUJrPBb/WxiRpJOg2ZAskJHRsVc1T2JvCh+MnVDJTW1xmHIA7JUxrXKTES15q6E8FW71hOeCutbZNkSqo4gElqRpkGrXHFKQ8KU3NfsytXxZ6KtFz8p9wiFT34FuZ2v nI7aC0V7 d02E0aq/EMCOMhIjX5Gscxl5apDuYjKWOaIJblRXVB9DPNdhsIP8tTd4d23BnFR74/keOC6Q2GUNjaJaEMbn+W3CvkmqwUq5mydV4IXqjkLIw+EiOZ3cSdQ6KzBsoI4ee6CHG1awOAz2CM6S5K66mjHjKwiekCRNYlmGdtq4MdeDzhjHe+f5c893+6PHizB9+ZIBy85ZbEtTAs1GNLa3zQqZCz8aVt9XjucDRS3jsQNEVWLQM+lHsiKU2lzy+WXRcuuCIrBY6TyHj6ROdlBnaHSKuENOr/HvP2EB9vsUjhNhRGiE+S0G3dNI66w== 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: Since the memfd pages associated with a udmabuf may be migrated as part of udmabuf create, we need to verify the data coherency after successful migration. The new tests added in this patch try to do just that using 4k sized pages and also 2 MB sized huge pages for the memfd. Successful completion of the tests would mean that there is no disconnect between the memfd pages and the ones associated with a udmabuf. And, these tests can also be augmented in the future to test newer udmabuf features (such as handling memfd hole punch). The idea for these tests comes from a patch by Mike Kravetz. Cc: Shuah Khan Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- .../selftests/drivers/dma-buf/udmabuf.c | 151 +++++++++++++++++- 1 file changed, 147 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/drivers/dma-buf/udmabuf.c b/tools/testing/selftests/drivers/dma-buf/udmabuf.c index c812080e304e..d76c813fe652 100644 --- a/tools/testing/selftests/drivers/dma-buf/udmabuf.c +++ b/tools/testing/selftests/drivers/dma-buf/udmabuf.c @@ -9,26 +9,132 @@ #include #include #include +#include #include #include +#include #include #include #define TEST_PREFIX "drivers/dma-buf/udmabuf" #define NUM_PAGES 4 +#define NUM_ENTRIES 4 +#define MEMFD_SIZE 1024 /* in pages */ -static int memfd_create(const char *name, unsigned int flags) +static unsigned int page_size; + +static int create_memfd_with_seals(off64_t size, bool hpage) +{ + int memfd, ret; + unsigned int flags = MFD_ALLOW_SEALING; + + if (hpage) + flags |= MFD_HUGETLB; + + memfd = memfd_create("udmabuf-test", flags); + if (memfd < 0) { + printf("%s: [skip,no-memfd]\n", TEST_PREFIX); + exit(77); + } + + ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) { + printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + exit(77); + } + + ret = ftruncate(memfd, size); + if (ret == -1) { + printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + exit(1); + } + + return memfd; +} + +static int create_udmabuf_list(int devfd, int memfd, off64_t memfd_size) +{ + struct udmabuf_create_list *list; + int ubuf_fd, i; + + list = malloc(sizeof(struct udmabuf_create_list) + + sizeof(struct udmabuf_create_item) * NUM_ENTRIES); + if (!list) { + printf("%s: [FAIL, udmabuf-malloc]\n", TEST_PREFIX); + exit(1); + } + + for (i = 0; i < NUM_ENTRIES; i++) { + list->list[i].memfd = memfd; + list->list[i].offset = i * (memfd_size / NUM_ENTRIES); + list->list[i].size = getpagesize() * NUM_PAGES; + } + + list->count = NUM_ENTRIES; + list->flags = UDMABUF_FLAGS_CLOEXEC; + ubuf_fd = ioctl(devfd, UDMABUF_CREATE_LIST, list); + free(list); + if (ubuf_fd < 0) { + printf("%s: [FAIL, udmabuf-create]\n", TEST_PREFIX); + exit(1); + } + + return ubuf_fd; +} + +static void write_to_memfd(void *addr, off64_t size, char chr) +{ + int i; + + for (i = 0; i < size / page_size; i++) { + *((char *)addr + (i * page_size)) = chr; + } +} + +static void *mmap_fd(int fd, off64_t size) { - return syscall(__NR_memfd_create, name, flags); + void *addr; + + addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + printf("%s: ubuf_fd mmap fail\n", TEST_PREFIX); + exit(1); + } + + return addr; +} + +static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size) +{ + off64_t off; + int i = 0, j, k = 0, ret = 0; + char char1, char2; + + while (i < NUM_ENTRIES) { + off = i * (memfd_size / NUM_ENTRIES); + for (j = 0; j < NUM_PAGES; j++, k++) { + char1 = *((char *)addr1 + off + (j * getpagesize())); + char2 = *((char *)addr2 + (k * getpagesize())); + if (char1 != char2) { + ret = -1; + goto err; + } + } + i++; + } +err: + munmap(addr1, memfd_size); + munmap(addr2, NUM_ENTRIES * NUM_PAGES * getpagesize()); + return ret; } int main(int argc, char *argv[]) { struct udmabuf_create create; int devfd, memfd, buf, ret; - off_t size; - void *mem; + off64_t size; + void *addr1, *addr2; devfd = open("/dev/udmabuf", O_RDWR); if (devfd < 0) { @@ -90,6 +196,9 @@ int main(int argc, char *argv[]) } /* should work */ + page_size = getpagesize(); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); create.memfd = memfd; create.offset = 0; create.size = size; @@ -98,6 +207,40 @@ int main(int argc, char *argv[]) printf("%s: [FAIL,test-4]\n", TEST_PREFIX); exit(1); } + munmap(addr1, size); + close(buf); + close(memfd); + + /* should work (migration of 4k size pages)*/ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, false); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) { + printf("%s: [FAIL,test-5]\n", TEST_PREFIX); + exit(1); + } + close(buf); + close(memfd); + + /* should work (migration of 2MB size huge pages)*/ + page_size = getpagesize() * 512; /* 2 MB */ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, true); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) { + printf("%s: [FAIL,test-6]\n", TEST_PREFIX); + exit(1); + } fprintf(stderr, "%s: ok\n", TEST_PREFIX); close(buf);