Skip to contentSkip to conversation

kmap_kiobuf()

DISCUSSION [email protected] 4 participants 7 messages Jun 28, 2000 – Jun 29, 2000 Memory management

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

I think it would be useful to provide a function which can be used to obtain a virtually-contiguous VM mapping of the pages of an iobuf.

Currently, to access the pages of an iobuf, you have to kmap() each page individually. For various purposes, it would be useful to be able to kmap the whole iobuf contiguously, so that you can guarantee that:

	page_address(iobuf->maplist[n]) + PAGE_SIZE 
		== page_address(iobuf->maplist[n+1])

    (for n such that n < iobuf->nr_pages, obviously. Don't be so pedantic.)

Rather than taking a kiobuf as an argument, the new function might as well be more generic:

unsigned long kremap_pages(struct page **maplist, int nr_pages); void kunmap_pages(struct page **maplist, int nr_pages);

I had a quick look at the code for kmap() and vmalloc() and decided that even if I attempted to do it myself, I'd probably bugger it up and a MM hacker would have to fix it anyway. So I'm not going to bother.

Quoted 7 lines

T'would be useful if someone else could find the time to do so, though.

--
dwmw2

The XFS port currently has exactly this beast, there is an extension to let us pass an existing set of pages into the vmalloc_area_pages function. It uses the existing pages instead of allocating new ones. We needed something to let us map groups of pages into a single byte array.

I always knew it would go down like a ton of bricks, because of the TLB flushing costs. As soon as you have a multi-cpu box this operation gets expensive, the code could be changed to do lazy tlb flushes on unmapping the pages, but you still have the cost every time you set a mapping up.

Steve

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux.eu.org/Linux-MM/

#1 lore raw
Quoted 5 lines · [email protected] said:

I always knew it would go down like a ton of bricks, because of the
TLB flushing costs. As soon as you have a multi-cpu box this operation gets expensive, the code could be changed to do lazy tlb flushes on unmapping the pages, but you still have the cost every time you set a mapping up.

Aha - is this why kmap uses a pre-allocated set of PTEs? I got about that far before deciding I had no clue what was going on and giving up.

MM is not exactly my field - I just know I want to be able to lock down a user's buffer and treat it as if it were in kernel-space, passing its address to functions which expect kernel buffers.

--
dwmw2

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux.eu.org/Linux-MM/

#2 lore raw
Quoted 3 lines · David Woodhouse

MM is not exactly my field - I just know I want to be able to lock down a user's buffer and treat it as if it were in kernel-space, passing its address to functions which expect kernel buffers.

Then pass in a kiovec (we're planning on adding a rw_kiovec file op!) and use kmap/kmap_atomic on individual pages as required. As to providing larger kmaps, I have yet to be convinced that providing primatives for dealing with objects larger than PAGE_SIZE is a Good Idea.

-ben

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux.eu.org/Linux-MM/

#6 lore raw
In reply to [email protected]

Hi,

Quoted 4 lines · On Wed, Jun 28, 2000 at 10:54:40AM -0500, [email protected] wrote:

I always knew it would go down like a ton of bricks, because of the TLB flushing costs. As soon as you have a multi-cpu box this operation gets expensive, the code could be changed to do lazy tlb flushes on unmapping the pages, but you still have the cost every time you set a mapping up.

That's exactly what kmap() is for --- it does all the lazy tlb flushing for you. Of course, the kmap area can get fragmented so it's not a magic solution if you really need contiguous virtual mappings.

However, kmap caches the virtual mappings for you automatically, so it may well be fast enough for you that you can avoid the whole contiguous map thing and just kmap pages as you need them. Is that impossible for your code?

Cheers,
Stephen
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux.eu.org/Linux-MM/

#3 lore raw
In reply to David Woodhouse

Hi,

Quoted 4 lines · David Woodhouse

MM is not exactly my field - I just know I want to be able to lock down a user's buffer and treat it as if it were in kernel-space, passing its address to functions which expect kernel buffers.

The pinning of user buffers is part of the reason we have kiobufs. But why do you need to pass it to functions expecting kernel buffers?

--Stephen
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux.eu.org/Linux-MM/

#4 lore raw
Quoted 2 lines · Stephen C. Tweedie

The pinning of user buffers is part of the reason we have kiobufs. But why do you need to pass it to functions expecting kernel buffers?

So far I've encountered two places where I've wanted to do this.

First, in copying a packet from userspace to a PCI card, where I have to have interrupts disabled locally (spin_lock_irq()).

Currently, it does:
lock_iobuf()
foreach(page)
kmap(page) (and store the address in an array)
spin_lock_irq()
foreach(page or part thereof)
memcpy_toio() (using the virtadr returned by kmap)
spin_unlock_irq()
foreach(page)
kunmap(page)
unlock_iobuf()

The memcpy_toio() has to be split into page-sized chunks, and because we have to do the kmap from outside the spinlock, I have to keep an array of virtual addresses.

If it's really that difficult to map them contiguously into VM, I suppose it can stay the way it is - actually I can probably get away without the array of virtual addresses by discarding the return value of kmap() and using page_address() from within the spinlock, can't I?

Secondly, for the character device access to MTD devices. Almost all access to MTD devices uses kernel-space buffers. I don't really want to bloat _every_ MTD driver by making it conditionally user/kernel.

The only exception is the direct chardevice access, for which I'm currently using bounce buffers, but would like to just lock down the pages and pass a contiguously-mapped VM address instead.

Again, if it's really that much of a problem, I can work round it. It just seemed like the ideal solution, that's all.

Quoted 3 lines

For any moderately large sized kiobuf, that just means that we risk running out of kmaps. You need to treat kmaps as a scarce resource; on PAE36-configured machines we only have 512 of them right now.

I noticed that kmap ptes seem to be allocated from array of static size, which is different to the method used for vmalloc(). Why is this?

Signature · 7 lines
dwmw2


--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux.eu.org/Linux-MM/
#5 lore raw

Hi,

Quoted 9 lines · David Woodhouse
Quoted 2 lines · Stephen C. Tweedie

The pinning of user buffers is part of the reason we have kiobufs. But why do you need to pass it to functions expecting kernel buffers?

So far I've encountered two places where I've wanted to do this.

First, in copying a packet from userspace to a PCI card, where I have to have interrupts disabled locally (spin_lock_irq()).

How much data is involved?

If it's not too much, then your current scheme looks like the right way to do this. You really should try to keep things per-page and not rely on the pages being contiguous, since using the kernel's vmalloc area for contiguifying the pages will be enormously expensive on SMP. I can certainly add kiobuf support routines for kmapping and memcpy()ing kiobuf pages to the kiobuf core patches to clean the code a bit.

Quoted 4 lines

If it's really that difficult to map them contiguously into VM, I suppose it can stay the way it is - actually I can probably get away without the array of virtual addresses by discarding the return value of kmap() and using page_address() from within the spinlock, can't I?

Yes, that should work fine.

Quoted 7 lines

Secondly, for the character device access to MTD devices. Almost all access to MTD devices uses kernel-space buffers. I don't really want to bloat _every_ MTD driver by making it conditionally user/kernel.

The only exception is the direct chardevice access, for which I'm currently using bounce buffers, but would like to just lock down the pages and pass a contiguously-mapped VM address instead.

Why does it need to be *contiguous*??? The right way to code this is most definitely in terms of kiobufs. That's basically the only way we'll support user-space direct access. If I can give you a memcpy_to_kiovec() and memcpy_from_kiovec() patch, then that gives you a canonical way of representing buffers from either user or kernel space without any assumption at all that the pages are contiguous, and you get direct IO for free.

The whole point of kiobufs is to abstract away the source of the pages. You don't have to know whether the pages were originally kernel or user space.

Quoted 2 lines

I noticed that kmap ptes seem to be allocated from array of static size, which is different to the method used for vmalloc(). Why is this?

vmalloc() and kmap() are meant for completely different purposes. vmalloc() is designed for long-term persistent regions (such as loadable moules). However, it is slow. kmap() is very fast, but is designed for transient mappings of individual pages. The fixed kmap pte list is used as a ring buffer. If we kmap a page twice without wrapping, we can reuse the old virtual address of the page, so it's pretty fast to repeatedly kmap and kunmap a single page (there's a spinlock cost but not much more).

The big advantage of kmap is that we only have to do an SMP TLB IPI once every wrap of the kmap ring buffer. vmalloc incurs that cost every time.

Cheers,
Stephen
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux.eu.org/Linux-MM/