Skip to content

Commit 16f4842

Browse files
jahurleygregkh
authored andcommitted
net: sched: prevent duplicate flower rules from tcf_proto destroy race
[ Upstream commit 59eb87c ] When a new filter is added to cls_api, the function tcf_chain_tp_insert_unique() looks up the protocol/priority/chain to determine if the tcf_proto is duplicated in the chain's hashtable. It then creates a new entry or continues with an existing one. In cls_flower, this allows the function fl_ht_insert_unque to determine if a filter is a duplicate and reject appropriately, meaning that the duplicate will not be passed to drivers via the offload hooks. However, when a tcf_proto is destroyed it is removed from its chain before a hardware remove hook is hit. This can lead to a race whereby the driver has not received the remove message but duplicate flows can be accepted. This, in turn, can lead to the offload driver receiving incorrect duplicate flows and out of order add/delete messages. Prevent duplicates by utilising an approach suggested by Vlad Buslov. A hash table per block stores each unique chain/protocol/prio being destroyed. This entry is only removed when the full destroy (and hardware offload) has completed. If a new flow is being added with the same identiers as a tc_proto being detroyed, then the add request is replayed until the destroy is complete. Fixes: 8b64678 ("net: sched: refactor tp insert/delete for concurrent execution") Signed-off-by: John Hurley <john.hurley@netronome.com> Signed-off-by: Vlad Buslov <vladbu@mellanox.com> Reviewed-by: Simon Horman <simon.horman@netronome.com> Reported-by: Louis Peens <louis.peens@netronome.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e6ba303 commit 16f4842

2 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎include/net/sch_generic.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/refcount.h>
1414
#include <linux/workqueue.h>
1515
#include <linux/mutex.h>
16+
#include <linux/hashtable.h>
1617
#include <net/gen_stats.h>
1718
#include <net/rtnetlink.h>
1819
#include <net/flow_offload.h>
@@ -359,6 +360,7 @@ struct tcf_proto {
359360
bool deleting;
360361
refcount_t refcnt;
361362
struct rcu_head rcu;
363+
struct hlist_node destroy_ht_node;
362364
};
363365

364366
struct qdisc_skb_cb {
@@ -409,6 +411,8 @@ struct tcf_block {
409411
struct list_head filter_chain_list;
410412
} chain0;
411413
struct rcu_head rcu;
414+
DECLARE_HASHTABLE(proto_destroy_ht, 7);
415+
struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
412416
};
413417

414418
#ifdef CONFIG_PROVE_LOCKING

‎net/sched/cls_api.c‎

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/slab.h>
2222
#include <linux/idr.h>
2323
#include <linux/rhashtable.h>
24+
#include <linux/jhash.h>
2425
#include <net/net_namespace.h>
2526
#include <net/sock.h>
2627
#include <net/netlink.h>
@@ -45,6 +46,62 @@ static LIST_HEAD(tcf_proto_base);
4546
/* Protects list of registered TC modules. It is pure SMP lock. */
4647
static DEFINE_RWLOCK(cls_mod_lock);
4748

49+
static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
50+
{
51+
return jhash_3words(tp->chain->index, tp->prio,
52+
(__force __u32)tp->protocol, 0);
53+
}
54+
55+
static void tcf_proto_signal_destroying(struct tcf_chain *chain,
56+
struct tcf_proto *tp)
57+
{
58+
struct tcf_block *block = chain->block;
59+
60+
mutex_lock(&block->proto_destroy_lock);
61+
hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
62+
destroy_obj_hashfn(tp));
63+
mutex_unlock(&block->proto_destroy_lock);
64+
}
65+
66+
static bool tcf_proto_cmp(const struct tcf_proto *tp1,
67+
const struct tcf_proto *tp2)
68+
{
69+
return tp1->chain->index == tp2->chain->index &&
70+
tp1->prio == tp2->prio &&
71+
tp1->protocol == tp2->protocol;
72+
}
73+
74+
static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
75+
struct tcf_proto *tp)
76+
{
77+
u32 hash = destroy_obj_hashfn(tp);
78+
struct tcf_proto *iter;
79+
bool found = false;
80+
81+
rcu_read_lock();
82+
hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
83+
destroy_ht_node, hash) {
84+
if (tcf_proto_cmp(tp, iter)) {
85+
found = true;
86+
break;
87+
}
88+
}
89+
rcu_read_unlock();
90+
91+
return found;
92+
}
93+
94+
static void
95+
tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
96+
{
97+
struct tcf_block *block = chain->block;
98+
99+
mutex_lock(&block->proto_destroy_lock);
100+
if (hash_hashed(&tp->destroy_ht_node))
101+
hash_del_rcu(&tp->destroy_ht_node);
102+
mutex_unlock(&block->proto_destroy_lock);
103+
}
104+
48105
/* Find classifier type by string name */
49106

50107
static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
@@ -232,9 +289,11 @@ static void tcf_proto_get(struct tcf_proto *tp)
232289
static void tcf_chain_put(struct tcf_chain *chain);
233290

234291
static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
235-
struct netlink_ext_ack *extack)
292+
bool sig_destroy, struct netlink_ext_ack *extack)
236293
{
237294
tp->ops->destroy(tp, rtnl_held, extack);
295+
if (sig_destroy)
296+
tcf_proto_signal_destroyed(tp->chain, tp);
238297
tcf_chain_put(tp->chain);
239298
module_put(tp->ops->owner);
240299
kfree_rcu(tp, rcu);
@@ -244,7 +303,7 @@ static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
244303
struct netlink_ext_ack *extack)
245304
{
246305
if (refcount_dec_and_test(&tp->refcnt))
247-
tcf_proto_destroy(tp, rtnl_held, extack);
306+
tcf_proto_destroy(tp, rtnl_held, true, extack);
248307
}
249308

250309
static int walker_check_empty(struct tcf_proto *tp, void *fh,
@@ -368,6 +427,7 @@ static bool tcf_chain_detach(struct tcf_chain *chain)
368427
static void tcf_block_destroy(struct tcf_block *block)
369428
{
370429
mutex_destroy(&block->lock);
430+
mutex_destroy(&block->proto_destroy_lock);
371431
kfree_rcu(block, rcu);
372432
}
373433

@@ -543,6 +603,12 @@ static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
543603

544604
mutex_lock(&chain->filter_chain_lock);
545605
tp = tcf_chain_dereference(chain->filter_chain, chain);
606+
while (tp) {
607+
tp_next = rcu_dereference_protected(tp->next, 1);
608+
tcf_proto_signal_destroying(chain, tp);
609+
tp = tp_next;
610+
}
611+
tp = tcf_chain_dereference(chain->filter_chain, chain);
546612
RCU_INIT_POINTER(chain->filter_chain, NULL);
547613
tcf_chain0_head_change(chain, NULL);
548614
chain->flushing = true;
@@ -1002,6 +1068,7 @@ static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
10021068
return ERR_PTR(-ENOMEM);
10031069
}
10041070
mutex_init(&block->lock);
1071+
mutex_init(&block->proto_destroy_lock);
10051072
flow_block_init(&block->flow_block);
10061073
INIT_LIST_HEAD(&block->chain_list);
10071074
INIT_LIST_HEAD(&block->owner_list);
@@ -1754,17 +1821,23 @@ static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
17541821

17551822
mutex_lock(&chain->filter_chain_lock);
17561823

1824+
if (tcf_proto_exists_destroying(chain, tp_new)) {
1825+
mutex_unlock(&chain->filter_chain_lock);
1826+
tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1827+
return ERR_PTR(-EAGAIN);
1828+
}
1829+
17571830
tp = tcf_chain_tp_find(chain, &chain_info,
17581831
protocol, prio, false);
17591832
if (!tp)
17601833
err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
17611834
mutex_unlock(&chain->filter_chain_lock);
17621835

17631836
if (tp) {
1764-
tcf_proto_destroy(tp_new, rtnl_held, NULL);
1837+
tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
17651838
tp_new = tp;
17661839
} else if (err) {
1767-
tcf_proto_destroy(tp_new, rtnl_held, NULL);
1840+
tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
17681841
tp_new = ERR_PTR(err);
17691842
}
17701843

@@ -1802,6 +1875,7 @@ static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
18021875
return;
18031876
}
18041877

1878+
tcf_proto_signal_destroying(chain, tp);
18051879
next = tcf_chain_dereference(chain_info.next, chain);
18061880
if (tp == chain->filter_chain)
18071881
tcf_chain0_head_change(chain, next);
@@ -2321,6 +2395,7 @@ static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
23212395
err = -EINVAL;
23222396
goto errout_locked;
23232397
} else if (t->tcm_handle == 0) {
2398+
tcf_proto_signal_destroying(chain, tp);
23242399
tcf_chain_tp_remove(chain, &chain_info, tp);
23252400
mutex_unlock(&chain->filter_chain_lock);
23262401

0 commit comments

Comments
 (0)