From patchwork Sat Jan 13 06:52:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13518878 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 1331CC4707C for ; Sat, 13 Jan 2024 07:16:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43FCA6B0087; Sat, 13 Jan 2024 02:16:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CA056B0085; Sat, 13 Jan 2024 02:16:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 243926B008C; Sat, 13 Jan 2024 02:16:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0E14F6B0085 for ; Sat, 13 Jan 2024 02:16:43 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D516C1201B5 for ; Sat, 13 Jan 2024 07:16:42 +0000 (UTC) X-FDA: 81673430244.25.08BF76D Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf10.hostedemail.com (Postfix) with ESMTP id C67AAC0005 for ; Sat, 13 Jan 2024 07:16:40 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="Ujq/3sQV"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 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=1705130201; 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=51RyVHcz8ejXQz+KmM5laDmGCKyn7tUIikYMRLagD4I=; b=6/+ugbpfvlWqPIk9nZrs4Dt0si/lC2TUSdZiqmwbhKY8v+2OggnawP+hrfe6YNk95DTXyC 0MEF85z6VucUCfIMUTWrc9L8Od4BuCNxvaRrsnhpUtmlwPSUfvwRM8U+QO5f/PFiP9TLHA 3nld87fg8zDnxqI6RGiJSwWgNEy43fA= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="Ujq/3sQV"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705130201; a=rsa-sha256; cv=none; b=3ud2lm9F9u2B7haQnuukZ7cmbje8POlTPcySgDnjfPSUc4G9EtSsoocCX8rL+S0ne4mnYh KeYBzqZNjrIR9TriXhAfHDCggfPIdsFBop3/iIcfjo1Dv5scEmmzki9SKQrJ/Q0UKUokt2 TrJjMg8MCEh5VTSCQIk74HUbzN3ixGY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705130201; x=1736666201; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Mnov74aVUnZ/JsntlOzaB+N14p4KDWATuD5/BakBI78=; b=Ujq/3sQVdkIolRfh+0+8tUTt0FoyuGDGVNAj5Y3taPSPthppBteG35hl P1dfenSeX9WkaG4uV7S6nHjXn3eZxLYAIXWGtG2fPs42GynNEUTfM3Xri WhGrPu+ZVfr/tDOmL0nkJD0syGtWbrVhvj5bWTzcc20Ed/+8FtiiQ3OdH f/bjSf9FyRk9TUGFKLEeZxB+mMRwCWHc3GuZQrVhKMK8AXv2tV25CYwBc u4JfjlBIAppa7ZZrzU0NbvKhmG43RUr3sJQ5jwr+3/Cua9nbd2z5Jfn3q geV9tpwD5NKdkJijBfvv+1iWW4MM/Vvmn7HL6NVteE1yzr69+v03Y1lwJ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="6078114" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="6078114" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:38 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="783269487" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="783269487" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:37 -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 v11 1/8] mm/gup: Introduce unpin_folio/unpin_folios helpers Date: Fri, 12 Jan 2024 22:52:16 -0800 Message-Id: <20240113065223.1532987-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240113065223.1532987-1-vivek.kasireddy@intel.com> References: <20240113065223.1532987-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: C67AAC0005 X-Stat-Signature: b58cakcbajjqtnencmhw8k5mtca48yp6 X-Rspam-User: X-HE-Tag: 1705130200-715081 X-HE-Meta: U2FsdGVkX1/UvfKahsZmM1/HaFoFDdiV2WRdvDM/enPbNa2mPXTynrqzxrEi7zKgrj93fl30sexCEVTZeKleTf3o2tsvxqDFPIMXandDgCq1nZCrePMvoKq0FYOVSXsSrYCUQQ3Cp+RgII6NS44nBRdiLPE00gr7MghJfhZfbaucwS/YqIMiZfiJQGCXy4eo+zyxrlvuku8sTSOW2eRfHF2wzcRbwUQ/BMqmszR0MgOxaaYsY5fxSoXDxMjmbg4uX1v2bHSNT83Su9hgJDTlicsT5DLPSihTf/aGsZ7RPAgH+QG7RVr59HZmXkhNHDA2H9YedeBc9mxZ6t8qctFshLxST1HysqCFvq0YaQisY9h4Y27xTNT40XcCXXrKiYe+V/0Ip0ugEaNjjVMfXz3Cxeu9pMknzv4SMih9sFu9bQOodxQGo4ZrgYDGjeZgFJKo+iRzZ3DrXCHfbUvI3sXjFz9zSckrhlOdgRPYIdA0Gc1tUyUwwJnpS7SjKLIsbVezKjIlBjnwlTTiTDozfztReNYKKYUVmKh+OXfgsaoL7E+Wv9Et+PxpFGI/tJMpTBYzPhqyZ1+pZW4IYdnjP9sJGjwqZ6vSdEgnV93u+ADVdDCh7Pq4lAq+nKinUJzpQHyeB9NOpXnNaOuYEIw5UnWNhhhf3g/bMIiDwYywXtq1hSF6kT9D2jX6fIfluGuNp8+tboupYEcwiD/ib34K+pk4P/kSOg5hXOs3XxouRrgOhMhDSXEhEpJTc8BU1UQ5cXsuWHzFeFxfZqZYX44XRYeR72F8FtxWUedSjeJLi1VyyldFOK/P4RKedPL+pHcOMZBf4O83kpB4sdgxcyV3cT8ZyXl5BUznGK9mWtKs8X5tj8+toaKiXkVbNCCfq+J53cZKq+babGtDmhr4v52E+62a/q46mUhSZ9N7mWeNuSQcyVLqyiSuPJR82xpH70neApUcX2mMUfOMP7r4QeALomw f7YjhAuD JTxf3ho07HaP8CQeqHjZR4375d6Wle0+/ppnJGklvsa8GJjPuEAQVAAQ3o6ufJkQvVA7wPA2Crq7e5YToXFTz/S8zrJfmx7M6yJ45p9esxrZcCLZA+xeG4rXzubCj0ZyzhRbzLT1Ja8TFsfhGu7t7PV/ft+msoVoM3m4OohrAbvetl5lyFvk0sgjeeNUyyL/tQlFXJV8RUrBG4Ia/ZacPwoidknoKFxf7SM6qJ+NlOp33T+MPVyZr9Thq5yRjwDocuV2gwn0kyUrc/KKdnseWVLTUjMGmACmwZZ487EE0xOZguZl6mmVBB8hfVw== 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 da5219b48d52..a0321adaefad 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1585,11 +1585,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 231711efa390..4d7bc4453819 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 Sat Jan 13 06:52:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13518880 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 E318FC4707B for ; Sat, 13 Jan 2024 07:16:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D6228D0005; Sat, 13 Jan 2024 02:16:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80EE28D0003; Sat, 13 Jan 2024 02:16:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 662948D0005; Sat, 13 Jan 2024 02:16:44 -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 486FA8D0003 for ; Sat, 13 Jan 2024 02:16:44 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 148A3140168 for ; Sat, 13 Jan 2024 07:16:44 +0000 (UTC) X-FDA: 81673430328.19.9C344A8 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf30.hostedemail.com (Postfix) with ESMTP id 0CC5680003 for ; Sat, 13 Jan 2024 07:16:41 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lsewqWpz; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf30.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 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=1705130202; 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=i0zAFHA+E4FLBv78drUCnI4hJVhfpf+CECYBKELK8Yo=; b=bsGCB1DBllWzH4bhzK0YGh/uOTDBiFcjFvSEg6iL8P7oGLWOBG+jVOFpKLOK+f0eawn+mS K80v7OJDrsND1hM7vgRknC/yNWXq4Jr6/FKJjTn1WV+UlB6RD4rfj+ez2rnVW45hmM6mwI R7OoB3ekNUY+goi7OJDX70JfjuzVJGU= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lsewqWpz; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf30.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705130202; a=rsa-sha256; cv=none; b=NHT4QX7NRzco+rNBVFIMHWGgUzFN/WHN0VqVxbInNFk6KL1WiUe7CHCpu+yYA/px8D/PWb VmMqT8x+BrBVrPnIqUUkHqtn4p368Ej41wOs6WOrc0F4mMmPlJV35W1Jf6p/8W0jZTrJig xCc2+ZJ+12AL8KI2bOKXC1p5bS1JZac= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705130202; x=1736666202; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O22F+haVAFNBQJcPOK3PWw+AnkziG39TSsVTrN+ZPik=; b=lsewqWpzB0aRztIUY0s1smHJyilDUdQyQ+8IibFhBKG/xzmrsLBsF+ev LdZKJPyaEfU9Yr+vsnjMT4acnNiMRUwglcermO/k4cQNDM9S5hps6J7hB r6U6SXEffZfjHZW3/QlTjT0XZCh/k8sglOBEmrzdlWbimWGvH6+JQvTvo so3+2qIwrBScvg90QGBADXtm099Dq6oV/gBTIUK0l70sm8pw8BwaTBnsi 30bTKyYj9Shl51oi5e+wptQCiDOoHxLT6jnnqIzYr3I1XX58VVoc1aTwk O05oT53iV5dcOX9l0PjRLF+V8xH/InUIrxmE/G11YWsARd0KA0PMNXSyj A==; X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="6078119" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="6078119" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:38 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="783269491" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="783269491" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:37 -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 v11 2/8] mm/gup: Introduce check_and_migrate_movable_folios() Date: Fri, 12 Jan 2024 22:52:17 -0800 Message-Id: <20240113065223.1532987-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240113065223.1532987-1-vivek.kasireddy@intel.com> References: <20240113065223.1532987-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 95tu7prnhnt15s1k3akomwdwdbxee6gt X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0CC5680003 X-HE-Tag: 1705130201-653980 X-HE-Meta: U2FsdGVkX1+zydwZksk8QD4xzujRo8rksQ7ofBFZkl969uDrok+16lwr5tvr++mP/4HGfuSexJtYpEb6A4w32KDT1VuvMz9x48O4m+JHvLzcUKEtJtjpnb4evT80tALMKb0Z1AooAmd6pPCKD0FJgKwnLv24fzYEDxpFGAHIka40utOHZ3WnFIw+iuwx4IJ0ZUTw9Oy47HbG8qYJIGUk2mXsx3ddmFFco7zZ9yg3pGvGNcZ/Z9Q1yr9fuWh8OvrrQNX6Y99wuCjeLM4+QS8G0G/1d//CQohfOs2pmqdBvO+PlMjKVh7fPZWWasONj7zPcXf0jAScaNy5H2jEVG1oxJpLpCOJv2EtYsofOUehx2/XjSjhJHLZgjoru5nuPDuod2quqt4JingyjRj+EY/xyGtsZX0fvU+1CPpHaPTBawxDrBK1Z/DTLDQ1blqp8AhB3EefTizPhyjlGX0ZXCzY1ZLmhDv9RaJRpQ+8jMPB2td/siaGMqyzgKs99X5qi0xqEwdckBwPBpOGji4Kez46Heet4gL9Tuw6iD03T43mtzK4BfoodIHYjIKDfryysxDzLHZ4mOi2T9WquHHfTKbhoJDrUHckKVxBaj1gvPvonVYVI+fuUKK4czlGxkXIopqFUe7FzC8uFZR9u1b5BEel87Gld/QH9UqovJtd0oKJuJgTU6mTXsFAb1WQuvAUl58w5AYVy3SZ2++sOgrmwYOK0BiIEZ8mA5JDPE1svmm2R7IyTcwVe+UDJrp00VhEKEnV2SjMXs4G9Q+u61sxOwiGllRpHpoADhYhjKpbu+sHzHCo3c4Ie8I/MQJmnTVYK0yCR4HotnQK4z3W4n9SOqcsxci50jUjFmKLDnM8bKhVIvx8P35FxQWZxcyptbeWS2MWewdQWDMGKrcq2e3bwbvxi1cl/J79CqkWeYQh0LjhVOz1zE3M0ExNfVxH3KBME9IFPbhVwsBubdEvLKTjTS7 SZ414yRn LDaE+vr20Ch61KoO34IoRoTwIfoUpoEeJ02FjHSsniuPcxTdC3GKOqIG+qMmEAUJq0TXDPPpsPSXthOskobfGbeGexS1hNf3RBgGDA4mEDJFG5mz216iW7XZETf8itTygcbwoBtr9aydqJyqO1BOzvztT0V6k9TdIKytfbKDjWkMS+Wu3gnycucf+Ax2dH/7EQdZQX3pKfXbJROgfwOFp2nS/jdm7+bNEFp0bLgLiY7keRSZYFsU2h3xIcvV2UiJhFucWgnr3e4Tdvs9ue+89LqDDAPiW0MMvatStHaJNvZZoSK2VCuhEr6WykA== 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 4d7bc4453819..00b24a429ba8 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2097,20 +2097,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; @@ -2124,7 +2128,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; } @@ -2136,7 +2140,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)); @@ -2146,27 +2150,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); @@ -2179,23 +2184,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; @@ -2203,15 +2208,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; } @@ -2235,16 +2240,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, @@ -2252,6 +2301,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 Sat Jan 13 06:52:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13518881 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 037DAC4707B for ; Sat, 13 Jan 2024 07:16:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 83C0F8D0007; Sat, 13 Jan 2024 02:16:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 772B68D0006; Sat, 13 Jan 2024 02:16:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 503B18D0007; Sat, 13 Jan 2024 02:16:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2F2218D0003 for ; Sat, 13 Jan 2024 02:16:45 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 04A531C0F16 for ; Sat, 13 Jan 2024 07:16:44 +0000 (UTC) X-FDA: 81673430370.13.120F55B Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf10.hostedemail.com (Postfix) with ESMTP id EE3DFC0005 for ; Sat, 13 Jan 2024 07:16:42 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=TdytgDBf; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 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=1705130203; 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=PdATLzrTLlhv4O2utZPG9dFld3I6Rs5i449B/+Ed/54=; b=EVJAHEX6HHkIsstFVtSK9V4DrfwjHVLGM4OHZS75CqGSEp6jYDscQEh7KtSvD00UUgyVVR RooACdQrulN0ioZFQ57dXdxS9CtA0xKLfajOFksVuVFfnZnZLqsL4vC4PabYBLllPS3K3z BG4Dl6aG0CKrijPcx4/gYflRWbO5pOE= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=TdytgDBf; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705130203; a=rsa-sha256; cv=none; b=a4UuM6gdiW6xaWmTjTu4HbT3LXG3P3DIg8azKMZSlEqtGjN6O5CXQ3a44ibkG0Nn62AxiP UHECFxa6+oMy6MmiKgx62jZtmg/SN348n2Up1GWSzggPSK1jC67KfWoU2Hfjz+T7TOMkNd QgnNCrkb/Nlky/z7iu7eFd+YsRo3Iro= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705130203; x=1736666203; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jAbty5Qumgn/YrmAy3Youb5P3WDG/W4/pB7FRMkfgxA=; b=TdytgDBf4i3/gFCqV3wva5GLk3NtsNYiKeaK/d154wTB/6udmQZ+rUh2 aVhMbvcU3tDzbJgbqf+xaf+9mna+RG5rcchtnUAGjk2vEZb8y35s9sdfH GKznn0jVk3Nmi++gEk2fS/UOPlp0BHzHxkWuS6/iNgFLjX3caOUeaGD9j BPJB4H2lJf2RjWRF3/QLEyth6YR0z8N2VId7nISxZe5pv2Knrde7lBeq8 c57e8w94dujS4zJmp/4P24+Isf7Hh8JEkgw3+3FlhEVen23E5RIyrzArP 6FnfPSYpoqtpgE1qbpr37z0hhHWKHoJrC9p8U6VhtWIRVoj7mJDsL+Ues w==; X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="6078125" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="6078125" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="783269496" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="783269496" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:38 -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 v11 3/8] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Fri, 12 Jan 2024 22:52:18 -0800 Message-Id: <20240113065223.1532987-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240113065223.1532987-1-vivek.kasireddy@intel.com> References: <20240113065223.1532987-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: EE3DFC0005 X-Stat-Signature: c1qbczyig4qj98x36my6s7j3kssmsm54 X-Rspam-User: X-HE-Tag: 1705130202-341122 X-HE-Meta: U2FsdGVkX1/i6SmfQCq2m8RzS9BcvvptmG+QfC5UVu/a2vcC82dk0idCazYlPsVZ4serSIF5YB5fD5hDsCN8+fHqZbMWvsCkKvGI0rmSvBx4pHTFk+WD2EdrNSOlp/QDt7YbfrHNDQkGkgqbGvAB78GgoBFPV4qJzgVW8Yz2nSyJ0WcHBGZ4Eg3Gqvnt26B6713x2OgORo6ofuakh21v5G9ZTSP9+ZBNEAcM6e0RVNRwB2SNPqw8VI/P8hGQdpJjQkOmzqlUOtcLHM24VmywET0546V0eu67bYSz1Q2JvvRtR7wMQ4WDGJOjuPa4eLvF6DhBVBnc6f20bMAbmQCp7ofg4xdt5AcFT0pGwz55BmZSqK46VBJXgbfgkfuof7spBY6aAKkpag8svJ/kQbrJzIqGmJgki35Aj+CaQH5nBM8tJtsPCgy/wHujgiBI0yv3aufHvtfQBlNDr0AdaT8sxTCj99yRVOb1QZs9gaw7uvNDvr24iPufpwlGv4fZR1+DFND6PlTDyIMjIkbmPbeJd/o7AD6zFI7FexQrqRvLWEUIsC0Z8Mp6ddSh7Na7QMH/KvXatwIRzTz71jZ9n8kTHwbYaht058w7RNgt9AJa7gdTRxrBGmRarUY9XNq3iSWETjzLbUoAvGdlWRnAeFCH2+96Oeh7qIFdBQrv36Tc5wahyp+tYg8grqew6Momihnh2Furyl93pWCNVee7tg6AvvJ/nb1oekpPynkKvE+tXhKG1sdLLVODiQ4Z/qFPINFDUFHTgR8ZyZPJ1fkwRNYDp5e+Yd2WeKKC3GF9OSqN7b2Cl3XhvvR6Cwdv6KNM7uUnbwIzUuKY0HXIw0YnFP2u0XYSVJ0p6NTH6WX6LWKNNNRMNNaG0utoBtPRkacei/uYTn5dg+GPPTD7Hz/y4Kli//gG6P/hk27ltJMzQF5I6mAHaGO+5zqbHHmTp+mvrVUJyCiCv0vkuJUUqghgTD6 qVz71cPJ WJrWl+wcWLA2YSip4PhuDQwyjJWHUJtRou+8gXW/ppLnsf1swacMmiFBBVx6rKlP5okstvzF0IyCDrCN2vLOSJNOf37y1ISq5hamlH2Z4b2JbDVfUKfxZEZ2IJIis7kd5dcQJ8NHy7xHhYd8S887YWZ28LkhQrq5000D3qfQaJffIMpvhAtwu1OlN8SFAhNHdVRFBJQyq3PDvH8pPn+J7svA2ZF27lY8xtJ1LU+wHNr1jDq56l9GEbBJh3xttbNq5GyHkKrCydWg8lor3Zb+R1k6kaOI6/MrjhigjltBaI0Hjccl44F9G6GydRnupb3O6OZWtxHkh55sUOBpVLOvV4LxYcR7NnwhXci4h7OGgxGQ3Fvk= 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 a0321adaefad..33ec267afc63 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2474,6 +2474,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 00b24a429ba8..5b2b55d87998 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 @@ -3528,3 +3530,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 Sat Jan 13 06:52:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13518882 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 0D22CC47DA6 for ; Sat, 13 Jan 2024 07:16:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ABA418D0003; Sat, 13 Jan 2024 02:16:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9ACAD940007; Sat, 13 Jan 2024 02:16:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 617768D0003; Sat, 13 Jan 2024 02:16:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4531D8D0006 for ; Sat, 13 Jan 2024 02:16:45 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 21D6B1C0F5E for ; Sat, 13 Jan 2024 07:16:45 +0000 (UTC) X-FDA: 81673430370.15.AC985A7 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf03.hostedemail.com (Postfix) with ESMTP id 1885C20010 for ; Sat, 13 Jan 2024 07:16:42 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gyapHY70; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705130203; a=rsa-sha256; cv=none; b=HxxGfFjaRfp1j1ls7t6qTqszgJranOHM0m/T1NV06xn1+XsVg1rBHSUJ/Z2TsJThYWSdxJ rMAW/cRGc3b49NrPDrGK1VTGuW9WZjnA5oADc2l3tWJh+EqMW2zEhQ4ODO2vbunq4wUajx Rvowy+Q4SdMry58hnAWAOgkDDkAP+o4= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gyapHY70; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 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=1705130203; 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=OFWxI1i+p4efvmikRdB46knSq10ZwkVaViiQ0SsvQMA=; b=XwKH4I0+wD50cQuhGDITPB16mV+t4vnJ4egj0FLbo9jGX+Tkfyk59krcOW6zkMYsEwk0Of nPiY9APce2F0a5o+UZf1XS9apdkcSRhrB5H/1MtmvpE/2l2X+t+7QmM6Jy2WCghn/JLIJL 42x5r6ktHkCE7EyRpQltVbxXHpLjH+8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705130203; x=1736666203; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U5Op4CjhDDAHfKtiDzpe6+9a1b7R8CllBxVTE/TesJE=; b=gyapHY706Mx4aHHjYQrzosDRoUfEwI9pBAWYOf1w1f5kXMikD5ARxrg/ MQyL2YhzKJoQY72tCJxoRFdT3tHswRs4o0W6ZYdWM+7J4KtycrIPIg6lX 3vbfBTgi690iJkLXfSRadCZWS74vfhhKP80s7DVJo+DGN46zm390Zakz7 Qsj3ljQL2s8c7jtGYkJeGh6HuehGiOgPrYOZZLnSJZ+Gz4hPgeCaRLsGv QJZCb8tF7HI9H9sHcX2DW16YDxqWxx0+/qgGOK1p8X79ngrpDMcAXJjvW RKsSLdJOp0xC7z++kqCo6Eg9GqRIyPw2ML+fQfcdMaSrTrkKCojtdBJri w==; X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="6078134" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="6078134" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="783269499" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="783269499" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:38 -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 v11 4/8] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Fri, 12 Jan 2024 22:52:19 -0800 Message-Id: <20240113065223.1532987-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240113065223.1532987-1-vivek.kasireddy@intel.com> References: <20240113065223.1532987-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 1885C20010 X-Stat-Signature: oddd463ew7yfgd31nt9qrj89g35j1831 X-Rspam-User: X-HE-Tag: 1705130202-761054 X-HE-Meta: U2FsdGVkX1+yoVOXZnW4MG/N2q03dvUiIXJvqvxYRmqW+LpkRpkkyzO54XJ+VMCv8pYu4DBihxbWMmh/9S5kA7y24wlHPIsF6WWTkYhMqQ23huFrhhNhlhlv0GvJ0kfuDRmol7tb9WDeSCe1MFSGyVzXYEkZLS03DqCo5PHWrtHe/n8dA4Qhkh3f83aHukimSW3PvrMDmitHhmoZLBZas8KuUBIgEl8FJEUDe8ynN7M9RfNXFXMJIi87cbQBMv3L0q5D+b7aStQ0qlLRoqfD9TZzvSa5x7Q1fSNNa8k/CIpUcpsyghbAPSBegmhT4mpITRqf7QStqHaYTZJgut5tGgUEAL/VpRYljFmu+Id8dUqqTD+Czke7vw8gCSK8JaEZeYNEYhLA+QKBcNVaGPxO4scFW3qrGqCFywrpFCEol5KGi/LGIsASq+J5T+9GfxHnbR0fWyHEC0fuoEoHoHbF2431HkUUlCrShrr7vU0VWH632X4v9lDgOHBJrOPS+tLw4EfZF4YLEkrvweWCsDOkBzzckCDcVL84d+X1mFuMyOmFlEvGhGWa9OY0UjGNVxn/lkDj5fm6cXkWulvRqkNhHLE6ydpDhcqbtlhGXrizrHjOOsx00pXUQ/mQ/JPHrxJEm2HI+l6ArilAxAZjWQQHLIL+Jb6yh2QpvNyOXBDj2VQVvl82CwnV79C4RUo2Ra7ks204rXhXaxin7ERLTM/+NfHMwg7gdsNyDWwcVKDFpu4SPRdbJeFdWChjpOf1LoCbzkJ4JOp1RF4nb/2LN9H8IAXnqbpibEEOPiyG5L97b5TWbNiNVclCWDlH+1FjVrgU5k2ANwvT52v49G9D5lUH03LraKVjMJf2pj9A47XxFy6sT1evqZTmBxgl14etHU2Mk19mPojffzeAF5C6+H60pE7IupLDALxnP72ApJMCSuns+oK7ysJ/wK65UBfB1pWbKUhPWXG8viLExWgCmUC JsTr/tem z5ALJfBFEJfXrx15A4apivNQkzcLI3zZiepxccHIdrez4UdLG4wsjjjBHMLJxHu2AoS1EwVd+rMqgDh3noThKN2+10+3csPT/DPohyG0H42gDagqA3gKz6X+V6eV8ezO9zqBwlL4wJuhTyfCGQCbma7Eqq921dvs44rXUI2IZTsjMypNvMGQ0JLegWGzVWXyX/m/YPKBs6zEy0wE94jSPMKg2i/DNDdOJ95KDKl1TDPH3gN7evgAo4ei/l9k0i2QeXjqcEk0/c6EQD93Yq1A8jUurB1IccuIV08KhzgX5PWyjhnk= 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 Sat Jan 13 06:52:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13518883 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 62357C4707B for ; Sat, 13 Jan 2024 07:16:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8323940008; Sat, 13 Jan 2024 02:16:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A321A940007; Sat, 13 Jan 2024 02:16:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8618F940008; Sat, 13 Jan 2024 02:16:46 -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 6E7C4940007 for ; Sat, 13 Jan 2024 02:16:46 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 40F5312029D for ; Sat, 13 Jan 2024 07:16:46 +0000 (UTC) X-FDA: 81673430412.04.BBCC835 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf30.hostedemail.com (Postfix) with ESMTP id 3989380003 for ; Sat, 13 Jan 2024 07:16:44 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="O7P/9HT1"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf30.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 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=1705130204; 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=0dU13wedLwZY9ah0V9yWtBs4t3lkVrutPqsW6Ir3Rpk=; b=3h6zYZWmtd3oWrYDpsOOLqYshNbEyL/4apxVdXleIK3MxqzzUzSpWSP/bSvD+8CIE/lwtD ryuwO6G3MXqAv3d1bzEXwFK9F2Dm93wY7UhmY3yA80rje5MUE9ghsWpnTM6WdVoOFBepYP I+8ynKzkfx2PiIRt82afekzD6F6UaEk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="O7P/9HT1"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf30.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705130204; a=rsa-sha256; cv=none; b=Jju+v31uVSsloWdm6SOVLq61aaRiHZygkNtQlfHPEweouma81ylwZAOP/q5TOsSzteIU0I SJUAr6Kg/R5/yGTOw14uTvB2lAAKckcYbPx2/THtjEOnk/zF2+tWgRGNvXWRD0xiuF5HAw rReLQkst+K53+V5j+6fZn9IdyvPlIZU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705130204; x=1736666204; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bKLDAWCjAgbs38r1PjGllY85Qw0mgnod6EtQ6GZ8emY=; b=O7P/9HT1wHXX6tIGCP3Mm3AgQVbJyuY6ht1P4HFujQ1eHR5fYDu+5ZVU krRDKoY7gDK/56HYQ2WY23bPkC2Y3HbmJl+87z0pUqgBTUTqCJaGeQh/b Hl82rYiTSXb658CdVnLWjjrAPSDqiPWZtRaNCnri4V+l/INZtUCFZ7XOv 7/TzOQT9kYLUUtEDM6yDx7UHHjcwExnOSX30SMF4sgUDnbuuSrHnDkoD5 OYZ5IEvL9GJMavC27xfmGQ11Kp6bP83BiqDzTL5jcQxgw/J8fJLn5J3nV gz0FbZ6H1C97HZR5NZbypKvBTOmubbyv5/Cf3j8jGhUuGMUFZbiiMQ0G0 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="6078143" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="6078143" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="783269503" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="783269503" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:39 -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 v11 5/8] udmabuf: Add back support for mapping hugetlb pages Date: Fri, 12 Jan 2024 22:52:20 -0800 Message-Id: <20240113065223.1532987-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240113065223.1532987-1-vivek.kasireddy@intel.com> References: <20240113065223.1532987-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: tocmfizxdwkm8j9nqag9x3moabpizzin X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3989380003 X-HE-Tag: 1705130204-246267 X-HE-Meta: U2FsdGVkX1/9LarTm4s9zaiFxfDMjV2PTLaN2p7wfBnKopQ6wWSQYyG9DtiP76hhUc7vSvRVDa0AklTKCnkkBrHfFOuxvRPSp7QyVwe26h9zDJi+8BuKcVQdf/bUov65/I9m/NAX7Kn4qY1XFIXEmYo9kisTZ1h7yFnqNxZL337EE3ioi/eEViQr1vrKLYdSNWjv1V9tWrkOnTzZlSlOmCpdw3A1n0jj5o+0jY2IPcFINPXQnUHCmPTv7x4NOR42UGi6v/9fy55DYmHhULMwVZEQnrWazQh4I8YOb7ThAdDDUz8gfJfLCs4xtxuGGxhvqZX8N1WAzS3vwTKkZ8cFZCXgxP5CZuH7m6ZpHO9Ii/h/tJN6d04hflsw81Fs9aPjeD+wTpkfrnjDwR53F0MlxoHSMvi3nE4mTEZydHsGeGtyNzttOlxPrvPGXwhRilq/NyjWiMdXaY86jD6Tt4bWOeQuOi0i99mGPKlDGk2kvvxJMjOltUizGOxWepJ0OxTh004an5d7E5kBuQerw9UM8yn4wWL3MeEj214A6Yd50QBPMGezjmvDpiRU/cC+FlVR2+YEKDnNOqQVxbMiK2A/p2GGC8Yr3b3gD1FjVzghnj7SBcijja+hKdKcLVm50yeVz37I5zY8W+Bc8NKOmu1MPGL5fXkNh+JVYCFRs8hY2kxQMysK1/5RXykm79JZtKUPwWRWbdQ/eRMil6J3okF5qKhtLLh11xfWScUTf/s3BNQRqUGGtlJzDdkOEvd4QZGdkzQDyBw0KrMVcI+68yPaChKet4HeQIbNA9FUgYSr5gAdpG5kFYJHuFNuTXWW/tTfFVH6CHPQJe4ZfLGfFp5fP1uzLIWqfrbEExmnna0oA5kEcEgb/hcj7O8PNv4fo+bD4bM1FlmZr22+u+VrbR4sMFWY4aA6Yj8QC9mZ5ZA1oef/wr7a72Uxe7AgFgw/OF97Yq0epu+6IS1CtHQstu5 Qo8ROqq6 Jzm+ikQdimiZmStHJG9q21G+w/HDpKaRFa1ZcyF7l4DQlS02a2muWj0O1Dl3VbeXq8oGXz1nN+CnaOuY7Ils1NadzAKcKde/5vF42dOXywXH9BJ4pbAekNmsulWcFqNuLgXRAq9BK8oAnVK6wkvCO0FMSVXmYiH/n96P7evsD5Gmv2BYeI16tbqRESWVjcA3VopYNCCjBMHSVdTc5fv5+8GLH8Dr5cIp4YPmbGQUg5GMxrE/y8UQ7+mYaQPz+lSsX/pei 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 Sat Jan 13 06:52:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13518884 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 18EC0C4707C for ; Sat, 13 Jan 2024 07:17:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6DC7C940009; Sat, 13 Jan 2024 02:16:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 66305940007; Sat, 13 Jan 2024 02:16:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48FEA940009; Sat, 13 Jan 2024 02:16:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2E4A0940007 for ; Sat, 13 Jan 2024 02:16:47 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F1842C01E9 for ; Sat, 13 Jan 2024 07:16:46 +0000 (UTC) X-FDA: 81673430412.30.3D92880 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf10.hostedemail.com (Postfix) with ESMTP id 0E925C0006 for ; Sat, 13 Jan 2024 07:16:44 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MckNWJ8U; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 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=1705130205; 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=lN0EG7A26DBWW/HGds1B8IVa/CJiihPWo4sxRUNdrfA=; b=lk9DxC49sA2JIJxkZ2abg9U4ILrmY8mkpH1FdkiJKJx07SDUfcmIH4AajL4302+HUohHGZ AJX02Ez+K2HNq7LTzU3HIV65wOaALdzunNMwfjuMc1Zi83aHYACxF8RXxYzSqdXEobX95w w7QVljIVM+v8pw37FrnPi+YS/T3VTwk= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MckNWJ8U; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705130205; a=rsa-sha256; cv=none; b=fl2lYcSkQHWHRmTq/iKuQ9/1YzqFbmHSGlfUYmpoA811hpf52NWX39vZapqmEZVkTLxhV5 3t1FsAkhi7DIjdh7VooazbKnWALssQjScBWkum3UGWM+6U8YHYn2j3bFgYyuHifa2WfiqD a8qVEynypWVrnBmJ+/VD/38vY0CbUeI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705130205; x=1736666205; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dh8/SqNpnEkNrlEC9xXvCsExkuNWJclv0Euiye+F/mY=; b=MckNWJ8UtwPm/jEGH95GSaQhMfiANKwQWZeGIkjcHocH63rPBU40Rk4j TVaOasFIsd/oW//4Q894B6Z9WKNvWwGpTOYTwyl1lwdDpbpsyFSAnQYTR 8n+elldtqDQlEPdWfsV7UPd68f4BgQLm4/TMgsJLZRq4CFvRnFBGFtePp NsQ3ZU1eXwrpzRQIqDBaOnlezhbecMR7YNxQMc6w/mc1j+R1Mo4tFBOfq Si/TfBMk3yVvJu1uKahmcwC2qApk5ed7dEJx7gAJs6tcUjIO9PO6YxfyW 7rjaF+wpL02aARroDrnG2IKJEh3s/VOl/tQ0yXK5qzKJtBobofo/pBwGm w==; X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="6078150" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="6078150" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="783269507" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="783269507" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:40 -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 v11 6/8] udmabuf: Convert udmabuf driver to use folios Date: Fri, 12 Jan 2024 22:52:21 -0800 Message-Id: <20240113065223.1532987-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240113065223.1532987-1-vivek.kasireddy@intel.com> References: <20240113065223.1532987-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 0E925C0006 X-Stat-Signature: 4whg63su6bepao3hkz5qoq9uuijrgnpn X-Rspam-User: X-HE-Tag: 1705130204-684718 X-HE-Meta: U2FsdGVkX1+CXcQFy8dsAz2vjittOTAhoqa9YU0rzgrtWFqONH74Is9It9m8r5GlW1i1AwvA+bQNmqTrhKkWYUHTOB99jX9jLoUFds72ujDWjx8QMtEokWsPC3ivn6Ie8qnvvsn3+FyXxSKvZsCXgNGhthd24/4EMAjodi8rx/C7rL8hqL0sysu/wHTGHRY1IrN6NKhIjzYpqhNfW5d5L8dUggl4JbkZJk4ijENU2Hvtvk4b2HXOVfvbBlIhtV40BMNhUJwnDwmqH93K6KyiJEM0/24fnl7g5sXTtJ102CGAPmLCQX110aATavgAcLnAqre+Easm2O/qR0reetg0JamrIYO13tPJF4wpe07gJPaPRW9TgYtDZTCHVLOS3FRUEBgNrU+BKejy5W5MUaWS87r2qxnrExgxHavIHGQzudspz4HJucn2MgnzCbF0dtHKI/W0l5zsKOul9mL+qPaWmWDkwxAjvygjSWFGSzaK48P+v6XSaGBJdKMwfPVidMtLhaWywm/F0RdWsmlnbWR8q9V0nCsqqgX+nr9Tec9jxm84LjRnKOoIUQQ21LxPpfeBb4PwEyYMBB8MwOTCjuncovFUJMLdrN9Jboiv4GCQrIGit2H9M5aAjURr2h+1meQ6EfwNBtmyGtveJZ4+NTD5MJgGsTc64rQhrooxTcbOXki1AtxNZZYGCmbFqL4Yhj9HuzeS9+/VGe/2xTumhLuXm1igia1iKgqGpqd/yrdg8c5ewQc6Uxk2pYRR7XrpYwlQYH7rbOcIlmbPi8/NAdniLSdRKmSNOu5OXLX5QIL9Bx7lx2FTTATyrEZiICObiHXOFcRgGhp5FxE1ZNrmAgkTP0ZFtcSfUu+VN3HTuR+rmKyJEjyl+fu0scrIeuuxXadJAXmmG2GHRRZ5I8GoXdjA+RAKwvNotVOTlyiW8dbMA7OTM7Gbye9CopDymYi39TdKlMsX8MH11t8+iGFEVkG PwWKiAKX baDK+nnjDO3upN9xfZ4yVZblYyysBHg+Gmpwt/3dEN6j+8vGDBKWOb2iu2jMrtJCYIJwA7AvKe+dljA2OOTpmly92620HOWe+t4phoFY6RoOS/j3GWge1NBh1BzBpI7vyyqrZg/HoZ09VNu+/FwTLRUe5wGUmreLb+suPvmhCNmfkFgadUnZr3ydUkZtQWgSb3j4rpOzliEmQJMAVh+nSbCR+ZO+TvE+1tdWK9DZ0n7WM1/nkk4xExlyDX1Cm7hcxrMoV8mFy4JaP1bHzN/0i0Fd6wRDkNlGe2jyriGkpdcj/R4hUA/DxqJ4+lQ== 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 Sat Jan 13 06:52:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13518885 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 D1099C47422 for ; Sat, 13 Jan 2024 07:17:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C08FB940007; Sat, 13 Jan 2024 02:16:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B912D94000A; Sat, 13 Jan 2024 02:16:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E597940007; Sat, 13 Jan 2024 02:16:47 -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 7F78B94000A for ; Sat, 13 Jan 2024 02:16:47 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5D41C401E9 for ; Sat, 13 Jan 2024 07:16:47 +0000 (UTC) X-FDA: 81673430454.29.21E4FD7 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf03.hostedemail.com (Postfix) with ESMTP id 41BBA20019 for ; Sat, 13 Jan 2024 07:16:45 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="dzMPM/wO"; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705130205; a=rsa-sha256; cv=none; b=unQoVOQ4LYWAAIQBrNXzP104iMNSTKIz6axNWyO0mGcGp/KngoNb9mUbtUeelhEDAGXH21 BR6jcnAb1sqcdi2AOn6pFZ0QbcLq6cKrdJkSrGG2i+XGYNCnXgr98GvMksXcBYhiONUq48 sgri2qWp5hspN/VcjZTink5okwUTM6c= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="dzMPM/wO"; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 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=1705130205; 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=yOgBVQKmKDj4PD+KJHINMdpEC7EG7X90VypbiNBAVbQ=; b=JA6M25Gw5jjmB/5s0f4q9qILG2Pp4NVStEYL+akhaxPCVGk4xU7h6FdjDRD3aMzeEj7bTo Ncq4SAgJqka2W0mMoqTorqYeET8klTGUis/s8FwIBf7mLHmnDJf7gkkiQyczTZ8odtx4wn dJ4S5kn/ku7RVgxJXM8KpAGLYZOh/g0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705130205; x=1736666205; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CPdbE6dv4gTz9/ceu4Ozo3TWtNEi41mmHjF0rn+A6NA=; b=dzMPM/wOOFauVryvh44Jm2gt1awjdqiURREAZkxyOAlTPPPy05mX5mbG C0/1Qfr4meCZ6MdD6tNu6gT/OFljGw8yBQQXaekx14VCkBBB37qI69QvM cHWoeFy/bHFsdEbpqMlcOmRi0ILLL9pElIjEd9ULbO4UdzYWvieY4Qyn8 QPASdoe0c7jye/EttRcJaJ4g20P7yysKCMCAB1DTWpr9Qg7tZCZWuq7nW i86xjp1Q6VsoB+U/ENs/L2L+9KqqkFhBTCV6/YFtjzJ2Smxi9Tq68pJcO lpDSMm3DbJpmFAiVxvRkTiosY8AHm/glfpSD2wmQVdlx2mPSu7LyQ0l8g A==; X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="6078158" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="6078158" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:41 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="783269515" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="783269515" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:40 -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 v11 7/8] udmabuf: Pin the pages using memfd_pin_folios() API Date: Fri, 12 Jan 2024 22:52:22 -0800 Message-Id: <20240113065223.1532987-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240113065223.1532987-1-vivek.kasireddy@intel.com> References: <20240113065223.1532987-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 41BBA20019 X-Stat-Signature: o7yx357qrgkjacs9o4y4yebjkm4yrb1c X-Rspam-User: X-HE-Tag: 1705130205-32594 X-HE-Meta: U2FsdGVkX191YE8ms0awWwoOKzVq7DGD2AZW9L4XVoPpMHaBYsWVnmelXKLpEIA/r/Q44lTw1WBnFFnQFFPUeXFMGTsgWVNf8T2xkhd2OY+Dx8FweONo3cmV+MLqAsra00PjLt0ctdeEqzCr71uHCNZJpUQjXJR0/ID7DGJAWYwY/aaNA58jzyjh69zqczyUqHyNIRJegv9GRQK0NOBPJfMUakaBbxEmwv1Fn/XDZQNZpZFA0ADTTtUVJQEjmFvBuwwcHp+IGB1R9kFhqwO0gAyD6Pj/gMbXDIWB8kU7oMcGdWA1gXT39R7r0VTVtUF/+UNeqThLRT1iePY8ao0h5eZ/v6kjbNLfzSBcOZOHRRnPR0VHRPdw75FFO8FGGhkPAE6/FNkLpRia9yDVrTvi6RNM9j7VlBO4n+IYLQjMEeA3UQUJ+buS8+g5BXAIBI3zy/mgb5MLmBZC/SXAGnBrN9WWSaHGr7tyjmZq5F2N7Ndf8Atk5bhtaSqEdV0KyC6jS6BWNAjPDM7aJAGoXcLJP2oWBoziJhty9maBB8yx9om/nlmvXKBTZ4dFjyvc9r5EELJkP7MVALmTPpwZVPvMU2bO5pw091nip7+Kr7M9siOlVBG3dU7iPSiMKy4ytgoNBAM9ZijLtc2LcEMplnaObyIMklTM/GCVa6HD8OLdoY0MpIyFSrZXQo/nkgb0hzMkXD3QwRu/5UBQ9SKthHobeDb1s0UlUcJ6vDp2p12jpzYu3Y95ARGqK9MHDtBBVvbblwilRgfDB8NjRgxUGHl1Hdu33y4NtO5a6JlksmcTlBKbFGtOZm+DV40rY+YLhz8h9WYGF5HqMQ+iESyavCPbLmFffX862jE87wyLQEtbJ4LOFPdRj0+ucLZ0/usQo3pvh0p4up7EX+LWi+bwCemqOXvHx1/cMCCVygA2IefKge1sxi0Z/qXo85Pl5EkZrzxNdYFm7yueQVtRDIB3YFc QQPC+8F1 jXEVPie6sPw7+9QVaAYMYV3wKWeLphqWoFc7i3pHW3DnY0cdYns1rWwk7DCwluqwrezkIyMuJwcMVWpOVqJSOiw79weqv2pDK1Y8fJUw/YOMGXTkLqZvL9EE6DPAknevtLSaPUX2tuqbYyxFH+RyUsFcOPP62ngGVRFXT1ZB0QT1X0K9PHAGxy8WH6OgKNfmKfHhwwuhaQHtD0WvFDQh+sEmUasrR1AG1xRdiDe4GXwRvMUMjK/zMmBURM6OK1Eupd2pahivMe828FkBxDrukRGe2ZWn1Ax1jLb5s6e8eOVpLMQzgvKiTBD/50EHXMAhYQYW4WyrgBsmwYjyuQRXQqKA56lfplcxRqyX6Hmq3urGbI5A= 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 Sat Jan 13 06:52:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13518886 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 9146AC4707C for ; Sat, 13 Jan 2024 07:17:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF78B94000B; Sat, 13 Jan 2024 02:16:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A802F94000A; Sat, 13 Jan 2024 02:16:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8866194000B; Sat, 13 Jan 2024 02:16:48 -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 6D14D94000A for ; Sat, 13 Jan 2024 02:16:48 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 501A7A1E44 for ; Sat, 13 Jan 2024 07:16:48 +0000 (UTC) X-FDA: 81673430496.26.A683075 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf30.hostedemail.com (Postfix) with ESMTP id 5B36680003 for ; Sat, 13 Jan 2024 07:16:46 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Exc0YdPO; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf30.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 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=1705130206; 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=vsjLvb1MVElc3705OSeKS7FFWwBMLjZD4BZGoHyBC3Q=; b=OMH+JASkbNAHgUNX/hVlsMG5E8pquAMSHQNhIxXKhdfMlDbnwtOtbZNhDTwTOCjhZ9GYWh iosCvIPoAoyAypwbhc9pa0bRJSAohb6BAUeIWY+ydcBnfDKpvTWleiV2Os/N6HXViMF9Z9 2zClJTQVU7Pa55sPBaLB888vn98S9MA= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Exc0YdPO; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf30.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705130206; a=rsa-sha256; cv=none; b=4RaeLI6tOHjx4LMVOps64cSubY8hp7umU1fcQ3X5BeeAeB2dlphvHKeCzlE4NHYJK3dC84 zTNgpFx2Va3f2ObrNDpsH1r6vczv9PTt8dDSRWrxijs6GoJ1Jk/zNg+dhfldzfpufbc+HV V5Cu2w6tC5iW+ikhYOPoUw8Er+azVRs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705130206; x=1736666206; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HkaJB5MACRgiA2p4lexf6azWCkfvx51m7Bj0EsrYLok=; b=Exc0YdPOoovc7EwBXip7sY7P1eBqqh88F4Qfd2MDbc2aIZxmmZY0DhZK 9J9nPSHHnHC9FcLGBS8sJlEW2eDd4kGljLz292ErnKKXKV4x4iONpmoB+ x3F/KcM5bTd1EAhmXHfFo8UiFtR4j5S3Ssq6WkjCv8/uYhwDRPQY/qsbH VbY9G9ryWr6raM5oFTcANA0GDVWXA7EVf1gabMkQTC3GlExIEV+t4BWZf 9e+G9DSYk+QGktyPLMWbUQk1IhzKlhYOIQpLwPT2rcJLkeYNz4sblSWGr DRiInSqLRQ0hMZqsZtJcx9IdgYsGPLeQkF8QIocMxQE3sUmPjRGORbHDh g==; X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="6078166" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="6078166" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10951"; a="783269519" X-IronPort-AV: E=Sophos;i="6.04,191,1695711600"; d="scan'208";a="783269519" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2024 23:16:41 -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 v11 8/8] selftests/udmabuf: Add tests to verify data after page migration Date: Fri, 12 Jan 2024 22:52:23 -0800 Message-Id: <20240113065223.1532987-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240113065223.1532987-1-vivek.kasireddy@intel.com> References: <20240113065223.1532987-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: b5wz7byyif9fuchtg185578348qdfqr5 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5B36680003 X-HE-Tag: 1705130206-474567 X-HE-Meta: U2FsdGVkX1+NAuPw7YAYQZi6V/9aHh/XxjdRnM/JPL8pD04Ley8rDqw+eIgqTTljJIGU84ExAwxfEzJ9QEznl1zqCZgXe3jag+Y9r+GEpuMJoKq9Izv/aJeEhABwomuYn3mQE/OSzh99odxGONBWmxBmKGsFfpelH1S5I14nJb2mhRh36W/dbuiu6AJ0xpSE4bEWEUDl8V4JJ11rEVpCwtHwJfMV3wG4A3g69wb0VrLIZcs7MJAL3UFvJ1tl01RdElQCkfYh14iktQHQcBSefoM5nYU4AQGeb2bOn5sXacaIPcz6IiC16MMM1VP3WgnDAWtBuH1R7jXVlBlOZtKNN9LFCKPef8jJU8E6LFkalOm/ayk9fIGtusIEfUJkliYmx11NcdEdxHxbWSueRbOTyLvR6rUWQRS0/R8rWGXNuWXfyHuOXxN+68PNjP3C/OtUwl0ljEwsPgXsopKtkK91RYkSsT9EocAO2wCj7jiBR2+U0S1N2TnmAFUhWjEC7M3p2hSTX86ha1IHi86n6uCgHQlNx4Zw5AyIq99wJ3+jO6VCC1f6eFmyT5NIlfeqjqVZARUKTq5RbB2cYwt+PcGLBw/NrKOlQsYZR9+m0DzqxPl5oNuRMxgoa/ljP4KIlo7JPglb7wmQLcRvbHnLV3DWCUDOvYo8wakGQkO8Ekk/QbBj6zqz9Xs7jyQFab9N576pPcDjtZxxPyERMVpI+FkjMO90ZJQOPaKT9TiVe2+u/mmfNcVe41rz4QwhLsyaB+PMrW67FwadUwieTAfeGg3DMT1C9IM/GESPNHkRf3BCLTjTjTu83sOC4chwSvCKqicsM5tPGeuceDI0sTp77puctkEkBiFNs4aZyZtumH4JEzBn4kVgH7sT44e7hAEcRZYeoCqyN68vSMdPGgh9YiDi+Ss9dQuv19rPuVhUj1+0VPI9ZZe3ujYdYW4WQlUvBaSyOZxunY8d9PaApkMXlAy 9V+jBwqu wabA8wFAAKpWItF2pfcjHsxX5/SW81AxSca7rExCl/Ix1Vnc6EUoiOvzgzm5A0INUmdLXKWuZjW126DGxY37QGe90VYiSSBBA5B+omp2LXPUd6Hjq7hgYcdW+Z1ozOfoAyKudpXS/+Vtp080hQT4UtiyChWhbGyLGNVUSy3ZJhOir22cPUwJaF6MgIeGPWAJZHMg78t+N4iO8WyLIM2brvArTNmsF62cj/996zUP23eSJjOiBE8wwRh1b8E04MlIyyKnc5qdM52PRcYLew22dwaXY8lwyRtUXLGTxKGDBwWqsK3IEPoFYMW/c5g== 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);