From patchwork Tue Dec 5 05:35:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13479379 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 C2F56C07E97 for ; Tue, 5 Dec 2023 05:59:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 006456B0080; Tue, 5 Dec 2023 00:59:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EF88B6B0081; Tue, 5 Dec 2023 00:58:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CFCBB6B0082; Tue, 5 Dec 2023 00:58:59 -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 B15A36B0080 for ; Tue, 5 Dec 2023 00:58:59 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8EC2CC0190 for ; Tue, 5 Dec 2023 05:58:59 +0000 (UTC) X-FDA: 81531711198.16.2A7DBDF Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf12.hostedemail.com (Postfix) with ESMTP id 781F640004 for ; Tue, 5 Dec 2023 05:58:56 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="XoOx/2fm"; spf=pass (imf12.hostedemail.com: domain of vivek.kasireddy@intel.com designates 198.175.65.12 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=1701755936; 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=vmBJDw9qqKKN35XiCpuvStJvcS2gMraGVEnNdA9VQws=; b=rLFvEhFODuKkp7K/6RCO9aUxOkDLtfmxahOlddOgAk2m+G9YITgPKAJGBfTctvdK+swmr1 UBdm2Y3vLMFXHT6WeFWKnwNdEflDQ3DViLG0ub79L1xLGnUb5UfscPMUGwNM9cplQYBt5W uAj6VjaSqHvHk+a/aKHGNwEmYFHt16w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701755936; a=rsa-sha256; cv=none; b=oFxghi22OtCyMWpzk4WKc3+H5Ky0MYKZblUyxnE4dGAv6af78ljeH4/bAMvKL1K3MwUI9M AGc125hihyierbtQkuGe0FWL553NNZiukx5M9E2hpc2f37ZNM2/0b1rehgE8SFtwKVw1Ai Wap/3B/ShFX5PwyMf+PHubyL9Yjs1gA= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="XoOx/2fm"; spf=pass (imf12.hostedemail.com: domain of vivek.kasireddy@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701755937; x=1733291937; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2lJJFbXrRuB2RlRAET5QtLu2irPUsw8bf+ZLAugxfOs=; b=XoOx/2fmSYZGOcnD4L/0ehNsjfu5wJtnkl9wHKXzom32LYEVuHksbatO /m9eatcxff93ZZoog/DaKzdpe3THIcDfjDBbLnvybf/T5zUx4/zyQKxDN 8MK7QEx8ViPjWd3kYXOCugVxnu3VlWYORfHF+NOnP6LsptFQJ99yKZM9y LgmFw5brRrQdTaId5HpECfLe1RnCQ3wEVNx5qeVk+e+YZlc8Eebg8qvNu yleu+0WSL1gvJhtuIbymEnEjSOEk7YIBlSKOsQVDraOcz+NHbI5nRkBDY HucnTVG537wL4Bffi1V0G6sFTcldcp6YdQSbrIk/xf/rHje1w8pIbrD88 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="906329" X-IronPort-AV: E=Sophos;i="6.04,251,1695711600"; d="scan'208";a="906329" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 21:58:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="888807735" X-IronPort-AV: E=Sophos;i="6.04,251,1695711600"; d="scan'208";a="888807735" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 21:58:50 -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 v6 4/5] udmabuf: Pin the pages using memfd_pin_user_pages() API (v4) Date: Mon, 4 Dec 2023 21:35:08 -0800 Message-Id: <20231205053509.2342169-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231205053509.2342169-1-vivek.kasireddy@intel.com> References: <20231205053509.2342169-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 781F640004 X-Rspam-User: X-Stat-Signature: jdo5fy5csdum7m6ipguz8ydn31kzm65x X-Rspamd-Server: rspam03 X-HE-Tag: 1701755936-796643 X-HE-Meta: U2FsdGVkX1/t4RQgID/S4laP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93VWAMrFHK8riRWTrKHrmejzBSLWq/yDpbBSpvXae3/dkzlW+wsVTKffB5/q6bMGeCwrEtNQw65uwDinB03z5s22vsP/JSoPGryksoboLhBGtkNS/WZzqIADOvd99sprIcxASYcfOJRUU6i004q3SvDsT8KJhb1tXAWwzRC3NXXVlOjV9534ZXofesEuK9q2Wo4Fhjo= 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_user_pages() 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 v4: - Adjust to the change in name of the API (memfd_pin_user_pages) 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 | 62 +++++++++++++++------------------------ 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 1d1cc5e7e613..887dc287a23c 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) { @@ -288,42 +286,27 @@ 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 = memfd_pin_user_pages(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 += pages_per_huge_page(hpstate); } - } 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; @@ -344,8 +327,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);