From patchwork Thu Jun 13 21:42:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697520 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 E4EE3C27C4F for ; Thu, 13 Jun 2024 22:13:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CBB26B0095; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A24E6B0096; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41C7D6B0098; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 251426B0095 for ; Thu, 13 Jun 2024 18:13:41 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id BCE4812033F for ; Thu, 13 Jun 2024 22:13:40 +0000 (UTC) X-FDA: 82227268200.10.F0B4FF9 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf03.hostedemail.com (Postfix) with ESMTP id 682712000A for ; Thu, 13 Jun 2024 22:13:37 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UoSSOvqk; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718316817; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=21Rm/So6PfwTAFaGs8THiodgmP7QxEnyxIvjwAIUe1A=; b=5Vvoa4NlH6vehRDNMo8A4OVHSx7IK5yDSBZgIcidChsKkLQgBJkLqZD+S8/zpqwhX3Bm6p KOK+sP7PXO8T0Vb4zJWfyBKWBpgnZv44upsgvnP0xEEpzKhJESnAe3Ezgjigea2rUTKU1Z 9RD/xXKUcxWv1ERD4wOzgUMumasSLUE= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UoSSOvqk; spf=pass (imf03.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718316817; a=rsa-sha256; cv=none; b=UVNCMapGJvBOkNTcRpZj0mSYZsOhxyBYHv9aQeZNO0BQpWN8PSQLrvhRX0opYZTX8XtsRs pozQtZ6hh2xK9cdyTmJblATM5gqyLilQaAKSoAKECw1C8heYuptguh9RWAwI7aQVAW98DH zQBcSt/b2IlcSQH7irZ+AtlQVdpE3UU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316818; x=1749852818; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=yuUIuebr9y7FqrecXTQRweoo9mv++jkQ761EA3G8Lv4=; b=UoSSOvqkUUYdFOHoI/jLiVH6X2IJoEpimemTi9d34s7j2nzO8Pak/aD7 6FqzzCWg6+D2jKQdiznaJM6fH4UmpgGljvaeoldWEHzcWhTQXcBeQfjsk 8XxHNbFgBu+uDkDIH7gjyr3Nt2FGLizvFSDjvHTL7S6I2tIbGS6e+FTzW bwSk9X72XGQqkQomJJEQMLYHzUoYQol8fyEzMqI5tvzMDUSbOmwNSt5dx W2Xt1I8Nty804erfNvakdzVA78z3ZpKqrUpoYqY7ELd8q46/Zazb/26B+ aua0Y+GhkBlB4OkNZORI9glo7UXagm5Zob+M9TReG9SvkTCwe86e2ysTS w==; X-CSE-ConnectionGUID: mct+Y7mpS3eIhYfeBONodQ== X-CSE-MsgGUID: Yw4JxkawTLOYCNwdn+ggDw== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720522" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720522" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:35 -0700 X-CSE-ConnectionGUID: OKRhEZ8YQw2JKdggKpIabQ== X-CSE-MsgGUID: bdPVqlA0STSBFwkSzUkJ3Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214087" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:35 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Andrew Morton , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v15 0/9] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Thu, 13 Jun 2024 14:42:02 -0700 Message-ID: <20240613214741.1029446-1-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 682712000A X-Stat-Signature: 83kf6s4hjf7kb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uHz+0dXUZdxyGJpaYTqa06VQoOKIbjT4r6rxSMEsKrr/mlCm+fby9i1f/wltQefSFHRxRpCTTJIQIPRC1lMYWjVuWhq9CmyOhRWjjWrqm3kET7o33hQWeg+R8fqTeJP8EIcHrKj8fAfK7HFZ6dNjg8hNSA9GEdTk7x/3jzo9jtifYiK70mIyzhMe+8yPjhFBFE4GMthIg3ud44vcndCxHudspP1gRdK+OPG/xSPsdPgkCQJHjZhq1cjI5xBstBbVvrfQ6PBO3fvpQGpxYYA70/bdU9E8GurfAezjfRvuv92hR+XqTcVS/1iNE3IsAutH7QVdAcmGq0PVyLFcUXmRB6uXlE7FJfCzP465arOubOTbv7d9BsocpN/PGpjofHpyEjpEMsSKKb4op92SeW4jeT 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: Currently, some drivers (e.g, Udmabuf) that want to longterm-pin the pages/folios associated with a memfd, do so by simply taking a reference on them. This is not desirable because the pages/folios may reside in Movable zone or CMA block. Therefore, having drivers use memfd_pin_folios() API ensures that the folios are appropriately pinned via FOLL_PIN for longterm DMA. This patchset also introduces a few helpers and converts the Udmabuf driver to use folios and memfd_pin_folios() API to longterm-pin the folios for DMA. Two new Udmabuf selftests are also included to test the driver and the new API. --- Patchset overview: Patch 1-2: GUP helpers to migrate and unpin one or more folios Patch 3: Introduce memfd_pin_folios() API Patch 4-5: Udmabuf driver bug fixes for Qemu + hugetlb=on, blob=true case Patch 6-8: Convert Udmabuf to use memfd_pin_folios() and add selftests This series is tested using the following methods: - Run the subtests added in Patch 8 - Run Qemu (master) with the following options and a few additional patches to Spice: qemu-system-x86_64 -m 4096m.... -device virtio-gpu-pci,max_outputs=1,blob=true,xres=1920,yres=1080 -spice port=3001,gl=on,disable-ticketing=on,preferred-codec=gstreamer:h264 -object memory-backend-memfd,hugetlb=on,id=mem1,size=4096M -machine memory-backend=mem1 - Run source ./run_vmtests.sh -t gup_test -a to check GUP regressions Changelog: v14 -> v15: - Add an error check start < 0 in memfd_pin_folios() - Return an error in udmabuf driver if memfd_pin_folios() returns 0 These two checks fix the following issue identified by syzbot: https://syzkaller.appspot.com/bug?extid=40c7dad27267f61839d4 - Set memfd = NULL before dmabuf export to ensure that memfd is not closed twice on error. This fixes the following syzbot issue: https://syzkaller.appspot.com/bug?extid=b2cfdac9ae5278d4b621 v13 -> v14: - Drop the redundant comments before check_and_migrate_movable_pages() and refer to check_and_migrate_movable_folios() comments (David) - Use appropriate ksft_* functions for printing and KSFT_* codes for exit() in udmabuf selftest (Shuah) - Add Mike Kravetz's suggested-by tag in udmabuf selftest patch (Shuah) - Collect Ack and Rb tags from David v12 -> v13: (suggestions from David) - Drop the sanity checks in unpin_folio()/unpin_folios() due to unavailability of per folio anon-exclusive flag - Export unpin_folio()/unpin_folios() using EXPORT_SYMBOL_GPL instead of EXPORT_SYMBOL - Have check_and_migrate_movable_pages() just call check_and_migrate_movable_folios() instead of calling other helpers - Slightly improve the comments and commit messages v11 -> v12: - Rebased and tested on mm-unstable v10 -> v11: - Remove the version string from the patch subject (Andrew) - Move the changelog from the patches into the cover letter - Rearrange the patchset to have GUP patches at the beginning v9 -> v10: - Introduce and use unpin_folio(), unpin_folios() and check_and_migrate_movable_folios() helpers - Use a list to track the folios that need to be unpinned in udmabuf v8 -> v9: (suggestions from Matthew) - Drop the extern while declaring memfd_alloc_folio() - Fix memfd_alloc_folio() declaration to have it return struct folio * instead of struct page * when CONFIG_MEMFD_CREATE is not defined - Use folio_pfn() on the folio instead of page_to_pfn() on head page in udmabuf - Don't split the arguments to shmem_read_folio() on multiple lines in udmabuf v7 -> v8: (suggestions from David) - Have caller pass [start, end], max_folios instead of start, nr_pages - Replace offsets array with just offset into the first page - Add comments explaning the need for next_idx - Pin (and return) the folio (via FOLL_PIN) only once v6 -> v7: - Rename this API to memfd_pin_folios() and make it return folios and offsets instead of pages (David) - Don't continue processing the folios in the batch returned by filemap_get_folios_contig() if they do not have correct next_idx - Add the R-b tag from Christoph v5 -> v6: (suggestions from Christoph) - Rename this API to memfd_pin_user_pages() to make it clear that it is intended for memfds - Move the memfd page allocation helper from gup.c to memfd.c - Fix indentation errors in memfd_pin_user_pages() - For contiguous ranges of folios, use a helper such as filemap_get_folios_contig() to lookup the page cache in batches - Split the processing of hugetlb or shmem pages into helpers to simplify the code in udmabuf_create() v4 -> v5: (suggestions from David) - For hugetlb case, ensure that we only obtain head pages from the mapping by using __filemap_get_folio() instead of find_get_page_flags() - Handle -EEXIST when two or more potential users try to simultaneously add a huge page to the mapping by forcing them to retry on failure v3 -> v4: - Remove the local variable "page" and instead use 3 return statements in alloc_file_page() (David) - Add the R-b tag from David v2 -> v3: (suggestions from David) - Enclose the huge page allocation code with #ifdef CONFIG_HUGETLB_PAGE (Build error reported by kernel test robot ) - Don't forget memalloc_pin_restore() on non-migration related errors - Improve the readability of the cleanup code associated with non-migration related errors - Augment the comments by describing FOLL_LONGTERM like behavior - Include the R-b tag from Jason v1 -> v2: - Drop gup_flags and improve comments and commit message (David) - Allocate a page if we cannot find in page cache for the hugetlbfs case as well (David) - Don't unpin pages if there is a migration related failure (David) - Drop the unnecessary nr_pages <= 0 check (Jason) - Have the caller of the API pass in file * instead of fd (Jason) Cc: David Hildenbrand Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Andrew Morton Cc: Daniel Vetter Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Arnd Bergmann (1): udmabuf: add CONFIG_MMU dependency Vivek Kasireddy (8): mm/gup: Introduce unpin_folio/unpin_folios helpers mm/gup: Introduce check_and_migrate_movable_folios() mm/gup: Introduce memfd_pin_folios() for pinning memfd folios udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap udmabuf: Add back support for mapping hugetlb pages udmabuf: Convert udmabuf driver to use folios udmabuf: Pin the pages using memfd_pin_folios() API selftests/udmabuf: Add tests to verify data after page migration drivers/dma-buf/Kconfig | 1 + drivers/dma-buf/udmabuf.c | 232 +++++++++---- include/linux/memfd.h | 5 + include/linux/mm.h | 5 + mm/gup.c | 307 +++++++++++++++--- mm/memfd.c | 35 ++ .../selftests/drivers/dma-buf/udmabuf.c | 214 ++++++++++-- 7 files changed, 662 insertions(+), 137 deletions(-)