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/8Gp2at6FN2Faf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eK/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;