sdcardfs: Switch package list to RCU
authorDaniel Rosenberg <drosen@google.com>
Tue, 10 May 2016 20:42:43 +0000 (13:42 -0700)
committerStricted <info@stricted.net>
Thu, 11 Oct 2018 16:03:17 +0000 (18:03 +0200)
Switched the package id hashmap to use RCU.

Change-Id: I9fdcab279009005bf28536247d11e13babab0b93
Signed-off-by: Daniel Rosenberg <drosen@google.com>
fs/sdcardfs/derived_perm.c
fs/sdcardfs/packagelist.c
fs/sdcardfs/sdcardfs.h

index b0278964e535b95e15e20b4baf05604c2930731d..68837e724af42cd4e2035935bd1fc8bae1768fcc 100644 (file)
@@ -47,7 +47,6 @@ void setup_derived_state(struct inode *inode, perm_t perm,
 /* While renaming, there is a point where we want the path from dentry, but the name from newdentry */
 void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry)
 {
-       struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
        struct sdcardfs_inode_info *info = SDCARDFS_I(dentry->d_inode);
        struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
        appid_t appid;
@@ -96,7 +95,7 @@ void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, st
                case PERM_ANDROID_DATA:
                case PERM_ANDROID_OBB:
                case PERM_ANDROID_MEDIA:
-                       appid = get_appid(sbi->pkgl_id, newdentry->d_name.name);
+                       appid = get_appid(newdentry->d_name.name);
                        if (appid != 0) {
                                info->d_uid = multiuser_get_uid(parent_info->userid, appid);
                        }
index 0cf9c345d05e040c3a908bd7d319b644fd995fb5..608a4b50ccd82a5d78e64a362062332243110da9 100644 (file)
 
 #include <linux/configfs.h>
 
-#define STRING_BUF_SIZE                (512)
-
 struct hashtable_entry {
        struct hlist_node hlist;
-       void *key;
-       unsigned int value;
-};
-
-struct sb_list {
-       struct super_block *sb;
-       struct list_head list;
+       const char *key;
+       atomic_t value;
 };
 
-struct packagelist_data {
-       DECLARE_HASHTABLE(package_to_appid,8);
-       struct mutex hashtable_lock;
-
-};
-
-static struct packagelist_data *pkgl_data_all;
+static DEFINE_HASHTABLE(package_to_appid, 8);
 
 static struct kmem_cache *hashtable_entry_cachep;
 
@@ -64,22 +51,21 @@ static unsigned int str_hash(const char *key) {
        return h;
 }
 
-appid_t get_appid(void *pkgl_id, const char *app_name)
+appid_t get_appid(const char *app_name)
 {
-       struct packagelist_data *pkgl_dat = pkgl_data_all;
        struct hashtable_entry *hash_cur;
        unsigned int hash = str_hash(app_name);
        appid_t ret_id;
 
-       mutex_lock(&pkgl_dat->hashtable_lock);
-       hash_for_each_possible(pkgl_dat->package_to_appid, hash_cur, hlist, hash) {
+       rcu_read_lock();
+       hash_for_each_possible_rcu(package_to_appid, hash_cur, hlist, hash) {
                if (!strcasecmp(app_name, hash_cur->key)) {
-                       ret_id = (appid_t)hash_cur->value;
-                       mutex_unlock(&pkgl_dat->hashtable_lock);
+                       ret_id = atomic_read(&hash_cur->value);
+                       rcu_read_unlock();
                        return ret_id;
                }
        }
-       mutex_unlock(&pkgl_dat->hashtable_lock);
+       rcu_read_unlock();
        return 0;
 }
 
@@ -120,116 +106,118 @@ int open_flags_to_access_mode(int open_flags) {
        }
 }
 
-static int insert_str_to_int_lock(struct packagelist_data *pkgl_dat, char *key,
-               unsigned int value)
+static struct hashtable_entry *alloc_packagelist_entry(const char *key,
+               appid_t value)
+{
+       struct hashtable_entry *ret = kmem_cache_alloc(hashtable_entry_cachep,
+                       GFP_KERNEL);
+       if (!ret)
+               return NULL;
+
+       ret->key = kstrdup(key, GFP_KERNEL);
+       if (!ret->key) {
+               kmem_cache_free(hashtable_entry_cachep, ret);
+               return NULL;
+       }
+
+       atomic_set(&ret->value, value);
+       return ret;
+}
+
+static int insert_packagelist_entry_locked(const char *key, appid_t value)
 {
        struct hashtable_entry *hash_cur;
        struct hashtable_entry *new_entry;
        unsigned int hash = str_hash(key);
 
-       hash_for_each_possible(pkgl_dat->package_to_appid, hash_cur, hlist, hash) {
+       hash_for_each_possible_rcu(package_to_appid, hash_cur, hlist, hash) {
                if (!strcasecmp(key, hash_cur->key)) {
-                       hash_cur->value = value;
+                       atomic_set(&hash_cur->value, value);
                        return 0;
                }
        }
-       new_entry = kmem_cache_alloc(hashtable_entry_cachep, GFP_KERNEL);
+       new_entry = alloc_packagelist_entry(key, value);
        if (!new_entry)
                return -ENOMEM;
-       new_entry->key = kstrdup(key, GFP_KERNEL);
-       new_entry->value = value;
-       hash_add(pkgl_dat->package_to_appid, &new_entry->hlist, hash);
+       hash_add_rcu(package_to_appid, &new_entry->hlist, hash);
        return 0;
 }
 
 static void fixup_perms(struct super_block *sb) {
        if (sb && sb->s_magic == SDCARDFS_SUPER_MAGIC) {
-               mutex_lock(&sb->s_root->d_inode->i_mutex);
                get_derive_permissions_recursive(sb->s_root);
-               mutex_unlock(&sb->s_root->d_inode->i_mutex);
        }
 }
 
-static int insert_str_to_int(struct packagelist_data *pkgl_dat, char *key,
-               unsigned int value) {
-       int ret;
+static void fixup_all_perms(void)
+{
        struct sdcardfs_sb_info *sbinfo;
-       mutex_lock(&sdcardfs_super_list_lock);
-       mutex_lock(&pkgl_dat->hashtable_lock);
-       ret = insert_str_to_int_lock(pkgl_dat, key, value);
-       mutex_unlock(&pkgl_dat->hashtable_lock);
-
-       list_for_each_entry(sbinfo, &sdcardfs_super_list, list) {
-               if (sbinfo) {
+       list_for_each_entry(sbinfo, &sdcardfs_super_list, list)
+               if (sbinfo)
                        fixup_perms(sbinfo->sb);
-               }
-       }
+}
+
+static int insert_packagelist_entry(const char *key, appid_t value)
+{
+       int err;
+
+       mutex_lock(&sdcardfs_super_list_lock);
+       err = insert_packagelist_entry_locked(key, value);
+       if (!err)
+               fixup_all_perms();
        mutex_unlock(&sdcardfs_super_list_lock);
-       return ret;
+
+       return err;
 }
 
-static void remove_str_to_int_lock(struct hashtable_entry *h_entry) {
-       kfree(h_entry->key);
-       hash_del(&h_entry->hlist);
-       kmem_cache_free(hashtable_entry_cachep, h_entry);
+static void free_packagelist_entry(struct hashtable_entry *entry)
+{
+       kfree(entry->key);
+       hash_del_rcu(&entry->hlist);
+       kmem_cache_free(hashtable_entry_cachep, entry);
 }
 
-static void remove_str_to_int(struct packagelist_data *pkgl_dat, const char *key)
+static void remove_packagelist_entry_locked(const char *key)
 {
-       struct sdcardfs_sb_info *sbinfo;
        struct hashtable_entry *hash_cur;
        unsigned int hash = str_hash(key);
-       mutex_lock(&sdcardfs_super_list_lock);
-       mutex_lock(&pkgl_dat->hashtable_lock);
-       hash_for_each_possible(pkgl_dat->package_to_appid, hash_cur, hlist, hash) {
+
+       hash_for_each_possible_rcu(package_to_appid, hash_cur, hlist, hash) {
                if (!strcasecmp(key, hash_cur->key)) {
-                       remove_str_to_int_lock(hash_cur);
-                       break;
-               }
-       }
-       mutex_unlock(&pkgl_dat->hashtable_lock);
-       list_for_each_entry(sbinfo, &sdcardfs_super_list, list) {
-               if (sbinfo) {
-                       fixup_perms(sbinfo->sb);
+                       hash_del_rcu(&hash_cur->hlist);
+                       synchronize_rcu();
+                       free_packagelist_entry(hash_cur);
+                       return;
                }
        }
+}
+
+static void remove_packagelist_entry(const char *key)
+{
+       mutex_lock(&sdcardfs_super_list_lock);
+       remove_packagelist_entry_locked(key);
+       fixup_all_perms();
        mutex_unlock(&sdcardfs_super_list_lock);
        return;
 }
 
-static void remove_all_hashentrys(struct packagelist_data *pkgl_dat)
+static void packagelist_destroy(void)
 {
        struct hashtable_entry *hash_cur;
        struct hlist_node *h_t;
+       HLIST_HEAD(free_list);
        int i;
-       mutex_lock(&pkgl_dat->hashtable_lock);
-       hash_for_each_safe(pkgl_dat->package_to_appid, i, h_t, hash_cur, hlist)
-               remove_str_to_int_lock(hash_cur);
-       mutex_unlock(&pkgl_dat->hashtable_lock);
-       hash_init(pkgl_dat->package_to_appid);
-}
-
-static struct packagelist_data * packagelist_create(void)
-{
-       struct packagelist_data *pkgl_dat;
+       mutex_lock(&sdcardfs_super_list_lock);
+       hash_for_each_rcu(package_to_appid, i, hash_cur, hlist) {
+               hash_del_rcu(&hash_cur->hlist);
+               hlist_add_head(&hash_cur->hlist, &free_list);
 
-       pkgl_dat = kmalloc(sizeof(*pkgl_dat), GFP_KERNEL | __GFP_ZERO);
-       if (!pkgl_dat) {
-                printk(KERN_ERR "sdcardfs: Failed to create hash\n");
-               return ERR_PTR(-ENOMEM);
        }
-
-       mutex_init(&pkgl_dat->hashtable_lock);
-       hash_init(pkgl_dat->package_to_appid);
-
-       return pkgl_dat;
-}
-
-static void packagelist_destroy(struct packagelist_data *pkgl_dat)
-{
-       remove_all_hashentrys(pkgl_dat);
+       synchronize_rcu();
+       hlist_for_each_entry_safe(hash_cur, h_t, &free_list, hlist)
+               free_packagelist_entry(hash_cur);
+       mutex_unlock(&sdcardfs_super_list_lock);
        printk(KERN_INFO "sdcardfs: destroyed packagelist pkgld\n");
-       kfree(pkgl_dat);
 }
 
 struct package_appid {
@@ -257,9 +245,7 @@ static ssize_t package_appid_attr_show(struct config_item *item,
                                      struct configfs_attribute *attr,
                                      char *page)
 {
-       ssize_t count;
-       count = sprintf(page, "%d\n", get_appid(pkgl_data_all, item->ci_name));
-       return count;
+       return scnprintf(page, PAGE_SIZE, "%u\n", get_appid(item->ci_name));
 }
 
 static ssize_t package_appid_attr_store(struct config_item *item,
@@ -267,17 +253,14 @@ static ssize_t package_appid_attr_store(struct config_item *item,
                                       const char *page, size_t count)
 {
        struct package_appid *package_appid = to_package_appid(item);
-       unsigned long tmp;
-       char *p = (char *) page;
+       unsigned int tmp;
        int ret;
 
-       tmp = simple_strtoul(p, &p, 10);
-       if (!p || (*p && (*p != '\n')))
-               return -EINVAL;
+       ret = kstrtouint(page, 10, &tmp);
+       if (ret)
+               return ret;
 
-       if (tmp > INT_MAX)
-               return -ERANGE;
-       ret = insert_str_to_int(pkgl_data_all, item->ci_name, (unsigned int)tmp);
+       ret = insert_packagelist_entry(item->ci_name, tmp);
        package_appid->add_pid = tmp;
        if (ret)
                return ret;
@@ -289,7 +272,7 @@ static void package_appid_release(struct config_item *item)
 {
        printk(KERN_INFO "sdcardfs: removing %s\n", item->ci_dentry->d_name.name);
        /* item->ci_name is freed already, so we rely on the dentry */
-       remove_str_to_int(pkgl_data_all, item->ci_dentry->d_name.name);
+       remove_packagelist_entry(item->ci_dentry->d_name.name);
        kfree(to_package_appid(item));
 }
 
@@ -347,21 +330,21 @@ static ssize_t packages_attr_show(struct config_item *item,
                                         char *page)
 {
        struct hashtable_entry *hash_cur;
-       struct hlist_node *h_t;
        int i;
        int count = 0, written = 0;
-       char errormsg[] = "<truncated>\n";
+       const char errormsg[] = "<truncated>\n";
 
-       mutex_lock(&pkgl_data_all->hashtable_lock);
-       hash_for_each_safe(pkgl_data_all->package_to_appid, i, h_t, hash_cur, hlist) {
-               written = scnprintf(page + count, PAGE_SIZE - sizeof(errormsg) - count, "%s %d\n", (char *)hash_cur->key, hash_cur->value);
+       rcu_read_lock();
+       hash_for_each_rcu(package_to_appid, i, hash_cur, hlist) {
+               written = scnprintf(page + count, PAGE_SIZE - sizeof(errormsg) - count, "%s %d\n",
+                                       (const char *)hash_cur->key, atomic_read(&hash_cur->value));
                if (count + written == PAGE_SIZE - sizeof(errormsg)) {
                        count += scnprintf(page + count, PAGE_SIZE - count, errormsg);
                        break;
                }
                count += written;
        }
-       mutex_unlock(&pkgl_data_all->hashtable_lock);
+       rcu_read_unlock();
 
        return count;
 }
@@ -433,7 +416,6 @@ int packagelist_init(void)
                return -ENOMEM;
        }
 
-       pkgl_data_all = packagelist_create();
        configfs_sdcardfs_init();
         return 0;
 }
@@ -441,7 +423,7 @@ int packagelist_init(void)
 void packagelist_exit(void)
 {
        configfs_sdcardfs_exit();
-       packagelist_destroy(pkgl_data_all);
+       packagelist_destroy();
        if (hashtable_entry_cachep)
                kmem_cache_destroy(hashtable_entry_cachep);
 }
index 7f3dee3b1f1d38ac892f78a6549b466058e8abea..48de31b5a53c985ad2476c7b0575da9ae4fd3ed8 100644 (file)
@@ -396,7 +396,7 @@ extern struct mutex sdcardfs_super_list_lock;
 extern struct list_head sdcardfs_super_list;
 
 /* for packagelist.c */
-extern appid_t get_appid(void *pkgl_id, const char *app_name);
+extern appid_t get_appid(const char *app_name);
 extern int check_caller_access_to_name(struct inode *parent_node, const char* name);
 extern int open_flags_to_access_mode(int open_flags);
 extern int packagelist_init(void);