From patchwork Thu Apr 4 07:26:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13617390 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 3380ACD1284 for ; Thu, 4 Apr 2024 07:54:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BDB276B0087; Thu, 4 Apr 2024 03:54:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B8AEF6B0088; Thu, 4 Apr 2024 03:54:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A52B96B0089; Thu, 4 Apr 2024 03:54:34 -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 829D56B0087 for ; Thu, 4 Apr 2024 03:54:34 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 326A7A0D6B for ; Thu, 4 Apr 2024 07:54:34 +0000 (UTC) X-FDA: 81971087268.11.B6610E6 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf16.hostedemail.com (Postfix) with ESMTP id DBF4D180013 for ; Thu, 4 Apr 2024 07:54:29 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=P6QJ9vlK; 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=1712217272; 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=3q5IyRUCKvZR/JIkto+QO8Yz1hKKeEDrSdRrm8dIWDc=; b=nRDhntd+BzkNDwGyikaS8+dBSn98S2gqzggJtHyZY5jgYjQ5Nx29uIcF1grbIzbT7aPu3r fU8UFEfhI5JEMyZFkPmD3deiBaji7M4ynrfKfjGuN3DMdYUODPMyhWZrTY+7B+wsjceO1P RVu03rP+TEBE1D3QbSBl5xsgu0wTZGc= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=P6QJ9vlK; 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=1712217272; a=rsa-sha256; cv=none; b=M8zGH30fNRkeK2QZ4W1oVXkwLk6DwTFklfZ69wQIK9vu2R7xI9o/tS3gGC2Q4/QDZoMygg mzj9yoBHPXx4U5/moo+TfSooxKOOKUg/DqsbyeI0mj7dxMw9xkf9gXNGnFwodHoVJBS8NN SILUEbjnBPqsfxXe7Ca6kBsNip/+CJ0= 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:mime-version: content-transfer-encoding; bh=Xl/YqAWAGD9u3AXZJ/rFZ3bIXu4R90MwzkSsPet4/Lc=; b=P6QJ9vlKj6PTf/ExzBMwFLtyEHWrdPlN9+QeCOKJOsRlH23H3ChdK9qK gdmf4FQe7vQfm8OmupazFzJM3HGvEiIm4EqC9XELtbvt8o+9hp4pKV867 5BTJAFy6uDLdlEEa/FR1bhE4fB3QoO9QA8jzhdPPSKz1Wo4QAeaSjRVpS 8Q6aldlEmHufiGCD49hWAd/XayofHVkJgLZAbtbqXmXn+G6a7TehnSq9p YJZ7kqeMtCW6MkQNKVtPGutfsDkbB3HIbmXHu/3T/zAk1s3A+9u2a+yAx PDhqfd6ZVOwQVM1xvFkQya1f9UR8CxqXqsXTDdmZ4bTxDqrbGdGgMNz/Y A==; X-CSE-ConnectionGUID: /ASKxa/NRbGUWsfBZt7aNg== X-CSE-MsgGUID: sWm1CgLUQ2KRwkRjplGRuQ== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450767" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450767" 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: 7FBtP+SSQmCTK3jeHOBccg== X-CSE-MsgGUID: J+YrQ6MhTdKyljrz1snQpA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298753" 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:26 -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 v13 0/8] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Thu, 4 Apr 2024 00:26:07 -0700 Message-ID: <20240404073053.3073706-1-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: DBF4D180013 X-Rspam-User: X-Rspamd-Server: rspam04 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ErtxzqVpgCBdB6qZoNrCoKPrxS39CSmLIdIB1I3nPXyosFNt8Tno0ZGWMxEWyugDwLf9fJBGsMEmh63yorbZODwhBmWTiPAmAf8A57Q3Dd1g03c25bBr7N6WA8aMWcvCPgsDX4Ace+e4amDJqxnOmWA4e24wepkhI1UjqYxpuLEzGIes9v08cIqtsjq4VrLt2EdolcRUZNeAryrgoPoaQ6BVeHGY8qx5TjRFmakZbFBGW0eHXNo351n8KuwBptxKy8h51kwEV7ogzFdKaTdxFplNStaRp2HoymH2fibqGXRqI32Xwe72Wu7jj6/KTIouWr+2z3A8+JFOp5qpjieX/6lmuET3OFzQck1aaSil6n6Y1ACE= 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 sefltests 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: 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 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/udmabuf.c | 231 +++++++++---- include/linux/memfd.h | 5 + include/linux/mm.h | 5 + mm/gup.c | 305 +++++++++++++++--- mm/memfd.c | 35 ++ .../selftests/drivers/dma-buf/udmabuf.c | 151 ++++++++- 6 files changed, 627 insertions(+), 105 deletions(-)