Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp5634786iog; Thu, 23 Jun 2022 02:09:36 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sffFmjwUiI95FjexAIB2OQPoj5JOk69rOKvuacczNeWBP5e+ezfc0NDloXbQ6OUomcBhtW X-Received: by 2002:a05:6402:268a:b0:435:c137:6452 with SMTP id w10-20020a056402268a00b00435c1376452mr4709640edd.419.1655975375869; Thu, 23 Jun 2022 02:09:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655975375; cv=none; d=google.com; s=arc-20160816; b=qckV0c6JdGBn4YK5BDmXNrEi7UPq+p+u9nTNnGgrhN5plXNMZFRPCOnYJYUhnfC6mj Vp2nFYcRTIEqXz3otgLH9ilnXAw5Jxa11vbISprwBrrbo/PF2K/sFgdqM3pwwuMze9AP EMEug0VRWbxQ8xa76iiAQnJUiqwlNnmKBVhjyk0XvEj1IPzeIUKtYCgK9KNY707klmhU epbVVYjFykti+3phEeekJn7VArAvOya6ocatzBN6hes1IpSD00Gj8Kv7MuJF1dJH/2do bozPGfKUTH1helDpakHw4djQs6LJalOEjEcWrP4zsS7aheXzUBqByp0jWpi6sG6b7GWy g6EQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=sCvhHjtMIl+GjRTFcw56i1SWGEFd3dolMopea/R9ugs=; b=irQqtSSw2sP37JVvcqcEGWg2XhF+RqEPxbYXCs66m00HOa6xWF+mJnNzAFbKvcPK4/ 5Nx4ON70sKFTO1Nm2Ow4297MqGxaP5ad9TdaQWc/2MdeFxzhNPO4KM556h+vfCkiYLIs DEeCT+OIzZZ4zzdYJhthvhDeY6Tgd57njKfUWQKEtuxo0YspR8MQKV0XoXo50e3Hl48P NAhRt4NlfFBb5o9K50mNHnUSF/LcaovlHyd4/nldPiYiQDgrFUcOd0q4xETAkCnYYVS5 jI4i7gtWJv5JVJwEYSZ2wEBHvPMMyQoBf2GCFGcLQwS9CdM7gOk8Um8OQQr+L1ky2YWx mFlQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g20-20020a50ee14000000b00435824dcdedsi11540587eds.283.2022.06.23.02.09.07; Thu, 23 Jun 2022 02:09:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230380AbiFWI6e (ORCPT + 99 others); Thu, 23 Jun 2022 04:58:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbiFWI6d (ORCPT ); Thu, 23 Jun 2022 04:58:33 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 219B222522 for ; Thu, 23 Jun 2022 01:58:31 -0700 (PDT) Received: from gallifrey.ext.pengutronix.de ([2001:67c:670:201:5054:ff:fe8d:eefb] helo=[IPv6:::1]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o4IfT-0001Zu-Cc; Thu, 23 Jun 2022 10:58:27 +0200 Message-ID: Subject: Re: DMA-buf and uncached system memory From: Lucas Stach To: Christian =?ISO-8859-1?Q?K=F6nig?= , Pekka Paalanen Cc: "Sharma, Shashank" , lkml , dri-devel , Nicolas Dufresne , linaro-mm-sig@lists.linaro.org, Sumit Semwal , linux-media Date: Thu, 23 Jun 2022 10:58:26 +0200 In-Reply-To: References: <91ff0bbb-ea3a-2663-3453-dea96ccd6dd8@amd.com> <9178e19f5c0e141772b61b759abaa0d176f902b6.camel@ndufresne.ca> <20220623101326.18beeab3@eldfell> <954d0a9b-29ef-52ef-f6ca-22d7e6aa3f4d@amd.com> <4b69f9f542d6efde2190b73c87096e87fa24d8ef.camel@pengutronix.de> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-1.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2001:67c:670:201:5054:ff:fe8d:eefb X-SA-Exim-Mail-From: l.stach@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Am Donnerstag, dem 23.06.2022 um 10:14 +0200 schrieb Christian König: > Am 23.06.22 um 10:04 schrieb Lucas Stach: > > Am Donnerstag, dem 23.06.2022 um 09:26 +0200 schrieb Christian König: > > > Am 23.06.22 um 09:13 schrieb Pekka Paalanen: > > > > On Thu, 23 Jun 2022 08:59:41 +0200 > > > > Christian König wrote: > > > > > > > > > The exporter isn't doing anything wrong here. DMA-buf are supposed to be > > > > > CPU cached and can also be cache hot. > > > > Hi, > > > > > > > > what is that statement based on? > > > On the design documentation of DMA-buf and the actual driver > > > implementations. > > > > > > Coherency and snooping of the CPU cache is mandatory for devices and > > > root complexes in the PCI specification. Incoherent access is just an > > > extension. > > > > > > We inherited that by basing DMA-buf on the Linux kernel DMA-API which in > > > turn is largely based on the PCI specification. > > > > > > > Were the (mandatory for CPU access) cpu_access_begin/end functions & > > > > ioctls not supposed to ensure that CPU cache is up-to-date / CPU cache > > > > is fully flushed out? > > > No, those functions are to inform the exporter that the importer has > > > started and finished accessing the buffer using the CPU. > > > > > > There is no signaling in the other direction. In other words the > > > exporter doesn't inform the importer about CPU accesses because it is > > > the owner of the buffer. > > > > > > It's the responsibility of the importer to make sure that it can > > > actually access the data in the buffer. If it can't guarantee that the > > > importer shouldn't import the buffer in the first place. > > This is not really correct. DMA-buf inherited the the map/unmap part > > from the DMA API, which on cache coherent architecture is mostly a no- > > op or ties into the IOMMU implementation to set up the pagetables for > > the translation. On non cache coherent architectures this is the point > > where any any necessary cache maintenance happens. DRM breaks this > > model by caching the DMA-buf mapping for performance reasons. > > That's not only because of performance reasons, but also because of > correctness. > > At least the Vulkan API and a bunch of OpenGL extensions make it > mandatory for the buffer to be cache coherent. The kernel is simply not > informed about domain transfers. > > For example you can just do a CPU copy to a ring buffer and the > expectation is that an already running shader sees that. Yes, that one is not really an issue as you know that at buffer creation time and can make sure to map those buffers uncached on non coherent arches. If there are no explicit domain transfer points non coherent must bite the bullet and bypass the CPU caches, running performance into the ground. > > > In the DMA API keeping things mapped is also a valid use-case, but then > > you need to do explicit domain transfers via the dma_sync_* family, > > which DMA-buf has not inherited. Again those sync are no-ops on cache > > coherent architectures, but do any necessary cache maintenance on non > > coherent arches. > > Correct, yes. Coherency is mandatory for DMA-buf, you can't use > dma_sync_* on it when you are the importer. > > The exporter could of course make use of that because he is the owner of > the buffer. In the example given here with UVC video, you don't know that the buffer will be exported and needs to be coherent without synchronization points, due to the mapping cache at the DRM side. So V4L2 naturally allocates the buffers from CPU cached memory. If the expectation is that those buffers are device coherent without relying on the map/unmap_attachment calls, then V4L2 needs to always synchronize caches on DQBUF when the buffer is allocated from CPU cached memory and a single DMA-buf attachment exists. And while writing this I realize that this is probably exactly what V4L2 should do... Regards, Lucas