From patchwork Thu Jan 4 07:25:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13510849 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 B6F10C4706F for ; Thu, 4 Jan 2024 07:49:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D85506B03B0; Thu, 4 Jan 2024 02:49:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C49F56B03B1; Thu, 4 Jan 2024 02:49:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FF666B03B2; Thu, 4 Jan 2024 02:49:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 624596B03B1 for ; Thu, 4 Jan 2024 02:49:00 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3A4DF1C09A1 for ; Thu, 4 Jan 2024 07:49:00 +0000 (UTC) X-FDA: 81640852440.27.655CAC6 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 40E634000D for ; Thu, 4 Jan 2024 07:48:58 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gFYeQ6BK; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf12.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 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=1704354538; 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=OFWxI1i+p4efvmikRdB46knSq10ZwkVaViiQ0SsvQMA=; b=YlUTd2Z+cHH6Q5yW6B+ya3zZryoGjaU/IVuz0hL+x3ARbWUVMZ30jVkNRsWMlxlGcxlkmf Vs6JyKRbb3c2dhpYKhfo9WVGkrUPRMSbqSBuLYPrbJ/eAJ5liLd/A06anhlq5Jw1AL9HN2 +ge3YjekF4dDxDFrzU/LGRV+Q1sQioY= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gFYeQ6BK; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf12.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704354538; a=rsa-sha256; cv=none; b=j4gtw8KvwlUdfzY0tlK8WPSqqY/E2nKWvXTlZWUpqt/nWGYsnMEmzSiGH837/LHQHzV99l wnzFh6G2G7g2W6tyjWcNhfvq42hZGJ46KH9cOUtNn28L+zr6PbVN2cE1LfJvWmlh8wJCl8 zDiX9oWTnEcadyJqLU6BBS//xzfewCQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704354538; x=1735890538; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U5Op4CjhDDAHfKtiDzpe6+9a1b7R8CllBxVTE/TesJE=; b=gFYeQ6BKPLLHTYZwlHyJaISaTN022SeeTGXd9N/eW5zS/T+Z9H2J8xJW 3eGPUIKSJW9IqOrGvjSjhiO5wrlJv9IiWRVCn/9EXpT0gRiAFGJuePDKx rKa7o3a01HpPbBQwu7UsB4Y1VfAD+KdYF+bFBLGBBJOLxh0Pjp/FuvLON Szhi+vh5oj/eF/HABprsjKPqyNq6CsNYzeIVoPbw0SUbBoongCzQckpy8 e79mOYujERExktuLPkyIqNLVorIvEIAgBWxJSgkhwm4Sfdv0Nvt7MfqRg sEsmzeEZpT8FGENWQ0dbDyG0vIyCu4fsE2EZLEge+LTEdc9idMqpHW1G6 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10942"; a="483331095" X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="483331095" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="14776719" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:47 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v10 1/8] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Wed, 3 Jan 2024 23:25:11 -0800 Message-Id: <20240104072518.807950-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240104072518.807950-1-vivek.kasireddy@intel.com> References: <20240104072518.807950-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 40E634000D X-Stat-Signature: k74rz57hcbp9p4jaufm1dd1tezfajzuf X-Rspam-User: X-HE-Tag: 1704354538-838865 X-HE-Meta: U2FsdGVkX194P4A0h9rWlHO5+OreLW1BWEquxVjO88VCKqI13QhSEPpoM23N10NDhM8woovEqWh9RFjIAj3h0X1vw1++uTUN9L/SFouLDSqRUdbEK5QWSEIPSy5nuOQYmyua6fNkMOTY2usaPE9IkwIEIrYfMMEYKBagAOaSdOoKtf6Gw4oUUy5DaMd5WGD4s9gD9xsEVBgxhrRDMlBxfu/7euDm2Pe6XOmIvcxhzqQvH1blYBdPwEDbL4DmoNNMI5v3+78d9XyQbW+meGbovfrn8lUBjPr5IoFEGBpwPlLq04ZYSVVSq1D76yOsMO7hpUuKNFXyuj8Cme59Z1F6hsvDz4IgsV1NLEjTpqBFWd1hRI3Ad9QX2S9dJ0UMnAV3P8M22e7agZN4uDRgHSUoZbBkJiZVyWah+rmirwL5653RlapJ4IUnOUXpm2W0VU1yetKB8E8jWZegN1KG8bVXIy5rYhuAFZ/Sq1nL7/C7m0YE6vey+JN54tgMcS2iWJ3tP1PWpNU9jEoEWDV4z896uUOfoeud4W7Yyj/HhHPQJFBOOZNxb1uHrVNbXARmrnDHjLEjzvhjpDsBixqxf5Bh74p9OAY3zN2aGYPXqDnOAY/6S6yQ5buU9wG+gL4VXqMRAHmxlBoLOXdS0buDCEuBU9lYhYMEoGN43yzX9nLn/Pv0nXFvkbI6WTkHp8EwA9YRY97Ep/yg7JKxLSA8MOnwMmcZfQMrA2oRrsYAiRVHfX2xjAVJzoDYdz/A/9wWeZU224AAxooSSCqk5cQ1w1uZgHI9Cnly8I3PEmQv0a3/m4tP/Tgqql8CTLfp9+DUPaBB5/ULsX5pQXwwpatk560A1KkmuETvae/Z3e1N27gsO6Di7XrKitTR7K5hcGIvDXA6fJlx6wjU78bHz/9HRm+rBsN1mT3BIPBWNSKL/0e9D+MCsmx7Kft2h8VOBzjZZEWYhcInfcbiy6gKZ6TM8Wq URSdE8kv G7O5oWJgE98jmqlnGz/tp/P5WG1N8fEOADpzOdqU/IkvpAF+C5oBAjd3qVcKgeuNK1K0ZE7QXxl5eABnzSmTqFzp9fzT+RBg9//eWzUunZ+iLNpVRmM8t4jQmFFCBHTz+WAL2WEN4yQMFC701+Tk3SH2s0s2D+7Jisaepb0VGStU7v+zkfPFBe9M7J0OQTOIo96dyvcq9m6R8WnhUPrUAPAXMbBWYEY+uSPu4Yt0CLg8H3DGA318YLXZIGA5UzUfbYRlpV3e2UQ8SxS/cVgrLDF7huAlT5yTAmPxrzbfZRcU5IFo= 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: Add VM_PFNMAP to vm_flags in the mmap handler to ensure that the mappings would be managed without using struct page. And, in the vm_fault handler, use vmf_insert_pfn to share the page's pfn to userspace instead of directly sharing the page (via struct page *). Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index c40645999648..820c993c8659 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -35,12 +35,13 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) struct vm_area_struct *vma = vmf->vma; struct udmabuf *ubuf = vma->vm_private_data; pgoff_t pgoff = vmf->pgoff; + unsigned long pfn; if (pgoff >= ubuf->pagecount) return VM_FAULT_SIGBUS; - vmf->page = ubuf->pages[pgoff]; - get_page(vmf->page); - return 0; + + pfn = page_to_pfn(ubuf->pages[pgoff]); + return vmf_insert_pfn(vma, vmf->address, pfn); } static const struct vm_operations_struct udmabuf_vm_ops = { @@ -56,6 +57,7 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) vma->vm_ops = &udmabuf_vm_ops; vma->vm_private_data = ubuf; + vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); return 0; } From patchwork Thu Jan 4 07:25:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13510847 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 E9A22C47074 for ; Thu, 4 Jan 2024 07:48:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5459B6B03AE; Thu, 4 Jan 2024 02:48:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CEF56B03AF; Thu, 4 Jan 2024 02:48:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D4436B03B0; Thu, 4 Jan 2024 02:48:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 187946B03AE for ; Thu, 4 Jan 2024 02:48:58 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D3C92402B7 for ; Thu, 4 Jan 2024 07:48:57 +0000 (UTC) X-FDA: 81640852314.04.908DD6C Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id D700B40002 for ; Thu, 4 Jan 2024 07:48:55 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Ep1xKDEd; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf12.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 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=1704354536; 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=gnFFCZ/4N7bQ8jwcsO7jGvxgDXP7BPVbDrHLX60MqX8=; b=a6a5+HNxNOwq0RxecyHs76HhUJ7y63qUZlW1vWim5YuepWMJhjObHoAAJbCWZNpqTUoT5e tu6rr1T2XQHl4nYHloy462xrZ4EIji9OejIFumAPP3KxTPVz4jp4xCTwZGAdTdkU8zPW6S Qv2NOfZas6LPFhRGnnzVtO3gspSx630= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Ep1xKDEd; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf12.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704354536; a=rsa-sha256; cv=none; b=g6P9ureR9KcnxxOGMc4qo9gtBK0qPrnQLBLjzVBWA22Tumm/c9R61Oyc5hkxXAcEoqusWV MCP4r4SIbZht2k+034vFsAVOAhE6qyrtTVwgE0uuxn422UETz2EFiRYcAw3vWs/6YwrLrl lwb+Rq2owCJxWAIZ6y6FgXNCPdJqgTg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704354535; x=1735890535; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O1c3JIhGATN1dxcRTvUuRK9RhQSZEkJPgfU5c33zkAo=; b=Ep1xKDEdk/dQEp4guL551hQ+GUzGp/2Mh4gzDlB+dFmZVu+/RWeIiMM3 Y/hpJQx/MGL7bts4xUhlCVhv3KdkuvonTEzBfxY45bkeUpWobR4apssYE M6nEVFLKGlBsdHquu1HTCJcq1/63aOWL/GjUeRPhHgDakHuOtyE/O01tQ QI5LITnQ2e596GgDR8fpOJy24bm9GYijDBAFw+5T2RitusuRMDBZ+sPLP e9MrarJXm9dPPLhOlUQBBMKKReqcFxkj1YEB4h0abQx1MYMRLslnc1VQC Y/dtG8+XFELCSsPudAEnb4DIil7h2DdxJWFoViSQRDqiuu+qtUsXlhaxq g==; X-IronPort-AV: E=McAfee;i="6600,9927,10942"; a="483331112" X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="483331112" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="14776723" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:47 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v10 2/8] udmabuf: Add back support for mapping hugetlb pages (v6) Date: Wed, 3 Jan 2024 23:25:12 -0800 Message-Id: <20240104072518.807950-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240104072518.807950-1-vivek.kasireddy@intel.com> References: <20240104072518.807950-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D700B40002 X-Stat-Signature: xb8egrn1x8eck6fryn96cj6zi6snj9tu X-Rspam-User: X-HE-Tag: 1704354535-933697 X-HE-Meta: U2FsdGVkX19ONMp7M9Gd/1BW5m2ymx2qPeFGfGYBO/O0h9VvfL3x2dSQM5pyGDjvA3NbZw5jUh8l92avr8lHgopbKlXPT91GNC32olfxxhBgjqOTYeWU1YvZnTKZuz5WNCyz0YojhgONc6Yy/Q/ZzDXdd9On5AMaNfmtTVdjhgltm8lf12CscPaKhtDYckroCLaiXY8z4jGHZn6IqfkJt48MXJadshMLTGkh4UQEePh+zUcV1A+c6MyrUIZodrTbVnVZNuDeUR8mzoClyhI+9CnnK+zq1MT2htw0XCesdaV8g+cmQiRfn2MVXS8dpqZGcg3FRVREXcH8oJlawfvARCNMCw4WOdW65tqjqaA3FmoSgALuxLzQnNo6mMHqcTM2PoEoV1/Mm5BAcLSK/wmcUcvkaL+fzxQ9QVA0YEs8Nkh36sVO6o6jKMbYSso0ebCN80DpXZ3fA74/EOf9g1jYcxJPMQZjBcvugU7d9t0sPzilExqIlRO0vKpM6O0nr6PuwdAz6y+gRSyUa+V5oYeORseyZpWpgomSuLP7J99od/eEjmKSUqtXbnoGneiwRCDShAz5r3p8qHu+23ce1wWzkmykcBeoIQO87bkMkkFlQ2RGzUylEqS3Kruvi7eF3WCf4D+7mN5B3gjQwoOhuYUi5l+kOjmsoqsqNOVlb1SABFZPp41chNSQ1TRKc5bH0W+akmk97KM8JpdZwMUYiuT/EWzzEqXiMRywDWa6AigMYrtZ268077DDuWwHMy31CA9tvafu6BfoNnCjA7od+Qu7nHZruXrGeK+7CTD0nPCkHGxMZDwMiyOe/5ttkb2+dNqGteVAmkS/FaGSnyD176rY5FrhAUzNC8BHAdLw8mGBbRUj5M50sOHfQWgJ1vNUG9HfP6TM7VXs8CrhA3I/oru7AGstjfnBVQn7jA9dFMdUMFnMhZYeGv+UkwqHDSX+5yRQpr1veoAHM8XurFHxiK8 JbMmUGFl MLwRwjuYd2jJNzY1vREPy4gZDgCDw9YM7oBQBfcx8elZlSwj/ltBU61aMqFkM5PHrm0eY247IIqtdS4XeIvZSUBUT1ojUO5hSF0hCYmkjroKm4vjqOOEXaHeaTFhzBD9KVzfRYUxPxPHHSrzfOUiyCG6gV1vZ/eMNIXxv3u0rPKI9N9FunqG6Fw5ne1gmnwoohviKLEmoyimBvHIOwg115ipJbLD3AJukqhx5tBtDAlFI6wF0T8wLjcNS3qoTM1O0Nx5R 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: A user or admin can configure a VMM (Qemu) Guest's memory to be backed by hugetlb pages for various reasons. However, a Guest OS would still allocate (and pin) buffers that are backed by regular 4k sized pages. In order to map these buffers and create dma-bufs for them on the Host, we first need to find the hugetlb pages where the buffer allocations are located and then determine the offsets of individual chunks (within those pages) and use this information to eventually populate a scatterlist. Testcase: default_hugepagesz=2M hugepagesz=2M hugepages=2500 options were passed to the Host kernel and Qemu was launched with these relevant options: qemu-system-x86_64 -m 4096m.... -device virtio-gpu-pci,max_outputs=1,blob=true,xres=1920,yres=1080 -display gtk,gl=on -object memory-backend-memfd,hugetlb=on,id=mem1,size=4096M -machine memory-backend=mem1 Replacing -display gtk,gl=on with -display gtk,gl=off above would exercise the mmap handler. v2: Updated get_sg_table() to manually populate the scatterlist for both huge page and non-huge-page cases. v3: s/offsets/subpgoff/g s/hpoff/mapidx/g v4: Replaced find_get_page_flags() with __filemap_get_folio() to ensure that we only obtain head pages from the mapping v5: Fix the calculation of mapidx to ensure that it is a order-n page multiple v6: - Split the processing of hugetlb or shmem pages into helpers to simplify the code in udmabuf_create() (Christoph) - Move the creation of offsets array out of hugetlb context and into common code Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Acked-by: Mike Kravetz (v2) Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 122 +++++++++++++++++++++++++++++++------- 1 file changed, 101 insertions(+), 21 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 820c993c8659..274defd3fa3e 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,7 @@ struct udmabuf { struct page **pages; struct sg_table *sg; struct miscdevice *device; + pgoff_t *offsets; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -41,6 +43,8 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) return VM_FAULT_SIGBUS; pfn = page_to_pfn(ubuf->pages[pgoff]); + pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT; + return vmf_insert_pfn(vma, vmf->address, pfn); } @@ -90,23 +94,29 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf, { struct udmabuf *ubuf = buf->priv; struct sg_table *sg; + struct scatterlist *sgl; + unsigned int i = 0; int ret; sg = kzalloc(sizeof(*sg), GFP_KERNEL); if (!sg) return ERR_PTR(-ENOMEM); - ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount, - 0, ubuf->pagecount << PAGE_SHIFT, - GFP_KERNEL); + + ret = sg_alloc_table(sg, ubuf->pagecount, GFP_KERNEL); if (ret < 0) - goto err; + goto err_alloc; + + for_each_sg(sg->sgl, sgl, ubuf->pagecount, i) + sg_set_page(sgl, ubuf->pages[i], PAGE_SIZE, ubuf->offsets[i]); + ret = dma_map_sgtable(dev, sg, direction, 0); if (ret < 0) - goto err; + goto err_map; return sg; -err: +err_map: sg_free_table(sg); +err_alloc: kfree(sg); return ERR_PTR(ret); } @@ -143,6 +153,7 @@ static void release_udmabuf(struct dma_buf *buf) for (pg = 0; pg < ubuf->pagecount; pg++) put_page(ubuf->pages[pg]); + kfree(ubuf->offsets); kfree(ubuf->pages); kfree(ubuf); } @@ -196,17 +207,77 @@ static const struct dma_buf_ops udmabuf_ops = { #define SEALS_WANTED (F_SEAL_SHRINK) #define SEALS_DENIED (F_SEAL_WRITE) +static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, + pgoff_t offset, pgoff_t pgcnt, + pgoff_t *pgbuf) +{ + struct hstate *hpstate = hstate_file(memfd); + pgoff_t mapidx = offset >> huge_page_shift(hpstate); + pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; + pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; + struct page *hpage = NULL; + struct folio *folio; + pgoff_t pgidx; + + mapidx <<= huge_page_order(hpstate); + for (pgidx = 0; pgidx < pgcnt; pgidx++) { + if (!hpage) { + folio = __filemap_get_folio(memfd->f_mapping, + mapidx, + FGP_ACCESSED, 0); + if (IS_ERR(folio)) + return PTR_ERR(folio); + + hpage = &folio->page; + } + + get_page(hpage); + ubuf->pages[*pgbuf] = hpage; + ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; + (*pgbuf)++; + if (++subpgoff == maxsubpgs) { + put_page(hpage); + hpage = NULL; + subpgoff = 0; + mapidx += pages_per_huge_page(hpstate); + } + } + + if (hpage) + put_page(hpage); + + return 0; +} + +static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, + pgoff_t offset, pgoff_t pgcnt, + pgoff_t *pgbuf) +{ + pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; + struct page *page; + + for (pgidx = 0; pgidx < pgcnt; pgidx++) { + page = shmem_read_mapping_page(memfd->f_mapping, + pgoff + pgidx); + if (IS_ERR(page)) + return PTR_ERR(page); + + ubuf->pages[*pgbuf] = page; + (*pgbuf)++; + } + + return 0; +} + static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { DEFINE_DMA_BUF_EXPORT_INFO(exp_info); struct file *memfd = NULL; - struct address_space *mapping = NULL; struct udmabuf *ubuf; struct dma_buf *buf; - pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; - struct page *page; + pgoff_t pgcnt, pgbuf = 0, pglimit; int seals, ret = -EINVAL; u32 i, flags; @@ -234,6 +305,12 @@ static long udmabuf_create(struct miscdevice *device, ret = -ENOMEM; goto err; } + ubuf->offsets = kcalloc(ubuf->pagecount, sizeof(*ubuf->offsets), + GFP_KERNEL); + if (!ubuf->offsets) { + ret = -ENOMEM; + goto err; + } pgbuf = 0; for (i = 0; i < head->count; i++) { @@ -241,8 +318,7 @@ static long udmabuf_create(struct miscdevice *device, memfd = fget(list[i].memfd); if (!memfd) goto err; - mapping = memfd->f_mapping; - if (!shmem_mapping(mapping)) + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) goto err; seals = memfd_fcntl(memfd, F_GET_SEALS, 0); if (seals == -EINVAL) @@ -251,16 +327,19 @@ static long udmabuf_create(struct miscdevice *device, if ((seals & SEALS_WANTED) != SEALS_WANTED || (seals & SEALS_DENIED) != 0) goto err; - pgoff = list[i].offset >> PAGE_SHIFT; - pgcnt = list[i].size >> PAGE_SHIFT; - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - page = shmem_read_mapping_page(mapping, pgoff + pgidx); - if (IS_ERR(page)) { - ret = PTR_ERR(page); - goto err; - } - ubuf->pages[pgbuf++] = page; - } + + pgcnt = list[i].size >> PAGE_SHIFT; + if (is_file_hugepages(memfd)) + ret = handle_hugetlb_pages(ubuf, memfd, + list[i].offset, + pgcnt, &pgbuf); + else + ret = handle_shmem_pages(ubuf, memfd, + list[i].offset, + pgcnt, &pgbuf); + if (ret < 0) + goto err; + fput(memfd); memfd = NULL; } @@ -287,6 +366,7 @@ static long udmabuf_create(struct miscdevice *device, put_page(ubuf->pages[--pgbuf]); if (memfd) fput(memfd); + kfree(ubuf->offsets); kfree(ubuf->pages); kfree(ubuf); return ret; From patchwork Thu Jan 4 07:25:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13510850 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 B5848C47073 for ; Thu, 4 Jan 2024 07:49:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D17E6B03B2; Thu, 4 Jan 2024 02:49:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 440BA6B03B3; Thu, 4 Jan 2024 02:49:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2620B6B03B4; Thu, 4 Jan 2024 02:49:02 -0500 (EST) 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 09D9E6B03B2 for ; Thu, 4 Jan 2024 02:49:02 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C40511A02E4 for ; Thu, 4 Jan 2024 07:49:01 +0000 (UTC) X-FDA: 81640852482.14.12404CF Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf13.hostedemail.com (Postfix) with ESMTP id CFB5B20005 for ; Thu, 4 Jan 2024 07:48:59 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZKazk8Wn; spf=pass (imf13.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 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=1704354540; 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=L3aK1QrFo7Sdzuln5p2RzT/mh+0rmXHOTlX5IlIL9BY=; b=xSahFrNsPSl7Y/z45U6JjjU2qztgYJiZ/tMk7awRF4aTsXrzRlMDIunqKJ2X6sqH1PvgmX i5SoNYcD8j8LIJUrqnnyXBjOK0jHBfK2RKFFLNYmT3433OVWIzVgx0jv6f0fL3Ky1uyjHW n1FCkET8ir8LUSskk6vR1AV30zDjLLg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704354540; a=rsa-sha256; cv=none; b=JDIORuBRp6f1ttMsE2I554GXYI94dYDayRJSvJn8SS9TFzzckIp1iMKlk1JLQXR/Q+KQMW ZBgldK6ozzYBdCVA0w8cC/J2eiloGxQplJMDt/ieLE6p+F3gYc1J+5+PEY8naW8Ml6nsES XPZOaQUZJORkGTbc0wUkwa1qhQ/noEs= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZKazk8Wn; spf=pass (imf13.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704354539; x=1735890539; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ACMBAqooHk2c1KcIOvZNkowcRiJJyIDQFFF7e9jD5SI=; b=ZKazk8Wn6XuheAtKcD4x62CnQvkEhgFaG5/YkXm2I+45LSFmMw7fWfcd 0/qYHh16WOAQi9EMhUTIXaviizpgSLMf6br3MvM5ZFLQPCmg+vpxOup2P h1ZDH9AkX1q2TS4QW3mm+wyEAfNWd+J8QU/z1CGKK9tLxS/MkzdkFVPq2 PeRY5YDJNOuQehbdgNu/cRmieQ37YkBnHWLglI8Cm5rqUFKQjdzG3t1or m9zScig9iQSisYrp450xzQZ8DUWOAbuELGE583MEpBmUyfyjAFbyqvqxm bbDekSwyEva4syaE+AyCpX8QDEkVKlB3qyqROYjViGwEgkadVkmCR7nEN Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10942"; a="483331150" X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="483331150" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="14776726" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:47 -0800 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 Subject: [PATCH v10 3/8] mm/gup: Introduce unpin_folio/unpin_folios helpers Date: Wed, 3 Jan 2024 23:25:13 -0800 Message-Id: <20240104072518.807950-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240104072518.807950-1-vivek.kasireddy@intel.com> References: <20240104072518.807950-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: CFB5B20005 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: sj6dotj39fpszqwwhe1t7uiyzzx6d7ca X-HE-Tag: 1704354539-703018 X-HE-Meta: U2FsdGVkX1+wVWW6aKE/baUxdEilWe1HTEeZgYz5GAsU6+Fd/XoywiYw72QZdQGxrbVfPtSnou19ONaXPVvZu4PWgibGMY0J3YddlSXGKygODRo7EzaZkfHiE6fDATKrllruCRuLKSi+HGnRHr2OOjwgFFMiyuJNMkl/XsZsQmXv3UH4YvmssoORt7S/1msOZdxDHhudGYJeqET8+2RDccXxm7Uuaw7oausjX0DOjA//mGsJYVg3CW1alsbUGyFuj4uQcypAnzQOFT7NX0sSezJI8hwraXp7e3PQ6t9SuigyeGEBn2y1RE6+5Y7E8j1K6xZs6L66MsKGVRrhvTuCjRASiQe2Xg26jiS5eEqyv4lTLplrL5hFwnRq8y/iDxazwaHY/YvXHbB7qrW+szltN3qHwW/i5JdwNTIKJ/15zhjFBxW8xduDqBzufWCWqWtZ7ikcLT4NwuPwBEos1fCwF/fw1m1LVmcCoNt/Z1oNzgNPuIUnIk25iiHokr8kfgH4+eVWR77PhUNQJu8ZjoEVU3AUKyHqGXyeyKd/Y4pheWCkaP/wlGfg2N5vUevVJ4KpLNksiQna//UsFBIX4fSHqpC+Mh8wm+RxskH8tkMNhSMxRhcwrVXbWJwvIMdNqcP1tSluF9nIJ0CNdqYZa52NNbNWPfZPJ5eLbpgbcFHH7rhzwi/W+lblM/3TE/FvjmYgagdntu9yFjZ/zQL7UJcI43zx621yi9VKWP03g8RwjZUMUfSMRjdMl8HE/ESjbOvV15E8a2H8s9McFWDynU5+Otathq4IWgr1Ow5cXLNd7gebi7swdSZQyWofxuc/RMDI4RkzUqoBwQY80x3YArGQ8zS4jyleGRlT+SIFmXlh2nr5CwtsIT882t+NEsa4XJK3mwEuRFkxlTQAsNChJUdA6zDkreXYVeK2zsYL14qByUDAC8bfusBrOhr97AJLESYQF7SkZe+3iZLL54KJEqn eHYp9fk8 A161qqDvVfaSIsq0iSKdan3jFogc/Mzqp55odGn3tuAprjv8BM91EjLdsX+vXBwTXW5yHObLElGBGvhunhcwZIAVeIMU1opPhlqkdIU/DhgAP4In96Jpl4FCOWndsnJi6qSGLlzHfVSdsDVfLiaEZc/xKP7BnHjf7DJgSJSAT3pmjFzQteOWDVTJWj4FnRzAmSKMaSPpavhyiV67hGhpDX8e3q+nYnezmZrc5mO3Sb46iA09lOsB+Qvdnm/J7JLLmLQ+Mz+9u8CuHYuuRh9K7ThWIWV9RMTxHRoXfSz3TV8clUYzp70FkF4cmVg== 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. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Peter Xu Suggested-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- include/linux/mm.h | 2 ++ mm/gup.c | 81 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..4dbfdd277014 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1585,11 +1585,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 231711efa390..4d7bc4453819 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -30,6 +30,23 @@ struct follow_page_context { unsigned int page_mask; }; +static inline void sanity_check_pinned_folios(struct folio **folios, + unsigned long nfolios) +{ + if (!IS_ENABLED(CONFIG_DEBUG_VM)) + return; + + for (; nfolios; nfolios--, folios++) { + struct folio *folio = *folios; + + if (is_zero_folio(folio) || + !folio_test_anon(folio)) + continue; + + VM_BUG_ON_FOLIO(!PageAnonExclusive(&folio->page), folio); + } +} + static inline void sanity_check_pinned_pages(struct page **pages, unsigned long npages) { @@ -52,15 +69,11 @@ static inline void sanity_check_pinned_pages(struct page **pages, struct page *page = *pages; struct folio *folio = page_folio(page); - if (is_zero_page(page) || - !folio_test_anon(folio)) - continue; - if (!folio_test_large(folio) || folio_test_hugetlb(folio)) - VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page), page); - else - /* Either a PTE-mapped or a PMD-mapped THP. */ - VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page) && - !PageAnonExclusive(page), page); + sanity_check_pinned_folios(&folio, 1); + + /* Either a PTE-mapped or a PMD-mapped THP. */ + if (folio_test_large(folio) && !folio_test_hugetlb(folio)) + VM_BUG_ON_PAGE(!PageAnonExclusive(page), page); } } @@ -276,6 +289,21 @@ 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(). This is so + * that such folios can be separately tracked and uniquely handled. + */ +void unpin_folio(struct folio *folio) +{ + sanity_check_pinned_folios(&folio, 1); + gup_put_folio(folio, 1, FOLL_PIN); +} +EXPORT_SYMBOL(unpin_folio); + /** * folio_add_pin - Try to get an additional pin on a pinned folio * @folio: The folio to be pinned @@ -488,6 +516,41 @@ 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 unpin_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; + + sanity_check_pinned_folios(folios, nfolios); + 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(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 From patchwork Thu Jan 4 07:25:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13510854 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 37E8EC47074 for ; Thu, 4 Jan 2024 07:49:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A28A86B03B6; Thu, 4 Jan 2024 02:49:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BB956B03B7; Thu, 4 Jan 2024 02:49:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DE7C6B03B8; Thu, 4 Jan 2024 02:49:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4594A6B03B7 for ; Thu, 4 Jan 2024 02:49:04 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F1E1E140999 for ; Thu, 4 Jan 2024 07:49:03 +0000 (UTC) X-FDA: 81640852566.05.B24CC24 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf13.hostedemail.com (Postfix) with ESMTP id E421920018 for ; Thu, 4 Jan 2024 07:49:01 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KEHEkBQv; spf=pass (imf13.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 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=1704354542; 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=i0zAFHA+E4FLBv78drUCnI4hJVhfpf+CECYBKELK8Yo=; b=MLtHumsQFF3EotWjncEcz8ZSxvzaprXN/YL3T+sKXrWcgtCwm0+AeseyP6gSBLLBB8kcoP A76iLhY1P5m/VG4oaqonDLOPFbBmHu+GBLzku0H1QODtmz8GxA505DL3+6vw4fV84Bazcr SQCqtHaQjp5OtgQ5M1QHY2FN7uyz9Vs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704354542; a=rsa-sha256; cv=none; b=X/lZknNhy2Wc8izmV7twNpWNZMhNSYf7b5crsgTJe4cd1K7wUuMJUmoOPX6rf/+Z4ibnuJ on1d1JIbDav4rGXB9UxOROO/Ap3XDTdv8LjoeDrNcuLkFhzjZZRoEg/a73joaYJr9FxbIe Vh7nFwTgiGHJwxTkpOXK2WcPISHsCkg= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KEHEkBQv; spf=pass (imf13.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704354542; x=1735890542; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O22F+haVAFNBQJcPOK3PWw+AnkziG39TSsVTrN+ZPik=; b=KEHEkBQvfitSLgWReu3H+DtWJDQnhFtKPuBZfXp8XmAzZoRoESxP4JdC J+QS+DUivKcr357qed7e9UxECBOWAW4LPFjXKl5SIiK8ri2HzJ9rxMZcZ QX9IM2FUa6XfIJ5hSIH/EprE1Egyueaq097l7K9MWSbNjzwuoUXdr0YEO /iM7aoYF9BGiXXFsIEWbhN6FQ/Uw7hMgd7js2kxiSmzewSCI8BpNK1+HU OMZyOHbJW6YtIrbvxeZ6ZMn2RwfqY1HC5dTU+xXJ/25okd2jyXD8naFbE VysYilO5T1SSmVJUVpVAG2xRSwtyUnyttyPpvOlfPFlXFvycr2/W3/H8B Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10942"; a="483331154" X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="483331154" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="14776729" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:47 -0800 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 Subject: [PATCH v10 4/8] mm/gup: Introduce check_and_migrate_movable_folios() Date: Wed, 3 Jan 2024 23:25:14 -0800 Message-Id: <20240104072518.807950-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240104072518.807950-1-vivek.kasireddy@intel.com> References: <20240104072518.807950-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: E421920018 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 9kf75fyuwps76txmufhyo8tatt4okngp X-HE-Tag: 1704354541-154163 X-HE-Meta: U2FsdGVkX18xT8TvBY3oPlC+eCoFgys5h64x9TiKNx0/Qa5YFV/pYc+KO4qZBzo8plcN5wz9Aj7SrEUErd/pp8c8nlQ1mLbNBLL6bxtM3rAgOapIsHz1NHWvyt3RxcVran+XgHXhzcfneyeLAnwUDxDdp8RGH+qp8PJsdn6DyaUmtgKrbrihGds2pgRzCPBgTNfdbrkYJrPaldDx+N0rr5aT4ei1RdJE3HN7RiUTqr7RmhBPnejs8S5YSXqKbeeXVnk/hBtbjZwnHn8RgxOmvFIw8ikGUojwK8Wedj8YDmgV1boYVXSIImMzBNWpdkUG2hv9KSE2otnBSJrSFPxnok2B23rQpYnvjz+kIV2DKJCy+vNsAHcP05IrHoA55BOm7hAuOveveM0aP3jtBQc+Se7JBL4ytf2P7VU0o5zWkt2cdMDtL+5yjs0T4HpSpKSI+5J57A5PIyPv4H0Cs3YHpQFlsXyN+u+KZo3ln8xlnfYexBMAzlt/FElA1TYShhSg+kGS95a6ike5HqA7u+34O1h215OrAnolr6djYhEqrFf29ZgICyTsPVDRblWzdzN9P2oeMrtvnEOptxoxDiSCQFcBF5sc6vBJx5W5qXUN+jpnq2KTL/SkmNi2tiLKcgoBYBHNhsuvDQsvCrWwmHIlF5iuMrKzyGdtUoE09AnpNU9pKd7vP1ogE5QHrxONIhitnieh8w7nVdpnInuDmnXeKg3F2RBH9I/sZYksgUJZCcJ2jIKfx4lVBzLoYkazZaBAMuoOTAGTU/H+BXxH++n/YkeEk7nYWH1fV6NIsVgxzlT8Vse+aAq/9uoCSB0E3DlYoAw8UeWtk79VySyAKJGcmtmTqq7IKjV+bOpGRVk6+TF/wF0+kZAoUV3bcJUs17/FcuWkvbD5GbggOiq3yGUzPoLazKtm7fhzj+sdryJKkp3HlHG13hg6oTWa5gFzCMBwMZIrvdlLtLjEPPfGHJV IF+RmNt0 km98spQsY5ccmV/Gqg81fYkLWrqlH3xrqnSWXX3VVWaJPZLg9deds6Sn+gqRC6+AXIW6jPpYgesrcHTJYUw1WGkBRFUcW3O48/Z+vel+W05tB7cioDcdixyWBURfjaUT/e79h7fTLaGPyS5veMr/JrU0bt2zJgUg4ykDLSOmZZburcoAOsJG4LVF+nblZrrUN2dvxWo0faruWhTnZUCKW8kjaOV0bT32AUNp6IgFWWHyNyact4gZAEzT8oklStT8LddqtkOKddslsZDk2uhBBESo086XdFImLCC0VZ5+z34W/2JxQTXuSuXGqfw== 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: This helper is the folio equivalent of check_and_migrate_movable_pages(). Therefore, all the rules that apply to check_and_migrate_movable_pages() also apply to this one as well. Currently, this helper is only used by memfd_pin_folios(). This patch also includes changes to rename and convert the internal functions collect_longterm_unpinnable_pages() and migrate_longterm_unpinnable_pages() to work on folios. Since they are also used by check_and_migrate_movable_pages(), a temporary array is used to collect and share the folios with these functions. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Peter Xu Suggested-by: David Hildenbrand Signed-off-by: Vivek Kasireddy --- mm/gup.c | 129 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 92 insertions(+), 37 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 4d7bc4453819..00b24a429ba8 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2097,20 +2097,24 @@ struct page *get_dump_page(unsigned long addr) #ifdef CONFIG_MIGRATION /* - * Returns the number of collected pages. Return value is always >= 0. + * Returns the number of collected folios. Return value is always >= 0. */ -static unsigned long collect_longterm_unpinnable_pages( - struct list_head *movable_page_list, - unsigned long nr_pages, +static unsigned long collect_longterm_unpinnable_folios( + struct list_head *movable_folio_list, + unsigned long nr_folios, + struct folio **folios, struct page **pages) { unsigned long i, collected = 0; struct folio *prev_folio = NULL; bool drain_allow = true; + struct folio *folio; - for (i = 0; i < nr_pages; i++) { - struct folio *folio = page_folio(pages[i]); + for (i = 0; i < nr_folios; i++) { + if (pages) + folios[i] = page_folio(pages[i]); + folio = folios[i]; if (folio == prev_folio) continue; prev_folio = folio; @@ -2124,7 +2128,7 @@ static unsigned long collect_longterm_unpinnable_pages( continue; if (folio_test_hugetlb(folio)) { - isolate_hugetlb(folio, movable_page_list); + isolate_hugetlb(folio, movable_folio_list); continue; } @@ -2136,7 +2140,7 @@ static unsigned long collect_longterm_unpinnable_pages( if (!folio_isolate_lru(folio)) continue; - list_add_tail(&folio->lru, movable_page_list); + list_add_tail(&folio->lru, movable_folio_list); node_stat_mod_folio(folio, NR_ISOLATED_ANON + folio_is_file_lru(folio), folio_nr_pages(folio)); @@ -2146,27 +2150,28 @@ static unsigned long collect_longterm_unpinnable_pages( } /* - * Unpins all pages and migrates device coherent pages and movable_page_list. - * Returns -EAGAIN if all pages were successfully migrated or -errno for failure - * (or partial success). + * Unpins all folios and migrates device coherent folios and movable_folio_list. + * Returns -EAGAIN if all folios were successfully migrated or -errno for + * failure (or partial success). */ -static int migrate_longterm_unpinnable_pages( - struct list_head *movable_page_list, - unsigned long nr_pages, - struct page **pages) +static int migrate_longterm_unpinnable_folios( + struct list_head *movable_folio_list, + unsigned long nr_folios, + struct folio **folios) { int ret; unsigned long i; - for (i = 0; i < nr_pages; i++) { - struct folio *folio = page_folio(pages[i]); + for (i = 0; i < nr_folios; i++) { + struct folio *folio = folios[i]; if (folio_is_device_coherent(folio)) { /* - * Migration will fail if the page is pinned, so convert - * the pin on the source page to a normal reference. + * Migration will fail if the folio is pinned, so + * convert the pin on the source folio to a normal + * reference. */ - pages[i] = NULL; + folios[i] = NULL; folio_get(folio); gup_put_folio(folio, 1, FOLL_PIN); @@ -2179,23 +2184,23 @@ static int migrate_longterm_unpinnable_pages( } /* - * We can't migrate pages with unexpected references, so drop + * We can't migrate folios with unexpected references, so drop * the reference obtained by __get_user_pages_locked(). - * Migrating pages have been added to movable_page_list after + * Migrating folios have been added to movable_folio_list after * calling folio_isolate_lru() which takes a reference so the - * page won't be freed if it's migrating. + * folio won't be freed if it's migrating. */ - unpin_user_page(pages[i]); - pages[i] = NULL; + unpin_folio(folios[i]); + folios[i] = NULL; } - if (!list_empty(movable_page_list)) { + if (!list_empty(movable_folio_list)) { struct migration_target_control mtc = { .nid = NUMA_NO_NODE, .gfp_mask = GFP_USER | __GFP_NOWARN, }; - if (migrate_pages(movable_page_list, alloc_migration_target, + if (migrate_pages(movable_folio_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC, MR_LONGTERM_PIN, NULL)) { ret = -ENOMEM; @@ -2203,15 +2208,15 @@ static int migrate_longterm_unpinnable_pages( } } - putback_movable_pages(movable_page_list); + putback_movable_pages(movable_folio_list); return -EAGAIN; err: - for (i = 0; i < nr_pages; i++) - if (pages[i]) - unpin_user_page(pages[i]); - putback_movable_pages(movable_page_list); + for (i = 0; i < nr_folios; i++) + if (folios[i]) + unpin_folio(folios[i]); + putback_movable_pages(movable_folio_list); return ret; } @@ -2235,16 +2240,60 @@ static int migrate_longterm_unpinnable_pages( static long check_and_migrate_movable_pages(unsigned long nr_pages, struct page **pages) { + unsigned long nr_folios = nr_pages; unsigned long collected; - LIST_HEAD(movable_page_list); + LIST_HEAD(movable_folio_list); + struct folio **folios; + long ret; - collected = collect_longterm_unpinnable_pages(&movable_page_list, - nr_pages, pages); + folios = kmalloc_array(nr_folios, sizeof(*folios), GFP_KERNEL); + if (!folios) + return -ENOMEM; + + collected = collect_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios, + pages); + if (!collected) { + kfree(folios); + return 0; + } + + ret = migrate_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); + kfree(folios); + return ret; +} + +/* + * Check whether all folios are *allowed* to be pinned. Rather confusingly, all + * folios in the range are required to be pinned via FOLL_PIN, before calling + * this routine. + * + * If any folios in the range are not allowed to be pinned, then this routine + * will migrate those folios away, unpin all the folios in the range and return + * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then + * call this routine again. + * + * If an error other than -EAGAIN occurs, this indicates a migration failure. + * The caller should give up, and propagate the error back up the call stack. + * + * If everything is OK and all folios in the range are allowed to be pinned, + * then this routine leaves all folios pinned and returns zero for success. + */ +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) +{ + unsigned long collected; + LIST_HEAD(movable_folio_list); + + collected = collect_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios, + NULL); if (!collected) return 0; - return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages, - pages); + return migrate_longterm_unpinnable_folios(&movable_folio_list, + nr_folios, folios); } #else static long check_and_migrate_movable_pages(unsigned long nr_pages, @@ -2252,6 +2301,12 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, { return 0; } + +static long check_and_migrate_movable_folios(unsigned long nr_folios, + struct folio **folios) +{ + return 0; +} #endif /* CONFIG_MIGRATION */ /* From patchwork Thu Jan 4 07:25:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13510848 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 D676FC4707B for ; Thu, 4 Jan 2024 07:49:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 65B7F6B03AF; Thu, 4 Jan 2024 02:49:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5B9406B03B0; Thu, 4 Jan 2024 02:49:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E7C86B03B1; Thu, 4 Jan 2024 02:49:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 15D526B03AF for ; Thu, 4 Jan 2024 02:49:00 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CD86EC0968 for ; Thu, 4 Jan 2024 07:48:59 +0000 (UTC) X-FDA: 81640852398.27.04EFBCA Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf13.hostedemail.com (Postfix) with ESMTP id 9EBB220002 for ; Thu, 4 Jan 2024 07:48:56 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=BMEm1eLQ; spf=pass (imf13.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 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=1704354537; 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=BlVvOSSL1BbB+lrnGVreAahr17yrMUl15YX1PIPNmWk=; b=GvXdfE2Gzj3jaYRDv1IdulnaCnwtGha6lRwAmjHj/2DpMr6YV8Yf5rC/Gg9xi6x0LzeFlM tFonQYhuHGo1YlUOIrPfh6m7FBKGXPusYJ24wxEyWngF89bjHjcBB4jK3v2aJkJPo97Jfq z0HuTJiH9Glmh9goam2u3TdGOXac0WA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704354537; a=rsa-sha256; cv=none; b=XKaXrhdqAbNQVPPEyp+xXUpopz0OSfkqtDocqRTEYIQ6GI8Os2fNCpwzh4HvDESQq/afCh V11MPLz3/mY/h9wq6Wl4sT4L1fx/53fIR1IaDJuwTJAoIUSCN2A2q9nKlqQ0QgY9AZ/8+E nVz9/taCNSMew4gTv5fyivrF/tMkrG8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=BMEm1eLQ; spf=pass (imf13.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704354536; x=1735890536; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=e3c0tjXA+h9k7oaKNB8qaNJNT1gODpsdEPYdMfV6qpA=; b=BMEm1eLQfjNQGNWL9TxOedy9GfQ5NWe20OIgfx/bxnV7V7SU6YlzNpiO mMgHdyjaYRLaF5FEgoWr7Fn+jzxhadQihDoNIhcS0gRt0BpvpawwGWc5H nu2PoDH2S0CHlSBj/8yaMTq1aQabkjXBznTIO+47jyczoDblRAmAa6JrI owi9Az6mm2ZkYiy7YhVbZSVop50KGu8UW3HVlD+ihfk433NB+7KGFeC3l XaQvRrnmsMVG3t66DuPWMHas40tWumorD2ce98PMgRdGlMYc7jLpXQmfS 7ccUqCD/iQuHWonyE6qdpNqOTcrli+S6UP6i0/FJXb0iwB72WWgHuR26w g==; X-IronPort-AV: E=McAfee;i="6600,9927,10942"; a="483331120" X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="483331120" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="14776733" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:47 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe , Christoph Hellwig Subject: [PATCH v10 5/8] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios (v10) Date: Wed, 3 Jan 2024 23:25:15 -0800 Message-Id: <20240104072518.807950-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240104072518.807950-1-vivek.kasireddy@intel.com> References: <20240104072518.807950-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9EBB220002 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 1edugpu68q49wrirbhsszcomhu14q8go X-HE-Tag: 1704354536-789030 X-HE-Meta: U2FsdGVkX1/DF8DrFlZX+m0lGahHklFZt1kAdBU7OP8JrAqfRxC1Bje1YjlIlA5RU0DBIosEKXOr1gSqZg206U9k8bxAWp6RhLHuD20Pl7/1n5aDeaqC/UsVBPETUjtCQ8482Z0aXCJPEzeBgj0GPXydarmtLiKQJBRpEI3lmenudQEQFvRbpJlLzuTfcxzmi/gqGJBv9aOPv1Rvszcu/2cOWAQzF3cYd6bpDaZDzfycMACWidJxknEty97L0dnFgSmS2nkB5cSEzsZbvvfG5sVJKpZ2shaD4Rd+370V4IiyXr29cSdnxpCSTjWq7ceueAKdyL9M1sAuDSJV0M2Zr2TqjSkA/DZLsghMQSgxIN4Vp0RerzA2WJA9mR4hG0/XnHj4qkRRC1kvZlu5VbJiq8HlwpctxFoUHIsgPU3U26KiBTVv2XJ8efdek1ScDUMev2JSnCsvKHzsIJkVAW2Y6i9qI+2TGCSMgINJtebWEcaKlTTPQq2U9e3ToOvz59yXD+VKme2QQEB09QyL2M4OQGzy6iPUc/pljuDVlnPNxhOQGkvm3m2UNC7fhLswBdlyso+/wSxrykzOe6OBHn/XqdU2KE+9qp+B8ybYOnVToyh9dplH/xCRHHLbouBPCqQ/MF5hMTH7UixfOVV3YW1oY4holQenXi19RKvpSGTxmeH/H8wYJOyplRkrEtbnYjn+4lCiV9d++6uFBP0i+DELujf0XwqTHs1I1lDfbem3tmia5O88VlqWsSsoQtQqaF11ScDeSUuOZ8zPb3NGfqw+Dufskx7fxTBFeQuq2U3CQcDva36sS1W1+aIvWkUTWgKYiW/tMET3C1HaRFoNKylCw0WqgVR67CGrptP/Btq9Jk/xuXKWM4n02dBSdafnAJUOo+qopGfFGTVV5qodSf/eRvD0ZnFU7HV2wLEseCLxyo+6INxtwFQHIku6l56LDw4zobEEE+tM+99zqC4TpF/ cMH5JiCO s50erdqHM6mMlNSJXpiFf54ftJJJPxFGRlO/KS+md2RFEBReE9iTFvf2A+LakncxgrFaW+hDlUiki59zIHuezaLAJw2PNIJ2JnKtIpcodMZyje2fEYBR261duxZAeqaQfylbUOKB0SuqP98VsrKchBFU89vBXTNXLENxWZhLZT6Cx3p9eOxGvjhcPFRYZBKRrlgWKZ+BhnT9zBR27Yre17/kddSG6xDsjmw9H4/OnVjW/6sLgyB9UcudMxpu8+CttdzXCp1Ih0jlmqV+fwVXCDuDeF2JDCUWMEvhmGFAvWWiHRfhfnsudCw1INwLpJzpzSTPILc9SfmTzYNMr2LpAtUPMlZUrNEt18wRJ4H9Cebq8QqE= 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: For drivers that would like to longterm-pin the folios associated with a memfd, the memfd_pin_folios() API provides an option to not only pin the folios via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with memfds but it should work with any files that belong to either shmemfs or hugetlbfs. Files belonging to other filesystems are rejected for now. The folios need to be located first before pinning them via FOLL_PIN. If they are found in the page cache, they can be immediately pinned. Otherwise, they need to be allocated using the filesystem specific APIs and then pinned. 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) v3: (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 v4: - Remove the local variable "page" and instead use 3 return statements in alloc_file_page() (David) - Add the R-b tag from David v5: (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 v6: (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 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 v8: (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 v9: (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 v10: - Use unpin_folios() to unpin folios on error - Use check_and_migrate_movable_folios() instead of check_and_migrate_movable_pages() for checking and migrating folios Cc: David Hildenbrand Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe (v2) Reviewed-by: David Hildenbrand (v3) Reviewed-by: Christoph Hellwig (v6) Signed-off-by: Vivek Kasireddy --- include/linux/memfd.h | 5 ++ include/linux/mm.h | 3 + mm/gup.c | 136 ++++++++++++++++++++++++++++++++++++++++++ mm/memfd.c | 34 +++++++++++ 4 files changed, 178 insertions(+) diff --git a/include/linux/memfd.h b/include/linux/memfd.h index e7abf6fa4c52..3f2cf339ceaf 100644 --- a/include/linux/memfd.h +++ b/include/linux/memfd.h @@ -6,11 +6,16 @@ #ifdef CONFIG_MEMFD_CREATE extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg); +struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx); #else static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a) { return -EINVAL; } +static inline struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) +{ + return ERR_PTR(-EINVAL); +} #endif #endif /* __LINUX_MEMFD_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 4dbfdd277014..cbc0c6360690 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2474,6 +2474,9 @@ long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); +long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, + struct folio **folios, unsigned int max_folios, + pgoff_t *offset); int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); diff --git a/mm/gup.c b/mm/gup.c index 00b24a429ba8..5b2b55d87998 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include @@ -3528,3 +3530,137 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +/** + * memfd_pin_folios() - pin folios associated with a memfd + * @memfd: the memfd whose folios are to be pinned + * @start: the first memfd offset + * @end: the last memfd offset (inclusive) + * @folios: array that receives pointers to the folios pinned + * @max_folios: maximum number of entries in @folios + * @offset: the offset into the first folio + * + * Attempt to pin folios associated with a memfd in the contiguous range + * [start, end]. Given that a memfd is either backed by shmem or hugetlb, + * the folios can either be found in the page cache or need to be allocated + * if necessary. Once the folios are located, they are all pinned via + * FOLL_PIN and @offset is populatedwith the offset into the first folio. + * And, eventually, these pinned folios must be released either using + * unpin_folios() or unpin_folio(). + * + * It must be noted that the folios may be pinned for an indefinite amount + * of time. And, in most cases, the duration of time they may stay pinned + * would be controlled by the userspace. This behavior is effectively the + * same as using FOLL_LONGTERM with other GUP APIs. + * + * Returns number of folios pinned, which could be less than @max_folios + * as it depends on the folio sizes that cover the range [start, end]. + * If no folios were pinned, it returns -errno. + */ +long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, + struct folio **folios, unsigned int max_folios, + pgoff_t *offset) +{ + unsigned int flags, nr_folios, nr_found; + unsigned int i, pgshift = PAGE_SHIFT; + pgoff_t start_idx, end_idx, next_idx; + struct folio *folio = NULL; + struct folio_batch fbatch; + struct hstate *h; + long ret; + + if (start > end || !max_folios) + return -EINVAL; + + if (!memfd) + return -EINVAL; + + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) + return -EINVAL; + + if (is_file_hugepages(memfd)) { + h = hstate_file(memfd); + pgshift = huge_page_shift(h); + } + + flags = memalloc_pin_save(); + do { + nr_folios = 0; + start_idx = start >> pgshift; + end_idx = end >> pgshift; + if (is_file_hugepages(memfd)) { + start_idx <<= huge_page_order(h); + end_idx <<= huge_page_order(h); + } + + folio_batch_init(&fbatch); + while (start_idx <= end_idx && nr_folios < max_folios) { + /* + * In most cases, we should be able to find the folios + * in the page cache. If we cannot find them for some + * reason, we try to allocate them and add them to the + * page cache. + */ + nr_found = filemap_get_folios_contig(memfd->f_mapping, + &start_idx, + end_idx, + &fbatch); + if (folio) { + folio_put(folio); + folio = NULL; + } + + next_idx = 0; + for (i = 0; i < nr_found; i++) { + /* + * As there can be multiple entries for a + * given folio in the batch returned by + * filemap_get_folios_contig(), the below + * check is to ensure that we pin and return a + * unique set of folios between start and end. + */ + if (next_idx && + next_idx != folio_index(fbatch.folios[i])) + continue; + + folio = try_grab_folio(&fbatch.folios[i]->page, + 1, FOLL_PIN); + if (!folio) { + folio_batch_release(&fbatch); + goto err; + } + + if (nr_folios == 0) + *offset = offset_in_folio(folio, start); + + folios[nr_folios] = folio; + next_idx = folio_next_index(folio); + if (++nr_folios == max_folios) + break; + } + + folio = NULL; + folio_batch_release(&fbatch); + if (!nr_found) { + folio = memfd_alloc_folio(memfd, start_idx); + if (IS_ERR(folio)) { + ret = PTR_ERR(folio); + if (ret != -EEXIST) + goto err; + } + } + } + + ret = check_and_migrate_movable_folios(nr_folios, folios); + } while (ret == -EAGAIN); + + memalloc_pin_restore(flags); + return ret ? ret : nr_folios; +err: + memalloc_pin_restore(flags); + unpin_folios(folios, nr_folios); + + return ret; +} +EXPORT_SYMBOL_GPL(memfd_pin_folios); + diff --git a/mm/memfd.c b/mm/memfd.c index d3a1ba4208c9..36a75e8249f8 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -63,6 +63,40 @@ static void memfd_tag_pins(struct xa_state *xas) xas_unlock_irq(xas); } +/* + * This is a helper function used by memfd_pin_user_pages() in GUP (gup.c). + * It is mainly called to allocate a page in a memfd when the caller + * (memfd_pin_user_pages()) cannot find a page in the page cache at a given + * index in the mapping. + */ +struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) +{ +#ifdef CONFIG_HUGETLB_PAGE + struct folio *folio; + int err; + + if (is_file_hugepages(memfd)) { + folio = alloc_hugetlb_folio_nodemask(hstate_file(memfd), + NUMA_NO_NODE, + NULL, + GFP_USER); + if (folio && folio_try_get(folio)) { + err = hugetlb_add_to_page_cache(folio, + memfd->f_mapping, + idx); + if (err) { + folio_put(folio); + free_huge_folio(folio); + return ERR_PTR(err); + } + return folio; + } + return ERR_PTR(-ENOMEM); + } +#endif + return shmem_read_folio(memfd->f_mapping, idx); +} + /* * Setting SEAL_WRITE requires us to verify there's no pending writer. However, * via get_user_pages(), drivers might have some pending I/O without any active From patchwork Thu Jan 4 07:25:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13510851 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 6058CC47074 for ; Thu, 4 Jan 2024 07:49:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C729C6B03B3; Thu, 4 Jan 2024 02:49:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BD7D06B03B4; Thu, 4 Jan 2024 02:49:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98AEB6B03B5; Thu, 4 Jan 2024 02:49:02 -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 713696B03B4 for ; Thu, 4 Jan 2024 02:49:02 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3BA241A0355 for ; Thu, 4 Jan 2024 07:49:02 +0000 (UTC) X-FDA: 81640852524.13.B59718D Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 53EFD40002 for ; Thu, 4 Jan 2024 07:49:00 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=f+aN61Rv; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf12.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 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=1704354540; 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=WoOlj0+FN2u6l8eE8sI64entA6PFFPqd725ekkUPvt0=; b=ITEqXd/2WeRI8mK2dP8RIKnJHT3qtZbLp5bTAFQqF2SZzaasNzw6AGvlWuvJ440zeyKvkV XrQJsL5HyTCaWokq7p6sP6uEP04YvJaVwQ4b0MOWjQuXx0R0wAZPPfa8KL2C6lgeUSeyif vYjEDhTvKibhVWneozwxGseT7unjxn8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=f+aN61Rv; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf12.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704354540; a=rsa-sha256; cv=none; b=TFKQLt82bFcm+dCP37TqOa0HFyvfC37Q6EhxT1/SFnZnRUOLrU4SUIQ7I6/tFZ4yjIsq1C EcUuXw23kb0IVuc5wVnuRwsT6x+HXJAMcDTDKqAreML+iAvNdeXYUWmAMsS3YFQ9h42NXR 0DJryRiHCvLNWTrfsKnuEpfCgE5WWP4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704354540; x=1735890540; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=srh+pDeiF4x93Sp9cmg48dB8ma2fshhkeNl1Nqm5xC4=; b=f+aN61RvcRCCJjc14Fx4IOsk/6SbGni8UGemQeSp2bfvL2wbaJEaF/3r Ub8jch5Idn2wtO7dHYy3VnOMqR+UpAANIWBRrqD2OtUmO3HT8WoNdP7q3 kbYU1jmJfXMfGuwptNYNJuG/mGzrsaUVLaDZE8yCOKlfqsVF5BTun597J VG9dEzTzxKFYoLTTjDlHX7zBkiucPgC/OGnZmGJObSRAVfMgxyqcVvDmU hpoMP5rHOSE89o0wRZHBgRI6uATpPX0pSdLuaxFPlLs9xjqiAOzDVtzq7 xpuxMPu00UBqBXR12MRNsW/4bd8BsgCG2jDGbSIKCBlUKZbofAVnkwD5w w==; X-IronPort-AV: E=McAfee;i="6600,9927,10942"; a="483331128" X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="483331128" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="14776736" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:47 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v10 6/8] udmabuf: Convert udmabuf driver to use folios (v2) Date: Wed, 3 Jan 2024 23:25:16 -0800 Message-Id: <20240104072518.807950-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240104072518.807950-1-vivek.kasireddy@intel.com> References: <20240104072518.807950-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 53EFD40002 X-Stat-Signature: 16yhormh3dx3t4juam9c7j799hmgnxcq X-Rspam-User: X-HE-Tag: 1704354540-401865 X-HE-Meta: U2FsdGVkX1/vQQ2nJcqFWY/AeUMJa30fcxeqDsU9hIdMXE8hRcNPquv5FE+IDEqAVtD7hrm28fb7ub2+ZivxlMYzPJdFUz9Zf5VYfy9oHH1dAjdL4xKfAZhcklHJ/nAyyYn5j7XatEKJ33BBE3h/ulM3HGVCy98N9GnGjqiP9rn4aCTWd8dT56soN3fO0syV4nZCSj3V2ENzUIbpxVwON4Y+u/3sOH7K0pyAMGLgXRbkkdrxeXCaZykieOp1jVuiWw0xasaik64N80xupp3DHGwaRi/iKawj540isIB5GxVQ1MrN2XVCgBn5DCXQ8rRr07no+Vg4iCUZ9p7IbWZVwrMJrfxCUSr+dy8+3Q8mgN0qp736/aq+pJGEUX3DUPYLb2svKGJZact02FV5xS/SpVx9Jc3WzCG0u7ZD522Xqg5nBkUWi+8BB64vD2GQIyejxQhBBwSOznqjxPaRcuH+m42zeYX5bJ2guasAOiJCZvJw2wWuwz8RHyJQ850k2FGmGpL54BYH9T+X0eEJmcgUba4Pkri65rZZF1mi+n1XddynJ3r8h1VHCPRQ4I6G9Xt24j7ttT8sfOYMe7H53VbTlnEIWkUSn06TwkcMcRVNE2B/EFkyud5vqJooz+UFhuF+jOyhO3xJqfufCrF9ErunPTyS53HBMgH7ynhDPgvZgyxNertmKexGjSCLxZsJ1FdRpT3SGFTYa0glzzgptA2Dy8z7Yr03Y0vkOCkX/20t/wEPyykSPRxsj5+iIQ+e8bXevqSiS0vXg/pfSjwTGGECGYu2LMutpFgZk581WVRXaSbPsqnROaEOibdudE+axUP0mOgI79nO0JXvA0xsD6MgbfXrYhSJFbY6uB99nS733EOEffAz9zPpfo8xID97/Ph3WTLHotVQVzTBbz0xQMMhx4IvAJfLi9UlDp59GCFpaEGJJbm9xvLrfAyocsKTSx8JNbzCYE6QOA0H37hMszR Au4dMXNg IeVuBjmYcEZBtrs6+uCrsIoXoF586d3qQ+k85Zwknwdpo5J43ikJfK8Xk1kxQBieVn6ReWJpJEL3OfgkrYrCQ51fZqVeHz27Ueg4AdWmH8WQ0yHcY8kod3+6CIkZPgmkM74TYTCOqI3AkNEsOFpiup4TrR31zbaSjQAZKHGZ3bJYbMpyY+hFz4v4Q6+VqTwW5P8PcB2ptu6y+Ht+A1lmCxFQBu+3YTB2mGQoXnDpDw1AiU3IO6nhWINJo/dnfUdjCfRFlPQc27nlj8lHq6AZlb07QZp9X157CcHZuD3gPqh1f3swqJxGXuxTHMQ== 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: This is mainly a preparatory patch to use memfd_pin_folios() API for pinning folios. Using folios instead of pages makes sense as the udmabuf driver needs to handle both shmem and hugetlb cases. However, the function vmap_udmabuf() still needs a list of pages; so, we collect all the head pages into a local array in this case. Other changes in this patch include the addition of helpers for checking the memfd seals and exporting dmabuf. Moving code from udmabuf_create() into these helpers improves readability given that udmabuf_create() is a bit long. v2: (Matthew) - Use folio_pfn() on the folio instead of page_to_pfn() on head page - Don't split the arguments to shmem_read_folio() on multiple lines Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 140 ++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 57 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 274defd3fa3e..a8f3af61f7f2 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -26,7 +26,7 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is struct udmabuf { pgoff_t pagecount; - struct page **pages; + struct folio **folios; struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; @@ -42,7 +42,7 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) if (pgoff >= ubuf->pagecount) return VM_FAULT_SIGBUS; - pfn = page_to_pfn(ubuf->pages[pgoff]); + pfn = folio_pfn(ubuf->folios[pgoff]); pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT; return vmf_insert_pfn(vma, vmf->address, pfn); @@ -68,11 +68,21 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map) { struct udmabuf *ubuf = buf->priv; + struct page **pages; void *vaddr; + pgoff_t pg; dma_resv_assert_held(buf->resv); - vaddr = vm_map_ram(ubuf->pages, ubuf->pagecount, -1); + pages = kmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL); + if (!pages) + return -ENOMEM; + + for (pg = 0; pg < ubuf->pagecount; pg++) + pages[pg] = &ubuf->folios[pg]->page; + + vaddr = vm_map_ram(pages, ubuf->pagecount, -1); + kfree(pages); if (!vaddr) return -EINVAL; @@ -107,7 +117,8 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf, goto err_alloc; for_each_sg(sg->sgl, sgl, ubuf->pagecount, i) - sg_set_page(sgl, ubuf->pages[i], PAGE_SIZE, ubuf->offsets[i]); + sg_set_folio(sgl, ubuf->folios[i], PAGE_SIZE, + ubuf->offsets[i]); ret = dma_map_sgtable(dev, sg, direction, 0); if (ret < 0) @@ -152,9 +163,9 @@ static void release_udmabuf(struct dma_buf *buf) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); for (pg = 0; pg < ubuf->pagecount; pg++) - put_page(ubuf->pages[pg]); + folio_put(ubuf->folios[pg]); kfree(ubuf->offsets); - kfree(ubuf->pages); + kfree(ubuf->folios); kfree(ubuf); } @@ -215,36 +226,33 @@ static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, pgoff_t mapidx = offset >> huge_page_shift(hpstate); pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; - struct page *hpage = NULL; - struct folio *folio; + struct folio *folio = NULL; pgoff_t pgidx; mapidx <<= huge_page_order(hpstate); for (pgidx = 0; pgidx < pgcnt; pgidx++) { - if (!hpage) { + if (!folio) { folio = __filemap_get_folio(memfd->f_mapping, mapidx, FGP_ACCESSED, 0); if (IS_ERR(folio)) return PTR_ERR(folio); - - hpage = &folio->page; } - get_page(hpage); - ubuf->pages[*pgbuf] = hpage; + folio_get(folio); + ubuf->folios[*pgbuf] = folio; ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; (*pgbuf)++; if (++subpgoff == maxsubpgs) { - put_page(hpage); - hpage = NULL; + folio_put(folio); + folio = NULL; subpgoff = 0; mapidx += pages_per_huge_page(hpstate); } } - if (hpage) - put_page(hpage); + if (folio) + folio_put(folio); return 0; } @@ -254,31 +262,69 @@ static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, pgoff_t *pgbuf) { pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; - struct page *page; + struct folio *folio = NULL; for (pgidx = 0; pgidx < pgcnt; pgidx++) { - page = shmem_read_mapping_page(memfd->f_mapping, - pgoff + pgidx); - if (IS_ERR(page)) - return PTR_ERR(page); + folio = shmem_read_folio(memfd->f_mapping, pgoff + pgidx); + if (IS_ERR(folio)) + return PTR_ERR(folio); - ubuf->pages[*pgbuf] = page; + ubuf->folios[*pgbuf] = folio; (*pgbuf)++; } return 0; } +static int check_memfd_seals(struct file *memfd) +{ + int seals; + + if (!memfd) + return -EBADFD; + + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) + return -EBADFD; + + seals = memfd_fcntl(memfd, F_GET_SEALS, 0); + if (seals == -EINVAL) + return -EBADFD; + + if ((seals & SEALS_WANTED) != SEALS_WANTED || + (seals & SEALS_DENIED) != 0) + return -EINVAL; + + return 0; +} + +static int export_udmabuf(struct udmabuf *ubuf, + struct miscdevice *device, + u32 flags) +{ + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct dma_buf *buf; + + ubuf->device = device; + exp_info.ops = &udmabuf_ops; + exp_info.size = ubuf->pagecount << PAGE_SHIFT; + exp_info.priv = ubuf; + exp_info.flags = O_RDWR; + + buf = dma_buf_export(&exp_info); + if (IS_ERR(buf)) + return PTR_ERR(buf); + + return dma_buf_fd(buf, flags); +} + static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { - DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + pgoff_t pgcnt, pgbuf = 0, pglimit; struct file *memfd = NULL; struct udmabuf *ubuf; - struct dma_buf *buf; - pgoff_t pgcnt, pgbuf = 0, pglimit; - int seals, ret = -EINVAL; + int ret = -EINVAL; u32 i, flags; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); @@ -299,9 +345,9 @@ static long udmabuf_create(struct miscdevice *device, if (!ubuf->pagecount) goto err; - ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages), + ubuf->folios = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios), GFP_KERNEL); - if (!ubuf->pages) { + if (!ubuf->folios) { ret = -ENOMEM; goto err; } @@ -314,18 +360,9 @@ static long udmabuf_create(struct miscdevice *device, pgbuf = 0; for (i = 0; i < head->count; i++) { - ret = -EBADFD; memfd = fget(list[i].memfd); - if (!memfd) - goto err; - if (!shmem_file(memfd) && !is_file_hugepages(memfd)) - goto err; - seals = memfd_fcntl(memfd, F_GET_SEALS, 0); - if (seals == -EINVAL) - goto err; - ret = -EINVAL; - if ((seals & SEALS_WANTED) != SEALS_WANTED || - (seals & SEALS_DENIED) != 0) + ret = check_memfd_seals(memfd); + if (ret < 0) goto err; pgcnt = list[i].size >> PAGE_SHIFT; @@ -341,33 +378,22 @@ static long udmabuf_create(struct miscdevice *device, goto err; fput(memfd); - memfd = NULL; } - exp_info.ops = &udmabuf_ops; - exp_info.size = ubuf->pagecount << PAGE_SHIFT; - exp_info.priv = ubuf; - exp_info.flags = O_RDWR; - - ubuf->device = device; - buf = dma_buf_export(&exp_info); - if (IS_ERR(buf)) { - ret = PTR_ERR(buf); + flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0; + ret = export_udmabuf(ubuf, device, flags); + if (ret < 0) goto err; - } - flags = 0; - if (head->flags & UDMABUF_FLAGS_CLOEXEC) - flags |= O_CLOEXEC; - return dma_buf_fd(buf, flags); + return ret; err: while (pgbuf > 0) - put_page(ubuf->pages[--pgbuf]); + folio_put(ubuf->folios[--pgbuf]); if (memfd) fput(memfd); kfree(ubuf->offsets); - kfree(ubuf->pages); + kfree(ubuf->folios); kfree(ubuf); return ret; } From patchwork Thu Jan 4 07:25:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13510853 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 20A34C47073 for ; Thu, 4 Jan 2024 07:49:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 468046B03B5; Thu, 4 Jan 2024 02:49:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 371246B03B6; Thu, 4 Jan 2024 02:49:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19C806B03B7; Thu, 4 Jan 2024 02:49:04 -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 F1CE26B03B5 for ; Thu, 4 Jan 2024 02:49:03 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C81E280181 for ; Thu, 4 Jan 2024 07:49:03 +0000 (UTC) X-FDA: 81640852566.07.CA66C25 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf20.hostedemail.com (Postfix) with ESMTP id B78C91C0024 for ; Thu, 4 Jan 2024 07:49:01 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EcRQvLe0; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 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=1704354542; 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=Fe6Q/8KY+0xSauOkzSOhdGajrS9CwV1UEVtIIpsOvYY=; b=4xyBZ87eeY5wlM9qzW/UozZNqO8RtEx92Uxg6oIUZHrRo5oyCo1yoBoIsOYHBf5va8JgPO pxodDqkv00AhdaeXpEcIQF6/NZ+JVlRMAfQYJXeXXMprRdyCgAuPCiPIH4k9y+LJXLasr+ kjIrVKN8kt1Q7MP/7GgNpOvBR9h3N9I= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EcRQvLe0; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704354542; a=rsa-sha256; cv=none; b=ze5/T3QPW0yY9EHDrIXXDrqxrMLhOMQd1fjw2WeUaVZ8pTE92nFIPU7BwkebXgyfRLnm6C ZCX4g5zKBF91N0JB3+h16av4Ov63JKxb9jIFCGbB8RBcKoPamlL6gXouczCZNrI2Z6D69k rBbZyZa0G74F+ecg6/4QgQ86A6VlPLk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704354541; x=1735890541; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HPqmDRTHlwBlDnRt25bFtTkxF656lUtiC3CZjHC0SUs=; b=EcRQvLe0/FfdFUY2+Byj1baq+Vp9/5U/QCctWWq9peBrG5PV6JdAm2dA f9amVQE3hl+RY4lHYDLEnP1Wbtqp4UoDAxXvFONH/T5/hHxalCxRC1RHl 0I0LdF+/5avXVFHkdiB9w3hfcb8bCQdkFJABRPoveGhHS+TEPyivyBhQf o7RHZ2k4Mz5FMntq/I6XwQNZ3SFY7sGR5rG6Fvwf4LZp4MHu0a8iBBYOl Dfz7HkfvRg1QL9nOEEath5JFxnszA+Gd9niQkHyFYMO+q+K6eiJTLrYU2 F26OjbGWr9fT9lWncVtzybWhPQJyC9l2+/XXU8hDYmvpr87vub3rPWrfZ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10942"; a="483331136" X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="483331136" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="14776739" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:47 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v10 7/8] udmabuf: Pin the pages using memfd_pin_folios() API (v8) Date: Wed, 3 Jan 2024 23:25:17 -0800 Message-Id: <20240104072518.807950-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240104072518.807950-1-vivek.kasireddy@intel.com> References: <20240104072518.807950-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: cuq6b3pfmjc1458umicr7waemsd916qj X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B78C91C0024 X-HE-Tag: 1704354541-168442 X-HE-Meta: U2FsdGVkX18lJvf9edgN80bYVJZKyD8jcHDegM6psUAW0xY3eDHXwyaz3W/fZVYQ6cP5ohPCzP/j6ZQi0RjHqUXnrooniXjVTbOqaj97Y49iN9J/CcvYvbKEAORBPotx9zOw59oSi+a9FIYLLqNa9tEPLQtd7RVPe4MZ9ymt694QzXKfUfLVSbxNvsIVRJZeSUhcBUBEbRjryDjYRjMqm2/7Sj8+lUhPDtjbw49aXaWDoDjGbfNf+VvYTrkchNZ/QKdFnWyOgZrpmr2q1XCnQziGhwk084/W8zU69B5FfmwZ87m1KAEaaB/i6WTRxPmJ8PvnrPlhLKbbGTyWBZJ7dPO43Kp+T08Tj0JnC9M6HYREDG+b2G2hV3xtFsNkUvnUtY7lkjN38yQ1XF2aa2TD53eD6tIQnWSZHaqzYvT5CBWmSg3fRi0IH5bfeSfDXLmeJIWOlxYQ/zHw2gljTD3FHcw5l/P8vkvbvDPXeQtAes5EazQ8feJYKPJXDyJ4H9bwkYeNyH6vgnwpan2rEZB0mG1SZXwEYJF2RIRLBAW64JWDLERgjxD3UawbsMhg3S6Xa8N2Y/p6SbwoRN1i0yVPJ7I/b+pohQRlZNio5Ahv7m+xtK5IKp3bvcePGV7P2Uf+KJxZwu1+QrPMRg51Dk3m5vvyLIpFGB6DIe2+tO2tzty3z5ELlHdnq0bwP09MURkVIqXiA/NsbLkWdAP4AHhzqA1q7ix0v5kU1Pj9bMHnsh6WtcTPcnO7XPtHsL6fGMn2DC/btu+qaaFpmGTj8bZGK9xEO/HtCZgcU3C10pgdub2tRPR/hC5m9Qfn/4dc8PI6l/TGAfgLw5o6QtrRlJz9x67VN7ryhzwZf+QX0mwMYjpJGVMLuJ+k2H7Js2xy/DXEIyV9aYGqtDrTxsAgoQ8yUbhL4qFvczqCnXWOj55bpBtmSScbA0i/75IMsZo6f9t2VTTFjrNxwMJA6sK4kUC /7YxOrzx Fj+vQm64ULAD3MmKWQ5O1uai1Fo3LNFx9I4coPxXZAici097a7fJmZLguCxIhKz+yPbpHIS4nqeYMuh2w8bPeFw+uBtwP39xjCAd3NJyTQm2CaNfaH//c8dcPTyQ+ZIgnsZ6ULNWy9hNYOFOOmujZaIjv1clkep39jT35Y1pVTrGO3OURLMM47MLSL8cU5AHh9/soUq7gKldIDsKhJXdMg0nRG5bWCmCEedDzlDJXA3pZ27pP1b1SbcqMuKJCYadtFSOLrK9tt6/nDTsrH5CtAeT/I1aN7h0YIUAYXNXBIZuF+KnTLm2AfNeznqAuzEKOvOTFMw/d7i1/8M9Q6qC/Zhw4U8swG+I1Uokv8yy8mAiyeYg= 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: Using memfd_pin_folios() will ensure that the pages are pinned correctly using FOLL_PIN. And, this also ensures that we don't accidentally break features such as memory hotunplug as it would not allow pinning pages in the movable zone. Using this new API also simplifies the code as we no longer have to deal with extracting individual pages from their mappings or handle shmem and hugetlb cases separately. v2: - Adjust to the change in signature of pin_user_pages_fd() by passing in file * instead of fd. v3: - Limit the changes in this patch only to those that are required for using pin_user_pages_fd() - Slightly improve the commit message v4: - Adjust to the change in name of the API (memfd_pin_user_pages) v5: - Adjust to the changes in memfd_pin_folios which now populates a list of folios and offsets v6: - Don't unnecessarily use folio_page() (Matthew) - Pass [start, end] and max_folios to memfd_pin_folios() - Create another temporary array to hold the folios returned by memfd_pin_folios() as we populate ubuf->folios. - Unpin the folios only once as memfd_pin_folios pins them once v7: - Use a list to track the folios that need to be unpinned v8: - Use unpin_folio() instead of unpin_user_page() to unpin folio Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 153 +++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 75 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index a8f3af61f7f2..afa8bfd2a2a9 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -30,6 +30,12 @@ struct udmabuf { struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; + struct list_head unpin_list; +}; + +struct udmabuf_folio { + struct folio *folio; + struct list_head list; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -153,17 +159,43 @@ static void unmap_udmabuf(struct dma_buf_attachment *at, return put_sg_table(at->dev, sg, direction); } +static void unpin_all_folios(struct list_head *unpin_list) +{ + struct udmabuf_folio *ubuf_folio; + + while (!list_empty(unpin_list)) { + ubuf_folio = list_first_entry(unpin_list, + struct udmabuf_folio, list); + unpin_folio(ubuf_folio->folio); + + list_del(&ubuf_folio->list); + kfree(ubuf_folio); + } +} + +static int add_to_unpin_list(struct list_head *unpin_list, + struct folio *folio) +{ + struct udmabuf_folio *ubuf_folio; + + ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL); + if (!ubuf_folio) + return -ENOMEM; + + ubuf_folio->folio = folio; + list_add_tail(&ubuf_folio->list, unpin_list); + return 0; +} + static void release_udmabuf(struct dma_buf *buf) { struct udmabuf *ubuf = buf->priv; struct device *dev = ubuf->device->this_device; - pgoff_t pg; if (ubuf->sg) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); - for (pg = 0; pg < ubuf->pagecount; pg++) - folio_put(ubuf->folios[pg]); + unpin_all_folios(&ubuf->unpin_list); kfree(ubuf->offsets); kfree(ubuf->folios); kfree(ubuf); @@ -218,64 +250,6 @@ static const struct dma_buf_ops udmabuf_ops = { #define SEALS_WANTED (F_SEAL_SHRINK) #define SEALS_DENIED (F_SEAL_WRITE) -static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, - pgoff_t offset, pgoff_t pgcnt, - pgoff_t *pgbuf) -{ - struct hstate *hpstate = hstate_file(memfd); - pgoff_t mapidx = offset >> huge_page_shift(hpstate); - pgoff_t subpgoff = (offset & ~huge_page_mask(hpstate)) >> PAGE_SHIFT; - pgoff_t maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT; - struct folio *folio = NULL; - pgoff_t pgidx; - - mapidx <<= huge_page_order(hpstate); - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - if (!folio) { - folio = __filemap_get_folio(memfd->f_mapping, - mapidx, - FGP_ACCESSED, 0); - if (IS_ERR(folio)) - return PTR_ERR(folio); - } - - folio_get(folio); - ubuf->folios[*pgbuf] = folio; - ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; - (*pgbuf)++; - if (++subpgoff == maxsubpgs) { - folio_put(folio); - folio = NULL; - subpgoff = 0; - mapidx += pages_per_huge_page(hpstate); - } - } - - if (folio) - folio_put(folio); - - return 0; -} - -static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, - pgoff_t offset, pgoff_t pgcnt, - pgoff_t *pgbuf) -{ - pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; - struct folio *folio = NULL; - - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - folio = shmem_read_folio(memfd->f_mapping, pgoff + pgidx); - if (IS_ERR(folio)) - return PTR_ERR(folio); - - ubuf->folios[*pgbuf] = folio; - (*pgbuf)++; - } - - return 0; -} - static int check_memfd_seals(struct file *memfd) { int seals; @@ -321,16 +295,19 @@ static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { - pgoff_t pgcnt, pgbuf = 0, pglimit; + pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0; + long nr_folios, ret = -EINVAL; struct file *memfd = NULL; + struct folio **folios; struct udmabuf *ubuf; - int ret = -EINVAL; - u32 i, flags; + u32 i, j, k, flags; + loff_t end; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); if (!ubuf) return -ENOMEM; + INIT_LIST_HEAD(&ubuf->unpin_list); pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { if (!IS_ALIGNED(list[i].offset, PAGE_SIZE)) @@ -366,17 +343,44 @@ static long udmabuf_create(struct miscdevice *device, goto err; pgcnt = list[i].size >> PAGE_SHIFT; - if (is_file_hugepages(memfd)) - ret = handle_hugetlb_pages(ubuf, memfd, - list[i].offset, - pgcnt, &pgbuf); - else - ret = handle_shmem_pages(ubuf, memfd, - list[i].offset, - pgcnt, &pgbuf); - if (ret < 0) + folios = kmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); + if (!folios) { + ret = -ENOMEM; goto err; + } + end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1; + ret = memfd_pin_folios(memfd, list[i].offset, end, + folios, pgcnt, &pgoff); + if (ret < 0) { + kfree(folios); + goto err; + } + + nr_folios = ret; + pgoff >>= PAGE_SHIFT; + for (j = 0, k = 0; j < pgcnt; j++) { + ubuf->folios[pgbuf] = folios[k]; + ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT; + + if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) { + ret = add_to_unpin_list(&ubuf->unpin_list, + folios[k]); + if (ret < 0) { + kfree(folios); + goto err; + } + } + + pgbuf++; + if (++pgoff == folio_nr_pages(folios[k])) { + pgoff = 0; + if (++k == nr_folios) + break; + } + } + + kfree(folios); fput(memfd); } @@ -388,10 +392,9 @@ static long udmabuf_create(struct miscdevice *device, return ret; err: - while (pgbuf > 0) - folio_put(ubuf->folios[--pgbuf]); if (memfd) fput(memfd); + unpin_all_folios(&ubuf->unpin_list); kfree(ubuf->offsets); kfree(ubuf->folios); kfree(ubuf); From patchwork Thu Jan 4 07:25:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13510852 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 2F7C8C4707B for ; Thu, 4 Jan 2024 07:49:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B0016B03B4; Thu, 4 Jan 2024 02:49:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 239256B03B5; Thu, 4 Jan 2024 02:49:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9B626B03B7; Thu, 4 Jan 2024 02:49:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C961D6B03B5 for ; Thu, 4 Jan 2024 02:49:02 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9F63940367 for ; Thu, 4 Jan 2024 07:49:02 +0000 (UTC) X-FDA: 81640852524.21.34E4031 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf20.hostedemail.com (Postfix) with ESMTP id B1BFC1C0010 for ; Thu, 4 Jan 2024 07:48:58 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FZaL59Yq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 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=1704354538; 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=h3emmYlObQz4YBWvxea89j9SxvY/uMNOIgA3BV8wvNU=; b=M5YTJ7L8n/KP+mWn/DZRPNPh8cDEepNcYWWX3RNWwPgr/HkAnGbNibMsLkyQSYbUjjWXZ+ 2lUvj8UgJvkAQWpuF72NrlYU7VIBEJr+SWX0W8N9oObQE8ss49CaRFBvZD7fPx8u7U0x3s NVA/gX2UQrRuXiUok7fl6dHsTv0lUdc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FZaL59Yq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704354538; a=rsa-sha256; cv=none; b=gKK+Bo19RepS3aYoWiNXArtv0WNxuN5d+N+QDUAqv3t2kfZfPtms5hRahzuwWrGG4b60PB kenN71TtB5xMXY7/wbJete96E8iTAhktfVCWbmlk19J8l+ryL98P2Towf/hviAL9+6KhN3 626bjWQ6XiX/eQhitkMy823tDyg2pbg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704354538; x=1735890538; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r39C9fURv/tYjjTgfRvVfOiacLkGBXJF65fG2x4l+b0=; b=FZaL59Yqau0/OsOAmrNCNcR739iZndOMTjJDnzXR1hLTLUqygL5fNOs7 ewWVU0uqj49QzSE1HKxa4swdd/gjslqUkjmpjyU5ID50J5HBIAKEGWUJ5 YBAHyUnwJcJ7vbtQuLsuZOl6snxML6WAjLuxRVizCIgG4WBGylqkZWQUF L4pn2Peb8aeYzRefOyQA0LKWhoLeGPIDUhfqP0gLdH+wheRlX+CErBQvK a/zHLb8ahs/NDn2QgAT4UJALoFumGERTCFoODLVBWCt/Geavq2cKjjECR 6MBRssfS5Q/Lj0WgxCuVSDNCXqEfrDfEvd/7F5vwyzsL0bYcO9/yxH4Ca Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10942"; a="483331144" X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="483331144" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,330,1695711600"; d="scan'208";a="14776743" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2024 23:48:47 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , Shuah Khan , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v10 8/8] selftests/dma-buf/udmabuf: Add tests to verify data after page migration Date: Wed, 3 Jan 2024 23:25:18 -0800 Message-Id: <20240104072518.807950-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240104072518.807950-1-vivek.kasireddy@intel.com> References: <20240104072518.807950-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: rmkgknqafzhufgf7moec9p7wm6run9mr X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B1BFC1C0010 X-HE-Tag: 1704354538-136359 X-HE-Meta: U2FsdGVkX19MWjFoKeB6Smd7llPJKJdOblt7AXBqInRr5vJgnXzdhuLkChnTQF5HDQhsF6xl84g+r4QkKHUU5DnjV/ALsmhW5IGb6kkB0Yy06IIgDfyUY/lTAADk4yAK86GMd8KFPZF9bVNLopSy7YeNneHf2Ewc14iZg2NvdHRbdEG4QRxcTpx7APBaRJLyOgjBa58UDEy9Kq9Kxc9uGWecdVMMynuTnX9f+ZQLooq+gCrMcDPwpcA3f2E2cA5MNbd6/YS8F5ah7JzLvU8FhS4fPvMU9m407SwZQOs/FaOy3MKzNYbxcGcvZFORoUkPISEVp3vdA+uOVPD0wpj+Vrp0cLsDd3PGUCHVqPac1CcQyEveQYlEiv5mlFplugRtm0stnD/JdEYPJPfWEkNn8JoP4cqRdCGv/ILACxPAQO1T8prDoU3NPIf6tAG5DbwP9MvC4vX/ZL73QHQ5pmSoFG+aD34SpYtFKp1qE0gv8eutuLowGP/YWV6ZSFDc+XPgARCQsqjiR81yAGLaS3gbiRvM9FYqzAG3Z7mOaV6TbWD/Wjt7c0775JBkUyj2B34RdzWtgwOIiLrLLmuXMGYTzWt9OeDLe3oci2oEkjp1iB7c0yYnFTf2PGRSC2DnyDAtsn3GFCmLYZLpRFOMuoNfVEvWJIMSZO/ZX3vEnhVYxCvQicqEmrdHo+jKI5B38ysVInNQ+svtE2tcGg1dZmtKRvWkuAELdu1hCIPqYUixmRitxxNsnIb54tYawt5e2BPuB/a9FqwStIIUfzPdSrkEBbBmbEF0YcHgW39kQ7YaLPrKpjpZgm0iGSwa8cMVJ2HG1wcYakw5+76a1Ro6MOjEh3vzPb/NovxMgnJ3FoIpVn4bBHIivl9U2ePW6ZOAWmfwcZaZmRCP4QTA5UZU88c2IxWqpU784Z4bfeSmPWUOnpwmnCYUQF4Mkeo2uCLFnbxJz1vY6UPudSwdG54ZX4F V3MgsUpi 6N5A+x4XATLLLMF7DOW+h3lCdX8/QcRI+BtdtBxtF1UgJkc9gwEQf6oyf2qIGkAUtCWo/8CrEzmwuayKuJLqalUrVybwZO+V3vthlm3JmJhmHSLBp2y1fn+V2vBP9a+8+NOMvpHY07faYNOJ0QokjWv3ZWxtX6qQv9kH8zZd4286zRSjmWjFMoqP9e1ICOc1/AYVVeIgfkLZBNIJ2eRj8Oe+cMGse54fxxtTYXOo0zGUdhqETTmzA1h4YBrGRNwHd+8RpoyKrLu0Gd+RlS7cEKLGtiH8pbps+a7bYXWaZaZqJfSYzr39XtKhHxg== 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: Since the memfd pages associated with a udmabuf may be migrated as part of udmabuf create, we need to verify the data coherency after successful migration. The new tests added in this patch try to do just that using 4k sized pages and also 2 MB sized huge pages for the memfd. Successful completion of the tests would mean that there is no disconnect between the memfd pages and the ones associated with a udmabuf. And, these tests can also be augmented in the future to test newer udmabuf features (such as handling memfd hole punch). Cc: Shuah Khan Cc: David Hildenbrand Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Based-on-patch-by: Mike Kravetz Signed-off-by: Vivek Kasireddy --- .../selftests/drivers/dma-buf/udmabuf.c | 151 +++++++++++++++++- 1 file changed, 147 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/drivers/dma-buf/udmabuf.c b/tools/testing/selftests/drivers/dma-buf/udmabuf.c index c812080e304e..d76c813fe652 100644 --- a/tools/testing/selftests/drivers/dma-buf/udmabuf.c +++ b/tools/testing/selftests/drivers/dma-buf/udmabuf.c @@ -9,26 +9,132 @@ #include #include #include +#include #include #include +#include #include #include #define TEST_PREFIX "drivers/dma-buf/udmabuf" #define NUM_PAGES 4 +#define NUM_ENTRIES 4 +#define MEMFD_SIZE 1024 /* in pages */ -static int memfd_create(const char *name, unsigned int flags) +static unsigned int page_size; + +static int create_memfd_with_seals(off64_t size, bool hpage) +{ + int memfd, ret; + unsigned int flags = MFD_ALLOW_SEALING; + + if (hpage) + flags |= MFD_HUGETLB; + + memfd = memfd_create("udmabuf-test", flags); + if (memfd < 0) { + printf("%s: [skip,no-memfd]\n", TEST_PREFIX); + exit(77); + } + + ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) { + printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + exit(77); + } + + ret = ftruncate(memfd, size); + if (ret == -1) { + printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + exit(1); + } + + return memfd; +} + +static int create_udmabuf_list(int devfd, int memfd, off64_t memfd_size) +{ + struct udmabuf_create_list *list; + int ubuf_fd, i; + + list = malloc(sizeof(struct udmabuf_create_list) + + sizeof(struct udmabuf_create_item) * NUM_ENTRIES); + if (!list) { + printf("%s: [FAIL, udmabuf-malloc]\n", TEST_PREFIX); + exit(1); + } + + for (i = 0; i < NUM_ENTRIES; i++) { + list->list[i].memfd = memfd; + list->list[i].offset = i * (memfd_size / NUM_ENTRIES); + list->list[i].size = getpagesize() * NUM_PAGES; + } + + list->count = NUM_ENTRIES; + list->flags = UDMABUF_FLAGS_CLOEXEC; + ubuf_fd = ioctl(devfd, UDMABUF_CREATE_LIST, list); + free(list); + if (ubuf_fd < 0) { + printf("%s: [FAIL, udmabuf-create]\n", TEST_PREFIX); + exit(1); + } + + return ubuf_fd; +} + +static void write_to_memfd(void *addr, off64_t size, char chr) +{ + int i; + + for (i = 0; i < size / page_size; i++) { + *((char *)addr + (i * page_size)) = chr; + } +} + +static void *mmap_fd(int fd, off64_t size) { - return syscall(__NR_memfd_create, name, flags); + void *addr; + + addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + printf("%s: ubuf_fd mmap fail\n", TEST_PREFIX); + exit(1); + } + + return addr; +} + +static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size) +{ + off64_t off; + int i = 0, j, k = 0, ret = 0; + char char1, char2; + + while (i < NUM_ENTRIES) { + off = i * (memfd_size / NUM_ENTRIES); + for (j = 0; j < NUM_PAGES; j++, k++) { + char1 = *((char *)addr1 + off + (j * getpagesize())); + char2 = *((char *)addr2 + (k * getpagesize())); + if (char1 != char2) { + ret = -1; + goto err; + } + } + i++; + } +err: + munmap(addr1, memfd_size); + munmap(addr2, NUM_ENTRIES * NUM_PAGES * getpagesize()); + return ret; } int main(int argc, char *argv[]) { struct udmabuf_create create; int devfd, memfd, buf, ret; - off_t size; - void *mem; + off64_t size; + void *addr1, *addr2; devfd = open("/dev/udmabuf", O_RDWR); if (devfd < 0) { @@ -90,6 +196,9 @@ int main(int argc, char *argv[]) } /* should work */ + page_size = getpagesize(); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); create.memfd = memfd; create.offset = 0; create.size = size; @@ -98,6 +207,40 @@ int main(int argc, char *argv[]) printf("%s: [FAIL,test-4]\n", TEST_PREFIX); exit(1); } + munmap(addr1, size); + close(buf); + close(memfd); + + /* should work (migration of 4k size pages)*/ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, false); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) { + printf("%s: [FAIL,test-5]\n", TEST_PREFIX); + exit(1); + } + close(buf); + close(memfd); + + /* should work (migration of 2MB size huge pages)*/ + page_size = getpagesize() * 512; /* 2 MB */ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, true); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) { + printf("%s: [FAIL,test-6]\n", TEST_PREFIX); + exit(1); + } fprintf(stderr, "%s: ok\n", TEST_PREFIX); close(buf);