Linux: mmap(2) does not map allocated memory correctly? - Raspberry Pi Forums


hi all.
reading.

tried these things in c code:
  1. allocate memory (p) using malloc(3).
  2. calculate offset memory.
  3. open "/dev/mem".
  4. map memory address on "/dev/mem" offset.
  5. access mapped area.
  6. unmap memory.
  7. 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; } 
(this code needs root privilege run.)

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).
according second point, kernel not map memory area allocated using malloc(3) or correctly.

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.


raspberrypi



Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support