There is some code written by someone I know that works on some kernels, but not others. In particular, it doesn't work on 2.4.18-3 from redhat 7.3, but it does work on later versions of 2.4.18 from redhat and from 2.4.19. I'm wondering if the code is wrong, but just happens to work with certain kernel versions. Also, if that is the case, I would like to know what is wrong. The code is for a kernel module that tries to share a variable between kernelspace and userspace via remap_page_range()/mmap() and the /proc filesystem. The variable gets incremented in kernelspace each time the mouse is moved. Depending on the kernel, the variable in userspace may or may not get updated. The code is small, so i'm just attaching it. I would appreciate any help at all, even if no one knows exactly why it's behaving this way. Thanks, Ray Strode
#include <sys/mman.h> #include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int fd, sc; int *ptr; fd = open("/proc/seqcnt", O_RDWR); if (fd < 0) { perror("open"); return -1; } ptr = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED/*|MAP_ANONYMOUS*/, fd, 0); if (ptr == MAP_FAILED) { perror("mmap"); return -1; } printf("mmap ptr = %p\n", ptr); /* Validate result. */ printf("mmap original = %d\n", *ptr); *ptr = argc > 1 ? atoi(argv[1]) : 1234; printf("mmap modified = %d\n", *ptr); *ptr = 0; printf("mmap cleared = %d\n", *ptr); read(fd, &sc, sizeof(sc)); printf("io original = %d\n", sc); sc = argc > 1 ? atoi(argv[1]) : 1234; write(fd, &sc, sizeof(sc)); read(fd, &sc, sizeof(sc)); printf("io modified = %d\n", sc); printf("mmap test = %d\n", *ptr); sc = 0; write(fd, &sc, sizeof(sc)); read(fd, &sc, sizeof(sc)); printf("io cleared = %d\n", sc); /* Allow something to happen. */ printf("sleep...\n"); sleep(5); msync(ptr, sizeof(int), MS_SYNC|MS_INVALIDATE); printf("mmap final = %d\n", *ptr); read(fd, &sc, sizeof(sc)); printf("io final = %d\n", sc); if (munmap(ptr, sizeof(int)) < 0) perror("munmap"); close(fd); return 0; }