Subj : mmap with MAP_ANONYMOUS gives EINVAL To : comp.os.linux.questions,comp.os.linux From : joga2052 Date : Fri Jul 09 2004 09:29 am Hello *, I'm experimenting with stack allocation for user-space threads using mmap. I'm using MAP_ANONYMOUS (so that i won't need a file descriptor) void *stack; stack = mmap(0, 8192, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0); if (stack == MAP_FAILED) { perror("mmap failed"); exit(1); } The call to mmap always fails (errno = EINVAL). I digged into the source and found the following: Linux kernel 2.4.18 "mm/mmap.c" [readonly] 1370 lines --41%-- 568,3-17 41% unsigned long do_mmap_pgoff(struct file * file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, unsigned long pgoff) { //... } else { vm_flags |= VM_SHARED | VM_MAYSHARE; switch (flags & MAP_TYPE) { default: return -EINVAL; case MAP_PRIVATE: vm_flags &= ~(VM_SHARED | VM_MAYSHARE); /* fall through */ case MAP_SHARED: break; } } //... } I observed that the MAP_ANONYMOUS flag is filtered in the default case and the call returns with EINVAL. I tried "MAP_ANONYMOUS | MAP_SHARED" (to avoid the default case) and things were working fine. Here did i step on a bug, or am i committing a blunder somewhere? .