From patchwork Sun Feb 25 07:56:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570753 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 70728C54E41 for ; Sun, 25 Feb 2024 08:24:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92B106B00F5; Sun, 25 Feb 2024 03:24:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DC5C6B00F4; Sun, 25 Feb 2024 03:24:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77C0B6B00F7; Sun, 25 Feb 2024 03:24:16 -0500 (EST) 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 6035B6B00F4 for ; Sun, 25 Feb 2024 03:24:16 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AE3C2A02CD for ; Sun, 25 Feb 2024 08:24:15 +0000 (UTC) X-FDA: 81829638870.01.AFAEC32 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf14.hostedemail.com (Postfix) with ESMTP id 21E4E100005 for ; Sun, 25 Feb 2024 08:24:12 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XFFaG0PY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 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=1708849454; 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=N0ASp2eTXpcqNtMARKi/rRYT5jQfRa5PAC6fTGG/q5Y=; b=VCC60muGzY+F1CUCTQbEu9TmfGRqZAnW/cBjxchoHaHaTYgtZyLqP1v/AUxPWDUb6+isrP GZ6W2TpsK9/081Qw4Fmd28/jk87rCDbfGN6j3u52NLXNHU8Uyb9LzEZi52tfTwkIKJTH7I OcSAObV2XVgsrGRq2bESrSDtzGzOB7o= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XFFaG0PY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849454; a=rsa-sha256; cv=none; b=TTCKgLHuNigja4qBnb3rI+XnQdxIFJjkoHDAzWrbSnz/aHTOyXi1fH2JUv1nG2ML1D1O2x Y5khFKmZF6LRHWiZ52K8shD7PLmXqCTD0jb4kjDjOoWsQPPC2Tfr+cqlryaQPtZowG8EWT W+DzMfxnPcfuWsIAHQunXle6X+i/Cu4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708849453; x=1740385453; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=m83pTz3K2qqIUawImvfQNYywn9xfk/mrJod7gpjE/48=; b=XFFaG0PYeo7oZE4mtIsJi7jkWaHrX7ji1HtJsRor6w8nM54wtaQHe3KP yy+RJa30aLyJJknAZDC8QNlE8eUnVTTSYzu4R6QKB79ydMDiB9XMWcBFf F8x7Nuv3FNWpvI67Kgww/g7AdziJX4TLslg7RkwRAWOafWkbaXKhykYft j4YFUldZLGV0mVUbbQPeF7bMjyMvne/TZCzItwA+yhy7MegmeYRktHL0l VIfkKuTt+eOSk1dOx9DdEUcfwbGLpRjwejs4EdLVE32kRSLF65Z3zrdc/ VGYDpI2+u7wBEN2rIWzh+ILb0agZtoB21Pp6tBRI0jF7p7Y2lduSZB1W/ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988339" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988339" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783239" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:10 -0800 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 , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v12 0/8] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Sat, 24 Feb 2024 23:56:56 -0800 Message-ID: <20240225080008.1019653-1-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 21E4E100005 X-Stat-Signature: tii4ofm9rfwujg95sc9qpmwfnnp1in84 X-Rspam-User: X-HE-Tag: 1708849452-217761 X-HE-Meta: U2FsdGVkX1/wacRAe+ZPN4C1gF+ej0wdQ6zl29KcYgr9BgAX3/F8OaVOQS/jNiSSVlfeJrnvncrUrp1d7BxfX3a3gUVW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VZWKGR2puJD2hIrRmG2bq7GdjY2MtJSDucbDPyjZn230GE6TvH7JptP+uKs54LdUdb+pvxXnOE9ov7ZYMI2An3q4+oOCk+kGWp4OfqNqcdHmaxQghUqt6qFjDd2EH8U4aUm+V3ncp8dJ1N3KDw9UQqxjYu1SyKeItLpR/itmRuQ5xuStiz0dfbfvMNlp0DrUv4AGpGWoEQBdAsgBuUrYQTMX4H 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: 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: Mike Kravetz 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 | 346 +++++++++++++++--- mm/memfd.c | 34 ++ .../selftests/drivers/dma-buf/udmabuf.c | 151 +++++++- 6 files changed, 662 insertions(+), 110 deletions(-)