public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Roland Dreier <roland@topspin.com>
To: "David S. Miller" <davem@redhat.com>
Cc: oliver@neukum.name, wjhun@ayrnetworks.com, paulus@samba.org,
	linux-kernel@vger.kernel.org
Subject: Re: PCI DMA to small buffers on cache-incoherent arch
Date: 11 Jun 2002 11:26:05 -0700	[thread overview]
Message-ID: <52y9dl65aa.fsf@topspin.com> (raw)
In-Reply-To: <200206111007.19142.oliver@neukum.name> <20020611.011525.29963495.davem@redhat.com> <200206111406.14274.oliver@neukum.name> <20020611.050433.28184805.davem@redhat.com>

>>>>> "David" == David S Miller <davem@redhat.com> writes:

    David> Maybe on arch FOO, target X needs no alignment when using
    David> PCI controller Y, but for PCI controller Z it does need
    David> alignment.

I'm not sure I understand this objection.  kmalloc() is will return a
buffer that is safe for DMA for all controllers.  Any compile-time
static alignment of structure members would similarly do worst-case
alignment.  Also I have to admit that I don't quite understand the
issue you're raising here.  Obviously the CPU's cache line size
doesn't change based on the PCI controller.  Are you referring to PCI
device's CLS register?  I don't see how that could matter since the
bus master shouldn't ever write beyond the buffer the CPU gives it.

In any case, given drivers that have:

        struct something {
                int field1;
                char dma_buffer[SMALLER_THAN_CACHELINE];
                int field2;
        };

        struct something *dev = kmalloc(sizeof *dev, GFP_KERNEL);
        /* do DMA into dev->dma_buffer */

I know of several ways to make this safe:

1) Add a static alignment macro and change the declaration to

        struct something {
                int field1;
                char dma_buffer[SMALLER_THAN_CACHELINE] __dma_buffer;
                int field2;
        };

__dma_buffer would be a NOP on cache coherent architectures (i386,
etc) but might introduce some alignment not strictly necessary on
architectures (???)

2) Change the code to

        struct something {
                int field1;
                char *dma_buffer;
                int field2;
        };

        struct something *dev = kmalloc(sizeof *dev, GFP_KERNEL);
        dev->dma_buffer = kmalloc(SMALLER_THAN_CACHELINE, GFP_KERNEL);
        /* do DMA into dev->dma_buffer */

This always uses strictly more memory than 1) above, complicates the
code, may introduce bugs (do we know if anyone did *dev_copy = *dev
anywhere?).

3) Change the code to

        struct something {
                int field1;
                char *dma_buffer;
                int field2;
        };

        struct something *dev = kmalloc(sizeof *dev, GFP_KERNEL);
        /* find pci_device */
        dev->dma_buffer = aligned_pci_alloc(pci_device, SMALLER_THAN_CACHELINE);
        /* do DMA into dev->dma_buffer */

This may use less memory than 1) or 2) above on some architectures but
will use more than 1) on cache-coherent architectures.  It makes the
code even more complex since now the code that allocates the dma
buffer has to know which PCI device will use it (for example, in USB,
the hub driver is separated from the HCD driver, which is who knows
about the PCI bus).

4) David Woodhouse's suggestion of turning off caching for pages where
unaligned DMA is in progress.  This may be doable but seems quite
complex.  Also, drivers will probably some way of aligning their
buffers to avoid turning of caching, which means that this approach
and the above are complementary.

Best,
  Roland

  parent reply	other threads:[~2002-06-11 18:26 UTC|newest]

Thread overview: 99+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-08 20:38 PCI DMA to small buffers on cache-incoherent arch Roland Dreier
2002-06-08 13:58 ` Anton Blanchard
2002-06-09  0:52   ` Roland Dreier
2002-06-08 23:03 ` David S. Miller
2002-06-09  0:40   ` Roland Dreier
2002-06-09  0:53     ` David S. Miller
2002-06-09  1:26       ` Roland Dreier
2002-06-09  5:29         ` David S. Miller
2002-06-09  6:16           ` Roland Dreier
2002-06-10 16:03             ` Roland Dreier
2002-06-11 14:04               ` David Woodhouse
2002-06-09  6:45           ` Oliver Neukum
2002-06-10  4:24             ` David S. Miller
2002-06-10 10:00               ` Oliver Neukum
2002-06-10 10:24                 ` David S. Miller
2002-06-09  9:51           ` Paul Mackerras
2002-06-09 10:54             ` Benjamin Herrenschmidt
2002-06-10  4:27             ` David S. Miller
2002-06-10 15:59               ` Roland Dreier
2002-06-10 17:03                 ` Tom Rini
2002-06-10 17:22                   ` Oliver Neukum
2002-06-10 17:29                     ` Tom Rini
2002-06-10 17:39                       ` Oliver Neukum
2002-06-10 19:03                       ` Roland Dreier
2002-06-10 19:14                         ` Tom Rini
2002-06-10 19:21                           ` Roland Dreier
2002-06-10 19:26                             ` Tom Rini
2002-06-10 17:57                     ` Russell King
2002-06-10 17:28                   ` Roland Dreier
2002-06-10 18:07               ` William Jhun
2002-06-10 18:29                 ` William Jhun
2002-06-10 18:33                 ` Mark Zealey
2002-06-10 18:44                   ` Oliver Neukum
2002-06-11  3:10                 ` David S. Miller
2002-06-11  4:04                   ` Roland Dreier
2002-06-11  4:16                     ` Brad Hards
2002-06-11  4:24                       ` Roland Dreier
2002-06-11  4:24                         ` David S. Miller
2002-06-11  4:21                     ` David S. Miller
2002-06-11  4:39                       ` Roland Dreier
2002-06-11  4:38                         ` David S. Miller
2002-06-11  6:23                           ` Oliver Neukum
2002-06-11  6:38                             ` David S. Miller
2002-06-11  7:38                               ` Oliver Neukum
2002-06-11  7:36                                 ` David S. Miller
2002-06-11  7:43                                   ` David S. Miller
2002-06-11  8:07                                     ` Oliver Neukum
2002-06-11  8:15                                       ` David S. Miller
2002-06-11 12:06                                         ` Oliver Neukum
2002-06-11 12:04                                           ` David S. Miller
2002-06-11 14:23                                             ` Oliver Neukum
2002-06-14  4:14                                               ` David S. Miller
2002-06-11 18:26                                             ` Roland Dreier [this message]
2002-06-11 17:29                                               ` Benjamin Herrenschmidt
2002-06-12 12:02                                                 ` Oliver Neukum
2002-06-11 20:06                                                   ` Benjamin Herrenschmidt
2002-06-12 12:02                                                   ` David S. Miller
2002-06-11 23:00                                               ` Thunder from the hill
2002-06-11 23:56                                                 ` Roland Dreier
2002-06-12  0:09                                                   ` Thunder from the hill
2002-06-11 15:57                                     ` William Jhun
2002-06-12  9:06                                     ` Pavel Machek
2002-06-12 20:16                                       ` David S. Miller
2002-06-12 11:47                 ` David S. Miller
2002-06-12 12:08                   ` Oliver Neukum
2002-06-14  4:41                     ` David S. Miller
2002-06-12 16:09                   ` William Jhun
2002-06-09  1:30   ` Albert D. Cahalan
2002-06-09  5:29     ` David S. Miller
2002-06-09  6:33       ` Albert D. Cahalan
2002-06-09  6:50         ` Oliver Neukum
2002-06-09  6:57           ` Albert D. Cahalan
2002-06-09  7:15             ` Oliver Neukum
2002-06-09  8:48         ` Russell King
2002-06-09 15:42           ` Albert D. Cahalan
2002-06-09 23:26           ` Oliver Neukum
  -- strict thread matches above, loose matches on Subject: below --
2002-06-11  5:31 David Brownell
2002-06-11  5:44 ` David S. Miller
2002-06-11 15:12   ` David Brownell
2002-06-11 15:44     ` Oliver Neukum
2002-06-12  3:25     ` David S. Miller
2002-06-11 17:33       ` Benjamin Herrenschmidt
2002-06-12  9:42         ` David S. Miller
2002-06-12 14:14           ` David Brownell
2002-06-12 15:00             ` Benjamin Herrenschmidt
2002-06-12 18:44             ` Roland Dreier
2002-06-12 19:13               ` David Brownell
2002-06-12 19:58                 ` Oliver Neukum
2002-06-12 22:51                   ` David S. Miller
2002-06-12 23:17                     ` Oliver Neukum
2002-06-13  4:57                   ` David Brownell
2002-06-12 22:46                 ` David S. Miller
2002-06-13  5:13                   ` David Brownell
2002-06-13  9:38                     ` David S. Miller
2002-06-12 22:50                 ` David S. Miller
2002-06-12  6:25       ` David Brownell
2002-06-12  6:24         ` David S. Miller
2002-06-12  7:06           ` David Brownell
2002-06-12  9:22             ` David S. Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=52y9dl65aa.fsf@topspin.com \
    --to=roland@topspin.com \
    --cc=davem@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oliver@neukum.name \
    --cc=paulus@samba.org \
    --cc=wjhun@ayrnetworks.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox