From patchwork Thu Apr 11 06:59:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13625509 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 385BACD129C for ; Thu, 11 Apr 2024 07:26:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 93CD76B0087; Thu, 11 Apr 2024 03:26:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9141A6B0088; Thu, 11 Apr 2024 03:26:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 766056B008A; Thu, 11 Apr 2024 03:26:25 -0400 (EDT) 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 534E06B0087 for ; Thu, 11 Apr 2024 03:26:25 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 20E7E140A9E for ; Thu, 11 Apr 2024 07:26:25 +0000 (UTC) X-FDA: 81996417930.23.B8C7C0D Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf14.hostedemail.com (Postfix) with ESMTP id 0BA1E100003 for ; Thu, 11 Apr 2024 07:26:22 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZW2kvnew; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820383; a=rsa-sha256; cv=none; b=w1W4ExngIuh5RdsT4hbz7X0luiTrAHVmlGNfNm41qEXPOBvR2kryZ+3kFhyBvzNUMZlvpM ynYEkKIMGqpjwsFSsGnNFBpPsELVXCjWsRAtiLlGjGNajn9sghvKcM9CTHdZ+wpnDDXMD2 TLCg+Uh65notCU7CNy5uAsIUEimN6FY= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZW2kvnew; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820383; 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=FqinJm5E4FoubuFAIPxp3KAeU2kYZz+fjdR1LgnUvX0=; b=F2vHESOG7+FpWKURf61dSUVPkerdu5gu15J2W/JqEQKwQEaZMr3/u8ylcEB+nEWjg9hE6g ZdmHsPIdCVHyQrEWVUzjYgC+k13GJptQ8LOe5bbstPpow06xGp/ozWdfX5DA3yja+eJlXh MkNwY2wFcOF6PTtFxGr3hyYkLh74GzI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712820383; x=1744356383; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bISZgHGBceajd5sS0GG6xpyZAR2HBITRWLbAKVvkKH4=; b=ZW2kvnewgFZCl6bofPIWd9THMac+1onwXq6KYJk1p6sLqg17wsK+QulU D+4p6OuNRFk5bGvbsjkVI8QoZD15c6kHmjW4UrP4VlQLVohPSr3BRAgPP qGcTmmZJksv8YgXAp/OVLPL1VjqbRT1OdrQg3OYjexGZQqwUmtN2/uns7 SdUH+jf0vZ/7Y8WB/qwj0d3TPWaPKYNknLUGg2Pn4PT2Lh1LSmg6hp6by sEdQUG8s+V92tM87DuuyPMgSV1wQpUe2mJUtNv84QeheHAqr+e1vV8NRh 0staBXD6e8XQBg1uDys4dyPCd9FaN2ojyK9hP9aH1iq1qbN5M24XDszE4 Q==; X-CSE-ConnectionGUID: pGpTLG16Tyaod7kpgQpQ4Q== X-CSE-MsgGUID: rdkF6hQ2ToyU8pWgvUARgg== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8074350" X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="8074350" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 X-CSE-ConnectionGUID: 2RFC9XLqS6eYHtN2ypTweA== X-CSE-MsgGUID: xUymOPFkT16f2zWGvUfKbA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="20848535" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Jason Gunthorpe , Peter Xu Subject: [PATCH v14 1/8] mm/gup: Introduce unpin_folio/unpin_folios helpers Date: Wed, 10 Apr 2024 23:59:37 -0700 Message-ID: <20240411070157.3318425-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411070157.3318425-1-vivek.kasireddy@intel.com> References: <20240411070157.3318425-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 0BA1E100003 X-Stat-Signature: ktdsag45p3wup3wjgkbxq3c8fcsahoys X-Rspam-User: X-HE-Tag: 1712820382-189143 X-HE-Meta: U2FsdGVkX18dDoZkWq5RzXSzKBHjLVSwuBYVeakeoNhtFzWZzYHSYEInqks2r+7wnmqQyo+1Pa+P1/XbGlHmmzjzgtjhUBGOo/E31UfVEmfnyZEcNsUFPr4VPGWFkeAthmN09nSPPHKLXNeH0XRn2Q81AsoZSWZHyfMv2VS8oqpuPI8YJOLsd6lXHxpHIKQAj5DO6lcKXv7j7+HXXld9MeNDZBpiLRoXdlTkW2bcJ/FFFvvuW6DHBMnFTVfOOzJaAHfucSndrh8JSP+ElgnzFEd4Hk7vudds8YcxDPVBrd/OZxPPJ3E+C9x+qh57PBOrf42OxzQeN2FNZfz4Hyh4CkidSM0yUuxwaEKn/oWv3BYuc8GE+bhr4c2v497N16PqvQizXDdhj1TOPFnAO+fKX+Wbig+c5eUJqPTpN+ka/ciXNpzdVRoMFnGzVE8cRohTcKCTmMp3qNob55ZGnP0XEpM0zP2v7/566rgXFhvli16/izGpChuaZfrcnDrMBVbVpT/Ur7F28S+EspMGCjibqYjCAsJScLlwwUVdKfEXH4NAkbHslGde5mLyWXZOVChRha1gpj6Ie84Odq7uNg+yCUNxlz+jMvfS3IPWGeuu040vB/AxiYf4jyIWvZRil/FmpRIbM7R8JBOt6lnSZzwbgdNqQ2EkoyBV/BSTokZQESqfcNfj3FdsUcjyd7dABlVEOfHCqb3NF1ZrsN2q3i/fiDw6YlFE6/gYLG7b9yozFpYkrpHcTcaPhHZvlToYIi6/0uKVlI0+5mnd1Z6UdrHzjjn59M8eJu4QC1CyQkirfHCuneskJVgSE8tOR32Ish5sYRS2iGC7v/uOIA4dF8BY5SK2vV2b5mJZ19BhobrcM32iqItgXD1ceX1uJJw+3qghYivkfCnrhJAEJ07dHQqjEc7Auk/XXAhvoYVvCpuOJtsQ1RpivPMJV6yT2npy2XVWCTOfXVanJFqQgiYOWOx G2RtAezS mk44VHl+faOSgotKoeMj3FTQn0P2VUp1Sy4xL4GrGo+mVq6kVCNikaxWZ/jBAFqKVtkOjuTgdZvqKNY3lbwhXNZtd1DMHIThhl7tXcPIDNC3jn0FKuOsfjavag7XRPIVmJeT8Dtlp82TMoknLd1rtyZsPk6nUBtTpakGdJN6hr8qMlDO1zDoIXTE1/sJ/vRf3vrUWyhKnwABLb1MyifP99l8zcsa3Fbn+GIoz5er9fjeEmPFv2EscsFy1+dvzeTDtPvRawgdP5UCDMaiAO0mQb904dRjFzmftvucPAraMa1a1fjhYkWt+yURWoQ== 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. We should probably sanity check the folio as part of unpin similar to how it is done in unpin_user_page/unpin_user_pages but we cannot cleanly do that at the moment without also checking the subpage. Therefore, sanity checking needs to be added to these routines once we have a way to determine if any given folio is anon-exclusive (via a per folio AnonExclusive flag). Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Peter Xu Suggested-by: David Hildenbrand Reviewed-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- include/linux/mm.h | 2 ++ mm/gup.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index ef34cf54c14f..d1ebd7a25105 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1612,11 +1612,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 8dcbeae714e2..14e94fdfa827 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -276,6 +276,19 @@ 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(). + */ +void unpin_folio(struct folio *folio) +{ + gup_put_folio(folio, 1, FOLL_PIN); +} +EXPORT_SYMBOL_GPL(unpin_folio); + /** * folio_add_pin - Try to get an additional pin on a pinned folio * @folio: The folio to be pinned @@ -488,6 +501,40 @@ 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 gup_put_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; + + 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_GPL(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 Thu Apr 11 06:59:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13625508 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 ABC07CD1299 for ; Thu, 11 Apr 2024 07:26:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D35986B0085; Thu, 11 Apr 2024 03:26:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C95B06B0087; Thu, 11 Apr 2024 03:26:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE8EC6B0088; Thu, 11 Apr 2024 03:26:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8E84F6B0085 for ; Thu, 11 Apr 2024 03:26:24 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3EF04160AA0 for ; Thu, 11 Apr 2024 07:26:24 +0000 (UTC) X-FDA: 81996417888.10.BFD53B4 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf11.hostedemail.com (Postfix) with ESMTP id F15D94000A for ; Thu, 11 Apr 2024 07:26:21 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EOrOwamP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820382; 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=FdtrZflmrTZEM4b8dBDzYd6rxmTmi7yvNu60nfLbPRY=; b=bjcsjzID6Yoi74ab9at3iIFwT4+R7PdUu4FU9gA7JP2EKgbzkSzer0Uake8hTnSVSC0zfh 2F1icTwgQ2BIz08dXzWqeReD/aQUF6U+RMOlpwd8J6KVd2b7JvIDru0KXIKZEe1h65qTyq mxv0taW3BBu5czPDC+Y5OrFYolSOy8Q= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EOrOwamP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712820382; a=rsa-sha256; cv=none; b=NERfR8RMRQdnmZur+XHZKNBfCpJln7FLbsyfRASVWJYLriRVi01ogtie8GDbSnm9USOknF 3GzFpaeqyUPDCrsBbSrgpwvqY2Wb1XbaNpkMtqiLqqEY45I/7jXszlp4613ihOJphrhaor LbWQrhV+x9heV5MPcpYL0zc/elaX3j0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712820382; x=1744356382; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=72Tv8oBNdscDR5cBIv8NthRyNYbTiNCpUQjbNcDeGj0=; b=EOrOwamPgPmd9QKb9iRx6Jxog/cLNqzgN4WjKO8AKjB/9gLT4RpRG0AY x69SImwqYx7/zscP4GPD0QeIt7qK6nb5SvNz7o2UNPHN2ELRbY21SYNvp FUynzFVvlCScsfeTzbREw8N1FrqskXBF02NWwGbZir7Z4wnEwYdSV26QG kLCfhQe/DFIK7Mv0sdw0wz2sVNsPUIoL9ygJm8SXwRygFRI51RGUVFQ3Z Bs3cCdMrqnQq/xH0PEt62tWOeFb7yWjDhOfEq6RWn3c94MdKLdTnSCKpW urTXqU/hKKBzUPzZZ9Vyus/AcbG9KlZ3s5kD1Dd1l0SitZSjmbK97bN+9 g==; X-CSE-ConnectionGUID: RjOjWjIPRZS/uW7cTQKXLg== X-CSE-MsgGUID: w7cryk77THmMAg3j8paEew== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8074355" X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="8074355" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 X-CSE-ConnectionGUID: oUQa4RU3T16v0izoR7aeUQ== X-CSE-MsgGUID: KHaQlRT2R8GqQPIM7PEg2Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="20848537" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Jason Gunthorpe , Peter Xu Subject: [PATCH v14 2/8] mm/gup: Introduce check_and_migrate_movable_folios() Date: Wed, 10 Apr 2024 23:59:38 -0700 Message-ID: <20240411070157.3318425-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411070157.3318425-1-vivek.kasireddy@intel.com> References: <20240411070157.3318425-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: F15D94000A X-Stat-Signature: psfym6ut4bjwdq8i7izpbcxteade1nkr X-Rspam-User: X-HE-Tag: 1712820381-844651 X-HE-Meta: U2FsdGVkX197hBuNzki7G5rlWuy5Nfhivtnb3QT6xSZOrg4rp2wzLPB/PygoY3iBwIKi97k2kH6zo04LKHg7+n5HGR3baKUXPmr7gShwDDG5EEMLkmomUR3qB5vwE8AKdZsp0c5b7HFZiUinBdiB4in30jXNdSlbvuuYO2bLu9+rCWxw20kKeF2ayblrScgaLOdWr1SoVzeRttjPkLOcLs7Ei5pqsV9Q5OQQrQ+nkV5dE1bLdAQMqslNF+oEHOqVMb4QB35Tip+T4XnaMZwfvlhWxULguk1akWhb8wxkRknzZjey1HNelQfZN4S4FuendwvEPd/COTzAlZVv48hiwZ/bLgRpxAtSwQs7PXYQu1N4iPtKLwMjdgeeb1UhRUY+nTnjW4OGoBZ9C2hKgl2YEzDlviD/rdzyW/cmsdeAN9vWjrfeUAcgJCl8powHdIomVwRyhxZvqE4gKKqwIae9qrDhFKPjlHvNo9YMawTiUxvhsz9aCSmv9vE0wmvUD0x4t/MeBldXFY+uIKes8CDweFYWUR4FCo4IPJr6vKJZoeXky1MFJHph8AUfyb/mAinDtJ0m5WzohBSAQW1DnJmNUTaHbXXf0GGH+i2JozAQNJUmPDIvo4RqXJO37ZSpYlyrMLyeyiix4ahTT40tsWwhIggb3fPvVOznMHpoinYE4c0xqQG0U8yWn+r4oDybqGZ+rwC9y8G4fthGvXgoKiue9ZIzCcHjT4dpk5PoF0QzCfbzAoRWgOG+tq/8e9xDL3lD2dqI/YCPj8yn5HhjrN2P76TGlb/EJzNVtWjbEH9nBTgQ0jqkiiujybbpj1nM3e7lCqucky0cM4uuA2FAZTj4MqsyLxrF8b0tMeZlAJYpJHOYUpzr0KqvLHSNwh9EJVVTOOjBwnNZSG0+3hZcBxekFA/nQPpsIs0X04RNLh8S0oNWBHxm7IU7zC39OszDVr6xWeskejwWaCjbO9mlBpz zgRhW17e tr8/XdMth7hezgCxzhLI9X03gokK4EefhqfZxYSUaiqO6NNkH4475aGFSA6V0CCpY0aN7TKGPfDj7bOhQHJoIjVifcfyQfWN7HY67qXA0ju5ow9aazR9LNT1Heyw7vhP1n9RoHGB1azAylyyrdOPHfdpkGbqXwKJy1BZL/8P/S+jtfGBpZeVIMm+c80UnqN6IJSa1KK8TzTnXYyAlGA5hLGMARKO1s2HxGN5M7eyp95QC0kKswcC3JMzsObnNKx0e+Uk+PShksQXQNC3LS2j8edFnmLdVoaIelm6XgRwGCLkcPaDu1nj4qU5XnA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This helper is the folio equivalent of check_and_migrate_movable_pages(). Therefore, all the rules that apply to check_and_migrate_movable_pages() also apply to this one as well. Currently, this helper is only used by memfd_pin_folios(). This patch also includes changes to rename and convert the internal functions collect_longterm_unpinnable_pages() and migrate_longterm_unpinnable_pages() to work on folios. As a result, check_and_migrate_movable_pages() is now a wrapper around check_and_migrate_movable_folios(). Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Peter Xu Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- mm/gup.c | 124 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 47 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 14e94fdfa827..20ec66e3c2c9 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2413,19 +2413,19 @@ struct page *get_dump_page(unsigned long addr) #ifdef CONFIG_MIGRATION /* - * Returns the number of collected pages. Return value is always >= 0. + * Returns the number of collected folios. Return value is always >= 0. */ -static unsigned long collect_longterm_unpinnable_pages( - struct list_head *movable_page_list, - unsigned long nr_pages, - struct page **pages) +static unsigned long collect_longterm_unpinnable_folios( + struct list_head *movable_folio_list, + unsigned long nr_folios, + struct folio **folios) { unsigned long i, collected = 0; struct folio *prev_folio = NULL; bool drain_allow = true; - for (i = 0; i < nr_pages; i++) { - struct folio *folio = page_folio(pages[i]); + for (i = 0; i < nr_folios; i++) { + struct folio *folio = folios[i]; if (folio == prev_folio) continue; @@ -2440,7 +2440,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; } @@ -2452,7 +2452,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)); @@ -2462,27 +2462,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); @@ -2495,24 +2496,24 @@ static int migrate_longterm_unpinnable_pages( } /* - * We can't migrate pages with unexpected references, so drop + * We can't migrate folios with unexpected references, so drop * the reference obtained by __get_user_pages_locked(). - * Migrating pages have been added to movable_page_list after + * Migrating folios have been added to movable_folio_list after * calling folio_isolate_lru() which takes a reference so the - * page won't be freed if it's migrating. + * folio won't be freed if it's migrating. */ - unpin_user_page(pages[i]); - pages[i] = NULL; + unpin_folio(folios[i]); + folios[i] = NULL; } - if (!list_empty(movable_page_list)) { + if (!list_empty(movable_folio_list)) { struct migration_target_control mtc = { .nid = NUMA_NO_NODE, .gfp_mask = GFP_USER | __GFP_NOWARN, .reason = MR_LONGTERM_PIN, }; - if (migrate_pages(movable_page_list, alloc_migration_target, + if (migrate_pages(movable_folio_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC, MR_LONGTERM_PIN, NULL)) { ret = -ENOMEM; @@ -2520,48 +2521,71 @@ static int migrate_longterm_unpinnable_pages( } } - putback_movable_pages(movable_page_list); + putback_movable_pages(movable_folio_list); return -EAGAIN; err: - for (i = 0; i < nr_pages; i++) - if (pages[i]) - unpin_user_page(pages[i]); - putback_movable_pages(movable_page_list); + unpin_folios(folios, nr_folios); + putback_movable_pages(movable_folio_list); return ret; } /* - * Check whether all pages are *allowed* to be pinned. Rather confusingly, all - * pages in the range are required to be pinned via FOLL_PIN, before calling - * this routine. + * Check whether all folios are *allowed* to be pinned indefinitely (longterm). + * Rather confusingly, all folios in the range are required to be pinned via + * FOLL_PIN, before calling this routine. * - * If any pages in the range are not allowed to be pinned, then this routine - * will migrate those pages away, unpin all the pages in the range and return + * If any folios in the range are not allowed to be pinned, then this routine + * will migrate those folios away, unpin all the folios in the range and return * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then * call this routine again. * * If an error other than -EAGAIN occurs, this indicates a migration failure. * The caller should give up, and propagate the error back up the call stack. * - * If everything is OK and all pages in the range are allowed to be pinned, then - * this routine leaves all pages pinned and returns zero for success. + * If everything is OK and all folios in the range are allowed to be pinned, + * then this routine leaves all folios pinned and returns zero for success. */ -static long check_and_migrate_movable_pages(unsigned long nr_pages, - struct page **pages) +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) { unsigned long collected; - LIST_HEAD(movable_page_list); + LIST_HEAD(movable_folio_list); - collected = collect_longterm_unpinnable_pages(&movable_page_list, - nr_pages, pages); + collected = collect_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); if (!collected) return 0; - return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages, - pages); + return migrate_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); +} + +/* + * This routine just converts all the pages in the @pages array to folios and + * calls check_and_migrate_movable_folios() to do the heavy lifting. + * + * Please see the check_and_migrate_movable_folios() documentation for details. + */ +static long check_and_migrate_movable_pages(unsigned long nr_pages, + struct page **pages) +{ + struct folio **folios; + long i, ret; + + folios = kmalloc_array(nr_pages, sizeof(*folios), GFP_KERNEL); + if (!folios) + return -ENOMEM; + + for (i = 0; i < nr_pages; i++) + folios[i] = page_folio(pages[i]); + + ret = check_and_migrate_movable_folios(nr_pages, folios); + + kfree(folios); + return ret; } #else static long check_and_migrate_movable_pages(unsigned long nr_pages, @@ -2569,6 +2593,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 Thu Apr 11 06:59:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13625511 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 3D43FCD1299 for ; Thu, 11 Apr 2024 07:26:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 322DD6B008C; Thu, 11 Apr 2024 03:26:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D1806B0092; Thu, 11 Apr 2024 03:26:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 127846B0096; Thu, 11 Apr 2024 03:26:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DD0736B008C for ; Thu, 11 Apr 2024 03:26:26 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7018640A61 for ; Thu, 11 Apr 2024 07:26:26 +0000 (UTC) X-FDA: 81996417972.29.6C82A7E Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf11.hostedemail.com (Postfix) with ESMTP id 5CA454000A for ; Thu, 11 Apr 2024 07:26:24 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=NtBhWCC+; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820384; 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=6XASOfguPFZvOH7OAtPOuDya6ibLHDWgknNA0AVnXR0=; b=5eNyud1YZpeAmegW9UhYHOUKSTglLmHhiAInZ8pCkUjHwfiNmSKJ1KmgEChl5YN+mKEXdh Zep1RRT7njRg3rMSUZm/F+XLzqZHe/fQBBEE+mYVfh7cLiRsLB1XaI0BCK+Tt8F5lCtNvi a8j8lygt56uLZ4pWMK2g1rO74qjhJ9M= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=NtBhWCC+; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712820384; a=rsa-sha256; cv=none; b=0vq461ZvJ230U8fN86spe0hRERYGSB/h43ds1imJ7WLYUuCPBzS+FrLgtd90WFjDRH96T8 YPi1O80jKJH5HH/OV6JqxOTNV4NrkjQxNLwS4yVkxUElWFZdSbMW/9kyJiBS6hjnomXvHb 1wdzwkJCybwGoUpsDT6P0lGFwWppMOo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712820384; x=1744356384; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PCSdl/PTe1WEWwl/Ib/RN5629UWyIxT2OAf7MEwbm6c=; b=NtBhWCC+vNEAq12bZd0EdrqAMGGtNhlC3dy45VYYMOhcElhk8HOcPrr4 NxV34qzFwpGXGgEz7TN+ESM8xPmzCz//Kidc/C+XrfrYU1VUSBjJvdtOW p40UuW3Vac8GU2DELuogP8FsPPxACSSw6vIVTRicZ7bn+Fx5+mSgwB8bz s9f4uauruzuRrldFSi9g2OxVY+0lm8nyvqKhFUYIgUmIZoLrfjsXLdPNU 8qIT1byMPawrmEiN69Mcrp2dn+g/ny1S+P5bRWfj5vlr28rOkthg+IJKP ON1lsIJr1P31sUULNgItGFhYpmNulNcvfR2DXztXBtPONt67/IB25e7q7 w==; X-CSE-ConnectionGUID: 6zDDedQ8QcqsJl3i8unM2A== X-CSE-MsgGUID: 4mk4+iCQRVuZD62B7QbejA== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8074357" X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="8074357" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 X-CSE-ConnectionGUID: BvK1T6QGTDih3q1v55iFVQ== X-CSE-MsgGUID: PA19Gt03RGeutO0/UbyLSg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="20848541" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Daniel Vetter , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe , Christoph Hellwig Subject: [PATCH v14 3/8] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Wed, 10 Apr 2024 23:59:39 -0700 Message-ID: <20240411070157.3318425-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411070157.3318425-1-vivek.kasireddy@intel.com> References: <20240411070157.3318425-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 5CA454000A X-Stat-Signature: zga4ys5odkac3qiozefwnaqhopt9mjgx X-Rspam-User: X-HE-Tag: 1712820384-992045 X-HE-Meta: U2FsdGVkX1/TOAZ7QUV0GWtCYTOqO3YIs/2LfWys9vlr20onrIQ1AiMOoW6ERUUOP0ewrWsxKhYxHXPuf2AAg3aJk7vIyx1aAPMGEVm61yY6BJS+1lFiDguhMoSbYPLdXSMnXF17aTYLxGxzfbIBZCZ1MZBVij9PwIoqFgEJ68MQcvFRLrQ3cACv7XLBzYDR345STRt494qZKnkxa2KaPY3JKpPuUc/7d/z/it3m8VlmAlFB14MBd9yFsuPY92zRnZSHXfv35gPdJhDc0fnQoGe3cF3vDKdH5Ylrr4zMesKyTA2LKdKwJmJETEQjPBVCTz2wUuQrqc/ozAgOnQn6AYqbYy+altS677WWY2YjrO9Xdwsgxeio/oOutSkn7hG72Fuh5VB+DMMEDmjAboH+Rxoe8FTvHsfUd+AWZp/EjPVAZkfd4Xmq1lbgkfSslTa1OZChXkQXwk3z5R8Tc30Tb6dGYl9J5dUM1PzbooYhw80+Gs8G6U37sspIocErydhu6gJ43L5Aoct+wuprgdDAEoTfCYmG9A8boasmTQNA+9jSBNeXIbk7kX05TQa5nTL3mEJwOJwA7V4YUqG4D09ZY3cTaWBrCAZlZgPEXR9Umykdp3gjwbWSgFQnT28waf/1EHUBDbIMwfAU8ymFrpZsFG41D+PMC1Bo/8pZxjfM1wDWi50/I5m1eXwndiGTF/A6cAgri5PyySjUYM55pVQroP9/poUlR3QjKi7leaIxbC66tH2cLaGRuVGhXOnfeS3P9DX0Pibh7NKx/xX9siCdJwT8yOwqomF+yfB16OKtVHpiED8xyKRNYNlIfrQlyZm9DAy/AC/UBf5eTDXijdXo1TjuO63jJFc+x4Bu+TtUpXG33/yshoaw55c1VgjIAxfg6fqMqa81wZn12rYsL6d+LxhqoIkUZNl/fzrXhhoeacTV/9FFzppa4p2VcvurI3MREayvH7SqbEVk3zVBYNj C/0AWwc8 M39hsN62i+EqzPrWASyY1tJ9D/qGHoA9RhXhcxonvfiKRxqzo+DWAv2nRSXwdwHXUXZ15ZskoAy9m4gNAoJ1OpRIyhAve+bDwGADhs1k5MHB7bHZ+QV9B2ZK3iaU3XqWLrIU8b1SsY8j9UUIkK9cn1xl7I6KvuMPAfk0nNL74O2vC1exGQ+Cdw6anYLrm4PRCJQ2m+vSeFLq1LYEe4Nx7e+a9LpuNsBY8Nz2FH2svMrhxnpwSKVIh73bA+ov4gU9SetkCbS/ejUYzeyU+WcU8gJ3uVZ3aLDUOticPZYQBAOSrhrbuO6oA5NvCe6zkOKV5Q9kW3NQlMhwAMIcWO/1PKs/0AQ== 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: 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 | 35 +++++++++++ 4 files changed, 179 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 d1ebd7a25105..ff196844f4af 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2531,6 +2531,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 20ec66e3c2c9..4bdedb1528f6 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 @@ -3734,3 +3736,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 7d8d3ab3fa37..6ce2abb22c2d 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -59,6 +59,41 @@ 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_folios()) 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, + false); + 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 Thu Apr 11 06:59:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13625510 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 E155BCD128A for ; Thu, 11 Apr 2024 07:26:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B94C6B0088; Thu, 11 Apr 2024 03:26:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 56B786B008C; Thu, 11 Apr 2024 03:26:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4097B6B0092; Thu, 11 Apr 2024 03:26:26 -0400 (EDT) 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 1D1146B0088 for ; Thu, 11 Apr 2024 03:26:26 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D1D29C0A8A for ; Thu, 11 Apr 2024 07:26:25 +0000 (UTC) X-FDA: 81996417930.26.2B2B16F Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf10.hostedemail.com (Postfix) with ESMTP id DE06AC0006 for ; Thu, 11 Apr 2024 07:26:23 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UTrztSU1; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820384; 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=fahG2thEVcPex80d2G3uCTOF5N5hyXCrixaFMbZYeY4=; b=Jdlzagr5Adbq63B7TPWcicImn8IIHN4SvsjylgKEaQyQBBBtGCSiWSiXMeBdxjSAeSAf4i PvWSMVjzj4vYhoTZRyJEldgqC4oyMBeD0IBIhksyfsEV4oyL9ozVt4sCzjj2kRVyAqrNFB DYcrCatvfofE/pattqXdxal0DVNbmcE= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UTrztSU1; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712820384; a=rsa-sha256; cv=none; b=jIz+uZX1G4EhNfYQMrQVYPIjvzME5+VbSGEVC2uCRBVzB31XliGiAvlG17y7z2eJtkBobc cgrpPWdReMQ8DQA2aut9hAPiAIBDEA9TfalpniBAK1CLVkIAO4hWrGSfvRWgvzSzi7TqAF PSapTHm/NyA/fNIbwbvjhZJsl3i+Z7Q= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712820384; x=1744356384; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4juM3xfivhaGo2McCt1eiAUdmharbNFcAc8goqvqufM=; b=UTrztSU1ZcHTxHADgZMX2NPtRa0xAQQiFS9UzDPgpmXaXUppwrFZiZP+ hqa4ly8OjCgqEUAXDmV+a/N0LA0zbfRe1sXfl2cXbXr9suBfQZbZPsUJI Dw23ItDnBER/X6+eodtgvm0aYkcPDQWEdukMnsy1PaMRE8fyse+HQKns4 gMPCyY6bz4M+LsGqPRFwxqC7xFeo80ZvMAjd3W2PHfpB00hpt+mqKx8B0 RJEIHYz3MinNHhbIDcpjzF0soZ/kSYlNTRAuaPKo2Kqb7evFLPbFJZWoh wVNQ2ac8RyaVyCoqP4rF0RH8B4olQM2DK93Nufj5t+v6H30qOvaiAGGPY Q==; X-CSE-ConnectionGUID: fbVQkXcKQcm62ZwYc0LrUQ== X-CSE-MsgGUID: 5u/xj+wXRYaMk19LPnpn2g== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8074366" X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="8074366" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 X-CSE-ConnectionGUID: HbYAmvT8SDy6an56H48FKw== X-CSE-MsgGUID: laxu6XeDR0Cq6Dz9keXnMw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="20848543" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v14 4/8] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Wed, 10 Apr 2024 23:59:40 -0700 Message-ID: <20240411070157.3318425-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411070157.3318425-1-vivek.kasireddy@intel.com> References: <20240411070157.3318425-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: cqngshksyn9o5tm159yf7qmthrbshffy X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DE06AC0006 X-HE-Tag: 1712820383-634937 X-HE-Meta: U2FsdGVkX19aQZoS9lTYsqujuEqxRVQhrqtAXoVF9zwqs/TMIkUYe/Aamvbu356fz8wbrJmmngGiSiUTsyoSBUWahgH//R1cLXiK+ZmpHt12IFCoZRgwecW9MY4wm77O884+NM/lORgi1HoO6aZp3DxNnCiVC2o1Nx4vx5LuSKZJ/KP+Ps0KZuL3vT9k7FVs5JDyrv6DImtF/pr6pkpL3n4jE9E7tbGQiJegtOEUSy/fHpotiYbExVCtop5WBwFV0Xg6/rzW5eEakzijZXcFtHx+X+I5cgxg/1OTxQZDC+8kDKpHz6wVwmdJ8SPZo22+62jXGBW3DkfTYu4ZKDaBejA/v3xv0ByGPWnUElReRzwho8vp65gYo8qhhU1H127FzxaTI0q6gHXdlb84etiLsAVL/mJq2vAoBTupf+D4Ets4g2gTj6bCsNzKuzxrw41ijEbVAq833sbT9AGOHtLvo7J7dkhoYGfXHfoNJjFKy2VBxoJ/PayvbL89r1eyetXSETFMmUzP5g4pQlOyD+OLXrUVaCqis5regKpYOTK7oxDK5298G+tqY8hL59Ke8O6weTzInCg+grQG5+i9zKGPMTy4m9lbgoXVvVpB6POMkoJZe2ISuw5pzQE8Ud6nU/IFCBF1SgkWSRw+fiHEA73beDwx/KnL6CFISvQQLU+U81mPUNxpnJB4OSgYDi7gacQZ7TugOGOPuylnsTyAelGfH2k47/L5OV6P54E8rRCgVtduZIo/1JStkQz4iZ2sCC1VmyrQdsicC67enf5Yd677pLnXrPBGxsIUkXamDPygHcHLwdQxKvJ5nZZ5qr0yXDIiORCWDqypN6tjg5CKE4Skz8BGUbbQozfdk+MzmlETScH1fryGMbHDT7QEuiuOEDOos1z2HU8IsADCi6qsyHsOqTe1J7TvYIWA9VI6+dE5TrIg2XE75xUQAA== 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: 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 Thu Apr 11 06:59:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13625512 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 0AB2ACD129D for ; Thu, 11 Apr 2024 07:26:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 89F296B0092; Thu, 11 Apr 2024 03:26:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 84FF46B0096; Thu, 11 Apr 2024 03:26:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CA156B0098; Thu, 11 Apr 2024 03:26:28 -0400 (EDT) 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 4CD286B0092 for ; Thu, 11 Apr 2024 03:26:28 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 05AB740A4C for ; Thu, 11 Apr 2024 07:26:28 +0000 (UTC) X-FDA: 81996418056.26.51D2814 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf14.hostedemail.com (Postfix) with ESMTP id 48189100003 for ; Thu, 11 Apr 2024 07:26:25 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IzASRikc; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820386; a=rsa-sha256; cv=none; b=Zvuf1zTpzYUyqHJRmhESLvEt+RC1iRElFeNmcM+qH2nFHhlFxVcjtVFR+UKr2lG2cS7sfv yzTaEOJQGFxTSSyouX8zkw9hRgm3QNIRY4dwVu3JpcRHgqiPHl1Rf8wyGME5e36I49dWHg jwBGTZ7P+2pFVOZBWLj1Ki4XlgX35zk= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IzASRikc; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820386; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=H5LZX4x0dPh49vNzSTLf7kYd0GJ+7/NAQZVV/ovBC1g=; b=XVN8kQlCFyKvJZEsncVRTf0hDhNqY1vyd9n5eynCHZqSi57HjMQXR+Wkv2qX4fd7eif96j SXAI8abh5MVC7TyMjp8sN+rtFWhT+bv4HIWMqE1ahxTwtKCH1hW/BYKFTlRDPJcAyGlkzR rEV3i8gRv694OaBdAwHSuF5FDB59xzA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712820386; x=1744356386; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OFY0cfHR/dm9OGHwjR/w6MaeqHarFwrIQpExmOosGtk=; b=IzASRikcW0Eyfrv9rfJeVrFCUmA6Od9bEWmxvCZSLErAbxzk/LqDAgDR J/6gLI83NyOBaAtOLZAO/MhJ0PskPCdUFMm5RTB/A1GgUBAYoNfnjoBIR UxDrsuchIQQZtDMIDmvGWjAncxAsavzZ5W3sAVJh5K4Nl81UAag5HUl/o UNsuYGHAkWT8pudGkc0hlqeL4MneZsDhaJvdvhVZrmDT6LctJI878n36m Oo1V0+UM4HDGNVoXij0WbpGCgF/QnxBl8H2dY/Kk/bbW50j4QBQGCoz5b 5l8+AKl1g/lIdf+gR244gJctJFhBmt0IiB1JQ3FsMNp8aZoJQauzZZh0I g==; X-CSE-ConnectionGUID: e9lUbj1nTSKOzmWuE2xgyg== X-CSE-MsgGUID: v93d6mpoRsWzrAiE+CQ03w== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8074371" X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="8074371" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 X-CSE-ConnectionGUID: BfafMFx9SlmVm0C2d2MLdw== X-CSE-MsgGUID: /+FK65cLRzat992+NTmTLQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="20848547" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:03 -0700 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 v14 5/8] udmabuf: Add back support for mapping hugetlb pages Date: Wed, 10 Apr 2024 23:59:41 -0700 Message-ID: <20240411070157.3318425-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411070157.3318425-1-vivek.kasireddy@intel.com> References: <20240411070157.3318425-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 48189100003 X-Stat-Signature: pzt5wwfacp9834ibpgurx6aihynojgb5 X-Rspam-User: X-HE-Tag: 1712820385-839757 X-HE-Meta: U2FsdGVkX1+d7FZXUNhO6nu5SL1P9eG9mobsDapfM8hHgTQ+v56WTnU5m2k2b0RsZg9PWsnoKllSdVL2ySTFTim+04xtLEHz9bS8XUZOxFzbFAqzRQUovAlstbmlAkhaJ776LIH4+1iP/wHLxMhN/Nxcc4vAsE8EDc5EBiIreYtOh5+UBcsJOsmxJDiiwPjCGUKPeGZ1+VW96SBQtoLVcS5l6CgcJ3GAq7ufl55N1+U9eVwHdHFQr6xFLw1cMEiON9Oq8t1M/RqmM/VVwkBOIqVk73QKctTqfkS2GBOjdfbrih4XMWYUsOW5uEi6FTBXxeA5OEP4DXSLCZS9jXl1L7dlcyfqEMBxAoAQKqbmRRealSxJIR1UUaVbdgK0+x+2ltIda22baPjpbK+ajIQ4MtVPvIiHdmxW7I7aMVLoCLK4iR+IsBuDKBgxOjKbUqLn23ZNfBbcsI/DlqrjA1pJL3QRVmrEuZT4FlJqpZw/f+5V5z6Jtmy122gawow3qe/qUQ+TWBCJ3jGazTBWqf56DmTlT4PlmmZTGqpEXgb5pj3oJqDbIXY/fCxnPlQgcamXWZRvZeJ4nGVYJmk/tF8VGNo05JN6gweEwtFFF2XX8vxxBliMh0kXXj2v5K0AORAW6VC14dxGVP+m02lzfna5UQ87Omf8fxN5OyoDXskqf/fubVZIT6Ho95qUcVkxmugqsaJoQl+gbhitW/RITY3k6uQwU/jw+pKu7aYPjVGTljHFEsIcj+xwN+K3QThlRlo23FBVS9wWguyf1lYlFE5gPMwdMcSNHsTuoJu8frG80tPn0FR6WH8yEOzNYQXOcBrexKgxbvALeH5lUu0VhWA+vrOD7zfeoP5h7fHJusD3wQ8eecIh+MgQuX11AkdloAiuigRX4l5BRGTt1dsFF5UufFoXxFOgX9kxZLeIO3WYm04eiRH+1mPCfZVPBUDLJ21mLTM9BcZUZFvgyZLd10f j64o++57 Xa9WsrmGk1VwMDFAZkmDbBXrzz8J9U0YVU5Udp0Fivftb8IOkppYEkLMv7LkPJhobBfUz/anMm0oPCbP1bq4I71ig22E8i4xROPLkrFrvRZDhMYniWgBEiQmVRQS/s8wbX+1cPeB0wxaZdxYfmicT34vYCYfSKf2m/IGNsIkJALlA9a4h15HCo1+faCsoRsrupczOQOWR5mEcJG8GHLFYVSpBzO9iLuXLZ5e9cIc0oA7i7AAGY+d9ewJcQOzCrjHY8Fm8 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 Thu Apr 11 06:59:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13625513 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 EF0C6CD128A for ; Thu, 11 Apr 2024 07:26:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E45B16B0096; Thu, 11 Apr 2024 03:26:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DDFE76B0098; Thu, 11 Apr 2024 03:26:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C21206B0099; Thu, 11 Apr 2024 03:26:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8F6666B0098 for ; Thu, 11 Apr 2024 03:26:28 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4F8981C0EA9 for ; Thu, 11 Apr 2024 07:26:28 +0000 (UTC) X-FDA: 81996418056.19.4189F58 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf10.hostedemail.com (Postfix) with ESMTP id 3ABF4C0013 for ; Thu, 11 Apr 2024 07:26:26 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KLoVRJRh; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820386; 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=nQZNyjDQ6HqlpKyYFqc7k/9GcM9mJkrAnuMI+cjCzwA=; b=PX9YwuURv6a6hNQx+45cJnkdHK2LoOerrsrqBj9yI+YbLtcL+wNglXOMRdM4yQba5Hw4+8 UO/mwmyM7o1DXXXvqvEZGlDtI2SGLLV6FkFj01HI4VVN6L3DlaQ/iGBg8BtJfyJzHWnYlY qhn5qOkaPQRwVYHy+jkingU0Wi+niBI= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KLoVRJRh; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf10.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712820386; a=rsa-sha256; cv=none; b=u47rtjMbRJn8Rcb9NODHUT6Bv970iMG6U2gON6cObfQcjBYim0eWRKp6A0cALsRz7wf6ax j934kTJqC8VpGN6I3qwjWFPVmyvs4hWm5vwi53oDAiDH/bqZmVOnTUKUQuozauhVo5fKU0 VhmoNYyXreXd0z8c1JhHyr9YXSjBIis= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712820386; x=1744356386; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rlyy82dvCWl+3L5KBvYaxGz5s7+0G8l7jrwrH0haFlo=; b=KLoVRJRh5Mm5INHvdA/MMHV/VzgX4eF/kB2DWEy+emVL52KjxtXddb09 tmNSLh3KER7h3pnGX77tlAL0N+HFPP9UVdKMxoo3x9BxbtWN81V5TfBh4 Qci8VSsgmjSHns/bY9nVcsxHfnEFBu4xCbn9dbdTjYJMLXE9we6/JvGcn SBkWBId4OtfJNHH4nz9D+wsSfwdU2ZLwy1zGDqK3zNuHEsiMumrsNAf0Z 7Pk2c7Rawx+1jBIN8yGfIyE1tkUEsH0NfygGWLhwL0EbgAdMRlVEvUq5c oe+IG9Uv1T4w2XChTmqa6nB0YJ7P800vTd5u7GpA2wBpUjFwwbXkgsXI9 A==; X-CSE-ConnectionGUID: QhQU9KiIR5eEzZLe30syDw== X-CSE-MsgGUID: 3f/2nlZQQI+y/OlIytPo+Q== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8074378" X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="8074378" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:04 -0700 X-CSE-ConnectionGUID: faXsY8pRQx6FXhadJFiLYw== X-CSE-MsgGUID: zvZ6+KPcTKKqEvG7SYUL4w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="20848549" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:04 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v14 6/8] udmabuf: Convert udmabuf driver to use folios Date: Wed, 10 Apr 2024 23:59:42 -0700 Message-ID: <20240411070157.3318425-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411070157.3318425-1-vivek.kasireddy@intel.com> References: <20240411070157.3318425-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: osixt19q7m1p6g7yrt8dpn8pxcdfrwdh X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3ABF4C0013 X-HE-Tag: 1712820386-6807 X-HE-Meta: U2FsdGVkX18d4ngyDioNgSN98+L5lOsTGOdpKZWckho4G6cqQ3ZiE/e/ua2BT7NlWpzNlMsdg8PBOfFFUSsXZ9Map9CCCj65ypPs1ILZEPoiwaJJx8XODTA6WouOyxqzifwbmVuv8g3WmuzHlNk07J2lOfrgC0hOVu8wE2MWgkWJjEloloqBNo5abwaPtu5ByVxIaaYxg+9cM9Bu8V/1Jm6r/CodoSHVI/7c6zWzP5n0624JM6GJR1Uw9E834k56k5LmzGqPeQNLrMj17wi97tTQZQ8QqhMB0Lx2yVw9tnfC5phPilgPaRag9NMicXX77LMdKSjWIc0NqzfvGjhuIEsBZQkXFBPS0cfe+Hht0dm+GqOOoBjghhye9C8x0GFHsq3SP7m0SnqWLkGmsaaAAWsMIYRIv9DG0F8B4Fij8wDG5iIfznrA7NEWsZtThdbXi1jHiHx1HvlILGh5yWDB6lTxD0pWHXXXzquLtHXy9E9X/eP5wc7Rkw4Xo61HAA2UB+WcGylsPRaY/SV7OFe85HrzldvBV26T2NsGaHMCPEy04rDxcKk1UcLbqKmkDyQeADzjpHXsFd6xf/botvmzMSe/Wq8J1TQW6J2aw+uNv3uCfNXK4R6Z469SxYygdnFMh0yMWF257NdVdvagqS43MT8piwrLpkROuaEU556T16WQZItY63Phf6hrhAZw8QnQVx6Y+ZsQUZ3PZVS52MsSnR410RCPNl29j1k2UOUP9NKE8MjrTP71d4TVI5DVEo69f+dL5bpnOKEGCgiosgM5py1p0ZO1Jiy8AOTMD8QUiKY2dDeubzesTfnsRn8VctfddUP5FxJfSbDzaZUFmJQ47mbyi7KYAOCCK7oH8+efCZ9PqLN9cPK4Hk64aeVm21gQEG6Ozb/g+84= 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. And, using the memfd_pin_folios() API makes this easier as we no longer need to separately handle shmem vs hugetlb cases in the udmabuf driver. Note that, 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: 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 Thu Apr 11 06:59:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13625514 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 06CE5CD1299 for ; Thu, 11 Apr 2024 07:26:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 791576B0098; Thu, 11 Apr 2024 03:26:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F6C26B0099; Thu, 11 Apr 2024 03:26:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 51BDE6B009A; Thu, 11 Apr 2024 03:26:29 -0400 (EDT) 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 2453C6B0098 for ; Thu, 11 Apr 2024 03:26:29 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C9DD1160792 for ; Thu, 11 Apr 2024 07:26:28 +0000 (UTC) X-FDA: 81996418056.16.5C4E0A0 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf11.hostedemail.com (Postfix) with ESMTP id 9A34940009 for ; Thu, 11 Apr 2024 07:26:26 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lnHrX9x9; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820386; 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=ZzslkP4FXSVcnARgjBpiasDIiO0zowADisAecquvRNE=; b=eAIJP4udXH+jTla18iWkRFwV9osBDKRbvFFnp9Mciskixs+JRCIdK5i3VHtfmSxlBV4iV7 BrSZNqXTwD//CwvNEJ2keqzd/ka1quYeEdmMUaRlLBSqZ54OaPCarDBjWrPOBW5fVcbS4S FjHA3AHu3cuG5vOtOb+8wA6fTstGXmM= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lnHrX9x9; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712820386; a=rsa-sha256; cv=none; b=AkD1XpQ2CnqGWNVHbL5u8J3JcSpPYBvw9/D2HQKu51oelaDgzNY0vUTXwcxU/4M6h46pfl 4q+DyNlGoVD6Z/FYiWdpD5+Xuy509zsNAOt1gR99t2ymk+9s5SEr+6KmV+jl9AhJ0kezjq yvAy9Hpb/sKIbbeMdI72NH4vMXWIhO4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712820386; x=1744356386; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rYDIAk/+JNBpe6qVQ2riIjSCYbGcP96O0fQsqo8pcLI=; b=lnHrX9x9KdBhIRbZf0jGBYV15LdUSwYl9CykV4yOS4WPJA+POGvJlMlO F64XgKmYDr7lF/DSELXjMzU0QlWUE1bBmrLoWXj5hwTdHDE+GWOZnl6Fu lwGAxDeaORPLMEX7LEDWYJubNmaV8fLd13U98hevdEyeW01hUImRjPxU4 XEfaIq6b3O7UmntCaYxrEBsTBZo1GgqUXTyRQEfvVyefrA9PRArbXFEfW adTKZMRJFfj580Nb/L/Ug/6MmnjkgJd2epiNZ1/+I+qRRuBJrJ+8/KBGT hkULyG7BwnN/d2GYCK11PgXAyKrv6OI6FJWZtqBki8YyLevMcQq93oXwu g==; X-CSE-ConnectionGUID: kjdwPQa2QAyPimOhQp3B0w== X-CSE-MsgGUID: m0xEzojdTUyz18u00ZBlJg== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8074387" X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="8074387" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:04 -0700 X-CSE-ConnectionGUID: QO9LNaNXSOe74PNJbN8IsQ== X-CSE-MsgGUID: pHZAg8fmS4iAnhUQiowFoQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="20848552" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:04 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v14 7/8] udmabuf: Pin the pages using memfd_pin_folios() API Date: Wed, 10 Apr 2024 23:59:43 -0700 Message-ID: <20240411070157.3318425-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411070157.3318425-1-vivek.kasireddy@intel.com> References: <20240411070157.3318425-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 9A34940009 X-Stat-Signature: 8cdthrwwuw9kryeioga46cq5hgbw1xmx X-Rspam-User: X-HE-Tag: 1712820386-372577 X-HE-Meta: U2FsdGVkX182uvdbhr8ii4z0voyfjj8uguUfSkiPSC40Ki2HVu0ksjHswONHfMlfU4Qu0ItmJV5wgNve7Cy06jTiIMLRWbDNk5RyZNep6DOdk/czQyVnll7PnvKPuP8IR9FlAXHiD4m5gu1Bdv8weh4z+D5g+5QRIDIZqg2xy8x4vkG8xGbnN9UA2uqaTbfkIVi4yjAKzT1fOaJuGjNBFvsVoO4p1VuTT9tjv4BQ2ALUxd445ilpDTsj31H6BxzFnLV3aRjYWaLYYUVdPc1SJg1TP0ZvT6xAKfJGUKlbOWIRaJbQb1mjpGk1YnaUyzvmv9umPubBTnD3XNUHvpfz3q1qeE8pkc1SlgsAbXNodE3adx3s+uUzTlxVS8HLQSmYDp/jztfs+1+aiKzd0NLCTsiEnpZ5f6QVBRlAyhyb0dfV8LFxrz/gB90/oWrp6cpMS+s8ss22l9mt0ITURNLDhHDWeFna1UgfDNoGUhZH7Joy9EDo2nxORAnkQb8YCgCSzHRxT8nz4qYXgABd6Gd1j0Xr3w0tGh0QPvEaxQiNn//PAShmDINZR5ceZuMBCuo4CDVr4kkLYxhUHo7i7zLXzP/xp4Sd+kn0zgLcq4qgmZ+Y1XgQYxtp9UCjSGG4kctyDS5Gzs3eGFLRimBHrvY638sSEAX5vaJqt1KhPxaAcVJvFOc7gx2yHOi1Y2lzT3L5uCF9SPJ7rpFPL6PDQIdvalVTXRUViMa7yjG1hIn22YL8b2BXmNQsUHUmNBHunCbjZu3igVYo8P3m92nr0C2QuVKYkBPGjELKVMJuiBQWKVEEBhaiPRG9L62b2rKSg62wgb+/lNWpELYpAMU2VrQm4v5J9e31GDH96MlBvsomjfjfrcJkUtzigjf4L4gL8Q+7TIo6oVF7LWluRsR4SaARLlVG7ZSHv8NiPe+K+L6E6PJdCnNwGZI88eLOmvBLHYeM8DHK/3HEwk1JgeQJP8i oK5RzDX+ eC/r2KOEoMAlUXqXQ9ajgvKD/+CPtQjYeu/aeGcBf61xMYIYkVxDsF9DKRaHPbKNybs3c08Z+1KsfGn82E50ejsuk4tYBI/6W4HSAg32UUN3qpkgx30q0JeEk5E+z8C6bnOU34Dy6S+EEPf3/IaigDTJIBEdo1cpTr3cyGWxWeOD98WFNg6nG5+Q/Q9lDeU5RWJZhGh870x0t04/4HimbwoyAOeMMb0XKr+vGhwd/UZRLU8/WW+LVC9YHRnmUTDnokVs/XoxmhNXPzh/8Xg5T1ycpRiEPJgy1xEPtniiz8Aisqi4MAh4nBo+ePrHVHsRIv4/tdgWCcjvMC1XPyV4V78u3Lw== 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: 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 Thu Apr 11 06:59:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13625515 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 BE73ACD128A for ; Thu, 11 Apr 2024 07:26:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF99A6B009A; Thu, 11 Apr 2024 03:26:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE3456B009B; Thu, 11 Apr 2024 03:26:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B11EB6B009C; Thu, 11 Apr 2024 03:26:30 -0400 (EDT) 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 885F26B009A for ; Thu, 11 Apr 2024 03:26:30 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5F3F7A15A2 for ; Thu, 11 Apr 2024 07:26:30 +0000 (UTC) X-FDA: 81996418140.18.170BAA9 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by imf14.hostedemail.com (Postfix) with ESMTP id 2F8BE100002 for ; Thu, 11 Apr 2024 07:26:28 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iDDDhT1Z; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820388; a=rsa-sha256; cv=none; b=fLKgPbTtx6fOvC1uHpqbNLO2h8VAW7fyxwPejkQbBRlphgqUcTSfs2uyzPWlM6p9v5aBtO hmBtpO/5xbTslvs4cFMkMNEnP6Tys5W/ThTfmg7CGBMBYaE+etv2QJh17q5YlcBVacGvHS dtk34L/3+ApVIrMjZBsr8YpLz7WiQsw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iDDDhT1Z; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.18 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=1712820388; 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=Z6+wvDrkutSDOw22Ejx1fEgvkMhl6byMwiuW888CkvU=; b=8oTmuhBC4ovzCUBtZbbb2SNbCOmnjIOLIYUIYL0tYVAQgnkAUj79NeKGp64+0gCIrcf1rH X9+YgXz1d20KzOetJrX2m1Iev+DO8vmwCevUjFacDS8oA8/MCI7+XFf2AFvIK4bfYvUOmC UEY8imjeUxeQv+PEEE8D+UlTSkOz2EY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712820388; x=1744356388; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=u4Zt6ncn47IgJB14JDZFwjyqGxYcviEbMnt3l0dVYJg=; b=iDDDhT1ZMa4GwEhfk1QAop5uoyZDZKYO6uG2XcVNMO24UzmODEpzAqde h9ecVjOlBvoMe56tqHNjNgip1ncXv04qbcrnpi22l+v7cetP8pVU9R5R5 erKm77pGwoi2svWUAOKoRBR4WEcd3WpoCS2c5APUL7dZ+vqrcS1ldUFMU XzIhuKFdXWBk4mcerBsySdyGf15XQ5+7xyEnySSB/1OhQfhwDuvgdrt2z Z37qSjHLq59wEl4NEwFOmZgsTIsFD9cM8pLkA+CSndxo6BzQJGNqzqXZG 6ZYLllFrvr55DpHiIfHEX4xyUN/9fYPNW/EeUL3hJNQi7sz9z6feHmvjA w==; X-CSE-ConnectionGUID: YaBva0GCS3yMXCwsX7+0Ug== X-CSE-MsgGUID: j/21UGpFRBK9/uyDVpYatg== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="8074392" X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="8074392" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:04 -0700 X-CSE-ConnectionGUID: lJ3/DrzzRJORhGhCD+Jd/A== X-CSE-MsgGUID: PvvaWwV2Qnyx++zeZKMVHA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,192,1708416000"; d="scan'208";a="20848555" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2024 00:26:04 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , Shuah Khan , David Hildenbrand , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , linux-kselftest@vger.kernel.org, Mike Kravetz Subject: [PATCH v14 8/8] selftests/udmabuf: Add tests to verify data after page migration Date: Wed, 10 Apr 2024 23:59:44 -0700 Message-ID: <20240411070157.3318425-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411070157.3318425-1-vivek.kasireddy@intel.com> References: <20240411070157.3318425-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 2F8BE100002 X-Stat-Signature: ofber1wh4ao4sedcegswt877ffgz4q9u X-Rspam-User: X-HE-Tag: 1712820388-342864 X-HE-Meta: U2FsdGVkX19JXHNsFlrO5bOSpCi72cxw5AXMvAmWrQ2OS7TRA1uiODRSGscdLQjXwp+R+D11i/dQK/2Tuud/HzZkxaxaqA7Nh5YoONCVpnhOAC88m2M2Jnt8GK9T1N6h12zB6EcDnaPde+UFdwMvfswDjtSxrOe68XvJHzU0GML22W0RS+5dvgN+1ZR9RRocMRBvdx1zs66pA8iVM/oZX/48FKeGOBKuK3Tq6z8a3RNUScW8uQTyNV2bwsSqmLeYJckNgmTI2pTKzcuKGPpCz2Z07aSssbWev8x0rVnM0Vhm7UFvj4U0kjlYSJlvW39gQSUBj2Wd+0xwX5WppKCK5JxGJQXudaUfgryXMiknzDXkv0/uyB/EcIm93W6B3K2hc3zRYcmlTscAebyhJ6zCSN+EaIQZC92J8exTVx4el2Nfei5TwMNs/WU/jNd/5yWi+DEJ0xZEp2HrsUjYc41TSVey6JbjTCJ7aTGV0UpE4Z4O03A5vFLLttCt2mJS+0q3g4YpoWs0gkjr12MeIYAQnd96+sm7cdWgT3+WwORXv/nlwim7Gh2lU+j6IFXajIh8+lUQUpDCedrNFcYeWnW1gFEBSfvxRoYwx7i2+KkPY0O4/O/9l5scmnOZISV9APbYgdkEDmFDMIAXwrbOa//hoh66fUiPM+vNcia+fy++vs6l/VWwxW7C9gQTJbsKJury4qNShXHwE1FeIRKAmoZncp4foQtj7jhUs2ZoyJKiTo76kTQQz60wCjSsx1di8nBtOtAKNDEZnoLogPoZCz+vXCtB1zlM6zwvd22oAF3K0gdUsMfuZegTmPTNFvv7Xs5yXBVt/IxjQj4lr6gkpC6DoO2t7wQzzjo/+mPqRircXD1oAkDcxTUUcVpyOa10EN7jNnvsbnmSgNKGpRiA3ASLCDEh16fGuYVYNyBW4tQVgS+eyGuMPwRWr7PtYKExOL85myoYydo42Ebww6Lff88 oV99GkZX Toe84vmZriqD3McZqjfGmkN8X3uDFqYnqM+jtlMa74juTvHeAxTKjpMdIzh9yxmANH6mO1YA0No6kYQyztEhEhqZsktxw7OY7g8EWIrtZQfyA2/7aXxgmy8cxhh0Y5JE95iV/u37SWguOIY7akUL/JCE72GHByh51cnKxBr3Hp1M9e+LwK5UWfMF01rf/0gcDB1nL/6QRlsE3ohbt+q4UbajxKhwaBeocOzFkmguHahOrW+O0F/aN760tz9Ws+9d88D3dpEdI9Jfh/MMGq7P9ClGCeaqSssvrVR8C2WBcTTddb258taa2zi0L8FLClXKnvFdZ7tyIMccNvq/Jy2xFz498A8vyTQXa/bgDFB7/w+wfpD5cPjkkU57U9wGFRlBVyuYDKY8H3tjdlGs= 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 here: https://lists.freedesktop.org/archives/dri-devel/2023-June/410623.html v1->v2: (suggestions from Shuah) - Use ksft_* functions to print and capture results of tests - Use appropriate KSFT_* status codes for exit() - Add Mike Kravetz's suggested-by tag Cc: Shuah Khan Cc: David Hildenbrand Cc: Daniel Vetter Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Cc: linux-kselftest@vger.kernel.org Suggested-by: Mike Kravetz Signed-off-by: Vivek Kasireddy --- .../selftests/drivers/dma-buf/udmabuf.c | 214 +++++++++++++++--- 1 file changed, 183 insertions(+), 31 deletions(-) diff --git a/tools/testing/selftests/drivers/dma-buf/udmabuf.c b/tools/testing/selftests/drivers/dma-buf/udmabuf.c index c812080e304e..6062723a172e 100644 --- a/tools/testing/selftests/drivers/dma-buf/udmabuf.c +++ b/tools/testing/selftests/drivers/dma-buf/udmabuf.c @@ -9,52 +9,162 @@ #include #include #include +#include #include #include +#include #include #include +#include "../../kselftest.h" #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) { + ksft_print_msg("%s: [skip,no-memfd]\n", TEST_PREFIX); + exit(KSFT_SKIP); + } + + ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) { + ksft_print_msg("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + exit(KSFT_SKIP); + } + + ret = ftruncate(memfd, size); + if (ret == -1) { + ksft_print_msg("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + exit(KSFT_FAIL); + } + + 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) { + ksft_print_msg("%s: [FAIL, udmabuf-malloc]\n", TEST_PREFIX); + exit(KSFT_FAIL); + } + + 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) { + ksft_print_msg("%s: [FAIL, udmabuf-create]\n", TEST_PREFIX); + exit(KSFT_FAIL); + } + + 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) { + ksft_print_msg("%s: ubuf_fd mmap fail\n", TEST_PREFIX); + exit(KSFT_FAIL); + } + + 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; + + ksft_print_header(); + ksft_set_plan(6); devfd = open("/dev/udmabuf", O_RDWR); if (devfd < 0) { - printf("%s: [skip,no-udmabuf: Unable to access DMA buffer device file]\n", - TEST_PREFIX); - exit(77); + ksft_print_msg( + "%s: [skip,no-udmabuf: Unable to access DMA buffer device file]\n", + TEST_PREFIX); + exit(KSFT_SKIP); } memfd = memfd_create("udmabuf-test", MFD_ALLOW_SEALING); if (memfd < 0) { - printf("%s: [skip,no-memfd]\n", TEST_PREFIX); - exit(77); + ksft_print_msg("%s: [skip,no-memfd]\n", TEST_PREFIX); + exit(KSFT_SKIP); } ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); if (ret < 0) { - printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); - exit(77); + ksft_print_msg("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + exit(KSFT_SKIP); } - size = getpagesize() * NUM_PAGES; ret = ftruncate(memfd, size); if (ret == -1) { - printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); - exit(1); + ksft_print_msg("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + exit(KSFT_FAIL); } memset(&create, 0, sizeof(create)); @@ -64,44 +174,86 @@ int main(int argc, char *argv[]) create.offset = getpagesize()/2; create.size = getpagesize(); buf = ioctl(devfd, UDMABUF_CREATE, &create); - if (buf >= 0) { - printf("%s: [FAIL,test-1]\n", TEST_PREFIX); - exit(1); - } + if (buf >= 0) + ksft_test_result_fail("%s: [FAIL,test-1]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-1]\n", TEST_PREFIX); /* should fail (size not multiple of page) */ create.memfd = memfd; create.offset = 0; create.size = getpagesize()/2; buf = ioctl(devfd, UDMABUF_CREATE, &create); - if (buf >= 0) { - printf("%s: [FAIL,test-2]\n", TEST_PREFIX); - exit(1); - } + if (buf >= 0) + ksft_test_result_fail("%s: [FAIL,test-2]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-2]\n", TEST_PREFIX); /* should fail (not memfd) */ create.memfd = 0; /* stdin */ create.offset = 0; create.size = size; buf = ioctl(devfd, UDMABUF_CREATE, &create); - if (buf >= 0) { - printf("%s: [FAIL,test-3]\n", TEST_PREFIX); - exit(1); - } + if (buf >= 0) + ksft_test_result_fail("%s: [FAIL,test-3]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-3]\n", TEST_PREFIX); /* 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; buf = ioctl(devfd, UDMABUF_CREATE, &create); - if (buf < 0) { - printf("%s: [FAIL,test-4]\n", TEST_PREFIX); - exit(1); - } + if (buf < 0) + ksft_test_result_fail("%s: [FAIL,test-4]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-4]\n", TEST_PREFIX); + + 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) + ksft_test_result_fail("%s: [FAIL,test-5]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-5]\n", TEST_PREFIX); + + 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) + ksft_test_result_fail("%s: [FAIL,test-6]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-6]\n", TEST_PREFIX); - fprintf(stderr, "%s: ok\n", TEST_PREFIX); close(buf); close(memfd); close(devfd); + + ksft_print_msg("%s: ok\n", TEST_PREFIX); + ksft_print_cnts(); + return 0; }