Skip to content

Commit e80857c

Browse files
azhou-nicirajessegross
authored andcommitted
openvswitch: Fix kernel panic on ovs_flow_free
Both mega flow mask's reference counter and per flow table mask list should only be accessed when holding ovs_mutex() lock. However this is not true with ovs_flow_table_flush(). The patch fixes this bug. Reported-by: Joe Stringer <joestringer@nicira.com> Signed-off-by: Andy Zhou <azhou@nicira.com> Signed-off-by: Jesse Gross <jesse@nicira.com>
1 parent aea0bb4 commit e80857c

3 files changed

Lines changed: 47 additions & 48 deletions

File tree

‎net/openvswitch/datapath.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
#include "datapath.h"
5757
#include "flow.h"
58+
#include "flow_table.h"
5859
#include "flow_netlink.h"
5960
#include "vport-internal_dev.h"
6061
#include "vport-netdev.h"
@@ -160,7 +161,6 @@ static void destroy_dp_rcu(struct rcu_head *rcu)
160161
{
161162
struct datapath *dp = container_of(rcu, struct datapath, rcu);
162163

163-
ovs_flow_tbl_destroy(&dp->table);
164164
free_percpu(dp->stats_percpu);
165165
release_net(ovs_dp_get_net(dp));
166166
kfree(dp->ports);
@@ -1287,7 +1287,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
12871287
err_destroy_percpu:
12881288
free_percpu(dp->stats_percpu);
12891289
err_destroy_table:
1290-
ovs_flow_tbl_destroy(&dp->table);
1290+
ovs_flow_tbl_destroy(&dp->table, false);
12911291
err_free_dp:
12921292
release_net(ovs_dp_get_net(dp));
12931293
kfree(dp);
@@ -1314,10 +1314,13 @@ static void __dp_destroy(struct datapath *dp)
13141314
list_del_rcu(&dp->list_node);
13151315

13161316
/* OVSP_LOCAL is datapath internal port. We need to make sure that
1317-
* all port in datapath are destroyed first before freeing datapath.
1317+
* all ports in datapath are destroyed first before freeing datapath.
13181318
*/
13191319
ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
13201320

1321+
/* RCU destroy the flow table */
1322+
ovs_flow_tbl_destroy(&dp->table, true);
1323+
13211324
call_rcu(&dp->rcu, destroy_dp_rcu);
13221325
}
13231326

‎net/openvswitch/flow_table.c‎

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,27 @@ static void rcu_free_flow_callback(struct rcu_head *rcu)
153153
flow_free(flow);
154154
}
155155

156-
static void flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
156+
void ovs_flow_free(struct sw_flow *flow, bool deferred)
157157
{
158-
if (!mask)
158+
if (!flow)
159159
return;
160160

161-
BUG_ON(!mask->ref_count);
162-
mask->ref_count--;
161+
ASSERT_OVSL();
163162

164-
if (!mask->ref_count) {
165-
list_del_rcu(&mask->list);
166-
if (deferred)
167-
kfree_rcu(mask, rcu);
168-
else
169-
kfree(mask);
170-
}
171-
}
163+
if (flow->mask) {
164+
struct sw_flow_mask *mask = flow->mask;
172165

173-
void ovs_flow_free(struct sw_flow *flow, bool deferred)
174-
{
175-
if (!flow)
176-
return;
166+
BUG_ON(!mask->ref_count);
167+
mask->ref_count--;
177168

178-
flow_mask_del_ref(flow->mask, deferred);
169+
if (!mask->ref_count) {
170+
list_del_rcu(&mask->list);
171+
if (deferred)
172+
kfree_rcu(mask, rcu);
173+
else
174+
kfree(mask);
175+
}
176+
}
179177

180178
if (deferred)
181179
call_rcu(&flow->rcu, rcu_free_flow_callback);
@@ -188,26 +186,9 @@ static void free_buckets(struct flex_array *buckets)
188186
flex_array_free(buckets);
189187
}
190188

189+
191190
static void __table_instance_destroy(struct table_instance *ti)
192191
{
193-
int i;
194-
195-
if (ti->keep_flows)
196-
goto skip_flows;
197-
198-
for (i = 0; i < ti->n_buckets; i++) {
199-
struct sw_flow *flow;
200-
struct hlist_head *head = flex_array_get(ti->buckets, i);
201-
struct hlist_node *n;
202-
int ver = ti->node_ver;
203-
204-
hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
205-
hlist_del(&flow->hash_node[ver]);
206-
ovs_flow_free(flow, false);
207-
}
208-
}
209-
210-
skip_flows:
211192
free_buckets(ti->buckets);
212193
kfree(ti);
213194
}
@@ -258,20 +239,38 @@ static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
258239

259240
static void table_instance_destroy(struct table_instance *ti, bool deferred)
260241
{
242+
int i;
243+
261244
if (!ti)
262245
return;
263246

247+
if (ti->keep_flows)
248+
goto skip_flows;
249+
250+
for (i = 0; i < ti->n_buckets; i++) {
251+
struct sw_flow *flow;
252+
struct hlist_head *head = flex_array_get(ti->buckets, i);
253+
struct hlist_node *n;
254+
int ver = ti->node_ver;
255+
256+
hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
257+
hlist_del_rcu(&flow->hash_node[ver]);
258+
ovs_flow_free(flow, deferred);
259+
}
260+
}
261+
262+
skip_flows:
264263
if (deferred)
265264
call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
266265
else
267266
__table_instance_destroy(ti);
268267
}
269268

270-
void ovs_flow_tbl_destroy(struct flow_table *table)
269+
void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
271270
{
272271
struct table_instance *ti = ovsl_dereference(table->ti);
273272

274-
table_instance_destroy(ti, false);
273+
table_instance_destroy(ti, deferred);
275274
}
276275

277276
struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
@@ -504,16 +503,11 @@ static struct sw_flow_mask *mask_alloc(void)
504503

505504
mask = kmalloc(sizeof(*mask), GFP_KERNEL);
506505
if (mask)
507-
mask->ref_count = 0;
506+
mask->ref_count = 1;
508507

509508
return mask;
510509
}
511510

512-
static void mask_add_ref(struct sw_flow_mask *mask)
513-
{
514-
mask->ref_count++;
515-
}
516-
517511
static bool mask_equal(const struct sw_flow_mask *a,
518512
const struct sw_flow_mask *b)
519513
{
@@ -554,9 +548,11 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
554548
mask->key = new->key;
555549
mask->range = new->range;
556550
list_add_rcu(&mask->list, &tbl->mask_list);
551+
} else {
552+
BUG_ON(!mask->ref_count);
553+
mask->ref_count++;
557554
}
558555

559-
mask_add_ref(mask);
560556
flow->mask = mask;
561557
return 0;
562558
}

‎net/openvswitch/flow_table.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void ovs_flow_free(struct sw_flow *, bool deferred);
6060

6161
int ovs_flow_tbl_init(struct flow_table *);
6262
int ovs_flow_tbl_count(struct flow_table *table);
63-
void ovs_flow_tbl_destroy(struct flow_table *table);
63+
void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred);
6464
int ovs_flow_tbl_flush(struct flow_table *flow_table);
6565

6666
int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,

0 commit comments

Comments
 (0)