Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1595056

[Question] devm_kmalloc() for DMA ?

Path csiph.com!weretis.net!feeder4.news.weretis.net!news.mixmin.net!aioe.org!bofh.it!news.nic.it!robomod
From Masahiro Yamada <yamada.masahiro@socionext.com>
Newsgroups linux.kernel
Subject [Question] devm_kmalloc() for DMA ?
Date Wed, 08 Mar 2017 12:10:02 +0100
Message-ID <tiHDI-5Js-13@gated-at.bofh.it> (permalink)
X-Original-To dmaengine@vger.kernel.org, linux-arm-kernel <linux-arm-kernel@lists.infradead.org>
Dkim-Filter OpenDKIM Filter v2.10.3 conssluserg-05.nifty.com v28Axo6X026919
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1488970790; bh=sB0f/71WsZSF48sogT2p+ulDc7UDnsHOFk5jtXaJbHc=; h=From:Date:Subject:To:Cc:From; b=g5hVV8eA0sKNVKYN3EWIlHGnhrM+0yzoBVvUZcbVYLNx2wxp/YKgTMLSl46arpYyp JhRZkUEqM34ea+HqbmcUwJizfvAJeIakYPvc9rBVAQQLcA5OJNwnKfaBNVnsHS+I7f TT7585UNPmnDFusdAzCm5PoaduaI20yR9mNschToVt99AEP3V3u7p6p5W6+FotdGMo a9oNhtuX5ULj5bon3ros4cSUcMvWF719RHB3Kvcs1l/1IaikREph5YKgj5bTd/aAmJ tyhTllCS1uJKKO/wN56+nvUWdXqF6SGKpdgA+oaKIVxCrnnJNp699kYvkzztHjUUQK LryYIsFYbrG5Q==
X-Nifty-Srcip [209.85.220.173]
X-Gm-Message-State AMke39lKolXtLx5kG11+vjm8psBSQoKXu6IJZ+4Dm+ZxsAth+bvUJqK9KVIJ7tUowUHcJ6I2KEr4YbnEGosuJw==
X-Received by 10.129.75.88 with SMTP id y85mr1359025ywa.86.1488970789301; Wed, 08 Mar 2017 02:59:49 -0800 (PST)
MIME-Version 1.0
X-Gmail-Original-Message-ID <CAK7LNARbD262isY7yOgQKAcbWQHV+3dsoRjtu6S+qUpPr98qSQ@mail.gmail.com>
Content-Type text/plain; charset=UTF-8
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 85
Organization linux.* mail to news gateway
X-Original-Cc Russell King - ARM Linux <linux@arm.linux.org.uk>, Arnd Bergmann <arnd@arndb.de>, Linux Kernel Mailing List <linux-kernel@vger.kernel.org>, Tejun Heo <tj@kernel.org>, "James E.J. Bottomley" <James.Bottomley@hansenpartnership.com>, "David S. Miller" <davem@davemloft.net>, masahiroy@kernel.org
X-Original-Date Wed, 8 Mar 2017 19:59:48 +0900
X-Original-Message-ID <CAK7LNARbD262isY7yOgQKAcbWQHV+3dsoRjtu6S+qUpPr98qSQ@mail.gmail.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1595056

Show key headers only | View raw


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.


On my board (ARM 32bit), devm_kmalloc() returns
(ARCH_DMA_MINALIGN aligned address) + 0x10.



The reason of the offset 0x10 is obvious.

struct devres {
        struct devres_node node;
        /* -- 3 pointers */
        unsigned long long data[]; /* guarantee ull alignment */
};


Management data is located at the top of struct devres.
Then, devm_kmalloc() returns dr->data.

The "unsigned long long" guarantees
the returned memory has 0x10 alignment,
but I think this may not be enough for DMA.

I noticed this when I was seeing drivers/mtd/nand/denali.c

The code looks as follows:


        denali->buf.buf = devm_kzalloc(denali->dev,
                             mtd->writesize + mtd->oobsize,
                             GFP_KERNEL);
        if (!denali->buf.buf) {
                ret = -ENOMEM;
                goto failed_req_irq;
        }

        /* Is 32-bit DMA supported? */
        ret = dma_set_mask(denali->dev, DMA_BIT_MASK(32));
        if (ret) {
                dev_err(denali->dev, "No usable DMA configuration\n");
                goto failed_req_irq;
        }

        denali->buf.dma_buf = dma_map_single(denali->dev, denali->buf.buf,
                             mtd->writesize + mtd->oobsize,
                             DMA_BIDIRECTIONAL);




Memory buffer is allocated by devm_kzalloc(), then
passed to dma_map_single().



Could this be a potential problem in general?

Is devm_kmalloc() not recommended
for buffer that can be DMA-mapped?


Any advice is appreciated.


-- 
Best Regards
Masahiro Yamada

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[Question] devm_kmalloc() for DMA ? Masahiro Yamada <yamada.masahiro@socionext.com> - 2017-03-08 12:10 +0100
  Re: [Question] devm_kmalloc() for DMA ? Robin Murphy <robin.murphy@arm.com> - 2017-03-08 12:20 +0100
    Re: [Question] devm_kmalloc() for DMA ? Masahiro Yamada <yamada.masahiro@socionext.com> - 2017-03-08 19:10 +0100
      Re: [Question] devm_kmalloc() for DMA ? Russell King - ARM Linux <linux@armlinux.org.uk> - 2017-03-08 21:20 +0100
        Re: [Question] devm_kmalloc() for DMA ? Lars-Peter Clausen <lars@metafoo.de> - 2017-03-08 21:50 +0100
          Re: [Question] devm_kmalloc() for DMA ? Russell King - ARM Linux <linux@armlinux.org.uk> - 2017-03-08 22:30 +0100
            Re: [Question] devm_kmalloc() for DMA ? Lars-Peter Clausen <lars@metafoo.de> - 2017-03-09 00:10 +0100
              Re: [Question] devm_kmalloc() for DMA ? Masahiro Yamada <yamada.masahiro@socionext.com> - 2017-03-09 04:30 +0100
                Re: [Question] devm_kmalloc() for DMA ? Russell King - ARM Linux <linux@armlinux.org.uk> - 2017-03-09 10:30 +0100
      Re: [Question] devm_kmalloc() for DMA ? Lars-Peter Clausen <lars@metafoo.de> - 2017-03-08 21:50 +0100
      Re: [Question] devm_kmalloc() for DMA ? Robin Murphy <robin.murphy@arm.com> - 2017-03-09 12:30 +0100

csiph-web