From patchwork Thu Jun 3 08:52:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 12296751 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA3DCC47097 for ; Thu, 3 Jun 2021 09:05:15 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A82F661361 for ; Thu, 3 Jun 2021 09:05:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A82F661361 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 019F66F45E; Thu, 3 Jun 2021 09:05:15 +0000 (UTC) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by gabe.freedesktop.org (Postfix) with ESMTPS id 136156F45E for ; Thu, 3 Jun 2021 09:05:14 +0000 (UTC) IronPort-SDR: KKwpdkhhAkgk1yDG8xXcxdo/F7+FDElf1c/K2Y3udVNxG9ND53NK8f7XNj4B4IYJf6N1w6KQve 6v1HfdNYCdfw== X-IronPort-AV: E=McAfee;i="6200,9189,10003"; a="183683549" X-IronPort-AV: E=Sophos;i="5.83,244,1616482800"; d="scan'208";a="183683549" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jun 2021 02:05:10 -0700 IronPort-SDR: Xfst5U1VEaiWhhasKW2arfTrZf6yYGZnWVcRaLSKmW3YQ9YlOhPEnaxuvd3AwWpNRwT8GNKIWj BIth/vib/wHw== X-IronPort-AV: E=Sophos;i="5.83,244,1616482800"; d="scan'208";a="446221738" Received: from vkasired-desk2.fm.intel.com ([10.105.128.127]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jun 2021 02:05:09 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org Subject: [PATCH] udmabuf: Add support for mapping hugepages Date: Thu, 3 Jun 2021 01:52:31 -0700 Message-Id: <20210603085231.285768-1-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vivek Kasireddy , Gerd Hoffmann Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" If the VMM's (Qemu) memory backend is backed up by memfd + Hugepages (hugetlbfs and not THP), we have to first find the hugepage(s) where the Guest allocations are located and then extract the regular 4k sized subpages from them. Cc: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 40 +++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index db732f71e59a..4a976a56cbc3 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -11,6 +11,7 @@ #include #include #include +#include static const u32 list_limit = 1024; /* udmabuf_create_list->count limit */ static const size_t size_limit_mb = 64; /* total dmabuf size, in megabytes */ @@ -162,8 +163,9 @@ static long udmabuf_create(struct miscdevice *device, struct file *memfd = NULL; struct udmabuf *ubuf; struct dma_buf *buf; - pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit; - struct page *page; + pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit, subpgoff; + struct page *page, *hpage = NULL; + struct hstate *hpstate; int seals, ret = -EINVAL; u32 i, flags; @@ -194,7 +196,8 @@ static long udmabuf_create(struct miscdevice *device, memfd = fget(list[i].memfd); if (!memfd) goto err; - if (!shmem_mapping(file_inode(memfd)->i_mapping)) + if (!shmem_mapping(file_inode(memfd)->i_mapping) && + !is_file_hugepages(memfd)) goto err; seals = memfd_fcntl(memfd, F_GET_SEALS, 0); if (seals == -EINVAL) @@ -205,17 +208,38 @@ static long udmabuf_create(struct miscdevice *device, 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( - file_inode(memfd)->i_mapping, pgoff + pgidx); - if (IS_ERR(page)) { - ret = PTR_ERR(page); + if (is_file_hugepages(memfd)) { + hpstate = hstate_file(memfd); + pgoff = list[i].offset >> huge_page_shift(hpstate); + subpgoff = (list[i].offset & + ~huge_page_mask(hpstate)) >> PAGE_SHIFT; + hpage = find_get_page_flags( + file_inode(memfd)->i_mapping, + pgoff, FGP_ACCESSED); + if (IS_ERR(hpage)) { + ret = PTR_ERR(hpage); goto err; } + } + for (pgidx = 0; pgidx < pgcnt; pgidx++) { + if (is_file_hugepages(memfd)) { + page = hpage + subpgoff + pgidx; + get_page(page); + } else { + page = shmem_read_mapping_page( + file_inode(memfd)->i_mapping, + pgoff + pgidx); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + goto err; + } + } ubuf->pages[pgbuf++] = page; } fput(memfd); memfd = NULL; + if (hpage) + put_page(hpage); } exp_info.ops = &udmabuf_ops;