From mboxrd@z Thu Jan 1 00:00:00 1970 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 99F45C2BA15 for ; Mon, 17 Jun 2024 14:42:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0935D6B01FA; Mon, 17 Jun 2024 10:42:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 043736B01FB; Mon, 17 Jun 2024 10:42:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFE8D6B01FC; Mon, 17 Jun 2024 10:42:09 -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 BDF726B01FA for ; Mon, 17 Jun 2024 10:42:09 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3AF3C1616F6 for ; Mon, 17 Jun 2024 14:42:09 +0000 (UTC) X-FDA: 82240645578.03.5EFAA05 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by imf27.hostedemail.com (Postfix) with ESMTP id EAE9240017 for ; Mon, 17 Jun 2024 14:42:06 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=ffwll.ch header.s=google header.b=Gz32Z2g4; spf=none (imf27.hostedemail.com: domain of daniel@ffwll.ch has no SPF policy when checking 209.85.221.45) smtp.mailfrom=daniel@ffwll.ch; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718635324; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=c1G/jrvdF3dCvmxuvQSxTJEMVpaOEFlBGDM8/a8dlWw=; b=kbKzFevsNzgRqZoYM0gM0Y0mXoRlrkZp525Vp3TB9OwUx2MVozItCwFr70f460ec6mUS9i i3ld3+7bNak4kbib7VaklGn919W0ZDHmd/fWvW3/7FxUxNR2ZLx1L08lwhDsgwdnUNYnTL 8ZiE8AvL2dD2kmKuUTwPHuPung7FjKI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=ffwll.ch header.s=google header.b=Gz32Z2g4; spf=none (imf27.hostedemail.com: domain of daniel@ffwll.ch has no SPF policy when checking 209.85.221.45) smtp.mailfrom=daniel@ffwll.ch; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718635324; a=rsa-sha256; cv=none; b=ifqy62jb19Sm/ln1cb1/i2TLuEFZ2SlGAoKyFtbtBNijLh7FvOppKIAQLo3rRT1UKTcj12 U6DVARzKmctJTjKW2SZg2h8/vs7OzJKBgz2rm4vo0Ef1yZPCCzxx9UJOE7E2SvSZKFno5z QBL08BvcHK5mXvGTcvf2kCU5yqfCFig= Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-35f1d407fc2so357500f8f.1 for ; Mon, 17 Jun 2024 07:42:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; t=1718635325; x=1719240125; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date:from:to:cc :subject:date:message-id:reply-to; bh=c1G/jrvdF3dCvmxuvQSxTJEMVpaOEFlBGDM8/a8dlWw=; b=Gz32Z2g4sX6ji795hsA6+Hx+0WnQatMAONjOcXhWtbS4K9agFyUh8S88pK5Y+KOWOs rM7RZe81VqP+MzdD52uTnsd8Nm9eX67Kofx7s5750tlh+xDxBsDAKjNHNTd8pZpDiRpl APloF52ORZbcldhmaGbxL2K9j9HKYh9GsbYcU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718635325; x=1719240125; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=c1G/jrvdF3dCvmxuvQSxTJEMVpaOEFlBGDM8/a8dlWw=; b=SPTshEnmoZAOxjpxxDHu4yICJ0h3W74/pdqiPQ8sKU53AtpUf432wO1TjZIoEn9r5H 1BkQdDt03jqKMsShzacDHCQK0xlq+04Ri1wFCJ1povSFaMCpI5aq4Mk+tvTuN/6FXH2+ tjlHpFZ4LPeHd0IeibqxPaPiGzgqsL7Jffkzwh2vh4WfgXC50eCJiQsrGgzCLmxheVTG JBpiwtF18la9x7y6asZJI5xyf63RBCyngUwLm/i0nEfrIxrExCCC7bCyLhFNwWljTNiK SKNS8jxxGyOiQ8U5KPpNPSXc/sffFik6+LGl1yyf7JdydIvXN5PpubjfPc149jOgBTBd iv8A== X-Forwarded-Encrypted: i=1; AJvYcCVbryIKQpLyUuWhsEJUDAM7yDV6URDLVGwYTPnjwGc6p6OWI1mShoJPeCtH9FGydZGD/xXcfNTJjbBN+zPE18EvOuA= X-Gm-Message-State: AOJu0Yyhl5f+CsTG2EAGIT/17GN8Fg1ksSMcWEEQ44BUEfawE8D1dVku 8UmR7MuWpPDRW9hcGxIfYQ3hyj0ccI43ngYlsZ2jhkTM/C3HXt8JWLa1JIya4iE= X-Google-Smtp-Source: AGHT+IFq7tlQ5YNy4T8Zg6kP8VdefpGvp1lRmKqeYDqCMXafolRWLTODU9a3M+/FPOB7MfmtlWWfQw== X-Received: by 2002:a5d:47a4:0:b0:360:874b:af9a with SMTP id ffacd0b85a97d-360874bb304mr4837491f8f.3.1718635304759; Mon, 17 Jun 2024 07:41:44 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-360750ad2cdsm12047313f8f.61.2024.06.17.07.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jun 2024 07:41:44 -0700 (PDT) Date: Mon, 17 Jun 2024 16:41:42 +0200 From: Daniel Vetter To: Vivek Kasireddy Cc: dri-devel@lists.freedesktop.org, linux-mm@kvack.org, David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Andrew Morton , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: Re: [PATCH v15 0/9] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Message-ID: Mail-Followup-To: Vivek Kasireddy , dri-devel@lists.freedesktop.org, linux-mm@kvack.org, David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Andrew Morton , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> X-Operating-System: Linux phenom 6.8.9-amd64 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: EAE9240017 X-Stat-Signature: ufbfh9zqztqmownnc4w4tpjuiosrq896 X-HE-Tag: 1718635326-803688 X-HE-Meta: U2FsdGVkX19s8Jq3haV6lpIA40u2NvBI/HXnW8fZDwVkQ/bh748xE4XjarjVOk7YwiGP/BJFr8V52Aq7j7Bj3JxcA0MtgTJDVbPOHHUhBZ5wTBlBlXR9yPcSH8A26W2XFF6MFVyPfgxd1cmKkayeYLFhbuH4hJmckEA5HHvoPVF/+uIgzgdIqivBtOW7DnqW12wWsCHBaU9SFtcwREdPHoYl+FYU7oSMdFSOuHhpPRwV/IPLt/QHzt/HaF8tlKRKchh240bsD9CKAinBcasPyi0h6bkWgbcAifMFvwMymPgF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mti5xWQz/G/MCv26KvdbsPzpMuzU/alq3jA/Na0rvaUDY5MRnOiAvbsS0ZkBqcw/i069LiiLv6401R7nKdD9PqRu+FpnTnQ7p+BFZ2/2eo2xRxPsXnKdIWFpNjs2YM5Lyv9ZObRmyJUzdoGAJWMwVZLim3G1zLRD+PWbmempmNMt3QAckzKcnqRQ2Mz391EIcYPf6ebCmweXWQ== 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: On Thu, Jun 13, 2024 at 02:42:02PM -0700, Vivek Kasireddy wrote: > 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. For merging, as soon as the -mm side is good it's imo best to land the entire pile through the -mm tree, including the udmabuf patches. My ack for that too (but I think Dave Airlie has done that already too). Cheers, Sima > > --- > > 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(-) > > -- > 2.45.1 > -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch