From patchwork Thu Jun 13 21:42:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697519 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 61CB3C27C79 for ; Thu, 13 Jun 2024 22:13:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD0B66B0093; Thu, 13 Jun 2024 18:13:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C80026B0095; Thu, 13 Jun 2024 18:13:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B47956B0096; Thu, 13 Jun 2024 18:13:40 -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 967FF6B0093 for ; Thu, 13 Jun 2024 18:13:40 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3780F1A02BA for ; Thu, 13 Jun 2024 22:13:40 +0000 (UTC) X-FDA: 82227268200.18.CE99C0F Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf09.hostedemail.com (Postfix) with ESMTP id 11E24140004 for ; Thu, 13 Jun 2024 22:13:37 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kkXFLC2V; spf=pass (imf09.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:in-reply-to:references:references:dkim-signature; bh=m9a+hOIT351Wv2xQxo5iTfv/+xw1PBLrEf1P3SY0Ylg=; b=OEvSTA6Nzc2dlih4SqEV7Cz2jZixocvNiy3PxtXbV23e9iDeN/b3Qm50+L8FFubqRThXNp mdY3qeD0j293zoOgxT4EPVcXskhZx4u27qWZWYS8KTNXKL/K4mz7JInM8E9qBA9UM3/Jjv RZL3S1zGcpWBLIqHvjRdvG2LxJ43k6M= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kkXFLC2V; spf=pass (imf09.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=1niiTVAv6x+VssZgcF4KgIIWv7Nj7U+nRUVSLrnLnWO5qD9RNxjCIRkRtAi6azW/8SxVFo XcaeOk7T4+qW60i4qM02DAZumsb6viafMgyzGXV+ez3ff3u11/S9Z4qG83AO+RwRkVBgII 8u2Q1pOON0TJj7hwNQWs803cEYbluHs= 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:in-reply-to: references:mime-version:content-transfer-encoding; bh=7LmtFrOhCV/a7FkyUUWDhsbXnORpzObssqSBq1B/s/M=; b=kkXFLC2VleN7pIowOu9BRtskQtbIzRCeeFzI2Cg91kubxIZv/YAu254m Owsbq5N70pgt73maqum82o9yB9KmyKgvQr3EpPLf3xnNVRoEBe9Y8cb6J oE500zk3tW0D57MDnC+wLEwY9tkgfSf/6Suu7QzbGUVSeZ0D6rwQHIutt ToZfuAnBj/X7MN0+8gUtVjMRAXft1BMxqMVz+zU37D32qKaINEmSxktdy uKP6Yb7+oTLPePRzxRJTVCHYmrxMoPcAxL25U/nrWv/uAjUnUAIFMt3ba GaaLc5ggSNCNll0DPDNuNe0MiJJ/arB6WBYFHVZvW2YMJpdSmMz4pUoNx Q==; X-CSE-ConnectionGUID: b1rJ+DsPSUC9r5fIEcyaeA== X-CSE-MsgGUID: zvZRUaWlTlGp9S6AdJCRJQ== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720532" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720532" 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: sYdjidJQQ4KkwzTdMnR1DA== X-CSE-MsgGUID: ZV0Z0hEGQdyMKJf3YvZfLg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214091" 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 , Jason Gunthorpe , Peter Xu , Dave Airlie , Gerd Hoffmann Subject: [PATCH v15 1/9] mm/gup: Introduce unpin_folio/unpin_folios helpers Date: Thu, 13 Jun 2024 14:42:03 -0700 Message-ID: <20240613214741.1029446-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X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oR/6JUyuvHCWMubGk1MGlNX7S73uRR361108GZ/uaCAnff9aw2GjfwO2GRLYu/XWtzBWThr26kOgLF28olcErRKRFlVNeDZg14arjfxjjuzUMohjIXHUYHUDerVTcntcJkVaZDfhCdszxYcSYTWta6g3Z8ssiBCQ3IB0jptB3a0LZBPAfqKSLe5Oie+qtGye6AoqHuuoVTEbN4FC6PAhA1firtogYExY0fNVRLLck/5j1LCuW1kU9dg/RP6hWtuTyCsZs9E9Csr2iHHraCQ8/5G4qe5fHm5vi0sVZI67lsQ== 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 Reviewed-by: David Hildenbrand Acked-by: Dave Airlie Acked-by: Gerd Hoffmann 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 25b541974134..0f953405834c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1579,11 +1579,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 6ff9f95a99a7..d9ea60621628 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