Skip to contentSkip to conversation

net/sched: act_gate: snapshot parameters with RCU on replace

PATCH Superseded Paul Moses 2 participants 5 messages Feb 5 – Feb 12

Subsystems (4)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Paul Moses

author
#0 lore raw

v5 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 v4:
- Drop the gate_clockid_to_offset helper, validate clockid once, and set tk_offset in one place.
- Seed missing attrs from the old params when entries are replaced
- Reimplemented the timer cancel from RCU snapshot and cancel before taking tcf_lock.
- Reuse a single protected deref under tcf_lock
- Compare old/new values directly in gate_timer_needs_cancel()
- Cleanup no longer assumes rtnl_lock

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         | 266 ++++++++++++++++++++++++++---------
 2 files changed, 228 insertions(+), 71 deletions(-)
Signature · 1 line
2.52.GIT
author
#1 lore raw

PATCH v5 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.

Fixes a51c328df310 ("net: qos: introduce a gate control flow action")
---
 include/net/tc_act/tc_gate.h |  33 ++++-
 net/sched/act_gate.c         | 266 ++++++++++++++++++++++++++---------
 2 files changed, 228 insertions(+), 71 deletions(-)
include/net/tc_act/tc_gate.h +26 -7
index c1a67149c6b62..006111b978eda 100644
@@ -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(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(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(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(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(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(a);
+	num_entries = p->num_entries;
 
 	return num_entries;
 }
@@ -105,7 +124,7 @@ static inline struct action_gate_entry
 	u32 num_entries;
 	int i = 0;
 
-	p = &to_gate(a)->param;
+	p = tcf_gate_params(a);
 	num_entries = p->num_entries;
 
 	list_for_each_entry(entry, &p->entries, list)
net/sched/act_gate.c +202 -64

386 lines not on this page · raw message

Signature · 1 line
2.52.GIT
#2 lore raw
Quoted 10 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 via call_rcu(). When REPLACE omits the entry list, preserve the existing schedule so the effective state is unchanged.
[...]

net/sched/act_gate.c +0 -0
index c1f75f2727576..4a1a10bfe3e62 100644

net/sched/act_gate.c static int parse_gate_list(struct nlattr *list_attr, · line 327

Quoted 8 lines
net/sched/act_gate.c +2 -3
[...]
-static void gate_setup_timer(struct tcf_gate *gact, u64 basetime,
-                            enum tk_offsets tko, s32 clockid,
-                            bool do_init)
[...]
+static void gate_timer_setup(struct tcf_gate *gact, s32 clockid,
+                            enum tk_offsets tko)
[...]

I don't believe you need to change the function name here.

Quoted 17 lines

[...]

net/sched/act_gate.c +0 -12
@@ -323,20 +370,11 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
 
        if (tb[TCA_GATE_CLOCKID]) {
                clockid = nla_get_s32(tb[TCA_GATE_CLOCKID]);
-               switch (clockid) {
-               case CLOCK_REALTIME:
-                       tk_offset = TK_OFFS_REAL;
-                       break;
-               case CLOCK_MONOTONIC:
-                       tk_offset = TK_OFFS_MAX;
-                       break;
-               case CLOCK_BOOTTIME:
-                       tk_offset = TK_OFFS_BOOT;
-                       break;
-               case CLOCK_TAI:
-                       tk_offset = TK_OFFS_TAI;

net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 380

Quoted 10 lines
net/sched/act_gate.c +5 -2
-                       break;
-               default:
+               clockid_provided = true;
+               if (clockid != CLOCK_REALTIME &&
+                   clockid != CLOCK_MONOTONIC &&
+                   clockid != CLOCK_BOOTTIME &&
+                   clockid != CLOCK_TAI) {
                        NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
                        return -EINVAL;
                }

This is better than what you had before, however it still is redundant given that you do the switch statement later and perform the same validation again. If there's no reason to keep this code, you probably can also get rid of "clockid_provided".

net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 415

Quoted 7 lines
net/sched/act_gate.c +5 -0
@@ -366,6 +404,37 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
[...]
+
+       if (ret != ACT_P_CREATED) {
+               rcu_read_lock();
+               old_p = rcu_dereference(gact->param);
+               if (old_p) {

When do you believe old_p might be NULL here?
From what I understand, you can't arrive here while
a delete for the same action instance is happening in parallel. Were you able to create such scenario when testing gate?

net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 511

Quoted 8 lines

[...]

net/sched/act_gate.c +7 -0
+       if (use_old_entries) {
+               err = tcf_gate_copy_entries(p, old_p, extack);
+               if (err)
+                       goto unlock;
+
+               if (!tb[TCA_GATE_CYCLE_TIME])
+                       cycletime = old_p->tcfg_cycletime;

Why did you keep this one as in v4?
You don't want to reuse the old "cycletime" if the user specified new entries?
Not saying you are necessarily wrong.
Just trying to understand your logic.

net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 566

Quoted 9 lines

[...]

net/sched/act_gate.c +3 -1
-chain_put:
+unlock:
        spin_unlock_bh(&gact->tcf_lock);
 
        if (goto_ch)
                tcf_chain_put_by_act(goto_ch);
+       release_entry_list(&p->entries);
+       kfree(p);

The 4 lines above look exactly like what you
do in err_free. Can't you label them as err_free
and remove the lines below?

Quoted 11 lines

[...]

net/sched/act_gate.c +7 -0
+err_free:
+       if (goto_ch)
+               tcf_chain_put_by_act(goto_ch);
+       release_entry_list(&p->entries);
+       kfree(p);
+       goto release_idr;
+}
[...]
 static void tcf_gate_cleanup(struct tc_action *a)

net/sched/act_gate.c static void tcf_gate_cleanup(struct tc_action *a) · line 601

Quoted 10 lines
net/sched/act_gate.c +3 -2
@@ -458,9 +594,10 @@ static void tcf_gate_cleanup(struct tc_action *a)
        struct tcf_gate *gact = to_gate(a);
        struct tcf_gate_params *p;
 
-       p = &gact->param;
        hrtimer_cancel(&gact->hitimer);
-       release_entry_list(&p->entries);
+       p = rcu_replace_pointer(gact->param, NULL, 1);
+       if (p)
+               call_rcu(&p->rcu, tcf_gate_params_free_rcu);
 }

Sorry, I think I lacked precision in my last comment. I meant that you should've removed the rtnl requirement (which you did), but also use rcu_dereference_protected as act_vlan does. This relates to my previous comment on "old_p" being NULL. I don't believe you need to set this to NULL unless you were able to reproduce the scenario I described earlier.

net/sched/act_gate.c static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a, · line 653

Quoted 8 lines
net/sched/act_gate.c +2 -1
 static int dumping_entry(struct sk_buff *skb,
@@ -512,7 +649,8 @@ static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
        spin_lock_bh(&gact->tcf_lock);
        opt.action = gact->tcf_action;
 
-       p = &gact->param;
+       p = rcu_dereference_protected(gact->param,
+                                     lockdep_is_held(&gact->tcf_lock));

You could've kept the rcu_read_lock approach here. One of the main advantages of making the params rcu is being able to dump without the tcf_lock.

cheers,
Victor

author
#3 lore raw

Proposed changes from v5...v6:
1. Agreed
-Fixed in (net/sched: act_gate: keep gate_setup_timer helper name)

2. Agreed
-Fixed in (net/sched: act_gate: drop redundant clockid pre-validation)

3. I was not able to reproduce it. I tended to keep it since NULL became representable in the conversion and it was not an expensive branch. -Fixed in (net/sched: act_gate: assume params exist on replace path)

4. use_old_entries is true only when REPLACE does not provide a usable new entry list (missing or empty) and we copy the previous entries into p to preserve effective behavior. This block is skipped when new entries are provided, so old cycletime is not reused in that case. It could be clearer but I didn't think it was worth the diff increase.

5. Yes
-Fixed in (net/sched: act_gate: deduplicate init error cleanup labels)

6. Agreed
-Fixed in (net/sched: act_gate: align cleanup dereference with act_vlan)

7. Agreed
-Fixed in (net/sched: act_gate: dump params under rcu read-side lock)

Thanks,
Paul

Quoted 142 lines · Victor Nogueira
Quoted 10 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 via call_rcu(). When REPLACE omits the entry list, preserve the existing schedule so the effective state is unchanged.
[...]

net/sched/act_gate.c +0 -0
index c1f75f2727576..4a1a10bfe3e62 100644

net/sched/act_gate.c static int parse_gate_list(struct nlattr *list_attr, · line 327

Quoted 8 lines
net/sched/act_gate.c +2 -3
[...]
-static void gate_setup_timer(struct tcf_gate *gact, u64 basetime,
-                            enum tk_offsets tko, s32 clockid,
-                            bool do_init)
[...]
+static void gate_timer_setup(struct tcf_gate *gact, s32 clockid,
+                            enum tk_offsets tko)
[...]

I don't believe you need to change the function name here.

Quoted 17 lines

[...]

net/sched/act_gate.c +0 -12
@@ -323,20 +370,11 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
 
        if (tb[TCA_GATE_CLOCKID]) {
                clockid = nla_get_s32(tb[TCA_GATE_CLOCKID]);
-               switch (clockid) {
-               case CLOCK_REALTIME:
-                       tk_offset = TK_OFFS_REAL;
-                       break;
-               case CLOCK_MONOTONIC:
-                       tk_offset = TK_OFFS_MAX;
-                       break;
-               case CLOCK_BOOTTIME:
-                       tk_offset = TK_OFFS_BOOT;
-                       break;
-               case CLOCK_TAI:
-                       tk_offset = TK_OFFS_TAI;

net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 380

Quoted 10 lines
net/sched/act_gate.c +5 -2
-                       break;
-               default:
+               clockid_provided = true;
+               if (clockid != CLOCK_REALTIME &&
+                   clockid != CLOCK_MONOTONIC &&
+                   clockid != CLOCK_BOOTTIME &&
+                   clockid != CLOCK_TAI) {
                        NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
                        return -EINVAL;
                }

This is better than what you had before, however it still is redundant given that you do the switch statement later and perform the same validation again. If there's no reason to keep this code, you probably can also get rid of "clockid_provided".

net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 415

Quoted 7 lines
net/sched/act_gate.c +5 -0
@@ -366,6 +404,37 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
[...]
+
+       if (ret != ACT_P_CREATED) {
+               rcu_read_lock();
+               old_p = rcu_dereference(gact->param);
+               if (old_p) {

When do you believe old_p might be NULL here?
From what I understand, you can't arrive here while
a delete for the same action instance is happening in parallel. Were you able to create such scenario when testing gate?

net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 511

Quoted 8 lines

[...]

net/sched/act_gate.c +7 -0
+       if (use_old_entries) {
+               err = tcf_gate_copy_entries(p, old_p, extack);
+               if (err)
+                       goto unlock;
+
+               if (!tb[TCA_GATE_CYCLE_TIME])
+                       cycletime = old_p->tcfg_cycletime;

Why did you keep this one as in v4?
You don't want to reuse the old "cycletime" if the user specified new entries?
Not saying you are necessarily wrong.
Just trying to understand your logic.

net/sched/act_gate.c static int tcf_gate_init(struct net *net, struct nlattr *nla, · line 566

Quoted 9 lines

[...]

net/sched/act_gate.c +3 -1
-chain_put:
+unlock:
        spin_unlock_bh(&gact->tcf_lock);
 
        if (goto_ch)
                tcf_chain_put_by_act(goto_ch);
+       release_entry_list(&p->entries);
+       kfree(p);

The 4 lines above look exactly like what you
do in err_free. Can't you label them as err_free
and remove the lines below?

Quoted 11 lines

[...]

net/sched/act_gate.c +7 -0
+err_free:
+       if (goto_ch)
+               tcf_chain_put_by_act(goto_ch);
+       release_entry_list(&p->entries);
+       kfree(p);
+       goto release_idr;
+}
[...]
 static void tcf_gate_cleanup(struct tc_action *a)

net/sched/act_gate.c static void tcf_gate_cleanup(struct tc_action *a) · line 601

Quoted 10 lines
net/sched/act_gate.c +3 -2
@@ -458,9 +594,10 @@ static void tcf_gate_cleanup(struct tc_action *a)
        struct tcf_gate *gact = to_gate(a);
        struct tcf_gate_params *p;
 
-       p = &gact->param;
        hrtimer_cancel(&gact->hitimer);
-       release_entry_list(&p->entries);
+       p = rcu_replace_pointer(gact->param, NULL, 1);
+       if (p)
+               call_rcu(&p->rcu, tcf_gate_params_free_rcu);
 }

Sorry, I think I lacked precision in my last comment. I meant that you should've removed the rtnl requirement (which you did), but also use rcu_dereference_protected as act_vlan does. This relates to my previous comment on "old_p" being NULL. I don't believe you need to set this to NULL unless you were able to reproduce the scenario I described earlier.

net/sched/act_gate.c static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a, · line 653

Quoted 8 lines
net/sched/act_gate.c +2 -1
 static int dumping_entry(struct sk_buff *skb,
@@ -512,7 +649,8 @@ static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
        spin_lock_bh(&gact->tcf_lock);
        opt.action = gact->tcf_action;
 
-       p = &gact->param;
+       p = rcu_dereference_protected(gact->param,
+                                     lockdep_is_held(&gact->tcf_lock));

You could've kept the rcu_read_lock approach here. One of the main advantages of making the params rcu is being able to dump without the tcf_lock.

cheers,
Victor

#4 lore raw
Quoted 3 lines · Paul Moses

Proposed changes from v5...v6:
[...]

I think I understand what you are saying, but I believe I'll be able to give you better comments once I see the code.

cheers,
Victor