From: Wang Wensheng <wangwensheng4@huawei.com> Offering: HULK hulk inclusion category: feature bugzilla: NA ---------------------------------------- Only sp_mapping of type SP_MAPPING_DVPP need some extra operations because this type of mapping is specified to a group. All the other mappings are global and are created when system starts and will not destroy. Now the general mapping operations do nothing meaningful for normal global mapping. So don't operate on normal mapping and rename those functions with prefix dvpp_mapping_ to simplify those operations. No logic change. Signed-off-by: Wang Wensheng <wangwensheng4@huawei.com> --- mm/share_pool.c | 128 ++++++++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 74 deletions(-) diff --git a/mm/share_pool.c b/mm/share_pool.c index 793111b3325c..1e417a117838 100644 --- a/mm/share_pool.c +++ b/mm/share_pool.c @@ -121,7 +121,6 @@ enum sp_mapping_type { */ struct sp_mapping { unsigned long type; - atomic_t user; unsigned long start[MAX_DEVID]; unsigned long end[MAX_DEVID]; struct rb_root area_root; @@ -129,11 +128,13 @@ struct sp_mapping { struct rb_node *free_area_cache; unsigned long cached_hole_size; unsigned long cached_vstart; + spinlock_t sp_mapping_lock; - /* list head for all groups attached to this mapping, dvpp mapping only */ - struct list_head group_head; + /* the following three element used for non-global dvpp mapping only */ + atomic_t user; + /* list head for all groups attached to this mapping */ + struct list_head dvpp_group_head; struct list_head spm_node; - spinlock_t sp_mapping_lock; }; /* Processes in the same sp_group can share memory. @@ -175,8 +176,8 @@ struct sp_group { /* protect the group internal elements */ struct rw_semaphore rw_lock; /* list node for dvpp mapping */ - struct list_head mnode; - struct sp_mapping *mapping[SP_MAPPING_END]; + struct list_head dvpp_mnode; + struct sp_mapping *dvpp_mapping; }; /* a per-process(per mm) struct which manages a sp_group_node list */ @@ -303,22 +304,6 @@ static void sp_mapping_set_type(struct sp_mapping *spm, unsigned long type) static struct sp_mapping *sp_mapping_normal; static struct sp_mapping *sp_mapping_ro; -static void sp_mapping_add_to_list(struct sp_mapping *spm) -{ - mutex_lock(&spm_list_lock); - if (sp_mapping_type(spm) == SP_MAPPING_DVPP) - list_add_tail(&spm->spm_node, &spm_dvpp_list); - mutex_unlock(&spm_list_lock); -} - -static void sp_mapping_remove_from_list(struct sp_mapping *spm) -{ - mutex_lock(&spm_list_lock); - if (sp_mapping_type(spm) == SP_MAPPING_DVPP) - list_del(&spm->spm_node); - mutex_unlock(&spm_list_lock); -} - static void sp_mapping_range_init(struct sp_mapping *spm) { int i; @@ -354,62 +339,57 @@ static struct sp_mapping *sp_mapping_create(unsigned long type) sp_mapping_set_type(spm, type); sp_mapping_range_init(spm); - atomic_set(&spm->user, 0); spm->area_root = RB_ROOT; - INIT_LIST_HEAD(&spm->group_head); spin_lock_init(&spm->sp_mapping_lock); - sp_mapping_add_to_list(spm); return spm; } -static void sp_mapping_destroy(struct sp_mapping *spm) +static void dvpp_mapping_destroy(struct sp_mapping *spm) { - sp_mapping_remove_from_list(spm); + mutex_lock(&spm_list_lock); + list_del(&spm->spm_node); + mutex_unlock(&spm_list_lock); + kfree(spm); } -static void sp_mapping_attach(struct sp_group *spg, struct sp_mapping *spm) +static void dvpp_mapping_attach(struct sp_group *spg, struct sp_mapping *spm) { - unsigned long type = sp_mapping_type(spm); - atomic_inc(&spm->user); - spg->mapping[type] = spm; - if (type == SP_MAPPING_DVPP) - list_add_tail(&spg->mnode, &spm->group_head); + spg->dvpp_mapping = spm; + list_add_tail(&spg->dvpp_mnode, &spm->dvpp_group_head); } -static void sp_mapping_detach(struct sp_group *spg, struct sp_mapping *spm) +static void dvpp_mapping_detach(struct sp_group *spg) { - unsigned long type; + struct sp_mapping *spm = spg->dvpp_mapping; if (!spm) return; - type = sp_mapping_type(spm); - if (type == SP_MAPPING_DVPP) - list_del(&spg->mnode); + list_del(&spg->dvpp_mnode); if (atomic_dec_and_test(&spm->user)) - sp_mapping_destroy(spm); + dvpp_mapping_destroy(spm); - spg->mapping[type] = NULL; + spg->dvpp_mapping = NULL; } /* merge old mapping to new, and the old mapping would be destroyed */ -static void sp_mapping_merge(struct sp_mapping *new, struct sp_mapping *old) +static void dvpp_mapping_merge(struct sp_mapping *new, struct sp_mapping *old) { struct sp_group *spg, *tmp; if (new == old) return; - list_for_each_entry_safe(spg, tmp, &old->group_head, mnode) { - list_move_tail(&spg->mnode, &new->group_head); - spg->mapping[SP_MAPPING_DVPP] = new; + list_for_each_entry_safe(spg, tmp, &old->dvpp_group_head, dvpp_mnode) { + list_move_tail(&spg->dvpp_mnode, &new->dvpp_group_head); + spg->dvpp_mapping = new; } atomic_add(atomic_read(&old->user), &new->user); - sp_mapping_destroy(old); + dvpp_mapping_destroy(old); } static bool is_mapping_empty(struct sp_mapping *spm) @@ -437,12 +417,12 @@ static bool can_mappings_merge(struct sp_mapping *m1, struct sp_mapping *m2) * the caller must hold sp_global_sem * NOTE: undo the mergeing when the later process failed. */ -static int sp_group_setup_mapping_normal(struct mm_struct *mm, struct sp_group *spg) +static int sp_group_setup_dvpp_mapping_normal(struct mm_struct *mm, struct sp_group *spg) { struct sp_mapping *local_dvpp_mapping, *spg_dvpp_mapping; - local_dvpp_mapping = mm->sp_group_master->local->mapping[SP_MAPPING_DVPP]; - spg_dvpp_mapping = spg->mapping[SP_MAPPING_DVPP]; + local_dvpp_mapping = mm->sp_group_master->local->dvpp_mapping; + spg_dvpp_mapping = spg->dvpp_mapping; if (!list_empty(&spg->proc_head)) { /* @@ -454,12 +434,12 @@ static int sp_group_setup_mapping_normal(struct mm_struct *mm, struct sp_group * bool is_conflict = !can_mappings_merge(local_dvpp_mapping, spg_dvpp_mapping); if (is_mapping_empty(local_dvpp_mapping)) { - sp_mapping_merge(spg_dvpp_mapping, local_dvpp_mapping); + dvpp_mapping_merge(spg_dvpp_mapping, local_dvpp_mapping); if (is_conflict) pr_warn_ratelimited("task address space conflict, spg_id=%d\n", spg->id); } else if (is_mapping_empty(spg_dvpp_mapping)) { - sp_mapping_merge(local_dvpp_mapping, spg_dvpp_mapping); + dvpp_mapping_merge(local_dvpp_mapping, spg_dvpp_mapping); if (is_conflict) pr_warn_ratelimited("group address space conflict, spg_id=%d\n", spg->id); @@ -469,17 +449,23 @@ static int sp_group_setup_mapping_normal(struct mm_struct *mm, struct sp_group * } } else { /* the mapping of local group is always set */ - sp_mapping_attach(spg, local_dvpp_mapping); - if (!spg->mapping[SP_MAPPING_NORMAL]) - sp_mapping_attach(spg, sp_mapping_normal); - if (!spg->mapping[SP_MAPPING_RO]) - sp_mapping_attach(spg, sp_mapping_ro); + dvpp_mapping_attach(spg, local_dvpp_mapping); } return 0; } -static int sp_group_setup_mapping_local(struct mm_struct *mm, struct sp_group *local) +static void dvpp_mapping_init(struct sp_mapping *spm) +{ + atomic_set(&spm->user, 0); + INIT_LIST_HEAD(&spm->dvpp_group_head); + + mutex_lock(&spm_list_lock); + list_add_tail(&spm->spm_node, &spm_dvpp_list); + mutex_unlock(&spm_list_lock); +} + +static int sp_group_setup_dvpp_mapping_local(struct mm_struct *mm, struct sp_group *local) { struct sp_mapping *spm; @@ -487,9 +473,8 @@ static int sp_group_setup_mapping_local(struct mm_struct *mm, struct sp_group *l if (!spm) return -ENOMEM; - sp_mapping_attach(local, spm); - sp_mapping_attach(local, sp_mapping_normal); - sp_mapping_attach(local, sp_mapping_ro); + dvpp_mapping_init(spm); + dvpp_mapping_attach(local, spm); return 0; } @@ -499,12 +484,12 @@ static inline bool is_local_group(int spg_id) return spg_id >= SPG_ID_LOCAL_MIN && spg_id <= SPG_ID_LOCAL_MAX; } -static int sp_group_setup_mapping(struct mm_struct *mm, struct sp_group *spg) +static int sp_group_setup_dvpp_mapping(struct mm_struct *mm, struct sp_group *spg) { if (is_local_group(spg->id)) - return sp_group_setup_mapping_local(mm, spg); + return sp_group_setup_dvpp_mapping_local(mm, spg); else - return sp_group_setup_mapping_normal(mm, spg); + return sp_group_setup_dvpp_mapping_normal(mm, spg); } static struct sp_group *sp_group_create(int spg_id); @@ -865,14 +850,11 @@ struct sp_k2u_context { static void free_sp_group_locked(struct sp_group *spg) { - int type; - fput(spg->file); fput(spg->file_hugetlb); idr_remove(&sp_group_idr, spg->id); - for (type = SP_MAPPING_START; type < SP_MAPPING_END; type++) - sp_mapping_detach(spg, spg->mapping[type]); + dvpp_mapping_detach(spg); if (!is_local_group(spg->id)) system_group_count--; @@ -1065,7 +1047,7 @@ static void sp_group_init(struct sp_group *spg, int spg_id) atomic_set(&spg->use_count, 1); atomic_set(&spg->spa_num, 0); INIT_LIST_HEAD(&spg->proc_head); - INIT_LIST_HEAD(&spg->mnode); + INIT_LIST_HEAD(&spg->dvpp_mnode); init_rwsem(&spg->rw_lock); meminfo_init(&spg->meminfo); } @@ -1225,7 +1207,7 @@ static int sp_group_link_task(struct mm_struct *mm, struct sp_group *spg, if (!node) return -ENOMEM; - ret = sp_group_setup_mapping(mm, spg); + ret = sp_group_setup_dvpp_mapping(mm, spg); if (ret) goto out_kfree; @@ -1670,11 +1652,11 @@ static struct sp_area *sp_area_alloc(unsigned long size, unsigned long flags, pr_err("invalid sp_flags [%lx]\n", flags); return ERR_PTR(-EINVAL); } - mapping = spg->mapping[SP_MAPPING_RO]; + mapping = sp_mapping_ro; } else if (flags & SP_DVPP) { - mapping = spg->mapping[SP_MAPPING_DVPP]; + mapping = spg->dvpp_mapping; } else { - mapping = spg->mapping[SP_MAPPING_NORMAL]; + mapping = sp_mapping_normal; } if (!mapping) { @@ -3276,7 +3258,7 @@ bool mg_sp_config_dvpp_range(size_t start, size_t size, int device_id, int tgid) if (IS_ERR(spg)) goto put_mm; - spm = spg->mapping[SP_MAPPING_DVPP]; + spm = spg->dvpp_mapping; default_start = MMAP_SHARE_POOL_DVPP_START + device_id * MMAP_SHARE_POOL_16G_SIZE; /* The dvpp range of each group can be configured only once */ if (spm->start[device_id] != default_start) @@ -3713,12 +3695,10 @@ static int __init share_pool_init(void) sp_mapping_normal = sp_mapping_create(SP_MAPPING_NORMAL); if (!sp_mapping_normal) goto fail; - atomic_inc(&sp_mapping_normal->user); sp_mapping_ro = sp_mapping_create(SP_MAPPING_RO); if (!sp_mapping_ro) goto free_normal; - atomic_inc(&sp_mapping_ro->user); proc_sharepool_init(); -- 2.43.0