From patchwork Thu Jun 13 21:42:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697519 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 61CB3C27C79 for ; Thu, 13 Jun 2024 22:13:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD0B66B0093; Thu, 13 Jun 2024 18:13:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C80026B0095; Thu, 13 Jun 2024 18:13:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B47956B0096; Thu, 13 Jun 2024 18:13:40 -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 967FF6B0093 for ; Thu, 13 Jun 2024 18:13:40 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3780F1A02BA for ; Thu, 13 Jun 2024 22:13:40 +0000 (UTC) X-FDA: 82227268200.18.CE99C0F Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf09.hostedemail.com (Postfix) with ESMTP id 11E24140004 for ; Thu, 13 Jun 2024 22:13:37 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kkXFLC2V; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316817; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=m9a+hOIT351Wv2xQxo5iTfv/+xw1PBLrEf1P3SY0Ylg=; b=OEvSTA6Nzc2dlih4SqEV7Cz2jZixocvNiy3PxtXbV23e9iDeN/b3Qm50+L8FFubqRThXNp mdY3qeD0j293zoOgxT4EPVcXskhZx4u27qWZWYS8KTNXKL/K4mz7JInM8E9qBA9UM3/Jjv RZL3S1zGcpWBLIqHvjRdvG2LxJ43k6M= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kkXFLC2V; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316817; a=rsa-sha256; cv=none; b=1niiTVAv6x+VssZgcF4KgIIWv7Nj7U+nRUVSLrnLnWO5qD9RNxjCIRkRtAi6azW/8SxVFo XcaeOk7T4+qW60i4qM02DAZumsb6viafMgyzGXV+ez3ff3u11/S9Z4qG83AO+RwRkVBgII 8u2Q1pOON0TJj7hwNQWs803cEYbluHs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316818; x=1749852818; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7LmtFrOhCV/a7FkyUUWDhsbXnORpzObssqSBq1B/s/M=; b=kkXFLC2VleN7pIowOu9BRtskQtbIzRCeeFzI2Cg91kubxIZv/YAu254m Owsbq5N70pgt73maqum82o9yB9KmyKgvQr3EpPLf3xnNVRoEBe9Y8cb6J oE500zk3tW0D57MDnC+wLEwY9tkgfSf/6Suu7QzbGUVSeZ0D6rwQHIutt ToZfuAnBj/X7MN0+8gUtVjMRAXft1BMxqMVz+zU37D32qKaINEmSxktdy uKP6Yb7+oTLPePRzxRJTVCHYmrxMoPcAxL25U/nrWv/uAjUnUAIFMt3ba GaaLc5ggSNCNll0DPDNuNe0MiJJ/arB6WBYFHVZvW2YMJpdSmMz4pUoNx Q==; X-CSE-ConnectionGUID: b1rJ+DsPSUC9r5fIEcyaeA== X-CSE-MsgGUID: zvZRUaWlTlGp9S6AdJCRJQ== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720532" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720532" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:35 -0700 X-CSE-ConnectionGUID: sYdjidJQQ4KkwzTdMnR1DA== X-CSE-MsgGUID: ZV0Z0hEGQdyMKJf3YvZfLg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214091" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:35 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Jason Gunthorpe , Peter Xu , Dave Airlie , Gerd Hoffmann Subject: [PATCH v15 1/9] mm/gup: Introduce unpin_folio/unpin_folios helpers Date: Thu, 13 Jun 2024 14:42:03 -0700 Message-ID: <20240613214741.1029446-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 11E24140004 X-Stat-Signature: 47rbzrp48u9roj75y9zp1rtbztgbux8r X-HE-Tag: 1718316817-168955 X-HE-Meta: U2FsdGVkX1+s1dTwRsbBIKNS9tlovWhfwVNWhDVI7QpfU8duEbQS1jRrzhYFJgD8xaFEFEuI1mK7L1oEL9Y00sk6hdLt6x2lb7HqnslbyFIU1ub3AlW5Ru6svQvFcMmRlBSr9hfhPb0wYsLZRiwBQCZPVvvnT1Nn9KOikHLSzmJsD4aqYilP7VSTVL9VDMbUNsNxJVDjRu6/QaGN09bgsOeExGxUZ/xdMzuABLQu+jmndmFjNpG+FHUABKTWD9UspFaRTcfu1nj3/PPb3c8DggvSgb0kAVyclxFxHOLy8+TVf1scjsEi8EzN2MpqpcJlWxyAFO8EzF52NWhFSFDq/P8fDFE2D+O1SlO91+aVB1/t/angE7ZeMJP/NSqmwzYKEB/A9gbd0bjrsYy1QQsVM+4sBxYe/Ruc5fmAu1/1P5Q5yOKLMQlRie6cB6jGMoxTlLuvMss6hgbrVI11/pXidBJzHUkRcVX7ylyn4xOkp91zLDKajOhlZ8eNori891lyqyJkd/YA+yV+Xbbi+20gQjumdri7HtbtIxlJcZIllGOuIrxR058wcbB1LUyy9be5grII63efteZ7rPOpJxDcORH2IupBEO/leyBveSh9cv/dWdIzz0SEKobrPhfGHwFO7QgswwHkai88NJkp9VZ6XHWwLLTBtAP5raFV8rN9Dp6EClQkKhyjd1wexd3nBDvQ1Sa995EYU2mvtl9xKxkPQzWXQFJyK5jExtN8iA3YfzWmT1nb4O7Jp+Ib8JVZWGYVeUmbqTtzjSk+FMDFWJpkZDYB6YwyP85a8R8focLYCMZz+Vla7panuP1Q05790qEZRFDii8jRSHw1iVlzc2I30HMydcIZ2VaqKLeNywUABTaqhtOt5lt2Tum786ASHYvWL07eEStdTfjFY+1iSAJ1Se57qBLitAmgHTZOHRi8895t1frpAfyETkBXZAnP7n1x9ssKFMgR0Ft6sY8b5al kM4oHila PiKthmorDyJpBvUSoR/6JUyuvHCWMubGk1MGlNX7S73uRR361108GZ/uaCAnff9aw2GjfwO2GRLYu/XWtzBWThr26kOgLF28olcErRKRFlVNeDZg14arjfxjjuzUMohjIXHUYHUDerVTcntcJkVaZDfhCdszxYcSYTWta6g3Z8ssiBCQ3IB0jptB3a0LZBPAfqKSLe5Oie+qtGye6AoqHuuoVTEbN4FC6PAhA1firtogYExY0fNVRLLck/5j1LCuW1kU9dg/RP6hWtuTyCsZs9E9Csr2iHHraCQ8/5G4qe5fHm5vi0sVZI67lsQ== 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 Acked-by: Dave Airlie Acked-by: Gerd Hoffmann 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 25b541974134..0f953405834c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1579,11 +1579,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 6ff9f95a99a7..d9ea60621628 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 Jun 13 21:42:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697521 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07E37C27C7B for ; Thu, 13 Jun 2024 22:13:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6DCC6B0099; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D1D6C6B0098; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE4806B0099; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A440A6B0096 for ; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 210E440359 for ; Thu, 13 Jun 2024 22:13:41 +0000 (UTC) X-FDA: 82227268242.09.E668396 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf16.hostedemail.com (Postfix) with ESMTP id EE64A180003 for ; Thu, 13 Jun 2024 22:13:38 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Vbv8+vbN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718316817; a=rsa-sha256; cv=none; b=NudYP6wDR8Noqimeqsqw5TZ+iEpm/9hb1ow/drI0VLGnuEpNmRitpdgAVUhFiyymoDh/cB hUCYLymCj+fraNYc5oJqU4C4Q3IUqyn5pEczo4ISRaIQNd0VDCf0d9QRFzGn64Nqtjfp/8 i4WWYu5Y43dRu+8nLRC/A0eJfnSj978= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Vbv8+vbN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718316817; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WdfJs2fGTcT6wyOt02CtufQGS6XpiVjlndTuH5LRebI=; b=qyZgdjL0hTpmJnL81++zVT2ZM1AuoH9UwQLQ1VWhi7x3fYMsgFA2QMmsmor5gj+lNEg8T1 kTCSxmXiz9Nv1ORYGP41I6QZbHzV1Q1J1KnJLxgN1TiK0zI6Z3WDJwALO8tAx7uxa82sAg f+XaO2dByR0PU0svfYWSFQeGqns1Zdg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316819; x=1749852819; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=A1Q4hK2VF6Kkq0SWMg39yfc5/Z639gK4zvJ914UtXkE=; b=Vbv8+vbNfKJLySDYKmpIIBqGrWWt0BnqcjXe3THkxYn4BUYNKYd8eCsH HPO05lKgSIk+byjsw5Ts5NlnwPiyPk1kECl18Xsv62g1mulMLHZEJR41J hKwXoFsKBPPoSCVj6rFoMcR1OFdYgR8OtgzKUijaOMmKs6ipOhBBA3oXG S05go1CTUmrP2boW0mGOz0NyKj+iaTEYy4sdkY7SPWVEw7lATK4d61eKr 8MFsKFF1PfQhCqWyZg1oOGbbhE9RUrQCNcZC32iKt9+fXwU5Y/QwHkAoG +e4a4vwqJDl9qVVRvmvYXm3S/sybhj7K53mMs+c1l9PdkgHOo7lvD402A w==; X-CSE-ConnectionGUID: LBkV5OrcQFC0NDYFtC/98g== X-CSE-MsgGUID: nwfelcPGQUq5d2Cpb2DtkQ== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720537" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720537" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 X-CSE-ConnectionGUID: NKbLsMRUT26Jf1IKfP3I9A== X-CSE-MsgGUID: LisDwqOGR3K43NzJBlu6Eg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214094" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Jason Gunthorpe , Peter Xu , Dave Airlie , Gerd Hoffmann Subject: [PATCH v15 2/9] mm/gup: Introduce check_and_migrate_movable_folios() Date: Thu, 13 Jun 2024 14:42:04 -0700 Message-ID: <20240613214741.1029446-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: EE64A180003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 98jjcmbwg8tbxr8si41tqn599q73jb8a X-HE-Tag: 1718316818-61125 X-HE-Meta: U2FsdGVkX19tNz6U7WiDBI+/mdaxdDuPp+PLKaxeT33VJ3CJ5AbiwQk+ln+ZjjAyxxerSPUKMe92Jd8n7p0CfxJ3o4FYstX3M9V9UdXoicbKuz4/sjB7a9NIrVclOzZFPga7KVWcpGhNBbDkesTyyyXtZNlstmVijXKcoRbAzEW2+ObVK2DwZ7E4RMKM4Yky5va4/nolPUay2//9JqpXWJRVZ8qJc6Y0boeG6K5P8cNNBEUugKc/D06fYNhZUo15GZ2MDGvL3cpesrwTuHLKvrUz7C/qI9bkKb/49MdZCjllUZMBPc2QBzXFXqvd+D1ZSWsNjGAPBN/PQbrPK5BIA9sIUMXAGXzDqov4gr0bGUVu1Tf4u45/yBG+mVISqKvjAHfj2awfjLV9fIXC1ttKR1C/Uq8CB0ikkY+QBnj8e3swZEuJ/gXbEgUUvjAnv6sgggA2p3wkOaJV9naSdAKYp6tyQJRIwep/3zfdZ2RbmtIl7iAAxcaCELWeLzsPaMG++amFjdJgoqrl6NIbLjZuRSMS0gBE953aop+A/YQUCqZYk39xEdobsKzUT1yE+p3hb/hwk2+nhVe0aW0jz3wm0kx6jp0p130KDUJ43DC5oh1ZBsibvppYAtxCvJdklA6QYEpk/MpyDbcRawxjGymXYA2hXUL1DPb0d6gGa8pKv8nP6joY0M0HVYmIhgmLbOlsraPE+Er5wdgay4SdPmtF/pCuCM+r8xc2m1C8LiK9zLmS/0ZYJddlUaFTvR+cqvbrI6z7bkdAMSBErf9mKPpeSjArXuns2++0bxKX53Jje3O65Ow0cWVNzNfbu40R9PpgWHz2qXniZ7g6xMTPT5ki376zoRHV2MOJ0EgEQFWK5aPpSOv4LbFvmQJBgLvPqjaxgMqhfAYNH5cEG+FiQvDNzdjUpA0TQ6IKq4FCJBdpbl2UD1uoXYNn9SDC4qOtqJMl5Q6LuohGuDFpP3pqEB5 RrqRN7UF owQFMcBOb5PcI92xa5sZVXVvOzXMlsM5xC13huygXAbhBz0t3Tt0Mg4ZfaPpUzsdZ7A5C8OdfXWCt+IhMXug+hUr+/i+fFilW5BjJXgDPVP0AFOcZGUSmAfSObRdOXdNYh28TfBqvK7n2latEPRECq3eXpLOL3kGbjXcGlq+pTUv3OEQ0ypaseISiVdXmXthwSQ+iCwGJexZ7GaDQ4qQMhf1qXW2NEDLmYMNa66QEOYx2i9ONg/v/gYbnKhj5D0AX7Id9YtPeo6J9MBl4gxVGejtYCQwUeKAqu/6kmoGWXd56C9KT1Mqs5734ew== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This helper is the folio equivalent of check_and_migrate_movable_pages(). Therefore, all the rules that apply to check_and_migrate_movable_pages() also apply to this one as well. Currently, this helper is only used by memfd_pin_folios(). This patch also includes changes to rename and convert the internal functions collect_longterm_unpinnable_pages() and migrate_longterm_unpinnable_pages() to work on folios. As a result, check_and_migrate_movable_pages() is now a wrapper around check_and_migrate_movable_folios(). Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Peter Xu Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Acked-by: Dave Airlie Acked-by: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- mm/gup.c | 124 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 47 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index d9ea60621628..a88e19c78730 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2427,19 +2427,19 @@ struct page *get_dump_page(unsigned long addr) #ifdef CONFIG_MIGRATION /* - * Returns the number of collected pages. Return value is always >= 0. + * Returns the number of collected folios. Return value is always >= 0. */ -static unsigned long collect_longterm_unpinnable_pages( - struct list_head *movable_page_list, - unsigned long nr_pages, - struct page **pages) +static unsigned long collect_longterm_unpinnable_folios( + struct list_head *movable_folio_list, + unsigned long nr_folios, + struct folio **folios) { unsigned long i, collected = 0; struct folio *prev_folio = NULL; bool drain_allow = true; - for (i = 0; i < nr_pages; i++) { - struct folio *folio = page_folio(pages[i]); + for (i = 0; i < nr_folios; i++) { + struct folio *folio = folios[i]; if (folio == prev_folio) continue; @@ -2454,7 +2454,7 @@ static unsigned long collect_longterm_unpinnable_pages( continue; if (folio_test_hugetlb(folio)) { - isolate_hugetlb(folio, movable_page_list); + isolate_hugetlb(folio, movable_folio_list); continue; } @@ -2466,7 +2466,7 @@ static unsigned long collect_longterm_unpinnable_pages( if (!folio_isolate_lru(folio)) continue; - list_add_tail(&folio->lru, movable_page_list); + list_add_tail(&folio->lru, movable_folio_list); node_stat_mod_folio(folio, NR_ISOLATED_ANON + folio_is_file_lru(folio), folio_nr_pages(folio)); @@ -2476,27 +2476,28 @@ static unsigned long collect_longterm_unpinnable_pages( } /* - * Unpins all pages and migrates device coherent pages and movable_page_list. - * Returns -EAGAIN if all pages were successfully migrated or -errno for failure - * (or partial success). + * Unpins all folios and migrates device coherent folios and movable_folio_list. + * Returns -EAGAIN if all folios were successfully migrated or -errno for + * failure (or partial success). */ -static int migrate_longterm_unpinnable_pages( - struct list_head *movable_page_list, - unsigned long nr_pages, - struct page **pages) +static int migrate_longterm_unpinnable_folios( + struct list_head *movable_folio_list, + unsigned long nr_folios, + struct folio **folios) { int ret; unsigned long i; - for (i = 0; i < nr_pages; i++) { - struct folio *folio = page_folio(pages[i]); + for (i = 0; i < nr_folios; i++) { + struct folio *folio = folios[i]; if (folio_is_device_coherent(folio)) { /* - * Migration will fail if the page is pinned, so convert - * the pin on the source page to a normal reference. + * Migration will fail if the folio is pinned, so + * convert the pin on the source folio to a normal + * reference. */ - pages[i] = NULL; + folios[i] = NULL; folio_get(folio); gup_put_folio(folio, 1, FOLL_PIN); @@ -2509,24 +2510,24 @@ static int migrate_longterm_unpinnable_pages( } /* - * We can't migrate pages with unexpected references, so drop + * We can't migrate folios with unexpected references, so drop * the reference obtained by __get_user_pages_locked(). - * Migrating pages have been added to movable_page_list after + * Migrating folios have been added to movable_folio_list after * calling folio_isolate_lru() which takes a reference so the - * page won't be freed if it's migrating. + * folio won't be freed if it's migrating. */ - unpin_user_page(pages[i]); - pages[i] = NULL; + unpin_folio(folios[i]); + folios[i] = NULL; } - if (!list_empty(movable_page_list)) { + if (!list_empty(movable_folio_list)) { struct migration_target_control mtc = { .nid = NUMA_NO_NODE, .gfp_mask = GFP_USER | __GFP_NOWARN, .reason = MR_LONGTERM_PIN, }; - if (migrate_pages(movable_page_list, alloc_migration_target, + if (migrate_pages(movable_folio_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC, MR_LONGTERM_PIN, NULL)) { ret = -ENOMEM; @@ -2534,48 +2535,71 @@ static int migrate_longterm_unpinnable_pages( } } - putback_movable_pages(movable_page_list); + putback_movable_pages(movable_folio_list); return -EAGAIN; err: - for (i = 0; i < nr_pages; i++) - if (pages[i]) - unpin_user_page(pages[i]); - putback_movable_pages(movable_page_list); + unpin_folios(folios, nr_folios); + putback_movable_pages(movable_folio_list); return ret; } /* - * Check whether all pages are *allowed* to be pinned. Rather confusingly, all - * pages in the range are required to be pinned via FOLL_PIN, before calling - * this routine. + * Check whether all folios are *allowed* to be pinned indefinitely (longterm). + * Rather confusingly, all folios in the range are required to be pinned via + * FOLL_PIN, before calling this routine. * - * If any pages in the range are not allowed to be pinned, then this routine - * will migrate those pages away, unpin all the pages in the range and return + * If any folios in the range are not allowed to be pinned, then this routine + * will migrate those folios away, unpin all the folios in the range and return * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then * call this routine again. * * If an error other than -EAGAIN occurs, this indicates a migration failure. * The caller should give up, and propagate the error back up the call stack. * - * If everything is OK and all pages in the range are allowed to be pinned, then - * this routine leaves all pages pinned and returns zero for success. + * If everything is OK and all folios in the range are allowed to be pinned, + * then this routine leaves all folios pinned and returns zero for success. */ -static long check_and_migrate_movable_pages(unsigned long nr_pages, - struct page **pages) +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) { unsigned long collected; - LIST_HEAD(movable_page_list); + LIST_HEAD(movable_folio_list); - collected = collect_longterm_unpinnable_pages(&movable_page_list, - nr_pages, pages); + collected = collect_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); if (!collected) return 0; - return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages, - pages); + return migrate_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); +} + +/* + * This routine just converts all the pages in the @pages array to folios and + * calls check_and_migrate_movable_folios() to do the heavy lifting. + * + * Please see the check_and_migrate_movable_folios() documentation for details. + */ +static long check_and_migrate_movable_pages(unsigned long nr_pages, + struct page **pages) +{ + struct folio **folios; + long i, ret; + + folios = kmalloc_array(nr_pages, sizeof(*folios), GFP_KERNEL); + if (!folios) + return -ENOMEM; + + for (i = 0; i < nr_pages; i++) + folios[i] = page_folio(pages[i]); + + ret = check_and_migrate_movable_folios(nr_pages, folios); + + kfree(folios); + return ret; } #else static long check_and_migrate_movable_pages(unsigned long nr_pages, @@ -2583,6 +2607,12 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, { return 0; } + +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) +{ + return 0; +} #endif /* CONFIG_MIGRATION */ /* From patchwork Thu Jun 13 21:42:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697522 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 9E2A9C27C75 for ; Thu, 13 Jun 2024 22:13:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C1286B0098; Thu, 13 Jun 2024 18:13:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 248A36B009A; Thu, 13 Jun 2024 18:13:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0EA386B009B; Thu, 13 Jun 2024 18:13:43 -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 E37A76B0098 for ; Thu, 13 Jun 2024 18:13:42 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A9AB91C0653 for ; Thu, 13 Jun 2024 22:13:42 +0000 (UTC) X-FDA: 82227268284.26.66610BC Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf09.hostedemail.com (Postfix) with ESMTP id 687D0140004 for ; Thu, 13 Jun 2024 22:13:40 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bEC7dHDM; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316820; 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=YS/xDhlmM2kXiZCnba4l347H730IOOlyjMN79n7xJg8=; b=Qeh5lI7azn585aPJoNvPPZJYbd8OkqD/G6WxCgeWrANo70dJDnnvhMypYHVnItki0MHjqw cQTX+p2SBmw2fe05+q/AwpYrTHuttHTntVxWyQ9ZoxMIY1etm7W5wLDeh8DOzAeDNWOu7b Ije3ptn8Q2fJJ79qJ2FuyJSWEGwu1xk= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bEC7dHDM; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316820; a=rsa-sha256; cv=none; b=QfaEoqFboWjMkjTQeRnqk2T5srKslv7lkiJXmnzidWG4I0Qu/1VY3EOA8HttyIq/B2+dR2 dSjTw7ldKzzwvIRXgqXwjnex2VE4Pmucj7EuCiCk2MYUNDHlFOGEEnjYvWmcdFiIUscvWS cRefzieaBOGi7Br0WbfyX5EJ5Sr9iMo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316821; x=1749852821; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SELNYctG11w1kS7xgsUKyh8QgxqBF2A+A27utcRl8zE=; b=bEC7dHDMSa55vmnqUR5rfx6Wc1V5yhdqVyT/0Y7qmZsJH9r91vW8ZO5W LcIRx5X+VzxeC+G7cON7HvT6NQe7EDdYnCv7oGySLh83Hm6CoMUfvBc/1 Dak+lrWG4mWfBsuIAgGnuSc01ne4mTTJ4quz+wtZmn10CWcqRP3RekxvY WEeRMb4Cxb92JD+r6f3Hi4ApXATB4Zt67a8bRm7FLHRNZ/ylwOj1nJ7Sy Z2Cmb01tbDxeksuhMEof7q38SIcw/E/ANZoLN9fVvec4ofQqzQgwZGqIn FoFM2Lo9IVj7M2zbQaPxZuBupEmmTXOolEvM82XSOqX0EQt36USIlVXjv g==; X-CSE-ConnectionGUID: 3S+++PsBQtmiA8V1po2vwA== X-CSE-MsgGUID: XVng0bo8ReGy/HvZELF2BQ== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720539" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720539" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 X-CSE-ConnectionGUID: NTr4VGmXTQq9W6IaZKxbQQ== X-CSE-MsgGUID: 2gVCWvGMQ023HJTrbmNRag== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214097" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Daniel Vetter , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe , Christoph Hellwig , Dave Airlie Subject: [PATCH v15 3/9] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Thu, 13 Jun 2024 14:42:05 -0700 Message-ID: <20240613214741.1029446-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 687D0140004 X-Stat-Signature: gq7ps71f3cub93fcx7b6xdtuuyu99byo X-HE-Tag: 1718316820-305639 X-HE-Meta: U2FsdGVkX1+uWlDusPjBOYyC5aK11mZhovbVuII005Qa/ZLAtPYplKHX4mSBZ5ZTgnc/MXj/xrxdxGRrZ6K3/OHxyIj7YTRSW4k/rkHHkLJ/c5ziscMxd74b2OUMAr5EtYzc5Yd81R0ftgN1mhfD7OqpaJRJpLxDraon7yAssffy1gGeU1HJNkhb2ckuFIoNBzxy2iCgR5OLWA3wL+QN34uH3uyIEFvucXo9hShmeFnqgULXm1i+s83hlVBApcx9NlFem5ft1hq1LnImE1rh8hcN9k0DlWtQxDwKaKA46tBeohb+TF8EzwxdTvsqk9n/h66QfS/1XxTLf26V0sRHu3KzExoJFJvG6UL1NEVQVFsZr0FYBIyLVKIWTtVMvrTqZBVAjN8DhmMRdwE98CqGX7XZv1hdvvgn+/3Tcuay2usSJMknaDVnYlBAaSVV9q3EuqFwACHhkcwO697auWtJGk3ltuPLGp5WQQRjT/8N8vEQMH2JJOTn3lw5tAazYtMv5EHg1Is6cLlfBgGeCCiOZrbh0XvVdUFEhS2kpvISQOdaXaVf7Kivtehh/sy/FdOPVqP/jmCfK5nGs81tPaowwucVaULoDClRhG+AWwyzvv88WJ9czrGmDk3rR9U/q6I3XmR7ABlNpC5ISRKPPEJgb8XmpRA6eSIMDoSshOcgLOSX4tny1D7khn6L+X1XBk/vjoGL01upFSWYaFSycee4IBIqEsxQd4WqjhDkuz6iA1H4o0zRzvH1Sp+VtE/e7cXGg8k2wi3+mzN0nB7MpxXaV8ARmkvXSJ7+ipwH6sGrbE087Tqwt7FH1aEGThYcTosOcflR8QGGlWwU7v/9Ux1zKbWu5xg+EEiAML2PRUP4runkbrWm5Y+UkMQrQKoNEfrXsTt9XyFl1vwoAnPbEvfo/PT49PHbb8xLJAdNOogHndnGEgtdcGRJF9hoVfC2tAKFt/XFJvMIa8VN++7uEo+ lggSIYwE E9nHe3Q9Cr6xKfgSQ2o28lYmrnY5uOLD6VmpV8Q4ktH2+bf6JITZeh5cWYtW91+07Pr4qylXaYZYNMKMVyhBnpuF7VVayFdwAmEtrxj5HixLlVXq04ZDw4Tl8MmAo4rk8ZXwDzHPwFntFrWGlHmHCQ1/MtWpo1hJYVJpQuq4Q0DqHPRj+PQEq8DRQfhLxcXkqVtvuUooJsHipeyYxoUnEz/YEBVoKzUomV4jQqu8RJeJxcZyb0FZl+BOL0FA74HPSRvKNhgHkEkcx27Qaqpqa+0Z8GiIaknOKj1QpTYkZncp9e/WOGAeEz7HLy2sTLnB7yjTCRlgdr3Z0mdQZrEA/0P3INw== 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) Acked-by: Dave Airlie Acked-by: Gerd Hoffmann 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 0f953405834c..42e3752b5eed 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2495,6 +2495,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 a88e19c78730..0278dd94f3e4 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 @@ -3747,3 +3749,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 = -EINVAL; + + if (start < 0 || 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 8b7f6afee21d..8035c6325e3c 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 Jun 13 21:42:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697523 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 8115FC27C7B for ; Thu, 13 Jun 2024 22:13:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C38186B009E; Thu, 13 Jun 2024 18:13:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BBDFC6B009B; Thu, 13 Jun 2024 18:13:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A386F6B009D; Thu, 13 Jun 2024 18:13:43 -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 7C8986B009A for ; Thu, 13 Jun 2024 18:13:43 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 247E914038C for ; Thu, 13 Jun 2024 22:13:43 +0000 (UTC) X-FDA: 82227268326.26.8CAE782 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf03.hostedemail.com (Postfix) with ESMTP id CC03A20002 for ; Thu, 13 Jun 2024 22:13:40 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UEyte0Jb; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316820; 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=imyxyEmjs7okOkW4X5bmS7u5/Tpdz/jJa9fnufd4E04=; b=jDMRNQBmdjrNgUXc5FUyglL+eakuwFQEcwVjrn8DbVWDx2jfVv7//wY4pGIxAwaQ45u2xd BATL2GnetP6gHrJFFd4Ih2BzhFo8nAmyKG17p/bHjt+a5Dhp254lP+RwxTijUHY8dnPSzY eNyRaqYbTKf7s0432loc+2UEzk4HQUU= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UEyte0Jb; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316820; a=rsa-sha256; cv=none; b=vOajxaukrm4gs8qzGVwTZy5BOwlvdOWkVTFOkwn+GLqIvf8xcwFtj3UD+2FII6PYc2w/U6 y4fCn1T70u0KmA7knjTdnOmtuMG5fVtS9BJTODDgt6LcvmWBcJtspuC/hb9Ddpu8jB0QlT yBTWA1PgfXOmePQzMrNgg26WXQYFV4E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316821; x=1749852821; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fbc/iEJxK8IpwWAyl9EpiegNS5GsUhXEj2obZVdXGcg=; b=UEyte0Jb7RWOYC0jgcQmzwpwiw6CdFnxICDjUQHsI+mwBtbV/2/JVsN5 U5x90yLHLLlv8+Sf3l+xbovRItaacI31ASGJuxqiq0q5RtpyyGLO5AwBh 7cNcVuywUcLlE8c33KCmI0JugyU+rWNahSSCFnpgb21c2uNLnlLhkXtzP BQe4+0Rm8OxlnqC+wEQMYmTWh0RWFIpvTcC74SoIi6uutjS2Elb5WfWEr E8C2ZmVBjyBEkNB/PLOGUejRt0SoqLKxFmDtXbCkZK55W/xmtgWJYvkMC ua1V7e100TqVs8ElxxpD9mUSPARsvJCDm8j95I0pjMpNrZRBmZKv5Dnf7 Q==; X-CSE-ConnectionGUID: n/GEBXRpQdKOfuxv1UecrQ== X-CSE-MsgGUID: Ae4fDjxPQWGpcTa5n/BhFQ== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720549" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720549" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 X-CSE-ConnectionGUID: v6Wh4F+bQM6nkWcKi1+LVA== X-CSE-MsgGUID: KZOhFuG5Qj2FkkdO5Mi3/w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214100" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Arnd Bergmann , David Hildenbrand , Vivek Kasireddy Subject: [PATCH v15 4/9] udmabuf: add CONFIG_MMU dependency Date: Thu, 13 Jun 2024 14:42:06 -0700 Message-ID: <20240613214741.1029446-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: CC03A20002 X-Stat-Signature: 4ykzz668jk876g1u95ah1of1xip15kn4 X-HE-Tag: 1718316820-973478 X-HE-Meta: U2FsdGVkX18Esuz5i9c8lArMeDgLn+DjDZFi8l4dicI5Mgf+YGcVpUMIHhzGGHQnH2zeTfR4I1+nCIAmBGjjYUaW0B3DQHKkgi3kgs7ygQgKdM1613ygePBHgbHgBmO00Yf4NIUjbPD56EA38X2Sxu9YkVs0AZ8SKinRjs/uKFaoLg5PB7r88rZgJQnzUE9DWSvAQazn6cfn0EWCzXMfBycdNBmRaOan+07Xer2oxtLDbDXvMIVXXLHoPFwNgS+vGf2WauOtf5OyeRHgZ3cvgvvv1fp7z25C79RLxUOt2UEmlv2/AtJGTC/CjDAPWUkGhPZ5EhvhPq6I0nv41I1+epbJNd0FPlo7FppQDVm2Ot4BRud4kSJOCQ8IajrRCilVu9byDn78sQ8KCwMK+dts5kM5zCYf4R8zp1lIN7BBiD1XUNDRxsauT/fvFQ00BMC0ve23Vqi5ohBJxconVItcCbJkmkn2GTfQAC0owh3N8TqasRSB67QYmcl3lT2ZE16sCZcqYsixSrbulcMb1uG2rehsnTrBtN5WgBS1xzE7rS0Eg7rbaSmtYhQM5jkeT7GWHOzMMxWgLCEbnffGfkQjdqAIzYgoj0HYgl7dY0XusShZl1CF7/Per24CRjcUbZCwAJQJ4OkjFt312Kc9KQeLNo1AqEIZ99qORN7MgBVBLAkUL3DM0VFst4ZvyZgM6MXWqk6xDhau9HOFWK5vLOu9U1PlqtKliXqai4TbarfOIxKYKwjzT57ABAjgFIU+tf3BsGOiR9G2hDDd5Ikagx+VQHdOjfQn7pzcMZkeFdA8OI4wMdWYHjGyesOntIxauedI5g+SvyKUEGuZNdKdxtSNAj38UBEVWZ3MgDY/7EzGHFtqg1GefqAFgD5nZ9Xb1Pr5dfz6/PgZBrrXDLxTccSU39acZq//IoLAQa167OobanMX7VwOZ59wiYKBFs8EXl3iVjwB7P3JxjHk409R3Q6 zwFCxnF5 SoBy416/+Ssp8A5ZFkQTB0DykrgUEYCKfzCbcMCvBbNrrpbij4z5fhvuelooThOnU41nVkmahWvOcynek55ajEDA+4iYNrFFsSzljaT2ou+09TkeLbB7NbRSc9ATUiGEfh0bx75CWyHVUBp3dhdCojalJc7FxHdjIUywZaUB4WSTFdfxuny+tGZemfrB7H+Aja5z9pH4a9JCxhKVqDxZuHBmSDimN/lcHAG5e 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: From: Arnd Bergmann There is no !CONFIG_MMU version of vmf_insert_pfn(): arm-linux-gnueabi-ld: drivers/dma-buf/udmabuf.o: in function `udmabuf_vm_fault': udmabuf.c:(.text+0xaa): undefined reference to `vmf_insert_pfn' Fixes: d1d00dd1fd2f ("udmabuf: use vmf_insert_pfn and VM_PFNMAP for handling mmap") Signed-off-by: Arnd Bergmann Acked-by: David Hildenbrand Acked-by: Vivek Kasireddy --- drivers/dma-buf/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig index e4dc53a36428..b46eb8a552d7 100644 --- a/drivers/dma-buf/Kconfig +++ b/drivers/dma-buf/Kconfig @@ -35,6 +35,7 @@ config UDMABUF default n depends on DMA_SHARED_BUFFER depends on MEMFD_CREATE || COMPILE_TEST + depends on MMU help A driver to let userspace turn memfd regions into dma-bufs. Qemu can use this to create host dmabufs for guest framebuffers. From patchwork Thu Jun 13 21:42:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697524 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 9D7F2C27C79 for ; Thu, 13 Jun 2024 22:13:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 029786B009A; Thu, 13 Jun 2024 18:13:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E94E56B009B; Thu, 13 Jun 2024 18:13:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C33C36B009A; Thu, 13 Jun 2024 18:13:43 -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 A065F6B009C for ; Thu, 13 Jun 2024 18:13:43 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5BB301C0431 for ; Thu, 13 Jun 2024 22:13:43 +0000 (UTC) X-FDA: 82227268326.13.4258855 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf16.hostedemail.com (Postfix) with ESMTP id 3EBA8180003 for ; Thu, 13 Jun 2024 22:13:41 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kHrHwLLP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718316819; a=rsa-sha256; cv=none; b=2xHBb5IA4LU0EysfDwfY+ZGs3zJmQ52XfW6L+cNjycX+z+00OFzXEWvgM6n/TbsYSqVgl8 kVYpntNDzJLOeO8pKaNn4lXxEESDKi9AFknkveD53+ScmFoX83lNAdUxVR++12rjbw5Mk3 IZRdo5u30zMecW3boFs1MQy4PJHf4as= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kHrHwLLP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718316819; 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=BRJfBTOc9Ht9AfvNPFbDMpild2soCLLjG6LLdkG7QSI=; b=yV2bM1bxlooXnlcagM6MoE+pUf9RaHmUJpESqVf9LayO4MVEdPrUoFzzx5l26UgY/JNuSS JFfgR1TNCIMwTnlE5iQ767y7/vo12XZ2ffm3UrSQv+LsRu9VY7obW38nRCcwqH2hKfeWa1 pqxi0sxkK0cnhEt8ONpuCdd9YOi37is= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316821; x=1749852821; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aVFPbZFzfDCXaBzmiLs1+yJntIbEwJ7MP8W2MkHw1Uk=; b=kHrHwLLPG2ka9ABEyKbEv1gtxTd0uC7wBf25ChIi4bERLJ59qLjFZ2j0 3Jmljnc9/QfRRByZ/RCu/iWYtHDZ5aadWG8Ym7BYfzU2u15ZVJcohGbHS gI7s1kN2WMgUQLIWDnCJGJDyY+E44C0gJNcQYgMug9H45lLG2Z/O76chS 8tOTFdEAN1jBOgPL03FbftrFeBbEw8AfCGOhDxPkvuxPvHj/fYT+tL7yd 6lqj8MMfQAKF7ecSBZW88nDD2GG1N4Oh8mptw+4+hAxbJtoobXkr8YdVM MTfekoo94cWFaM1CSXw7cJ8LZF9NZ2YMZ917Fh/0E11pKVsmc72sio2Tq A==; X-CSE-ConnectionGUID: lXOjuaIbS8WTYsQlFJPY5g== X-CSE-MsgGUID: ckVPtu+ZQCKQzuXH1kgEeg== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720551" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720551" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:37 -0700 X-CSE-ConnectionGUID: Ri767mIRQ8ClceH+wjElmA== X-CSE-MsgGUID: Rr9ZrDOPT/WHO7G+/KjQaA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214104" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:36 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Dave Airlie Subject: [PATCH v15 5/9] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Thu, 13 Jun 2024 14:42:07 -0700 Message-ID: <20240613214741.1029446-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 3EBA8180003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: a5i4pr5tpfnk6i4ahfuuommzfotqhxyi X-HE-Tag: 1718316821-120677 X-HE-Meta: U2FsdGVkX19+Afd2biCbZ7rwCBKb3rRaiDoc7cNJIueoIQTPRh6APj/PCFHtV4qkTVFP5Pfpx9iKimqb75Q4xeYfjVuY80/V4AcTSwL75kc7CduExwEsTKp2XEjcY9SKYCtgGru0JO01170/EgI8WW9qP/yR1Tn4w2ztBkpBdFzijZ2qpUEUYk1Rn3WT0dJ/xP2IClICxNCoATy5WDTisSmmA8HMO0Z2dtA0aXy/1wdlfcP+CaLPqDKdQkZYFGy0GVusMNdAazBuhdVilcUzd+ZnVsn2+BDcoyUW1M9EDUnRvDe015pusneKzvUfTkTYWXCA8OX2hkahBe0hJfEBSxCUQjv4B2rouGsQE6KW7EfUYmX5y97A7psWIhADWVAHm7qPyMTPdmikF7wVsi5Dvo2FBqPFd2Wlg8wRNEpnJMEq2hy5HgjGnzwctXndAP95VC7A96uXyBDkvLjzS6sc2PuzhCY0F7Qd73e1utH3Rux1rwj3FHIpCWmw+tCWDI7v07ROF7liwu4Mkvym1vZE+0etHyeKgDwR9x6OYuqgciXj6gDc78BfXa7vvwJWSNFKG909aw69z/5AEut2CBTTpN6N5NGobwfDyjK8BPifjl0YgiyeU42f4SD36n1yK0uCYhPFsUeiuWlyAoKuHZyrkegQTPfObrzoqVOo41rPPZ8HM8c9i0p7sgoZlKH5Yzl/QGtYabF4Hj5mxDSqUZRkDkGw/aPKo/i/vOMRvqUQwOhWFqQecptpVzzRduO2ZWTiA+9JqKgZ4Ee/HHJatSJhdu6kRjCi80ohtGU49RDlOQ1xoH+Mp2X4h8GZfBok3jnJjWhzlLDKCeAxJQm8FctykTOvf4jrUX72w50yPM0SigfzZdHgrNGv3zyEI++qc6KlzTrfLhXETQNFmzM8tKXZoE2LL6U85UpQwza0+nAGp0Q6dB7GMbK/vlanr7Tl8Bmm/fuRfBiI7gIV3/qxjAx 3bomZ7y7 1Fd2O9tLcW8tykMiHxYbogmW2nDKwOZamZAyb6EWQ8swmqAihz8eneen25glrzeVjwN1R17/4+e6kC2aE1mkcwnv3UNwg88DfLA2kbZVF/D3RqZQs4E4Ec6XTCsbnCQHaeylfbW3fnaXoS/ZMVxmo9UrwS7pzYqCK7sJSneP4skCG71C/UdBIwKGl8GWNZQ09h5jm1T1H4+xaBY1MeJ3/E2hZl+kauOckLPDOBG1zDmfMCENltmnGMvRizQqCWexNY2+cdJo//oOrCLhlJ9zqka5ikQ== 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 Acked-by: Dave Airlie Acked-by: Gerd Hoffmann 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 Jun 13 21:42:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697525 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 5E7A0C27C4F for ; Thu, 13 Jun 2024 22:13:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 929366B009B; Thu, 13 Jun 2024 18:13:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D6EE6B009C; Thu, 13 Jun 2024 18:13:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 750DF6B009D; Thu, 13 Jun 2024 18:13:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 551C66B009B for ; Thu, 13 Jun 2024 18:13:45 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id ED66D40300 for ; Thu, 13 Jun 2024 22:13:44 +0000 (UTC) X-FDA: 82227268368.14.8117B31 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf09.hostedemail.com (Postfix) with ESMTP id C6BC6140004 for ; Thu, 13 Jun 2024 22:13:42 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="G2w/6qFG"; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316822; 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=fYNzMo8R9IZza3lof7PJHFdB/0yyaWpys8zRImzF5hA=; b=qtw+XH8otFdp7w14rpCoJ/tOUkAVvS4Iz2eghesCXpfqrqmzzAOwfFASNXxtorOvgLQjFe R4LADgcA22oD1K30peEN9Ff9nG6HnJNFjKCvMcCKnJMMpndELaAculJKFPZQRqLK0mZilO qPF4Rz3Pi7BFtr5ZZRFIkMA9ub/NzA4= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="G2w/6qFG"; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316822; a=rsa-sha256; cv=none; b=Aey3weAUdk57YttPgU9Iz6rMIIl3vAxxKDrpNhuc+/reQ3eXmPZurnosj0a7b44EsoG7WC L6aHXRZWh4hbDF95iyj+x+ymN3YSSyJGG/3OucNf4xkY2jleKV2KhEpwDVH7qZlENjBRmC h8d8qDjnVuLytTFQjXoZ0iTVlynAB58= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316823; x=1749852823; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KWfvKYp6qtQmdJwHokBW0M3ksxOQNq0tWLBPfJBLXlM=; b=G2w/6qFGYbN4ryMaUiTHXxjLw7Ql9XjNbhSeDinOcG8c9mhqVvhVVs/Y oeSMTC6fvHoI7VzWTOPUFzYDQCnfexZZpDTB0tHx3fFf1EcKahmhfGcq+ BUhf5sxGImKy2j5VXWVlmZm+ktp/kaP/tXsD8jKAriGukLQhyzceeQTcz IfN3DFYmfda5Zz4pxWcpJOfZCyPUFGOHeksdVQTmWjfb8p9PulTc/okTM RFcoJUK6L123KPi2i+cFpzY+fGC9mYi+vn9RPIJYwdHGTJhg4dxZ2ol0j kkKup/MFkATrg3pczkUjdgQn0vD7vAhrOFuPufsABjkbmkTDRk8NlKZ/4 A==; X-CSE-ConnectionGUID: 047d/c5sRC+lBJh2wxLXGA== X-CSE-MsgGUID: F6N0p2ZzQvC5StyqrjyWkA== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720557" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720557" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:37 -0700 X-CSE-ConnectionGUID: Iw7WpsIcSCOsE8JejDKvRg== X-CSE-MsgGUID: m6PDUEu0TtWRmlcYtDwzMw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214107" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:37 -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 , Dave Airlie Subject: [PATCH v15 6/9] udmabuf: Add back support for mapping hugetlb pages Date: Thu, 13 Jun 2024 14:42:08 -0700 Message-ID: <20240613214741.1029446-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: C6BC6140004 X-Stat-Signature: bsxmt73s9h5xdbe6hm7d3jwq9e3ip1s7 X-HE-Tag: 1718316822-312958 X-HE-Meta: U2FsdGVkX1+tJcpNfVOcZVIj3yBYlmWSqpjUSAkK1uaHG266XoixiDPeoK7IToghjthUDxs2CdessTZQrStKOT9q6a2WbwyrWZqds85L9/VO4MTdSw/lp4pO5hhNANZphbTaLgLB6udbCx9f7LIIHSGjWItck6Z6GGqzAtNMyOlEGlRgeoCUtuwj/Gm2H4Gg68hqstg1QgXH8CawZkskrx7aT355n5xHvszx6yX9xnTfwRWSKruOonNRywkuJMSFy+tGo4vZbweHgawwmvnRNyi7MlRxcRvufJv2Z5R6+bNbod8RB9mEbe73IyHNZCp0CY/qFWsgA4XQkQ4VP5dr4WqwUnJNr1Q75ddpWi+/8kGKieufnVGoDD0Et/ulmjaXXO6t/dAzOlAMIZ1EHjUmqUpo7DH2lbrhOo9GqTie6AU/fejXPdugeAvk+rTMal7+M03n2NDYLH9QmGUtFVBpb0pcAcgycsnuMZJGXU/wxqkhQO4yxur+aGCuX0XdVo7kmwEMSpo3uryRSYbXjJH4+8DYJJeF5aW0tkSZYiFpRVEuixxgJKzft92Pd375zvyekz7H7OlunXrocdqPjnXUlp66fNBkMAL6uSYW0C60oNppd6uNxXXoE7AcOIr6ffFj+0Y/syzZPT54wPKakut31ZJYmftHnghjdy9RkgUusTNzsrNgk8pAFO1MP4LL6NwVXBzqXI5AuwXw5fvArY0RKSymzj7NNXuG6rZidjK1pbhsFD+83EopGWmrquX1dct3JNjOgmO1dNugFD26LCqFXK6nMiTEvWUNi6atD/5h1S2QnmpQkfh44Zo7xHNXpZqFce+oxPtdwQ/OwEFMUHOYbdYwcVJc6vStPUrPB7bIommCDdp95BqA8VuaZlM+9DB4BiRw0I3FNrit4vMbe4ia6+bIp0RoZ5EvUenNoVgEEEVQmr7Uo8jV+bwDqixZJnXhes6Rgs/vtWj9yLrlNyF FuoB4jy4 3cip6aAl9a/NFmYTcoatNbkdDE8J617rvLYhDkPE280pCmpfHzVzNMTw+8oG3/f5/Hj7hRlUEsk/NUKU35j/RaJ9wiHELB/LhRglVjgtJtC83GY55pp39CsHJ84BboceLREV90YhX71syaslWxGmQLmGHEWObiujTcrvmdcZAvZ8QjrqccHMANKT/7tDeUEXBq11rx1GLYqiObz9McNaeWf4jPB/2bd8WF5UySs9TvhRNmZId99dsxG+afg== 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) Acked-by: Dave Airlie Acked-by: Gerd Hoffmann 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 Jun 13 21:42:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697526 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 6A960C27C4F for ; Thu, 13 Jun 2024 22:13:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 111216B009D; Thu, 13 Jun 2024 18:13:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C3136B00A0; Thu, 13 Jun 2024 18:13:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0A516B009F; Thu, 13 Jun 2024 18:13:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BD1246B009C for ; Thu, 13 Jun 2024 18:13:45 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 40619160345 for ; Thu, 13 Jun 2024 22:13:45 +0000 (UTC) X-FDA: 82227268410.21.F7A866B Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf03.hostedemail.com (Postfix) with ESMTP id 2BF7220014 for ; Thu, 13 Jun 2024 22:13:43 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kRpAoKjL; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316822; 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=zjBBzWTJEvTtFfv4IWPpVd4e6eP0fgXlLOTDqKvQBc0=; b=Hu70XYOyprmt2jruVDGiNqUGvRaXUZBCPNq+Ox61gpdBSXhE5ViQhs+sYrJCMONBoh5bDA et+nPwTKGosioDAXnmJILfEuiv3LcGtylGETNi7frEhp7JPntV1dxzBLx64YZMiU7mdgCG T8ORK5umFtGFxVtOt0XrkVaWsozryqc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kRpAoKjL; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316822; a=rsa-sha256; cv=none; b=5nlvi4w/ETyogMU8QDFllf8uT1bUwPIEIZIcXsGMB/mpm0D9paYhxgmtvalo2C2Fofxkca Jel8+xgiN9Oy+0aEmR5T2pVmV75Bb0hjsDlcbs8WreDwyfFHhUIUPtqTqY+BsQDiFqrDJG xXnse0UnvZ0Sch9uhFMgwHoBWBR9u9g= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316823; x=1749852823; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cAHfonYtnIV65GKLrtLc1wEw5/ZdeM3gwFaTcvhYa/c=; b=kRpAoKjL27VQUbx3uZcvM58X3cnOO/guSED1n1Gm8NxVSXY2mP3E2nms QegmgnkKJQaqlSl1NLkDk9YyuHQhdW4ZGwu3JZSTEj4GZJuqSnCs01eeE hbtIlgj1qR1b+v4qMQ3ocq0Ar1qvPPiSRsur+WL5scHKyqVyTOHpTJPL4 ELXgldhuEqeuGqmhii+RnZwalNDNonb9jIpv0yvdHDd5+1qOMYBnEqvFy aCe7baAOmwLdQYWmBYxzBorFnp5EKXhXzBnp7OohsukUJ+YOqAQsanmfg O9YDPuGiRSEigfMbZZIYu+zkJvEG9xusiMwqAJEwUqAhgZsML+hTfKcI1 Q==; X-CSE-ConnectionGUID: fADmq/ehQcGiH1bjXRXvcQ== X-CSE-MsgGUID: luL+HRSQTLmkND9lviHGjQ== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720565" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720565" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:37 -0700 X-CSE-ConnectionGUID: D5RgXkveQCmos9L7eo6hiw== X-CSE-MsgGUID: TjL6YXY4RUmWSLe+Oa+8mA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214111" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:37 -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 , Dave Airlie Subject: [PATCH v15 7/9] udmabuf: Convert udmabuf driver to use folios Date: Thu, 13 Jun 2024 14:42:09 -0700 Message-ID: <20240613214741.1029446-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 2BF7220014 X-Stat-Signature: 8h9cujrs84oam3t68dw8twhwk8pcmd7n X-HE-Tag: 1718316823-527762 X-HE-Meta: U2FsdGVkX1/bkVJtuF/9mOJDCy72ZWs/FE4DVYclPLLnE5zkbAeK4/NY2Fm5BVpQv1vYOHBnzopB3UrAfrDOj3meuo8CzbRC4gHzeqv6cQnNvy60Hdbc1qOkEsb2Jz5x3T2xyCj7OncG2v92Wetv0QdpiC4+HuTjNn6jzQYZnODPcGGkS7CoJFGKryHmQluiZRvkKPdDCtPUeAs19aS48bswsNq1qVutugiXHJUaDaYOObq60YMGqllA/5IcOZUynR3hBydTcjkFRJzBmrC8fz4XZq/TWs3SshdKjrWpo8OPprbpybTPD87Hd2r/yDDP6zJGMuaMLjNka8nwZsnDjRIbGTs+NOjuVfy9V212A5fBrOAXEmLFOPjJcrq13If0lMPRLEC9/iv+HgC06x1Yw/lKiH9mSwo8E8FGpYTettpdAXVphx53Ilw/yz3qa+SDCSKLoyKi4LMZlsFAn6r8j7EzDF4F8QCwWKE+uE70LLM8cbkX8vdrN7i5q85NJrRndL/kugkgNzBdAIXpNWacPXa21aR92sW8GdqAvB8i8Qfg6ctjrJSDIEN6MPaHnVRT+vwRQTJmNawqGlQTRtrIakkKo54teN8+SqrGvmIpATngvmLUplA4zDRm0BGLC0FbemiGNR6EviBxVqllRuBu0ZEhabVgeIcdhJU5zk86wzPuaI96ExZ7wUgAYK9xVo95c4/jPQh6lPvu6F5K7mFM6iFuJLl5ZMV42A40pMKfJW5YZXGH21C1IRAqxPljeokeIz9uXezV2droGOIXmWlKNwDvhevUZhuSfWKmQ24Rbba+4Om1sG36QjITFUgNQfqB587ge+bGM/mpjgeElqEvzpLDUWPLxqhhWR9eiDs24AfJSJjT4y4fVTNCp+IcUG0O0sWxmHvQ6bSAStEhAO9nFgOpjso2rxewgslIyY9nuVaZfsCoABArDu2fYqe9xQ4WbKH6GMctJbCMkOuBsoz 4gj8qrnm qw5SFoEBe1O36a4L/PwFg8X/GdYeU1yoCr895MO8REcVhybbTUhCgicEf4ai+DGCZfcvSGsuPwJZol64lpmIZi33uw+NvoR8idUPZSCC1BscdY77ByrRqvGB/g2dhl8v1STpfqRst0oUYG6Avr4U32Zg9IftjD3bHeUQ1hlGNKM4SokvjxoVBYOJl8EmVA5VKYxBpGX540Iwk+HD6bEZDrH1Oe0r92e5a5YpeTXNskZHoi2ELKg0yu5rVsjIhJ0geIC+Aa1rnjZ140ApqoitUeeSf1xMytMp/ZxDO 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 Acked-by: Dave Airlie Acked-by: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 139 +++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 56 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 274defd3fa3e..e67515808ed3 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; @@ -344,30 +381,20 @@ static long udmabuf_create(struct miscdevice *device, 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 Jun 13 21:42:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697527 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 64FDEC27C75 for ; Thu, 13 Jun 2024 22:14:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CEC96B009C; Thu, 13 Jun 2024 18:13:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 531746B009F; Thu, 13 Jun 2024 18:13:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3590C6B00A1; Thu, 13 Jun 2024 18:13:46 -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 07ACC6B009C for ; Thu, 13 Jun 2024 18:13:46 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B8030A02C9 for ; Thu, 13 Jun 2024 22:13:45 +0000 (UTC) X-FDA: 82227268410.15.3B8ECF8 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf16.hostedemail.com (Postfix) with ESMTP id 7258A180003 for ; Thu, 13 Jun 2024 22:13:43 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=dr8QDi4H; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718316822; a=rsa-sha256; cv=none; b=HV93WyiKZjvVX2/c9DhrQHE7p6OkbUdHxPzXqORi6FUHxGb85NtP1YhiaVv2j/BOn8T6TH hX2rUZiInKJ3+TodEzGTFvSqWBNH6dXi/NlNO9oPgEWIbCYTNPLULpfEPhe+r2tm2UAhjj miMgX1JKs4YBm/x22yy5XR7QzpQNZ0I= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=dr8QDi4H; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718316822; 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=e2ZvWo51t2OTB9zRJu4TglOkpkiWkEyG9BmKUgtRmOA=; b=5tj+P6fCZQswxgGXR0Vo1oIh9n1LZARrh3GrNT6e3F27cdthaDLA9skZY5wi/2JUmCNpbb LfAi0oQ46WuOOzUYkbcvSjFPMg//9z01x5tcnbasqSLgSeJQupayLQsB8SLe3WWj8kyTxc raEYGkzMgoZbFkcWNyxQzoxzSTm2AbI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316824; x=1749852824; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tt6lFLF/Si06sd9m8G4bs00brFbAJk62FJizVRolUmA=; b=dr8QDi4H5DYZqE7qHaJDb0TYefmyQkgwxOTanumvXvR0c7a9ND02mzcd WykxInnOixwBwDuAGzGPlod8b/BZLGU+f+7/X1D0nOoWrWd8SMwJuQbUH v5a1YVNS7pQI+/Z+9FTnQDIA0Zkquul7BBbcyHXaixv0LL5phVkNCKoW1 PhnqgZb+FKb4eFPsfsvbJ9clhuN0DTBQ6xm/iUK0jV3hxYXYNAYhBt09Q O5pelCuecD6LrfjwPQC5EES6nYwDFcIC2nENDF67TvQbCs1L7UIUdfiL5 Hnm1s3kLKt9RZbJY/EYu7TtdYaTE0/BJPPRmouiyafdgVrhUu40npVWJw g==; X-CSE-ConnectionGUID: /VTB1IQKQRuIkJSuKQ4juQ== X-CSE-MsgGUID: LcVXVJQaTLe0iMMDPH1xag== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720572" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720572" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:38 -0700 X-CSE-ConnectionGUID: xrU9K2iCT16aHJMZi0oBxQ== X-CSE-MsgGUID: mj1NBO4RQICH4KdWcZDYNw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214114" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:37 -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 , Dave Airlie Subject: [PATCH v15 8/9] udmabuf: Pin the pages using memfd_pin_folios() API Date: Thu, 13 Jun 2024 14:42:10 -0700 Message-ID: <20240613214741.1029446-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7258A180003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: u16nyc6c5n939ckkpe7scgo8ftjgmmtb X-HE-Tag: 1718316823-597059 X-HE-Meta: U2FsdGVkX1+xaRLUjQN7PcWWofvk8NGi5sQoht+3khg3+fSp5zR96ZaroQgkGp+1fRpZTi/jtalLDM8ZCDeaGltthmh2syuuLtok3YnYw7awIeQ4Xhyiw3LhCw9eOz1CqA5VWCUmKWHA/V3na5JLxZGmSx8GpeYuaP+qqjhC2KnPD1aLk9xuqXo2Mstr9xvoKXQP+O0eQSn+F2MdzLI7Cufr9YUriK2rJivYC74lBiJ3l5W922+MzTweDidvbf5mxLMHmuMfpmwfBYH6xKdv2JuhsJVr2tlyvtRJ41W4qmMx7PyDQLBMGawb+BCgOun/EIK3r38G3etY75qAxvW5E2+MzDdZFucq9qaWOTla+nbniLhSsINcJ9G3SmyaXghavDVE+RqFVWkCPh3mwr5o6mCXK/rKgGy3dtQjWelzLkgl3+ejNiB4U3bA21xrEaR6TM+8I/oxzhGnsEpfnuLbjwO587/OmXBof7dAOLhv3bTh2boFrZnBF742d6nStKL1aCwLYR8mxHYQl1FdLBhPvgjjPLFb5LTac5t0fpsUbkM5N5eaMgoyAnzZYB+d1Jzr6NuHEm/V/LkzSLNNTxtz3Ep2gRsjzGoltQAi8w+M365uWnYz5JX2rhwlmVS0cVdmp8g0UiEBLoAw/ZxzJVudc1TC4RQDSB2hVth4nbuxAWYefotC+ubtaQPUErrbCgK4HXPvc1ee+rJZUJ6b1tjj0vBNx6DLHyYbMbgv9VHTTa8JhVD+OsveQA+lhyyhQVVYqaOuLkoLwRzg/Homr/OU0EHldi2ZQ6T2IiNIOwXND9Syz9ikBfgPGZX3Yd1Z1GrFNbz8eVYE01XbGOZS5A5fBSfOSGRPyfJIExKzGiKmBcrUIsEgcijF2Fmx+9GpB9TZjuTWtwGYy1jyfWpWmmFK0c3SuOzEr5FLwOvggnyXzpEsVR4RIS+7Dd/3wDx6gt7evOcCaM87radYA7jzkVc dDySfdYx 6mPS9ojUgTBmCpQ2NQJAwTjpB/k57ZUle4vpVwhfuDZ0S+E8AB8waPB6TpecJS+KVwjJ84qm/oatV7YhIBglWmorzfdUiKbkPGKEpZzBs7jyTe3Hf/tbErYYZ+oQnJFetOwyR96l9iEK4nJCcFNApyd7wrdM1EUumc03qITon9A51w+HwWtz29igNXu7Go4z0opjj3O6pU/7asg/tSv3KBjxSEgQQvqhH+PCct8Yw3q/UBFU9jMKqvx114BPmrSxQXIRutr0OOG1HJgfY65zvEo7cKPJF4r9j+CCoJ6l3aYSKebiy6lcETWgLEz+P79XhH3YzjWZhtuAgkcs60VaN+rrHIg== 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 Acked-by: Dave Airlie Acked-by: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 155 ++++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 75 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index e67515808ed3..047c3cd2ceff 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,46 @@ 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); + if (!ret) + ret = -EINVAL; + 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); memfd = NULL; } @@ -389,10 +395,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 Jun 13 21:42:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697528 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 76DFCC27C4F for ; Thu, 13 Jun 2024 22:14:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6E3D6B0089; Thu, 13 Jun 2024 18:13:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD29E6B00A0; Thu, 13 Jun 2024 18:13:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD49D6B00A1; Thu, 13 Jun 2024 18:13:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 864866B0089 for ; Thu, 13 Jun 2024 18:13:47 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 42E14A0F08 for ; Thu, 13 Jun 2024 22:13:47 +0000 (UTC) X-FDA: 82227268494.30.5E52268 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf09.hostedemail.com (Postfix) with ESMTP id 128BC14000A for ; Thu, 13 Jun 2024 22:13:44 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IIW4kSCF; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316824; 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=K6deuOdligGOWcs/cUpiN5v80J3yBU/FgM9oryyRYQU=; b=l3OHGJs5AWIS/uJLP6K5NghtjRqtih/3uvh/rFDT62c+KrWkm8Qfdut7IbJIm7DLeqslOC EthL0rUwfP8bnJ0kNeFw7SJzCHFVpCk77Wnl4owxVARJyGiqSq3ROeadtchppoWKjc8viI VjQChdY1goF75cHdLNKLkW+0WTE16NU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IIW4kSCF; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 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=1718316824; a=rsa-sha256; cv=none; b=hoRDKxl+IBQ4xjWB78bvLcI1TF769np+xwF4HieNKLvXvbDb7a2/5ktcBVBmTd+cfaYADz 0xl2NdjlTC3stFksYlgEpqaEmeFscHuOZuIt/sKdXWnbjavYXcZS8m2WL0yTZLCh5COQV1 EWY1Zf0jj9qNFOBg/vlBes94cw5Q7s4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316825; x=1749852825; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mwX/Ut3liG+2eL4EiqMYYCOWpRlKdeioXbS3wyxFBs0=; b=IIW4kSCFeNTWoKXneYzIMZpIFJWmdTKuq5xNv12jSJkb8gDpENWFEMRf MUDn1wGhV8tW8lK2jYZywmJU3SJ9YtG3Ix2V3U9AGdjvfpLgWJlRuCX40 8Vgs2uZKcC2AAHah0triaKI48huQ4pWvwuzQavIMOCi0MNPHXS9t4szn+ EEBZ2ReNFsmVKcl6e2gg3b80GPKlWCtA6P6l1avJyzzHCxmJHntvJcuTc UOCSl1yVNU4XEnvBbdNCC/cezMiUCOqACUy4tdXinKJ2/6Rudq9pkRINn qUK+aWOXu1bQ5vFE6pCRbGx3PQql8DXM4AnSp9eNy3L3c7ntFbroAuw9O Q==; X-CSE-ConnectionGUID: VZFZ9cTYRAyjDvmtrHt2Wg== X-CSE-MsgGUID: 7G2dAORPThO5vg/l74Znlw== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720580" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720580" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:38 -0700 X-CSE-ConnectionGUID: Tc0wLCpTSm23vyp5b7zatw== X-CSE-MsgGUID: rE40TlMOSMimxsMr6bf50g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214117" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:38 -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 , Dave Airlie Subject: [PATCH v15 9/9] selftests/udmabuf: Add tests to verify data after page migration Date: Thu, 13 Jun 2024 14:42:11 -0700 Message-ID: <20240613214741.1029446-10-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 128BC14000A X-Stat-Signature: xbd9gten59dopywt5ugjr7noeunfu6of X-HE-Tag: 1718316824-584786 X-HE-Meta: U2FsdGVkX19CsY7Zdy3GS+2WkiWk+PGdCxJvKdYOFRVP6oS38yHyXETfVN59CMRXeC1ldlCiNF8NeklFQpCqZHPKefVjFaV6W4QCq/02IdqDMv31pbAbA95f7rundfz9AbS6IN9POqsgOVT2lmh3MQlUN/676IdbjD2DO4rRlOLHKmKt2C8yoNDa3+1Ag0H7L/XDEqPkjj5jqlALcTYh3Pd1wrsniPGCj79LmyNkuXOBo16Y82PZcEYF0m7aVYgCu/zdiwBGHn5CgvzQrT19qbnnEYMEryPL2k+TryC03ZoaNDSAbyw20BXYE77MCcQ5cMap+soFS+F46NOK9Nf0eohJXmImlrdk9ucqqb174nqoObhva5Q5ia/MX7SXrAxpPdBH1u3ZZqUWKVg7lW94MlsNLVOcpfVMdEHU4Z0CkZR6fcUFJVTUv5FkToUsSXAahGQWmWqR2rjtT26TShYx/HSEaebHsjuzMsgWlNhViJ6CYI8ko/PyXZHMdhadbpzWAMJm8KhtPespnijThUcU8zgCp9rtGLNbRKtjZCr+NaVNchXPeT7s6F2/w9fqxrH6kL7KDqaJ4wfTZp1i0NpixblUfXXhzqYRRBK7SMrARHxKxMm//0vm1yPcACciMbAZqCjbr9H5YOT4o4mseVOpvRaZdEfR8NeOipK+a5XkWk7pud4c8R0eifZFa8CfFDk2GtoTWAplIQYbBr4lnoIuDTpJVqKeGRNI24KzeEGZZhbR/uV8GJY5dGSR9cZOXLPK6u1hfAjf5AGcRvosDLNhRtRUvP9xHO4oky/3tSYPUvgEbXQLvJ7v2gLCvWu7Trs4i8YF28okRtKQ6ZqMQ5RGeYqpP21gQh/WSr64YmOLx1kZ0SKHaCAenPAcokE/35RgrlC6+2VoC1/kg7I8n7nQ/65yT14cxh7DzUxcrqbVCkaLZlibRJfVy+KmKuiR4BsTk6BWnXvEO5ZsSxbCiRT kPE8mZDt V6c4D96EylEuTSE83IPoQVtcAHH56yZA2VQvnUzuxI6EnPysaNvGnf4WD9nV1j6/K9NLX5uO3gQSZ8VNBx7dwxGT2fYwcpEtAtCR5DarlgplFyM0PdwLzSNT5KrqElqzra3XsfY4kWo6D8xNBBwRf2W4Dfv3/ktNdfmpZIGqUQxG8mzk3zjhHiQNF9s4dZO9rdOoHOBSlIkIb1/3zVb8J0y+9McdHV55oKe4cd84S9B49Ot8BAeme8LC+W+moLfyOUYHiCIdG64RXii+vIJEQCkWfrqUs+Qvo8uxLLQR2sRqa6tKTwhm/SCVXUL+JBWcxXuTONKdHlzWrrEoHAqSu6LCCoFFy7CAj0QaFNYcuZluwjiEfZoRN70wSbhQwN5BmEInHLZa8AJL75HU= 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 Acked-by: Dave Airlie Acked-by: Gerd Hoffmann 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; }