From patchwork Thu Aug 1 10:45:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13750181 Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01on2087.outbound.protection.outlook.com [40.107.255.87]) (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 C182761FD7; Thu, 1 Aug 2024 10:45:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.255.87 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722509139; cv=fail; b=TEErysIky4nzO2wOh2rXufp7+qi4owe97Syh/WRz8ss0zaPVBOZwWs3zI2y/gsVQbYMrX9Es3p2tb+fcfLW8EENWD2bJBm/uhxdJ5S2zlGXsJoKmRqY00qrNbmiKs7ZpZWVQ01Gv3P6mj1RVFKsd7Gqo2qrSNNGR/sRqrqp5kbI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722509139; c=relaxed/simple; bh=a87geGfM8c3D9pmtNMhvm8Z8O0Ha01VZObjDYqvo4Q8=; h=From:To:Cc:Subject:Date:Message-ID:Content-Type:MIME-Version; b=IVSD52uiXxiLy4NpqmMSYBBWz6CFW/nru/q79or4S0PMi0z9+L+KeiCpBKEQb0C+HUDBQbBQVjHzIBBxZCTb6gXxec5D/9BP9EsYiVvxluEH5szO8Ztx4DVRc4p/4VdeObgYi6n7mBndDGb4tQ5Uvs0qQ2KZe7UcMLP3dwBE/RE= 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=mwH4q6bb; arc=fail smtp.client-ip=40.107.255.87 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="mwH4q6bb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AyN1t2jbQ6VFB0sEYryH8lYXxHQQXI4lCy4cyzAGfFIgSZ9L36Qb5OvUtgAFqD1oBHdfQPxX6hSVNzUhHY0uBehcjR4bdqAExhJE8DMA69qRlkrPlpJGzDsoFKxHJyWFRK0QuBAnjPspUb4znyOT9t9YB9DsJdRqjLYAMZo+7aA0yV79zZlliNwLGr8r78d5sfc1CVoBuuj8tBhG5f6ReoTlGjw4vPsc3I2BOWhDL6WAsi6QNqnUqH+OYN6KOWj+Dn/ioFVzApXYJRNhFCsYEV9MZbx+/8K7+/SOj3PADzTlzjEkeHOUCrrxilAMZ4ZU4OaxEZb2N6VAY4lgy8ApFA== 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=hF9wuMpHxu42jpsq2wh5cH97MY+xQEZ45ORDKVAmJkg=; b=NxxlGkDlWMhh+6xbd7XYvfF8zYLrkbLkFsiTpxYoonAP6AFyKWvDeVQftY9eRjGnDXIEoWkO40OYNN+/k6QYwVSe7mDGa3o+90mTYmVFqDE0gTilkyc+ZB17/htpN+pA3MilXrDvmFEVcYy6cSgImRhbE5DGHKZR6G6TpH7cpHo96RrxFwmK6qXwNinRSH1ozsel/+xSVDBM+vPR8Joz6zA9fsATCCMil1HMZfKlpXqWAMdIked/6jvg0+D0Q7J3bU4UXhiwU5xT3ioq65OGbdt9r77McxRU8EpwdYGbDwAr9TdWx1Fi56iN0pdPGdwFXd26oLuDiMgbtfBfOL/d1A== 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=hF9wuMpHxu42jpsq2wh5cH97MY+xQEZ45ORDKVAmJkg=; b=mwH4q6bbwS/1ZCTtj2z4euo4HZgEob68P9FoJP4Q8rSt3uHFWtEl7YxoXIzbXLztCkyni6tVV3V3/Ou3DH3k/g3DE39Wx+MJ4axRjA2QveLXByW9WtYryeXanfvCa+RLQWiOG0ZzuQ3OUz5BkPZxan0SAhXr5nif3/Kir6NqYBaqWlYnr0E3j/fUgry9AisZ27g5tOfGGpbYQc6yrZDlykXlRL8GZPFgprPN9IhgfahohgvAQHFqziHvBkaePD88Eo+nyEmRi8OHXmNyy2KudgF9bpWfQ5DJvaeI9l3l6NW51l4Zmdp1Draszfkhfa0aY3qXAIfp0YVAMuHWu0PwlQ== 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 SEZPR06MB6304.apcprd06.prod.outlook.com (2603:1096:101:10d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7807.28; Thu, 1 Aug 2024 10:45:23 +0000 Received: from PUZPR06MB5676.apcprd06.prod.outlook.com ([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com ([fe80::a00b:f422:ac44:636f%4]) with mapi id 15.20.7828.021; Thu, 1 Aug 2024 10:45:23 +0000 From: Huan Yang To: Gerd Hoffmann , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: opensource.kernel@vivo.com, Huan Yang Subject: [PATCH 0/5] udmbuf bug fix and some improvements Date: Thu, 1 Aug 2024 18:45:04 +0800 Message-ID: <20240801104512.4056860-1-link@vivo.com> X-Mailer: git-send-email 2.45.2 X-ClientProxiedBy: SG2PR06CA0209.apcprd06.prod.outlook.com (2603:1096:4:68::17) 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_|SEZPR06MB6304:EE_ X-MS-Office365-Filtering-Correlation-Id: 66280d8d-756b-46c2-a4f6-08dcb2170b9e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|52116014|366016|376014|38350700014; X-Microsoft-Antispam-Message-Info: hkY42Tx2NFZ3BTzFiWp+nwvGcMQVixvTrxiUVj3biUNKRyrL4Y9n5rcf+Hps3DkttvbbuDiP0CjIr45fyERpaZJVQE3cgZcZSYXw+LCgTguF6H/JEW/OAvafavorUrkiOH9qkb2j9DQnQbOoE3uMBnCXCPWrxvHIUDau/Bu/4SvUZdhTD4ZiBRhQZcvSgNAU90zXYzO3O9aomkx9b5LQlQ+DXP9FEJ4BDsH9GNMXjBTnTYJiUdyCaqqoWnDXeQFtjHNBv63PBvMT3p/AoZfNeQZ/yR1q49TztV+n8eHtgnQdikhDBVwqpy6B1FC1Sr+GvmsAvowsG2/u3fLkix79JXQ6isogrYNV2FbTEV77Xvpze30OPAZi4stnE7lub2SFN28nQFa2O0PIYIxSnWRx9TaD0eheRX+E9v2riYDs4eVjfR2/Vp/D/pU27u0N9WJtqN2dqq8TzmB7FSSidZfpV7rvMBGPImLAMlgzjwUKKRaS0oJWCamEr62ES6pRWYEo/Wcmg0RhyE3jtXI5chV5MJaJIoQWhOj+LLagEUxcdJfm0BXo090jmzfNc6iLD6P5ZUBrN1noSZBC8Uisv5DFQ94MxZeTRkLvfQgX476iMaPXsyWfndV3tMCu7mhMoXZLfHqKTAfxCS6u9FNlJ2E1bL5rOR/2y9Nl55PjtWMKxdSuy8tNHaoJADno9xDRVZZHsKE307ekBq3jr3l6L9gmgV7aeRpQ5IYrWoRbupXeBQWE+rIaqFWI3HKKP2ib/v35G7WZUsVzpDnyFcBfK4WoQKY+c6v1mqSBWqMh3EHrSLaTFrzK1SPHAqhhqchiyAg5+C2dQ8x+VzcTCwMMY4SQUkMDHGDnm4TTTkTJq+JSKKvy9Cu/dKWClexuMCHBEdIrFU+LXvVK/zhvomd/AF3/voV6YSl6zciaSVVm15mFrjm9hafLR5ge2pF41QiEm0Tvmdo6iQyH3V8Kzw1cMcHM01tr9XBYBpOKMHIChqLB8ENX97480WTPj3nwsYdBGxQl53M5wDTR8Jn0D8qoaS+DiJMjpjHqeaC6Ks3BZY9O/sV1JQC11nfgeP/icTzZdVIuAK9ivkFpoUXx89YRbKEXoxAIGD3kwvTlkFkEuv3q4vjyKuCV7u2Cu/d7YFX3rfbns2Z5wxLB82hwMvpUB/SMCAR2SS9oFsjp2HHIXqMQHXDWa/7uO5WG9E4O9dGH3xQ9G8/FcEPQuYujjhrRtfxlY/jgYTE8HU3uao13GwYDpaGLxXvV4imYnlZo0dn7reQUo92Hfb0BnFSQembNEcKd73KHJ7OmOlUz3FsOmZB596yANlPE2BZDlSuQLyPqKOI1Ce/Fw4Ku1TL8W3fcIMKb7Y3JgRozUROX263cZADvZX4= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(52116014)(366016)(376014)(38350700014);D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TC04tzTyqEJ8qWiELw1miEkJ+gpVt5zt2i/TFH77c5K70/iwa3mFsv0Odx13OJws2vt/RTfxryRpOk/jv9E+Bnz4K9r8OPOH5eJhYqCFxsjwpTiiTql//xEgPA6YdGExR03d5GzuC8f6rHNkdtAMldnKST89D6F3HGVKnlI6lUaaE/ak5nxKuNeARfqSFLArJ515b5E9NoRPvbUscRMDVHOtTSgYZBmzRGGglzlij+ifjATFfaEm+jH/J4TQXWfLFUS845mJZiqf+TTmSxh/SXD9cff2sS/jwsVsMqXgA6xpVpStn36+2RSoSjgHJfrvea0/qY1SCRAoxLW0R1kF34rk7gJUqokAS2hT9O2NZlsrlHvuvaNnLZiibtL94hg5SoDFWMpIHOEWAi9PNs3TpXzG2IeRAWTYAHIztY820BN897H4Ud//J3augOPDQDool9oZUIYnyAOBH3DvfH0KqBzT8d/lIutUb0pGgGoT69oLG8DCe31VaMxLTPpyzljLMqtAtLdaH7c4WKDcY9u6LgIbxPP+Aj+crYGIfcnaY3EiXSSswWIW1Gs+LrhJk4LRcJIc9QhzNHvY6KUxFrD5RTWC4s X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 66280d8d-756b-46c2-a4f6-08dcb2170b9e X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Aug 2024 10:45:23.0451 (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: nAkXnv5iGho3rRdkRxbwNQ2DPnJT/sSjwaG1CbIhNKlHvzkoUBM+g9OEkjn+cwJtlzSmSxIuR9HEZNVpz80Ztw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEZPR06MB6304 This patchset attempts to fix some errors in udmabuf and remove the upin_list structure. Some of this fix just gather the patches which I upload before. Patch1 === Try to remove page fault mmap and direct map it. Due to current udmabuf has already obtained and pinned the folio upon completion of the creation.This means that the physical memory has already been acquired, rather than being accessed dynamically. The current page fault method only saves some page table memory. As a result, the page fault mechanism has lost its purpose as a demanding page. Due to the fact that page fault requires trapping into kernel mode and filling in when accessing the corresponding virtual address in mmap, this means that user mode access to virtual addresses needs to trap into kernel mode. Therefore, when creating a large size udmabuf, this represents a considerable overhead. Therefore, the current patch removes the page fault method of mmap and instead fills it directly when mmap is triggered. This is achieved by using the scatter-gather table to establish a linear relationship for the page. Calling remap_pfn_range does not cause the previously set VMA flags to become invalid. Patch2 === This is the same to patch: https://lore.kernel.org/all/20240725021349.580574-1-link@vivo.com/ I just gather it to this patchset. Patch3 === The current implementation of udmabuf's vmap has issues. It does not correctly set each page of the folio to the page structure, so that when vmap is called, all pages are the head page of the folio. This implementation is not the same as this patch: https://lore.kernel.org/all/20240731090233.1343559-1-link@vivo.com/ This reuse sgt table to map all page into vmalloc area. Patch4 === Wrap the repeated calls to get_sg_table, add a helper function to do it. Set to udmabuf->sg use cmpxchg, It should be able to prevent concurrent access situations. (I see mmap do not use lock) Patch5 === Attempt to remove unpin_list and other related data structures. In order to adapt to Folio, we established the unpin_list data structure to unpin all folios and maintain the page mapping relationship. However, this data structure requires 24 bytes for each page and has low traversal performance for the list. And maintaining the offset structure also consumes a portion of memory. This patch attempts to remove these data structures and modify the semantics of some existing data structures. udmabuf: folios -> folios array, which only contain's the folio, org contains duplicate. add item_offset -> base on create item count, record it's start offset in every memfd. add item_size -> base on create item count, record it's size in every memfd. add nr_folios -> folios array number So, when building the sg table, it is necessary to iterate in this way: if size cross item->size, take care of it's start offset in folio. if got folio, set each page into sgl until reach into folio size. This patch also remove single folios' create on each create item, use it be the ubuf->folios arrays' pointer, slide to fill the corresponding folio under the item into the array. After the modification, the various data structures in udmabuf have the following corresponding relationships: pagecount * PAGESIZE = sum(folios_size(folios[i])) i=0->nr_folios pagecount * PAGESIZE = sum(item_size[i]) i=0, item_count (do not record) item_offset use to record each memfd offset if exist, else 0. Huan Yang (5): udmabuf: cancel mmap page fault, direct map it udmabuf: change folios array from kmalloc to kvmalloc udmabuf: fix vmap_udmabuf error page set udmabuf: add get_sg_table helper function udmabuf: remove folio pin list drivers/dma-buf/udmabuf.c | 270 +++++++++++++++++++++----------------- 1 file changed, 148 insertions(+), 122 deletions(-) base-commit: cd19ac2f903276b820f5d0d89de0c896c27036ed