Now I'm befuddled. I modified the source so that it was easy to try different combinations. If I sudo it, I can map a 1GiB (1<<30) private anonymous region if I sudo the program. I cannot find any combination with MAP_HUGETLB that works at all.
Here it is in working form, with an anonymous map and no backing file. The mmap request is for a single byte, so it works for ordinary users Try uncommenting the MAP_HUGETLB line, and I'd love to hear about a version that works, even if it's only for root.
Since I'm aiming for a huge memory region, I figured HUGETLB would make sense to limit the overhead.
============================ CUT HERE ================
/**
* @file
* <pre>"Find out the limits on locked memory"
* Last Modified: Mon Aug 18 11:21:23 PDT 2014
* @author Kevin O'Gorman
*/
#define _GNU_SOURCE /* enable some of the mmap flags */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
/* for getrlimit(2) */
#include <sys/time.h>
#include <sys/resource.h>
/* for open(2) */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
int
main(int argc, char *argv[])
{
void *where;
size_t length = 1;
struct rlimit rlim;
int flags = 0;
int fd = -1;
// fd = open("solvedtaq", O_RDWR); if (fd == -1) perror("open");
printf("fd is %d\n", fd);
// flags |= MAP_HUGETLB;
flags |= MAP_PRIVATE;
flags |= MAP_LOCKED;
flags |= MAP_ANONYMOUS;
if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
printf("RLIMIT_MEMLOCK: hard: %lld, soft: %lld\n", (long long)rlim.rlim_max, (long long)rlim.rlim_cur);
} else {
perror("getrlimit failed");
}
where = mmap(NULL, length, PROT_READ | PROT_WRITE, flags, fd, 0);
if (where != MAP_FAILED) {
printf("Mapped at %p\n", where);
} else {
perror("Mapping failed");
}
return EXIT_SUCCESS;
}
============================ CUT HERE ================