Skip to content

Commit c629808

Browse files
Chen Ridonggregkh
authored andcommitted
padata: add pd get/put refcnt helper
[ Upstream commit ae15420 ] Add helpers for pd to get/put refcnt to make code consice. Signed-off-by: Chen Ridong <[email protected]> Acked-by: Daniel Jordan <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Stable-dep-of: dd7d37c ("padata: avoid UAF for reorder_work") Signed-off-by: Sasha Levin <[email protected]>
1 parent 573ac9c commit c629808

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

kernel/padata.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ struct padata_mt_job_state {
4747
static void padata_free_pd(struct parallel_data *pd);
4848
static void __init padata_mt_helper(struct work_struct *work);
4949

50+
static inline void padata_get_pd(struct parallel_data *pd)
51+
{
52+
refcount_inc(&pd->refcnt);
53+
}
54+
55+
static inline void padata_put_pd_cnt(struct parallel_data *pd, int cnt)
56+
{
57+
if (refcount_sub_and_test(cnt, &pd->refcnt))
58+
padata_free_pd(pd);
59+
}
60+
61+
static inline void padata_put_pd(struct parallel_data *pd)
62+
{
63+
padata_put_pd_cnt(pd, 1);
64+
}
65+
5066
static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
5167
{
5268
int cpu, target_cpu;
@@ -206,7 +222,7 @@ int padata_do_parallel(struct padata_shell *ps,
206222
if ((pinst->flags & PADATA_RESET))
207223
goto out;
208224

209-
refcount_inc(&pd->refcnt);
225+
padata_get_pd(pd);
210226
padata->pd = pd;
211227
padata->cb_cpu = *cb_cpu;
212228

@@ -380,8 +396,7 @@ static void padata_serial_worker(struct work_struct *serial_work)
380396
}
381397
local_bh_enable();
382398

383-
if (refcount_sub_and_test(cnt, &pd->refcnt))
384-
padata_free_pd(pd);
399+
padata_put_pd_cnt(pd, cnt);
385400
}
386401

387402
/**
@@ -688,8 +703,7 @@ static int padata_replace(struct padata_instance *pinst)
688703
synchronize_rcu();
689704

690705
list_for_each_entry_continue_reverse(ps, &pinst->pslist, list)
691-
if (refcount_dec_and_test(&ps->opd->refcnt))
692-
padata_free_pd(ps->opd);
706+
padata_put_pd(ps->opd);
693707

694708
pinst->flags &= ~PADATA_RESET;
695709

@@ -1137,8 +1151,7 @@ void padata_free_shell(struct padata_shell *ps)
11371151
mutex_lock(&ps->pinst->lock);
11381152
list_del(&ps->list);
11391153
pd = rcu_dereference_protected(ps->pd, 1);
1140-
if (refcount_dec_and_test(&pd->refcnt))
1141-
padata_free_pd(pd);
1154+
padata_put_pd(pd);
11421155
mutex_unlock(&ps->pinst->lock);
11431156

11441157
kfree(ps);

0 commit comments

Comments
 (0)