Problems with raw interface.
Subsystems (2)
The problem seems to be with the flush_dcache_page () routine in asm-parisc/pgalloc.h. The routine does not handle the case when page->mapping == NULL. It simply ends up calling __flush_dcache_page () which only flushes out the cache lines corresponding to the kernel VA.
The cache lines corresponding to the user VA remain unaffected. In order for
the user cache lines to be flushed a call to flush_cache_page () or flush_user_dcache_range ()
must be made.
In order to do this the flush_dcache_page () interface may need to be modified to take in a user VA as an argument.
If this is not feasible we could as a hack check for page->mapping == NULL in flush_dcache_page () and call flush_data_cache(). This would be very expensive.
Also, does'nt __flush_dcache_page () need to have a for loop for "i_mmap" similar to the one for "i_mmap_shared" ?
thanks,
santosh.
Quoted 3 lines
Also, does'nt
__flush_dcache_page() need to have a for loop for "i_mmap" similar to the one for "i_mmap_shared" ?
no, this was discussed recently on this list.
see jejb's message:
http://lists.parisc-linux.org/pipermail/parisc-linux/2003-August/020791.html
and the corresponding thread for more info.
i thought flush_dcache_page is for making kernel mapping visible to user, if you are trying to make user data visible to the kernel, you should not be relying on flush_dcache_page.
randolph
Signature · 3 lines
Randolph Chung Debian GNU/Linux Developer, hppa/ia64 ports http://www.tausq.org/
hmm.. why is map_user_kiobuf () calling flush_dcache_page () then ? should it not be calling flush_cache_{range,page} ?
map_user_kiobuf () called from the raw I/O path should ,in the write case, be flushing user data out, so that its visible to the kernel VA.
This is not being done in flush_dcache_page () when page->mapping is NULL.
-----Original Message-----
From: Randolph Chung [mailto:[email protected]]
Sent: Thursday, September 25, 2003 4:20 PM
To: [email protected]
Cc: [email protected]
Subject: Re: [parisc-linux] Fwd: Problems with raw interface.
Quoted 3 lines
Also, does'nt
__flush_dcache_page() need to have a for loop for "i_mmap" similar to the one for "i_mmap_shared" ?
no, this was discussed recently on this list.
see jejb's message:
http://lists.parisc-linux.org/pipermail/parisc-linux/2003-August/020791.html
and the corresponding thread for more info.
i thought flush_dcache_page is for making kernel mapping visible to user, if you are trying to make user data visible to the kernel, you should not be relying on flush_dcache_page.
randolph
--
Randolph Chung
Debian GNU/Linux Developer, hppa/ia64 ports
http://www.tausq.org/
Quoted 7 lines · Santosh Abraham
hmm.. why is
map_user_kiobuf() callingflush_dcache_page() then ? should it not be calling flush_cache_{range,page} ?
map_user_kiobuf() called from the raw I/O path should ,in the write case, be flushing user data out, so that its visible to the kernel VA.
I think you're right. flush_dcache_page is for page cache pages, not for random user addresses. Would something like this make sense?
Index: mm/memory.c
===================================================================
RCS file: /var/cvs/linux-2.4/mm/memory.c,v
retrieving revision 1.24
mm/memory.c 25 Sep 2003 14:22:42 -0000 renamed from mm/memory.c 29 Nov 2002 02:21:13 -0000 1.24 +1 -6
@@ -569,12 +569,7 @@ int map_user_kiobuf(int rw, struct kiobu return err; } iobuf->nr_pages = err; - while (pgcount--) { - /* FIXME: flush superflous for rw==READ, - * probably wrong function for rw==WRITE - */ - flush_dcache_page(iobuf->maplist[pgcount]); - } + flush_cache_range(mm, va, va + len); dprintk ("map_user_kiobuf: end OK\n"); return 0; }
I deleted the comment because it's not correct ;-) The kernel is about to access these pages. Any existing cache for the contents of those pages must now be invalidated if the kernel's about to write to it, and must be written back if the kernel's about to read from it. flush_cache_range() must already have these properties otherwise munmap() wouldn't work.
Signature · 3 lines
"It's not Hollywood. War is real, war is primarily not about defeat or victory, it is about death. I've seen thousands and thousands of dead bodies. Do you think I want to have an academic debate on this subject?" -- Robert Fisk
Quoted 2 lines · On Thu, 25 Sep 2003 15:26:23 +0100 Matthew Wilcox <[email protected]> wrote:
I think you're right.
flush_dcache_pageis for page cache pages, not for random user addresses. Would something like this make sense?
The flush_dcache_page() is needed in case the platform has deferred the flushing for a kernel cpu store to a page cache page.
You cannot just delete this call and replace it with a flush_cache_range() call, that simply won't work.
Quoted 8 lines · David S. Miller
Quoted 2 lines · On Thu, 25 Sep 2003 15:26:23 +0100 Matthew Wilcox <[email protected]> wrote:
I think you're right.
flush_dcache_pageis for page cache pages, not for random user addresses. Would something like this make sense?The
flush_dcache_page()is needed in case the platform has deferred the flushing for a kernel cpu store to a page cache page.
But this isn't a page cache page.
Quoted 2 lines
You cannot just delete this call and replace it with a
flush_cache_range()call, that simply won't work.
Signature · 3 lines
"It's not Hollywood. War is real, war is primarily not about defeat or victory, it is about death. I've seen thousands and thousands of dead bodies. Do you think I want to have an academic debate on this subject?" -- Robert Fisk
Quoted 5 lines · On Fri, 26 Sep 2003 12:24:00 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 2 lines · David S. Miller
The
flush_dcache_page()is needed in case the platform has deferred the flushing for a kernel cpu store to a page cache page.But this isn't a page cache page.
Are you sure? Isn't raw I/O allowed on user buffers backed by some kind of file based mmap()?
Hi,
Quoted 2 lines · David S. Miller
Are you sure? Isn't raw I/O allowed on user buffers backed by some kind of file based
mmap()?
Yes, it is.
--Stephen
Quoted 6 lines · On 26 Sep 2003 12:57:01 +0100 "Stephen C. Tweedie" <[email protected]> wrote:
Quoted 2 lines · David S. Miller
Are you sure? Isn't raw I/O allowed on user buffers backed by some kind of file based
mmap()?Yes, it is.
So there we go, that's how page can be a page cache page Matthew.
Quoted 11 lines · David S. Miller
Quoted 6 lines · On 26 Sep 2003 12:57:01 +0100 "Stephen C. Tweedie" <[email protected]> wrote:
Quoted 2 lines · David S. Miller
Are you sure? Isn't raw I/O allowed on user buffers backed by some kind of file based
mmap()?Yes, it is.
So there we go, that's how page can be a page cache page Matthew.
OK, so it *might* be a page cache page, but isn't necessarily. So we need to call *both* flush_dcache_page() and flush_cache_range(), right?
Signature · 3 lines
"It's not Hollywood. War is real, war is primarily not about defeat or victory, it is about death. I've seen thousands and thousands of dead bodies. Do you think I want to have an academic debate on this subject?" -- Robert Fisk
Quoted 2 lines · On Fri, 26 Sep 2003 13:20:23 +0100 Matthew Wilcox <[email protected]> wrote:
OK, so it *might* be a page cache page, but isn't necessarily. So we need to call *both*
flush_dcache_page()andflush_cache_range(), right?
I'm starting to think that flush_dcache_page() needs to take care of this.
Santosh remarked that flush_dcache_page() doesn't get the user virtual address, but that is not needed. From the page you can walk the mmap()s and flush the mapping in each address space it is contained within.
And you absolutely must flush each address space, not just the one currently doing the raw I/O request.
In that light, doing a flush_cache_range() with a specific VMA (2.6.x) or MM (2.4.x) is totally illogical here.
Quoted 6 lines
Quoted 2 lines · On Fri, 26 Sep 2003 13:20:23 +0100 Matthew Wilcox <[email protected]> wrote:
OK, so it *might* be a page cache page, but isn't necessarily. So we need to call *both*
flush_dcache_page()andflush_cache_range(), right?
Quoted 6 lines
I'm starting to think that
flush_dcache_page()needs to take care of this.Santosh remarked that
flush_dcache_page()doesn't get the user virtual address, but that is not needed. From the page you can walk the mmap()s and flush the mapping in each address space it is contained within.
Are you talking about the page->mapping->{i_mmap_shared, i_mmap} lists ? If so, this is already handled in __flush_dcache_page ().
The problem in this case is page->mapping is NULL.
Quoted 7 lines
And you absolutely must flush each address space, not just the one currently doing the raw I/O request.
In that light, doing a
flush_cache_range()with a specific VMA (2.6.x) or MM (2.4.x) is totally illogical here.
Quoted 1 line · On Fri, 26 Sep 2003 18:41:41 +0530 (IST) SANTOSH ABRAHAM…
The problem in this case is
page->mappingis NULL.
When page->mapping is NULL, flush_dcache_page() should purge the page (by physical addres) from it's caches. This is what sparc64 does.
Flushing just the current address space is incorrect, so don't get the fancy idea to pass the 'mm' and 'va' into flush_dcache_page().
Quoted 7 lines · David S. Miller
Quoted 1 line · On Fri, 26 Sep 2003 18:41:41 +0530 (IST) SANTOSH ABRAHAM…
The problem in this case is
page->mappingis NULL.When
page->mappingis NULL,flush_dcache_page()should purge the page (by physical addres) from it's caches. This is what sparc64 does.
But you can't do that on PA-RISC. You can only purge virtual addresses.
Quoted 2 lines
Flushing just the current address space is incorrect, so don't get the fancy idea to pass the 'mm' and 'va' into
flush_dcache_page().
Signature · 3 lines
"It's not Hollywood. War is real, war is primarily not about defeat or victory, it is about death. I've seen thousands and thousands of dead bodies. Do you think I want to have an academic debate on this subject?" -- Robert Fisk
Quoted 5 lines · On Fri, 26 Sep 2003 14:29:01 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 2 lines · David S. Miller
When
page->mappingis NULL,flush_dcache_page()should purge the page (by physical addres) from it's caches. This is what sparc64 does.But you can't do that on PA-RISC. You can only purge virtual addresses.
Then for page->mapping == NULL your flush_dcache_page() is not doing what it is supposed to, and you can expect problems extending further than this raw I/O case.
You have to find a way to walk all the address spaces to figure out where the page is mapped.
If you just put a flush_cache_range() there, you may get your current test working but that code is wrong. It will be wrong in any case where the page is mapped to anywhere other then this mapping in this address space.
The real problem for you guys is that in 2.4.x there is no easy way to go from a page to it's mapping regardless of what kind of page it is. That's what you need to rectify somehow.
Quoted 4 lines · David S. Miller
Quoted 1 line · On Fri, 26 Sep 2003 14:29:01 +0100 Matthew Wilcox <[email protected]> wrote:
On Fri, Sep 26, 2003 at 05:56:52AM -0700, David S. Miller wrote:
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
When
page->mappingis NULL,flush_dcache_page()should purge the page (by physical addres) from it's caches. This is what sparc64 does.But you can't do that on PA-RISC. You can only purge virtual addresses.
Then for
page->mapping== NULL yourflush_dcache_page()is not doing what it is supposed to, and you can expect problems extending further than this raw I/O case.
Oh, come on, Dave. You just changed the definition of flush_dcache_page(). You can't claim the implementation isn't doing what it's supposed to! The definition in cachetlb talks *only* about page cache pages.
Quoted 2 lines
You have to find a way to walk all the address spaces to figure out where the page is mapped.
That's ridiculous. For a start, pages can't be mapped into multiple address spaces without being in the page cache. Second, we already know where it is, you just refuse to pass that information in.
Signature · 3 lines
"It's not Hollywood. War is real, war is primarily not about defeat or victory, it is about death. I've seen thousands and thousands of dead bodies. Do you think I want to have an academic debate on this subject?" -- Robert Fisk
Quoted 5 lines · On Fri, 26 Sep 2003 15:09:48 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 2 lines
You have to find a way to walk all the address spaces to figure out where the page is mapped.
That's ridiculous. For a start, pages can't be mapped into multiple address spaces without being in the page cache.
How in the world can anonymous pages work then? Of course anonymous pages can be in multiple address spaces without being in the page cache.
Quoted 6 lines · David S. Miller
Quoted 3 lines · On Fri, 26 Sep 2003 15:09:48 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 2 lines
You have to find a way to walk all the address spaces to figure out where the page is mapped.
Quoted 6 lines
Quoted 2 lines
That's ridiculous. For a start, pages can't be mapped into multiple address spaces without being in the page cache.
How in the world can anonymous pages work then? Of course anonymous pages can be in multiple address spaces without being in the page cache.
How? By fork()? If so, that's not a problem -- the pages stay at the same address in both processes, and flushing one will flush any inherited pages. Otherwise you'll have to be more explicit.
Signature · 3 lines
"It's not Hollywood. War is real, war is primarily not about defeat or victory, it is about death. I've seen thousands and thousands of dead bodies. Do you think I want to have an academic debate on this subject?" -- Robert Fisk
Quoted 10 lines · On Fri, 26 Sep 2003 15:30:52 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 9 lines · David S. Miller
Quoted 5 lines · On Fri, 26 Sep 2003 15:09:48 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 2 lines
You have to find a way to walk all the address spaces to figure out where the page is mapped.
That's ridiculous. For a start, pages can't be mapped into multiple address spaces without being in the page cache.
Quoted 7 lines
Quoted 3 lines
How in the world can anonymous pages work then? Of course anonymous pages can be in multiple address spaces without being in the page cache.
How? By
fork()? If so, that's not a problem -- the pages stay at the same address in both processes, and flushing one will flush any inherited pages. Otherwise you'll have to be more explicit.
Any page can be mremap()'d to any other address.
You really do have to walk all the address spaces to sufficiently tap the page out of the caches. Unfortunately, this is only easy in 2.6.x :(
Hi,
Quoted 1 line · David S. Miller
Any page can be mremap()'d to any other address.
Yep. At least this case is rare, and you can optimise for the simple case by checking page->count. But the general case really does need to be covered if you're going to be fully correct for all cases.
And with O_DIRECT being an unprivileged operation, users have the ability to expose this problem quite easily.
Cheers,
Stephen
Hi,
Quoted 2 lines · Matthew Wilcox
That's ridiculous. For a start, pages can't be mapped into multiple address spaces without being in the page cache.
fork(). At least we know the pages aren't dirty or writable in that case, though. But because of sys_mremap(), we don't know the VA for all possible mappings, and the pages are _not_ necessarily in the page cache.
There's only one place where anon pages get into the page cache, and that's via swap cache. Note that we don't have to worry about kernel-context access to such pages: the contents are only ever seen from user-space process context. The fork case still means there are multiple such referencing contexts, though.
--Stephen
The flush_dcache_page () might be required in cases when the same user buffer is being used for both reads and writes in an alternating fashion, right ?
-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Matthew Wilcox
Sent: Friday, September 26, 2003 4:54 PM
To: David S. Miller
Cc: Matthew Wilcox; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [parisc-linux] Fwd: Problems with raw interface.
Quoted 4 lines · David S. Miller
Quoted 1 line · On Thu, 25 Sep 2003 15:26:23 +0100 Matthew Wilcox <[email protected]> wrote:
I think you're right.
flush_dcache_pageis for page cache pages, not
for
Quoted 4 lines
Quoted 1 line
random user addresses. Would something like this make sense?
The
flush_dcache_page()is needed in case the platform has deferred the flushing for a kernel cpu store to a page cache page.
But this isn't a page cache page.
Quoted 2 lines
You cannot just delete this call and replace it with a
flush_cache_range()call, that simply won't work.
--
"It's not Hollywood. War is real, war is primarily not about defeat or victory, it is about death. I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk
A call to flush_cache_range(), before the call to flush_dcache_page () would fix the problem. I tested this a couple of days back, but called flush_user_dcache_range () instead of flush_cache_range(), and our apps seemed to work fine.
flush_cache_range () calls flush_user_dcache_range () internally. (i am aware that flush_user_dcache_range () is not an exported interface, but I urgently needed to get our apps working ).
Would the following be acceptable ?
------------------------------------------------------------------------
*** mm/memory.c.orig Fri Nov 29 07:51:13 2002
--- mm/memory.c Fri Sep 26 15:56:02 2003
***************
*** 570,578 ****
}
iobuf->nr_pages = err;
while (pgcount--) {
! /* FIXME: flush superflous for rw==READ,
! * probably wrong function for rw==WRITE
*/
flush_dcache_page(iobuf->maplist[pgcount]);
}
dprintk ("map_user_kiobuf: end OK\n");
--- 570,595 ----
}
iobuf->nr_pages = err;
while (pgcount--) {! struct page *page;
!
! page = iobuf->maplist[pgcount];
!
! /*
! * flush_dcache_page () calls flush_cache_page () internally
! * in certain cases. This check is an attempt to avoid
! * spurious cache flushes.
*/+
+ if (!page->mapping) {
+ unsigned long start = va + (pgcount * PAGE_SIZE);
+ unsigned long end = va + ((pgcount+1) *PAGE_SIZE);
+
+ /* Strictly not necessary, perhaps ? */
+ if (end > va+len) {
+ end = va+len;
+ }
+ flush_cache_range (mm, start, end);
+ }
flush_dcache_page(iobuf->maplist[pgcount]);
-----------------------------------------------------------------------
Does the order of the calls to flush_cache_range ()/flush_dcache_page() matter ?
Also, what would be the effect on other platforms ?
-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Matthew Wilcox
Sent: Thursday, September 25, 2003 7:56 PM
To: [email protected]
Cc: 'Randolph Chung'; [email protected]; Stephen C. Tweedie; David S. Miller
Subject: Re: [parisc-linux] Fwd: Problems with raw interface.
Quoted 7 lines · Santosh Abraham
hmm.. why is
map_user_kiobuf() callingflush_dcache_page() then ? should it not be calling flush_cache_{range,page} ?
map_user_kiobuf() called from the raw I/O path should ,in the write case, be flushing user data out, so that its visible to the kernel VA.
I think you're right. flush_dcache_page is for page cache pages, not for random user addresses. Would something like this make sense?
Index: mm/memory.c
===================================================================
RCS file: /var/cvs/linux-2.4/mm/memory.c,v
retrieving revision 1.24
mm/memory.c 25 Sep 2003 14:22:42 -0000 renamed from mm/memory.c 29 Nov 2002 02:21:13 -0000 1.24 +1 -6
@@ -569,12 +569,7 @@ int map_user_kiobuf(int rw, struct kiobu return err; } iobuf->nr_pages = err; - while (pgcount--) { - /* FIXME: flush superflous for rw==READ, - * probably wrong function for rw==WRITE - */ - flush_dcache_page(iobuf->maplist[pgcount]); - } + flush_cache_range(mm, va, va + len); dprintk ("map_user_kiobuf: end OK\n"); return 0; }
I deleted the comment because it's not correct ;-) The kernel is about to access these pages. Any existing cache for the contents of those pages must now be invalidated if the kernel's about to write to it, and must be written back if the kernel's about to read from it. flush_cache_range() must already have these properties otherwise munmap() wouldn't work.
--
"It's not Hollywood. War is real, war is primarily not about defeat or victory, it is about death. I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk
1) Please use unified diffs when you post patches.
2) If you insist on using Microsoft Outlook when posting patches at least configure it properly when doing so. By default it is going to turn tabs into spaces and that makes the patch corrupt and unusable by anyone.
2) Your patch is unacceptable because it is taking advantage of internal implementation details of flush_dcache_page() on parisc64 that might not (and in fact, is not) be true on other platforms.
Apologies for 1) & 2). I guess ignorance is not bliss or excusable,
is it :)
As for 3) i agree that there are issues, but what are the alternatives
we have ?
Assuming that you agree that we need to call flush_cache_range ( ) to fix the issue.
1) Call flush_cache_range () directly before the call to flush_dcache_page (). But this could impact other architectures too, right ?
2) Modify flush_dcache_page () in asm-parisc/pgalloc.h to handle this. But this is not easy cause we do not pass the user VA in flush_dcache_page ().
-----Original Message-----
From: David S. Miller [mailto:[email protected]]
Sent: Friday, September 26, 2003 4:05 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [parisc-linux] Fwd: Problems with raw interface.
1) Please use unified diffs when you post patches.
2) If you insist on using Microsoft Outlook when posting patches at least configure it properly when doing so. By default it is going to turn tabs into spaces and that makes the patch corrupt and unusable by anyone.
3) Your patch is unacceptable because it is taking advantage of internal implementation details of flush_dcache_page() on parisc64 that might not (and in fact, is not) be true on other platforms.