10 #include <sys/ioctl.h>
12 #include <sys/types.h>
21 #define SYS_PATH "/sys/devices/system/memory/"
31 unsigned long long *start_offline;
32 unsigned long long *len_offline;
34 unsigned long long kernel_max_order;
35 unsigned long long kernel_max_page_alloc_bytes;
36 unsigned long long kernel_num_nodes;
37 unsigned long long kernel_num_cpus;
38 unsigned long long palacios_compiled_mem_block_size;
39 unsigned long long palacios_runtime_mem_block_size;
42 #define VPRINTF(...) do { if (verbose) { printf(__VA_ARGS__); } } while (0)
43 #define EPRINTF(...) do { fprintf(stderr,__VA_ARGS__); } while (0)
46 static int read_offlined();
47 static int write_offlined();
48 static int find_offlined(unsigned long long base_addr);
49 static int clear_offlined();
52 static int offline_memory(unsigned long long mem_size_bytes,
53 unsigned long long mem_min_start,
56 unsigned long long *num_bytes,
57 unsigned long long *base_addr);
59 static int online_memory(unsigned long long num_bytes,
60 unsigned long long base_addr);
63 static int get_kernel_setup();
67 int main(int argc, char * argv[]) {
68 unsigned long long mem_size_bytes = 0;
69 unsigned long long mem_min_start = 0;
75 enum {NONE, ADD, REMOVE} op;
78 unsigned long long num_bytes, base_addr;
79 struct v3_mem_region mem;
81 while ((c=getopt(argc,argv,"hvarklm:n:"))!=-1) {
102 mem_min_start = atoll(optarg) * (1024*1024);
109 EPRINTF("-n requires the numa node...\n");
111 } else if (optopt=='m') {
112 EPRINTF("-m requires the minimum starting address (in MB)...\n");
115 EPRINTF("Unknown option %c\n",optopt);
120 EPRINTF("Unknown option %c\n",optopt);
126 if (op==NONE || optind==argc || help) {
127 EPRINTF("usage: v3_mem [-h] [-v] [ [-k] [-l] [-n k] [-m n] -a <memory size (MB)>] | [-r <hexaddr> | offline]\n\n"
128 "Palacios Memory Management\n\nMemory Addition\n"
129 " -a <mem> Allocate memory for use by Palacios (MB).\n\n"
130 " With -k this requests in-kernel allocation\n"
131 " Without -k this attempts to offline memory via hot remove\n\n"
132 " With -l the request or offlining is limited to first 4 GB\n"
133 " Without -l the request or offlining has no limits\n\n"
134 " With -m n the search for offlineable memory starts at n MB\n"
135 " Without -m n the search for offlineable memory starts at 0 MB\n\n"
136 " With -n i the request is for numa node i\n"
137 " Without -n i the request can be satified on any numa node\n\n"
139 " -r <hexaddr> Free Palacios memory containing hexaddr, online it if needed\n"
140 " -r offline Free all offline Palacios memory and online it\n\n"
150 if (get_kernel_setup()) {
151 EPRINTF("Cannot read kernel setup\n");
156 mem_size_bytes = atoll(argv[optind]) * (1024 * 1024);
158 if (mem_size_bytes > palacios_runtime_mem_block_size) {
159 EPRINTF("Trying to add a larger single chunk of memory than Palacios can manage\n"
160 "Your request: %llu bytes\n"
161 "Palacios run-time memory block size: %llu bytes\n",
162 mem_size_bytes, palacios_runtime_mem_block_size);
166 if (mem_size_bytes < palacios_runtime_mem_block_size) {
167 EPRINTF("Trying to add a smaller single chunk of memory than Palacios can manage\n"
168 "Your request: %llu bytes\n"
169 "Palacios run-time memory block size: %llu bytes\n",
170 mem_size_bytes, palacios_runtime_mem_block_size);
174 if (request && mem_size_bytes > kernel_max_page_alloc_bytes) {
175 EPRINTF("Trying to request a larger single chunk of memory than the kernel can allocate\n"
176 "Your request: %llu bytes\n"
177 "Kernel largest page allocation: %llu bytes\n"
178 "Kernel MAX_ORDER: %llu\n",
179 mem_size_bytes, kernel_max_page_alloc_bytes, kernel_max_order);
183 if (node>=0 && node>=kernel_num_nodes) {
184 EPRINTF("Trying to request or allocate memory for a nonexistent node\n"
185 "Your request: node %d\n"
186 "Kernel number of nodes: %llu\n",
187 node, kernel_num_nodes);
191 } else if (op==REMOVE) {
192 if (!strcasecmp(argv[optind],"offline")) {
195 base_addr=strtoll(argv[optind],NULL,16);
199 if (!getenv("PALACIOS_DIR")) {
200 EPRINTF("Please set the PALACIOS_DIR variable\n");
204 strcpy(offname,getenv("PALACIOS_DIR"));
205 strcat(offname,"/.v3offlinedmem");
207 if (!(off=fopen(offname,"a+"))) {
208 EPRINTF("Cannot open or create offline memory file %s",offname);
212 // removing all offlined memory we added is a special case
213 if (op==REMOVE && alloffline) {
217 // we just need to reinvoke ourselves
219 for (i=0;i<num_offline;i++) {
221 sprintf(cmd,"v3_mem -r %llx", start_offline[i]);
229 v3_fd = open(v3_dev, O_RDONLY);
232 EPRINTF("Error opening V3Vee control device\n");
241 VPRINTF("Trying to offline memory (size=%llu, min_start=%llu, limit32=%d)\n",mem_size_bytes,mem_min_start,limit32);
242 if (offline_memory(mem_size_bytes,mem_min_start,limit32,node,&num_bytes, &base_addr)) {
243 EPRINTF("Could not offline memory\n");
249 fprintf(off,"%llx\t%llx\n",base_addr, num_bytes);
251 printf("Memory of size %llu at 0x%llx has been offlined\n",num_bytes,base_addr);
253 mem.type=PREALLOCATED;
255 mem.base_addr=base_addr;
256 mem.num_pages=num_bytes/4096;
260 VPRINTF("Generating memory allocation request (size=%llu, limit32=%d)\n", mem_size_bytes, limit32);
261 mem.type = limit32 ? REQUESTED32 : REQUESTED;
264 mem.num_pages = mem_size_bytes / 4096;
267 VPRINTF("Allocation request is: type=%d, node=%d, base_addr=0x%llx, num_pages=%llu\n",
268 mem.type, mem.node, mem.base_addr, mem.num_pages);
270 if (ioctl(v3_fd, V3_ADD_MEMORY, &mem)<0) {
271 EPRINTF("Request rejected by Palacios\n");
272 printf("Allocation of memory by Palacios has failed. Check dmesg output for more information.\n");
277 VPRINTF("Request accepted by Palacios\n");
278 printf("%llu bytes of memory has been allocated by Palacios\n",mem.num_pages*4096);
284 } else if (op==REMOVE) {
289 entry=find_offlined(base_addr);
292 // no need to offline
295 mem.type=PREALLOCATED;
298 mem.base_addr=base_addr;
300 // now remove it from palacios
301 VPRINTF("Deallocation request is: type=%d, base_addr=0x%llx\n",
302 mem.type, mem.base_addr);
304 if (ioctl(v3_fd, V3_REMOVE_MEMORY, &mem)<0) {
305 EPRINTF("Request rejected by Palacios\n");
311 VPRINTF("Request accepted by Palacios\n");
313 printf("Memory at 0x%llx has been deallocated by Palacios\n", mem.base_addr);
316 VPRINTF("Onlining the memory to make it available to the kernel\n");
317 online_memory(start_offline[entry],len_offline[entry]);
319 len_offline[entry] = 0;
323 printf("Memory at 0x%llx has been onlined\n",mem.base_addr);
326 VPRINTF("Memory was deallocated in the kernel\n");
327 printf("Memory at 0x%llx has been onlined\n",mem.base_addr);
340 static int dir_filter(const struct dirent * dir) {
341 if (strncmp("memory", dir->d_name, 6) == 0) {
349 static int dir_cmp(const struct dirent **dir1, const struct dirent ** dir2) {
350 int num1 = atoi((*dir1)->d_name + 6);
351 int num2 = atoi((*dir2)->d_name + 6);
358 #define UNWIND(first,last) \
361 for (i = first; i <= last; i++) { \
364 snprintf(name,256,"%smemory%d/state",SYS_PATH,i); \
365 f=fopen(name,"r+"); \
367 perror("Cannot open state file\n"); \
370 VPRINTF("Re-onlining block %d (%s)\n",i,name); \
371 fprintf(f,"online\n"); \
377 static int offline_memory(unsigned long long mem_size_bytes,
378 unsigned long long mem_min_start,
381 unsigned long long *num_bytes,
382 unsigned long long *base_addr)
385 unsigned int block_size_bytes = 0;
386 int bitmap_entries = 0;
387 unsigned char * bitmap = NULL;
394 VPRINTF("Trying to find %dMB (%d bytes) of memory above %llu with limit32=%d\n", mem_size_bytes/(1024*1024), mem_size_bytes, mem_min_start, limit32);
396 /* Figure out the block size */
399 char tmp_buf[BUF_SIZE];
401 tmp_fd = open(SYS_PATH "block_size_bytes", O_RDONLY);
404 perror("Could not open block size file: " SYS_PATH "block_size_bytes");
408 if (read(tmp_fd, tmp_buf, BUF_SIZE) <= 0) {
409 perror("Could not read block size file: " SYS_PATH "block_size_bytes");
415 block_size_bytes = strtoll(tmp_buf, NULL, 16);
417 VPRINTF("Memory block size is %dMB (%d bytes)\n", block_size_bytes / (1024 * 1024), block_size_bytes);
422 num_blocks = mem_size_bytes / block_size_bytes;
423 if (mem_size_bytes % block_size_bytes) num_blocks++;
425 mem_min_start = block_size_bytes *
426 ((mem_min_start / block_size_bytes) + (!!(mem_min_start % block_size_bytes)));
428 VPRINTF("Looking for %d blocks of memory starting at %p (block %llu) with limit32=%d for node %d\n", num_blocks, (void*)mem_min_start, mem_min_start/block_size_bytes,limit32,node);
431 // We now need to find <num_blocks> consecutive offlinable memory blocks
433 /* Scan the memory directories */
435 struct dirent ** namelist = NULL;
440 int first_block = mem_min_start/block_size_bytes;
441 int limit_block = 0xffffffff / block_size_bytes; // for 32 bit limiting
443 last_block = scandir(SYS_PATH, &namelist, dir_filter, dir_cmp);
444 bitmap_entries = atoi(namelist[last_block - 1]->d_name + 6) + 1;
446 size = bitmap_entries / 8;
447 if (bitmap_entries % 8) size++;
449 bitmap = alloca(size);
452 VPRINTF("ERROR: could not allocate space for bitmap\n");
456 memset(bitmap, 0, size);
458 for (i = 0 ; j < bitmap_entries - 1; i++) {
459 struct dirent * tmp_dir = namelist[i];
461 char status_str[BUF_SIZE];
462 char fname[BUF_SIZE];
463 char nname[BUF_SIZE];
466 memset(status_str, 0, BUF_SIZE);
468 memset(fname, 0, BUF_SIZE);
469 snprintf(fname, BUF_SIZE, "%s%s/removable", SYS_PATH, tmp_dir->d_name);
471 memset(nname, 0, BUF_SIZE);
472 snprintf(nname, BUF_SIZE, "%s%s/node%d", SYS_PATH, tmp_dir->d_name,node);
474 j = atoi(tmp_dir->d_name + 6);
480 VPRINTF("Skipping %s due to minimum start constraint\n",fname);
484 if (limit32 && i>limit_block) {
485 VPRINTF("Skipping %s due to 32 bit constraint\n",fname);
489 // The prospective block must be (a) removable, and (b) currently online
490 // and for the needed node
492 VPRINTF("Checking %s...", fname);
495 if (stat(nname,&s)) {
496 VPRINTF("Skipping %s due to it being in the wrong node\n", fname);
502 block_fd = open(fname, O_RDONLY);
504 if (block_fd == -1) {
505 EPRINTF("Hotpluggable memory not supported or could not determine if block is removable...\n");
509 if (read(block_fd, status_str, BUF_SIZE) <= 0) {
510 perror("Could not read block removability information\n");
514 status_str[BUF_SIZE-1]=0;
518 if (atoi(status_str) == 1) {
519 VPRINTF("Removable ");
521 VPRINTF("Not removable\n");
525 snprintf(fname, BUF_SIZE, "%s%s/state", SYS_PATH, tmp_dir->d_name);
527 block_fd = open(fname, O_RDONLY);
530 perror("Could not open block state\n");
534 if (read(block_fd, status_str, BUF_SIZE) <=0) {
535 perror("Could not read block state information\n");
539 status_str[BUF_SIZE-1]=0;
543 if (!strncasecmp(status_str,"offline",7)) {
544 VPRINTF("and Already Offline (unusable)\n");
545 } else if (!strncasecmp(status_str,"online",6)) {
546 VPRINTF("and Online (usable)\n");
547 bitmap[major] |= (0x1 << minor);
549 VPRINTF("and in Unknown State '%s' (unusable)\n",status_str);
558 /* Scan bitmap for enough consecutive space */
560 // num_blocks: The number of blocks we need to find
561 // bitmap: bitmap of blocks (1 == allocatable)
562 // bitmap_entries: number of blocks in the system/number of bits in bitmap
563 // reg_start: The block index where our allocation will start
568 for (i = 0; i < bitmap_entries; i++) {
572 if (!(bitmap[i_major] & (0x1 << i_minor))) {
573 reg_start = i + 1; // skip the region start to next entry
580 if (run_len >= num_blocks) {
586 if (run_len < num_blocks) {
587 EPRINTF("Could not find enough consecutive memory blocks... (found %d)\n", run_len);
588 // no offlining yet, so no need to unwind here
594 /* Offline memory blocks starting at reg_start */
598 for (i = 0; i < num_blocks; i++) {
599 FILE * block_file = NULL;
602 memset(fname, 0, 256);
604 snprintf(fname, 256, "%smemory%d/state", SYS_PATH, i + reg_start);
606 block_file = fopen(fname, "r+");
608 if (block_file == NULL) {
609 perror("Could not open block file");
610 UNWIND(reg_start, i+reg_start-1);
615 VPRINTF("Offlining block %d (%s)\n", i + reg_start, fname);
616 fprintf(block_file, "offline\n");
624 /* We asked to offline set of blocks, but Linux could have lied.
625 * To be safe, check whether blocks were offlined and start again if not
631 mem_ready = 1; // Hopefully we are ok...
634 for (i = 0; i < num_blocks; i++) {
636 char fname[BUF_SIZE];
637 char status_buf[BUF_SIZE];
640 memset(fname, 0, BUF_SIZE);
641 memset(status_buf, 0, BUF_SIZE);
643 snprintf(fname, BUF_SIZE, "%smemory%d/state", SYS_PATH, i + reg_start);
646 block_fd = open(fname, O_RDONLY);
648 if (block_fd == -1) {
649 perror("Could not open block state file");
653 if (read(block_fd, status_buf, BUF_SIZE) <= 0) {
654 perror("Could not read block state");
658 status_buf[BUF_SIZE]=0;
660 VPRINTF("Checking offlined block %d (%s)...", i + reg_start, fname);
662 int ret = strncmp(status_buf, "offline", strlen("offline"));
664 if (ret != 0) { // uh oh
666 int major = (i + reg_start) / 8;
667 int minor = (i + reg_start) % 8;
670 bitmap[major] &= ~(0x1 << minor); // mark the block as not removable in bitmap
672 mem_ready = 0; // Keep searching
674 // remove trailing newline
675 if ((pos=strchr(status_buf, '\n')) != NULL) {
679 EPRINTF("ERROR - block status is '%s'\n", status_buf);
682 UNWIND(reg_start,reg_start+num_blocks-1);
688 VPRINTF("Offlined Memory OK\n");
693 /* Memory is offlined. Calculate size and phys start addr to send to Palacios */
694 *num_bytes = (unsigned long long)(num_blocks) * (unsigned long long)(block_size_bytes);
695 *base_addr = (unsigned long long)(reg_start) * (unsigned long long)(block_size_bytes);
701 static int online_memory(unsigned long long base_addr,
702 unsigned long long num_bytes)
705 unsigned int block_size_bytes = 0;
706 int bitmap_entries = 0;
707 unsigned char * bitmap = NULL;
714 VPRINTF("Trying to online memory from %llu to %llu\n",base_addr,base_addr+num_bytes-1);
716 /* Figure out the block size */
719 char tmp_buf[BUF_SIZE];
721 tmp_fd = open(SYS_PATH "block_size_bytes", O_RDONLY);
724 perror("Could not open block size file: " SYS_PATH "block_size_bytes");
728 if (read(tmp_fd, tmp_buf, BUF_SIZE) <= 0) {
729 perror("Could not read block size file: " SYS_PATH "block_size_bytes");
735 block_size_bytes = strtoll(tmp_buf, NULL, 16);
737 VPRINTF("Memory block size is %dMB (%d bytes)\n", block_size_bytes / (1024 * 1024), block_size_bytes);
741 num_blocks = num_bytes / block_size_bytes;
742 if (num_bytes % block_size_bytes) num_blocks++;
744 reg_start = base_addr / block_size_bytes;
746 VPRINTF("That is %lu blocks of size %llu starting at block %d\n", num_blocks, block_size_bytes, reg_start);
750 /* Online memory blocks starting at reg_start */
754 for (i = 0; i < num_blocks; i++) {
755 FILE * block_file = NULL;
758 memset(fname, 0, 256);
760 snprintf(fname, 256, "%smemory%d/state", SYS_PATH, i + reg_start);
762 block_file = fopen(fname, "r+");
764 if (block_file == NULL) {
765 perror("Could not open block file");
770 VPRINTF("Onlining block %d (%s)\n", i + reg_start, fname);
771 fprintf(block_file, "online\n");
784 static int read_offlined()
787 unsigned long long base, len;
791 while (fscanf(off,"%llx\t%llx\n",&base,&len)==2) { num_offline++; }
794 start_offline=(unsigned long long *)calloc(num_offline, sizeof(unsigned long long));
795 len_offline=(unsigned long long *)calloc(num_offline, sizeof(unsigned long long));
797 if (!start_offline || !len_offline) {
798 EPRINTF("Cannot allocate space to load offline map\n");
803 for (i=0;i<num_offline;i++) {
804 fscanf(off,"%llx\t%llx",&(start_offline[i]),&(len_offline[i]));
806 // we are now back to the end, and can keep appending
811 static int write_offlined()
816 if (!(off=fopen(offname,"w+"))) { // truncate
817 EPRINTF("Cannot open %s for writing!\n");
821 for (i=0;i<num_offline;i++) {
822 if (len_offline[i]) {
823 fprintf(off,"%llx\t%llx\n",start_offline[i],len_offline[i]);
826 // we are now back to the end, and can keep appending
831 static int clear_offlined()
838 static int find_offlined(unsigned long long base_addr)
842 for (i=0;i<num_offline;i++) {
843 if (base_addr>=start_offline[i] &&
844 base_addr<(start_offline[i]+len_offline[i])) {
855 static int get_kernel_setup()
859 f = fopen("/proc/v3vee/v3-info", "r");
862 EPRINTF("Cannot open /proc/v3vee/v3-info\n");
866 if (fscanf(f,"kernel MAX_ORDER:\t%llu\n",&kernel_max_order)!=1) {
867 EPRINTF("Cannot read kernel MAX_ORDER\n");
871 kernel_max_page_alloc_bytes = 4096ULL * (0x1ULL << kernel_max_order);
873 if (fscanf(f,"number of nodes:\t%llu\n",&kernel_num_nodes)!=1) {
874 EPRINTF("Cannot read kernel number of numa nodes\n");
878 if (fscanf(f,"number of cpus:\t%llu\n",&kernel_num_cpus)!=1) {
879 EPRINTF("Cannot read kernel number of cpus\n");
883 if (fscanf(f,"palacios compiled mem_block_size:\t%llu\n",&palacios_compiled_mem_block_size)!=1) {
884 EPRINTF("Cannot read palacios compiled mem_block_size\n");
888 if (fscanf(f,"palacios run-time mem_block_size:\t%llu\n",&palacios_runtime_mem_block_size)!=1) {
889 EPRINTF("Cannot read palacios run-time mem_block_size\n");