From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754506AbdCHUrq (ORCPT ); Wed, 8 Mar 2017 15:47:46 -0500 Received: from www381.your-server.de ([78.46.137.84]:45697 "EHLO www381.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754453AbdCHUra (ORCPT ); Wed, 8 Mar 2017 15:47:30 -0500 From: Lars-Peter Clausen Subject: Re: [Question] devm_kmalloc() for DMA ? To: Masahiro Yamada , Robin Murphy References: Cc: dmaengine@vger.kernel.org, linux-arm-kernel , Russell King - ARM Linux , Arnd Bergmann , Linux Kernel Mailing List , "James E.J. Bottomley" , Tejun Heo , "David S. Miller" X-Enigmail-Draft-Status: N1110 Message-ID: Date: Wed, 8 Mar 2017 20:48:31 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Authenticated-Sender: lars@metafoo.de Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03/08/2017 07:06 PM, Masahiro Yamada wrote: > Hi Robin, > > > 2017-03-08 20:15 GMT+09:00 Robin Murphy : >> On 08/03/17 10:59, Masahiro Yamada wrote: >>> Hi experts, >>> >>> I have a question about >>> how to allocate DMA-safe buffer. >>> >>> >>> In my understanding, kmalloc() returns >>> memory with DMA safe alignment >>> in order to avoid cache-sharing problem when used for DMA. >>> >>> The alignment is decided by ARCH_DMA_MINALIGN. >>> For example, on modern ARM 32bit boards, this value is typically 64. >>> So, memory returned by kmalloc() has >>> at least 64 byte alignment. >>> >>> >>> On the other hand, devm_kmalloc() does not return >>> enough-aligned memory. >> >> How so? If anything returned by kmalloc() is guaranteed to occupy some >> multiple of ARCH_DMA_MINALIGN bytes in order to avoid two allocations >> falling into the same cache line, I don't see how stealing the first 16 >> bytes *of a single allocation* could make it start sharing cache lines >> with another? :/ > > I just thought of traverse of the linked list of devres_node > on a different thread, but it should not happen. When the DMA memory is mapped for reading from the device the associated cachelines are invalidated without writeback. There is no guarantee that the changes made to the devres_node have made it to main memory yet, or is there? So those updates could be lost and you'd get a data structure corruption.