From patchwork Thu Apr 4 07:26: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: 13617391 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 6F2C8CD129A for ; Thu, 4 Apr 2024 07:54:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 265EB6B0088; Thu, 4 Apr 2024 03:54:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 23C956B0089; Thu, 4 Apr 2024 03:54:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DD206B008A; Thu, 4 Apr 2024 03:54:35 -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 E4A356B0088 for ; Thu, 4 Apr 2024 03:54:34 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9C0971C1207 for ; Thu, 4 Apr 2024 07:54:34 +0000 (UTC) X-FDA: 81971087268.09.C875433 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf25.hostedemail.com (Postfix) with ESMTP id 7CF79A0003 for ; Thu, 4 Apr 2024 07:54:30 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=V7W8pv+i; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 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=1712217271; 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=spsxjLz9xHahODsx9a+NCs7hmIV3g28U1Tck8A2bWsg=; b=WYqNuXGJr2FGw+v6bjKEo5oNI9MbnZmSYweH/Vsw1wY50XSKWkt+Jw8vIrqFzINgXWtzXf /vtbsQT7AqR4sAJ4NYjUoJl2uNWB0Gm3DaVKdhOPOZp5aEwXi9Ib9mIerG8f1Tx5uAXSIU c/7fI+CS/sf9rrhyieXjepHWoMjWFq4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217271; a=rsa-sha256; cv=none; b=JQsLeH/sygnuYj+6eheS84cqjPUz+G3C+bDMWWYA0JQjgOPOf6IwS6NjGjH167nb1UpKSt Lu3K1pI5Y4L2eLfjpAr8+SLkruq9w8R3izLz+8Ry9hq2R3X0DpBxHPVlQjhv8LRy2vZ8+j 5xmnm36lFLH1z17tZ4zOCNonKMpXQO0= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=V7W8pv+i; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217270; x=1743753270; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PGtmtG3VhMVfYs+DXUcts3PKnWJf19JlGtMJ+EYjTaE=; b=V7W8pv+iLbopJkiC94kX1YoscQk2qGEmgJ1IU17+OzaYzT0Tad5UJGm+ b9tXKRDLtX2ha8SfzbPPvDwaPk63V2OCO6kSWhfcXeEnm9Fh6EG7OX961 N5EEc/uYoRWXQ9m2DWiq2DAYDjr43vqKotfgT3aauT+tqD83sYKQJCi1P bm7yejLMEAvu5LpSeqeMSjM68fVsTNdkv5FiL1vxiuzzTKGbYPyuY0Ugf Rm5yLoSvapHQbJSwL/J/ebV2jke9Xesi1PeDNQhg3uI3H7NzB94/FINTF n5UnNmTSghwFXHv55nQGj3yQpR5ZGjF261Jp8yMe/dGLbNpiwdvJEXDfu A==; X-CSE-ConnectionGUID: Saqp0m9iRm+scebZz9LZ9A== X-CSE-MsgGUID: uJW/GLvzToapkJR4+jTMSQ== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450776" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450776" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:26 -0700 X-CSE-ConnectionGUID: nPEUM93hQRKwVvp9iZMtuQ== X-CSE-MsgGUID: Y6t8X28USxKSDeB5/WokOQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298756" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:27 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Jason Gunthorpe , Peter Xu Subject: [PATCH v13 1/8] mm/gup: Introduce unpin_folio/unpin_folios helpers Date: Thu, 4 Apr 2024 00:26:08 -0700 Message-ID: <20240404073053.3073706-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Stat-Signature: 4spafndifmqwwaykfc8mhta1tbph8qjj X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 7CF79A0003 X-Rspam-User: X-HE-Tag: 1712217270-727647 X-HE-Meta: U2FsdGVkX19wDusa3JNq034x0X+vbkoL6uGEA5mWlLg6D/QEjynZtcRIrxb9xwvhoCfajKaFDtxaoprNtyOVZHmDL1pZ9vghrS3RO3a5KiAPPUkIoWhTlvRX2d7ujBZcP7CrNZEGPr+gP8eeStudEFGRGbJs/lk0GLXcqwSdFUi5DJ/ZUoXQ4njA7reRz7NYm9WQkSpSJRfR6wr0vSYoZFhu3apGoBLHNk+IkRmYV2e/EhXX7bvn62AfvTYsYnSZuDFOypNl6MxgHSA+A0pUuiMo6IJQYDm53EdjQkz7/jvbuBBhsdRVPAEpG+eQrncK35cl+Gb6Sy6RKnFSfdVqedlQZISq4H3C8e+3jtw+qaKa6diAdgIrwYytxNrM5XThVZaFPJbNxu+p4RhoCIND3+cDnGsrzpb/Uvbc0Zx8L5xReD6iTIqcgvyu4I0tmm5GmMNVmWHIvKRBToA7Nj5GvddOsZmoIzVMjU01KEwcfiBcqCqdX/FHbZfQfn8NEq4IHdezzfe4rE0A1GJgPUHKch8AtXZbcZ+ROMONukljCjNPB2TcqzRTGUE+e/NfSj7qTPJgtdhP6ml1eB4bWEI4vcu+Mz/rl2rFMWNHgg7MsgPQ+GQJ5biP6lqQwxILkds+G5MUAy1sN6OG2QjYRuV6WdwT1TTa4VRqzhIdj3Qjo8NstonSn06KwbKjQYfolGIpw2nss5FlnejgeDPUDMcKCP8jExh6TQDWcVG9bYiWhAPQB4Uj6YkZF4U1i7eaCFMfxY12+tK6RxtlTNJbHVOakFC4FN2vq7fpY8CEVDZr9gwxvw+6HncBLlcNaiiWdaV6jK5nO9wtAomZ3AJ1yetsobXhtGdDDcpbTSaOe4RzrsmMdX5ZgF/OS94VL/X9EoKl+Ldt70DykiQGUJe7r/mzW6qlUbiOZk66jmQNVkGKaiSUjQ938gWD44Q9ob4aGM3Ns+LYA2abteAsStzbVNc 8/v4J/oM k5F2OaealymRAqI+WPKdiuDY1D2/ZHcG+9dY7La58ueLwpqI7mr3t9JJbvBLz9j8uY8ZnbRJ9mFvi5cKjhp9wzJnjYqITHtoFWQO2duj8Sn7FUC6fOZIeVg3q775MaakQkGMrUX1cxoa0FXi5sZ+h5m3kb3SDskWwldOYGVpoZrXdsVMcbVlO/Yu0mxvYhGbXXihAppZN7DAEPpO4aTdqEY5YREdooac49kZI/3xov/hrOGDSmCiU2baHSg+bI0fwWW2hULU2rZLAYJy4B3pKLkHp/frPC7SG86RXGdb+8mCNYz9ZQ0MMoqxZ+g== 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 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 bc0cd34a8042..f2ffd54e1736 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1614,11 +1614,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 95bd9d4b7cfb..9cf2adfa4ce5 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -276,6 +276,19 @@ void unpin_user_page(struct page *page) } EXPORT_SYMBOL(unpin_user_page); +/** + * unpin_folio() - release a dma-pinned folio + * @folio: pointer to folio to be released + * + * Folios that were pinned via memfd_pin_folios() or other similar routines + * must be released either using unpin_folio() or unpin_folios(). + */ +void unpin_folio(struct folio *folio) +{ + gup_put_folio(folio, 1, FOLL_PIN); +} +EXPORT_SYMBOL_GPL(unpin_folio); + /** * folio_add_pin - Try to get an additional pin on a pinned folio * @folio: The folio to be pinned @@ -488,6 +501,40 @@ void unpin_user_pages(struct page **pages, unsigned long npages) } EXPORT_SYMBOL(unpin_user_pages); +/** + * unpin_folios() - release an array of gup-pinned folios. + * @folios: array of folios to be marked dirty and released. + * @nfolios: number of folios in the @folios array. + * + * For each folio in the @folios array, release the folio using gup_put_folio. + * + * Please see the unpin_folio() documentation for details. + */ +void unpin_folios(struct folio **folios, unsigned long nfolios) +{ + unsigned long i = 0, j; + + /* + * If this WARN_ON() fires, then the system *might* be leaking folios + * (by leaving them pinned), but probably not. More likely, gup/pup + * returned a hard -ERRNO error to the caller, who erroneously passed + * it here. + */ + if (WARN_ON(IS_ERR_VALUE(nfolios))) + return; + + while (i < nfolios) { + for (j = i + 1; j < nfolios; j++) + if (folios[i] != folios[j]) + break; + + if (folios[i]) + gup_put_folio(folios[i], j - i, FOLL_PIN); + i = j; + } +} +EXPORT_SYMBOL_GPL(unpin_folios); + /* * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's * lifecycle. Avoid setting the bit unless necessary, or it might cause write From patchwork Thu Apr 4 07:26: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: 13617392 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 B3A18CD1284 for ; Thu, 4 Apr 2024 07:54:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3EBCD6B008A; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 39CD46B008C; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17AFB6B0092; Thu, 4 Apr 2024 03:54:37 -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 EC4436B008A for ; Thu, 4 Apr 2024 03:54:36 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AF7881C1150 for ; Thu, 4 Apr 2024 07:54:36 +0000 (UTC) X-FDA: 81971087352.27.67F0B6E Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf16.hostedemail.com (Postfix) with ESMTP id 6E44C18000E for ; Thu, 4 Apr 2024 07:54:34 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PoLeD+6G; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 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=1712217274; 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=Fr6hftSb77xo8ynJlx4hz/8WpMiNeovf6F5pnqyo6/w=; b=eZ0fckceHxqcuqU9yTbk1LLxeM37ePSsy3M7JM/ADoWPDFt9Gk8OGCF+UyhkD4y8YOiGIH /vre737SpiEP5btz1hBhSjhIAdG/78oTqEtLnMpl1X0w0R5G0UX8ECmdgcjytSx1E3hDi+ y4hV4r5hu9CoZRnb43b1pxjOpwJzDfU= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PoLeD+6G; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217274; a=rsa-sha256; cv=none; b=X2b08j6AEdNSPfBwkg22EQRHd78oT9xkN8h7+2HO1vtvmamcolzS3uHOR+CVUQLIP7WRvb S+OFV7Nd++CcyS4wiY7Kq/SupxQ1unYvAXS9a5vrmtoAQBqg0mSgXXgfXg9+n5kSLFVt7Y Sg4DwkntRngDq4BreyjxCeJl2gR4b44= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217274; x=1743753274; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9qpUkYyGQ6L0Mu+KSteX8nLnFmHei1juZzZ9jluV9vI=; b=PoLeD+6GqC4qb6M1oeGX8DMwJ52qCg0VJp9QpnVfUhAwfdNOBY+Chi00 NoivQgfhAq7W9T2LsOW7GKkGA8nHUX5kN24Chkx2ivKM3wQM4RtbO6yhM bV0wJBAruYRvfatpqHVJlmheMYjKuSjbzetyXhIVr3oI9KLRk3qbyLmVr 0A1LBzxEdThnEkCmiksaaAiWCJsB72lF3WK4RYQnbrCb0pPhht1/AFnLu n3Pt+NjmDjjBAxTS66BiQP/jl34SeVB38GVDI81voEsoFB72R8uTd/WXn mUe2qkUUERaPVYnj8nYF2aQyDhjLGgMsMbOahok1BTHBzOKV8fRj2/YLz Q==; X-CSE-ConnectionGUID: zizxumJ/QJSM9gFjTUBiBQ== X-CSE-MsgGUID: Imfddzv6TIewStYULWQbBQ== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450782" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450782" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:27 -0700 X-CSE-ConnectionGUID: 4trDXvagRGyRG4Ql3bqG2Q== X-CSE-MsgGUID: yv8NIvqbSyySHFMUE5nu2A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298759" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:27 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Jason Gunthorpe , Peter Xu Subject: [PATCH v13 2/8] mm/gup: Introduce check_and_migrate_movable_folios() Date: Thu, 4 Apr 2024 00:26:09 -0700 Message-ID: <20240404073053.3073706-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6E44C18000E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: xh4ddc5d1p9n4u439frwnp1xnqfbkutf X-HE-Tag: 1712217274-791646 X-HE-Meta: U2FsdGVkX18Na/WOXKEa3XsJd5ASPhOa/1CQkamOOpNd50oeOyzu+Wpta7ZEsRHuEr1e8pwUTncNekEz7uXJAIQZGsyrm4xwV6T1hBCCfD+/iL4YXXRaALlLi3QnnvqqlyyiXNgXdE/dhs5Va73FpKH1VbVtbr2aLx9NeXYlBNCgeuWUHTMVVOAYdLiO4odhBeYqUekhGiBsENGzYvo4YWUyrCvfVQKliYERLVzbc6YsN2gJsOm/FHwb813RZljHOK8rmqgaMzm7YRYwiXOf5q73r5vipAV4/W1PgtrPuk8xtbLu8gUScID0b3LLc/gTAo/V3nhbfS6LdncwG5XDTPpFEFmf1EavMd7nv61hsXTVz6hibmD+jz3MH3Vz7cpIakuVyJSSu09EZ0UQiDbOVT3lAEQE0Ts89NT6ftYu+2vvPbCfalosISEqtWLBR7JGeLof5qxi5bZNdH4XKB7PPfOS15rccBMJUymVktZiBvf47r1rwdsDQJyG2GMo4vcFdFLNMlKzjmphH1f8NihUuTz6WwpxtfIAVQ+1MVDPdjY/8t9Ttw97CQqEjKrNVVYMXTII3HpEIqdbdgErdUVwuwYQQ4pGuhPxnrE4uEUdG951s9qyVG92frYtDNJheC8suQm9KD92YO7BF7i189nQIpzLuWoegqh2BW9kWDLFDyLNxGuc6mt2iSuD+cSiBXx5Xhhgx1tqBg1ghyR/pS/QBvZ7DO0WhKs0DrFucFxS8VXuSSUxzX+3h+MU/Lly4nY2GpjTVPabr1DZNsdbDBNeH2wD147Tk6Vtu5Jm355JF6stijCIk7J1dwTW2x3SkMrTiLQcBnHPQDdO8hb5eSX1CFA5kRGUGkKYDY4mXrezowzwObKNr3F4iZxou0v7n0Ha+m8yPfLTayiEzdfFFTMpb0KBJg1mrgx/unkCt3v5KeE1mIHSYb6429VWv+Rh3fEttn4UaK/EpV1bjHWgnRR I2NExv9l MLZyMpDPhOyd9E6tFtUQif/4tivLtuVhZWIxT3qwDoKjcT0LSpOiAJPla6SdVTaO1JQwAnziS/sl/oq6qY+jK3H/AV+N3L9m0uew+1qGA/WA0rPBis8vxA4wY6quEZFhKVBm1Us6vpwzlTROpVG8faeGrZckskI2+KxK7dFCcJohAZjgYkiWAr9+AXttCdnb/2EZvp52ZRmoHFeJ6PovL41ZfltwTu3ZXlQkODioN2SrdO9P8N4Wafdw0E0BdqOFW5PPHgP7+Qqjo3uE/nzj9EQV7cH08dWwQ1CZHkVBp917etP9wOKWjjjRK3A== 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 Signed-off-by: Vivek Kasireddy Acked-by: David Hildenbrand --- mm/gup.c | 122 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 41 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 9cf2adfa4ce5..00ee3b987307 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2416,19 +2416,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; @@ -2443,7 +2443,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; } @@ -2455,7 +2455,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)); @@ -2465,27 +2465,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); @@ -2498,24 +2499,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; @@ -2523,19 +2524,48 @@ 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 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 folios in the range are not allowed to be pinned, then this routine + * will migrate those folios away, unpin all the folios in the range and return + * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then + * call this routine again. + * + * If an error other than -EAGAIN occurs, this indicates a migration failure. + * The caller should give up, and propagate the error back up the call stack. + * + * If everything is OK and all folios in the range are allowed to be pinned, + * then this routine leaves all folios pinned and returns zero for success. + */ +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) +{ + unsigned long collected; + LIST_HEAD(movable_folio_list); + + collected = collect_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); + if (!collected) + return 0; + + return migrate_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); +} + /* * 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 @@ -2555,16 +2585,20 @@ static int migrate_longterm_unpinnable_pages( static long check_and_migrate_movable_pages(unsigned long nr_pages, struct page **pages) { - unsigned long collected; - LIST_HEAD(movable_page_list); + struct folio **folios; + long i, ret; - collected = collect_longterm_unpinnable_pages(&movable_page_list, - nr_pages, pages); - if (!collected) - return 0; + folios = kmalloc_array(nr_pages, sizeof(*folios), GFP_KERNEL); + if (!folios) + return -ENOMEM; - return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages, - pages); + 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, @@ -2572,6 +2606,12 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, { return 0; } + +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) +{ + return 0; +} #endif /* CONFIG_MIGRATION */ /* From patchwork Thu Apr 4 07:26: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: 13617393 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 E2C28CD129A for ; Thu, 4 Apr 2024 07:54:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A56626B008C; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B88F6B0092; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8087C6B0095; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 608E46B008C for ; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E5996120895 for ; Thu, 4 Apr 2024 07:54:36 +0000 (UTC) X-FDA: 81971087352.18.B90AD25 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf25.hostedemail.com (Postfix) with ESMTP id BB672A001A for ; Thu, 4 Apr 2024 07:54:34 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cPE2G0te; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 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=1712217275; 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=V6kCHyWZKqn7mR1PNWai0TB/t0/0xfCN0prTWy3MkRg=; b=r1VcgoSjCP53H8f6jgLx/c7LV0dryNLj9/iNk2EhJp35F0UAtn8jsaB2xzcXVHDfViwwP4 Mr8Ey12vPohlhiHns31abHxjuWlw0fGiOg+jjmxBJR3aOdc4tiflCL0xAi5YaNCO90DJFg ZJm1Kg13U21gScWBzZdxPiIBgkPTZV0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217275; a=rsa-sha256; cv=none; b=QG9J/ESkWh9JalzK7wz1mXuHb1ODKnWBSy2wnSe/vo0m7pVPMLpMEohXvJSp2e6xVLkrJH 1LT58a0RXJuNasX8AnroB3khdz4eDBQsGwm/2ENmsJw9LFEM11hbesz5Gxp2O4Za78xMEN zrt+wJ7LQqvs2V3p1THqYsbXunwy8Bk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cPE2G0te; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217274; x=1743753274; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Yb7amEGe1VBc3b0oe6v93Jw3wp7VQuVAA8DFhsG6G1g=; b=cPE2G0tebq2KYc96uQoyTGnBkFGuHkLWZBFHhq94RzH8yM7kvuOzBrKs qicvOYROrx4xzuL0UqClEhGspgAWyaTQQI7ZOB5iEinq/26nsCHCvg42Z gp0rX6wXHn+ANua1y5FiRYbjRL6+S3Ztx7AAk7X2rVMpkGkCuxFNJCohF tSlcFpbMZb9w1oOz9S2tUlvy+k8WTzSWv+4M/eS+LeJfxZRvVN8fHbeOU JbIaW4OVYpXOYajr80w503+W5HddAII5IZflnxIl4sNX4Sr39W78Dp1iU K4YPJIK0VBKeudBBTw7jRfJShB14tVaiUHoR8DnDqKDge7GiYuBPLnAk/ Q==; X-CSE-ConnectionGUID: 2d4kx6HDRp+BxLBUQAEH/A== X-CSE-MsgGUID: A3kEcpGcQSS5VFAT40BmZA== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450788" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450788" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:27 -0700 X-CSE-ConnectionGUID: oyRgjK/WTKuzsJABcC3ksA== X-CSE-MsgGUID: vozHhhVGQIeScfbT58z8VA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298762" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:28 -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 , Mike Kravetz , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe , Christoph Hellwig Subject: [PATCH v13 3/8] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Thu, 4 Apr 2024 00:26:10 -0700 Message-ID: <20240404073053.3073706-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Stat-Signature: 6adrqdb8on4j4zaezpduka4sy4jgxskg X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: BB672A001A X-Rspam-User: X-HE-Tag: 1712217274-572436 X-HE-Meta: U2FsdGVkX1+5gzjpbRufKrddwcHGTtdTQiiYJyfKeOzfzSykz1qgdg1c3g6nuPh1glGYrLSJCCm5OijwHMF57rliaCGtB2m0LqHo567AV8nkPVoj34CeIM3QP/1haJSYZ5Krnduf14GpByOK8BuF8xse25dL5Ts9aC0LD7YXwqjbBBTsUoAncfwiaRPdwKZ5NK3NCPlOqfnktBJ6lr9LpLpwNXFmWZt75Lg0A8mSV3c575dc8c/QsbK5eQBPG0zE2T4Ib9elg9b9I9Sfs30hHi/HXuk48u7BZ6KRf3l18mBf/mqQJv18NvbIoATG2sa4IAQyld3ZFDpaF1onJvnXL8XRpakqw3cM4vxWYJxfLYE+ZJQExyxd7iJVPFYRdCe1xXJK1a+LtemmnWyxinJ4DjBbYJ6sXKwcyP0tn0yAa3ApEYdzLBzVE7gmNnhgLh2MgRKZwiCcRiyVBeM9jF2NXyQ4fnIJ5J9E6NKwS92nnBFZDVHWk4b6cT1JeRARTfVhI747FEs/Fi9tIVvdX/61TOFprE/g3w3AGjZdYnihoJiueb1jFn3hJRlXc+S6RO4B1DrN70UDlUxWifqQg+FGf3UUX5jh4lD/m9NQ0U7GzGPE+JPV1Qus5tvvRO/FTrIZ0xJS3MHjpU68YIwSbjNl4yU1c1B9Ry54LDyRumqBbEzQA3x7KyZyBidDkeJHhWpRQCmoWMcnkP6lAoHCKfvIPO/Hg8Tq/A0wHMEPaT2KC38PAqlLRtqe09RR24C/2JxKAyUgOiHa4Iw0G4IvcMpptjaDPdRwiwd0rCBVw3G8dyilKTfqA/33eAHC8hAXGB54NtWeLZ8y/b8BjG3St0ihmTHPw70SxJ3zphkGX0vyms+dKX+oyUGBNDsYz/silR4iDHqs/+DA4YM6Fs08CBA82jVM21qlYjn+patcreaAHyxNOzUAzZh6bcICpkMrzknuX2Z8YjXzkWETceR7zaX zDDTo0pq 6LX/q/AeR9vYLlWcSj2Ie4qFbEO74bjkGnv4VJQ6lU3YC1LDbdrwc4sNiHaIEVh+q8UdIYEUuLO4ZYN5eqXA/PvCgAeOpec5B5N3wcEN4EjlW7Zvsf+OM0UDQkiYhVY1hgusEXzPl/rIpBNeRRDiAHrSe0nizb+vap5sqagHJWa96vKSqHk5YcidE4Tp1Y4z34g1EBxf/sKeIv97FIA/qvjH1cxQIJWlKMDEGwvhbrNrG6HocUOk0ozq5PW7jwPyxElAx/R5UmFIDvltO+aC1Q30kLFc6nK7gxXbUx6Z8/4FsuK4fk/OrxyzRaftfS+fevOZBYSaGDUuyEvechhkBoqdXMkMCnddqj3zQLvnI+of4wvo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: For drivers that would like to longterm-pin the folios associated with a memfd, the memfd_pin_folios() API provides an option to not only pin the folios via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with memfds but it should work with any files that belong to either shmemfs or hugetlbfs. Files belonging to other filesystems are rejected for now. The folios need to be located first before pinning them via FOLL_PIN. If they are found in the page cache, they can be immediately pinned. Otherwise, they need to be allocated using the filesystem specific APIs and then pinned. Cc: David Hildenbrand Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe (v2) Reviewed-by: David Hildenbrand (v3) Reviewed-by: Christoph Hellwig (v6) Signed-off-by: Vivek Kasireddy --- include/linux/memfd.h | 5 ++ include/linux/mm.h | 3 + mm/gup.c | 136 ++++++++++++++++++++++++++++++++++++++++++ mm/memfd.c | 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 f2ffd54e1736..9e5055f11021 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2533,6 +2533,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 00ee3b987307..9e64a89182b5 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 @@ -3746,3 +3748,137 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +/** + * memfd_pin_folios() - pin folios associated with a memfd + * @memfd: the memfd whose folios are to be pinned + * @start: the first memfd offset + * @end: the last memfd offset (inclusive) + * @folios: array that receives pointers to the folios pinned + * @max_folios: maximum number of entries in @folios + * @offset: the offset into the first folio + * + * Attempt to pin folios associated with a memfd in the contiguous range + * [start, end]. Given that a memfd is either backed by shmem or hugetlb, + * the folios can either be found in the page cache or need to be allocated + * if necessary. Once the folios are located, they are all pinned via + * FOLL_PIN and @offset is populatedwith the offset into the first folio. + * And, eventually, these pinned folios must be released either using + * unpin_folios() or unpin_folio(). + * + * It must be noted that the folios may be pinned for an indefinite amount + * of time. And, in most cases, the duration of time they may stay pinned + * would be controlled by the userspace. This behavior is effectively the + * same as using FOLL_LONGTERM with other GUP APIs. + * + * Returns number of folios pinned, which could be less than @max_folios + * as it depends on the folio sizes that cover the range [start, end]. + * If no folios were pinned, it returns -errno. + */ +long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, + struct folio **folios, unsigned int max_folios, + pgoff_t *offset) +{ + unsigned int flags, nr_folios, nr_found; + unsigned int i, pgshift = PAGE_SHIFT; + pgoff_t start_idx, end_idx, next_idx; + struct folio *folio = NULL; + struct folio_batch fbatch; + struct hstate *h; + long ret; + + if (start > end || !max_folios) + return -EINVAL; + + if (!memfd) + return -EINVAL; + + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) + return -EINVAL; + + if (is_file_hugepages(memfd)) { + h = hstate_file(memfd); + pgshift = huge_page_shift(h); + } + + flags = memalloc_pin_save(); + do { + nr_folios = 0; + start_idx = start >> pgshift; + end_idx = end >> pgshift; + if (is_file_hugepages(memfd)) { + start_idx <<= huge_page_order(h); + end_idx <<= huge_page_order(h); + } + + folio_batch_init(&fbatch); + while (start_idx <= end_idx && nr_folios < max_folios) { + /* + * In most cases, we should be able to find the folios + * in the page cache. If we cannot find them for some + * reason, we try to allocate them and add them to the + * page cache. + */ + nr_found = filemap_get_folios_contig(memfd->f_mapping, + &start_idx, + end_idx, + &fbatch); + if (folio) { + folio_put(folio); + folio = NULL; + } + + next_idx = 0; + for (i = 0; i < nr_found; i++) { + /* + * As there can be multiple entries for a + * given folio in the batch returned by + * filemap_get_folios_contig(), the below + * check is to ensure that we pin and return a + * unique set of folios between start and end. + */ + if (next_idx && + next_idx != folio_index(fbatch.folios[i])) + continue; + + folio = try_grab_folio(&fbatch.folios[i]->page, + 1, FOLL_PIN); + if (!folio) { + folio_batch_release(&fbatch); + goto err; + } + + if (nr_folios == 0) + *offset = offset_in_folio(folio, start); + + folios[nr_folios] = folio; + next_idx = folio_next_index(folio); + if (++nr_folios == max_folios) + break; + } + + folio = NULL; + folio_batch_release(&fbatch); + if (!nr_found) { + folio = memfd_alloc_folio(memfd, start_idx); + if (IS_ERR(folio)) { + ret = PTR_ERR(folio); + if (ret != -EEXIST) + goto err; + } + } + } + + ret = check_and_migrate_movable_folios(nr_folios, folios); + } while (ret == -EAGAIN); + + memalloc_pin_restore(flags); + return ret ? ret : nr_folios; +err: + memalloc_pin_restore(flags); + unpin_folios(folios, nr_folios); + + return ret; +} +EXPORT_SYMBOL_GPL(memfd_pin_folios); + diff --git a/mm/memfd.c b/mm/memfd.c index 7d8d3ab3fa37..6ce2abb22c2d 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -59,6 +59,41 @@ static void memfd_tag_pins(struct xa_state *xas) xas_unlock_irq(xas); } +/* + * This is a helper function used by memfd_pin_user_pages() in GUP (gup.c). + * It is mainly called to allocate a page in a memfd when the caller + * (memfd_pin_folios()) cannot find a page in the page cache at a given + * index in the mapping. + */ +struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) +{ +#ifdef CONFIG_HUGETLB_PAGE + struct folio *folio; + int err; + + if (is_file_hugepages(memfd)) { + folio = alloc_hugetlb_folio_nodemask(hstate_file(memfd), + NUMA_NO_NODE, + NULL, + GFP_USER, + false); + if (folio && folio_try_get(folio)) { + err = hugetlb_add_to_page_cache(folio, + memfd->f_mapping, + idx); + if (err) { + folio_put(folio); + free_huge_folio(folio); + return ERR_PTR(err); + } + return folio; + } + return ERR_PTR(-ENOMEM); + } +#endif + return shmem_read_folio(memfd->f_mapping, idx); +} + /* * Setting SEAL_WRITE requires us to verify there's no pending writer. However, * via get_user_pages(), drivers might have some pending I/O without any active From patchwork Thu Apr 4 07:26: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: 13617394 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 305ECCD129B for ; Thu, 4 Apr 2024 07:54:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C93856B0092; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE45D6B0098; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CE5F6B0093; Thu, 4 Apr 2024 03:54:37 -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 6C2FC6B0092 for ; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 39F18A151A for ; Thu, 4 Apr 2024 07:54:37 +0000 (UTC) X-FDA: 81971087394.30.CA95212 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf14.hostedemail.com (Postfix) with ESMTP id 209DE100005 for ; Thu, 4 Apr 2024 07:54:34 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="C/ur8kxV"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217275; a=rsa-sha256; cv=none; b=8GG2rE1FXOjhh9DDZDJJsRuvJ9JVDoWctxl0EjYdHctc4ShZwRHzaNmQfmu5qY9E1HqcNe Q0JA9yKvU2R+NCKQX0AiCQfVYiI7ID9tl03pLaWxoN8o8oyRUCDD3SpnIu+rTVHWAK54x4 C5i8D511ReAlqgpApSvq7Sdzql3HiOw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="C/ur8kxV"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 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=1712217275; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=G7Bv7jg6ypbCtXkKx3Gshd1i85VTcQErigj+7qKNZ54=; b=m4dkpHJdcTaEHM6m0K7qqdPIqo1xBYzZLmcspcWdxuZgkdSdliGFsv02UarvKY3ZI5FbSE htfsbUl3V7yzEmzOFprYuikqyuKNfata9nJ7/3uyfedvqd3MwEvgNYIEjBg/QvklVDv/Bi 41/T91vbSxFVPfyG5ot6Xj54fI1Nx40= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217275; x=1743753275; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tyJeBuptegtLMXafEhsS2DHQW53sLM788gfa3Q0ViXw=; b=C/ur8kxVqN72dCnS6w3cRXNlc3H86EZqFCejCRdPM6h4HHSxt6c0F5Wy 5pEX+SugXnY0u4YkMDwDgJvfI/dGF/i8T0GBuAj/o+JZH+nShOjBGHyfn shz8OddtYMIW9BTiRhRqJCmZCvsVgS9IANQEIJLfuqQmPP9SvfiRJgnQP BjsC4YWp7BLKHpIqsZ6TgLIk9GO2si/jnW1/BjzJXP//hLPbTZSGgh74t gUTu8eZnq2aUTIOXEjo/jgGJ+hpivG3sYVkOJE8qOjWq2fyx+swkQNMcV t+j1fGJCk2l6AxLiOGVfvqlodG/17xGmZvcKYcW8+rp/17v7wQDQMIkIR w==; X-CSE-ConnectionGUID: d+WEnkonQ5OuezsXxWgspQ== X-CSE-MsgGUID: aNliYFyYSS2rac3s91Qr7w== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450801" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450801" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:28 -0700 X-CSE-ConnectionGUID: jnjQ46gVQ52yJhGu0ZLl8Q== X-CSE-MsgGUID: 7Z2c6NZ1SzKO78d3Nx6ccQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298766" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:28 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v13 4/8] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Thu, 4 Apr 2024 00:26:11 -0700 Message-ID: <20240404073053.3073706-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 209DE100005 X-Stat-Signature: 3cop5byqhattq5dakg6t1xqdj8xubyb4 X-HE-Tag: 1712217274-708640 X-HE-Meta: U2FsdGVkX18VVEaq4rux5IU5vRzKLEBhpOBlgQJdvbqqA1VR3st1v7Mek34dWZxZD3oX6rM5obAldRCUjMzWMlsVaHhLCwI7oa6UZVbvJVcN8Os1Jvhfmh/TNdt3mcUpazYM994NFLVe696o2+1JOWRdldlHe1jrDv5Ge381+o+7CzRA7KT0+2+LF0/ZhU5HC0cfQhS4upXNcREEhvmEfiLfdC0RrYcUCvTal/zUDmlS2gbe5PYKCyGFELNuqxpOjFho08j8x/x4LMzSghiQL6JIHqfGII+AQ5IJCraJRpuv2rxlP9bOFPjKMikdeVX1VI0r0MswaYLBOCu7y71JE/dWbXkgzF08oIl91jQXigIzdE7f/S7Tx8jMKOBx3pqJrvAm9cA2otwC8nJDu/yGeYFCB0vc8b0S2OJwg0nHI9XJCDlVZk+PF7eWswO6Pr4Q1zfVFFplwc6tkrgjV6huu1xwcIskk4Dl5MF1mkCOZhEkOJ16ycuSbs3rKJShxB4Cmg8f5+le8km3r3bX4j5NCf7orUm1JUmE243LExNENEgrv3Yz+aE0U+OSH5N5Z8abmNyxal/809kdFpoaEUBMD7UplbmZ15/txqk2mC8EZbH1Ock/AkJd1fRC7PmIfVQCOlYhuv9mXT3beaBz4ov4NfPlUa2WmTzge17VcfvaI2+gmwCHAPdBDmqeLtvSbAFbllYKfTRfsZTaJccIuMXPKQ80qfKsgTKwJ5+g/BJJSHXTogqJG+DPoRCGteCPODj/zEQYZPl8YArXv7S5RQkDevf4fGd/ISlsMR9wyma2ZDQy/wKZjJkjtO9Y0QVtfffxtYS+VftcN7ZTHrRdrU4B8pMkdaaYOtNJ7zkoPjl8LoXia+ODUyupsc6DIeR0JzXG+OgQxJN1VqV4Sr0+IDtlsPuN8PRnrm6a3ZM3hoZvFjc81bX/aLnd0tlvhkUlCOwElGQF14E42nlO6cQeO13 S9CmQNqQ ACDw6oAD7mdEaD0zFubwpFwT69o7v+SUgpHPINDMeQJ0qSuJl+eJ+bPY/Z89GtvR7bDUqwoZkzS1Bfr0YVKxD9wm01juqdEpNFCoWMTdptEBIIMsBr8RVSTLMuGobxoJZlTG24Bw5sTNEQ995t0sQT7nKTNSDve5WjE6WSXQ3ZbEdxEO2Q/t1VQ47JJGlOo486pp10N1gvefTcME0VhPScVKif0XCNuBCRz3QjiKQz42GLqWltGENjc3IszsEMpRzMTyPMgYvExJJFWlrZAKiGNNE5J0Mw1hifgPDUmhEZEwHFNY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add VM_PFNMAP to vm_flags in the mmap handler to ensure that the mappings would be managed without using struct page. And, in the vm_fault handler, use vmf_insert_pfn to share the page's pfn to userspace instead of directly sharing the page (via struct page *). Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index c40645999648..820c993c8659 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -35,12 +35,13 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) struct vm_area_struct *vma = vmf->vma; struct udmabuf *ubuf = vma->vm_private_data; pgoff_t pgoff = vmf->pgoff; + unsigned long pfn; if (pgoff >= ubuf->pagecount) return VM_FAULT_SIGBUS; - vmf->page = ubuf->pages[pgoff]; - get_page(vmf->page); - return 0; + + pfn = page_to_pfn(ubuf->pages[pgoff]); + return vmf_insert_pfn(vma, vmf->address, pfn); } static const struct vm_operations_struct udmabuf_vm_ops = { @@ -56,6 +57,7 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) vma->vm_ops = &udmabuf_vm_ops; vma->vm_private_data = ubuf; + vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); return 0; } From patchwork Thu Apr 4 07:26:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13617395 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 9F255CD1284 for ; Thu, 4 Apr 2024 07:54:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80CC06B0093; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BCAE6B0095; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 637E96B0098; Thu, 4 Apr 2024 03:54:39 -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 445A76B0093 for ; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0E610C0F53 for ; Thu, 4 Apr 2024 07:54:39 +0000 (UTC) X-FDA: 81971087478.23.44A5D02 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf16.hostedemail.com (Postfix) with ESMTP id DB85118000C for ; Thu, 4 Apr 2024 07:54:36 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FQ21eimu; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 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=1712217277; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=H5LZX4x0dPh49vNzSTLf7kYd0GJ+7/NAQZVV/ovBC1g=; b=lb67touL7cvdMxH8o6nqHGZFfD97vvaS9rSmBflsDu1ZHYUDaYDLLz2QIsEENrszo2kk59 +oJnmWD0TPT5kEJSriD3o16KTTPOX5sBcpDNviSYC535HzbHKPqkDwVtZuFW4vZdWZNQDO KZni4YRjshLFyI0SqWld63FI6S6M23E= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FQ21eimu; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217277; a=rsa-sha256; cv=none; b=lijovyW6+MbPSHlO+/ZTYREc2nIULn4+53EX5+3fECbV8xJaS19GTXN7iGWNsgvNEh9NOu vWeN8LB3e/JpEVlLj/t+hDjcaueoPWFGzQF8ag5a8IxVXbb1GMO+4OdLGG8cxY5uH6cyF3 m9TjpkcClGIw6f4WpHu0RQw6q/Gv6MA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217277; x=1743753277; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OFY0cfHR/dm9OGHwjR/w6MaeqHarFwrIQpExmOosGtk=; b=FQ21eimuBqy3UTmOGzEA9P0cpWiUZQGIeZ0bputwmPfHnBZ7X6JTOZEc 2rAc0RlBnX5htHNe0YyRFL3r9FEBgCTVlCSuSPgiNQIMm88GMxtcitRMA e0oboMvBTGwmQVUXb32UGO3R4wTbc3W473KimEbLFXMHuI45xNJVFnJDM qWox557G+Wi3s0nbX8zkcbd8hsFhe8V5ipsJGnm7EVxryPz2oa8oIXdWb 29lLpaIA+dkyTkXY/2D5UpVAKak7DcW5Zyga0oOIX+dRnfokqWDTKy7jg ZrMcv3WVpMT2DwWMRMAKq6+y2C4ZV432MWSqRAOqyw6o7eTawdysZEsuS A==; X-CSE-ConnectionGUID: lXLVr5hNRO6UiatWtyGTqw== X-CSE-MsgGUID: q20AW11lSNyK8qN7wb0q/Q== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450807" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450807" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:28 -0700 X-CSE-ConnectionGUID: pccvBgTbTRG+xVR8YbZcIg== X-CSE-MsgGUID: UN/AcFhtTkCd1wNsSwGGuw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298771" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:28 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v13 5/8] udmabuf: Add back support for mapping hugetlb pages Date: Thu, 4 Apr 2024 00:26:12 -0700 Message-ID: <20240404073053.3073706-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: DB85118000C X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: xibjffmtwndpb3h4fz94mxpqpbfrbmna X-HE-Tag: 1712217276-971051 X-HE-Meta: U2FsdGVkX1+3g2+cgUqfU8wvM3TDmx3FcphcsUKUPRuDk+Zmzn6YiE8VF4jTL8GnF/KDxN0W9F5SU1NcbrO2PqFLeQtzFKKCKmK6HKSi1thNSRAc/nuul1sYZ6PPghHsF+R1op1cPtMUz3Wnsc/JJ2vZVj9ZUwcoA8h6TQYxsW2VTuM+zbrwhmC17kH1EO7eigsb1NvzB0XagynD4h84KqEOkxWzwf5rVI+lkCtgbUhNtp3v3Y7/BDpIZYq+Uj6dDkTXovuDd9llrjwJ0fsVTAS/wNgQTCvrOh5Iw5dPOIijdR+ZhYUOGG7v7cvlHycSOiLEx70AO46J6QjJQAdLGnFwJmGrQnFa5iQM/5gHScbQzP5wzhYVmZENwLW6L5hvqsaC/zVLt4u4tAvijrM4j7IqplkzR8mWJEO7k14HWzFVV8B1TgOjoZX9OPrCUOfZkZqjRXi/QVvOgdx6xnSQpMx1YRtAs9WbVyWwxGWhdjAQtwdq8dpN/nXdq4xcs7nRREZ7eVPLYx4QEaPvz47mxrhoJeQ00jyQKtocf4GS7MSBgwIUSt1mVc+idL+LSbNEWiFdXOVt8zqS/tiaMJOAF/WasRO9Rzw1sLUq7p7El43h0TzP+q3MlxZcASixToKVkpajUxoa5luYbxhLpZFfnc74Ft3JMJyPLOsZ2iOlu9u1yRE1eBaAgaCzWs8C49VowsU+I5hAn81TTha1gNISEWqxJHnE3RdG+Xr0OS5neYG0M+4LAjeKjtzt6wwMHMnX851ERP0xNfNibXOD/RkrNLTc5ZtSPp0Uw5bLT38CS2p6ik6OeRfW2j6STvkIISW4xS8fPTqZsqLMjZ6CVBqnNJGhPaTWK2i/xgn/ILqX65puKRLg22xCMu7y8dxGBDIXPeHQBbOH44osBKJz7LvvOmV9ip0T5W2CHU7kZCOPEi/TwuxOCZpurmPl+v4JjHT5wJynMwCGw8EVD4lGvg0 zJcbkRWK c3SWq6DSTJwbKr5kW7MogKFxYBMNQ3yM2JLqDjsG8m3QBW6UiJPOxHFiMvhkUCdup8xIOCObSpOCZeYiUG8nwn7NdUe4XxiJ+ea2zrGs444a/m7Fo2P1WeH+F18athe8vTfbCR+xwUVZDvZfL8AvNPUuZwsZYqeanFv+OMXpka7KkaBqzmeQAGfS8Fb2AYJxUNPWeL2hkb0zdXPK5n30fyQguSiuQ5I7tgjeKLvflQULEGoDbQb10Qaw67ZqLnTsGxTib X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: A user or admin can configure a VMM (Qemu) Guest's memory to be backed by hugetlb pages for various reasons. However, a Guest OS would still allocate (and pin) buffers that are backed by regular 4k sized pages. In order to map these buffers and create dma-bufs for them on the Host, we first need to find the hugetlb pages where the buffer allocations are located and then determine the offsets of individual chunks (within those pages) and use this information to eventually populate a scatterlist. Testcase: default_hugepagesz=2M hugepagesz=2M hugepages=2500 options were passed to the Host kernel and Qemu was launched with these relevant options: qemu-system-x86_64 -m 4096m.... -device virtio-gpu-pci,max_outputs=1,blob=true,xres=1920,yres=1080 -display gtk,gl=on -object memory-backend-memfd,hugetlb=on,id=mem1,size=4096M -machine memory-backend=mem1 Replacing -display gtk,gl=on with -display gtk,gl=off above would exercise the mmap handler. Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Acked-by: Mike Kravetz (v2) Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 122 +++++++++++++++++++++++++++++++------- 1 file changed, 101 insertions(+), 21 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 820c993c8659..274defd3fa3e 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,7 @@ struct udmabuf { struct page **pages; struct sg_table *sg; struct miscdevice *device; + pgoff_t *offsets; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -41,6 +43,8 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) return VM_FAULT_SIGBUS; pfn = page_to_pfn(ubuf->pages[pgoff]); + pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT; + return vmf_insert_pfn(vma, vmf->address, pfn); } @@ -90,23 +94,29 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf, { struct udmabuf *ubuf = buf->priv; struct sg_table *sg; + struct scatterlist *sgl; + unsigned int i = 0; int ret; sg = kzalloc(sizeof(*sg), GFP_KERNEL); if (!sg) return ERR_PTR(-ENOMEM); - ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount, - 0, ubuf->pagecount << PAGE_SHIFT, - GFP_KERNEL); + + ret = sg_alloc_table(sg, ubuf->pagecount, GFP_KERNEL); if (ret < 0) - goto err; + goto err_alloc; + + for_each_sg(sg->sgl, sgl, ubuf->pagecount, i) + sg_set_page(sgl, ubuf->pages[i], PAGE_SIZE, ubuf->offsets[i]); + ret = dma_map_sgtable(dev, sg, direction, 0); if (ret < 0) - goto err; + goto err_map; return sg; -err: +err_map: sg_free_table(sg); +err_alloc: kfree(sg); return ERR_PTR(ret); } @@ -143,6 +153,7 @@ static void release_udmabuf(struct dma_buf *buf) for (pg = 0; pg < ubuf->pagecount; pg++) put_page(ubuf->pages[pg]); + kfree(ubuf->offsets); kfree(ubuf->pages); kfree(ubuf); } @@ -196,17 +207,77 @@ static const struct dma_buf_ops udmabuf_ops = { #define SEALS_WANTED (F_SEAL_SHRINK) #define SEALS_DENIED (F_SEAL_WRITE) +static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, + pgoff_t offset, pgoff_t pgcnt, + pgoff_t *pgbuf) +{ + struct hstate *hpstate = hstate_file(memfd); + pgoff_t mapidx = offset >> huge_page_shift(hpstate); + pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; + pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; + struct page *hpage = NULL; + struct folio *folio; + pgoff_t pgidx; + + mapidx <<= huge_page_order(hpstate); + for (pgidx = 0; pgidx < pgcnt; pgidx++) { + if (!hpage) { + folio = __filemap_get_folio(memfd->f_mapping, + mapidx, + FGP_ACCESSED, 0); + if (IS_ERR(folio)) + return PTR_ERR(folio); + + hpage = &folio->page; + } + + get_page(hpage); + ubuf->pages[*pgbuf] = hpage; + ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; + (*pgbuf)++; + if (++subpgoff == maxsubpgs) { + put_page(hpage); + hpage = NULL; + subpgoff = 0; + mapidx += pages_per_huge_page(hpstate); + } + } + + if (hpage) + put_page(hpage); + + return 0; +} + +static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, + pgoff_t offset, pgoff_t pgcnt, + pgoff_t *pgbuf) +{ + pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; + struct page *page; + + for (pgidx = 0; pgidx < pgcnt; pgidx++) { + page = shmem_read_mapping_page(memfd->f_mapping, + pgoff + pgidx); + if (IS_ERR(page)) + return PTR_ERR(page); + + ubuf->pages[*pgbuf] = page; + (*pgbuf)++; + } + + return 0; +} + static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { DEFINE_DMA_BUF_EXPORT_INFO(exp_info); struct file *memfd = NULL; - struct address_space *mapping = NULL; struct udmabuf *ubuf; struct dma_buf *buf; - pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; - struct page *page; + pgoff_t pgcnt, pgbuf = 0, pglimit; int seals, ret = -EINVAL; u32 i, flags; @@ -234,6 +305,12 @@ static long udmabuf_create(struct miscdevice *device, ret = -ENOMEM; goto err; } + ubuf->offsets = kcalloc(ubuf->pagecount, sizeof(*ubuf->offsets), + GFP_KERNEL); + if (!ubuf->offsets) { + ret = -ENOMEM; + goto err; + } pgbuf = 0; for (i = 0; i < head->count; i++) { @@ -241,8 +318,7 @@ static long udmabuf_create(struct miscdevice *device, memfd = fget(list[i].memfd); if (!memfd) goto err; - mapping = memfd->f_mapping; - if (!shmem_mapping(mapping)) + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) goto err; seals = memfd_fcntl(memfd, F_GET_SEALS, 0); if (seals == -EINVAL) @@ -251,16 +327,19 @@ static long udmabuf_create(struct miscdevice *device, if ((seals & SEALS_WANTED) != SEALS_WANTED || (seals & SEALS_DENIED) != 0) goto err; - pgoff = list[i].offset >> PAGE_SHIFT; - pgcnt = list[i].size >> PAGE_SHIFT; - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - page = shmem_read_mapping_page(mapping, pgoff + pgidx); - if (IS_ERR(page)) { - ret = PTR_ERR(page); - goto err; - } - ubuf->pages[pgbuf++] = page; - } + + pgcnt = list[i].size >> PAGE_SHIFT; + if (is_file_hugepages(memfd)) + ret = handle_hugetlb_pages(ubuf, memfd, + list[i].offset, + pgcnt, &pgbuf); + else + ret = handle_shmem_pages(ubuf, memfd, + list[i].offset, + pgcnt, &pgbuf); + if (ret < 0) + goto err; + fput(memfd); memfd = NULL; } @@ -287,6 +366,7 @@ static long udmabuf_create(struct miscdevice *device, put_page(ubuf->pages[--pgbuf]); if (memfd) fput(memfd); + kfree(ubuf->offsets); kfree(ubuf->pages); kfree(ubuf); return ret; From patchwork Thu Apr 4 07:26:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13617396 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 8A26ECD1284 for ; Thu, 4 Apr 2024 07:54:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DAC686B0095; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D39106B0098; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDC866B009A; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9F8EC6B0099 for ; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4748F1A0F05 for ; Thu, 4 Apr 2024 07:54:39 +0000 (UTC) X-FDA: 81971087478.16.2E7807A Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf25.hostedemail.com (Postfix) with ESMTP id 04CB8A000D for ; Thu, 4 Apr 2024 07:54:36 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=flOUONld; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 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=1712217277; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LKuz1kue+PFB3PtnGqxy8j+AQVi5RxBcHxsg6i4pFDM=; b=FFsqYJ5jJTchomgr0Sd26ZNi/DV4UfpfvAEwhJcCg6+iOusXvrGy/jECTjGSFOwc1oz7VG KwEnqswaFt1eFGkAZ9RqYA1cE09sAollGDtivhMoV5B4uGBxHAsvx81cpfdztfMpUFd/xE 9wnrK149yDWzitYN5RfYyba4JiOjQkA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217277; a=rsa-sha256; cv=none; b=ts1bES7yusoLTtF9fZXiTGqZJgrGZLU8CG+5CDxBA7MR1dNiahLjJ8qwx4ejFGsRFUbA+/ X9RhY5Dglw5gzQ/4gwdDASESvxQbNLhfgd49RkVtRRvTG+K9Ke1eC3UzQRQPFw6mlkn9XU m4zKQiWCRijf6YHcHIuAoAsGbJU2N0k= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=flOUONld; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217277; x=1743753277; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=z2j62I0uVNUXZaGZCUjggQYnU/PyD5X487LNKLaNl5c=; b=flOUONld53SOv1IJEbpWPfFC2jXU7eEuNCW8Lzv51eP5lyVNvZpdoR3g VBSaA7Z44JSzRw+g4BlfpeqOiHKPXjxzB3WJ9dA2PYGSrOWYyt/a5V9/6 y6SrukIugz1mUgvbNGAXZy5sGJfmqLVKEibgfqeJ5ZoDkjdDaJ9VGK+Kv vNtHbQFPVPF3R+4x2XSxtSDuTSWzDttnoXkxIAL3m8yLI8/8gcsxLlfgb yoejqtObliRhbIim1ORgr0BKN08GPbD2qt91lHwP/TVuUJYxcKcxX3NDc m6BzSkf7XIzZPLwvaMTOgTakUvx3MEOo+nhzyzKq5ojtkDbx6m/dWAjP5 Q==; X-CSE-ConnectionGUID: UuEOideER4Wosd1xVjL4Aw== X-CSE-MsgGUID: Jfxfyab/Q5enj6mSlM0uCg== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450816" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450816" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:28 -0700 X-CSE-ConnectionGUID: gbB/s+TyTciBbcmCRtqxFw== X-CSE-MsgGUID: 8mRM29iZRS+J9JDBWwcZEg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298774" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:29 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v13 6/8] udmabuf: Convert udmabuf driver to use folios Date: Thu, 4 Apr 2024 00:26:13 -0700 Message-ID: <20240404073053.3073706-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Stat-Signature: utzyud1mjhpwk935nch6wksz9757pq8j X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 04CB8A000D X-Rspam-User: X-HE-Tag: 1712217276-176003 X-HE-Meta: U2FsdGVkX18zq6SszZubQoqFj0Xvl2SvXBznWHF5aafywCXC/7N3N4uSYQJYuKEWtez2VY6UEMqG3nD/LnMiOYLzcYceejRIth3kiCCnCMJrzpYPWgfr9F3rVM2YLdEcgqgp/oIikgO+1+5OkDTM7sKitfunEdusSyii1ljX3KZEENPDrOpjFqkBQCsjudp6xllEmRkEJhKd89gYETzaVOF932ov/5GogR0Cg9aa9J9Qz7zC6SUCYpO2hmNPjSKGAfaVazDOQlf1XzrvnJWUU8mKtlXr+kQmotR/6ksNoVHXucsOrXndwbOUSic16zVghl740KZTLxIYcQGVpSoWbbsFlVdv7/3pKnwgs00wdJWXmatCDN0tSft9xUPpox1r7Um4EP71UGlSWGgI+gLV2pTpyzSzq+RtkxvhtRxTZVPFPN0D2abf647VZ/hnVq63I05ILaOItFpF6pIFau/Blb7uig8p1+jbLGLe2Bm1MQRan6KLsJbR7RKatR3R9EwMmLBuwj43x5WYD3jnzS5dMpVRv3y3y8Ft+nTgyklpbwOp0aAGz0IM0v4sjfknpNj4QY8uV0u2eXXmCOIUJS28EzQu86O24BVYyQzcT26fbMHCrx8c3z7mfYuQBVGWM9/z3H4PjBssz1uEijiw0gQANkT57Cw8qoPx+oThTG2ppLpa3+xW3lnRqr9TkwcLvSZC/g6w/PmGNHhybojQlxLqbVrop1SYDW8cBQ+60iR1g2VdvXZy/zCFZoxSxxINo42AphriQfqIA9uKBJQcoEBXPPFj2mMsBbKJRuStETXIjivySb4M++4fHyklQ0NxJEUIxNITXAwFHKJZ47hqpWifA9YLtsSlZoYsoVd7NEC5ALeRvUTQ3yXrSFR/Ho7do8ptD3lR8wtuMBOJRs6YMrz2BA8Dfhi30Xcb12qEKorYBG7dOzN7/oZomkKw9mUM+ESeHS3OZLq25tRur7HM0Ya Mk8ziUk2 jxkEETidHE9lzpOwBNiIp5SY6fiZjwwODPvwFlXtH5qjk4omSMt9pIm4deTvMreFZF424GB7vozoQ4JBRgtH/s5F8WOOTVmw61GYtMrIOSrTOte8WTh1Aqi6iPVOgD56C4+CxtZlfu+vt4PjDUW6hmeC2CtbwLT/GD9XtaDoysDUc7GUCY7Um3pxJPx/4Us9zQT/Rq24/PaiCdeuwdOdXaeF8O5qKF8BNmCSOVtKVSd/nOd2j7C+HlwBemBCcMu6fKTsWawL8kwVuFCmdqPX96MOU/xNkOqWfDBqeWMB8OLexvT2rBhlGikTcWg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This is mainly a preparatory patch to use memfd_pin_folios() API for pinning folios. Using folios instead of pages makes sense as the udmabuf driver needs to handle both shmem and hugetlb cases. However, the function vmap_udmabuf() still needs a list of pages; so, we collect all the head pages into a local array in this case. Other changes in this patch include the addition of helpers for checking the memfd seals and exporting dmabuf. Moving code from udmabuf_create() into these helpers improves readability given that udmabuf_create() is a bit long. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 140 ++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 57 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 274defd3fa3e..a8f3af61f7f2 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -26,7 +26,7 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is struct udmabuf { pgoff_t pagecount; - struct page **pages; + struct folio **folios; struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; @@ -42,7 +42,7 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) if (pgoff >= ubuf->pagecount) return VM_FAULT_SIGBUS; - pfn = page_to_pfn(ubuf->pages[pgoff]); + pfn = folio_pfn(ubuf->folios[pgoff]); pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT; return vmf_insert_pfn(vma, vmf->address, pfn); @@ -68,11 +68,21 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map) { struct udmabuf *ubuf = buf->priv; + struct page **pages; void *vaddr; + pgoff_t pg; dma_resv_assert_held(buf->resv); - vaddr = vm_map_ram(ubuf->pages, ubuf->pagecount, -1); + pages = kmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL); + if (!pages) + return -ENOMEM; + + for (pg = 0; pg < ubuf->pagecount; pg++) + pages[pg] = &ubuf->folios[pg]->page; + + vaddr = vm_map_ram(pages, ubuf->pagecount, -1); + kfree(pages); if (!vaddr) return -EINVAL; @@ -107,7 +117,8 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf, goto err_alloc; for_each_sg(sg->sgl, sgl, ubuf->pagecount, i) - sg_set_page(sgl, ubuf->pages[i], PAGE_SIZE, ubuf->offsets[i]); + sg_set_folio(sgl, ubuf->folios[i], PAGE_SIZE, + ubuf->offsets[i]); ret = dma_map_sgtable(dev, sg, direction, 0); if (ret < 0) @@ -152,9 +163,9 @@ static void release_udmabuf(struct dma_buf *buf) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); for (pg = 0; pg < ubuf->pagecount; pg++) - put_page(ubuf->pages[pg]); + folio_put(ubuf->folios[pg]); kfree(ubuf->offsets); - kfree(ubuf->pages); + kfree(ubuf->folios); kfree(ubuf); } @@ -215,36 +226,33 @@ static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, pgoff_t mapidx = offset >> huge_page_shift(hpstate); pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; - struct page *hpage = NULL; - struct folio *folio; + struct folio *folio = NULL; pgoff_t pgidx; mapidx <<= huge_page_order(hpstate); for (pgidx = 0; pgidx < pgcnt; pgidx++) { - if (!hpage) { + if (!folio) { folio = __filemap_get_folio(memfd->f_mapping, mapidx, FGP_ACCESSED, 0); if (IS_ERR(folio)) return PTR_ERR(folio); - - hpage = &folio->page; } - get_page(hpage); - ubuf->pages[*pgbuf] = hpage; + folio_get(folio); + ubuf->folios[*pgbuf] = folio; ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; (*pgbuf)++; if (++subpgoff == maxsubpgs) { - put_page(hpage); - hpage = NULL; + folio_put(folio); + folio = NULL; subpgoff = 0; mapidx += pages_per_huge_page(hpstate); } } - if (hpage) - put_page(hpage); + if (folio) + folio_put(folio); return 0; } @@ -254,31 +262,69 @@ static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, pgoff_t *pgbuf) { pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; - struct page *page; + struct folio *folio = NULL; for (pgidx = 0; pgidx < pgcnt; pgidx++) { - page = shmem_read_mapping_page(memfd->f_mapping, - pgoff + pgidx); - if (IS_ERR(page)) - return PTR_ERR(page); + folio = shmem_read_folio(memfd->f_mapping, pgoff + pgidx); + if (IS_ERR(folio)) + return PTR_ERR(folio); - ubuf->pages[*pgbuf] = page; + ubuf->folios[*pgbuf] = folio; (*pgbuf)++; } return 0; } +static int check_memfd_seals(struct file *memfd) +{ + int seals; + + if (!memfd) + return -EBADFD; + + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) + return -EBADFD; + + seals = memfd_fcntl(memfd, F_GET_SEALS, 0); + if (seals == -EINVAL) + return -EBADFD; + + if ((seals & SEALS_WANTED) != SEALS_WANTED || + (seals & SEALS_DENIED) != 0) + return -EINVAL; + + return 0; +} + +static int export_udmabuf(struct udmabuf *ubuf, + struct miscdevice *device, + u32 flags) +{ + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct dma_buf *buf; + + ubuf->device = device; + exp_info.ops = &udmabuf_ops; + exp_info.size = ubuf->pagecount << PAGE_SHIFT; + exp_info.priv = ubuf; + exp_info.flags = O_RDWR; + + buf = dma_buf_export(&exp_info); + if (IS_ERR(buf)) + return PTR_ERR(buf); + + return dma_buf_fd(buf, flags); +} + static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { - DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + pgoff_t pgcnt, pgbuf = 0, pglimit; struct file *memfd = NULL; struct udmabuf *ubuf; - struct dma_buf *buf; - pgoff_t pgcnt, pgbuf = 0, pglimit; - int seals, ret = -EINVAL; + int ret = -EINVAL; u32 i, flags; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); @@ -299,9 +345,9 @@ static long udmabuf_create(struct miscdevice *device, if (!ubuf->pagecount) goto err; - ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages), + ubuf->folios = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios), GFP_KERNEL); - if (!ubuf->pages) { + if (!ubuf->folios) { ret = -ENOMEM; goto err; } @@ -314,18 +360,9 @@ static long udmabuf_create(struct miscdevice *device, pgbuf = 0; for (i = 0; i < head->count; i++) { - ret = -EBADFD; memfd = fget(list[i].memfd); - if (!memfd) - goto err; - if (!shmem_file(memfd) && !is_file_hugepages(memfd)) - goto err; - seals = memfd_fcntl(memfd, F_GET_SEALS, 0); - if (seals == -EINVAL) - goto err; - ret = -EINVAL; - if ((seals & SEALS_WANTED) != SEALS_WANTED || - (seals & SEALS_DENIED) != 0) + ret = check_memfd_seals(memfd); + if (ret < 0) goto err; pgcnt = list[i].size >> PAGE_SHIFT; @@ -341,33 +378,22 @@ static long udmabuf_create(struct miscdevice *device, goto err; fput(memfd); - memfd = NULL; } - exp_info.ops = &udmabuf_ops; - exp_info.size = ubuf->pagecount << PAGE_SHIFT; - exp_info.priv = ubuf; - exp_info.flags = O_RDWR; - - ubuf->device = device; - buf = dma_buf_export(&exp_info); - if (IS_ERR(buf)) { - ret = PTR_ERR(buf); + flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0; + ret = export_udmabuf(ubuf, device, flags); + if (ret < 0) goto err; - } - flags = 0; - if (head->flags & UDMABUF_FLAGS_CLOEXEC) - flags |= O_CLOEXEC; - return dma_buf_fd(buf, flags); + return ret; err: while (pgbuf > 0) - put_page(ubuf->pages[--pgbuf]); + folio_put(ubuf->folios[--pgbuf]); if (memfd) fput(memfd); kfree(ubuf->offsets); - kfree(ubuf->pages); + kfree(ubuf->folios); kfree(ubuf); return ret; } From patchwork Thu Apr 4 07:26:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13617397 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 6E635CD1284 for ; Thu, 4 Apr 2024 07:54:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 130D06B0098; Thu, 4 Apr 2024 03:54:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 019AE6B009C; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC6BE6B0099; Thu, 4 Apr 2024 03:54:39 -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 93FB46B0098 for ; Thu, 4 Apr 2024 03:54:39 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 69E60A1315 for ; Thu, 4 Apr 2024 07:54:39 +0000 (UTC) X-FDA: 81971087478.02.72B8903 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf14.hostedemail.com (Postfix) with ESMTP id 578FC100005 for ; Thu, 4 Apr 2024 07:54:37 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IjwSa+Am; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217277; a=rsa-sha256; cv=none; b=8fBzYmClX6B3grHQBQAcFGy3iOG9wqPQDFbiKrbuNYcpVIglnaz9zFhjuebmPJxRifwcHx uyauDSPd9XFLE13DurqVVuVhYDYt6yYtxrhEzShgDFyqy/00NpOxcRB5vDxR14amF3gbFL liFtP/Phx6+eo2/qGDd+gHjfzkAWdlw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IjwSa+Am; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 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=1712217277; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=S0hncFE+TN2V8h0Vy9nPky+8cKi/00juTYk6MbcQZr4=; b=P39rC1chWhjqlmuJaKdVVRa+buYcTGBIpL2lXgrraBMzcbWrKikPMmf4nPzcFe3XcfRS3f 9hn8zlfWy4vN6ZPHDECAC2fqk3qL0JEObMaFhtD/+dfPn3uq84hgRKZm5tQY3F9ptQDVfv DS0jnziiwQiiTAk8+iwmmHmNvEGTGYA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217277; x=1743753277; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CRNrLA7Zi25moWFEfOmiw/XIaGMGZ6Mt2vHfsiWi2qA=; b=IjwSa+Am87xY9CR1QO1nyi0Mi3r4w//7emzod17W3vpaZHKxfMbRfoU4 MaJUOj5gwYa4DMVyMkHuUZsChfQWDQL/3ePGwm1XHm8P6cYZy4ZBKGdav WePn2R26RrQNhTKB9SM+UpLHGvvfnDia8TSd0s5147IzoGs3k6bq/R7Pw dKF38NZ4crYSw+TgR+hVB8ix+LYXIsdjYR4nr+mJ8fbYQETNXUCwCqGhq gmJvcG9OAN5K0NoNMg3fKTo5qjCF+G2fPMM8FWPX+m5LtyXEJ6We0U4qm WvNskrKCXCVJmkHVt2G7gO+PFGYWQmigF8EEwvJoxNjK4Su4Aloi1KiZY w==; X-CSE-ConnectionGUID: QChKU5J/TzS42Wrs7ysCDA== X-CSE-MsgGUID: nab/+NYgTtWfr7kUhMFVkA== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450823" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450823" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:29 -0700 X-CSE-ConnectionGUID: B2+98LLfRjC4ffidreQs8Q== X-CSE-MsgGUID: IyPDF0H4TNivTfewedllvQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298778" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:29 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v13 7/8] udmabuf: Pin the pages using memfd_pin_folios() API Date: Thu, 4 Apr 2024 00:26:14 -0700 Message-ID: <20240404073053.3073706-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 578FC100005 X-Stat-Signature: 77uz7zekjwwn6w9epwpw85qeg97ekgom X-HE-Tag: 1712217277-242848 X-HE-Meta: U2FsdGVkX1//X+0RWK66Sbrpo9R+Q9omUnqOG/qBudNu3nmqJsEMIkwoxEV7QGp8QtPEHJyMQTb0ous5Uy0tNb/KpfOWXdgxeBrrwb7bzgU7BA6fiobRnsEiCrihUhrGs1KV1PLw2IAWHOHL41K/WT8lbDmrJ0Tz+1+TrHxobZLBmV0ovVxknOeM10zUGVR21WGVhmcyWUDdKls+fox/cxwgnATO+n9lkKamTpXtvnSV1f4UlbyC0M0QD4MyABzsk5sm5Bo48t8CjjQh5mn9/U7NSPw0ys3j503yeNeidsGGZ5uUvcmTFLfi+sXEGgcSIoOFQn3XJOL/HvT3GQ/CH4ErM6DXA9ZU/2ruk2U3pqWahYXLX5PF71Z5e9ZmtcookB/4ewhWJrO9+CcUKjgx8BF39ZrFYjzWzOzKZHaRaeXJ/e14CmCer7khP1ci50gR+hT7in8nMt7Df+NuV1O93ZfxNmrCOAR2hFaVb3sgBdvjX/2VtOAYAwAltmQ8mQ410nlzLiNqaciOAB8RILZQV7PconDONDJOcSlohNAJbpiuqmszCGBrNcuKSNwFt3j5hpiuRQqwSg/kWL+8tHmPx6kU0dq3tLtRvlGv5PwM/9ideiwvJVuK4xA2TwdoKxY2iys4n7Tw4evDCMUeu42vJXL/orOBibrlUYUJjaxIWTLbGvZ7J7Jl41ZlV0+3+mhbulPJW+XVIx1Llp9soOGl+QKxMQ0oq4lzGuW1WApYxeQUR6NhyBXOYk33CLE2qYVRejgMamMXl3NBP6fMvLRyoBSJzlY/8P4wMs2qtgEvAoWeD0eDz4YqZ2q5Crwh1HRqN937Z4KMU0Co3s9C1gjCmLSqA9PkIk9cBrqHRq2ZEThQecEQqQA2dzOxxg5J1B0DdRZPxklUiEZPcZ7wJpMMrAbw0NMvOjcF3xrKQExYgaN3pv7GW/Bw558beV9P4lJ2gmbr/W+yrzq4+AGWrOV f6F427vQ m1I5hChO5pSlQRToYbkq828PPffbBS6aHD+djaaiAhMq2p5aMMx3rEL+mfgdmsm9vunozBMbUwOmGOX/cC1kJP/8X0MgSN+dH40dRuBEUjXB0IhuQeT0z8whbNp0A8033BW4QUNEh8TAso8hcsva5Gz9NNPfyz9qv6kHIGYr2RC40U4TTx0MKCtEI7TRdmFg0U4LILhpeX0hKXRFy9jD6oLwfZlQAevegWzfadA53hAs+oAAoykNNXI8BFuxXxmKC3l6ZLp1SVKfhycJ83Iczj45im9qXS5b0Ve2mhG0DfBAeKSPwfeixpsmMH70s69klgdEKHDLa+J4X630Agu7g79kgMhFuArk6iFX4aXs6Fb0Tre4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Using memfd_pin_folios() will ensure that the pages are pinned correctly using FOLL_PIN. And, this also ensures that we don't accidentally break features such as memory hotunplug as it would not allow pinning pages in the movable zone. Using this new API also simplifies the code as we no longer have to deal with extracting individual pages from their mappings or handle shmem and hugetlb cases separately. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 153 +++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 75 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index a8f3af61f7f2..afa8bfd2a2a9 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -30,6 +30,12 @@ struct udmabuf { struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; + struct list_head unpin_list; +}; + +struct udmabuf_folio { + struct folio *folio; + struct list_head list; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -153,17 +159,43 @@ static void unmap_udmabuf(struct dma_buf_attachment *at, return put_sg_table(at->dev, sg, direction); } +static void unpin_all_folios(struct list_head *unpin_list) +{ + struct udmabuf_folio *ubuf_folio; + + while (!list_empty(unpin_list)) { + ubuf_folio = list_first_entry(unpin_list, + struct udmabuf_folio, list); + unpin_folio(ubuf_folio->folio); + + list_del(&ubuf_folio->list); + kfree(ubuf_folio); + } +} + +static int add_to_unpin_list(struct list_head *unpin_list, + struct folio *folio) +{ + struct udmabuf_folio *ubuf_folio; + + ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL); + if (!ubuf_folio) + return -ENOMEM; + + ubuf_folio->folio = folio; + list_add_tail(&ubuf_folio->list, unpin_list); + return 0; +} + static void release_udmabuf(struct dma_buf *buf) { struct udmabuf *ubuf = buf->priv; struct device *dev = ubuf->device->this_device; - pgoff_t pg; if (ubuf->sg) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); - for (pg = 0; pg < ubuf->pagecount; pg++) - folio_put(ubuf->folios[pg]); + unpin_all_folios(&ubuf->unpin_list); kfree(ubuf->offsets); kfree(ubuf->folios); kfree(ubuf); @@ -218,64 +250,6 @@ static const struct dma_buf_ops udmabuf_ops = { #define SEALS_WANTED (F_SEAL_SHRINK) #define SEALS_DENIED (F_SEAL_WRITE) -static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, - pgoff_t offset, pgoff_t pgcnt, - pgoff_t *pgbuf) -{ - struct hstate *hpstate = hstate_file(memfd); - pgoff_t mapidx = offset >> huge_page_shift(hpstate); - pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; - pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; - struct folio *folio = NULL; - pgoff_t pgidx; - - mapidx <<= huge_page_order(hpstate); - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - if (!folio) { - folio = __filemap_get_folio(memfd->f_mapping, - mapidx, - FGP_ACCESSED, 0); - if (IS_ERR(folio)) - return PTR_ERR(folio); - } - - folio_get(folio); - ubuf->folios[*pgbuf] = folio; - ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; - (*pgbuf)++; - if (++subpgoff == maxsubpgs) { - folio_put(folio); - folio = NULL; - subpgoff = 0; - mapidx += pages_per_huge_page(hpstate); - } - } - - if (folio) - folio_put(folio); - - return 0; -} - -static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, - pgoff_t offset, pgoff_t pgcnt, - pgoff_t *pgbuf) -{ - pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; - struct folio *folio = NULL; - - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - folio = shmem_read_folio(memfd->f_mapping, pgoff + pgidx); - if (IS_ERR(folio)) - return PTR_ERR(folio); - - ubuf->folios[*pgbuf] = folio; - (*pgbuf)++; - } - - return 0; -} - static int check_memfd_seals(struct file *memfd) { int seals; @@ -321,16 +295,19 @@ static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { - pgoff_t pgcnt, pgbuf = 0, pglimit; + pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0; + long nr_folios, ret = -EINVAL; struct file *memfd = NULL; + struct folio **folios; struct udmabuf *ubuf; - int ret = -EINVAL; - u32 i, flags; + u32 i, j, k, flags; + loff_t end; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); if (!ubuf) return -ENOMEM; + INIT_LIST_HEAD(&ubuf->unpin_list); pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { if (!IS_ALIGNED(list[i].offset, PAGE_SIZE)) @@ -366,17 +343,44 @@ static long udmabuf_create(struct miscdevice *device, goto err; pgcnt = list[i].size >> PAGE_SHIFT; - if (is_file_hugepages(memfd)) - ret = handle_hugetlb_pages(ubuf, memfd, - list[i].offset, - pgcnt, &pgbuf); - else - ret = handle_shmem_pages(ubuf, memfd, - list[i].offset, - pgcnt, &pgbuf); - if (ret < 0) + folios = kmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); + if (!folios) { + ret = -ENOMEM; goto err; + } + end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1; + ret = memfd_pin_folios(memfd, list[i].offset, end, + folios, pgcnt, &pgoff); + if (ret < 0) { + kfree(folios); + goto err; + } + + nr_folios = ret; + pgoff >>= PAGE_SHIFT; + for (j = 0, k = 0; j < pgcnt; j++) { + ubuf->folios[pgbuf] = folios[k]; + ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT; + + if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) { + ret = add_to_unpin_list(&ubuf->unpin_list, + folios[k]); + if (ret < 0) { + kfree(folios); + goto err; + } + } + + pgbuf++; + if (++pgoff == folio_nr_pages(folios[k])) { + pgoff = 0; + if (++k == nr_folios) + break; + } + } + + kfree(folios); fput(memfd); } @@ -388,10 +392,9 @@ static long udmabuf_create(struct miscdevice *device, return ret; err: - while (pgbuf > 0) - folio_put(ubuf->folios[--pgbuf]); if (memfd) fput(memfd); + unpin_all_folios(&ubuf->unpin_list); kfree(ubuf->offsets); kfree(ubuf->folios); kfree(ubuf); From patchwork Thu Apr 4 07:26:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13617398 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 F0651CD1292 for ; Thu, 4 Apr 2024 07:54:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D63FD6B0099; Thu, 4 Apr 2024 03:54:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEC496B009A; Thu, 4 Apr 2024 03:54:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF1856B009C; Thu, 4 Apr 2024 03:54:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 895EC6B0099 for ; Thu, 4 Apr 2024 03:54:41 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 59AE5A0BEB for ; Thu, 4 Apr 2024 07:54:41 +0000 (UTC) X-FDA: 81971087562.06.DEB417C Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf16.hostedemail.com (Postfix) with ESMTP id 2E1D518000C for ; Thu, 4 Apr 2024 07:54:38 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="m3v/ChUB"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 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=1712217279; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=L3pLh4fLJQF/oufy9DxeNABqnWyH70s/g2oNx/25370=; b=3W1yDtESd/mp/ESYkvpg0tlHPWeqIwAlGcAbxUc845pXzBKrDKPisIvPqb6u1uArKMY2I/ SeRGM3liXv7nSLXwA7CHtXiPO1aa9HuhgUcnlztgo2mHeo2FdubXqZxl0U9uNkmze7Pj8z v7hP7lTRLAPPHpcE8rBt1JlosKakGnA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="m3v/ChUB"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217279; a=rsa-sha256; cv=none; b=g3kPPDtQPVgZAJ23EXw+sG41J1eOcBM+m2JZpo6FLNPjC5DLt7KtUv/IPHkXTJB/R+qgfr tMU9WoSItvSwxh8gAELilFEBVvr8vwLNiBif7UHOULkgRxk06b0v/OI4VaY5pY9P+5a/kp 3oT3a/zQs49cehTzE4XPn2KA7HvLUSk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217279; x=1743753279; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vDxNOas+i9L0StB2Z+w5S+W+SfphqDLhtnXhh3tvztc=; b=m3v/ChUBzPNfwScWgyIOHCLjCAzF9rnZmkF+zZM7RqhXbk+BXb00axJx NXZbxb7HxvRql81IZvGiRMhpMArXsuH/TsioXgvdBwfCNxQ39hmvh6Ztn EH/mhVorBDw1nPj3GMG/1+zvdPv0HvRRlYb2tRreDQRghiOyrjpPmtqAb +7WQpfoBdPvyMjUKrjs8btAE3c0gaJlhzO4iZisyEeLCpOcqVTMEbBagu tTqAyHGaJri6L3n0zoAida7nXQSfCpMFuGfLAi/KfPuyJ/cOYGsjyItYj PCTjcgeezGz3fDu4uUCtdzdScsI4anq/VjZb8vxh9iuCzxB63MFvVRQa/ Q==; X-CSE-ConnectionGUID: si7hX8gzS/28qkPiAmdEtA== X-CSE-MsgGUID: rJe7UoK6R6u2LS9gihKjIg== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450831" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450831" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:29 -0700 X-CSE-ConnectionGUID: AHaE0TXUSoe6Q1ZtaDaO6Q== X-CSE-MsgGUID: 13zI+G2HRJmPlB9eYyxnQg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298781" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:30 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , Shuah Khan , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v13 8/8] selftests/udmabuf: Add tests to verify data after page migration Date: Thu, 4 Apr 2024 00:26:15 -0700 Message-ID: <20240404073053.3073706-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2E1D518000C X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 7q4x4uoe8eomx31riufiou1q79pimirb X-HE-Tag: 1712217278-735348 X-HE-Meta: U2FsdGVkX1/4G6dS61QjZryCySb3BiBm9KavpAfbTv7xhD4ga7s8Upobgoo3sXJHvkGJivUOv/yC8ugph06a7iDamyLmxysRqfSd1xbe2HIYloeNX64xDdqbvyr9Su5FLDHu9kTQGNuPTXsYzTCib3F6DZdR5k/RJ/+SF+z/tCSPRHrs4706u6NM759rHdOjdfqQb3q3A2dtNu2J+JiMXRRemsp6A/dYMKCPb/ROPdrfB+W8pRrU5LqtyKmoFN/20dFI/dRZX+oHGR76GaewFjm2LN9OScdFh2TE9rsCmZZK+/wqpj3ypMzF7RdcN4nS/CUo0YrWyc88n/RfZSUU+Mabm0NMhev8ZOqH+Rmla+30W4VzpXo6f7vRsHVsPQG1xp+Xf0Bb3Y6N6qwMrAypR7uXfcbPa7TPMwSB9xhIFRfoB04fgp6j4TLogFn+Q7TAlSJOxQ6R1Hex5EGGt9cLM/ypeuF/2ClVUiVj3Go4/wPF7lXS9czNy5HEIBf9Dt3yDwhAhKKsB2a6fWVuchxODUxN1TYx4Z19wJ1q5AF2zlstOFXWiKM8Y9+Bzb+YI6vI3jM3Pz0eoPenDM7lj9uclt2XOrqxQdB+aNabWx0jFDB5RzGDV/vlVBkbrdu3Ibx2Hv6Ij//XTM2BSqRhTwhlPYQ2aSdzTj1WO+VmTXAn1fRc+E5fVYooqKzJ3NWVt6UsbaY0qHoGIlqFa1++rBW0ToqsOlCrm1rGtIDZyDY9zztVNqanSZON67RAlxB3usfSDbM9I4jarCFur+gartBDL2LTGx4Vj5QVhUkK9Kj/w0VvMCOtpOB/cVzWM26viLNl/0nyTZNBGvdXbeIjEeeCLFM/p4p3E3D02IoGj8QBM7S0mhf8zGN+O5572CyD86icrSQCyqpDg9Vp3Ai5K5H0wSaGvPI/4e3xvAN02htU2YW0y+nmTXzDbko3hkPk9dt+OpR/+xE18I6UELqfJT7 gx2cenlR GaNPSdI2Ac8DTY4OwdUd3sbriceLlmPgoDuVoICl4Jycd7ln4VZJfxZ6Ge19g1z5wtoSRW2bku6tJfx/Ugs8y1q6kJjLRYnEfbMXtS8jD8WSMjon3Fp5hfYUePO9PtKvLKk4rZrj3koXxPo5EIQtja0+O5MRM7ID7SHNZkA0lA3DXCKeWnfztaxAzo72TLiMfyscAHGwgQLYumQtinbx+2Mi+d1uYduCkCGStJbvuBwr822Y+jQiFhncrfCqhE8MSRrxcAytScgoay8rNZ7kxKWrnbPaqIv18kY/athBMXVJzPJTwv4Pkz2Dvfg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since the memfd pages associated with a udmabuf may be migrated as part of udmabuf create, we need to verify the data coherency after successful migration. The new tests added in this patch try to do just that using 4k sized pages and also 2 MB sized huge pages for the memfd. Successful completion of the tests would mean that there is no disconnect between the memfd pages and the ones associated with a udmabuf. And, these tests can also be augmented in the future to test newer udmabuf features (such as handling memfd hole punch). The idea for these tests comes from a patch by Mike Kravetz. Cc: Shuah Khan Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- .../selftests/drivers/dma-buf/udmabuf.c | 151 +++++++++++++++++- 1 file changed, 147 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/drivers/dma-buf/udmabuf.c b/tools/testing/selftests/drivers/dma-buf/udmabuf.c index c812080e304e..d76c813fe652 100644 --- a/tools/testing/selftests/drivers/dma-buf/udmabuf.c +++ b/tools/testing/selftests/drivers/dma-buf/udmabuf.c @@ -9,26 +9,132 @@ #include #include #include +#include #include #include +#include #include #include #define TEST_PREFIX "drivers/dma-buf/udmabuf" #define NUM_PAGES 4 +#define NUM_ENTRIES 4 +#define MEMFD_SIZE 1024 /* in pages */ -static int memfd_create(const char *name, unsigned int flags) +static unsigned int page_size; + +static int create_memfd_with_seals(off64_t size, bool hpage) +{ + int memfd, ret; + unsigned int flags = MFD_ALLOW_SEALING; + + if (hpage) + flags |= MFD_HUGETLB; + + memfd = memfd_create("udmabuf-test", flags); + if (memfd < 0) { + printf("%s: [skip,no-memfd]\n", TEST_PREFIX); + exit(77); + } + + ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) { + printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + exit(77); + } + + ret = ftruncate(memfd, size); + if (ret == -1) { + printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + exit(1); + } + + return memfd; +} + +static int create_udmabuf_list(int devfd, int memfd, off64_t memfd_size) +{ + struct udmabuf_create_list *list; + int ubuf_fd, i; + + list = malloc(sizeof(struct udmabuf_create_list) + + sizeof(struct udmabuf_create_item) * NUM_ENTRIES); + if (!list) { + printf("%s: [FAIL, udmabuf-malloc]\n", TEST_PREFIX); + exit(1); + } + + for (i = 0; i < NUM_ENTRIES; i++) { + list->list[i].memfd = memfd; + list->list[i].offset = i * (memfd_size / NUM_ENTRIES); + list->list[i].size = getpagesize() * NUM_PAGES; + } + + list->count = NUM_ENTRIES; + list->flags = UDMABUF_FLAGS_CLOEXEC; + ubuf_fd = ioctl(devfd, UDMABUF_CREATE_LIST, list); + free(list); + if (ubuf_fd < 0) { + printf("%s: [FAIL, udmabuf-create]\n", TEST_PREFIX); + exit(1); + } + + return ubuf_fd; +} + +static void write_to_memfd(void *addr, off64_t size, char chr) +{ + int i; + + for (i = 0; i < size / page_size; i++) { + *((char *)addr + (i * page_size)) = chr; + } +} + +static void *mmap_fd(int fd, off64_t size) { - return syscall(__NR_memfd_create, name, flags); + void *addr; + + addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + printf("%s: ubuf_fd mmap fail\n", TEST_PREFIX); + exit(1); + } + + return addr; +} + +static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size) +{ + off64_t off; + int i = 0, j, k = 0, ret = 0; + char char1, char2; + + while (i < NUM_ENTRIES) { + off = i * (memfd_size / NUM_ENTRIES); + for (j = 0; j < NUM_PAGES; j++, k++) { + char1 = *((char *)addr1 + off + (j * getpagesize())); + char2 = *((char *)addr2 + (k * getpagesize())); + if (char1 != char2) { + ret = -1; + goto err; + } + } + i++; + } +err: + munmap(addr1, memfd_size); + munmap(addr2, NUM_ENTRIES * NUM_PAGES * getpagesize()); + return ret; } int main(int argc, char *argv[]) { struct udmabuf_create create; int devfd, memfd, buf, ret; - off_t size; - void *mem; + off64_t size; + void *addr1, *addr2; devfd = open("/dev/udmabuf", O_RDWR); if (devfd < 0) { @@ -90,6 +196,9 @@ int main(int argc, char *argv[]) } /* should work */ + page_size = getpagesize(); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); create.memfd = memfd; create.offset = 0; create.size = size; @@ -98,6 +207,40 @@ int main(int argc, char *argv[]) printf("%s: [FAIL,test-4]\n", TEST_PREFIX); exit(1); } + munmap(addr1, size); + close(buf); + close(memfd); + + /* should work (migration of 4k size pages)*/ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, false); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) { + printf("%s: [FAIL,test-5]\n", TEST_PREFIX); + exit(1); + } + close(buf); + close(memfd); + + /* should work (migration of 2MB size huge pages)*/ + page_size = getpagesize() * 512; /* 2 MB */ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, true); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) { + printf("%s: [FAIL,test-6]\n", TEST_PREFIX); + exit(1); + } fprintf(stderr, "%s: ok\n", TEST_PREFIX); close(buf);