Linux: mmap(2) does not map allocated memory correctly? - Raspberry Pi Forums
hi all.
reading.
tried these things in c code: (this code needs root privilege run.)
problems below:
behavior obvious or memory space misunderstanding?
have idea?
p.s.
replaced malloc in code posix_memalign or so, behavior did not change.
, checked "/dev/mem" can accessed correctly (i.e. config_strict_mem in kernel configuration disabled).
finally, sorry humble english.
regards,
akane.
reading.
tried these things in c code:
- allocate memory (p) using malloc(3).
- calculate offset memory.
- open "/dev/mem".
- map memory address on "/dev/mem" offset.
- access mapped area.
- unmap memory.
- release memory.
code: select all
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> int main() { const int size = 1000; /* */ int align; int fd; int p_offset; int *p, *pp; align = sysconf(_sc_pagesize); if (align == -1) { fprintf(stderr, "error: sysconf: _sc_pagesize: %s\n", strerror(errno)); exit(exit_failure); } printf("align: %d\n", align); p = malloc(size * 2); /* *2 aligning */ if (p == null) { fprintf(stderr, "error: malloc returned null\n"); exit(exit_failure); } printf("p: %p\n", p); p_offset = align - (off_t) p % align; printf("p_offset: %d\n", p_offset); p += p_offset / sizeof(*p); fd = open("/dev/mem", o_rdwr); if (fd == -1) { fprintf(stderr, "error: open: /dev/mem: %s\n", strerror(errno)); exit(exit_failure); } pp = mmap(null, size, prot_read | prot_write, map_shared, fd, (off_t) p); if (pp == map_failed) { fprintf(stderr, "error: mmap: %s\n", strerror(errno)); exit(exit_failure); } /* * write values pp. * can "pp[0] = 0;", example. * bug can caused more many writes pp (idk why...). */ memset(pp, 0, size); if (munmap(pp, size) == -1) { fprintf(stderr, "error: munmap: %s\n", strerror(errno)); exit(exit_failure); } if (close(fd) == -1) { fprintf(stderr, "error: close: %s\n", strerror(errno)); exit(exit_failure); } p -= p_offset / sizeof(*p); free(p); return 0; }
problems below:
- the linux kernel (at least 3.18.9 released part of raspbian) panics or freezes when code executed many times (usually, 3 or 4 times).
- when write values mapped memory, original memory not changed (even after unmapping).
behavior obvious or memory space misunderstanding?
have idea?
p.s.
replaced malloc in code posix_memalign or so, behavior did not change.
, checked "/dev/mem" can accessed correctly (i.e. config_strict_mem in kernel configuration disabled).
finally, sorry humble english.
regards,
akane.
/dev/mem represents physical address space, seen arm processor. when using mmap() on file, offset argument should hardware bus address. malloc() returns virtual address, specific process's view of memory. not know in physical memory particular block resides, , kernel may move or swap out disk without notice.
mapping random block of memory may belong process, or kernel. no surprise writing crashes system. in worst case, corrupt filesystems.
trying do? see no benefit in mapping same block of memory twice in same process.
mapping random block of memory may belong process, or kernel. no surprise writing crashes system. in worst case, corrupt filesystems.
trying do? see no benefit in mapping same block of memory twice in same process.
raspberrypi
Comments
Post a Comment