net/sched: act_gate: snapshot parameters with RCU on replace
Subsystems (4)
v7 cover net/sched: act_gate: snapshot parameters with RCU on replace
This series fixes a schedule lifetime race in act_gate between the control-plane replace path and the running hrtimer callback (gate_timer_func).
Changes since v6:
- revert gate_start_timer()
- added explicit chain_put: label before release_idr:
- kzalloc failure path changed from goto release_idr to goto chain_put
- formatting only: blankline cleanup for readability, no behavior change
Paul Moses (1):
net/sched: act_gate: snapshot parameters with RCU on replace
include/net/tc_act/tc_gate.h | 33 ++++- net/sched/act_gate.c | 265 +++++++++++++++++++++++++---------- 2 files changed, 215 insertions(+), 83 deletions(-)
Signature · 1 line
2.53.0
PATCH v7 1/1 net/sched: act_gate: snapshot parameters with RCU on replace
The gate action can be replaced while the hrtimer callback or dump path is walking the schedule list.
Convert the parameters to an RCU-protected snapshot and swap updates under tcf_lock, freeing the previous snapshot via call_rcu(). When REPLACE omits the entry list, preserve the existing schedule so the effective state is unchanged.
--- include/net/tc_act/tc_gate.h | 33 ++++- net/sched/act_gate.c | 265 +++++++++++++++++++++++++---------- 2 files changed, 215 insertions(+), 83 deletions(-)
include/net/tc_act/tc_gate.h +26 -7
@@ -32,6 +32,7 @@ struct tcf_gate_params {s32 tcfg_clockid;size_t num_entries;struct list_head entries; +struct rcu_head rcu; };#define GATE_ACT_GATE_OPEN BIT(0)
@@ -39,7 +40,7 @@ struct tcf_gate_params {struct tcf_gate {struct tc_action common; -struct tcf_gate_params param; +struct tcf_gate_params __rcu *param;u8 current_gate_status;ktime_t current_close_time;u32 current_entry_octets;
@@ -51,47 +52,65 @@ struct tcf_gate {#define to_gate (a) ((struct tcf_gate *)a) +static inline struct tcf_gate_params *tcf_gate_params_locked (const struct tc_action *a) +{ +struct tcf_gate *gact =to_gate (a); + +return rcu_dereference_protected (gact->param, +lockdep_is_held (&gact->tcf_lock)); +} +static inline s32 tcf_gate_prio (const struct tc_action *a) { +struct tcf_gate_params *p;s32 tcfg_prio; - tcfg_prio =to_gate (a)->param.tcfg_priority; + p =tcf_gate_params_locked (a); + tcfg_prio = p->tcfg_priority;return tcfg_prio; }static inline u64 tcf_gate_basetime (const struct tc_action *a) { +struct tcf_gate_params *p;u64 tcfg_basetime; - tcfg_basetime =to_gate (a)->param.tcfg_basetime; + p =tcf_gate_params_locked (a); + tcfg_basetime = p->tcfg_basetime;return tcfg_basetime; }static inline u64 tcf_gate_cycletime (const struct tc_action *a) { +struct tcf_gate_params *p;u64 tcfg_cycletime; - tcfg_cycletime =to_gate (a)->param.tcfg_cycletime; + p =tcf_gate_params_locked (a); + tcfg_cycletime = p->tcfg_cycletime;return tcfg_cycletime; }static inline u64 tcf_gate_cycletimeext (const struct tc_action *a) { +struct tcf_gate_params *p;u64 tcfg_cycletimeext; - tcfg_cycletimeext =to_gate (a)->param.tcfg_cycletime_ext; + p =tcf_gate_params_locked (a); + tcfg_cycletimeext = p->tcfg_cycletime_ext;return tcfg_cycletimeext; }static inline u32 tcf_gate_num_entries (const struct tc_action *a) { +struct tcf_gate_params *p;u32 num_entries; - num_entries =to_gate (a)->param.num_entries; + p =tcf_gate_params_locked (a); + num_entries = p->num_entries;return num_entries; }
@@ -105,7 +124,7 @@ static inline struct action_gate_entryu32 num_entries;int i =0 ; - p = &to_gate (a)->param; + p =tcf_gate_params_locked (a); num_entries = p->num_entries;list_for_each_entry (entry, &p->entries, list)
net/sched/act_gate.c +189 -76
386 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 23 lines
The gate action can be replaced while the hrtimer callback or dump path is walking the schedule list.
Convert the parameters to an RCU-protected snapshot and swap updates under
tcf_lock, freeing the previous snapshot viacall_rcu(). When REPLACE omits the entry list, preserve the existing schedule so the effective state is unchanged.
[...]net/sched/act_gate.c +4
-0@@ -375,25 +472,26 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla, +if (need_cancel) +hrtimer_cancel (&gact->hitimer); + +spin_lock_bh (&gact->tcf_lock);if (!cycletime) {struct tcfg_gate_entry *entry;
net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 506
Quoted 7 lines
net/sched/act_gate.c +1
-1@@ -404,20 +502,22 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla, cycletime = cycle;if (!cycletime) { err = -EINVAL ; - goto chain_put; + goto unlock; }
The AI says:
"If this error path is taken after hrtimer_cancel() was called above (when need_cancel is true), does the timer get restarted with the old parameters?
Looking at the unlock path:
unlock:
spin_unlock_bh(&gact->tcf_lock);
err_free:
release_entry_list(&p->entries);
kfree(p);
chain_put:
if (goto_ch)
tcf_chain_put_by_act(goto_ch);
release_idr:
if (ret == ACT_P_CREATED)
gate_setup_timer(gact, timer_clockid, tko);
tcf_idr_release(*a, bind);
return err;The gate_setup_timer() call at release_idr only runs for ACT_P_CREATED, not for REPLACE operations. When a REPLACE operation changes basetime/clockid/tk_offset (making need_cancel true), hrtimer_cancel() is called. If the subsequent cycletime validation fails, this error path never restarts the timer, leaving the gate action with a permanently stopped hrtimer.
Should the error path restart the timer with the old parameters when need_cancel was true?"
What it describes won't happen (at least from what I understand) because your current code won't allow zero entries and the interval parameter can't be 0. However it has a point because, if cycletime can't be 0 anymore, you should remove this error path altogether.
cheers,
Victor
Yes, I only see it as unreachable code cleanup.
While looking at cycletime, should I move that block before spin_lock_bh()? It only touches the private p snapshot before it is published, so it looks safe to run outside the lock. I am not sure where the line is before this turns into a refactor.
net/sched/act_gate.c +9 -8
@@ -483,6 +483,15 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla, goto err_free; timer_clockid = clockid; +if (!cycletime) { +struct tcfg_gate_entry *entry; +ktime_t cycle =0 ; + +list_for_each_entry (entry, &p->entries, list) + cycle =ktime_add_ns (cycle, entry->interval); + cycletime = cycle; + } + need_cancel = ret !=ACT_P_CREATED && gate_timer_needs_cancel(basetime, old_basetime, tko, old_tk_offset,
@@ -493,14 +502,6 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,spin_lock_bh (&gact->tcf_lock); -if (!cycletime) { -struct tcfg_gate_entry *entry; -ktime_t cycle =0 ; - -list_for_each_entry (entry, &p->entries, list) - cycle =ktime_add_ns (cycle, entry->interval); - cycletime = cycle; - } p->tcfg_cycletime = cycletime; p->tcfg_cycletime_ext = cycletime_ext;
Quoted 5 lines · Paul Moses
Yes, I only see it as unreachable code cleanup.
While looking at cycletime, should I move that block before
spin_lock_bh()?
I think it would be best to leave it as is and just do the unreachable code cleanup.
cheers,
Victor