TOMOYO: Remove memory pool for string data.

Currently, TOMOYO allocates memory for string data from memory pool allocated
by kmalloc(PAGE_SIZE). But that makes it difficult to kfree() when garbage
collector is added. Thus, remove memory pool and use kmalloc(strlen()).

Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: James Morris <jmorris@namei.org>
This commit is contained in:
Tetsuo Handa 2010-01-05 06:39:00 +09:00 committed by James Morris
parent 0ed731859e
commit e41035a996

View File

@ -293,13 +293,6 @@ struct tomoyo_name_entry {
struct tomoyo_path_info entry; struct tomoyo_path_info entry;
}; };
/* Structure for available memory region. */
struct tomoyo_free_memory_block_list {
struct list_head list;
char *ptr; /* Pointer to a free area. */
int len; /* Length of the area. */
};
/* /*
* tomoyo_name_list is used for holding string data used by TOMOYO. * tomoyo_name_list is used for holding string data used by TOMOYO.
* Since same string data is likely used for multiple times (e.g. * Since same string data is likely used for multiple times (e.g.
@ -314,52 +307,32 @@ static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
* @name: The string to store into the permernent memory. * @name: The string to store into the permernent memory.
* *
* Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
*
* The RAM is shared, so NEVER try to modify or kfree() the returned name.
*/ */
const struct tomoyo_path_info *tomoyo_save_name(const char *name) const struct tomoyo_path_info *tomoyo_save_name(const char *name)
{ {
static LIST_HEAD(fmb_list);
static DEFINE_MUTEX(lock); static DEFINE_MUTEX(lock);
struct tomoyo_name_entry *ptr; struct tomoyo_name_entry *ptr;
unsigned int hash; unsigned int hash;
/* fmb contains available size in bytes.
fmb is removed from the fmb_list when fmb->len becomes 0. */
struct tomoyo_free_memory_block_list *fmb;
int len; int len;
char *cp; int allocated_len;
struct list_head *head; struct list_head *head;
if (!name) if (!name)
return NULL; return NULL;
len = strlen(name) + 1; len = strlen(name) + 1;
if (len > TOMOYO_MAX_PATHNAME_LEN) {
printk(KERN_WARNING "ERROR: Name too long "
"for tomoyo_save_name().\n");
return NULL;
}
hash = full_name_hash((const unsigned char *) name, len - 1); hash = full_name_hash((const unsigned char *) name, len - 1);
head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)]; head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
mutex_lock(&lock); mutex_lock(&lock);
list_for_each_entry(ptr, head, list) { list_for_each_entry(ptr, head, list) {
if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name)) if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
goto out; goto out;
} }
list_for_each_entry(fmb, &fmb_list, list) { ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL);
if (len <= fmb->len) allocated_len = ptr ? ksize(ptr) : 0;
goto ready; if (!ptr || (tomoyo_quota_for_savename &&
} tomoyo_allocated_memory_for_savename + allocated_len
if (!tomoyo_quota_for_savename || > tomoyo_quota_for_savename)) {
tomoyo_allocated_memory_for_savename + PATH_MAX kfree(ptr);
<= tomoyo_quota_for_savename)
cp = kzalloc(PATH_MAX, GFP_KERNEL);
else
cp = NULL;
fmb = kzalloc(sizeof(*fmb), GFP_KERNEL);
if (!cp || !fmb) {
kfree(cp);
kfree(fmb);
printk(KERN_WARNING "ERROR: Out of memory " printk(KERN_WARNING "ERROR: Out of memory "
"for tomoyo_save_name().\n"); "for tomoyo_save_name().\n");
if (!tomoyo_policy_loaded) if (!tomoyo_policy_loaded)
@ -367,24 +340,11 @@ const struct tomoyo_path_info *tomoyo_save_name(const char *name)
ptr = NULL; ptr = NULL;
goto out; goto out;
} }
tomoyo_allocated_memory_for_savename += PATH_MAX; tomoyo_allocated_memory_for_savename += allocated_len;
list_add(&fmb->list, &fmb_list); ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
fmb->ptr = cp; memmove((char *) ptr->entry.name, name, len);
fmb->len = PATH_MAX;
ready:
ptr = tomoyo_alloc_element(sizeof(*ptr));
if (!ptr)
goto out;
ptr->entry.name = fmb->ptr;
memmove(fmb->ptr, name, len);
tomoyo_fill_path_info(&ptr->entry); tomoyo_fill_path_info(&ptr->entry);
fmb->ptr += len;
fmb->len -= len;
list_add_tail(&ptr->list, head); list_add_tail(&ptr->list, head);
if (fmb->len == 0) {
list_del(&fmb->list);
kfree(fmb);
}
out: out:
mutex_unlock(&lock); mutex_unlock(&lock);
return ptr ? &ptr->entry : NULL; return ptr ? &ptr->entry : NULL;