PATCH: make mmap(/dev/mem) use O_SYNC and O_DIRECT

739 views
Skip to first unread message

Tim Hockin

unread,
Jul 29, 2008, 1:11:31 AM7/29/08
to iotool...@googlegroups.com
Upcoming patches to Linux will require O_SYNC to guarantee uncached access
to /dev/mem.


Index: mmio_rw.c
===================================================================
--- mmio_rw.c (revision 11)
+++ mmio_rw.c (working copy)
@@ -51,18 +51,18 @@
mmap_addr->off = mmap_addr->addr & (mmap_addr->pgsize - 1);
mmap_addr->addr &= ~ ((uint64_t)mmap_addr->pgsize - 1);

- mmap_addr->fd = open("/dev/mem", flags);
+ mmap_addr->fd = open("/dev/mem", flags|O_DIRECT|O_SYNC);
if (mmap_addr->fd < 0) {
fprintf(stderr, "open(/dev/mem): %s\n", strerror(errno));
return -1;
}

prot = 0;
- if (flags == O_RDWR) {
+ if ((flags&O_ACCMODE) == O_RDWR) {
prot = PROT_READ | PROT_WRITE;
- } else if (flags == O_RDONLY) {
+ } else if ((flags & O_ACCMODE) == O_RDONLY) {
prot = PROT_READ;
- } else if (flags == O_WRONLY) {
+ } else if ((flags & O_ACCMODE) == O_WRONLY) {
prot = PROT_WRITE;
}

Index: Makefile
===================================================================
--- Makefile (revision 11)
+++ Makefile (working copy)
@@ -30,7 +30,8 @@
IOTOOLS_DEBUG = -O2 -DNDEBUG
endif

-CFLAGS = -Wall -Werror $(ARCHFLAGS) $(IOTOOLS_STATIC) $(IOTOOLS_DEBUG)
+CFLAGS = -Wall -Werror $(DEFS) $(ARCHFLAGS) $(IOTOOLS_STATIC) $(IOTOOLS_DEBUG)
+DEFS = -D_GNU_SOURCE
SBINDIR ?= /usr/local/sbin

BINARY=iotools

Aaron Durbin

unread,
Jul 30, 2008, 11:02:18 AM7/30/08
to iotool...@googlegroups.com
lg. I guess it doesn't matter if you open the file w/ O_SYNC but read-only.

Tim Hockin

unread,
Jul 30, 2008, 11:15:44 AM7/30/08
to iotool...@googlegroups.com
On Wed, Jul 30, 2008 at 8:02 AM, Aaron Durbin <adu...@gmail.com> wrote:
>
> lg. I guess it doesn't matter if you open the file w/ O_SYNC but read-only.

I don't think it matters.

Tim

Reply all
Reply to author
Forward
0 new messages