From patchwork Tue Sep 3 08:38:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13788303 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 6AADA1D86E4; Tue, 3 Sep 2024 08:41:54 +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=1725352916; cv=fail; b=IFSIDFkgajUlkDdOZWITS0ozRaXQOGFJ6/0z1vIMutm/q473EAoQladuWJvWcL3IVWRjgRy6aY7TLAGB32fboTu45IU8m5AMpU07JN+nl0A42begBJbixk8naTRXKKcuTzbZch5derodRNMDsZmK/QzcwYDUoYAQA6xpYUdzodY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352916; c=relaxed/simple; bh=R8I81NvpBR0lVlTEVYqm7gYrhKrTWrG+8HtvdWKwmNo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=UdrKC1CgY5KWl60YsK9iIz62Ix9X/crS8AiMv5exLD0+JSwWOLWn7j45nmrQcCJd4D4ddnK4YXFAGr55GpDg+fclpg/OVqtiTSUnhThrhWXR1QT0QQHEmZoYb7auRCTyuTHkWo7Bk56Kgcw4jm+Hf5W3CterzkxZcQ0k85GlyFw= 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=EHIPtM9x; 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="EHIPtM9x" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Nle11Vm9Bod4tsd5/kHfEUXC6GsdqVIiwV/12SjavF1OizJMIw1UsXmOQJs77P7QjD2V6HTVTBtv/XWW18suphh5lkPci88FjYqZStt5SSou/cnMqPqHhsysIKlPPulB6vtY1qT53iUIgtlFTyn0Sk/25nIN21W1VYe06HuZ9D5wHsph26BFolATTlWtFMeIQfeT9TfzqX0oGKlzGX0nfCsEaauV6CUrCYwR7YjLWAlpexBKLszg6/ZWYbJBuix8x1vHR9U+KD9hdPHElgSW9NK6JxU/RqlCM1V6XNqaHU9JJn1y2MX9gGUxtgNJHg+GZ8FY3MET0YbgeKizhFwpyw== 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=2w9ISoNQ1TCRixOf5sLn0DMvJi7b12W4RPHtDUCStiw=; b=FS6I4mVkCTIv/nMkq4nckItnc0IWwJA266V8F51FNCNxhitHpib5wm0Erg9G7O5cHEb+SL9uHjRkdSkulmTTWq/JURbMaok8TWQNx5YQExmjtoRMg8JtHNRV2VWUV5lQJpAO2uVQb4K3XMazzoYzguJ0qqZH/qyIxsy8ZBSja+7ZQ7TBBEhV7CFDJWtN+YB7anHgOXcHIvWmBIHh/R++il9MZIGEoiBjOmsg1CBbkph4Bi0QUDBBrviZBpgGFEIKZeSbVFbVazoLvK0mEc8d2xBl2fIYlZpL36A+B8SljvyyCOJmT4RdrSmqqKDAQxPseKEts3qb6rRmQfI3k8OxyQ== 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=2w9ISoNQ1TCRixOf5sLn0DMvJi7b12W4RPHtDUCStiw=; b=EHIPtM9xQiQPONc11ouLV0nXGwx2PTMr5f+VCu68inahk2iWOisvKvo+fvOUa/+2/oTTcHOtz0DZTW1Q6ixAKbu6gBWikGVCc8NJG7/wv6vJAMTv24QYuaTiPo/HKVyX/I+SJyB38LW824XE8wg+Xh43Z9QJGophbW+ndNGZut6cxsW7or8wxZvz746smRxxprVvqFH/tkMrO+PRB2/QftICv9UFH2QKeoFTgMGHBq7tFg/Krmogat1Ypn1nUgD7JoGd47jBkmriA6VaK5xVi1PC99G9qC2x+1OG2SupEh2H6DmwoizVKPQ09IdWY/dy8iyNhlw6LQs3H5BS3SiyCw== 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:52 +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:52 +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 6/7] udmabuf: remove udmabuf_folio Date: Tue, 3 Sep 2024 16:38:11 +0800 Message-ID: <20240903083818.3071759-7-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: f25467ed-1eca-48a1-e229-08dccbf44219 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: D57RObyxoOlSOocTFfzsVAo0sVRHkAdZUGEtrcvwqINfk10nulEN6B3nwQxCbPwx7Ub8aRMRkckYkyxkW/nxztxbb0y8Ac5ClrLZTD0U2yqdjET+v0n0m5xo79lSw+S/m4m4m+4w7AXVzrsjDn/d9o9abfxYRFC6Lgan1KwhdplBQG3LMSMxBPkKj1ipc+8LDvbhpUP5++CdNkMqCZM5fxscUGjrc9KJNmYW4QKLCZIfr4FJYeZlNAN0gdIKjGhGjh9edHzuQyxthtlZpZqv4EoJO7r+HaU3fzVSaGlj+dgD2lfPutvlQ4oynAsymZPucNkyH4jzoW7IupBIxTqMHMl5URGDS16xVsvu0E3sFmjtFp1GTAXu9LTWlYfwvp5Gf1zq5ZFGw+iJ2FGqZo0mpDjIWpPBCGu5kR6E5zOBa19k4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5676.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: USTIxZZq6cMXN4G8MbZtah8/MEF8JEXQ7oQ5JR2L0h7CGDPTEGhdWgPtSn5vrT0W3AavUXencKlmV3krMfmjlh3xB7NdjDb3+DcVe5gGmtjr0WGwEK+xhTshZFnowzthTuvINnbXs8jxU6geFk8IErADWKkOIoCK2j8m3isoqrHXyM7UsMH2jD4n8aORO53qUTLEpxdt6GwUWNaveom/v2RBIERDk44eRrTnNmgRvpN6JO6WoKHrVnCGCE+7DBOls09pmUimH5gbfJBDcvm/NLKZ1QwU/I+r3XU9vbfLnT4Nep8hWluw+5GoEuLWa5D1BrZX8n7KtczW6hqfKUOOiYCxnhyQTXq0x/xgAJuEVN+aoyXqN0Rn50P+0aUnzrQuV83ezTR+VvrBtaGEy9mo0yx1c6YuJD2ipkT/P0UekLHiqS652TQFVwNAIi4FnR41L23ATelgIqq5FXa4rBHM3yWU7hxMCsS0KuOte5d/wtHb7mge91jD/LiSDm3D8ETp2VYs+YVR69FyjUg+9zJvfcZUfpLvpnW2qHFMqLhZ5TwrisiuPwaOkz2UtCc6TotxUm4T6ovrF3sRImS7Kjqw+nqNqcPlnfBPqMkIYYunDGVFV7dceRYVqW/Ki5V+TqMA/+uDzx0dp5dkofttqFSl0Bou5Ome3HQI7v4r7jj+iHfbCaPfMNluOHLo2gcY8yiWIg2HjPHNkCBiGuW/9NBO3KpXtoAb5mwrrYmmWaHZezE9Gj/N7DOVExcPPdMW9MHQK+6qrx6XEZBKv3xFDcZ3KX8i2l/dPdecd0ziFj/80WRUl4Wg2eVpswP0PibOneD+z63KgtaZNC9Ai1jnLB0Hwk9QpmjuOvPAjIzCvqO9clMyzdD6lQxHXTvKia6zE41+wBT75G234MDqkt+kX0b7aVgH3j3q0mvLxISU56EGFKguTuy0k/PTXXXeHy93Cw1e2yDtojc8QP5QR8DkgqcKaMo096+7ZAjQaqRjsaUc3jLj8yvw92eoZZcZ/37QX6z0QUYtLnwef9GrDmwYBlan7EgT5r4G2aoW6yPK0Jd3u3RMweNmLxKMSP8ANJRuDU6tt2eGBj/xaqn6C+JQn8uA33OVuhZR5Ha1NSbCDqwb3V65s8b/VpFsMR+bCOBFFGvHtoK58TOlWLMHwV541ODWSMGuNxx5uxJm4J120UTGay8emqMcGrjIA0nSsrie70xYOMfoZuew1WvVTrMYNgAt2Q9eDhTmWPCbKuQUWnBLg0NK2aTvIVyQorrZOsBhdtCtDdzoXk/e27/pMRFGHolo7hJZo1gcKE8HaAZ5gQtIIjT/HWMVOFmZUuyLeRtp0ui2FwXvveh0Yt/izRC9v7Otf3fBhi4t41JQFyyUol65rESBOI4yMWAZsLqtb13ZWRc0GKW8KsfomDVtEeLbe/pFBZ4rEwWibAnzIbLB9ybKc1VtDg7KKiBi6V0RSEVnw5VW2VcKXIJ1R/qG8oSrh88To0XRPQwIJjEXP6flPGL1RhI4TBAJTt0WXiLM8jYrIhac0LG0ZkfR7XcExxOsH66IR+CMHzQxaXDb56KMgprA+jvxfnxq8/mruRdqUxvAXxn6 X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: f25467ed-1eca-48a1-e229-08dccbf44219 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:52.2579 (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: +fZMt5zDK8qC+CCNcWzpx5ydeJohsc67KhGiYXZ2zH5+ISHTljGqpEum4bzih0G8TJci8uE3/83oGmoMc9wR3g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB5359 Currently, udmabuf handles folio by creating an unpin list to record each folio obtained from the list and unpinning them when released. To maintain this approach, many data structures have been established. However, maintaining this type of data structure requires a significant amount of memory and traversing the list is a substantial overhead, which is not friendly to the CPU cache. Considering that during creation, we arranged the folio array in the order of pin and set the offset according to pgcnt. We actually don't need to use unpin_list to unpin during release. Instead, we can iterate through the folios array during release and unpin any folio that is different from the ones previously accessed. By this, not only saves the overhead of the udmabuf_folio data structure but also makes array access more cache-friendly. Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 65 +++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 254d9ec3d9f3..d449c1fd67a5 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -27,15 +27,21 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is struct udmabuf { pgoff_t pagecount; struct folio **folios; + + /** + * Unlike folios, pinned_folios is only used for unpin. + * So, nr_pinned is not the same to pagecount, the pinned_folios + * only set each folio which already pinned when udmabuf_create. + * Note that, since a folio may be pinned multiple times, each folio + * can be added to pinned_folios multiple times, depending on how many + * times the folio has been pinned when create. + */ + pgoff_t nr_pinned; + struct folio **pinned_folios; + 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) @@ -198,38 +204,18 @@ 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) +static void unpin_all_folios(struct udmabuf *ubuf) { - struct udmabuf_folio *ubuf_folio; + pgoff_t i; - while (!list_empty(unpin_list)) { - ubuf_folio = list_first_entry(unpin_list, - struct udmabuf_folio, list); - unpin_folio(ubuf_folio->folio); + for (i = 0; i < ubuf->nr_pinned; ++i) + unpin_folio(ubuf->pinned_folios[i]); - 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; + kvfree(ubuf->pinned_folios); } static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt) { - INIT_LIST_HEAD(&ubuf->unpin_list); - ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL); if (!ubuf->folios) return -ENOMEM; @@ -238,12 +224,18 @@ static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt) if (!ubuf->offsets) return -ENOMEM; + ubuf->pinned_folios = kvmalloc_array(pgcnt, + sizeof(*ubuf->pinned_folios), + GFP_KERNEL); + if (!ubuf->pinned_folios) + return -ENOMEM; + return 0; } static __always_inline void deinit_udmabuf(struct udmabuf *ubuf) { - unpin_all_folios(&ubuf->unpin_list); + unpin_all_folios(ubuf); kvfree(ubuf->offsets); kvfree(ubuf->folios); } @@ -353,7 +345,9 @@ static int export_udmabuf(struct udmabuf *ubuf, static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, loff_t start, loff_t size) { - pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount; + 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; @@ -377,9 +371,7 @@ static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, pgoff_t subpgoff = pgoff; size_t fsize = folio_size(folios[cur_folio]); - ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]); - if (ret < 0) - goto err; + ubuf->pinned_folios[nr_pinned++] = folios[cur_folio]; for (; subpgoff < fsize; subpgoff += PAGE_SIZE) { ubuf->folios[upgcnt] = folios[cur_folio]; @@ -399,6 +391,7 @@ static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, end: err: ubuf->pagecount = upgcnt; + ubuf->nr_pinned = nr_pinned; kvfree(folios); return ret; }