From patchwork Thu Nov 23 06:44:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13465859 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 29FFDC61D97 for ; Thu, 23 Nov 2023 07:08:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB0FF6B0645; Thu, 23 Nov 2023 02:08:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B5BEA6B0647; Thu, 23 Nov 2023 02:08:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 93B446B0649; Thu, 23 Nov 2023 02:08:37 -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 783166B0645 for ; Thu, 23 Nov 2023 02:08:37 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 50B7580834 for ; Thu, 23 Nov 2023 07:08:37 +0000 (UTC) X-FDA: 81488341074.25.6E105CA Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) by imf09.hostedemail.com (Postfix) with ESMTP id 2B64014002C for ; Thu, 23 Nov 2023 07:08:34 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QDWqRNbd; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 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=1700723315; 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=Bk1tKqMZbjCtIJiJE6D7fxqDbcnv8p6TGJDeIPokPsq12Jh3OEzLKAmdzrbOJ+juhma9GJ pEjjHCouAYqUFD0X8qQ5KNdGXuzU7WdLyu7aiX2H3Oko1YG3YGuds+kAuULuwntk8vPTuH JGCK3v7WNBrskgEJyX4Hx5DpbriBsx0= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QDWqRNbd; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700723315; a=rsa-sha256; cv=none; b=j6Zvreog2YcsEHdLtliCnLSuzyd6+H/aShYsp9LI8Mbq6uJy3Ht/k23ybkh0S70EwihLgg c9T0HyVuenhGVHlUdOTIchm6Z0v7kYijJYBmVtz/gTQUS1OHoO5F5str8obDUqCuLA2JPL QTv1kN77WHa1eQOsDanl8LxtVv344g8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700723315; x=1732259315; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U5Op4CjhDDAHfKtiDzpe6+9a1b7R8CllBxVTE/TesJE=; b=QDWqRNbdWXACEU9ruzqBiv2e6l1eunZ4Mai41qgX5qprJAD0ZYxaF83K F0mVnYT46CMMaTQ9QzJJO230+Mpf/OGT+APmikwXNd/o8PUmHKWisoZty xh4eo8d1fhvzt3btPNt6UkWdRHR5Yq3pldVWxI1qUPFB1Hfa/9uSlab1y ZcyvpEMqvM4UcE5s4ggYVwj6Kyp51fu55/MrVm9DOlx421v5orcWsuwmM Eai1Gz0qgJDVNbs65/jMd39ua861VvcUElvJoQAwQ6QzP1hxfPYF9Jvt1 HsDYnvMw7ClI1qYZDB6glr0smb9VM9w/xrlWhv5v/NP8h9OcSYCogaTYR A==; X-IronPort-AV: E=McAfee;i="6600,9927,10902"; a="382612317" X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="382612317" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Nov 2023 23:08:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="8757714" 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; 22 Nov 2023 23:08:23 -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 v5 1/5] udmabuf: Use vmf_insert_pfn and VM_PFNMAP for handling mmap Date: Wed, 22 Nov 2023 22:44:39 -0800 Message-Id: <20231123064443.1035709-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231123064443.1035709-1-vivek.kasireddy@intel.com> References: <20231123064443.1035709-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 2B64014002C X-Stat-Signature: cmzdtodcmexyi6mghhg8ohd3ebrnu9zd X-Rspam-User: X-HE-Tag: 1700723314-841956 X-HE-Meta: U2FsdGVkX19VWhgvHsxS0da4YADcTX67LbINaNyffd9GJiqtw3Aw8WGNyrXg57fYumU8TtB/m5Y4ZChwCeEO9ujDV42l0XeJjo+O5Y6cDOgaGgWQYzG1VUa7mm/+9fJQcQ5DPxcMQotg0IkN6ahzhwcmj1E2qlQ7yDOq/45WolNuCbAjpeF4QKxQEsNC7LL0OHeQHz1onGBdkyU7WbxGCLI8Qh+znySi9M6STtefZaEmz1i+S59U3/ErHL68aRRJzGLCB2nUfiUgLt/x7HOt4V6qONrXhwhR4BD/DyWleMVnRK6ihbyOc2eyKcc++8PYHe2hKo4Zz2r3f2jOf2A5zXM5KnAjii9XS9zOdXOrc4hrYHqd4HfSfcNBDqvFrwmYIHATYEgrJMoET4Kn6tQsdR1aCfFQ4XVTKRavvIViYrMtZufBDMi+DXh1d5US6xk38G9xvLQnJG7oWRnjrmC7V9uJEJtx0tyMhIj9dlqq1U6qqc86e3GKao3pdBfcfrsc+XWz+4HsB+kes/Pbzlp6RNf6phKrxQGXXs2LFvCW7bbKarbKuAi/Y22wjbUcZXJffmg8IJjSmuox2oUyKlUzdJR/DkDZio+/Ct/ZQwHPFiU8HVlwdIwg0M5D7XjpWnGvHzTuIojXLOzIYb1hpicdGgYZSkLfQTRjOPgXGUs+kuPL5YuJyCCuyb59D3eprm9se/a2jbIxuP6VaxqAn1XdM6vfACl2XAhQ5Fqyr/JKXqM0vtHVbWTNn+TbPHp97b6Vil9x1tcZ/OB3C3Am2IfxFDdnO0QTld75RUNwHDZuEn4tCB7DPFH3f1V0Zj3HhRYMaG/B/mTvhU+JzlL0xmDm3M0jP2b7DSd4AOFB1b7OE4opYCgYrsXiGZBdqQHsIuF+1OhHZfH3Y0cYfMRsz4OoIH09alA7zxWaw4vNPBpqeI/9rqxE8MLFNsLEv+V0XOyNKpqP7eoboTqMuNKG6j5 ajE68Upn qZ/4iXU/H8TynNvYfatA8QktX+BXkW2Ffe1TrfaVnYjdtbgsIRozJ7i6aZ5r46DUjStRUNkwwMMAQ1GsaJe907gzyR2boeYRPSPVZ8kZdJ21m3pZJM0FP4QWdU8ZNsGVlenxZxzLNp03umZ7AQgQ9zyyeEV8urjhxu5I3x2IBreraBu3sXQ73pgJu4OjWIBC6Q8FnCKRKXk0pptiJOh301OqZ8uJRaVYm1aMqBGOyWoZ86FvfBfoXcw4aRIqze7RFbIju3OYxIZWokIN1C5i1GTE0Ox4CWhTUpw8Kh2gT5kkOXdo= 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 Nov 23 06:44:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13465860 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 2E2F8C636CB for ; Thu, 23 Nov 2023 07:08:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 398366B0647; Thu, 23 Nov 2023 02:08:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3487F6B0649; Thu, 23 Nov 2023 02:08:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C05D6B064D; Thu, 23 Nov 2023 02:08:38 -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 020E26B0647 for ; Thu, 23 Nov 2023 02:08:38 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CB858140EBF for ; Thu, 23 Nov 2023 07:08:37 +0000 (UTC) X-FDA: 81488341074.19.D1788B0 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) by imf26.hostedemail.com (Postfix) with ESMTP id CC82B140014 for ; Thu, 23 Nov 2023 07:08:35 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=h7PGF7TU; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf26.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700723316; a=rsa-sha256; cv=none; b=QhvMVlnLAhOt5ia/99L3q6gSM7RE7/HBiBbtPDBu77c6mdkC8xDVG6m2V4MhZ1T+UNPfQJ dfZkLPdBtxKqBt8TONRnGFzlE2q1BFBlut4lg/yfU4lKH5Dq9pqadKNOPseJdyeMF77YOE GzvtKZ7OaflggCBmFx8z0I5Q4oI0c78= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=h7PGF7TU; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf26.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 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=1700723316; 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=1ECyaqfXabDRRCXKYt8XlVHT7cpzTzRLcnQmkyRvuns=; b=brOyY97xo1yobWfj3cu5j4cDBk4CKVxDcSIuySfw45gNmSq+wvVb2KNYPAL8JoKoLvfDgj EMs86jtsxIQdrekiG3M7PODEdQM3NMMIPzYGdj9xr/NREAin109qvmyvEgA9tbjIoAXMko P3EHb3fFc+kInDTSgyQJVjmehG8WhjE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700723315; x=1732259315; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BBBpGkx9U2ta9/cr9k44Lm2x4HshkBuLRlfX8ndU3Vw=; b=h7PGF7TUkEcp8hUH/jiQzOaDkP0yJzVLUIY3ozq2KSuj090xq7UJfTOv 3/L9BXj95HFdJMFdy5TBszuut4KYZ7IjUOjYBkJ3qg6TT2Nb3FpvTfWK0 aBDfASA6j2dUg679Osl2QSkIq26bQZZcMvUF+RudaE4zo8KtzfCN5aPZO dv1U1Em5L19zAkQabpo8rPx1ffI8Wrh4M+Im/IRvJDmAazXDhxZHXVSZS IczDVPd91/z/E3Y96XXLJe4B7Zrz21oAZK8ryy2Ed8ozc/jjxSMXAtbmR jQSI/z8GEWpiA5GGLCd/npLb1QCzTcN5tlZ7IdHK0+6YlQmRBsUv/6EsV g==; X-IronPort-AV: E=McAfee;i="6600,9927,10902"; a="382612321" X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="382612321" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Nov 2023 23:08:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="8757717" 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; 22 Nov 2023 23:08:23 -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 v5 2/5] udmabuf: Add back support for mapping hugetlb pages (v4) Date: Wed, 22 Nov 2023 22:44:40 -0800 Message-Id: <20231123064443.1035709-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231123064443.1035709-1-vivek.kasireddy@intel.com> References: <20231123064443.1035709-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: CC82B140014 X-Stat-Signature: ugaqxmf8hc6q9pxgjuz9c78wggrm4ixh X-HE-Tag: 1700723315-141882 X-HE-Meta: U2FsdGVkX1/k2TXHperxKAHXumZlZmh+W/8Gp2at6FN2FafvwgEj4TK7zxKm3xOEa3c1NYZ/FqDdv3G9pUrqHDm78Nt8GHFmjpmtfGsDNgXZIhrAfzYINOfv0kyqqOkLMeq401lRrTbWPUujaX6zK6FjiVOu4q36d75M2kmGrSU6SudC7ZhjqtPogBgsm1E0dkwdoCeKFE7WisaZE0YuyFM25PIGtEI6CDPf3xYXD/+xAdD+8FS3yPAW9N4J8IpUPAtNu4B7hD/NxLGT2jDmceMNiZ5vSfBk4L4I9MhN8jDwUctT3JLt4sCmCwpiBU6OkaR2eyfpT2X6fR9fFNrs+jEqpy4CPtRqqSkUovxKDL0anym3e0xNRq4iBAlX6ygOFzQY4R7zlzgPo+PFkai6ALqhD+ebLuM4xWeaRtJ0lA+IKM1DwGAkaAzYGAbwx1tQjTqseaC3M6MNlpG4cxPgoecARByN4nC8oJR/1+GD8PuMhAEC2gErgUD9mfb5OuVuKq/gGUF5ehTarnOXfK9Q1sdxg73l99p2tKF/hzG00tWgrZ0ViggbTjTam3Fabbv9JoVvhU7fv6kJCLpJVXdiJnLqovFQsBTpsbskr4kLvkeuQ5X3Q+LoZUgDmef6+WVoZoVBBnLrabumb8ATHuxhoc33Zpji0XEbkw6ip4oQLtoMPUxafIY24Qwh1novv7owKb6Wa8hfadKkCBRxmQxXBzwaxVeFynQekabTifLjFrjgl0aNKNrlhJA7o8+D/Bnp8biOIPGk/Y8EgCJd84EcyGwMSWpxS5MPXZN9JQpr1EsP0vFUxG3rhrbabdvsp7NU4qA2SF3UBb6ahRnuNXR1wol/OHQAwTPoo+VObPwimamrQpyL+Rmhnie80Uwzj55+xz/byfwM90oqothcu4BL5aeh9mXma0vP43Ecv+nEwuIMxn7BYKhkG5X3spY+X5irzT/E4olLfo0/JsabAWH DUXMfkJk uNbIGQyMOInJN0DgvljnOPDOTPPFTzOr+8xm+pzHm/nXKUnZgV9fp7iXN+aUAAAF4Z7QijcAEh+sXFH5GWG0wTBnNIfqFkWOwXZVIk0gviWodcEky1IJ4Zs+bK6YRaB+3VBUMKIeK/6+b+jnSFEiJLaKKcW1ZUIhySVXtaxMM31B+3dcA6uA0GSK2aKL0yaKnc2Q2WZA75FP5QhsoSxPD1s23MbP8QhzmUH01fxp9xUGgCW/QRKZ6cGpmQg/JEKedDmM1 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 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 | 87 +++++++++++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 820c993c8659..d77d119f4048 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 *subpgoff; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -41,6 +43,10 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) return VM_FAULT_SIGBUS; pfn = page_to_pfn(ubuf->pages[pgoff]); + if (ubuf->subpgoff) { + pfn += ubuf->subpgoff[pgoff] >> PAGE_SHIFT; + } + return vmf_insert_pfn(vma, vmf->address, pfn); } @@ -90,23 +96,31 @@ 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; + pgoff_t offset; + unsigned long 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) { + offset = ubuf->subpgoff ? ubuf->subpgoff[i] : 0; + sg_set_page(sgl, ubuf->pages[i], PAGE_SIZE, offset); + } 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 +157,7 @@ static void release_udmabuf(struct dma_buf *buf) for (pg = 0; pg < ubuf->pagecount; pg++) put_page(ubuf->pages[pg]); + kfree(ubuf->subpgoff); kfree(ubuf->pages); kfree(ubuf); } @@ -206,7 +221,10 @@ static long udmabuf_create(struct miscdevice *device, struct udmabuf *ubuf; struct dma_buf *buf; pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; - struct page *page; + struct page *page, *hpage = NULL; + struct folio *folio; + pgoff_t mapidx, chunkoff, maxchunks; + struct hstate *hpstate; int seals, ret = -EINVAL; u32 i, flags; @@ -242,7 +260,7 @@ static long udmabuf_create(struct miscdevice *device, if (!memfd) goto err; mapping = memfd->f_mapping; - if (!shmem_mapping(mapping)) + if (!shmem_mapping(mapping) && !is_file_hugepages(memfd)) goto err; seals = memfd_fcntl(memfd, F_GET_SEALS, 0); if (seals == -EINVAL) @@ -253,16 +271,58 @@ static long udmabuf_create(struct miscdevice *device, goto err; pgoff = list[i].offset >> PAGE_SHIFT; pgcnt = list[i].size >> PAGE_SHIFT; + if (is_file_hugepages(memfd)) { + if (!ubuf->subpgoff) { + ubuf->subpgoff = kmalloc_array(ubuf->pagecount, + sizeof(*ubuf->subpgoff), + GFP_KERNEL); + if (!ubuf->subpgoff) { + ret = -ENOMEM; + goto err; + } + } + hpstate = hstate_file(memfd); + mapidx = list[i].offset >> huge_page_shift(hpstate); + chunkoff = (list[i].offset & + ~huge_page_mask(hpstate)) >> PAGE_SHIFT; + maxchunks = huge_page_size(hpstate) >> 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; + if (is_file_hugepages(memfd)) { + if (!hpage) { + folio = __filemap_get_folio(mapping, mapidx, + FGP_ACCESSED, 0); + hpage = IS_ERR(folio) ? NULL: &folio->page; + if (!hpage) { + ret = -EINVAL; + goto err; + } + } + get_page(hpage); + ubuf->pages[pgbuf] = hpage; + ubuf->subpgoff[pgbuf++] = chunkoff << PAGE_SHIFT; + if (++chunkoff == maxchunks) { + put_page(hpage); + hpage = NULL; + chunkoff = 0; + mapidx++; + } + } else { + mapidx = pgoff + pgidx; + page = shmem_read_mapping_page(mapping, mapidx); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + goto err; + } + ubuf->pages[pgbuf++] = page; } - ubuf->pages[pgbuf++] = page; } fput(memfd); memfd = NULL; + if (hpage) { + put_page(hpage); + hpage = NULL; + } } exp_info.ops = &udmabuf_ops; @@ -287,6 +347,7 @@ static long udmabuf_create(struct miscdevice *device, put_page(ubuf->pages[--pgbuf]); if (memfd) fput(memfd); + kfree(ubuf->subpgoff); kfree(ubuf->pages); kfree(ubuf); return ret; From patchwork Thu Nov 23 06:44:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13465856 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 CBAE3C61D85 for ; Thu, 23 Nov 2023 07:08:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F52D6B05AB; Thu, 23 Nov 2023 02:08:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A4CE6B063F; Thu, 23 Nov 2023 02:08:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36E806B0641; Thu, 23 Nov 2023 02:08:33 -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 24DCB6B05AB for ; Thu, 23 Nov 2023 02:08:33 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id CCDF4160372 for ; Thu, 23 Nov 2023 07:08:32 +0000 (UTC) X-FDA: 81488340864.23.98BD7B3 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) by imf09.hostedemail.com (Postfix) with ESMTP id ACF87140004 for ; Thu, 23 Nov 2023 07:08:29 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RBGiaZRV; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 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=1700723310; 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=cShyoQNOKFiAdLKHd8/KIzSL7Do4nyLKbs95L25GBXQ=; b=JPdUrAcEFaAlHXzEoMVMHTQ940S8BZ827DlZ7VNJ82c+0UlUw6APw11wOZrM0xoSVK+ptb 1YvhsoKEDArmWcKXPf2v4kPHQzuhy0BOwVxvPGsxFXbfzZcXNBR7nOL0XKACzM1npKENyf aQSl9gflZqNIHlwcGfTk2FHp9/w6F4Q= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RBGiaZRV; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700723310; a=rsa-sha256; cv=none; b=xFzXOBtjBCaP06+wAgd9ywtPTSJoQDrJX4rtpDGg5qEOjctUzNZShtBWD9e8Cvr87X7ALC W4C+9AOKjSY7pAUqo1jTpcMzrRk9TNrvVPG+VUDhlUpYufL0ORIzHQUAIqRJ3mRwsMEjTM 96e0I734dgfRp0b9v6/pGhL3y7se7q0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700723309; x=1732259309; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Dk8F7Q/oBhEAmsAwkSHXM8PeNKfeERmLQrUShteflOg=; b=RBGiaZRVWR7db53oH+DutqPWdxLbw9uiuOdcUZIPkUcqF9InKsLyYGBu FQh/VnwoctveKQLn5BeD0wjdtnnL24qmgL907T9FNAITpr87WiTX4tBQW COpqm0AxM9ZTgYJfpB2uRElef3isvx6+hRTfjtFyfvmQr04PVois5ByR/ xE8PCixn9r8VST3Yhoy5FrN5yhmGA2uQh4OxilSvDf5py5rgJBKvur2SK eQAXUC3b2lbcg6eTVG84SL/bc5q3KEVKS8h6zoGgaVmEQGtGVA9FyZO+I Dx09q4zqI+/6KMGIGuHMHx3YC8PnmNU3cWZGQ9rn0te6DV4MXO9qOaXAK w==; X-IronPort-AV: E=McAfee;i="6600,9927,10902"; a="382612329" X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="382612329" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Nov 2023 23:08:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="8757720" 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; 22 Nov 2023 23:08:23 -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 , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe Subject: [PATCH v5 3/5] mm/gup: Introduce pin_user_pages_fd() for pinning shmem/hugetlbfs file pages (v5) Date: Wed, 22 Nov 2023 22:44:41 -0800 Message-Id: <20231123064443.1035709-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231123064443.1035709-1-vivek.kasireddy@intel.com> References: <20231123064443.1035709-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: ACF87140004 X-Stat-Signature: eo8tj4erooc413ftg1c3h6p94747soqy X-Rspam-User: X-HE-Tag: 1700723309-908652 X-HE-Meta: U2FsdGVkX1/pADiW2Za2j+nNJ+b5bpdqWPOoTNPKvcAgSAfHW7C+WUSt/UoAX7s7GckIEZFB/BOGONILDKR5FEjnwSN32y5Tw+zHgNJcD9r0CZHPpYQKcLUMfVGeFKuhrZuYhHI1jRkqMK4Q9wc3e5SJYujJd4NyUa1TLs94JLALoN+yNvEMfmnmtJ1vfwpBHMxfZP8x5x0vmn00S9pPS4P+anhSxOydUEKovOB4OC2PivXgijphujkOOlhdsClwq5vIpFT+vhzz4nOIMJ+G9n55jY9uBbrApEJbIPAEdifHI+HTjcSbwWWJp2I/jsy0MqCBjoOzUDAsWLYFQNBjdaOjtLvIoYI+sdVmrV0aBBIPndbcwSjiub5F8Lz4DeS8r7U4Wqv6fJet8zeJBL5fLnjkqPhbkKKRHZBQey7WUOBTKTlCaTxO+fyHN8rODsSBXJ/tqBCwnqPpmjrzf0ouekXt+V2Hq5BV8mjkVx1uUaaQ4fVP1fpQBwDm4wRcZI7A+FZTW2U80L5PPmaeNwTtCC74mwpsisSq2UkwUurYJSn8cKhl8tidgS8QIJ1OEZpsAmZ4N5+iMQaqOHy0g+w2KBNKqmBAYWLL4z+4AO4YJAreZk37t43ZM16l1bIZUXqItrWx+y2/Xt+Cv7UHNTiL9xJLA5mT6Au4/7eFHzvOK2tVqHpKVcffrxznvOC9knZ8aZKqkH2a+847hVoyLnNqGq4tGcXrKybIP5rUPvKtsu0zDTEZSWVTooCW/hL1QtcLwi7tP14WagOoC+y0hm73y2eyEnZw9Z5wqIL2vQKj+vBXJc2hZ05xzvMUSz7Y+hEd+8rold2Jdxotr7EbTOht+yafgRwwhDz6OEUH6GSSAOIEfpLsaE6uUdJpFd0EbnWZSfBQWXZ++4Prj/9uAiKXp+t/an/ik2nprt2GJobI+/SFECUNY4Gy60KGuWHUFcfbaOtIYSG0yj3wSkGvm4u tMaQ/8DO hKkNX7wwOoxRyuPx7oXX7BSbdpwWdYZzOurFZ+pDteM/OQ7SwQCt52Pg2XCcMqXIk6FZDeyQZdNrfGHsbY8ZKvDrGsFa3R7BjDl/tSdU7iSVV3DC9mlZWx/x+7CVAi5mhgc4mkccX8DneTp6ttO7EtRzvQlIEIxP3ZgnvUA9jg2rc6susNePiwqc0iKq0+76TK5kF57neXPQsLFrB0xpUNYJT6ITt6jf4FRXqzXDzHCm0bSTUe8howL2+2pipmS8Xu6/EMY0luX2YvB5g85BdP3+5nJE1wSSLSVwayftlFzjj5VE= 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 pages associated with a file, the pin_user_pages_fd() API provides an option to not only pin the pages via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with files that belong to either shmem or hugetlbfs. Files belonging to other filesystems are rejected for now. The pages 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 Cc: David Hildenbrand 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) Signed-off-by: Vivek Kasireddy --- include/linux/mm.h | 2 + mm/gup.c | 114 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..1b675fa35059 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2472,6 +2472,8 @@ 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 pin_user_pages_fd(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages); 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..ab1056b3e75b 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -3410,3 +3410,117 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +static struct page *alloc_file_page(struct file *file, pgoff_t idx) +{ +#ifdef CONFIG_HUGETLB_PAGE + struct folio *folio; + int err; + + if (is_file_hugepages(file)) { + folio = alloc_hugetlb_folio_nodemask(hstate_file(file), + NUMA_NO_NODE, + NULL, + GFP_USER); + if (folio && folio_try_get(folio)) { + err = hugetlb_add_to_page_cache(folio, + file->f_mapping, + idx); + if (err) { + folio_put(folio); + free_huge_folio(folio); + return ERR_PTR(err); + } + return &folio->page; + } + return ERR_PTR(-ENOMEM); + } +#endif + return shmem_read_mapping_page(file->f_mapping, idx); +} + +/** + * pin_user_pages_fd() - pin user pages associated with a file + * @file: the file whose pages are to be pinned + * @start: starting file offset + * @nr_pages: number of pages from start to pin + * @pages: array that receives pointers to the pages pinned. + * Should be at-least nr_pages long. + * + * Attempt to pin pages associated with a file that belongs to either shmem + * or hugetlb. The pages are either found in the page cache or allocated if + * necessary. Once the pages are located, they are all pinned via FOLL_PIN. + * And, these pinned pages need to be released either using unpin_user_pages() + * or unpin_user_page(). + * + * It must be noted that the pages 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 pages pinned. This would be equal to the number of + * pages requested. If no pages were pinned, it returns -errno. + */ +long pin_user_pages_fd(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages) +{ + struct page *page; + struct folio *folio; + unsigned int flags, i; + long ret; + + if (start < 0) + return -EINVAL; + + if (!file) + return -EINVAL; + + if (!shmem_file(file) && !is_file_hugepages(file)) + return -EINVAL; + + flags = memalloc_pin_save(); + do { + for (i = 0; i < nr_pages; i++) { + /* + * In most cases, we should be able to find the page + * in the page cache. If we cannot find it, we try to + * allocate one and add it to the page cache. + */ +retry: + folio = __filemap_get_folio(file->f_mapping, + start + i, + FGP_ACCESSED, 0); + + page = IS_ERR(folio) ? NULL: &folio->page; + if (!page) { + page = alloc_file_page(file, start + i); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + if (ret == -EEXIST) + goto retry; + goto err; + } + } + ret = try_grab_page(page, FOLL_PIN); + if (unlikely(ret)) + goto err; + + pages[i] = page; + put_page(pages[i]); + } + + ret = check_and_migrate_movable_pages(nr_pages, pages); + } while (ret == -EAGAIN); + + memalloc_pin_restore(flags); + return ret ? ret : nr_pages; +err: + memalloc_pin_restore(flags); + while (i-- > 0) + if (pages[i]) + unpin_user_page(pages[i]); + + return ret; +} +EXPORT_SYMBOL_GPL(pin_user_pages_fd); + From patchwork Thu Nov 23 06:44:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13465858 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 18AD9C5AD4C for ; Thu, 23 Nov 2023 07:08:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 238806B0643; Thu, 23 Nov 2023 02:08:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 170536B0647; Thu, 23 Nov 2023 02:08:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F06D66B0645; Thu, 23 Nov 2023 02:08:35 -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 E24476B0641 for ; Thu, 23 Nov 2023 02:08:35 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B585C120E59 for ; Thu, 23 Nov 2023 07:08:35 +0000 (UTC) X-FDA: 81488340990.03.DD1029E Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) by imf26.hostedemail.com (Postfix) with ESMTP id AC1E4140005 for ; Thu, 23 Nov 2023 07:08:33 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="O/+AlHGQ"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf26.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700723314; a=rsa-sha256; cv=none; b=7p1fZmTXxXkn++DB5TJk6CjaTTyW7A13zafZW0ir7JLXv5lmwPURmV3JRSDfkdmDUh31Yb 1DUPQ0/f6KHXuH0Kx0veLib5tB0G6wn4tQjFco8o9eB8V923xmUr2eYWAbmQt5TaAJhV2J AOaXC1A1h+uuXJzH6pW7jHkN+mgrRPw= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="O/+AlHGQ"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf26.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 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=1700723313; 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=Dxy1bPaM/KUrNwBBR+vLy6E7L/nCzL4ajpk2sJDKizY=; b=E4mN7r7VffxkM6M8YhKt0Huv8Sb5A7AeHTAxz9GQT5opcx9Zey6kwaoTX2VjUP1NljfPWn KAAfqHa0EoKGQWQflK9qksjVtxQ3Y68KICTncm1vOCN45Kx8bjEUuLSYAzARBLHUjCBToq HmszxYmOXea/7RC6ZarnDuuzi0TsEAY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700723313; x=1732259313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JveC5J73AXuIFwcZCQaP9Gn0jICa/20AkT8Zw92eLmk=; b=O/+AlHGQCjweufMU9FnRSCmc8TyxWYENERbcJ1JnhVZVgSWnafSJsZdw nGNu7h6si1TlPzhrLPpZ4FRvvQ1Ub0+OMKTwjGBILSWlFsSKCWqH6V1ig 1B69oO6UbpCBbv3ub3dfOZle//LMPk4q6NfjLiICWJ161h79b3wg4pDR6 uaZJ0Mf7UxGDFEERe6E/oGphKll1DIkw2x/gE/kQ8Y3kVoGuTvvGNi4nz OrOkV8pdmrEl1yZiu1gFmtWzqK/kaOc0iEXjzaPCqJKxOoj/nB4seIBuY NCVS8+QCi0fHuD6TG3DObrYeJQQApB2Y03qRDrA65k2sqNmLOIaSrn0Z+ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10902"; a="382612336" X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="382612336" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Nov 2023 23:08:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="8757723" 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; 22 Nov 2023 23:08:24 -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 v5 4/5] udmabuf: Pin the pages using pin_user_pages_fd() API (v3) Date: Wed, 22 Nov 2023 22:44:42 -0800 Message-Id: <20231123064443.1035709-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231123064443.1035709-1-vivek.kasireddy@intel.com> References: <20231123064443.1035709-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AC1E4140005 X-Stat-Signature: 5qzb3pfm6a3nrwa84kege1c5mdj5dfji X-HE-Tag: 1700723313-329514 X-HE-Meta: U2FsdGVkX18P9gLoBioUzNQ308+imtWCTZiSqBA7ZCvChe7cxo2jWQm+e9jN+S6KJATlQaqD2jZshsMOTLvFH1S7HRWZLJxqQxprHqDj67MA+kUemYOZHbE6QsnTS2mtry1v0pjrl03E8EKWKITj/P+R55J+hdgkjhllZcVNUrjFr9+HFicnZOr1/ie1FLEW1+srImGERq/WqqIk8XdRVO8dQM2+/mG1ronj4XWKEeircI9dh8tlqDO/dghLYkg61oSLnxOy0bIVPKqOYEh6VTMxUDJLpgZmYVGzulY71gCNflB9gCkmtfMi9WYALseKyyYAysjoGFW38LRCv+hzflJFOyZlWgLVwB+3nfFSfPjTz7Ult7kdI9idBv9uhWlrvbmPtGvh/Zx6H2fYRkUIys7i3U0gt7yJufvKaloood5AllL6pALi/io3rQy0qLYrBS7OoJgMulV6u9ARwnsnk1hgYs+Ve5NFyQPx3qOIM3zx/58mzLC8bCbZsSggypAbOwgVX0l2ElQT3cGma1/SSgyd95VaiiZXokZX4eQFwjPZ2rQz5BO9GfdtBds03d364CkWxcskr1eQ38C1S5P5HsjWqVqNkJfz6Pp4pBmXM/kVqbqVAK72EpfUI308kwtJ14yaq2Wp2o76Y7FqyZpHEs9LHKZ/bPZ2KdtdccT3Q+vaLJtC81OHDWdY3eL19JICOtKaVkV6XXoLl+RZ8+JXFHSEoclJjDmtKeVfqUHOuHBZygHYp8ta1iYucU4ZEkMrP3pt5N/RVrQN/WnlELKRq0IED0fBYzkibsQq0Z4GSZiIk5RVbhqJXXUT7ruBv2ckdzvPgIfxB/wkM7CPjgEpUvnK+MEeDE9ixY43j4VJ5ByJkFboCmRDED9szPKdsCVPcf84sANWidFIFAjVgZChPwNeS1/PjkSgDMjeu5xOs8IpTz8ja7dp4OFda7+Sb/yA+Tg288ngfGA0Fy7GqGT z5PxK1T9 oQUx/EDuXvmajea8JPGxvjlqAcpWIVL9dW8YSfCwHsExAvlgCH0qswtbOoGH36TcNFlK0SK6Gjor+IfaqqccZkpz5oam/cQ/7e8NyL1N06A4AM4FKSDy0aiSKZ1O6w2g9+QmS2jJ8wl0FA9P9gPrSM1Y642f/7GwQBxQT4J8kHmp8ES/o7oiJYWdC3h1YfHKsAG1OasvtyQDTBGZGRXgWfMZa2Dij6Cd1L+gYAu9320hNl16AiL77x32CxBrlvRLfM9aBhHTqTMk+jNqkZSJIKlOYmSYyH4CMPEnKhxEWCXKyB8o= 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 pin_user_pages_fd() 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. As a result, we can drop some of the local variables such as page, hpage, mapping, etc. 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 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 Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 61 ++++++++++++++------------------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index d77d119f4048..883bd97e4076 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -156,7 +156,8 @@ 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]); + unpin_user_page(ubuf->pages[pg]); + kfree(ubuf->subpgoff); kfree(ubuf->pages); kfree(ubuf); @@ -217,15 +218,13 @@ static long udmabuf_create(struct miscdevice *device, { 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, *hpage = NULL; - struct folio *folio; + pgoff_t pgcnt, pgbuf = 0, pglimit, nr_pages; pgoff_t mapidx, chunkoff, maxchunks; struct hstate *hpstate; - int seals, ret = -EINVAL; + long ret = -EINVAL; + int seals; u32 i, flags; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); @@ -259,8 +258,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) && !is_file_hugepages(memfd)) + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) goto err; seals = memfd_fcntl(memfd, F_GET_SEALS, 0); if (seals == -EINVAL) @@ -269,7 +267,7 @@ 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; + mapidx = list[i].offset >> PAGE_SHIFT; pgcnt = list[i].size >> PAGE_SHIFT; if (is_file_hugepages(memfd)) { if (!ubuf->subpgoff) { @@ -287,42 +285,26 @@ static long udmabuf_create(struct miscdevice *device, ~huge_page_mask(hpstate)) >> PAGE_SHIFT; maxchunks = huge_page_size(hpstate) >> PAGE_SHIFT; } - for (pgidx = 0; pgidx < pgcnt; pgidx++) { + + do { + nr_pages = shmem_file(memfd) ? pgcnt : 1; + ret = pin_user_pages_fd(memfd, mapidx, nr_pages, + ubuf->pages + pgbuf); + if (ret < 0) + goto err; + if (is_file_hugepages(memfd)) { - if (!hpage) { - folio = __filemap_get_folio(mapping, mapidx, - FGP_ACCESSED, 0); - hpage = IS_ERR(folio) ? NULL: &folio->page; - if (!hpage) { - ret = -EINVAL; - goto err; - } - } - get_page(hpage); - ubuf->pages[pgbuf] = hpage; - ubuf->subpgoff[pgbuf++] = chunkoff << PAGE_SHIFT; + ubuf->subpgoff[pgbuf] = chunkoff << PAGE_SHIFT; if (++chunkoff == maxchunks) { - put_page(hpage); - hpage = NULL; chunkoff = 0; mapidx++; } - } else { - mapidx = pgoff + pgidx; - page = shmem_read_mapping_page(mapping, mapidx); - if (IS_ERR(page)) { - ret = PTR_ERR(page); - goto err; - } - ubuf->pages[pgbuf++] = page; } - } + pgbuf += nr_pages; + pgcnt -= nr_pages; + } while (pgcnt > 0); fput(memfd); memfd = NULL; - if (hpage) { - put_page(hpage); - hpage = NULL; - } } exp_info.ops = &udmabuf_ops; @@ -343,8 +325,9 @@ static long udmabuf_create(struct miscdevice *device, return dma_buf_fd(buf, flags); err: - while (pgbuf > 0) - put_page(ubuf->pages[--pgbuf]); + while (pgbuf-- > 0) + if (ubuf->pages[pgbuf]) + unpin_user_page(ubuf->pages[pgbuf]); if (memfd) fput(memfd); kfree(ubuf->subpgoff); From patchwork Thu Nov 23 06:44:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13465861 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 7E213C5AD4C for ; Thu, 23 Nov 2023 07:08:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 19B686B0649; Thu, 23 Nov 2023 02:08:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 125B16B064E; Thu, 23 Nov 2023 02:08:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E91236B064F; Thu, 23 Nov 2023 02:08:38 -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 D5E9D6B0649 for ; Thu, 23 Nov 2023 02:08:38 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9E7EF16068E for ; Thu, 23 Nov 2023 07:08:38 +0000 (UTC) X-FDA: 81488341116.14.695EECB Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) by imf23.hostedemail.com (Postfix) with ESMTP id 884E314000A for ; Thu, 23 Nov 2023 07:08:35 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=itQl9LXn; spf=pass (imf23.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 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=1700723316; a=rsa-sha256; cv=none; b=Tb5O7pMFX1HpHoNIQQG7GwuUs2zT1vUlyjtwqRmJ+U2ZJRqdzzkYACqb6BLM9Y1QtnLnHN XqJTdnIAGbEutzISUJXYwM/7hxBXlOtjM5C+SvNfOANhr/a8t6XsqcadbwYwscqV3LomvO LKEzxNz1JxB6ig9BH8PM9MhI3PuC4EM= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=itQl9LXn; spf=pass (imf23.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 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=1700723316; 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=w/tAcsbDfY/GebeqRkuwtICF6qttG5C2gjiA3HQqd+r6HB0WD9ztcOKvxqggktky6wB1T9 y9muhQebN8ev1/qpJ5MEQlSFjs0epXzBIN01Tw+KcrEfGcnaQXs3m+64j57UriA1NvDsk+ 6AsYJSbb7tejVLLxfEaR3iP+a85sjoY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700723316; x=1732259316; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r39C9fURv/tYjjTgfRvVfOiacLkGBXJF65fG2x4l+b0=; b=itQl9LXnzspaH/0MIvgVfvTKoKXFi0YGu/WAqpAylGaN5iEmMVevSo9g BlL8PeHoPRhcxcBhZt6rE4wA8Zo4AgMGUcm7O3987Tvt9SzItpfODUNXW JTYpC+3qGerxOkgp9iYUsy1W6mV4JJKPNNFCj+kEK5gNr7xJp88dh41LF wwUUhpfkqAQFLOpOaSg2f7G1/x5I+mrzqWFcCv0KQWln2joBqnfxOtCbN I72Z8Ci0EtCyPyXNbp4DWRS9tGj+CZG//eu0rlZAhr53vZlt9NDIAyiyx x7VCChnfYjnWISrsN9rVToi4EGlIGuoCxwHGyP7HrOxvZ6DvHkC+TnKbD A==; X-IronPort-AV: E=McAfee;i="6600,9927,10902"; a="382612341" X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="382612341" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Nov 2023 23:08:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="8757726" 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; 22 Nov 2023 23:08:24 -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 v5 5/5] selftests/dma-buf/udmabuf: Add tests to verify data after page migration Date: Wed, 22 Nov 2023 22:44:43 -0800 Message-Id: <20231123064443.1035709-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231123064443.1035709-1-vivek.kasireddy@intel.com> References: <20231123064443.1035709-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 884E314000A X-Stat-Signature: 85xt8863rfatcohfn5gq7fi3nudmcu8t X-Rspam-User: X-HE-Tag: 1700723315-767205 X-HE-Meta: U2FsdGVkX19jVKrCMkCTRJBXzaaVLiXaeTEPjLHD591lakygQ/E/ErX0VaCgceOn8LlbBjBt7Tzu3xHVbaHE/wFvmMzvPUXzwYIo2kn0AD+o6HnDdVpWDhtFzEI/y54a4kdlIUOUoytwHu/RuP2ICNwgKPJR+FUem0JWJCmsaUVvJbyRmkoNzJsazabKYrI2esQykV0v+cfNSZkpFDSWzrMZCwEuboN9NenbsyECVhdSkgJE65tV6Dm2SB4j50f5PRmN53LuZk070CKw2VFGtoNBYwfJg0/R7lcvb9mrzyN8anL6KkFenH394Ps+s5pHbWSqIUqhEsb8O2vul4bR8KsFJ4xQiVmv85w61BGQa+vFWlvK0cghQLuS8+MqPFwCq+VrEb5z6fwHQFBuYmd3fhgx+j1GH6PbmTpDE/S6Rx8fsR7ctR/QX6DlUteYefU45p0T4pygyUi6SKzIoWlCWNrcKyg6Qw/s5j2XgN6AopeDNCsKGBLMqWvQAZZvfto8QyERx2Ev9T4XHaqtJFZm3Bunn7JLlVGy9th7vvlA22t2nLra7DN7/J9d2njnH9qRHdmF+gRRgeMz4X51k2vJGC+OF4hXvpMKAAuJJ0ZHevDp8o5UGVJ8e/Z/gqEqFn0ZUsLljqP6h1aSli2hNGGCB5Sgbm/6lAEZ/C3mIQ0ZO/LhKnCnV0SuOfVXFxHg9nNA2qh0HL0wB0Dd5xgi2zLYN0okatcLGSK9P499wsBIzOuw4rLtiVey5RpInvq5kIQIGiiGvIP2nR+DxqeuLcjBT8mIxIL1x3D9HK0YJZTFJhAOMHJtAs2dwGPA4HmZfQI0SYuLOfQLk8JpMO0FNURkE1VnSJfuD18GMdDCeflo6m0cTk07rTE0pPj51hgmOJ1wSdwCbh6yz2PDNkwId/n/uFfcOwE9nSQ8y/T6L9uQkfhsJj7qqU4oynXjSRebKEENvLCHz+2rgMyopMBq7oy C9YuCasG dfLUAECnNhW1DmL4JeuQmG53foHZEBjDXoYQXjPrhSLCdIuImU6rIVhg2oSl055QU4Y+SACr0dwxIL4IzMwAEpNRlQzrc9Ap/mgXBZoSFExBIQxK9MNkYMxNXj56EvEW3JwhwIwoYr0bam5Mkz+iOq1vlWV58v6L/gaHU8f2HjulI5zgS796g5yli2hDkfnutrmYTNlcWQeH7cJvZgtE7nC1yf9X/TerF5yrBy3SKWv2BwiP+eyC8qF9ij1fyUFcZL/53NKmoefYbFfQQpqec02qdlhbB09MgN5dJHP6hPHIE+qGrHJc1bPoGog== 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);