Skip to contentSkip to conversation

generic device DMA (dma_pool update)

PATCH Adam J. Richter 4 participants 6 messages Jan 2, 2003 – Jan 3, 2003

lore
Share & tools
lore
#0 lore raw
Quoted 2 lines

Note that this "add gfp flags to dma_alloc_coherent()" issue is a tangent to the dma_pool topic ... it's a different "generic device DMA" issue.

I think we understand each other, but let's walk through
the three level of it: mempool_alloc, {dma,pci}_pool_alloc, and {dma,pci}_alloc_consistent.

We agree that mempool_alloc should take gfp_flags so that
it can fail rather than block on optional IO such as read-aheads.

Whether pci_pool_alloc (which does not guarantee a reserve of
memory) should continue to take gfp_flags is not something we were discussing, but is an interesting question for the future. I see that pci_pool_alloc is only called with GFP_ATOMIC in two places:

uhci_alloc_td in drivers/usb/host/uhci-hcd.c alloc_safe_buffer in arch/arm/mach-sa1100/sa1111-pcibuf.c

The use in uhci_alloc_td means that USB IO can fail under
memory load. Conceivably, swapping could even deadlock this way. uhci_alloc_td should be going through a mempool which individual drivers should expand and shrink as they are added and removed so that transfers that are sent with GFP_KERNEL will be guaranteed not to fail for lack of memory (and gfp_flags should be passed down so that it will be possible to abort transfers like read-aheads rather than blocking, but that's not essential).

The pci_pool_alloc in sa1111-buf.c is more interesting.
alloc_safe_buffer is used to implement an unusual version of pci_map_single. It is unclear to me whether this approach is optimal. I'll look into this more.

Anyhow, the question that we actually were talking about is
whether dma_alloc_consistent should take gfp_flags. So far, the only potential example that I'm aware of is that the question of whether the call to pci_pool_alloc in sa1111-pcibuf.c needs.to call down to {pci,dma}_alloc_consistent with GFP_ATOMIC. It's something that I just noticed and will look into.

Quoted 2 lines

We already have the pci_pool allocator that knows how to cope (timeout and retry) with the awkward semantics of pci_alloc_consistent() ...

pci_pool_alloc currently implments mempool's GFP_KERNEL
semantics (block indefinitely, never fail), but, unlike mempool, it does not guarantee a minimum memory allocation, so there is no way to guarantee that it will ever return. It can hang if there is insufficient memory. It looks like you cannot even control-C out of it.

By the way, since you call pci_alloc_consistent's GFP_KERNEL
semantics "awkward", are you saying that you think it should wait indefinitely for memory to become available?

Quoted 4 lines

likewise,
we can tell that those semantics are problematic, both because they need that kind of workaround and because they complicate reusing "better" allocator code (like mm/slab.c) on top of the DMA page allocators.

Maybe it's irrelevant, but I don't understand what you mean by
"better" here.

Quoted 8 lines
Quoted 4 lines

Let me clarify or revise my request. By "show me or invent an
example" I mean describe a case where this would be used, as in specific hardware devices that Linux has trouble supporting right now, or specific programs that can't be run efficiently under Linux, etc.

That doesn't really strike me as a reasonable revision, since that wasn't an issue an improved dma_alloc_coherent() syntax was intended to address directly.

It is always reasonable to ask for an example of where a
requested change would be used, as in specific hardware devices that Linux has trouble supporting right now, or specific programs that can't be run efficiently under Linux, or some other real external benefit, because that is the purpose of software. This applies to every byte of code in Linux.

Don't start by positing some situation in the middle of a
call graph. Instead, I'm asking you to show an example starting at the top of the call graph. What *actual* use will cause this parameter to be useful in reality? By what external metric will users benefit?

I'll look into the sa1111-pcibuf.c case. That's the only
potential example that I've seen so far.

Quoted 4 lines

To the extent that it's reasonable, you should also be considering this corresponding issue: ways that removing gfp_flags from the corresponding generic memory allocator, __get_free_pages(), would be improving those characteristics of Linux. (IMO, it wouldn't.)

There are other users of __get_free_pages. If it turns out
that there is no true use for gfp_mask in dma_alloc_consistent then it might be worth exploring in the future, but we don't have to decide to eliminate gfp_mask from __get_free_pages just to decide not to add it to dma_alloc_consistent.

Quoted 6 lines
Quoted 5 lines

I have trouble understanding why, for example, a USB hard
disk driver would want anything more than a fixed size pool of transfer descriptors. At some point you know that you've queued enough IO so that the driver can be confident that it will be called again before that queue is completely emptied.

Quoted 8 lines

For better or worse, that's not how it works today and it's not likely to change in the 2.5 kernels. "Transfer Descriptors" are resources inside host controller drivers (bus drivers), allocated dynamically (GFP_KERNEL in many cases, normally using a pci_pool) when USB device drivers (like usb-storage) submit requests (URBs).

They are invisible to usb device drivers, except implicitly as another reason that submitting an urb might trigger -ENOMEM.

It might not be trivial to fix that but it would be
straightforward to do so. In the future, submit_urb(...GFP_KERNEL) should be guaranteed never to fail with -ENOMEM (see my comments about uhci_alloc_td above on how to fix this).

This existing straighforwardly fixable bug does not justify
changing the DMA API just to encrust it further. Users will be better off if we fix the bug (it will be able to swap reliably on USB disks, or at least they'll be closer to it if there are other bugs in the way).

In practice, I think that if we just added one, maybe two,
URB's by default for every endpoint when a device is added, that that would be enough to guarantee that would reduce the number of drivers that needed to reserve more URB's than that to few or none.

Quoted 3 lines

And for that matter, the usb-storage driver doesn't "fire and forget" as you described; that'd be a network driver model. The storage driver needs to block till its request completes.

That only makes it easier to calculate the number of
URB's that need to be guaranteed to be available.

By the way, as far as I can tell, usb_stor_msg_common in
drivers/usb/storage/transport.c only waits for the urb to be transferred, not for the operation to complete.

Quoted 9 lines
Quoted 5 lines

Also, your use of the term GFP_KERNEL is potentially
ambiguous. In some cases GFP_KERNEL seems to mean "wait indifinitely until memory is available; never fail." In other cases it means "perhaps wait a second or two if memory is unavailable, but fail if memory is not available by then."

Hmm, I was unaware that anyone expected GFP_KERNEL (or rather, __GFP_WAIT) to guarantee that memory was always returned. It's not called __GFP_NEVERFAIL, after all.

mempool_alloc does. That's the point of it. You calculate
how many objects you need in order to guarantee no deadlocks and reserve that number in advance (the initial reservation can fail).

Quoted 2 lines

I've always seen such fault returns documented as unrelated to allocator parameters ...

By design, mempool_alloc(GFP_KERNEL) never fails (although
mempool_alloc(GFP_ATOMIC) can).

By the way, pci_pool_alloc(GFP_KERNEL) also never returns
failure, but I consider that a bug as I explained above.

Quoted 2 lines

and treated callers that break on fault
returns as buggy, regardless of GFP_* parameters in use.

It is not a bug for mempool_alloc(GFP_KERNEL) callers to assume
success upon return. It is a wasete of kernel footprint for them to check for failure.

Quoted 4 lines

A
random sample of kernel docs agrees with me on that: both in Documentation/* (like the i2c stuff) or the O'Reilly book on device drivers.

mempool really needs a file in Documentation/. The only reference
to it there is in linux-2.5.54/Documentation/block/biodoc.txt, line 663 ff:

| This makes use of Ingo Molnar's mempool implementation, which enables
| subsystems like bio to maintain their own reserve memory pools for guaranteed
| deadlock-free allocations during extreme VM load. [...]
Adam J. Richter     __     ______________   575 Oroville Road
[email protected]     \ /                  Milpitas, California 95035
+1 408 309-6081         | g g d r a s i l   United States of America

"Free Software For The Rest Of Us."

#1 lore raw
Quoted 4 lines · Adam J. Richter

The pci_pool_alloc in sa1111-buf.c is more interesting.
alloc_safe_buffer is used to implement an unusual version of pci_map_single. It is unclear to me whether this approach is optimal. I'll look into this more.

Welcome to the world of seriously broken hardware that a fair number of people put on their boards. Unfortunately, the hardware bug got marged as "never fix" by Intel.

Basically, the chip is only able to DMA from certain memory regions and its RAM size dependent. For 32MB of memory, it works out at around 1MB regions - odd regions are not able to perform DMA, even regions can.

Linux only supports one DMA region per memory node though, so in this configuration, we only have 1MB of memory able to be used for OHCI.

To work around this, we have "unusual" versions of the mapping functions. Although they may not be optimal, they are, afaik completely functional.

Signature · 2 lines
Russell King ([email protected])                The developer of ARM Linux
             http://www.arm.linux.org.uk/personal/aboutme.html
#2 lore raw
In reply to Adam J. Richter
Quoted 4 lines · Adam J. Richter

The pci_pool_alloc in sa1111-buf.c is more interesting.
alloc_safe_buffer is used to implement an unusual version of pci_map_single. It is unclear to me whether this approach is optimal. I'll look into this more.

Darn if I didn't mention SA1111 up front! Here's where avoiding mapping APIs is a win, in favor of allocating dma buffers that will work from the get go. (That is, dma_addr_t can usefully push up the call stack, while gfp_flags pushes down.)

USB device drivers like "hid" and "usblp" do that already; later, more drivers could convert.

Of course, the ohci-sa1111.c codepaths are also relevant here. They use pci_pools for TD allocation. The fact that they use SLAB_ATOMIC (you missed one!), and once didn't, bothers me; luckily that became an easy fix a few months back.

And the dma_pool_patch I posted would make all this code morph to using the slab allocator underneath. The slab code would be smarter about managing it than pci_pool, so it'd be better at sharing that 1 MByte of DMA memory with other devices.

- Dave

#3 lore raw
In reply to Adam J. Richter
Quoted 10 lines · Adam J. Richter
Quoted 2 lines

Note that this "add gfp flags to dma_alloc_coherent()" issue is a tangent to the dma_pool topic ... it's a different "generic device DMA" issue.

I think we understand each other, but let's walk through
the three level of it: mempool_alloc, {dma,pci}_pool_alloc, and {dma,pci}_alloc_consistent.

We agree that mempool_alloc should take gfp_flags so that
it can fail rather than block on optional IO such as read-aheads.

I can imagine code using mempool to implement the __GFP_HIGH "use emergency pools" behavior. In the case of USB that'd mean more or less that GFP_ATOMIC allocations, and many usb-storage paths, might want to initialize such emergency pools ... freeing into them, or allocating from them when __GFP_HIGH is specified.

Quoted 7 lines

Whether pci_pool_alloc (which does not guarantee a reserve of
memory) should continue to take gfp_flags is not something we were discussing, but is an interesting question for the future. I see that pci_pool_alloc is only called with GFP_ATOMIC in two places:

	uhci_alloc_td in drivers/usb/host/uhci-hcd.c
	alloc_safe_buffer in arch/arm/mach-sa1100/sa1111-pcibuf.c

And SLAB_* elsewhere, per spec ... :) There are other places it gets called, even with SLAB_ATOMIC. Plus, things like HID and usblp buffer alloc will be passing down SLAB_KERNEL ... without __GFP_HIGH, so the allocator clearly shouldn't tap emergency reserves then.

The gfp_flags parameter is a good example of something that shouldn't be discarded except when it's unavoidable. (Like "I have to hold that spinlock during this allocation" forcing GFP_ATOMIC.)

Quoted 2 lines

The use in uhci_alloc_td means that USB IO can fail under
memory load. Conceivably, swapping could even deadlock this way.

That's not new, or just because of UHCI. There's per-request allocation happening, and the APIs don't support pre-allocation of most anything. Maybe 2.7 should change that.

The LK 2.5 USB calls (not 2.4) will pass gfp_flags down from callers, with the net effect that many allocations respect __GFP_HIGH, and maybe even __GFP_WAIT. Clearly uhci-hcd doesn't; ehci-hcd does, and ohci-hcd could, with a small patch (longstanding minor peeve).

The problem is that at the next layer down, when pci_pool runs out of its preallocated pages, it's got to talk to pci_alloc_consistent().

THAT forces pci_pool to discard gfp_flags; the lower levels are stuck using GFP_ATOMIC. There lies potential lossage: it enables emergency allocations (on x86, this can fragment a zone, and it'd stay that way since pci_pool won't free pages) when it'd be better to just block for a moment, or maybe even just fail. Portably supporting graceful degradation just isn't in the cards.

Which is why it'd be better if dma_alloc_coherent() took gfp_flags. That way any allocator layered on top of it won't do dumb things like that, when the whole stack on top of it is saying "don't!!".

Quoted 3 lines

By the way, since you call pci_alloc_consistent's GFP_KERNEL
semantics "awkward", are you saying that you think it should wait indefinitely for memory to become available?

You mean change its semantics so it can't be used in non-sleeping contexts any more? No. I didn't like James' notion to change them so this call _had_ to be used in non-sleeping contexts, either.

The point is that dma_alloc_coherent() shouldn't be making the same mistake of _precluding_ implementations from implementing __GFP_WAIT, which might be appropriate, or distinguishing normal allocations from those that are __GFP_HIGH priority. (And so on.)

The "awkward" aspect is that pci_alloc_consistent() implementations may only use GFP_ATOMIC, regardless of whether the caller has better knowledge of the true restrictions.

Quoted 6 lines

It is always reasonable to ask for an example of where a
requested change would be used, as in specific hardware devices that Linux has trouble supporting right now, or specific programs that can't be run efficiently under Linux, or some other real external benefit, because that is the purpose of software. This applies to every byte of code in Linux.

Then there are basic characteristics that don't work well when taken out of context. The context here is that higher level code is telling lower levels some important things in gfp_flags, but there's this darn lossy API in between, discarding that information.

Sort of like thinking the shortest difference between two cities in California involves McLean, Virginia: that makes no sense.

So: this is something that a shiny new API introduced a week or so ago can/should fix ASAP (IMO). There's no impact on an installed base, and implementations can reasonably ignore all the gfp_flags that are really quality-of-service hints ... they have ignored such information so far, but future code could be smarter.

- Dave

#4 lore raw
In reply to Adam J. Richter
Quoted 4 lines · Adam J. Richter

In practice, I think that if we just added one, maybe two,
URB's by default for every endpoint when a device is added, that that would be enough to guarantee that would reduce the number of drivers that needed to reserve more URB's than that to few or none.

I seem to recall someone posted a patch to make non-iso URB allocation use a mempool.

Quoted 8 lines
Quoted 3 lines

Hmm, I was unaware that anyone expected GFP_KERNEL (or rather, __GFP_WAIT) to guarantee that memory was always returned. It's not called __GFP_NEVERFAIL, after all.

mempool_alloc does. That's the point of it. You calculate
how many objects you need in order to guarantee no deadlocks and reserve that number in advance (the initial reservation can fail).

To rephrase that so it illustrates my point: the whole reason to use mempool is to try adding __GFP_NEVERFAIL when __GFP_WAIT is given ... because __GFP_WAIT doesn't otherwise mean NEVERFAIL.

- Dave

#5 lore raw
Quoted 3 lines · Adam J. Richter
Quoted 3 lines

mempool_alloc does. That's the point of it. You calculate
how many objects you need in order to guarantee no deadlocks and reserve that number in advance (the initial reservation can fail).

Quoted 3 lines · David Brownell

To rephrase that so it illustrates my point: the whole reason to use mempool is to try adding __GFP_NEVERFAIL when __GFP_WAIT is given ... because __GFP_WAIT doesn't otherwise mean NEVERFAIL.

Well, it's not quite that general. There is a constraint of the objects allocated with a mempool having a finite lifetime. And non-waiting mempool allocations are also available, but may fail.

So long as the queueing is fair everyone eventually gets their turn.

Bill