Skip to contentSkip to conversation

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

PATCH Superseded Paul Moses 4 participants 8 messages Feb 13 – Feb 18

Subsystems (4)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Paul Moses

author
#0 lore raw

v6 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 v5:
- Reset stale err before optional old-entry copy on REPLACE
- Centralize goto_ch cleanup at release_idr for init error unwinding
- Snapshot replace defaults from old params once
- Drop redundant early clockid pre-validation and remove clockid_provided
- Resolve clockid once into tk_offset and pass clockid/tk_offset as scalars
- Deduplicate init error cleanup by falling through shared err_free/release_idr
- Deduplicate empty entry list rejection on create into a single check
- Rename lock-only accessor helper to tcf_gate_params_locked()
- Simplify entry-list handling by deriving use_old_entries from attributes
- Remove replace-path defensive old_p NULL handling
- Keep helper name gate_setup_timer()
- Align cleanup dereference with act_vlan using rcu_dereference_protected(gact->param, 1) and call_rcu()
- Switch dump to lockless RCU read-side access and use READ_ONCE(gact->tcf_action)
- Compute need_cancel only on replace and keep gate_timer_needs_cancel() focused

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         | 275 ++++++++++++++++++++++++-----------
 2 files changed, 219 insertions(+), 89 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v6 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         | 275 ++++++++++++++++++++++++-----------
 2 files changed, 219 insertions(+), 89 deletions(-)
include/net/tc_act/tc_gate.h +26 -7
index c1a67149c6b62..5223c00279d5a 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_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_entry
 	u32 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 +193 -82

409 lines not on this page · raw message

Signature · 1 line
2.53.0
reviewer
#2 lore raw

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.

For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
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.

Quoted 44 lines
include/net/tc_act/tc_gate.h +13 -2
index c1a67149c6b6..5223c00279d5 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_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;
 }

[ ... ]

Quoted 4 lines
net/sched/act_gate.c +0 -0
index c1f75f272757..60c80e609ec3 100644

[ ... ]

Quoted 20 lines
net/sched/act_gate.c +3 -5
@@ -509,10 +621,9 @@ static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
 	struct nlattr *entry_list;
 	struct tcf_t t;
 
-	spin_lock_bh(&gact->tcf_lock);
-	opt.action = gact->tcf_action;
-
-	p = &gact->param;
+	rcu_read_lock();
+	opt.action = READ_ONCE(gact->tcf_action);
+	p = rcu_dereference(gact->param);
 
 	if (nla_put(skb, TCA_GATE_PARMS, sizeof(opt), &opt))
 		goto nla_put_failure;
@@ -552,12 +663,12 @@ static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
 	tcf_tm_dump(&t, &gact->tcf_tm);
 	if (nla_put_64bit(skb, TCA_GATE_TM, sizeof(t), &t, TCA_GATE_PAD))
 		goto nla_put_failure;
-	spin_unlock_bh(&gact->tcf_lock);

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

Quoted 10 lines
net/sched/act_gate.c +2 -1
+	rcu_read_unlock();
 
 	return skb->len;
 
 nla_put_failure:
-	spin_unlock_bh(&gact->tcf_lock);
+	rcu_read_unlock();
 	nlmsg_trim(skb, b);
 	return -1;
 }

Can tcf_gate_dump be called before gact->param is initialized?

After the conversion to RCU pointers, if tcf_gate_dump is called during action initialization (between tcf_idr_create_from_flags and rcu_replace_pointer in tcf_gate_init), p will be NULL:

net/sched/act_gate.c:tcf_gate_init() {
    tcf_idr_create_from_flags(...);
    ...
    // gact->param still NULL here
    ...
    rcu_replace_pointer(gact->param, p, ...);  // line 517
}
net/sched/act_gate.c:tcf_gate_dump() {
    rcu_read_lock();
    p = rcu_dereference(gact->param);  // can be NULL

    if (nla_put_u64_64bit(skb, TCA_GATE_BASE_TIME,
                          p->tcfg_basetime, TCA_GATE_PAD))  // NULL deref
}

Before the patch, param was an embedded struct that was zero-initialized, so dump would return valid (if meaningless) data. After the patch, this becomes a NULL pointer dereference.

While the tc action framework may prevent dumping partially initialized actions through external synchronization, should this code include a NULL check to make the initialization window safer?

The second issue is in the hardware offload path. Looking at net/sched/act_gate.c:tcf_gate_offload_act_setup(), the function calls the accessor helpers without holding tcf_lock:

net/sched/act_gate.c:tcf_gate_offload_act_setup() {
    // no lock held here
    entry->gate.prio = tcf_gate_prio(act);           // line 723
    entry->gate.basetime = tcf_gate_basetime(act);
    entry->gate.cycletime = tcf_gate_cycletime(act);
    ...
}

include/net/tc_act/tc_gate.h:tcf_gate_prio() {
p = tcf_gate_params_locked(a); // uses rcu_dereference_protected
...
}

include/net/tc_act/tc_gate.h:tcf_gate_params_locked() {
    return rcu_dereference_protected(gact->param,
                                     lockdep_is_held(&gact->tcf_lock));
}

Does tcf_gate_offload_act_setup violate the locking requirements?

The accessor functions all use tcf_gate_params_locked(), which requires tcf_lock to be held (verified via lockdep_is_held). The offload setup function doesn't acquire this lock before calling the accessors.

With lockdep enabled, this will trigger warnings. Without lockdep, there's a race where param can be replaced via rcu_replace_pointer while the offload function is reading it, potentially causing reads of inconsistent state.

The dump path uses the correct pattern with rcu_read_lock() and rcu_dereference(). Should the offload path either acquire tcf_lock or use a similar RCU-only approach?

author
#3 lore raw

1. I don’t think this is reachable, during create, the IDR slot is populated with ERR_PTR(-EBUSY) by tcf_idr_check_alloc(). The real action pointer is only published later by tcf_idr_insert_many(), which runs after init() returns and after tcf_gate_init() has already done:

     rcu_replace_pointer(gact->param, p, ...)

Both the normal lookup path and the dump walker treat ERR_PTR entries as “not ready”: tcf_idr_search() rejects them and the dump walker skips.

2. offload_act_setup() is currently called with act->tcfa_lock held in both call chains (net/sched/act_api.c and net/sched/cls_api.c). Since gact->tcf_lock aliases common.tcfa_lock, the lockdep_is_held(&gact->tcf_lock) condition in tcf_gate_params_locked() is satisfied.

Thanks,
Paul

Quoted 167 lines · Simon Horman

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.

For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
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.

Quoted 44 lines
include/net/tc_act/tc_gate.h +13 -2
index c1a67149c6b6..5223c00279d5 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_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;
 }

[ ... ]

Quoted 4 lines
net/sched/act_gate.c +0 -0
index c1f75f272757..60c80e609ec3 100644

[ ... ]

Quoted 20 lines
net/sched/act_gate.c +3 -5
@@ -509,10 +621,9 @@ static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
 	struct nlattr *entry_list;
 	struct tcf_t t;
 
-	spin_lock_bh(&gact->tcf_lock);
-	opt.action = gact->tcf_action;
-
-	p = &gact->param;
+	rcu_read_lock();
+	opt.action = READ_ONCE(gact->tcf_action);
+	p = rcu_dereference(gact->param);
 
 	if (nla_put(skb, TCA_GATE_PARMS, sizeof(opt), &opt))
 		goto nla_put_failure;
@@ -552,12 +663,12 @@ static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
 	tcf_tm_dump(&t, &gact->tcf_tm);
 	if (nla_put_64bit(skb, TCA_GATE_TM, sizeof(t), &t, TCA_GATE_PAD))
 		goto nla_put_failure;
-	spin_unlock_bh(&gact->tcf_lock);

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

Quoted 10 lines
net/sched/act_gate.c +2 -1
+	rcu_read_unlock();
 
 	return skb->len;
 
 nla_put_failure:
-	spin_unlock_bh(&gact->tcf_lock);
+	rcu_read_unlock();
 	nlmsg_trim(skb, b);
 	return -1;
 }

Can tcf_gate_dump be called before gact->param is initialized?

After the conversion to RCU pointers, if tcf_gate_dump is called during action initialization (between tcf_idr_create_from_flags and rcu_replace_pointer in tcf_gate_init), p will be NULL:

net/sched/act_gate.c:tcf_gate_init() {
    tcf_idr_create_from_flags(...);
    ...
    // gact->param still NULL here
    ...
    rcu_replace_pointer(gact->param, p, ...);  // line 517
}
net/sched/act_gate.c:tcf_gate_dump() {
    rcu_read_lock();
    p = rcu_dereference(gact->param);  // can be NULL

    if (nla_put_u64_64bit(skb, TCA_GATE_BASE_TIME,
                          p->tcfg_basetime, TCA_GATE_PAD))  // NULL deref
}

Before the patch, param was an embedded struct that was zero-initialized, so dump would return valid (if meaningless) data. After the patch, this becomes a NULL pointer dereference.

While the tc action framework may prevent dumping partially initialized actions through external synchronization, should this code include a NULL check to make the initialization window safer?

The second issue is in the hardware offload path. Looking at net/sched/act_gate.c:tcf_gate_offload_act_setup(), the function calls the accessor helpers without holding tcf_lock:

net/sched/act_gate.c:tcf_gate_offload_act_setup() {
    // no lock held here
    entry->gate.prio = tcf_gate_prio(act);           // line 723
    entry->gate.basetime = tcf_gate_basetime(act);
    entry->gate.cycletime = tcf_gate_cycletime(act);
    ...
}

include/net/tc_act/tc_gate.h:tcf_gate_prio() {
p = tcf_gate_params_locked(a); // uses rcu_dereference_protected
...
}

include/net/tc_act/tc_gate.h:tcf_gate_params_locked() {
    return rcu_dereference_protected(gact->param,
                                     lockdep_is_held(&gact->tcf_lock));
}

Does tcf_gate_offload_act_setup violate the locking requirements?

The accessor functions all use tcf_gate_params_locked(), which requires tcf_lock to be held (verified via lockdep_is_held). The offload setup function doesn't acquire this lock before calling the accessors.

With lockdep enabled, this will trigger warnings. Without lockdep, there's a race where param can be replaced via rcu_replace_pointer while the offload function is reading it, potentially causing reads of inconsistent state.

The dump path uses the correct pattern with rcu_read_lock() and rcu_dereference(). Should the offload path either acquire tcf_lock or use a similar RCU-only approach?

maintainer
#4 lore raw
Quoted 11 lines · Paul Moses

1. I don’t think this is reachable, during create, the IDR slot is populated with ERR_PTR(-EBUSY) by tcf_idr_check_alloc(). The real action pointer is only published later by tcf_idr_insert_many(), which runs after init() returns and after tcf_gate_init() has already done:

     rcu_replace_pointer(gact->param, p, ...)

Both the normal lookup path and the dump walker treat ERR_PTR entries as “not ready”: tcf_idr_search() rejects them and the dump walker skips.

Correct.

Quoted 6 lines

2. offload_act_setup() is currently called with act->tcfa_lock held in both call chains (net/sched/act_api.c and net/sched/cls_api.c). Since gact->tcf_lock aliases common.tcfa_lock, the lockdep_is_held(&gact->tcf_lock) condition in tcf_gate_params_locked() is satisfied.

The spinlock will catch it as stated.

The AI is clearly hallucinating..

cheers,
jamal

Quoted 175 lines

Thanks,
Paul

Quoted 167 lines · Simon Horman

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.

For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
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.

Quoted 44 lines
include/net/tc_act/tc_gate.h +13 -2
index c1a67149c6b6..5223c00279d5 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_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;
 }

[ ... ]

Quoted 4 lines
net/sched/act_gate.c +0 -0
index c1f75f272757..60c80e609ec3 100644

[ ... ]

Quoted 20 lines
net/sched/act_gate.c +3 -5
@@ -509,10 +621,9 @@ static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
    struct nlattr *entry_list;
    struct tcf_t t;
 
-   spin_lock_bh(&gact->tcf_lock);
-   opt.action = gact->tcf_action;
-
-   p = &gact->param;
+   rcu_read_lock();
+   opt.action = READ_ONCE(gact->tcf_action);
+   p = rcu_dereference(gact->param);
 
    if (nla_put(skb, TCA_GATE_PARMS, sizeof(opt), &opt))
            goto nla_put_failure;
@@ -552,12 +663,12 @@ static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
    tcf_tm_dump(&t, &gact->tcf_tm);
    if (nla_put_64bit(skb, TCA_GATE_TM, sizeof(t), &t, TCA_GATE_PAD))
            goto nla_put_failure;
-   spin_unlock_bh(&gact->tcf_lock);

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

Quoted 10 lines
net/sched/act_gate.c +2 -1
+   rcu_read_unlock();
 
    return skb->len;
 
 nla_put_failure:
-   spin_unlock_bh(&gact->tcf_lock);
+   rcu_read_unlock();
    nlmsg_trim(skb, b);
    return -1;
 }

Can tcf_gate_dump be called before gact->param is initialized?

After the conversion to RCU pointers, if tcf_gate_dump is called during action initialization (between tcf_idr_create_from_flags and rcu_replace_pointer in tcf_gate_init), p will be NULL:

net/sched/act_gate.c:tcf_gate_init() {
    tcf_idr_create_from_flags(...);
    ...
    // gact->param still NULL here
    ...
    rcu_replace_pointer(gact->param, p, ...);  // line 517
}
net/sched/act_gate.c:tcf_gate_dump() {
    rcu_read_lock();
    p = rcu_dereference(gact->param);  // can be NULL

    if (nla_put_u64_64bit(skb, TCA_GATE_BASE_TIME,
                          p->tcfg_basetime, TCA_GATE_PAD))  // NULL deref
}

Before the patch, param was an embedded struct that was zero-initialized, so dump would return valid (if meaningless) data. After the patch, this becomes a NULL pointer dereference.

While the tc action framework may prevent dumping partially initialized actions through external synchronization, should this code include a NULL check to make the initialization window safer?

The second issue is in the hardware offload path. Looking at net/sched/act_gate.c:tcf_gate_offload_act_setup(), the function calls the accessor helpers without holding tcf_lock:

net/sched/act_gate.c:tcf_gate_offload_act_setup() {
    // no lock held here
    entry->gate.prio = tcf_gate_prio(act);           // line 723
    entry->gate.basetime = tcf_gate_basetime(act);
    entry->gate.cycletime = tcf_gate_cycletime(act);
    ...
}

include/net/tc_act/tc_gate.h:tcf_gate_prio() {
p = tcf_gate_params_locked(a); // uses rcu_dereference_protected
...
}

include/net/tc_act/tc_gate.h:tcf_gate_params_locked() {
    return rcu_dereference_protected(gact->param,
                                     lockdep_is_held(&gact->tcf_lock));
}

Does tcf_gate_offload_act_setup violate the locking requirements?

The accessor functions all use tcf_gate_params_locked(), which requires tcf_lock to be held (verified via lockdep_is_held). The offload setup function doesn't acquire this lock before calling the accessors.

With lockdep enabled, this will trigger warnings. Without lockdep, there's a race where param can be replaced via rcu_replace_pointer while the offload function is reading it, potentially causing reads of inconsistent state.

The dump path uses the correct pattern with rcu_read_lock() and rcu_dereference(). Should the offload path either acquire tcf_lock or use a similar RCU-only approach?

#5 lore raw
In reply to Paul Moses
Quoted 17 lines · Paul Moses

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..60c80e609ec3d 100644
[...]
@@ -56,11 +59,10 @@ static void gate_start_timer(struct tcf_gate *gact, ktime_t start)
 {
        ktime_t expires;

net/sched/act_gate.c static void gate_start_timer(struct tcf_gate *gact, ktime_t start) · line 65

Quoted 10 lines
net/sched/act_gate.c +4 -5
 
-       expires = hrtimer_get_expires(&gact->hitimer);
-       if (expires == 0)
-               expires = KTIME_MAX;
-
-       start = min_t(ktime_t, start, expires);
+       if (hrtimer_active(&gact->hitimer)) {
+               expires = hrtimer_get_expires(&gact->hitimer);
+               start = min_t(ktime_t, start, expires);
+       }

Is this change really necessary?

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

Quoted 10 lines

[...]
static int parse_gate_list(struct nlattr *list_attr,
struct tcf_gate_params *sched, struct netlink_ext_ack *extack)

net/sched/act_gate.c +0 -1
@@ -261,7 +294,6 @@ static int parse_gate_list(struct nlattr *list_attr,
        }
 
        sched->num_entries = i;
-
        return i;

Removing this line also seems unnecessary.

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

Quoted 5 lines

[...]

net/sched/act_gate.c +4 -0
+static void gate_setup_timer(struct tcf_gate *gact, s32 clockid,
+                            enum tk_offsets tko)
+{
+       WRITE_ONCE(gact->tk_offset, tko);

Why do you need this WRITE_ONCE?

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

Quoted 9 lines

static int tcf_gate_init(struct net *net, struct nlattr *nla,
[...]

net/sched/act_gate.c +4 -0
@@ -366,6 +407,60 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
[...]
+       if (ret != ACT_P_CREATED) {
[...]
+               if (use_old_entries) {
+                       err = tcf_gate_copy_entries(p, cur_p, extack);
+                       if (!err && !tb[TCA_GATE_CYCLE_TIME])

This check for TCA_GATE_CYCLE_TIME seems unnecessary. If I understand your code correctly, cycletime will be overwritten further down if TCA_GATE_CYCLE_TIME was specified.

Quoted 6 lines
net/sched/act_gate.c +2 -1
+                               cycletime = cur_p->tcfg_cycletime;
[...]
@@ -434,33 +532,47 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
[...]
-chain_put:
+unlock:

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

Quoted 10 lines
net/sched/act_gate.c +4 -1
        spin_unlock_bh(&gact->tcf_lock);
 
+err_free:
+       release_entry_list(&p->entries);
+       kfree(p);
+release_idr:
        if (goto_ch)
                tcf_chain_put_by_act(goto_ch);
-release_idr:
[...]

This looks weird.
You will go to the release_idr label when tcf_action_check_ctrlact fails, so the "if (goto_ch)" part of the code will be reached in that code path. I believe it would be better to keep the "chain_put" label and keep "release_idr" below it (as it was before your change). Something like:

chain_put:

        if (goto_ch)
                tcf_chain_put_by_act(goto_ch);
release_idr:
        ...

cheers,
Victor

author
#6 lore raw

1. hrtimer_get_expires() just returns the stored node.expires and hrtimer_cancel() doesn’t clear it, so expires==0 is not a reliable inactivity test. Logic was that although I detected no observable behavior difference, relying on stale expires could theoretically cause infrequent subtle intermittent misses of intended behavior. It's maybe more appropriate to leave it alone for stable or at least not in this patch/series?

2. Agreed. This was a mistake.

3. It's the same pattern used in sch_taprio and it's documented in Documentation/memory-barriers.txt: the compiler may merge/discard/ invent/reorder plain accesses and READ_ONCE()/WRITE_ONCE() exist to make intentional lockless shared variable accesses well defined. Since tk_offset is read with READ_ONCE() outside tcf_lock, the writer uses WRITE_ONCE() to pair with that lockless read.

4. Agreed, I’ll remove the redundant guard.

5. goto_ch is initialized to NULL and tcf_action_check_ctrlact() only sets it on success, so the current code is safe, but I agree that it's confusing, I'll improve.

Thanks
Paul

Quoted 99 lines · Victor Nogueira
Quoted 17 lines · Paul Moses

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..60c80e609ec3d 100644
[...]
@@ -56,11 +59,10 @@ static void gate_start_timer(struct tcf_gate *gact, ktime_t start)
 {
        ktime_t expires;

net/sched/act_gate.c static void gate_start_timer(struct tcf_gate *gact, ktime_t start) · line 65

Quoted 10 lines
net/sched/act_gate.c +4 -5
 
-       expires = hrtimer_get_expires(&gact->hitimer);
-       if (expires == 0)
-               expires = KTIME_MAX;
-
-       start = min_t(ktime_t, start, expires);
+       if (hrtimer_active(&gact->hitimer)) {
+               expires = hrtimer_get_expires(&gact->hitimer);
+               start = min_t(ktime_t, start, expires);
+       }

Is this change really necessary?

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

Quoted 10 lines

[...]
static int parse_gate_list(struct nlattr *list_attr,
struct tcf_gate_params *sched, struct netlink_ext_ack *extack)

net/sched/act_gate.c +0 -1
@@ -261,7 +294,6 @@ static int parse_gate_list(struct nlattr *list_attr,
        }
 
        sched->num_entries = i;
-
        return i;

Removing this line also seems unnecessary.

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

Quoted 5 lines

[...]

net/sched/act_gate.c +4 -0
+static void gate_setup_timer(struct tcf_gate *gact, s32 clockid,
+                            enum tk_offsets tko)
+{
+       WRITE_ONCE(gact->tk_offset, tko);

Why do you need this WRITE_ONCE?

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

Quoted 9 lines

static int tcf_gate_init(struct net *net, struct nlattr *nla,
[...]

net/sched/act_gate.c +4 -0
@@ -366,6 +407,60 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
[...]
+       if (ret != ACT_P_CREATED) {
[...]
+               if (use_old_entries) {
+                       err = tcf_gate_copy_entries(p, cur_p, extack);
+                       if (!err && !tb[TCA_GATE_CYCLE_TIME])

This check for TCA_GATE_CYCLE_TIME seems unnecessary. If I understand your code correctly, cycletime will be overwritten further down if TCA_GATE_CYCLE_TIME was specified.

Quoted 6 lines
net/sched/act_gate.c +2 -1
+                               cycletime = cur_p->tcfg_cycletime;
[...]
@@ -434,33 +532,47 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
[...]
-chain_put:
+unlock:

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

Quoted 10 lines
net/sched/act_gate.c +4 -1
        spin_unlock_bh(&gact->tcf_lock);
 
+err_free:
+       release_entry_list(&p->entries);
+       kfree(p);
+release_idr:
        if (goto_ch)
                tcf_chain_put_by_act(goto_ch);
-release_idr:
[...]

This looks weird.
You will go to the release_idr label when tcf_action_check_ctrlact fails, so the "if (goto_ch)" part of the code will be reached in that code path. I believe it would be better to keep the "chain_put" label and keep "release_idr" below it (as it was before your change). Something like:

chain_put:

        if (goto_ch)
                tcf_chain_put_by_act(goto_ch);
release_idr:
        ...

cheers,
Victor

#7 lore raw
Quoted 7 lines · Paul Moses

1. hrtimer_get_expires() just returns the stored node.expires and hrtimer_cancel() doesn’t clear it, so expires==0 is not a reliable inactivity test. Logic was that although I detected no observable behavior difference, relying on stale expires could theoretically cause infrequent subtle intermittent misses of intended behavior. It's maybe more appropriate to leave it alone for stable or at least not in this patch/series?

Yes, even if it is an issue, we should leave it for another patchset.

Quoted 7 lines

[...]
3. It's the same pattern used in sch_taprio and it's documented in Documentation/memory-barriers.txt: the compiler may merge/discard/ invent/reorder plain accesses and READ_ONCE()/WRITE_ONCE() exist to make intentional lockless shared variable accesses well defined. Since tk_offset is read with READ_ONCE() outside tcf_lock, the writer uses WRITE_ONCE() to pair with that lockless read.

Ok.

cheers,
Victor