Skip to content

Commit 02dea08

Browse files
cfd-36Linus Torvalds
authored andcommitted
[PATCH] UML: Hotplug memory, take 2
Changes since first version added check for MADV_REMOVE support on the host fixed error return botch shrunk sprintf array by one character This adds hotplug memory support to UML. The mconsole syntax is config mem=[+-]n[KMG] In other words, add or subtract some number of kilobytes, megabytes, or gigabytes. Unplugged pages are allocated and then madvise(MADV_TRUNCATE), which is a currently experimental madvise extension. These pages are tracked so they can be plugged back in later if the admin decides to give them back. The first page to be unplugged is used to keep track of about 4M of other pages. A list_head is the first thing on this page. The rest is filled with addresses of other unplugged pages. This first page is not madvised, obviously. When this page is filled, the next page is used in a similar way and linked onto a list with the first page. Etc. This whole process reverses when pages are plugged back in. When a tracking page no longer tracks any unplugged pages, then it is next in line for plugging, which is done by freeing pages back to the kernel. Signed-off-by: Jeff Dike <jdike@addtoit.com> Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 85b6bce commit 02dea08

6 files changed

Lines changed: 185 additions & 47 deletions

File tree

‎arch/um/drivers/mconsole_kern.c‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "linux/namei.h"
2121
#include "linux/proc_fs.h"
2222
#include "linux/syscalls.h"
23+
#include "linux/list.h"
24+
#include "linux/mm.h"
2325
#include "linux/console.h"
2426
#include "asm/irq.h"
2527
#include "asm/uaccess.h"
@@ -347,6 +349,142 @@ static struct mc_device *mconsole_find_dev(char *name)
347349
return(NULL);
348350
}
349351

352+
#define UNPLUGGED_PER_PAGE \
353+
((PAGE_SIZE - sizeof(struct list_head)) / sizeof(unsigned long))
354+
355+
struct unplugged_pages {
356+
struct list_head list;
357+
void *pages[UNPLUGGED_PER_PAGE];
358+
};
359+
360+
static unsigned long long unplugged_pages_count = 0;
361+
static struct list_head unplugged_pages = LIST_HEAD_INIT(unplugged_pages);
362+
static int unplug_index = UNPLUGGED_PER_PAGE;
363+
364+
static int mem_config(char *str)
365+
{
366+
unsigned long long diff;
367+
int err = -EINVAL, i, add;
368+
char *ret;
369+
370+
if(str[0] != '=')
371+
goto out;
372+
373+
str++;
374+
if(str[0] == '-')
375+
add = 0;
376+
else if(str[0] == '+'){
377+
add = 1;
378+
}
379+
else goto out;
380+
381+
str++;
382+
diff = memparse(str, &ret);
383+
if(*ret != '\0')
384+
goto out;
385+
386+
diff /= PAGE_SIZE;
387+
388+
for(i = 0; i < diff; i++){
389+
struct unplugged_pages *unplugged;
390+
void *addr;
391+
392+
if(add){
393+
if(list_empty(&unplugged_pages))
394+
break;
395+
396+
unplugged = list_entry(unplugged_pages.next,
397+
struct unplugged_pages, list);
398+
if(unplug_index > 0)
399+
addr = unplugged->pages[--unplug_index];
400+
else {
401+
list_del(&unplugged->list);
402+
addr = unplugged;
403+
unplug_index = UNPLUGGED_PER_PAGE;
404+
}
405+
406+
free_page((unsigned long) addr);
407+
unplugged_pages_count--;
408+
}
409+
else {
410+
struct page *page;
411+
412+
page = alloc_page(GFP_ATOMIC);
413+
if(page == NULL)
414+
break;
415+
416+
unplugged = page_address(page);
417+
if(unplug_index == UNPLUGGED_PER_PAGE){
418+
INIT_LIST_HEAD(&unplugged->list);
419+
list_add(&unplugged->list, &unplugged_pages);
420+
unplug_index = 0;
421+
}
422+
else {
423+
struct list_head *entry = unplugged_pages.next;
424+
addr = unplugged;
425+
426+
unplugged = list_entry(entry,
427+
struct unplugged_pages,
428+
list);
429+
unplugged->pages[unplug_index++] = addr;
430+
err = os_drop_memory(addr, PAGE_SIZE);
431+
if(err)
432+
printk("Failed to release memory - "
433+
"errno = %d\n", err);
434+
}
435+
436+
unplugged_pages_count++;
437+
}
438+
}
439+
440+
err = 0;
441+
out:
442+
return err;
443+
}
444+
445+
static int mem_get_config(char *name, char *str, int size, char **error_out)
446+
{
447+
char buf[sizeof("18446744073709551615")];
448+
int len = 0;
449+
450+
sprintf(buf, "%ld", uml_physmem);
451+
CONFIG_CHUNK(str, size, len, buf, 1);
452+
453+
return len;
454+
}
455+
456+
static int mem_id(char **str, int *start_out, int *end_out)
457+
{
458+
*start_out = 0;
459+
*end_out = 0;
460+
461+
return 0;
462+
}
463+
464+
static int mem_remove(int n)
465+
{
466+
return -EBUSY;
467+
}
468+
469+
static struct mc_device mem_mc = {
470+
.name = "mem",
471+
.config = mem_config,
472+
.get_config = mem_get_config,
473+
.id = mem_id,
474+
.remove = mem_remove,
475+
};
476+
477+
static int mem_mc_init(void)
478+
{
479+
if(can_drop_memory())
480+
mconsole_register_dev(&mem_mc);
481+
else printk("Can't release memory to the host - memory hotplug won't "
482+
"be supported\n");
483+
return 0;
484+
}
485+
486+
__initcall(mem_mc_init);
487+
350488
#define CONFIG_BUF_SIZE 64
351489

352490
static void mconsole_get_config(int (*get_config)(char *, char *, int,

‎arch/um/include/mem_user.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ extern int iomem_size;
4949
extern unsigned long host_task_size;
5050
extern unsigned long task_size;
5151

52-
extern void check_devanon(void);
5352
extern int init_mem_user(void);
5453
extern void setup_memory(void *entry);
5554
extern unsigned long find_iomem(char *driver, unsigned long *len_out);

‎arch/um/include/os.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ extern int os_map_memory(void *virt, int fd, unsigned long long off,
205205
extern int os_protect_memory(void *addr, unsigned long len,
206206
int r, int w, int x);
207207
extern int os_unmap_memory(void *addr, int len);
208+
extern int os_drop_memory(void *addr, int length);
209+
extern int can_drop_memory(void);
208210
extern void os_flush_stdout(void);
209211

210212
/* tt.c

‎arch/um/os-Linux/mem.c‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,11 @@ int create_tmp_file(unsigned long long len)
121121
return(fd);
122122
}
123123

124-
static int create_anon_file(unsigned long long len)
125-
{
126-
void *addr;
127-
int fd;
128-
129-
fd = open("/dev/anon", O_RDWR);
130-
if(fd < 0) {
131-
perror("opening /dev/anon");
132-
exit(1);
133-
}
134-
135-
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
136-
if(addr == MAP_FAILED){
137-
perror("mapping physmem file");
138-
exit(1);
139-
}
140-
munmap(addr, len);
141-
142-
return(fd);
143-
}
144-
145-
extern int have_devanon;
146-
147124
int create_mem_file(unsigned long long len)
148125
{
149126
int err, fd;
150127

151-
if(have_devanon)
152-
fd = create_anon_file(len);
153-
else fd = create_tmp_file(len);
128+
fd = create_tmp_file(len);
154129

155130
err = os_set_exec_close(fd, 1);
156131
if(err < 0){

‎arch/um/os-Linux/process.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/unistd.h>
1212
#include <sys/mman.h>
1313
#include <sys/wait.h>
14+
#include <sys/mman.h>
1415
#include "ptrace_user.h"
1516
#include "os.h"
1617
#include "user.h"
@@ -20,6 +21,7 @@
2021
#include "kern_util.h"
2122
#include "longjmp.h"
2223
#include "skas_ptrace.h"
24+
#include "kern_constants.h"
2325

2426
#define ARBITRARY_ADDR -1
2527
#define FAILURE_PID -1
@@ -187,6 +189,48 @@ int os_unmap_memory(void *addr, int len)
187189
return(0);
188190
}
189191

192+
#ifndef MADV_REMOVE
193+
#define MADV_REMOVE 0x5 /* remove these pages & resources */
194+
#endif
195+
196+
int os_drop_memory(void *addr, int length)
197+
{
198+
int err;
199+
200+
err = madvise(addr, length, MADV_REMOVE);
201+
if(err < 0)
202+
err = -errno;
203+
return err;
204+
}
205+
206+
int can_drop_memory(void)
207+
{
208+
void *addr;
209+
int fd;
210+
211+
printk("Checking host MADV_REMOVE support...");
212+
fd = create_mem_file(UM_KERN_PAGE_SIZE);
213+
if(fd < 0){
214+
printk("Creating test memory file failed, err = %d\n", -fd);
215+
return 0;
216+
}
217+
218+
addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
219+
MAP_PRIVATE, fd, 0);
220+
if(addr == MAP_FAILED){
221+
printk("Mapping test memory file failed, err = %d\n", -errno);
222+
return 0;
223+
}
224+
225+
if(madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0){
226+
printk("MADV_REMOVE failed, err = %d\n", -errno);
227+
return 0;
228+
}
229+
230+
printk("OK\n");
231+
return 1;
232+
}
233+
190234
void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
191235
{
192236
int flags = 0, pages;

‎arch/um/os-Linux/start_up.c‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -470,25 +470,6 @@ int can_do_skas(void)
470470
}
471471
#endif
472472

473-
int have_devanon = 0;
474-
475-
/* Runs on boot kernel stack - already safe to use printk. */
476-
477-
void check_devanon(void)
478-
{
479-
int fd;
480-
481-
printk("Checking for /dev/anon on the host...");
482-
fd = open("/dev/anon", O_RDWR);
483-
if(fd < 0){
484-
printk("Not available (open failed with errno %d)\n", errno);
485-
return;
486-
}
487-
488-
printk("OK\n");
489-
have_devanon = 1;
490-
}
491-
492473
int __init parse_iomem(char *str, int *add)
493474
{
494475
struct iomem_region *new;
@@ -664,6 +645,5 @@ void os_check_bugs(void)
664645
{
665646
check_ptrace();
666647
check_sigio();
667-
check_devanon();
668648
}
669649

0 commit comments

Comments
 (0)