From patchwork Wed Dec 27 07:38:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13505180 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 662EEC4706F for ; Wed, 27 Dec 2023 08:02:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C26B36B0074; Wed, 27 Dec 2023 03:02:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BD61A6B0075; Wed, 27 Dec 2023 03:02:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A515B6B007B; Wed, 27 Dec 2023 03:02:41 -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 924F46B0074 for ; Wed, 27 Dec 2023 03:02:41 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5B0621406DB for ; Wed, 27 Dec 2023 08:02:41 +0000 (UTC) X-FDA: 81611856522.30.48BCF7B Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf20.hostedemail.com (Postfix) with ESMTP id 554DA1C002D for ; Wed, 27 Dec 2023 08:02:38 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="MNSQ/3fR"; 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; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703664159; a=rsa-sha256; cv=none; b=1HbJ2DUxZtB0N2ZPRO+oYT9dllP791uCPfAEdkfcDmr/bHpG+QUeY/hHa62ftoQNU9mLZR E3TVF0J8bKqT8PjKDwe6Yv8ULghYRj2KoMAGIhEurv0/zZHXV/K/LZa+l6G6vFilbTkt/w Us+Z5lGdA/Vtq5NiMWbYFMeyC+OQ9Fs= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="MNSQ/3fR"; 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; 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=1703664159; 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=I6nDxDAyUn5ggfKDgL+h5Y65usOZY7/cSVKEXreYCRGIO9l/N/ukAUi9CmrX7yD9Zlo2In Q6AKl1WLASIaHD47gwJBmizDfrl6S2Mo8KwPNvIqj7gl8lst7yuYQzJv4bY5TA4SKnCOI0 PZT2P8sLb6PlRn0FzjqQYD1pK2SiCpk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703664158; x=1735200158; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U5Op4CjhDDAHfKtiDzpe6+9a1b7R8CllBxVTE/TesJE=; b=MNSQ/3fRh98h5tbm5+oPaUyGyeyuWgl0WIV/B+i+IeweWDeIvhPLJ5Y7 IoM8E8YGUKeNqGeFuGKt5xgATI4gRQAsF6LoKKpSCWTFI2Ur9JD7Gw0yp 2KsuzzRG5Mf1+UdmFrxUNpZ/UYZef3KVAb+bUqOhNoFLYQWsmfQgTqGYm CCTED9B7yWDJWX4XQW3G9TEhtkmj0FEj27z7313bpKBPZdn2yhaGSq27Y m9aV2Lpdl6icE90sXMX5hfOPmEDaXlLtwnhMqzEr3oOZUoR8Fsyyvq/07 SRv6SG8E8/Qj0qHaozG3e+//8e5uqTyqePirGF7UW1GpsXYsgMyt/fjFT Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="482604283" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="482604283" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="781668181" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="781668181" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -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 v9 1/6] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Tue, 26 Dec 2023 23:38:17 -0800 Message-Id: <20231227073822.390518-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231227073822.390518-1-vivek.kasireddy@intel.com> References: <20231227073822.390518-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 554DA1C002D X-Stat-Signature: qnk6awnm3545rjeyw7kq5bwtadsdt5qf X-Rspam-User: X-HE-Tag: 1703664158-694915 X-HE-Meta: U2FsdGVkX1+LFyTCeQg8clNVoM80bYOAzlEz7VQN2h3bJ5GBn85pNeK2Qf2DRz8Xvk51v+i5brUXLQcy6fyD25egOSHJG8Bt7TdEe/dcXBLoZN48ykPxF2ACFCDZ8DuQnAk9nM0ZvFPO9MCUejYCxbe5vxYxd7rhVn4Pv7ZujwdW+r825xUdRgGqk7y86LJaI1VpQeYjBRn1mlkKEGTj3MQN1GYkXF8UkxciV+Wt7uYS2Ge4ufkbF8GfTQ0S1CCZD6gbj6Ck2X+X8Zu4TKfCRtdM5VhtE4B4jpjxYvSfvNTjJBjYUkEMpUy5qLrQwd4A3Pnwp9rbHTgR5rg311YfF+IAE0xipXttnybTPua8jcjpkR1RpKCIxJVWq13yrswqXtsPTvOB8RkhiDCjtMnhnqYhGFFErXf3Yc/q/5C1K+0zzaoXkz8YjFkjKcdBXeddCzYdXL1g9kPiZ6UHEDziZPz1d9k/xHuHk6XKWuoxb3a59Xb7YWQ67th+JVTeAjAoJEGTsM3nhcX268PpL8RN8ngDReRGtJ7TBdWHTlMci4FGNGFAkhvVl7s8YYXsqPFBT94Z4hUcZrJ4ZQM1PeC5BwzUIvPgwkQE6p9E0mH/TvRrGdQh4yggB0qWjeuPhzMvzDoueXbGOTieGexODL8oVkrASRex9Bh4sdQH5LEOTroCzRcFmYWe39fR6uMz87fc9TXMNp5m0GE+rZ5q7ph8gBBMpd30tptUDz36WN+7dhJbtrAjzE6PHJwbwKLxh00XsJ0S2TzDcvevbtBF1FWqA6ADsRoRorc36A1MiSfmXWBr/c7jRdodFnDrU+v1KLc8PFFD3Gp7NPX6wq6JoOAkKnDYP9zhIboxcWkKZ1jMJA59Oj9r3lqCGe9ZwvSx3Lu3FFTNmOlMLy9cEBoktshQywoJAQ7rYTF9P8txDJEVZU036y33ZL/pcOmylDr+thMFwG1GkM0MvmB3Vf81ygs LMn7xvLd unTcXGwzrRrCLL7e0D1lQxZA2tl21STDTaIyKN1WAO3AyNWM75JG75gIyAwQIQ9cuhEsHKoifBOBS3ei/VScJT45MNek4gIQOrrlCyNhoiGUisgiOPVo4TiqMgjVjXiackCvFy9c2ECystSxamHavP/sceh38aPJql1gsr1PkGJMtZGt4W/HxVVhSw7LYCKBDgFN1LRHmIkQwVsiGx4t33qhCYOzPvp7qkoX5pcaFs1naULEaab57+hD3EqQmqQaIiHacRRUsIi9oWiFplV9ZBwfWssB+he9HOGfUdiD6IlV4uKQ= 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 Wed Dec 27 07:38:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13505182 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 BA01BC46CD4 for ; Wed, 27 Dec 2023 08:02:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EFC48D0006; Wed, 27 Dec 2023 03:02:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 178218D0001; Wed, 27 Dec 2023 03:02:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E987B8D0006; Wed, 27 Dec 2023 03:02:43 -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 D3C0D8D0001 for ; Wed, 27 Dec 2023 03:02:43 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A2F2D12066A for ; Wed, 27 Dec 2023 08:02:43 +0000 (UTC) X-FDA: 81611856606.08.EEE35CA Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf20.hostedemail.com (Postfix) with ESMTP id 904771C000F for ; Wed, 27 Dec 2023 08:02:41 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ODz6N1gx; 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; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703664161; a=rsa-sha256; cv=none; b=pp2AaptbURJpLFDKM095XlXCvAYmjd4Sn1gRWa2ibZrgF0lFN22J6ftoyjm/I/upaU8wTZ f7OHfbFEA9rRgJR2j9xdCBJGhKcxhR5Jg2SyaqDxZQuXKnbiSiRGPtuq0b5+/fd1WY438L 4CZs/IpWn4FDc0mdpQokTevUAqvHKsE= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ODz6N1gx; 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; 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=1703664161; 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=Fsz0rrAWryOguN+NxEHjAUDo6FiP2mk3m4Fj28yduQ4A2hN+um7cdXkuN3pzBgWKJAhQ4e xLs2XJAbX/hOP3YEdDGGwMZJJOmlEVF0QqA8lZ71stefiKMweFneUs6fKkQ89gze7rfhEe jjBcwvQ8wqnFLtLiT1Un2J9LkSRh1W8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703664161; x=1735200161; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O1c3JIhGATN1dxcRTvUuRK9RhQSZEkJPgfU5c33zkAo=; b=ODz6N1gx6v5tqFdP/JtjKm1gxrq4u8WLF7oenjQ7roV+NmizBpCcFfC0 RJT1aNUTTPBYl5aK92UuQfXeU0hzd1NU/wlT3EXvzTvm/hoO/8obx4fVp UhMqKCdqIhDUNphwaERIVJN43yRR+GfNtzCZYQEzQrtNCMlJYNleEEFNn 4EQiR0F1xFd4ZSbTMPjWaaaIv4DDN/xGajPwgwqQynT8uZnwrFhFXs/Ek 4O48wvxy3rZDeKIbgesSud7orE6JHF2Gvvzz9GGW5U1ZLbtnNJ0r7tILF 2W9xQbDk3PG7VH9kmSvWX+SO+58SRqmy0THKpiRFtDGE14eNNrtQEE69S w==; X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="482604295" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="482604295" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="781668184" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="781668184" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -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 v9 2/6] udmabuf: Add back support for mapping hugetlb pages (v6) Date: Tue, 26 Dec 2023 23:38:18 -0800 Message-Id: <20231227073822.390518-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231227073822.390518-1-vivek.kasireddy@intel.com> References: <20231227073822.390518-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 904771C000F X-Stat-Signature: p6fqgaz5ujfzc3sci9yw8yjcteew3qsm X-Rspam-User: X-HE-Tag: 1703664161-848059 X-HE-Meta: U2FsdGVkX18W8cjR7JRDnD7IXRYpBPqNpCBIQN2N+bv9j7vnNv8MY4mkiXC+aXmepI0mi0kGgrV1Ry7McSUY0F3thE7bZ3jh1f+xlw6beRvbkBog+d7lVCZQM+2mqDNYyMSvvh69mUci3WJoruYpxFIg7oKB12z3XsHdHbIIisaxDk+MRH44wxpMnlIfJYV/vDmuohDa7z0pqk4RkYw5GnKYJEi1mWU+mt4KWfYPX7glgwLyIDKWK1Kfk85iWVm1K16RQo3bmA2uM2oJzy5nhyK3m+2tDNDJffrRUvHtDVcJHchbBVz9+WwmYL5Srvdaq69+w3BJkzPwpUir7ZAD6+rd4fRUDMzGMq/laIkIGGni5p7LL5EzlUYn1ENOglAw/Rm6CoSuNMpXZpZvNuaAIBSzZJqJgjwS9V+82tcmeFWqdfD877x5EhZ264IUQ5SVFKlFVTPzRuXl+Z6MijNWp2mJsa5y3LbQF5UOIBsMs8m7/ZtpT8J7pjAzaAbmrXw0A9YYnlj3YcB3XwkerpehuJyeZLZ2O17iAHpq0aYtXyHlMMc7oUXv6/cYfmuIc1aLFWMAiNn3jj9vsWxLKT88AXdK3ESXGkeJS84s1Spk6i1FmbQWAxZ2iHwo7u75any65ORpMvwf/szo9a53cU0ZblcqUeLcbkx3QJMfF3VPHMkwbpt944gPHQTvwmpxiK77cypoRRBz2BOa7q06R0eTWbdaNu8MW9bYEy1F8p2vWvZ/rLMHjMOGF+tU217Y7B59xUbh95Bhv1DA5/3RXF1F44/iNa9o0ttmYASO86fGveb44qFXQjmcNrponBiFJBY1GKMWy7XOHNIWe/aC6PHNxXTzaXnaJmQW0ie6RaafGT8Nq2JPOEGCXVe3e/sguYa3ODdCoON4HN3oBABBenuZ7lsV/JfqSXlTgQvGhRpFseMX6coQERGDaB+cBtUDcig48S/hhgxK3Ii+4W0EYlA 8ZJs3kY5 J8oiBwtKt2SXW8iE9JClWlvNQ5Y2a8jZt3y9ud1Hdd3C2lt1Ci6t9QA04haKomWDT43mBs0TUkWXq/3N9ZUO07QPDC82OPSJs1ITFRg5sjuiXNercZpmxwaI1pMFw8L03xRI/+AZwbY3qMJR/znSuV6TCG5zP+n1p2s6MTVx8fq4H9TJh/lUOlblEMz5wmOsOnc2TnoReHD5T3VicYmmzK5nr2FvE/UvTndvO/fK9liL3JocCuiYhDQQCyQ== 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 Wed Dec 27 07:38:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13505185 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 406F0C46CD2 for ; Wed, 27 Dec 2023 08:02:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BF0A8D0009; Wed, 27 Dec 2023 03:02:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 547A88D0001; Wed, 27 Dec 2023 03:02:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 374EB8D0009; Wed, 27 Dec 2023 03:02:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 198DC8D0001 for ; Wed, 27 Dec 2023 03:02:46 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E45531606C3 for ; Wed, 27 Dec 2023 08:02:45 +0000 (UTC) X-FDA: 81611856690.12.4E0D88C Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf20.hostedemail.com (Postfix) with ESMTP id CDEDA1C000F for ; Wed, 27 Dec 2023 08:02:43 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PRJCulGW; 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; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703664164; a=rsa-sha256; cv=none; b=I5uWBU5I0Kvz637Q4Rtj81WbJYu8Dhja7OZ3XFOIQUq2/r4/Fa5xrGntgMQLkK7gRlfeJT IrE5Z3f7FC3pP1f06HEyDSUJFpjgLY9MEk3jEbqTIpqhnlljN5yvivoaFkxz1A0G8UPuFn 6I58G/fSxeun1uYBXH1ANh90u6Dt1s4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PRJCulGW; 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; 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=1703664164; 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=Y329FJ3UdvBikNiGvIBNgIzWPQ8UL0tCZzkHiz1VAuY=; b=pPccA3m3Nj//iJlPLHlOmmyId0vHPrRNeydJzbUCxprENirdm4WjUtOj2VDFDKPfyUFFkk fhNdXW6qOhW2kfzzE1teOsGWV+545Wop+kaRZFjmZKBvh3GV4zXVRFfChoO7HCz+FLm5Tw kALnMcwQSoOA8LAzpQUgXCRyERWCpC8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703664163; x=1735200163; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ysvgkra0A4hz+Fh+hWsAQrFZ6yvogefwsiMZ4geO3JM=; b=PRJCulGWJFmATD4aolaGk6qK5lZn9FCnhsNaJZ3BuogNV37Jdx3EzWfL x5LQx7i4R+F5U4NQ9ywIybpZyAyqogbncUPSbDI8fiaeX/XT8FJPLrsMF YEtG6LFp4JMX0kDIy9JqWr8h+2JeYMUgAUbM0/420XtHVut+cf6eJrOJr b9/g5ZyhNpQy+diRgHsKxnlnFl25NOZEmOuWH4hd4oIdwVD07k8Gz4sLb CmiTITCqJSZGeKhk9qk0D5ta6QmpkMDefpfdew1/1x06l3/1mjD5eScA2 5T0sx/SnMgmbseEGJv/h7FjFymPb644Tm6h8wXHkOwebdy+lmrf9XAasN A==; X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="482604304" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="482604304" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="781668188" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="781668188" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -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 v9 3/6] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios (v9) Date: Tue, 26 Dec 2023 23:38:19 -0800 Message-Id: <20231227073822.390518-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231227073822.390518-1-vivek.kasireddy@intel.com> References: <20231227073822.390518-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: CDEDA1C000F X-Stat-Signature: ua9566qsym4q1fuc4w8pekffiheec8zo X-Rspam-User: X-HE-Tag: 1703664163-983722 X-HE-Meta: U2FsdGVkX18fnvSXcdnyoFg+ZZ8Ij4Irx8LHqr5yNagLlIL03Xk0PEJBRnd85UrzY4ndlK966fYF+jOt9BGA84QaBcQlbuAJH+pImoZ7bZbbjiL58I+oG3jy0goxHSyHNtfpU85h8y6SL0ICRoyaDSada8D6zmOaGgp4h0Oyargu8sqzy43pb1SF5EjQ30UbNu+bILgm1xP+rKPL49WvvYSv/oAX3iFCf5daM15jHgpY4rVeKxOPj1qVPziosbjW5MS3LDhkbzUVOWULC6ooR0wS6VQgsVqOwNJzM2dRwp1lAtQ+YB9JhEXGtU9CpS0xyFMVIHySOJwUvJKLdF642s2UWHls9n7vX0Xvq8EvHnOXI+9v39zy1JbdMevaGjog7u3On0VSDF5lTtbN9t4ua8lZydffdZZ5wphaWNmg1XGlA8Ph9lTgNbUCX/84Jzy9HaHYghbKhvWa8rLoilF7wlFSJxM55OYiNL2CDMzEDuWhnGm4hR3l/bkJstrm1wBqVF7SctBB9NRbGLejoNhxLx4XslI+9kxwn+3hggSYg56iB9nAXiEc3oMHh/ZcfnTs6t6/077LhjcgsEX9RK89uiYkDsW2gIoWncnHZ0F09Lm0e8BI0397CMjpFcmqh8NoF84t0t4ZRWr9YF+A69ZOqWEcElYNRWruiLXrekDxEWwGOlB6+FCFRnXzkDZX9XGOIMBIbpfhRGBLB/CNJJULBloyPsC4R1foM/VP7u+86jpC6T+LDmLQrv4pMeZ48ftu4at4lFgNBiyt1GotaVvUI2cRBXK2RGl7OQ1f5BxBLFKM3wgE5g8feYlP2s/49PebNT6d1UHf0ftqWjiQ3tgB0mCEtf+zKEf827s1VGcYJj87IlmfcH41rdb5WsvnYREen5jTNz7YCx93hpKW7Ue0ahrH0QHMmcORsbXqUy2Nsl4LY0v8gVfBER7EUf2nnDau38bREdoACGWYhW943Tv 6EhgYjyz GaMVQSZ7C8Q2BtVp1lNNCCMDGGEd80a1j3CW2lWSmH0HNRdIA4jrVBE+ZvmqTyoV/bZN3LOSgH3h4c46Q9eATeSPG0w56+c85SPXf2RRUFbjS6gxzdIf07uVnWI7zT0L/FV46HlUHXLNOPdDZpOkJhg+1AZ0HcvJHPNvyhaLlIbf83uq/qLV6FtUQ7OodSHRuGxhVMhxMSW7cfotP4d+P9Uk8+JIjnPdtw/rO2UzfY81g/esdqVUqjAtnmu0whAhwsempRpZKGxWzqUNEuINBMSK318DcJlBO+R9ZQvtJRzI5cbxmOBpTtZys8/l0pfLkcHg16HR0dlsc5GfaZcJOx+Cxh62gldZ3h/kOllMiBh9Qxnk= 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 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 | 149 ++++++++++++++++++++++++++++++++++++++++++ mm/memfd.c | 34 ++++++++++ 4 files changed, 191 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 418d26608ece..942d2e618253 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2472,6 +2472,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 231711efa390..42eb212af73f 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 @@ -3410,3 +3412,150 @@ 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_user_pages() or unpin_user_page(). + * + * 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 page **pages; + 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; + + pages = kmalloc_array(max_folios, sizeof(*pages), GFP_KERNEL); + if (!pages) + return -ENOMEM; + + 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); + kfree(pages); + goto err; + } + + if (nr_folios == 0) + *offset = offset_in_folio(folio, start); + + folios[nr_folios] = folio; + pages[nr_folios] = folio_page(folio, 0); + 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) { + kfree(pages); + goto err; + } + } + } + } + + ret = check_and_migrate_movable_pages(nr_folios, pages); + } while (ret == -EAGAIN); + + kfree(pages); + memalloc_pin_restore(flags); + return ret ? ret : nr_folios; +err: + memalloc_pin_restore(flags); + while (i-- > 0) + if (folios[i]) + gup_put_folio(folios[i], 1, FOLL_PIN); + + 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 Wed Dec 27 07:38:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13505184 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 258BDC47077 for ; Wed, 27 Dec 2023 08:02:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 858C18D0008; Wed, 27 Dec 2023 03:02:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 792A78D0001; Wed, 27 Dec 2023 03:02:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 597728D0008; Wed, 27 Dec 2023 03:02:45 -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 405EA8D0001 for ; Wed, 27 Dec 2023 03:02:45 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 167411406DB for ; Wed, 27 Dec 2023 08:02:45 +0000 (UTC) X-FDA: 81611856690.20.719B8B8 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf05.hostedemail.com (Postfix) with ESMTP id 0130110000F for ; Wed, 27 Dec 2023 08:02:42 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=TMYLaSBB; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.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=1703664163; 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=YFNi1w3YRszc/94/Iwbdd403edmN4INLkCFGCoUTxuWFUTV0ZEjT7pjc6jmwSsQ5AABufV K2l715qa6VaZt+ad9AR1pIcxXYLnljLujEdvbr/ept692w/btt/fFLw3ew9UlFpLJPo6cV 5XQwVw9Y0UsuqikpsK7B9EKSh0VQGQY= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=TMYLaSBB; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.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=1703664163; a=rsa-sha256; cv=none; b=udDJxDMXK+0u8J5+XkQ+6f52pfwzQM6OEnw/7D885YOEsauqB2a65SzMmk+3mDPD1eC37o 307x/uaWNXbT2Wjrn5xV06qPX5wrUZs9CXYh9ffxLX4ovvauoxv/UyqyCfpL08leGtvOFE tyI7aJXntZJN/GuFE+fAn5ufY6vy9NY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703664163; x=1735200163; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=srh+pDeiF4x93Sp9cmg48dB8ma2fshhkeNl1Nqm5xC4=; b=TMYLaSBBMLwxhXPorF5qwsnhczhByku/EuDJpnTKEbGrj0WS0CK0H89f vKHHJ98jsCJ+YF8/DwL8HgcAiG8ZqmUs/fW96MMO/rg3ighrHc+9ElQpm AP4+Esfl7TdKxvs0U8F99eSbb99pZwMF9Wrji/KrWE/AypubkspfqFrxi tj1Rk78v0vzilX4tIXWXuQZpNUIhnSbp7vC3TriPthGN7JNeQoXCxypxe HBgVf+t0fMnLMOeLSo1TjaZhXfunnLXgLGauHR4tqeqznMrNNrB866kyX o5vyKqF1xsKurYnwpkGTDaahcvOo7XaGK9wde++EqvdyeYoAvt7USNY/n Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="482604313" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="482604313" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="781668191" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="781668191" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -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 v9 4/6] udmabuf: Convert udmabuf driver to use folios (v2) Date: Tue, 26 Dec 2023 23:38:20 -0800 Message-Id: <20231227073822.390518-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231227073822.390518-1-vivek.kasireddy@intel.com> References: <20231227073822.390518-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0130110000F X-Stat-Signature: 746o94hyqp4ecjbitisouqok6si9mf6t X-HE-Tag: 1703664162-732392 X-HE-Meta: U2FsdGVkX1+R76TntkyR/S+ytcpj+fbwcIF6QC7v9QsJQBW73zZKE5UNoBchR1rJI83DjvR41yoTIwJq9O/humHd6t7ilYt7RpZEuMgHAxwonYH8hkgFPTfW67EhQBtLmxEr/Gl/dJeaRcaSAgcZdSFPxHeAV7aIRuPI3b33GoFpHn8HnSlFZv1rKEXUJZOGnQWlsTWzcGZ4TcoSqQzXH9C+mU5azT1E1Vr2HxXzUrCLgcenygZ/eFnaQyEspD1mZ9YyFelberE/DCnycot64N0pn6PQMEbUajKVXU0Km0vBK78gCcQf+0oo9dZUuQg5cPIuwpYge41bI36ZUDwYkiEftGCQqOtQOhLZ3K4ZKYDR+GeUze8J8+fFTEtc1y0Imv/DKBSBmjn4U28uGuGvTBzHCwp9DusFeUm0gzyQ/A0vxJmdHFmUIIU1ixjHcLPcRipQJtBv2hr2hLoVjTmI//hu49apzQQLOD4fliDPeLZ8z2QckA4YBQuCyNOQkIQUockO3cA8V1KrVOYlx5SYz/D8tywkRImfTTnltKXBcp6rmFwl759IPLDjZqO1HVGt+KqWcy3RFSgl8qydLOdsj9ZxlgjPrtXPHMF9Xd6tAMFXyDm8g7Iq4JG7fR20ZwVLEHewNNuSS+R7OLjxj3n4Dqx8uZEu3Rwn6CUNKQalvQSdVM7KXIyBRTjsEIupLmQsQ0J++oKhhBsXMk4gJGPNn66XBVXu811ycdhZdwx3Aqfs/vs6kv28x3aCwaVThDP2sJm7I+fpqcvjNa3uTXqfm2bx1oMC/8elAZB2We/0IFZojoksgDZoly1RdldtNSzGv5d0jducUSTept5yb45z69wmHM8Mb7DmktDkc8DdDX/lCsrfFdK/fAqxDpqyMGDPVT9FbQ/XkLlJIqfKEq436QL6jNBavL9ottUBq4N9EzfeeOYfgeIaU4Xxj5WfSULyM6IVkQkyiwjy+uf21MH +daryvcl 4NpRgCog6xUAcqUf3kZ3opOrQOe7vkziniPT8HQXEIYwJ5IcF3J4rG8OOYLSBM+U/C1LUZNQ1Iwg9qh7NxY0Xd6nBGsRWOEvYla7f6lv0YxHcIr/XXexPIJoEhbGen5Of12IHqcMw21Xon11RbThRJlEAWahAcHeOVnm0ropdxKtSkuVuka+nfZD5A5OsJlVRD3F7GVyfAFJfjti69ug2Xiw3rWNMbkb/ey2994OubgyjK3/ndWcxtqBwf0bHcs0mAAoVSZJol3pI1zfmHEgw7XX8hD/4ga4v++S3TCSShKMmJG+djPdLAfn+2g== 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 Wed Dec 27 07:38:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13505183 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 04AEBC47073 for ; Wed, 27 Dec 2023 08:02:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C4E718D0007; Wed, 27 Dec 2023 03:02:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BFF278D0001; Wed, 27 Dec 2023 03:02:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A2A978D0007; Wed, 27 Dec 2023 03:02:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8818C8D0001 for ; Wed, 27 Dec 2023 03:02:44 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 51A321406CA for ; Wed, 27 Dec 2023 08:02:44 +0000 (UTC) X-FDA: 81611856648.02.001190E Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf21.hostedemail.com (Postfix) with ESMTP id 5C8911C001E for ; Wed, 27 Dec 2023 08:02:42 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Vf5ExOCz; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.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=1703664162; 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=AjirTdZ1/+2r7DM5iaSbg1iK9Y5ayl+qnV7mb9bRMUM=; b=WehgLp1tuy6UvqMpZ1vtKEwIY/bR/R1aQnPO3lez3OEsmPAlYfO8H/kMEey93j0x2ISePP U97UTyvBgEyWPnJ+QiU3Inuon8oyFn/5/VRvh/mSyQ/hlex4VSqlkCadlP5vBZRes0gZlo oVpSjdUsfuaeyNTCcPqg02nbpAf5N8s= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Vf5ExOCz; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.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=1703664162; a=rsa-sha256; cv=none; b=3dS8axAXy9ajRKb9Z+PZ50yQ1FZAx5Za/95p16q8+lf5c5nYZMCtBqoBgfbz42/GiZBnHU Pg2dN2kk46fypq8ZfcR5cFRMoRfnCGsQJv0QWL/x3lCNU6XlpqMh0RepJvuQqaKhLW1yXN Hs6eerKDqfrgeXTjRsE6hzTLccSQsmU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703664162; x=1735200162; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+D2WzD3rI8NbEmKw61bSPQboFfyOeN2u/DR9dqBLiB0=; b=Vf5ExOCzxnRABrbV3XLQGE2yPvVqJc4KshcXosg5hdzuS64QKdrMJb80 j8qKLzLxPz0b31fcNAx5mcz484AryqZ7hg3I9uXtcyrYCItKJPxvUduqM z2O7IQe/cH7KOJcKGt374iki38XfJjbmwbUCh4omsS4Jv9xa5TjfwPBgC 7pkPZcDNR+iUOL1r2Vu9nXL1fjRvG2pPd+OCeb7nvoRD0qi24NHtRnfCQ 2hr7R4Z30Nka7ikDNzphZBpin9Qcf88n8SVwe5RsJijC9DGZFYte5z297 G1nMn6jO2Np9qpvNnKmYext/PCqoOFpY2mqJt65tPonRk4YRbrVc8sK30 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="482604320" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="482604320" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="781668194" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="781668194" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -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 v9 5/6] udmabuf: Pin the pages using memfd_pin_folios() API (v7) Date: Tue, 26 Dec 2023 23:38:21 -0800 Message-Id: <20231227073822.390518-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231227073822.390518-1-vivek.kasireddy@intel.com> References: <20231227073822.390518-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5C8911C001E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: mru53ewtuhz9hp8amgqs56o75utczu3s X-HE-Tag: 1703664162-768529 X-HE-Meta: U2FsdGVkX18um8z9Vyr0zdN4pNAAXfpnGftjiBvMQ6oiLxk016stz2Nvduh7aW2qWanOxWsI1o/HHNHkkLWcsE7uiURacyO33nMD/QZFJw3ic7FBmko/hCQogTn47EFgJglf4W9OSTNr8kpn26ogLw5AvQdAQUKCCCbgyDttYtLl4+FwSRULC60MPlI0oMw9wocjEkVQ4tTWsqfd7/i3FaEQ9uI2eBdXYza+fSxscJDu/3xXgOMGZoBZCMiaPh6cfVXeR1SsZ9AzrgPIwma63B74oLKLhmoFj6Z1r4AydA8ByqIxYapVzB8gtFotybi9HtdF4LL64npP+K5fNasVjB0uP4GXWvaW/pAsPTYWv3CEqleGclUnIo0gNMuOLuBxsfaiCNWqf8lgVXGK+t9R1I7/ejuc63Xx99dgsA5lgb3jaeb+UPUADYx4Ed/H0RmkIawPSnbvVYvKvt/nxrYHo7gDbZ3Irx6y2Picia2QMWIXNwxihb8TY90KgIzBqTJVnUINuK1ucFhgvkVMndaUpAPCqmfl2o9OIo+uvHbdhH5DNQurUNRqfwAWqsDSx4pKJKQfihKzREGpyZu27bQ2CzIDhabwLJmcLdwF300owHMtAEBS/tiv1XHhY2sabtLqihpvZ7YVWm9zfZjyasJMMMGb6senFORARQaS6kR2mvqDbrZ+XCLFjdYf+998f8PSRnzI3ZFHDBNd3B+qmxH5d3t7+hn6wpvZf5EOmWSwiMneU57DQXRgSWSo51KBmuDhUhzzIpKDn8CguAEZFA09myZQyAtUK51nyJQicviK1rlp97FIJkVtQ6qkPWcVkXN5g51hjWQz/wHCyAwBi9YW+hM9bKL8B+Ww5j6OgSGFs/M505t+5pY5Bz0dtFCVr5Z+2RGDRgotZnJn9eNhiRnnMJjTa3My2TX2kbeUjXNToujPQdN2TP5aoA== 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 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..8086c2b5be5a 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_user_page(&ubuf_folio->folio->page); + + 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 Wed Dec 27 07:38:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13505186 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 BCBFDC46CD2 for ; Wed, 27 Dec 2023 08:02:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF1468D000A; Wed, 27 Dec 2023 03:02:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A77D88D0001; Wed, 27 Dec 2023 03:02:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 87B648D000A; Wed, 27 Dec 2023 03:02:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7278B8D0001 for ; Wed, 27 Dec 2023 03:02:47 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 47590806A5 for ; Wed, 27 Dec 2023 08:02:47 +0000 (UTC) X-FDA: 81611856774.25.8538749 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf21.hostedemail.com (Postfix) with ESMTP id 284BB1C0003 for ; Wed, 27 Dec 2023 08:02:44 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="eujCaL3/"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.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=1703664165; 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=DiosQ1pNOpxJGspGck4n7VUWEALeBATV0PX4Kzfoli4NToDW9OjqPOgEsLUvHnHAeko86w Bou541ZIzb5kpwsCe8pKxedfm2eGPdAqfgfFgpwsnwDXuF/c5CgjEvN22AmB3oPMXlFelN Oyumy6MQqnuLSMc2YZWGTXOsCSsrox4= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="eujCaL3/"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.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=1703664165; a=rsa-sha256; cv=none; b=0MnEdpfIZLkgr800B2rJ7MF6tEs/tklHe7g8te2ye1S8qG4ewm55Qwu0QbGOUvOYWn2p/d PLjK1J4laecSCPrL7t3/9HJImCq8cEiP+JIa6S7vLCRTmEdR/kv/pXYHBtw6RDmtjyNC4d idqOqhpybCRNTLdMBRkg6EQ7LFfdxAE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703664165; x=1735200165; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r39C9fURv/tYjjTgfRvVfOiacLkGBXJF65fG2x4l+b0=; b=eujCaL3/3LpisJpHnr6ynVIL3YfRZcRWTW51NdmeK9vISBWd1eBGwx0Y mlRqJ0gBeNnERr7AQJhOChAoA0jvCfvcqhAaa4D1T9DiZgIuN0h8saEwP fOnJI7eH+UN2Qv/0ZveQOzhI00ABP60d03u13flCEq6dKj8DulRduPWz+ RHkE1xv7TwbhVpfVpqjKZQcB7vSFOHv5i0AankbjF/4fjuXnM4on8dUWh m40NQonKQ0cC/7SJwpaGq6QUhlBfGK5HbxkDMnLkXyzSM8shmQkOh55rB IXF0lDgCP/8kM2cxQwjKATDL29zYW09CSFYfOpYFA+LXqM+2EQ/jSvPfi A==; X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="482604327" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="482604327" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:37 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10935"; a="781668197" X-IronPort-AV: E=Sophos;i="6.04,308,1695711600"; d="scan'208";a="781668197" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Dec 2023 00:02:36 -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 v9 6/6] selftests/dma-buf/udmabuf: Add tests to verify data after page migration Date: Tue, 26 Dec 2023 23:38:22 -0800 Message-Id: <20231227073822.390518-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231227073822.390518-1-vivek.kasireddy@intel.com> References: <20231227073822.390518-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 284BB1C0003 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: axio4t57p6wsdwokaby5ettaxuij5gmr X-HE-Tag: 1703664164-734125 X-HE-Meta: U2FsdGVkX19/TjoJ+xE3PuAbB8kMFeQM0nHjiHeMtoZONybpGqaEpznIv+71wShfV0fpR+ozo6vk9el9ImzSg7DCcrdSVQWH+XLlx1wSrT+xPns6RYh51S90M7hJP3rwGHfp0sfm7vHF0a0kqYHnjH9E9sCaktELzWSTSmpNSJPwA90Q+fjnkdwfchZ6joEa0PF/SmKXBTawQH1GDQq2p+/t4ujIz8KR3Ipi83gs5l9OKA6+LVH5pK62wFxYuU1PfQ8TJG1snhcVkws4jDlu2+S776FPZuVkKum0L0C6Gy/TgdcTXRRCwqW4PgTYuYe/Z50my3sELjBPMZpZLAZzbfKc0A6QrhwaA5Y/b8OIazfWnJIMGLtWsdXfmTikHWONLJGTwcQoaGxPrRHCGZVu0pT19UwPfel9L4OnyiccBF3kGbjd02800e8cv31ze9Pg7xU8vAgaVlP+3uGlywYPfmkT2iW0thOVUNaleON+mQbb8UHAwKb3xOlhpmFykkAiHuDQpmR4QAPqWsulaMhHuvmENIYtXUmXHEnTv5y2vyCoeH7pGoEbn+iOvq3v6Ah++6+724CjfYAgUThJZQ/1wiq9upkPMwVI3M40B0ROYxV+tKwrY3hz/BNfFSpHiNKddofd1SIwTmNQoF2M9kzDIwUMVohVp1HpXHo4HpjFxJDPGcKchbaLnJfaVR0nXlDgV8tkU6q3b8F3JpHMfiz8LfGRCrAhfyaWj2pVC4ulPPzLuiowHnz3Fi4ntPrE5VknKKUa4AdNXcNjbR+Ox7jUMfi4fZ6tRyJQ7uDB0kTwJSbPgRAqIwIZUFtSfPF/ef+VeX/hLU+Q4Aw+MVPI/cvHm7k5ycLnVvcX3LGhsyJz4nYjeLGGZZOZ4Ow2aPITx5RF 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);