From patchwork Sun Feb 25 07:57:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13570760 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 E9EE3C54E41 for ; Sun, 25 Feb 2024 08:24:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 97B4C6B0102; Sun, 25 Feb 2024 03:24:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 904876B0103; Sun, 25 Feb 2024 03:24:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66E6A6B0104; Sun, 25 Feb 2024 03:24:20 -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 3A47F6B0101 for ; Sun, 25 Feb 2024 03:24:20 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1D9831A036E for ; Sun, 25 Feb 2024 08:24:20 +0000 (UTC) X-FDA: 81829639080.11.C5BAD3B Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) by imf09.hostedemail.com (Postfix) with ESMTP id 4B9E614000F for ; Sun, 25 Feb 2024 08:24:18 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PIDRqP2O; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 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=1708849458; 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=S0hncFE+TN2V8h0Vy9nPky+8cKi/00juTYk6MbcQZr4=; b=gpN/URqAG5M0brnkBVSC0SVMcuc6eiEFw86hS/rRX4Iv/1HCho7RxKKKGIVQRp3+xtv1z1 cHkXdqgrUXUOrGIv1juvB1WuuzaHOBjjbQvUlcEfdZbo2lQvyMobzXMOKH+PXDmxrbamJ1 lcXduq3GPpR5hu2LijZgQ2bL5EUT1sU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708849458; a=rsa-sha256; cv=none; b=OwNgJoZ0CJYsJju3l7ga+FzOJFlLz9b3D/cMaIiO0hwUDjiYFYYAss67jp4om8HojhAWOi 3GqLYCeAnKTmiF5E5YwFEmIQDIzBowD5F7DDu9ZiMqEgPJQxS8xxkaB7Eib+c0BzrKWZyi cemJqHM5F2mV1xkE05d+HAzYXCJyXv8= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PIDRqP2O; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.17 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=1708849458; x=1740385458; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CRNrLA7Zi25moWFEfOmiw/XIaGMGZ6Mt2vHfsiWi2qA=; b=PIDRqP2Oi9bldfA+UPh0WrXcm9FFVNZUiX77hJYI1ji7vuCLAgCBo+O2 84O9xZbBWhKoUG/AoWq+x36hpvyCsgxStK+e/jZUDpc71l8T8uFJRLe1L shWARx/tpbpE9aquoGC5XFO8QbCx7Fwn55TfPj4hot/5ccagL3dDsr90J ON+ZaM9W6Co+quDByeJydzjHXCxSuQ4DfdorZyJy7UrHqIKZG3WT2Pg34 fWIjec5SH9PvhSfMNjDtSyjWFdwQrJXWMZ4Zzugoa1NVXyQlFuoX5BAsx vrMrg2/XpOeQO67Rjh/52Mz5BDp0U9YQGr2bbkxC3uUdLvm6dKLLPmBsl Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10994"; a="2988395" X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="2988395" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,183,1705392000"; d="scan'208";a="6783274" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Feb 2024 00:24:14 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang Subject: [PATCH v12 7/8] udmabuf: Pin the pages using memfd_pin_folios() API Date: Sat, 24 Feb 2024 23:57:03 -0800 Message-ID: <20240225080008.1019653-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240225080008.1019653-1-vivek.kasireddy@intel.com> References: <20240225080008.1019653-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Stat-Signature: y6s5jdf5c6sr1zdqd4x1oa7daoo47sio X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4B9E614000F X-Rspam-User: X-HE-Tag: 1708849458-936648 X-HE-Meta: U2FsdGVkX1/dA1Hb7/+snUdwU4H+4FoOojkiYJw9le7n2xHGo7pYWhTG7ybEbS9u9lEXmul5v0XA5deORx7aOhzYA9LpO1osSLLL/+Y1OIwAflxWdhxRF+O2orZtMmmnMP1NZ0pVN95CHRu2e2ec47FkyjCnK27UBBhbG7l0U26D681V715zZKBeDCLTQxxs54EIGFAQbgx++hOz3uElLxErRiWkUO+ylEV4rweicq07V/Hv/qOARhAcyJ+i5CIBQaBrqCWTZTJbVB+PhcGZBWt+Xrb1EULB1knv5WPEPPh0E+t1zfhvbxzUr5TS+pufNoWcODa53XDv2YCMRGknp2l02z4DevEenBeg+VbcUCwbphl6+3vjOvurxsDE6K9FL5XyysdVr5eRuIWEBgQLNNeLQxM+zDk9HVDQS6mRgF7ACF8+OUlzrQNstZ8ky1VoZWZe7a1+SkS5hytOEa9MIuuZZAJkAedSuyGvvzOeH+crewM1T3ZFSQoj/i0gJ82KoZ8R251AoifMdoJ65goqYK8x6de4Yd5yCEytYPecfXdohgXL+YqPVATYMPDL2/Bme0mQd+l97HveyZohottsPaidsbDgghbfj9943aRwmIWyiEXB0cu4Fl1qheWGUgrC99Db6X08TbjdzuXj6UZewQJhZoR841dRo6TG2IowbpYPuRQyfzzOZG95Bcp5iW2IPhYn3C2QUT+UvHcTx6f/nZoriMF3ivhV5gWHCOmkbfGDKEiTLVTcvPUmI/xCJeuiYWLp0s0uWM4xKMt3T1M5OVcybA558LfcKgkJbZ0KYkTg2hLjKhyH3ge9PiPtnCNycyZszJyYtd2boh7yzWNVMxpHlyxrJbkXF8gKKEWjCRhNqEmIb9WpCISBI4rsM4m2YXKo3CValIuc3aSswIdsREpasie2DpRr1qcNPFioN1v7l0JQ8hx/7A== 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_folios() 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 or handle shmem and hugetlb cases separately. Cc: David Hildenbrand Cc: Matthew Wilcox 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 | 153 +++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 75 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index a8f3af61f7f2..afa8bfd2a2a9 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -30,6 +30,12 @@ struct udmabuf { struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; + struct list_head unpin_list; +}; + +struct udmabuf_folio { + struct folio *folio; + struct list_head list; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -153,17 +159,43 @@ static void unmap_udmabuf(struct dma_buf_attachment *at, return put_sg_table(at->dev, sg, direction); } +static void unpin_all_folios(struct list_head *unpin_list) +{ + struct udmabuf_folio *ubuf_folio; + + while (!list_empty(unpin_list)) { + ubuf_folio = list_first_entry(unpin_list, + struct udmabuf_folio, list); + unpin_folio(ubuf_folio->folio); + + list_del(&ubuf_folio->list); + kfree(ubuf_folio); + } +} + +static int add_to_unpin_list(struct list_head *unpin_list, + struct folio *folio) +{ + struct udmabuf_folio *ubuf_folio; + + ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL); + if (!ubuf_folio) + return -ENOMEM; + + ubuf_folio->folio = folio; + list_add_tail(&ubuf_folio->list, unpin_list); + return 0; +} + static void release_udmabuf(struct dma_buf *buf) { struct udmabuf *ubuf = buf->priv; struct device *dev = ubuf->device->this_device; - pgoff_t pg; if (ubuf->sg) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); - for (pg = 0; pg < ubuf->pagecount; pg++) - folio_put(ubuf->folios[pg]); + unpin_all_folios(&ubuf->unpin_list); kfree(ubuf->offsets); kfree(ubuf->folios); kfree(ubuf); @@ -218,64 +250,6 @@ static const struct dma_buf_ops udmabuf_ops = { #define SEALS_WANTED (F_SEAL_SHRINK) #define SEALS_DENIED (F_SEAL_WRITE) -static int handle_hugetlb_pages(struct udmabuf *ubuf, struct file *memfd, - pgoff_t offset, pgoff_t pgcnt, - pgoff_t *pgbuf) -{ - struct hstate *hpstate = hstate_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 folio *folio = NULL; - pgoff_t pgidx; - - mapidx <<= huge_page_order(hpstate); - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - if (!folio) { - folio = __filemap_get_folio(memfd->f_mapping, - mapidx, - FGP_ACCESSED, 0); - if (IS_ERR(folio)) - return PTR_ERR(folio); - } - - folio_get(folio); - ubuf->folios[*pgbuf] = folio; - ubuf->offsets[*pgbuf] = subpgoff << PAGE_SHIFT; - (*pgbuf)++; - if (++subpgoff == maxsubpgs) { - folio_put(folio); - folio = NULL; - subpgoff = 0; - mapidx += pages_per_huge_page(hpstate); - } - } - - if (folio) - folio_put(folio); - - return 0; -} - -static int handle_shmem_pages(struct udmabuf *ubuf, struct file *memfd, - pgoff_t offset, pgoff_t pgcnt, - pgoff_t *pgbuf) -{ - pgoff_t pgidx, pgoff = offset >> PAGE_SHIFT; - struct folio *folio = NULL; - - for (pgidx = 0; pgidx < pgcnt; pgidx++) { - folio = shmem_read_folio(memfd->f_mapping, pgoff + pgidx); - if (IS_ERR(folio)) - return PTR_ERR(folio); - - ubuf->folios[*pgbuf] = folio; - (*pgbuf)++; - } - - return 0; -} - static int check_memfd_seals(struct file *memfd) { int seals; @@ -321,16 +295,19 @@ static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { - pgoff_t pgcnt, pgbuf = 0, pglimit; + pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0; + long nr_folios, ret = -EINVAL; struct file *memfd = NULL; + struct folio **folios; struct udmabuf *ubuf; - int ret = -EINVAL; - u32 i, flags; + u32 i, j, k, flags; + loff_t end; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); if (!ubuf) return -ENOMEM; + INIT_LIST_HEAD(&ubuf->unpin_list); pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { if (!IS_ALIGNED(list[i].offset, PAGE_SIZE)) @@ -366,17 +343,44 @@ static long udmabuf_create(struct miscdevice *device, goto err; pgcnt = list[i].size >> PAGE_SHIFT; - if (is_file_hugepages(memfd)) - ret = handle_hugetlb_pages(ubuf, memfd, - list[i].offset, - pgcnt, &pgbuf); - else - ret = handle_shmem_pages(ubuf, memfd, - list[i].offset, - pgcnt, &pgbuf); - if (ret < 0) + folios = kmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); + if (!folios) { + ret = -ENOMEM; goto err; + } + end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1; + ret = memfd_pin_folios(memfd, list[i].offset, end, + folios, pgcnt, &pgoff); + if (ret < 0) { + kfree(folios); + goto err; + } + + nr_folios = ret; + pgoff >>= PAGE_SHIFT; + for (j = 0, k = 0; j < pgcnt; j++) { + ubuf->folios[pgbuf] = folios[k]; + ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT; + + if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) { + ret = add_to_unpin_list(&ubuf->unpin_list, + folios[k]); + if (ret < 0) { + kfree(folios); + goto err; + } + } + + pgbuf++; + if (++pgoff == folio_nr_pages(folios[k])) { + pgoff = 0; + if (++k == nr_folios) + break; + } + } + + kfree(folios); fput(memfd); } @@ -388,10 +392,9 @@ static long udmabuf_create(struct miscdevice *device, return ret; err: - while (pgbuf > 0) - folio_put(ubuf->folios[--pgbuf]); if (memfd) fput(memfd); + unpin_all_folios(&ubuf->unpin_list); kfree(ubuf->offsets); kfree(ubuf->folios); kfree(ubuf);