From patchwork Tue Apr 14 14:18:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?=C3=98rjan_Eide?= X-Patchwork-Id: 11489977 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D27196CA for ; Wed, 15 Apr 2020 07:18:43 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B04E8206F9 for ; Wed, 15 Apr 2020 07:18:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.b="0y5xFEfU"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.b="0y5xFEfU" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B04E8206F9 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8ABD66E868; Wed, 15 Apr 2020 07:17:58 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from EUR02-HE1-obe.outbound.protection.outlook.com (mail-eopbgr10068.outbound.protection.outlook.com [40.107.1.68]) by gabe.freedesktop.org (Postfix) with ESMTPS id A1D8D6E142 for ; Tue, 14 Apr 2020 14:20:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Yot3Zxj9fu5OJnFKoioziT1z8b6vkpN8f/vIFsaKci8=; b=0y5xFEfUG7nVhWtTte9YvO2PP1RoDHKduEg2Ld4xDM2P1j9//LhwMKwzq0lzYsycqWjACuUdiZuSFZN7UyPejJbN/SQYwp6qlpnEdybvwYPJ5OBaxSLTXaMj3y9CaCh+39jjGwsmmJI+Zf7UgeeN7YDrSGtq9pTStEKOKfy+Gww= Received: from VI1PR04CA0137.eurprd04.prod.outlook.com (2603:10a6:803:f0::35) by AM6PR08MB4753.eurprd08.prod.outlook.com (2603:10a6:20b:cb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.26; Tue, 14 Apr 2020 14:20:28 +0000 Received: from VE1EUR03FT030.eop-EUR03.prod.protection.outlook.com (2603:10a6:803:f0:cafe::47) by VI1PR04CA0137.outlook.office365.com (2603:10a6:803:f0::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.17 via Frontend Transport; Tue, 14 Apr 2020 14:20:27 +0000 Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; lists.freedesktop.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;lists.freedesktop.org; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by VE1EUR03FT030.mail.protection.outlook.com (10.152.18.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.18 via Frontend Transport; Tue, 14 Apr 2020 14:20:27 +0000 Received: ("Tessian outbound af37c2b81632:v50"); Tue, 14 Apr 2020 14:20:25 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 9cd3325e54d7a9a2 X-CR-MTA-TID: 64aa7808 Received: from 56e8c3965797.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 6AB08F36-6864-41D0-823F-0AE3BB2C1C3F.1; Tue, 14 Apr 2020 14:20:20 +0000 Received: from EUR04-HE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 56e8c3965797.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Tue, 14 Apr 2020 14:20:20 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ebpxkd8D6c41cV5AMCM4D7BVbRZSjUFS+6jE+qEzbfiDcVHfrKIWIAlD0m+OA7eJNkHNPhEgSSSuy9orDR4ROJDT3nAsG5wr9P4xQ7c4oOYP3+JurV+dz7fkpvHXzx3DtcNtcvD/FMxUzCx5qGwsWsNA3nYq532U4vX+fTGpeZ2eZt7yborfCLe9K53Gzvds8s8cTnPwujRNks9+CNdCg04lgFrdlMzsAZqm9s2aYv4ddmBp9JwNCCuOFf45WzSjbKLjtAlQjkDdW3OJe4XjoFbEImbcmrfM7lA1O6xZK5qjACglFA5z+5hI63xs281NMZQyXzbP0oggm1yvhVeqRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Yot3Zxj9fu5OJnFKoioziT1z8b6vkpN8f/vIFsaKci8=; b=MW8wCF4r14msGevWpf4egYtM/cqFG60QVVctpcRNKAMFwlcvYcmTB0mx0PNz/qKJzu4LFxDvPpWEiBnxvXF9J3vucgQVJ4BoS93bKkH/xaGiYF3xaHOT9MuU8ucqlLzbQuEMMB+pQbw1/jHmMWukROcRl1DByZaewHMSer4El3lqqcR1BmSPzQq3KjXN3THSfP8ya8ajG3mXxTQHDdQBLPCahstA/xKq3uI2mmGUZiRbVS8WEoNSmw70Zgz75bb/kz0uA2sVefAR2Fhe5LBJsio+8t7zUoyl995IZOedwrt52s5O8e+npOgnTaogH23ETX/70G8L7b3lUcEawqUuiw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=lists.linaro.org smtp.mailfrom=arm.com; dmarc=bestguesspass action=none header.from=arm.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Yot3Zxj9fu5OJnFKoioziT1z8b6vkpN8f/vIFsaKci8=; b=0y5xFEfUG7nVhWtTte9YvO2PP1RoDHKduEg2Ld4xDM2P1j9//LhwMKwzq0lzYsycqWjACuUdiZuSFZN7UyPejJbN/SQYwp6qlpnEdybvwYPJ5OBaxSLTXaMj3y9CaCh+39jjGwsmmJI+Zf7UgeeN7YDrSGtq9pTStEKOKfy+Gww= Received: from VI1P194CA0033.EURP194.PROD.OUTLOOK.COM (2603:10a6:803:3c::22) by DBBPR08MB4235.eurprd08.prod.outlook.com (2603:10a6:10:c2::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.17; Tue, 14 Apr 2020 14:20:16 +0000 Received: from VE1EUR03FT023.eop-EUR03.prod.protection.outlook.com (2603:10a6:803:3c:cafe::63) by VI1P194CA0033.outlook.office365.com (2603:10a6:803:3c::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.15 via Frontend Transport; Tue, 14 Apr 2020 14:20:16 +0000 Authentication-Results-Original: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; lists.linaro.org; dkim=none (message not signed) header.d=none;lists.linaro.org; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; Received: from nebula.arm.com (40.67.248.234) by VE1EUR03FT023.mail.protection.outlook.com (10.152.18.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.2900.18 via Frontend Transport; Tue, 14 Apr 2020 14:20:15 +0000 Received: from AZ-NEU-EX03.Arm.com (10.251.24.31) by AZ-NEU-EX03.Arm.com (10.251.24.31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1415.2; Tue, 14 Apr 2020 14:19:18 +0000 Received: from e123356-lin.trondheim.arm.com (10.40.16.105) by mail.arm.com (10.251.24.31) with Microsoft SMTP Server id 15.1.1415.2 via Frontend Transport; Tue, 14 Apr 2020 14:19:17 +0000 From: =?utf-8?q?=C3=98rjan_Eide?= To: Subject: [PATCH] staging: android: ion: Skip sync if not mapped Date: Tue, 14 Apr 2020 16:18:47 +0200 Message-ID: <20200414141849.55654-1-orjan.eide@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200414134629.54567-1-orjan.eide@arm.com> References: <20200414134629.54567-1-orjan.eide@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:nebula.arm.com; PTR:InfoDomainNonexistent; CAT:NONE; SFTY:; SFS:(10009020)(4636009)(39860400002)(396003)(136003)(346002)(376002)(46966005)(186003)(36756003)(86362001)(5660300002)(82740400003)(316002)(7416002)(54906003)(8936002)(70206006)(70586007)(8676002)(478600001)(81166007)(336012)(47076004)(1076003)(81156014)(2906002)(6666004)(356005)(26005)(4326008)(2616005)(109986005)(7696005)(426003)(266003); DIR:OUT; SFP:1101; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 536996ca-90c0-4029-a1cf-08d7e07efae8 X-MS-TrafficTypeDiagnostic: DBBPR08MB4235:|AM6PR08MB4753: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:10000;OLM:10000; X-Forefront-PRVS: 0373D94D15 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: vwt6JmX/jYgYH221sT3He0dXJ9cM5UO91FaGM6MJ3VQTfWd1/8g6S0WXYq5PuvSVFd9o7Q/h2aDk8P/V+LX84kpKBCg7NvPVPD0SDMSpxzpTLCAc+YgC3HQVHfEyUac8WmV9TKGKcSYIWDkvS6LsAPzA7Af0bF5gab8UPFxm3VYMnNGg7u2gdus2TKVv1pB++DrcTsTTye8OwZxNjYR0yMC7CdaG+6y6UC64iPn37Cy7vqs1DISoQbQFzwb+FAG6c/J62jqxHwbFT2KelceKSE10KSw0ab3EXRTbxsteWG5C2KNk4RLtzj1JS8vOwByZFwhsiYqTGvJKq6n8GVi4emgcH11elcnOyz01WMXUQuXAPhQiB7iixGSQetVaMGJ2gitGlyi/G5lqzh4gxWYkoHzeT4OK5ssV+ryWDqoUQJ7+WWw7GPZxMc4hAE4n7dZDKG/+nE0bxWkjzNShrRdeK/oK8dM1uxVQWv5b4vpTdQJWX79/Z8bTbdXnVZ09DVqPcJQkKOyaI8TOjLKU6hugBhjRCJJTu9E1pEHKAEPsq7A= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR08MB4235 Original-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; lists.linaro.org; dkim=none (message not signed) header.d=none;lists.linaro.org; dmarc=bestguesspass action=none header.from=arm.com; X-MS-Exchange-Transport-CrossTenantHeadersStripped: VE1EUR03FT030.eop-EUR03.prod.protection.outlook.com X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFTY:; SFS:(10009020)(4636009)(376002)(396003)(346002)(39860400002)(136003)(46966005)(478600001)(26005)(8936002)(86362001)(426003)(8676002)(7696005)(5660300002)(54906003)(6666004)(107886003)(109986005)(81156014)(26826003)(36756003)(82740400003)(47076004)(336012)(2906002)(70586007)(70206006)(186003)(2616005)(316002)(1076003)(36906005)(4326008)(81166007)(266003); DIR:OUT; SFP:1101; X-MS-Office365-Filtering-Correlation-Id-Prvs: c2b39d7b-d07f-4370-27b8-08d7e07ef3ba X-Forefront-PRVS: 0373D94D15 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9pklo+P5UybQKli3fImTqmkdMO9fOuTvYyUz4LJ4o3V6ZWi/mnbBsleYUvBSRNvkUmWXDu3uzphtYZiQeW0RLM6oGGHZsKZ0KjpfT+HBWgFhUnNVCn/f4aCqutUu08wS1wH1ZO2e/oxmCbl8TpHInBZO6BOTArivf0pcjOYomyCy22v/lSGgHuvFOvb3ZFSA6Y6zrkdifHUIXNup23QoXoPFHcGr1MtpC6jCC/i/yfgis2JqjhFLEo98zhebf0F4xq7ug1asS2Rn7XHU6S+vihGlfwtGfo9T8LZfco3vJj5+ale3qYhltnFl78AwEq0RYMMcZ/0yXCw9NsoUBO5tU/lPjX7P6IT7Y7c8jFq9OJpOAVx9ceivZIBc0Ul2suJSFfxEDXf3bsLzca4hbg3rnMboWf3V+EDK+Z0ICMQL0hplSxmz5XRXjT6+gw1qrDpHZCDPB8I2mR0kUn/ZJggwB6FZdeTG3OVv5VxVhB7swiSmGpLJsB0W5+/ItpetQuDANXwWTRXZwtVC4n4/UxYBy98F+H4665N7KrHiRv41/TM= X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Apr 2020 14:20:27.4834 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 536996ca-90c0-4029-a1cf-08d7e07efae8 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB4753 X-Mailman-Approved-At: Wed, 15 Apr 2020 07:17:53 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devel@driverdev.osuosl.org, nd@arm.com, Todd Kjos , Lecopzer Chen , Arnd Bergmann , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, =?utf-8?q?Arve_Hj=C3=B8nnev=C3=A5g?= , anders.pedersen@arm.com, Daniel Vetter , Joel Fernandes , "Darren Hart \(VMware\)" , orjan.eide@arm.com, Laura Abbott , Martijn Coenen , Christian Brauner , linux-media@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Only sync the sg-list of an Ion dma-buf attachment when the attachment is actually mapped on the device. dma-bufs may be synced at any time. It can be reached from user space via DMA_BUF_IOCTL_SYNC, so there are no guarantees from callers on when syncs may be attempted, and dma_buf_end_cpu_access() and dma_buf_begin_cpu_access() may not be paired. Since the sg_list's dma_address isn't set up until the buffer is used on the device, and dma_map_sg() is called on it, the dma_address will be NULL if sync is attempted on the dma-buf before it's mapped on a device. Before v5.0 (commit 55897af63091 ("dma-direct: merge swiotlb_dma_ops into the dma_direct code")) this was a problem as the dma-api (at least the swiotlb_dma_ops on arm64) would use the potentially invalid dma_address. How that failed depended on how the device handled physical address 0. If 0 was a valid address to physical ram, that page would get flushed a lot, while the actual pages in the buffer would not get synced correctly. While if 0 is an invalid physical address it may cause a fault and trigger a crash. In v5.0 this was incidentally fixed by commit 55897af63091 ("dma-direct: merge swiotlb_dma_ops into the dma_direct code"), as this moved the dma-api to use the page pointer in the sg_list, and (for Ion buffers at least) this will always be valid if the sg_list exists at all. But, this issue is re-introduced in v5.3 with commit 449fa54d6815 ("dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device") moves the dma-api back to the old behaviour and picks the dma_address that may be invalid. dma-buf core doesn't ensure that the buffer is mapped on the device, and thus have a valid sg_list, before calling the exporter's begin_cpu_access. Signed-off-by: Ørjan Eide Acked-by: Daniel Vetter Acked-by: Christian Brauner --- drivers/staging/android/ion/ion.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) Resubmit without disclaimer, sorry about that. This seems to be part of a bigger issue where dma-buf exporters assume that their dma-buf begin_cpu_access and end_cpu_access callbacks have a certain guaranteed behavior, which isn't ensured by dma-buf core. This patch fixes this in ion only, but it also needs to be fixed for other exporters, either handled like this in each exporter, or in dma-buf core before calling into the exporters. diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c index 38b51eace4f9..7b752ba0cb6d 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -173,6 +173,7 @@ struct ion_dma_buf_attachment { struct device *dev; struct sg_table *table; struct list_head list; + bool mapped:1; }; static int ion_dma_buf_attach(struct dma_buf *dmabuf, @@ -195,6 +196,7 @@ static int ion_dma_buf_attach(struct dma_buf *dmabuf, a->table = table; a->dev = attachment->dev; INIT_LIST_HEAD(&a->list); + a->mapped = false; attachment->priv = a; @@ -231,6 +233,8 @@ static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, direction)) return ERR_PTR(-ENOMEM); + a->mapped = true; + return table; } @@ -238,6 +242,10 @@ static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment, struct sg_table *table, enum dma_data_direction direction) { + struct ion_dma_buf_attachment *a = attachment->priv; + + a->mapped = false; + dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction); } @@ -297,6 +305,8 @@ static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, mutex_lock(&buffer->lock); list_for_each_entry(a, &buffer->attachments, list) { + if (!a->mapped) + continue; dma_sync_sg_for_cpu(a->dev, a->table->sgl, a->table->nents, direction); } @@ -320,6 +330,8 @@ static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, mutex_lock(&buffer->lock); list_for_each_entry(a, &buffer->attachments, list) { + if (!a->mapped) + continue; dma_sync_sg_for_device(a->dev, a->table->sgl, a->table->nents, direction); }