From patchwork Mon Jun 24 06:36:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13709047 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 4044DC2BD09 for ; Mon, 24 Jun 2024 07:05:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D42E6B0275; Mon, 24 Jun 2024 03:05:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 35B846B027C; Mon, 24 Jun 2024 03:05:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D96C6B0278; Mon, 24 Jun 2024 03:05:33 -0400 (EDT) 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 E24736B026B for ; Mon, 24 Jun 2024 03:05:32 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AA650A12E0 for ; Mon, 24 Jun 2024 07:05:32 +0000 (UTC) X-FDA: 82264896504.06.08C3D8F Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) by imf22.hostedemail.com (Postfix) with ESMTP id 90333C0006 for ; Mon, 24 Jun 2024 07:05:30 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="Bl4o0/5F"; spf=pass (imf22.hostedemail.com: domain of vivek.kasireddy@intel.com designates 198.175.65.21 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=1719212717; 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=zjBBzWTJEvTtFfv4IWPpVd4e6eP0fgXlLOTDqKvQBc0=; b=0hPuFRctgHWkSfxv5fd9HpaiXAM/TKJbb/Siwto5jBnbDeHZgAj9rR2VtZkk95bk6LW1ku 2l3QB4Dox93VB/d8lO0ttwcJ3Zxo2WM7gBP06anpF2Lu2i2hyprc5A/+aPtc5OsTIeigd4 pKDNfkRXZAGV4UwD/+fD8XLfskDh+8M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719212717; a=rsa-sha256; cv=none; b=arvqh6V55figRl76Be9sHPOZe4n6FKiRpCwYTePlPZHqb/s7t/JimnhywZxLgsmoMCIu6n RI0TiXV5FbxFplaiihvIyFuCyqKc0uQiZ1daP7EBibaaN1irQFX1tdPqR27AMEzG3xeoNe +RPQzHhssKHCD4VJY9ORnTtvVIIEJlM= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="Bl4o0/5F"; spf=pass (imf22.hostedemail.com: domain of vivek.kasireddy@intel.com designates 198.175.65.21 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=1719212731; x=1750748731; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cAHfonYtnIV65GKLrtLc1wEw5/ZdeM3gwFaTcvhYa/c=; b=Bl4o0/5F2HUd8ErlhHkiAfi3CVK6EWTHf0v4bTsFN6dyv/+I31bP8UMl KeWsONyN8yMZK/4zZJCUZfX+c9y/pNCa34KG6BdhSNzndc1beH0x9OmsO Dyb98Qs4uc8BR3vez6LTxAvGW63RTbYirB1M74X903+bFayxoqz97wUtc dEfNq8fTNPztUSTs/hPdekJoxJ9cLzFiuJH8KsCoXAaeNW8buC2fGgkLJ 0c/cOo6BwrEzbNuch6qNN4XUfG4I4OqbzgH1xRE6Otw3RY4CQCPhZqW8e gddjy0pqZvp0cc05qjUKolvVtlsOdqCzPUlwH+TgPe6MOL38yyHErf3ve w==; X-CSE-ConnectionGUID: qHrOUc40T5GBP8F8mxo3/g== X-CSE-MsgGUID: v8J3CKNTSMSvF+2t6zKZwA== X-IronPort-AV: E=McAfee;i="6700,10204,11112"; a="16134970" X-IronPort-AV: E=Sophos;i="6.08,261,1712646000"; d="scan'208";a="16134970" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jun 2024 00:05:26 -0700 X-CSE-ConnectionGUID: vnn23QgRTeC63B2xXrdjNg== X-CSE-MsgGUID: f3p+pMkTSH+fePj1IfeTIQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,261,1712646000"; d="scan'208";a="73955885" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jun 2024 00:05:26 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Dave Airlie Subject: [PATCH v16 7/9] udmabuf: Convert udmabuf driver to use folios Date: Sun, 23 Jun 2024 23:36:15 -0700 Message-ID: <20240624063952.1572359-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240624063952.1572359-1-vivek.kasireddy@intel.com> References: <20240624063952.1572359-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Stat-Signature: 9ogsquiffejhh8jqox19a5stas3c8iwm X-Rspamd-Queue-Id: 90333C0006 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1719212730-613968 X-HE-Meta: U2FsdGVkX1+wbozDpxYszUOUsgX8XLqGM2ODy6VCjNgtZaKDbzIsPund1uxRH9Vl/WSMPmault2W2eO3P0LE8Th85Ga+o61CVqKlBQq/93/yqeoK863dQ8qZ376YYrwzN2uKQTPpwfHEqgS0/Z3//2I+acsT/9YsKFt71ALkZECnQqvYS1F5cxo58CmifBePardomp7L3NTa3u8kSUt2cRzDziNtuGRMljRAp8hFnfMRb6kFVBVo+i3K+9k99xGCo+yrPRneHUnFRqNIuvqhxnfuQT1dBM54qSIMChxS8HfYqLPi5gG/6lwKxd+aBquzJkSRJBBgGliX1y9BKVKo51UOfCNHTORrxkLZwblbQDwvmlRgRpv7y103fo1M50KtckeHbNHuBJCWTrGGnMeJ4Fk5m9nESWcKApdC5inqH1UNypARkUX9nHA445kfhm98eUrCogynTnl8ARTqv/TsBiSYmVwAx8Xwx71vclGJTrYTc7rs7vmzsYbKN1FEkuIN9ASI8MxJ7+xbZjxv3KT6c2puwKg1uULG4XmGfJcnVJZxG87v5+uyTvpl1p9Bbuv/0xpvHmzGHs6MVGqFamGJC61gI6fs35wz/1hxZmFI62VTr7lxJzW4hcUX8+yINfgpAiZvYD3kJQ6rXIDUJrVd0ksciXUsSrVHuHZfrhZ+eCN4rpjijrxISUmgmqMAfkZUafLbbT7KHOPXzOlyjG0nEbgUmis/hYkckKtANyW+oKr6pVjoblEwWr1ZJWZFdTO4dIwzE2zjgO6+LP5ywioZ8bUkZRhv5bC+8krEZF4XDPiKsEdL3KI2nnH++t7b4qqxJ1mIh4moPlZnXmUfLNirf5SbV5heoaBQVsRa+lcLzO23nZ5YBkcs9sxM6Nmg0Ukvs1RwIUx4Ps4RuYwmX/QFT2lZlTtwbHwhljp29R61nsLNRE3rPA9jyGJCbExKdMJgAmd9MDmr5HWRdkwkf53 7O8Bi7l3 sZNNLw7mBN2uqPpdfTgSiXz+rsGG7oc7DIay4HtLSOcPC4yVwDBS++A8duQ+tXZOFfGU5fpGVsAYiRye3M4oGLJyYrpA4NhLWNBPgHbmqqlrFQogYSjfp6GIGWaCDaJI/tdPJ6CnCB0xzdvoQgKgAlqlriUvG6G1DglL01ivrl+emTDEttpKkZlJ/i9JxUtQcucgrnxzIz7ji7+ZNamdh3Qdx7BR90Q5anbeWn1pgcsZe5rBXB4PNtTU7ryRptp8JeieCuV+BxPuLiVGVLs6SgOqYqm4tZUOH9d/G 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. And, using the memfd_pin_folios() API makes this easier as we no longer need to separately handle shmem vs hugetlb cases in the udmabuf driver. Note that, 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. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Daniel Vetter Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Acked-by: Dave Airlie Acked-by: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 139 +++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 56 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 274defd3fa3e..e67515808ed3 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; @@ -344,30 +381,20 @@ static long udmabuf_create(struct miscdevice *device, 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; }