Skip to content

Commit f0a31b2

Browse files
joelagnelFrederic Weisbecker
authored andcommitted
srcu: Fix error handling in init_srcu_struct_fields()
The current error handling in init_srcu_struct_fields() is a bit inconsistent. If init_srcu_struct_nodes() fails, the function either returns -ENOMEM or 0 depending on whether ssp->sda_is_static is true or false. This can make init_srcu_struct_fields() return 0 even if memory allocation failed! Simplify the error handling by always returning -ENOMEM if either init_srcu_struct_nodes() or the per-CPU allocation fails. This makes the control flow easier to follow and avoids the inconsistent return values. Add goto labels to avoid duplicating the error cleanup code. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joel Fernandes (Google) <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]>
1 parent ff18cb8 commit f0a31b2

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

kernel/rcu/srcutree.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,29 +255,31 @@ static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
255255
ssp->srcu_sup->sda_is_static = is_static;
256256
if (!is_static)
257257
ssp->sda = alloc_percpu(struct srcu_data);
258-
if (!ssp->sda) {
259-
if (!is_static)
260-
kfree(ssp->srcu_sup);
261-
return -ENOMEM;
262-
}
258+
if (!ssp->sda)
259+
goto err_free_sup;
263260
init_srcu_struct_data(ssp);
264261
ssp->srcu_sup->srcu_gp_seq_needed_exp = 0;
265262
ssp->srcu_sup->srcu_last_gp_end = ktime_get_mono_fast_ns();
266263
if (READ_ONCE(ssp->srcu_sup->srcu_size_state) == SRCU_SIZE_SMALL && SRCU_SIZING_IS_INIT()) {
267-
if (!init_srcu_struct_nodes(ssp, GFP_ATOMIC)) {
268-
if (!ssp->srcu_sup->sda_is_static) {
269-
free_percpu(ssp->sda);
270-
ssp->sda = NULL;
271-
kfree(ssp->srcu_sup);
272-
return -ENOMEM;
273-
}
274-
} else {
275-
WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_BIG);
276-
}
264+
if (!init_srcu_struct_nodes(ssp, GFP_ATOMIC))
265+
goto err_free_sda;
266+
WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_BIG);
277267
}
278268
ssp->srcu_sup->srcu_ssp = ssp;
279269
smp_store_release(&ssp->srcu_sup->srcu_gp_seq_needed, 0); /* Init done. */
280270
return 0;
271+
272+
err_free_sda:
273+
if (!is_static) {
274+
free_percpu(ssp->sda);
275+
ssp->sda = NULL;
276+
}
277+
err_free_sup:
278+
if (!is_static) {
279+
kfree(ssp->srcu_sup);
280+
ssp->srcu_sup = NULL;
281+
}
282+
return -ENOMEM;
281283
}
282284

283285
#ifdef CONFIG_DEBUG_LOCK_ALLOC

0 commit comments

Comments
 (0)