Fixed mckernel procfs so that the host kernel does not complain about leaking.

This commit is contained in:
Naoki Hamada
2014-09-11 15:25:52 +09:00
parent 7e6fbc4854
commit 2a0e9c689f
2 changed files with 59 additions and 20 deletions

View File

@ -42,7 +42,7 @@ void mcexec_prepare_ack(ihk_os_t os, unsigned long arg, int err);
static void mcctrl_ikc_init(ihk_os_t os, int cpu, unsigned long rphys, struct ihk_ikc_channel_desc *c); static void mcctrl_ikc_init(ihk_os_t os, int cpu, unsigned long rphys, struct ihk_ikc_channel_desc *c);
int mcexec_syscall(struct mcctrl_channel *c, int pid, unsigned long arg); int mcexec_syscall(struct mcctrl_channel *c, int pid, unsigned long arg);
void procfs_create(void *__os, int ref, int osnum, int pid, unsigned long arg); void procfs_create(void *__os, int ref, int osnum, int pid, unsigned long arg);
void procfs_delete(void *__os, unsigned long arg); void procfs_delete(void *__os, int osnum, unsigned long arg);
void procfs_answer(unsigned long arg, int err); void procfs_answer(unsigned long arg, int err);
static int syscall_packet_handler(struct ihk_ikc_channel_desc *c, static int syscall_packet_handler(struct ihk_ikc_channel_desc *c,
@ -73,7 +73,7 @@ static int syscall_packet_handler(struct ihk_ikc_channel_desc *c,
break; break;
case SCD_MSG_PROCFS_DELETE: case SCD_MSG_PROCFS_DELETE:
procfs_delete(__os, pisp->arg); procfs_delete(__os, pisp->osnum, pisp->arg);
break; break;
case SCD_MSG_PROCFS_ANSWER: case SCD_MSG_PROCFS_ANSWER:

View File

@ -44,6 +44,13 @@ struct procfs_list_entry {
char fname[PROCFS_NAME_MAX]; char fname[PROCFS_NAME_MAX];
}; };
/*
* In the procfs_file_list, mckenrel procfs files are
* listed in the manner that the leaf file is located
* always nearer to the list top than its parent node
* file.
*/
LIST_HEAD(procfs_file_list); LIST_HEAD(procfs_file_list);
static ihk_spinlock_t procfs_file_list_lock; static ihk_spinlock_t procfs_file_list_lock;
@ -51,6 +58,7 @@ static ihk_spinlock_t procfs_file_list_lock;
* \brief Return specified procfs entry. * \brief Return specified procfs entry.
* *
* \param p a name of the procfs file * \param p a name of the procfs file
* \param osnum os number
* \param mode if zero create a directory otherwise a file * \param mode if zero create a directory otherwise a file
* *
* return value: NULL: Something wrong has occurred. * return value: NULL: Something wrong has occurred.
@ -62,7 +70,7 @@ static ihk_spinlock_t procfs_file_list_lock;
* This process is recursive to the root of the procfs tree. * This process is recursive to the root of the procfs tree.
*/ */
static struct proc_dir_entry *get_procfs_entry(char *p, int mode) static struct proc_dir_entry *get_procfs_entry(char *p, int osnum, int mode)
{ {
char *r; char *r;
struct proc_dir_entry *ret = NULL, *parent = NULL; struct proc_dir_entry *ret = NULL, *parent = NULL;
@ -70,7 +78,7 @@ static struct proc_dir_entry *get_procfs_entry(char *p, int mode)
char name[PROCFS_NAME_MAX]; char name[PROCFS_NAME_MAX];
unsigned long irqflags; unsigned long irqflags;
dprintk("get_procfs_entry: %s for mode %o\n", p, mode); dprintk("get_procfs_entry: %s for osnum %d mode %o\n", p, osnum, mode);
irqflags = ihk_ikc_spinlock_lock(&procfs_file_list_lock); irqflags = ihk_ikc_spinlock_lock(&procfs_file_list_lock);
list_for_each_entry(e, &procfs_file_list, list) { list_for_each_entry(e, &procfs_file_list, list) {
if (e == NULL) { if (e == NULL) {
@ -91,7 +99,7 @@ static struct proc_dir_entry *get_procfs_entry(char *p, int mode)
/* We have non-null parent dir. */ /* We have non-null parent dir. */
strncpy(name, p, r - p); strncpy(name, p, r - p);
name[r - p] = '\0'; name[r - p] = '\0';
parent = get_procfs_entry(name, 0); parent = get_procfs_entry(name, osnum, 0);
if (parent == NULL) { if (parent == NULL) {
/* We counld not get a parent procfs entry. Give up.*/ /* We counld not get a parent procfs entry. Give up.*/
return NULL; return NULL;
@ -121,6 +129,7 @@ static struct proc_dir_entry *get_procfs_entry(char *p, int mode)
return NULL; return NULL;
} }
ret->data = e; ret->data = e;
e->osnum = osnum;
e->entry = ret; e->entry = ret;
e->parent = parent; e->parent = parent;
@ -170,14 +179,13 @@ void procfs_create(void *__os, int ref, int osnum, int pid, unsigned long arg)
printk("ERROR: procfs_creat: file name not properly terminated.\n"); printk("ERROR: procfs_creat: file name not properly terminated.\n");
goto quit; goto quit;
} }
entry = get_procfs_entry(name, mode); entry = get_procfs_entry(name, osnum, mode);
if (entry == NULL) { if (entry == NULL) {
printk("ERROR: could not create a procfs entry for %s.\n", name); printk("ERROR: could not create a procfs entry for %s.\n", name);
goto quit; goto quit;
} }
e = entry->data; e = entry->data;
e->osnum = osnum;
e->os = __os; e->os = __os;
e->cpu = ref; e->cpu = ref;
e->pid = pid; e->pid = pid;
@ -191,10 +199,11 @@ quit:
* \brief Delete a procfs entry. * \brief Delete a procfs entry.
* *
* \param __os (opaque) os variable * \param __os (opaque) os variable
* \param osnum os number
* \param arg sent argument * \param arg sent argument
*/ */
void procfs_delete(void *__os, unsigned long arg) void procfs_delete(void *__os, int osnum, unsigned long arg)
{ {
ihk_device_t dev = ihk_os_to_dev(__os); ihk_device_t dev = ihk_os_to_dev(__os);
unsigned long parg; unsigned long parg;
@ -211,24 +220,25 @@ void procfs_delete(void *__os, unsigned long arg)
dprintk("fname: %s.\n", f->fname); dprintk("fname: %s.\n", f->fname);
irqflags = ihk_ikc_spinlock_lock(&procfs_file_list_lock); irqflags = ihk_ikc_spinlock_lock(&procfs_file_list_lock);
list_for_each_entry(e, &procfs_file_list, list) { list_for_each_entry(e, &procfs_file_list, list) {
if (strncmp(e->fname, f->fname, PROCFS_NAME_MAX) == 0) { if ((strncmp(e->fname, f->fname, PROCFS_NAME_MAX) == 0) &&
dprintk("found and delete an entry in the list.\n"); (e->osnum == osnum)) {
list_del(&e->list); list_del(&e->list);
e->entry->read_proc = NULL; e->entry->read_proc = NULL;
e->entry->data = NULL; e->entry->data = NULL;
parent = e->parent; parent = e->parent;
kfree(e); kfree(e);
r = strrchr(f->fname, '/');
if (r == NULL) {
strncpy(name, f->fname, PROCFS_NAME_MAX);
} else {
strncpy(name, r + 1, PROCFS_NAME_MAX);
}
dprintk("found and remove %s from the list.\n", name);
remove_proc_entry(name, parent);
break; break;
} }
} }
ihk_ikc_spinlock_unlock(&procfs_file_list_lock, irqflags); ihk_ikc_spinlock_unlock(&procfs_file_list_lock, irqflags);
r = strrchr(f->fname, '/');
if (r == NULL) {
strncpy(name, f->fname, PROCFS_NAME_MAX);
} else {
strncpy(name, r + 1, PROCFS_NAME_MAX);
}
remove_proc_entry(name, parent);
f->status = 1; /* Now the peer can free the data. */ f->status = 1; /* Now the peer can free the data. */
ihk_device_unmap_virtual(dev, f, sizeof(struct procfs_file)); ihk_device_unmap_virtual(dev, f, sizeof(struct procfs_file));
ihk_device_unmap_memory(dev, parg, sizeof(struct procfs_file)); ihk_device_unmap_memory(dev, parg, sizeof(struct procfs_file));
@ -288,7 +298,7 @@ retry:
r->ret = -EIO; /* default to error */ r->ret = -EIO; /* default to error */
r->offset = offset; r->offset = offset;
r->count = count; r->count = count;
strncpy(r->fname, e->fname, PROCFS_NAME_MAX); strncpy((char *)r->fname, e->fname, PROCFS_NAME_MAX);
isp.msg = SCD_MSG_PROCFS_REQUEST; isp.msg = SCD_MSG_PROCFS_REQUEST;
isp.ref = e->cpu; isp.ref = e->cpu;
isp.arg = virt_to_phys(r); isp.arg = virt_to_phys(r);
@ -318,7 +328,7 @@ retry:
} }
*start = buffer; *start = buffer;
ret = r->ret; ret = r->ret;
kfree(r); kfree((void *)r);
return ret; return ret;
} }
@ -339,10 +349,37 @@ void procfs_init(int osnum) {
*/ */
void procfs_exit(int osnum) { void procfs_exit(int osnum) {
char buf[20]; char buf[20], *r;
int error; int error;
mm_segment_t old_fs = get_fs(); mm_segment_t old_fs = get_fs();
struct kstat stat; struct kstat stat;
struct proc_dir_entry *parent;
struct procfs_list_entry *e, *temp = NULL;
unsigned long irqflags;
dprintk("remove remaining mckernel procfs files.\n");
irqflags = ihk_ikc_spinlock_lock(&procfs_file_list_lock);
list_for_each_entry_safe(e, temp, &procfs_file_list, list) {
if (e->osnum == osnum) {
dprintk("found entry for %s.\n", e->fname);
list_del(&e->list);
e->entry->read_proc = NULL;
e->entry->data = NULL;
parent = e->parent;
r = strrchr(e->fname, '/');
if (r == NULL) {
r = e->fname;
} else {
r += 1;
}
remove_proc_entry(r, parent);
dprintk("free the entry\n");
kfree(e);
}
dprintk("iterate it.\n");
}
ihk_ikc_spinlock_unlock(&procfs_file_list_lock, irqflags);
sprintf(buf, "/proc/mcos%d", osnum); sprintf(buf, "/proc/mcos%d", osnum);
@ -353,6 +390,8 @@ void procfs_exit(int osnum) {
return; return;
} }
printk("procfs_exit: We have to remove unexpectedly remaining %s.\n", buf);
/* remove remnant of previous mcos%d */ /* remove remnant of previous mcos%d */
remove_proc_entry(buf + 6, NULL); remove_proc_entry(buf + 6, NULL);
} }