From patchwork Tue Sep 3 08:38:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13788304 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2060.outbound.protection.outlook.com [40.107.117.60]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74E9819F407; Tue, 3 Sep 2024 08:41:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352918; cv=fail; b=Zk+TRpye+xb+vkpgJq+vwnPOK4Z3TVLjjTQRDjqO/7q22xoq6+pgfo1dupVFL6pBJT87+1lX1p227nInI4BcUNVM38li410clgqA3bxHEmEtu3lRSQ3ywHBbPeQIOZnUlPdAxBWg/75JNnY5Tc1iRB5gwNRrTAiYiNQkBLR6CHs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352918; c=relaxed/simple; bh=3GhRg7mSN5QNZmL4NHe5fzimHgjym9npTMSvLPvtTho=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=aWXp973yOby6GrDcabW0Ofmvj+RvTenNM7JV9chgRZOxJnySRHMpqeLoVbw+JeNCTQLIAdR4N7B/7OWwBJkVUbLZuLDK/rM6WOzXCBuIBEgrqTb9BUzQ5ycXDJEafU59q2UanvB7ZS5hQ9O1EgeSF201ofpzuD1/7M6KYU9re8Y= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com; spf=pass smtp.mailfrom=vivo.com; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b=gBrldKQ4; arc=fail smtp.client-ip=40.107.117.60 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=vivo.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b="gBrldKQ4" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WjrzoBmjhL8lpT1oYnHLmwoiP6KVNdS54hJA2aBZuEWOAGnJ82Xo0J94jyWkFWH9lJQfevy3Pc0TBQGNsbaG0hSb01gLOAgWvp7JJedE9iwAtymZRgk/ELqcK/XkPIm4vjEgHfOO8GjTvMhKTF3NzcI3yI/sJEJHnwUWrxcIeHeqfCxh/1qQt8wwEnASkv2TxAOba+lqnqv5XbHgTY+ZKCiQGQtfk2hj8ctSOUiF1FyeVEcygW4dptJWUEm4qHUuPl40anOtT66K07dGZdMuAZleDT2FV6J9b9QkqgQPskPbLyJs2kZB5LYpF8tMqit/wB5dYXmnj0JqBe/CKxixqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ZO19NvW2yqwHS9ErLNNIkL0bAv+E9vx+kkHbMqAsKoc=; b=RvurjIzPAWKNApuZkF+y1rnaq8cm0Q6pHwg2mTAVLd4tCZ8NM3zeKTvaw/WHj4NnriMEwvfvS7AWdW9zaYHEX61U2mAUdrMCqO35SYJb/hNnvzZ+Vn5NaYKXqMQ5/t161ekBTYziSJck8ywdiHk2DeycltAN2tJZAe2B4RMISveesUFLDdZQwX/oOIvxRsUmrWEu3EVhxjYMeDUpUX0DfZD14ls0cE7pVfg2X0ndt9+j7xeBW80XVV+GDNsIi18wzYQsXtzdHD30+7oDn0hIontd+CnKvmldP5ipfwiPGS8/iuXCirkPQ7aUoS91vMbvPqd7gLTSjo1wvFty4yN29A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZO19NvW2yqwHS9ErLNNIkL0bAv+E9vx+kkHbMqAsKoc=; b=gBrldKQ4tOnFXvjooYnCQQmsqyeD85qnWKYvTzBWjrOXTM3dbMIoB7yuwJwxsirGBBBUU8W46LpwPc/NvnmMrqVlS2umz1qhrCkqTaXHnWp3PSsWK2785qIptaO0Rnm0Ee3qZJv+UpGRMsO8r5J40bdJuQ1YgZlP3FiqYtVNJ8D/TmipWCiRUCcKS9EjjLDWDcFvKgZQAMw9fj5NIiJz1P1O5sZvkNDTF8TLRhJy1tA7pta5DKY0d5AOouLxjOk01sMLXW2sKrQYSf6DwJccCidBMd+yQ7J/gcz/fuDkF1x2Jd4EWebwWOtG+h2I9FQPZSxbUwft16JlIFTC3lxNuQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10) by SG2PR06MB5359.apcprd06.prod.outlook.com (2603:1096:4:1d7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.24; Tue, 3 Sep 2024 08:41:55 +0000 Received: from PUZPR06MB5676.apcprd06.prod.outlook.com ([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com ([fe80::a00b:f422:ac44:636f%6]) with mapi id 15.20.7918.024; Tue, 3 Sep 2024 08:41:54 +0000 From: Huan Yang To: vivek.kasireddy@intel.com, Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Gerd Hoffmann , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: opensource.kernel@vivo.com, Huan Yang Subject: [PATCH v5 7/7] udmabuf: reuse folio array when pin folios Date: Tue, 3 Sep 2024 16:38:12 +0800 Message-ID: <20240903083818.3071759-8-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240903083818.3071759-1-link@vivo.com> References: <20240903083818.3071759-1-link@vivo.com> X-ClientProxiedBy: SI1PR02CA0026.apcprd02.prod.outlook.com (2603:1096:4:1f4::11) To PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10) Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|SG2PR06MB5359:EE_ X-MS-Office365-Filtering-Correlation-Id: 4d0df1d2-189b-419d-aab8-08dccbf443a7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|52116014|376014|38350700014; X-Microsoft-Antispam-Message-Info: QaUsqUtOJKBAq4wo9kQdnWPRJoaLGKKPUO2WvuMF1iBjNtf/MNJPa/C5ce4WltNglHA88rkBluAd3ONVKEvc1TP/VBDs9cNs9yzB8y33NDjyJ1lhyf9Twd842L9NlK9J+EfaU85T/MoDrgLOrnax5zVOyf6QjsiUHVZck7+FA6BJJslcFl3yepbD25GCQ5Hp0L5VQ5szF0nU0Crygf0LRmrX86LZagJuBiMBultfjM9u62rVDR0R9IUfq/yWa/sDZzLqJi7N9/RI+rsu88XudtxhfrYy9fsruNPW9blmWLN6M2F8QXoXWabCpcmyH2SRBvWqtPc8OscsZ/HstpiA0rH0aYhLaKrMxQji0sGz2qtihQy/bSzRAjRvDNKFtouUplvvr4qpnSB94s+HOdF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FV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(52116014)(376014)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VfJ/7h8ztenxhHQ4FnT2vEw8aFGrOXpXTM02QOo79FkOFLbPPoeMKDZKUNDtZYjhgkg4HhUY7QKqnh9MdFtzOUMulhxHl4oOIw7ZatLJqVLtGxIQo2199sK3HgipopahGDbIJn06TPVzvPgrbOvsv+xmEisvzwpyCezs/sPPMTEsxE/SY78KAhYZTRYlYVSs1RE5kYVhMpbnZTYvlM+VOwB48RNNl9LKI8XlEXBaSVm5kmqA0IVYnrQh4j6TiPnni7vAiGuRAQgsd6sAZodNMO7fpbUgX5nrK0TdA+K3QGUgUkP49PDK2skp01Ey7RHGQDtWlwP/XCcuXRV4Ai/iFYb11holfZ2hUgpHZyGrJToE0StzwaBMAKNw4u5E7G0qwSU9KbITO4Pw7ZWB5qd5vZa58RdtEJfOt3jgUen/OId3RZpXl71PqWBj7sjmJyoB9gbGYUQDBeaQPsvAMfnfLx36WPxvqA9WV9kBNRi1ZAQEgkWzAu2UgPnXdwzFiJEH1LX9WFSJWjVb12yIkmI9ni6+xsCgS4yQIvsX3etqhnfnYGUbHpE4sh32zPJ3ZRyOM4AxAL5Bgr76vi4DO3eEalHjj8J6pXx7pu7eYDmB9vypHDPEiJ0sEIoiCzdCxFfD7J8HWCwl3VTvG9UFesUWGF+UC0xuQOvoWXrIxAYhIjWPY5lAwdc+gSRxgozslmEqDo4hM+g3srlFKJVHDdRvwENOsPbPDYVZbTCZQfsRkj90izjZ+K0xBhPZlVoMdX3dnIbHZKaW2nBlK6MEbyRwLL5q8TP92FEKLLxYwGnTyAS2/5PLA0Yql25i05uH9JDKTryYsYiNPfOy2N7sUOtTwahy0YWNKYzc12X43cCyBRtKzLxf/i1TG7KaetZy1WaUObWjDPVlpF7XZQ8QQuh3LO83sBtkVpDKzrDlzWPaHSX1GEHeh2h+i6WRSSigYPEat6Ftv/mIsK13rTFmfHPVf74Xp9ZEM+dF+tPz89Weo/AWj2GqhdalSIcL0BoyC/H/7tJc5ecFImERmpOseQMiIv3ugE/vu53k+4ftP6+wL2qxaTD0/Yy9B69H7Wl4HaQHz8blUbSBndS7XZX8kGRHUSP1tmZ/UhICdG7Qmv/DG/f84Sn8ViCHakTmEsnOT661/ekuTh3GHqlEDFr0Y8edvwHubsPaXyoSA2gLd9pavt2/e1sJElNPJxGJh1EF9Ot/ZeAgn3fIPVSl2F9/8brwJO33XWKmdgsDMcVlvjAAq1vs83cNzOS2dekSH5UxFli2LS/lxqJx001mlg37H1k5y7PSLvuqQ/QJD65iwFddKyESwPaB42dHaT4+uD3OMlQF4pxs52dJQZ9oDfZ1d5US1wqPtzJ73R2Xxzpr0Gdn23Kkr3QibHbof8AcRgVkDcHh6yvcpo4gdhsNmHoLDyWK+k5nHKWqcv1vUmaZGMMM/I0LPBt14ko+ky4ndL2BivM2Is2bmFEu9jwV73mnhM7AWFSGUGN+yqSSemzvEiAw1MLZYTI18lR2D4Bv6TI+L8BzPw/JwRGOBHUEyFQtUwEXUMsHpQScwusK90jL96Tevod+PFAdbMrJVAd2Pho/cUlB X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4d0df1d2-189b-419d-aab8-08dccbf443a7 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Sep 2024 08:41:54.8969 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: PGwm1g29Hv0XqNBYTKe5spvNeBVPwFAiHEwvfGt2dYujebYrQ9Il7ik/TAEJPCId5N48qqI3O13y+4lk207dGg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB5359 When invoke memfd_pin_folios, we need offer an array to save each folio which we pinned. The currently way is dynamic alloc an array, get folios, save into udmabuf and then free. If the size is tiny, alloc from slab, is ok due to slab can cache it. Or, just PCP order can cover, also ok. But if size is huge, need fallback into vmalloc, then, not well, due to each page will iter alloc, and map into vmalloc area. Too heavy. Now that we need to iter each udmabuf item, then pin it's range folios, we can reuse the maximum size range's folios array. Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index d449c1fd67a5..d70e45c33442 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -343,28 +343,21 @@ static int export_udmabuf(struct udmabuf *ubuf, } static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, - loff_t start, loff_t size) + loff_t start, loff_t size, struct folio **folios) { pgoff_t pgoff, pgcnt; pgoff_t upgcnt = ubuf->pagecount; pgoff_t nr_pinned = ubuf->nr_pinned; u32 cur_folio, cur_pgcnt; - struct folio **folios = NULL; long nr_folios; loff_t end; int ret = 0; pgcnt = size >> PAGE_SHIFT; - folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); - if (!folios) - return -ENOMEM; - end = start + (pgcnt << PAGE_SHIFT) - 1; nr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt, &pgoff); - if (nr_folios <= 0) { - ret = nr_folios ? nr_folios : -EINVAL; - goto err; - } + if (nr_folios <= 0) + return nr_folios ? nr_folios : -EINVAL; cur_pgcnt = 0; for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) { @@ -389,10 +382,8 @@ static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, pgoff = 0; } end: -err: ubuf->pagecount = upgcnt; ubuf->nr_pinned = nr_pinned; - kvfree(folios); return ret; } @@ -403,6 +394,8 @@ static long udmabuf_create(struct miscdevice *device, pgoff_t pgcnt = 0, pglimit; long ret = -EINVAL; struct udmabuf *ubuf; + struct folio **folios = NULL; + unsigned long max_nr_folios = 0; u32 i, flags; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); @@ -411,14 +404,19 @@ static long udmabuf_create(struct miscdevice *device, pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { + pgoff_t subpgcnt; + if (!PAGE_ALIGNED(list[i].offset)) goto err_noinit; if (!PAGE_ALIGNED(list[i].size)) goto err_noinit; - pgcnt += list[i].size >> PAGE_SHIFT; + subpgcnt = list[i].size >> PAGE_SHIFT; + pgcnt += subpgcnt; if (pgcnt > pglimit) goto err_noinit; + + max_nr_folios = max_t(unsigned long, subpgcnt, max_nr_folios); } if (!pgcnt) @@ -428,6 +426,12 @@ static long udmabuf_create(struct miscdevice *device, if (ret) goto err; + folios = kvmalloc_array(max_nr_folios, sizeof(*folios), GFP_KERNEL); + if (!folios) { + ret = -ENOMEM; + goto err; + } + for (i = 0; i < head->count; i++) { struct file *memfd = fget(list[i].memfd); @@ -436,7 +440,7 @@ static long udmabuf_create(struct miscdevice *device, goto err; ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset, - list[i].size); + list[i].size, folios); fput(memfd); if (ret) goto err; @@ -447,12 +451,14 @@ static long udmabuf_create(struct miscdevice *device, if (ret < 0) goto err; + kvfree(folios); return ret; err: deinit_udmabuf(ubuf); err_noinit: kfree(ubuf); + kvfree(folios); return ret; }