Synchronizing userspace and kernel using mmaped memory

From: Tomer Margalit
Date: Wed Apr 11 2012 - 06:30:35 EST


Hi All,

I was wondering whether it is possible to use an mmaped byte to
synchronize userspace and kernel.
To elaborate, I have a line of buffers that need to be transfered from
a kernel driver into a userspace daemon, so they can be sent to the
internet.
My current communication scheme is to use a series of mmaped buffers,
copy the data to them, and then when done, set a bit (in that same
memory mapped region), that indicates the page was filled and is ready
for transmission.

I have implemented this scheme in two ways - one is to change the bit
both from kernel and userspace (set the bit when a page is filled and
clear it when emptied).
The other is to only change the bit from kernel, and only read the bit
from userspace (when the page is to be cleared, a custom ioctl is
used).

The problem is that this only works 99% of time. I.e. once in every
million pages, there is a page that the kernel says it has filled
(according to logs), but userspace sees as cleared.
My initial instinct was that this is a bug, and I have tried debugging
it for a long time - with no results.
Figuring it was some kind of "illegal" memory access, I put guard
bytes around the problematic byte - but after checking, they remain
untouched.

So, before I continue debugging, I thought to ask whether this is a
good approach?
Specifically, when writing/reading a memory mapped page from kernel
(or userspace for that matter) - is there any action that needs to be
taken to flush the data or protect it?

Thanks,
Tomer
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/