Skip to contentSkip to conversation

net: openvswitch: masks cache enhancements

PATCH Superseded Eelco Chaudron 2 participants 9 messages Jul 29, 2020 – Jul 30, 2020

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Eelco Chaudron

author maintainer
#0 lore raw

v3 cover net: openvswitch: masks cache enhancements

This patchset adds two enhancements to the Open vSwitch masks cache.

Changes in v3 [patch 2/2 only]:
- Use is_power_of_2() function
- Use array_size() function
- Fix remaining sparse errors

Changes in v2 [patch 2/2 only]:
- Fix sparse warnings
- Fix netlink policy items reported by Florian Westphal

Eelco Chaudron (2):
net: openvswitch: add masks cache hit counter
net: openvswitch: make masks cache size configurable

 include/uapi/linux/openvswitch.h |    3 +
 net/openvswitch/datapath.c       |   19 ++++++
 net/openvswitch/datapath.h       |    3 +
 net/openvswitch/flow_table.c     |  116 ++++++++++++++++++++++++++++++++------
 net/openvswitch/flow_table.h     |   13 ++++
 5 files changed, 132 insertions(+), 22 deletions(-)
author maintainer
#1 lore raw

PATCH v3 1/2 net: openvswitch: add masks cache hit counter

Add a counter that counts the number of masks cache hits, and export it through the megaflow netlink statistics.

---
 include/uapi/linux/openvswitch.h |    2 +-
 net/openvswitch/datapath.c       |    5 ++++-
 net/openvswitch/datapath.h       |    3 +++
 net/openvswitch/flow_table.c     |   19 ++++++++++++++-----
 net/openvswitch/flow_table.h     |    3 ++-
 5 files changed, 24 insertions(+), 8 deletions(-)
include/uapi/linux/openvswitch.h +1 -1
index 9b14519e74d9..7cb76e5ca7cf 100644
@@ -102,8 +102,8 @@ struct ovs_dp_megaflow_stats {
 	__u64 n_mask_hit;	 /* Number of masks used for flow lookups. */
 	__u32 n_masks;		 /* Number of masks for the datapath. */
 	__u32 pad0;		 /* Pad for future expension. */
+	__u64 n_cache_hit;       /* Number of cache matches for flow lookups. */
 	__u64 pad1;		 /* Pad for future expension. */
-	__u64 pad2;		 /* Pad for future expension. */
 };
 
 struct ovs_vport_stats {
net/openvswitch/datapath.c +4 -1
index 95805f0e27bd..a54df1fe3ec4 100644
@@ -225,13 +225,14 @@ void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
 	struct dp_stats_percpu *stats;
 	u64 *stats_counter;
 	u32 n_mask_hit;
+	u32 n_cache_hit;
 	int error;
 
 	stats = this_cpu_ptr(dp->stats_percpu);
 
 	/* Look up flow. */
 	flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
-					 &n_mask_hit);
+					 &n_mask_hit, &n_cache_hit);
 	if (unlikely(!flow)) {
 		struct dp_upcall_info upcall;
 
@@ -262,6 +263,7 @@ void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
 	u64_stats_update_begin(&stats->syncp);
 	(*stats_counter)++;
 	stats->n_mask_hit += n_mask_hit;
+	stats->n_cache_hit += n_cache_hit;
 	u64_stats_update_end(&stats->syncp);
 }
 
@@ -699,6 +701,7 @@ static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
 		stats->n_missed += local_stats.n_missed;
 		stats->n_lost += local_stats.n_lost;
 		mega_stats->n_mask_hit += local_stats.n_mask_hit;
+		mega_stats->n_cache_hit += local_stats.n_cache_hit;
 	}
 }
 
net/openvswitch/datapath.h +3 -0
index 697a2354194b..86d78613edb4 100644
@@ -38,12 +38,15 @@
  * @n_mask_hit: Number of masks looked up for flow match.
  *   @n_mask_hit / (@n_hit + @n_missed)  will be the average masks looked
  *   up per packet.
+ * @n_cache_hit: The number of received packets that had their mask found using
+ * the mask cache.
  */
 struct dp_stats_percpu {
 	u64 n_hit;
 	u64 n_missed;
 	u64 n_lost;
 	u64 n_mask_hit;
+	u64 n_cache_hit;
 	struct u64_stats_sync syncp;
 };
 
net/openvswitch/flow_table.c +14 -5
index af22c9ee28dd..a5912ea05352 100644
@@ -667,6 +667,7 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl,
 				   struct mask_array *ma,
 				   const struct sw_flow_key *key,
 				   u32 *n_mask_hit,
+				   u32 *n_cache_hit,
 				   u32 *index)
 {
 	u64 *usage_counters = this_cpu_ptr(ma->masks_usage_cntr);
@@ -682,6 +683,7 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl,
 				u64_stats_update_begin(&ma->syncp);
 				usage_counters[*index]++;
 				u64_stats_update_end(&ma->syncp);
+				(*n_cache_hit)++;
 				return flow;
 			}
 		}
@@ -719,7 +721,8 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl,
 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 					  const struct sw_flow_key *key,
 					  u32 skb_hash,
-					  u32 *n_mask_hit)
+					  u32 *n_mask_hit,
+					  u32 *n_cache_hit)
 {
 	struct mask_array *ma = rcu_dereference(tbl->mask_array);
 	struct table_instance *ti = rcu_dereference(tbl->ti);
@@ -729,10 +732,13 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 	int seg;
 
 	*n_mask_hit = 0;
+	*n_cache_hit = 0;
 	if (unlikely(!skb_hash)) {
 		u32 mask_index = 0;
+		u32 cache = 0;
 
-		return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
+		return flow_lookup(tbl, ti, ma, key, n_mask_hit, &cache,
+				   &mask_index);
 	}
 
 	/* Pre and post recirulation flows usually have the same skb_hash
@@ -753,7 +759,7 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 		e = &entries[index];
 		if (e->skb_hash == skb_hash) {
 			flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
-					   &e->mask_index);
+					   n_cache_hit, &e->mask_index);
 			if (!flow)
 				e->skb_hash = 0;
 			return flow;
@@ -766,10 +772,12 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 	}
 
 	/* Cache miss, do full lookup. */
-	flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index);
+	flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, n_cache_hit,
+			   &ce->mask_index);
 	if (flow)
 		ce->skb_hash = skb_hash;
 
+	*n_cache_hit = 0;
 	return flow;
 }
 
@@ -779,9 +787,10 @@ struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
 	struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
 	u32 __always_unused n_mask_hit;
+	u32 __always_unused n_cache_hit;
 	u32 index = 0;
 
-	return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
+	return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &n_cache_hit, &index);
 }
 
 struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
net/openvswitch/flow_table.h +2 -1
index 1f664b050e3b..325e939371d8 100644
@@ -82,7 +82,8 @@ struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *table,
 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,
 					  const struct sw_flow_key *,
 					  u32 skb_hash,
-					  u32 *n_mask_hit);
+					  u32 *n_mask_hit,
+					  u32 *n_cache_hit);
 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *,
 				    const struct sw_flow_key *);
 struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
author maintainer
#2 lore raw

PATCH v3 2/2 net: openvswitch: make masks cache size configurable

In reply to Eelco Chaudron

This patch makes the masks cache size configurable, or with a size of 0, disable it.

---
Changes in v3:
- Use is_power_of_2() function
- Use array_size() function
- Fix remaining sparse errors

Changes in v2:
- Fix sparse warnings
- Fix netlink policy items reported by Florian Westphal

 include/uapi/linux/openvswitch.h |    1 
 net/openvswitch/datapath.c       |   14 +++++
 net/openvswitch/flow_table.c     |   97 +++++++++++++++++++++++++++++++++-----
 net/openvswitch/flow_table.h     |   10 ++++
 4 files changed, 108 insertions(+), 14 deletions(-)
include/uapi/linux/openvswitch.h +1 -0
index 7cb76e5ca7cf..8300cc29dec8 100644
@@ -86,6 +86,7 @@ enum ovs_datapath_attr {
 	OVS_DP_ATTR_MEGAFLOW_STATS,	/* struct ovs_dp_megaflow_stats */
 	OVS_DP_ATTR_USER_FEATURES,	/* OVS_DP_F_*  */
 	OVS_DP_ATTR_PAD,
+	OVS_DP_ATTR_MASKS_CACHE_SIZE,
 	__OVS_DP_ATTR_MAX
 };
 
net/openvswitch/datapath.c +14 -0
index a54df1fe3ec4..114b2ddb8037 100644
@@ -1498,6 +1498,7 @@ static size_t ovs_dp_cmd_msg_size(void)
 	msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
 	msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
 	msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
+	msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_MASKS_CACHE_SIZE */
 
 	return msgsize;
 }
@@ -1535,6 +1536,10 @@ static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
 	if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
 		goto nla_put_failure;
 
+	if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
+			ovs_flow_tbl_masks_cache_size(&dp->table)))
+		goto nla_put_failure;
+
 	genlmsg_end(skb, ovs_header);
 	return 0;
 
@@ -1599,6 +1604,13 @@ static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
 #endif
 	}
 
+	if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
+		u32 cache_size;
+
+		cache_size = nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
+		ovs_flow_tbl_masks_cache_resize(&dp->table, cache_size);
+	}
+
 	dp->user_features = user_features;
 
 	if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
@@ -1894,6 +1906,8 @@ static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
 	[OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
 	[OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
 	[OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
+	[OVS_DP_ATTR_MASKS_CACHE_SIZE] =  NLA_POLICY_RANGE(NLA_U32, 0,
+		PCPU_MIN_UNIT_SIZE / sizeof(struct mask_cache_entry)),
 };
 
 static const struct genl_ops dp_datapath_genl_ops[] = {
net/openvswitch/flow_table.c +84 -13

167 lines not on this page · raw message

net/openvswitch/flow_table.h +9 -1
index 325e939371d8..f2dba952db2f 100644
@@ -27,6 +27,12 @@ struct mask_cache_entry {
 	u32 mask_index;
 };
 
+struct mask_cache {
+	struct rcu_head rcu;
+	u32 cache_size;  /* Must be ^2 value. */
+	struct mask_cache_entry __percpu *mask_cache;
+};
+
 struct mask_count {
 	int index;
 	u64 counter;
@@ -53,7 +59,7 @@ struct table_instance {
 struct flow_table {
 	struct table_instance __rcu *ti;
 	struct table_instance __rcu *ufid_ti;
-	struct mask_cache_entry __percpu *mask_cache;
+	struct mask_cache __rcu *mask_cache;
 	struct mask_array __rcu *mask_array;
 	unsigned long last_rehash;
 	unsigned int count;
@@ -77,6 +83,8 @@ int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
 			const struct sw_flow_mask *mask);
 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow);
 int  ovs_flow_tbl_num_masks(const struct flow_table *table);
+u32  ovs_flow_tbl_masks_cache_size(const struct flow_table *table);
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size);
 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *table,
 				       u32 *bucket, u32 *idx);
 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,
#3 lore raw

Re: [PATCH net-next v3 2/2] net: openvswitch: make masks cache size configurable

Quoted 58 lines · Eelco Chaudron

This patch makes the masks cache size configurable, or with a size of 0, disable it.

---
Changes in v3:
- Use is_power_of_2() function
- Use array_size() function
- Fix remaining sparse errors

Changes in v2:
- Fix sparse warnings
- Fix netlink policy items reported by Florian Westphal

 include/uapi/linux/openvswitch.h |    1
 net/openvswitch/datapath.c       |   14 +++++
 net/openvswitch/flow_table.c     |   97 +++++++++++++++++++++++++++++++++-----
 net/openvswitch/flow_table.h     |   10 ++++
 4 files changed, 108 insertions(+), 14 deletions(-)
include/uapi/linux/openvswitch.h +1 -0
index 7cb76e5ca7cf..8300cc29dec8 100644
@@ -86,6 +86,7 @@ enum ovs_datapath_attr {
        OVS_DP_ATTR_MEGAFLOW_STATS,     /* struct ovs_dp_megaflow_stats */
        OVS_DP_ATTR_USER_FEATURES,      /* OVS_DP_F_*  */
        OVS_DP_ATTR_PAD,
+       OVS_DP_ATTR_MASKS_CACHE_SIZE,
        __OVS_DP_ATTR_MAX
 };
 
net/openvswitch/datapath.c +5 -0
index a54df1fe3ec4..114b2ddb8037 100644
@@ -1498,6 +1498,7 @@ static size_t ovs_dp_cmd_msg_size(void)
        msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
        msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
        msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
+       msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_MASKS_CACHE_SIZE */
 
        return msgsize;
 }
@@ -1535,6 +1536,10 @@ static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
        if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
                goto nla_put_failure;
 
+       if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
+                       ovs_flow_tbl_masks_cache_size(&dp->table)))
+               goto nla_put_failure;
+
        genlmsg_end(skb, ovs_header);
        return 0;
 

net/openvswitch/datapath.c static int ovs_dp_change(struct datapath *dp, struct nlattr *a[]) · line 1611

Quoted 8 lines
net/openvswitch/datapath.c +5 -0
@@ -1599,6 +1604,13 @@ static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
 #endif
        }
 
+       if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
+               u32 cache_size;
+
+               cache_size = nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
+               ovs_flow_tbl_masks_cache_resize(&dp->table, cache_size);

Do we should return error code, if we can't change the "mask_cache" size ? for example, -EINVAL, -ENOMEM

Quoted 29 lines
net/openvswitch/datapath.c +4 -0
+       }
+
        dp->user_features = user_features;
 
        if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
@@ -1894,6 +1906,8 @@ static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
        [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
        [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
        [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
+       [OVS_DP_ATTR_MASKS_CACHE_SIZE] =  NLA_POLICY_RANGE(NLA_U32, 0,
+               PCPU_MIN_UNIT_SIZE / sizeof(struct mask_cache_entry)),
 };
 
 static const struct genl_ops dp_datapath_genl_ops[] = {
net/openvswitch/flow_table.c +1 -1
index a5912ea05352..5280aeeef628 100644
@@ -38,8 +38,8 @@
 #define MASK_ARRAY_SIZE_MIN    16
 #define REHASH_INTERVAL                (10 * 60 * HZ)
 
+#define MC_DEFAULT_HASH_ENTRIES        256
 #define MC_HASH_SHIFT          8
-#define MC_HASH_ENTRIES                (1u << MC_HASH_SHIFT)
 #define MC_HASH_SEGS           ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT)
 
 static struct kmem_cache *flow_cache;

net/openvswitch/flow_table.c static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) · line 347

Quoted 7 lines
net/openvswitch/flow_table.c +4 -0
@@ -341,15 +341,75 @@ static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
        }
 }
 
+static void __mask_cache_destroy(struct mask_cache *mc)
+{
+       if (mc->mask_cache)
+               free_percpu(mc->mask_cache);

free_percpu the NULL is safe. we can remove the "if".

Quoted 194 lines
net/openvswitch/flow_table.c +78 -12
+       kfree(mc);
+}
+
+static void mask_cache_rcu_cb(struct rcu_head *rcu)
+{
+       struct mask_cache *mc = container_of(rcu, struct mask_cache, rcu);
+
+       __mask_cache_destroy(mc);
+}
+
+static struct mask_cache *tbl_mask_cache_alloc(u32 size)
+{
+       struct mask_cache_entry __percpu *cache = NULL;
+       struct mask_cache *new;
+
+       /* Only allow size to be 0, or a power of 2, and does not exceed
+        * percpu allocation size.
+        */
+       if ((!is_power_of_2(size) && size != 0) ||
+           (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)
+               return NULL;
+       new = kzalloc(sizeof(*new), GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       new->cache_size = size;
+       if (new->cache_size > 0) {
+               cache = __alloc_percpu(array_size(sizeof(struct mask_cache_entry),
+                                                 new->cache_size),
+                                      __alignof__(struct mask_cache_entry));
+               if (!cache) {
+                       kfree(new);
+                       return NULL;
+               }
+       }
+
+       new->mask_cache = cache;
+       return new;
+}
+
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size)
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_cache *new;
+
+       if (size == mc->cache_size)
+               return;
+
+       new = tbl_mask_cache_alloc(size);
+       if (!new)
+               return;
+
+       rcu_assign_pointer(table->mask_cache, new);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+}
+
 int ovs_flow_tbl_init(struct flow_table *table)
 {
        struct table_instance *ti, *ufid_ti;
+       struct mask_cache *mc;
        struct mask_array *ma;
 
-       table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) *
-                                          MC_HASH_ENTRIES,
-                                          __alignof__(struct mask_cache_entry));
-       if (!table->mask_cache)
+       mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);
+       if (!mc)
                return -ENOMEM;
 
        ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
@@ -367,6 +427,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
        rcu_assign_pointer(table->ti, ti);
        rcu_assign_pointer(table->ufid_ti, ufid_ti);
        rcu_assign_pointer(table->mask_array, ma);
+       rcu_assign_pointer(table->mask_cache, mc);
        table->last_rehash = jiffies;
        table->count = 0;
        table->ufid_count = 0;
@@ -377,7 +438,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
 free_mask_array:
        __mask_array_destroy(ma);
 free_mask_cache:
-       free_percpu(table->mask_cache);
+       __mask_cache_destroy(mc);
        return -ENOMEM;
 }
 
@@ -453,9 +514,11 @@ void ovs_flow_tbl_destroy(struct flow_table *table)
 {
        struct table_instance *ti = rcu_dereference_raw(table->ti);
        struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
 
-       free_percpu(table->mask_cache);
-       call_rcu(&table->mask_array->rcu, mask_array_rcu_cb);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+       call_rcu(&ma->rcu, mask_array_rcu_cb);
        table_instance_destroy(table, ti, ufid_ti, false);
 }
 
@@ -724,6 +787,7 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
                                          u32 *n_mask_hit,
                                          u32 *n_cache_hit)
 {
+       struct mask_cache *mc = rcu_dereference(tbl->mask_cache);
        struct mask_array *ma = rcu_dereference(tbl->mask_array);
        struct table_instance *ti = rcu_dereference(tbl->ti);
        struct mask_cache_entry *entries, *ce;
@@ -733,7 +797,7 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 
        *n_mask_hit = 0;
        *n_cache_hit = 0;
-       if (unlikely(!skb_hash)) {
+       if (unlikely(!skb_hash || mc->cache_size == 0)) {
                u32 mask_index = 0;
                u32 cache = 0;
 
@@ -749,11 +813,11 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 
        ce = NULL;
        hash = skb_hash;
-       entries = this_cpu_ptr(tbl->mask_cache);
+       entries = this_cpu_ptr(mc->mask_cache);
 
        /* Find the cache entry 'ce' to operate on. */
        for (seg = 0; seg < MC_HASH_SEGS; seg++) {
-               int index = hash & (MC_HASH_ENTRIES - 1);
+               int index = hash & (mc->cache_size - 1);
                struct mask_cache_entry *e;
 
                e = &entries[index];
@@ -867,6 +931,13 @@ int ovs_flow_tbl_num_masks(const struct flow_table *table)
        return READ_ONCE(ma->count);
 }
 
+u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table)
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+
+       return READ_ONCE(mc->cache_size);
+}
+
 static struct table_instance *table_instance_expand(struct table_instance *ti,
                                                    bool ufid)
 {
@@ -1095,8 +1166,8 @@ void ovs_flow_masks_rebalance(struct flow_table *table)
        for (i = 0; i < masks_entries; i++) {
                int index = masks_and_count[i].index;
 
-               new->masks[new->count++] =
-                       rcu_dereference_ovsl(ma->masks[index]);
+               if (ovsl_dereference(ma->masks[index]))
+                       new->masks[new->count++] = ma->masks[index];
        }
 
        rcu_assign_pointer(table->mask_array, new);
net/openvswitch/flow_table.h +9 -1
index 325e939371d8..f2dba952db2f 100644
@@ -27,6 +27,12 @@ struct mask_cache_entry {
        u32 mask_index;
 };
 
+struct mask_cache {
+       struct rcu_head rcu;
+       u32 cache_size;  /* Must be ^2 value. */
+       struct mask_cache_entry __percpu *mask_cache;
+};
+
 struct mask_count {
        int index;
        u64 counter;
@@ -53,7 +59,7 @@ struct table_instance {
 struct flow_table {
        struct table_instance __rcu *ti;
        struct table_instance __rcu *ufid_ti;
-       struct mask_cache_entry __percpu *mask_cache;
+       struct mask_cache __rcu *mask_cache;
        struct mask_array __rcu *mask_array;
        unsigned long last_rehash;
        unsigned int count;
@@ -77,6 +83,8 @@ int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
                        const struct sw_flow_mask *mask);
 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow);
 int  ovs_flow_tbl_num_masks(const struct flow_table *table);
+u32  ovs_flow_tbl_masks_cache_size(const struct flow_table *table);
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size);
 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *table,
                                       u32 *bucket, u32 *idx);
 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,
Signature · 1 line
Best regards, Tonghao
author maintainer
#4 lore raw

Re: [PATCH net-next v3 2/2] net: openvswitch: make masks cache size configurable

Thanks Tonghao for the review, see comments inline below!

If I get no more reviews by the end of the week I’ll send out a v4.

//Eelco

Quoted 70 lines · Tonghao Zhang
Quoted 69 lines · Eelco Chaudron

This patch makes the masks cache size configurable, or with a size of 0, disable it.

---
Changes in v3:
- Use is_power_of_2() function
- Use array_size() function
- Fix remaining sparse errors

Changes in v2:
- Fix sparse warnings
- Fix netlink policy items reported by Florian Westphal

 include/uapi/linux/openvswitch.h |    1
 net/openvswitch/datapath.c       |   14 +++++
 net/openvswitch/flow_table.c     |   97 
+++++++++++++++++++++++++++++++++-----
 net/openvswitch/flow_table.h     |   10 ++++
 4 files changed, 108 insertions(+), 14 deletions(-)
include/uapi/linux/openvswitch.h +0 -0
index 7cb76e5ca7cf..8300cc29dec8 100644
@@ -86,6 +86,7 @@ enum ovs_datapath_attr {
        OVS_DP_ATTR_MEGAFLOW_STATS,     /* struct 

ovs_dp_megaflow_stats */

        OVS_DP_ATTR_USER_FEATURES,      /* OVS_DP_F_*  */
        OVS_DP_ATTR_PAD,
include/uapi/linux/openvswitch.h +1 -0
+       OVS_DP_ATTR_MASKS_CACHE_SIZE,
        __OVS_DP_ATTR_MAX
 };
net/openvswitch/datapath.c +0 -0
index a54df1fe3ec4..114b2ddb8037 100644
@@ -1498,6 +1498,7 @@ static size_t ovs_dp_cmd_msg_size(void)
        msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
        msgsize += nla_total_size_64bit(sizeof(struct 
 ovs_dp_megaflow_stats));
        msgsize += nla_total_size(sizeof(u32)); /* 

OVS_DP_ATTR_USER_FEATURES */
+ msgsize += nla_total_size(sizeof(u32)); /*
OVS_DP_ATTR_MASKS_CACHE_SIZE */

        return msgsize;

}

net/openvswitch/datapath.c +4 -0
@@ -1535,6 +1536,10 @@ static int ovs_dp_cmd_fill_info(struct 
 datapath *dp, struct sk_buff *skb,
        if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, 
 dp->user_features))
                goto nla_put_failure;
 
+       if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
+                       ovs_flow_tbl_masks_cache_size(&dp->table)))
+               goto nla_put_failure;
+
        genlmsg_end(skb, ovs_header);
        return 0;
net/openvswitch/datapath.c +0 -0
@@ -1599,6 +1604,13 @@ static int ovs_dp_change(struct datapath *dp, 
 struct nlattr *a[])
 #endif
        }
 
Quoted 9 lines

net/openvswitch/datapath.c static int ovs_dp_change(struct datapath *dp, · line 1614

Quoted 7 lines
net/openvswitch/datapath.c +5 -0
+       if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
+               u32 cache_size;
+
+               cache_size = 
 nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
+               ovs_flow_tbl_masks_cache_resize(&dp->table, 
 cache_size);

Do we should return error code, if we can't change the "mask_cache" size ? for example, -EINVAL, -ENOMEM

Initially, I did not do this due to the fact the new value is reported, and on failure, the old value is shown. However thinking about it again, it makes more sense to return an error. Will sent a v4 with the following to return:

-
-void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size) +int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size)

 {
        struct mask_cache *mc = rcu_dereference(table->mask_cache);
        struct mask_cache *new;

        if (size == mc->cache_size)

- return;
+ return 0;
+
+ if (!is_power_of_2(size) && size != 0)
+ return -EINVAL;

        new = tbl_mask_cache_alloc(size);
        if (!new)

- return;
+ return -ENOMEM;

        rcu_assign_pointer(table->mask_cache, new);
        call_rcu(&mc->rcu, mask_cache_rcu_cb);

+
+ return 0;
}

Quoted 38 lines
Quoted 38 lines
net/openvswitch/datapath.c +2 -0
+       }
+
        dp->user_features = user_features;

        if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
net/openvswitch/datapath.c +2 -0
@@ -1894,6 +1906,8 @@ static const struct nla_policy 
 datapath_policy[OVS_DP_ATTR_MAX + 1] = {
        [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = 
 IFNAMSIZ - 1 },
        [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
        [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
+       [OVS_DP_ATTR_MASKS_CACHE_SIZE] =  NLA_POLICY_RANGE(NLA_U32, 
 0,
+               PCPU_MIN_UNIT_SIZE / sizeof(struct 

mask_cache_entry)),
};

static const struct genl_ops dp_datapath_genl_ops[] = {

net/openvswitch/flow_table.c +1 -1
index a5912ea05352..5280aeeef628 100644
@@ -38,8 +38,8 @@
 #define MASK_ARRAY_SIZE_MIN    16
 #define REHASH_INTERVAL                (10 * 60 * HZ)
 
+#define MC_DEFAULT_HASH_ENTRIES        256
 #define MC_HASH_SHIFT          8
-#define MC_HASH_ENTRIES                (1u << MC_HASH_SHIFT)
 #define MC_HASH_SEGS           ((sizeof(uint32_t) * 8) / 
 MC_HASH_SHIFT)
 

static struct kmem_cache *flow_cache;

net/openvswitch/flow_table.c +0 -0
@@ -341,15 +341,75 @@ static void flow_mask_remove(struct flow_table 
 *tbl, struct sw_flow_mask *mask)
        }
 }
 
Quoted 5 lines

net/openvswitch/flow_table.c static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) · line 347

Quoted 4 lines
net/openvswitch/flow_table.c +4 -0
+static void __mask_cache_destroy(struct mask_cache *mc)
+{
+       if (mc->mask_cache)
+               free_percpu(mc->mask_cache);

free_percpu the NULL is safe. we can remove the "if".

Makes sense, will remove the if() check.

Quoted 218 lines
Quoted 214 lines
net/openvswitch/flow_table.c +16 -0
+       kfree(mc);
+}
+
+static void mask_cache_rcu_cb(struct rcu_head *rcu)
+{
+       struct mask_cache *mc = container_of(rcu, struct mask_cache, 
 rcu);
+
+       __mask_cache_destroy(mc);
+}
+
+static struct mask_cache *tbl_mask_cache_alloc(u32 size)
+{
+       struct mask_cache_entry __percpu *cache = NULL;
+       struct mask_cache *new;
+
+       /* Only allow size to be 0, or a power of 2, and does not 

exceed

net/openvswitch/flow_table.c +4 -0
+        * percpu allocation size.
+        */
+       if ((!is_power_of_2(size) && size != 0) ||
+           (size * sizeof(struct mask_cache_entry)) > 

PCPU_MIN_UNIT_SIZE)

net/openvswitch/flow_table.c +8 -0
+               return NULL;
+       new = kzalloc(sizeof(*new), GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       new->cache_size = size;
+       if (new->cache_size > 0) {
+               cache = __alloc_percpu(array_size(sizeof(struct 

mask_cache_entry),

net/openvswitch/flow_table.c +2 -0
+                                                 new->cache_size),
+                                      __alignof__(struct 

mask_cache_entry));

net/openvswitch/flow_table.c +11 -0
+               if (!cache) {
+                       kfree(new);
+                       return NULL;
+               }
+       }
+
+       new->mask_cache = cache;
+       return new;
+}
+
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 

size)

net/openvswitch/flow_table.c +16 -1
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_cache *new;
+
+       if (size == mc->cache_size)
+               return;
+
+       new = tbl_mask_cache_alloc(size);
+       if (!new)
+               return;
+
+       rcu_assign_pointer(table->mask_cache, new);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+}
+
 int ovs_flow_tbl_init(struct flow_table *table)
 {
        struct table_instance *ti, *ufid_ti;
+       struct mask_cache *mc;
        struct mask_array *ma;
 
-       table->mask_cache = __alloc_percpu(sizeof(struct 

mask_cache_entry) *

net/openvswitch/flow_table.c +0 -2
-                                          MC_HASH_ENTRIES,
-                                          __alignof__(struct 

mask_cache_entry));

net/openvswitch/flow_table.c +8 -4
-       if (!table->mask_cache)
+       mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);
+       if (!mc)
                return -ENOMEM;
 
        ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
@@ -367,6 +427,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
        rcu_assign_pointer(table->ti, ti);
        rcu_assign_pointer(table->ufid_ti, ufid_ti);
        rcu_assign_pointer(table->mask_array, ma);
+       rcu_assign_pointer(table->mask_cache, mc);
        table->last_rehash = jiffies;
        table->count = 0;
        table->ufid_count = 0;
@@ -377,7 +438,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
 free_mask_array:
        __mask_array_destroy(ma);
 free_mask_cache:
-       free_percpu(table->mask_cache);
+       __mask_cache_destroy(mc);
        return -ENOMEM;
 }
 
@@ -453,9 +514,11 @@ void ovs_flow_tbl_destroy(struct flow_table 
 *table)
 {
        struct table_instance *ti = rcu_dereference_raw(table->ti);
        struct table_instance *ufid_ti = 
 rcu_dereference_raw(table->ufid_ti);
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_array *ma = 
 rcu_dereference_ovsl(table->mask_array);
 
-       free_percpu(table->mask_cache);
-       call_rcu(&table->mask_array->rcu, mask_array_rcu_cb);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+       call_rcu(&ma->rcu, mask_array_rcu_cb);
        table_instance_destroy(table, ti, ufid_ti, false);

}

net/openvswitch/flow_table.c +1 -0
@@ -724,6 +787,7 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct 
 flow_table *tbl,
                                          u32 *n_mask_hit,
                                          u32 *n_cache_hit)
 {
+       struct mask_cache *mc = rcu_dereference(tbl->mask_cache);
        struct mask_array *ma = rcu_dereference(tbl->mask_array);
        struct table_instance *ti = rcu_dereference(tbl->ti);
        struct mask_cache_entry *entries, *ce;
net/openvswitch/flow_table.c +3 -3
@@ -733,7 +797,7 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct 
 flow_table *tbl,
 
        *n_mask_hit = 0;
        *n_cache_hit = 0;
-       if (unlikely(!skb_hash)) {
+       if (unlikely(!skb_hash || mc->cache_size == 0)) {
                u32 mask_index = 0;
                u32 cache = 0;
@@ -749,11 +813,11 @@ struct sw_flow 
 *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 
        ce = NULL;
        hash = skb_hash;
-       entries = this_cpu_ptr(tbl->mask_cache);
+       entries = this_cpu_ptr(mc->mask_cache);
 
        /* Find the cache entry 'ce' to operate on. */
        for (seg = 0; seg < MC_HASH_SEGS; seg++) {
-               int index = hash & (MC_HASH_ENTRIES - 1);
+               int index = hash & (mc->cache_size - 1);
                struct mask_cache_entry *e;
 
                e = &entries[index];
net/openvswitch/flow_table.c +7 -0
@@ -867,6 +931,13 @@ int ovs_flow_tbl_num_masks(const struct 
 flow_table *table)
        return READ_ONCE(ma->count);
 }
 
+u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table)
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+
+       return READ_ONCE(mc->cache_size);
+}
+
 static struct table_instance *table_instance_expand(struct 
 table_instance *ti,

bool ufid)
{

net/openvswitch/flow_table.c +2 -2
@@ -1095,8 +1166,8 @@ void ovs_flow_masks_rebalance(struct flow_table 
 *table)
        for (i = 0; i < masks_entries; i++) {
                int index = masks_and_count[i].index;
 
-               new->masks[new->count++] =
-                       rcu_dereference_ovsl(ma->masks[index]);
+               if (ovsl_dereference(ma->masks[index]))
+                       new->masks[new->count++] = ma->masks[index];
        }
 
        rcu_assign_pointer(table->mask_array, new);
net/openvswitch/flow_table.h +9 -1
index 325e939371d8..f2dba952db2f 100644
@@ -27,6 +27,12 @@ struct mask_cache_entry {
        u32 mask_index;
 };
 
+struct mask_cache {
+       struct rcu_head rcu;
+       u32 cache_size;  /* Must be ^2 value. */
+       struct mask_cache_entry __percpu *mask_cache;
+};
+
 struct mask_count {
        int index;
        u64 counter;
@@ -53,7 +59,7 @@ struct table_instance {
 struct flow_table {
        struct table_instance __rcu *ti;
        struct table_instance __rcu *ufid_ti;
-       struct mask_cache_entry __percpu *mask_cache;
+       struct mask_cache __rcu *mask_cache;
        struct mask_array __rcu *mask_array;
        unsigned long last_rehash;
        unsigned int count;
@@ -77,6 +83,8 @@ int ovs_flow_tbl_insert(struct flow_table *table, 
 struct sw_flow *flow,
                        const struct sw_flow_mask *mask);
 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow 
 *flow);
 int  ovs_flow_tbl_num_masks(const struct flow_table *table);
+u32  ovs_flow_tbl_masks_cache_size(const struct flow_table *table);
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 
 size);

struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *table,

                                       u32 *bucket, u32 *idx);

struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,

Signature · 1 line
Best regards, Tonghao
Reviewed-by
#5 lore raw

Re: [PATCH net-next v3 2/2] net: openvswitch: make masks cache size configurable

Quoted 101 lines · Eelco Chaudron

Thanks Tonghao for the review, see comments inline below!

If I get no more reviews by the end of the week I’ll send out a v4.

//Eelco

Quoted 72 lines · Tonghao Zhang
Quoted 70 lines · Eelco Chaudron

This patch makes the masks cache size configurable, or with a size of 0, disable it.

---
Changes in v3:
- Use is_power_of_2() function
- Use array_size() function
- Fix remaining sparse errors

Changes in v2:
- Fix sparse warnings
- Fix netlink policy items reported by Florian Westphal

 include/uapi/linux/openvswitch.h |    1
 net/openvswitch/datapath.c       |   14 +++++
 net/openvswitch/flow_table.c     |   97
+++++++++++++++++++++++++++++++++-----
 net/openvswitch/flow_table.h     |   10 ++++
 4 files changed, 108 insertions(+), 14 deletions(-)
include/uapi/linux/openvswitch.h +0 -0
index 7cb76e5ca7cf..8300cc29dec8 100644
@@ -86,6 +86,7 @@ enum ovs_datapath_attr {
        OVS_DP_ATTR_MEGAFLOW_STATS,     /* struct

ovs_dp_megaflow_stats */

        OVS_DP_ATTR_USER_FEATURES,      /* OVS_DP_F_*  */
        OVS_DP_ATTR_PAD,
include/uapi/linux/openvswitch.h +1 -0
+       OVS_DP_ATTR_MASKS_CACHE_SIZE,
        __OVS_DP_ATTR_MAX
 };
net/openvswitch/datapath.c +0 -0
index a54df1fe3ec4..114b2ddb8037 100644
@@ -1498,6 +1498,7 @@ static size_t ovs_dp_cmd_msg_size(void)
        msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
        msgsize += nla_total_size_64bit(sizeof(struct
 ovs_dp_megaflow_stats));
        msgsize += nla_total_size(sizeof(u32)); /*

OVS_DP_ATTR_USER_FEATURES */
+ msgsize += nla_total_size(sizeof(u32)); /*
OVS_DP_ATTR_MASKS_CACHE_SIZE */

        return msgsize;

}

net/openvswitch/datapath.c +4 -0
@@ -1535,6 +1536,10 @@ static int ovs_dp_cmd_fill_info(struct
 datapath *dp, struct sk_buff *skb,
        if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES,
 dp->user_features))
                goto nla_put_failure;
 
+       if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
+                       ovs_flow_tbl_masks_cache_size(&dp->table)))
+               goto nla_put_failure;
+
        genlmsg_end(skb, ovs_header);
        return 0;
net/openvswitch/datapath.c +0 -0
@@ -1599,6 +1604,13 @@ static int ovs_dp_change(struct datapath *dp,
 struct nlattr *a[])
 #endif
        }
 
Quoted 9 lines

net/openvswitch/datapath.c static int ovs_dp_change(struct datapath *dp, · line 1614

Quoted 7 lines
net/openvswitch/datapath.c +5 -0
+       if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
+               u32 cache_size;
+
+               cache_size =
 nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
+               ovs_flow_tbl_masks_cache_resize(&dp->table,
 cache_size);

Do we should return error code, if we can't change the "mask_cache" size ? for example, -EINVAL, -ENOMEM

Initially, I did not do this due to the fact the new value is reported, and on failure, the old value is shown.
However thinking about it again, it makes more sense to return an error. Will sent a v4 with the following to return:

(quoted diff) +0 -2
-
-void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size)

(quoted diff) +1 -0
+int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size)

Review

Quoted 10 lines
(quoted diff) +4 -1
  {
         struct mask_cache *mc = rcu_dereference(table->mask_cache);
         struct mask_cache *new;
 
         if (size == mc->cache_size)
-               return;
+               return 0;
+
+       if (!is_power_of_2(size) && size != 0)
+               return -EINVAL;

add check as below, and we can remove the check in tbl_mask_cache_alloc function. That
function will only return NULL, if there is no memory. And we can comment for tbl_mask_cache_alloc, to indicate that size should be a power of 2.
if ((!is_power_of_2(size) && size != 0) ||
(size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)

Thanks Eelco.

Quoted 278 lines
         new = tbl_mask_cache_alloc(size);
         if (!new)
(quoted diff) +3 -1
-               return;
+               return -ENOMEM;
 
         rcu_assign_pointer(table->mask_cache, new);
         call_rcu(&mc->rcu, mask_cache_rcu_cb);
+
+       return 0;
  }
Quoted 40 lines
Quoted 40 lines
net/openvswitch/datapath.c +2 -0
+       }
+
        dp->user_features = user_features;

        if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
net/openvswitch/datapath.c +2 -0
@@ -1894,6 +1906,8 @@ static const struct nla_policy
 datapath_policy[OVS_DP_ATTR_MAX + 1] = {
        [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len =
 IFNAMSIZ - 1 },
        [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
        [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
+       [OVS_DP_ATTR_MASKS_CACHE_SIZE] =  NLA_POLICY_RANGE(NLA_U32,
 0,
+               PCPU_MIN_UNIT_SIZE / sizeof(struct

mask_cache_entry)),
};

static const struct genl_ops dp_datapath_genl_ops[] = {

net/openvswitch/flow_table.c +1 -1
index a5912ea05352..5280aeeef628 100644
@@ -38,8 +38,8 @@
 #define MASK_ARRAY_SIZE_MIN    16
 #define REHASH_INTERVAL                (10 * 60 * HZ)
 
+#define MC_DEFAULT_HASH_ENTRIES        256
 #define MC_HASH_SHIFT          8
-#define MC_HASH_ENTRIES                (1u << MC_HASH_SHIFT)
 #define MC_HASH_SEGS           ((sizeof(uint32_t) * 8) /
 MC_HASH_SHIFT)
 

static struct kmem_cache *flow_cache;

net/openvswitch/flow_table.c +0 -0
@@ -341,15 +341,75 @@ static void flow_mask_remove(struct flow_table
 *tbl, struct sw_flow_mask *mask)
        }
 }
 
Quoted 5 lines

net/openvswitch/flow_table.c static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) · line 347

Quoted 4 lines
net/openvswitch/flow_table.c +4 -0
+static void __mask_cache_destroy(struct mask_cache *mc)
+{
+       if (mc->mask_cache)
+               free_percpu(mc->mask_cache);

free_percpu the NULL is safe. we can remove the "if".

Makes sense, will remove the if() check.

Quoted 219 lines
Quoted 212 lines
net/openvswitch/flow_table.c +16 -0
+       kfree(mc);
+}
+
+static void mask_cache_rcu_cb(struct rcu_head *rcu)
+{
+       struct mask_cache *mc = container_of(rcu, struct mask_cache,
 rcu);
+
+       __mask_cache_destroy(mc);
+}
+
+static struct mask_cache *tbl_mask_cache_alloc(u32 size)
+{
+       struct mask_cache_entry __percpu *cache = NULL;
+       struct mask_cache *new;
+
+       /* Only allow size to be 0, or a power of 2, and does not

exceed

net/openvswitch/flow_table.c +4 -0
+        * percpu allocation size.
+        */
+       if ((!is_power_of_2(size) && size != 0) ||
+           (size * sizeof(struct mask_cache_entry)) >

PCPU_MIN_UNIT_SIZE)

net/openvswitch/flow_table.c +8 -0
+               return NULL;
+       new = kzalloc(sizeof(*new), GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       new->cache_size = size;
+       if (new->cache_size > 0) {
+               cache = __alloc_percpu(array_size(sizeof(struct

mask_cache_entry),

net/openvswitch/flow_table.c +2 -0
+                                                 new->cache_size),
+                                      __alignof__(struct

mask_cache_entry));

net/openvswitch/flow_table.c +11 -0
+               if (!cache) {
+                       kfree(new);
+                       return NULL;
+               }
+       }
+
+       new->mask_cache = cache;
+       return new;
+}
+
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size)

net/openvswitch/flow_table.c +16 -1
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_cache *new;
+
+       if (size == mc->cache_size)
+               return;
+
+       new = tbl_mask_cache_alloc(size);
+       if (!new)
+               return;
+
+       rcu_assign_pointer(table->mask_cache, new);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+}
+
 int ovs_flow_tbl_init(struct flow_table *table)
 {
        struct table_instance *ti, *ufid_ti;
+       struct mask_cache *mc;
        struct mask_array *ma;
 
-       table->mask_cache = __alloc_percpu(sizeof(struct

mask_cache_entry) *

net/openvswitch/flow_table.c +0 -2
-                                          MC_HASH_ENTRIES,
-                                          __alignof__(struct

mask_cache_entry));

net/openvswitch/flow_table.c +8 -4
-       if (!table->mask_cache)
+       mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);
+       if (!mc)
                return -ENOMEM;
 
        ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
@@ -367,6 +427,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
        rcu_assign_pointer(table->ti, ti);
        rcu_assign_pointer(table->ufid_ti, ufid_ti);
        rcu_assign_pointer(table->mask_array, ma);
+       rcu_assign_pointer(table->mask_cache, mc);
        table->last_rehash = jiffies;
        table->count = 0;
        table->ufid_count = 0;
@@ -377,7 +438,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
 free_mask_array:
        __mask_array_destroy(ma);
 free_mask_cache:
-       free_percpu(table->mask_cache);
+       __mask_cache_destroy(mc);
        return -ENOMEM;
 }
 
@@ -453,9 +514,11 @@ void ovs_flow_tbl_destroy(struct flow_table
 *table)
 {
        struct table_instance *ti = rcu_dereference_raw(table->ti);
        struct table_instance *ufid_ti =
 rcu_dereference_raw(table->ufid_ti);
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_array *ma =
 rcu_dereference_ovsl(table->mask_array);
 
-       free_percpu(table->mask_cache);
-       call_rcu(&table->mask_array->rcu, mask_array_rcu_cb);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+       call_rcu(&ma->rcu, mask_array_rcu_cb);
        table_instance_destroy(table, ti, ufid_ti, false);

}

net/openvswitch/flow_table.c +1 -0
@@ -724,6 +787,7 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct
 flow_table *tbl,
                                          u32 *n_mask_hit,
                                          u32 *n_cache_hit)
 {
+       struct mask_cache *mc = rcu_dereference(tbl->mask_cache);
        struct mask_array *ma = rcu_dereference(tbl->mask_array);
        struct table_instance *ti = rcu_dereference(tbl->ti);
        struct mask_cache_entry *entries, *ce;
net/openvswitch/flow_table.c +3 -3
@@ -733,7 +797,7 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct
 flow_table *tbl,
 
        *n_mask_hit = 0;
        *n_cache_hit = 0;
-       if (unlikely(!skb_hash)) {
+       if (unlikely(!skb_hash || mc->cache_size == 0)) {
                u32 mask_index = 0;
                u32 cache = 0;
@@ -749,11 +813,11 @@ struct sw_flow
 *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 
        ce = NULL;
        hash = skb_hash;
-       entries = this_cpu_ptr(tbl->mask_cache);
+       entries = this_cpu_ptr(mc->mask_cache);
 
        /* Find the cache entry 'ce' to operate on. */
        for (seg = 0; seg < MC_HASH_SEGS; seg++) {
-               int index = hash & (MC_HASH_ENTRIES - 1);
+               int index = hash & (mc->cache_size - 1);
                struct mask_cache_entry *e;
 
                e = &entries[index];
net/openvswitch/flow_table.c +7 -0
@@ -867,6 +931,13 @@ int ovs_flow_tbl_num_masks(const struct
 flow_table *table)
        return READ_ONCE(ma->count);
 }
 
+u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table)
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+
+       return READ_ONCE(mc->cache_size);
+}
+
 static struct table_instance *table_instance_expand(struct
 table_instance *ti,

bool ufid)
{

net/openvswitch/flow_table.c +2 -2
@@ -1095,8 +1166,8 @@ void ovs_flow_masks_rebalance(struct flow_table
 *table)
        for (i = 0; i < masks_entries; i++) {
                int index = masks_and_count[i].index;
 
-               new->masks[new->count++] =
-                       rcu_dereference_ovsl(ma->masks[index]);
+               if (ovsl_dereference(ma->masks[index]))
+                       new->masks[new->count++] = ma->masks[index];
        }
 
        rcu_assign_pointer(table->mask_array, new);
net/openvswitch/flow_table.h +9 -1
index 325e939371d8..f2dba952db2f 100644
@@ -27,6 +27,12 @@ struct mask_cache_entry {
        u32 mask_index;
 };
 
+struct mask_cache {
+       struct rcu_head rcu;
+       u32 cache_size;  /* Must be ^2 value. */
+       struct mask_cache_entry __percpu *mask_cache;
+};
+
 struct mask_count {
        int index;
        u64 counter;
@@ -53,7 +59,7 @@ struct table_instance {
 struct flow_table {
        struct table_instance __rcu *ti;
        struct table_instance __rcu *ufid_ti;
-       struct mask_cache_entry __percpu *mask_cache;
+       struct mask_cache __rcu *mask_cache;
        struct mask_array __rcu *mask_array;
        unsigned long last_rehash;
        unsigned int count;
@@ -77,6 +83,8 @@ int ovs_flow_tbl_insert(struct flow_table *table,
 struct sw_flow *flow,
                        const struct sw_flow_mask *mask);
 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow
 *flow);
 int  ovs_flow_tbl_num_masks(const struct flow_table *table);
+u32  ovs_flow_tbl_masks_cache_size(const struct flow_table *table);
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32
 size);

struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *table,

Quoted 3 lines
                                       u32 *bucket, u32 *idx);

struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,

--
Best regards, Tonghao

Signature · 1 line
Best regards, Tonghao
author maintainer
#6 lore raw

Re: [PATCH net-next v3 2/2] net: openvswitch: make masks cache size configurable

Quoted 109 lines · Tonghao Zhang
Quoted 108 lines · Eelco Chaudron

Thanks Tonghao for the review, see comments inline below!

If I get no more reviews by the end of the week I’ll send out a v4.

//Eelco

Quoted 74 lines · Tonghao Zhang
Quoted 72 lines · Eelco Chaudron

This patch makes the masks cache size configurable, or with a size of 0, disable it.

---
Changes in v3:
- Use is_power_of_2() function
- Use array_size() function
- Fix remaining sparse errors

Changes in v2:
- Fix sparse warnings
- Fix netlink policy items reported by Florian Westphal

 include/uapi/linux/openvswitch.h |    1
 net/openvswitch/datapath.c       |   14 +++++
 net/openvswitch/flow_table.c     |   97
+++++++++++++++++++++++++++++++++-----
 net/openvswitch/flow_table.h     |   10 ++++
 4 files changed, 108 insertions(+), 14 deletions(-)
include/uapi/linux/openvswitch.h +0 -0
index 7cb76e5ca7cf..8300cc29dec8 100644
@@ -86,6 +86,7 @@ enum ovs_datapath_attr {
        OVS_DP_ATTR_MEGAFLOW_STATS,     /* struct

ovs_dp_megaflow_stats */

        OVS_DP_ATTR_USER_FEATURES,      /* OVS_DP_F_*  */
        OVS_DP_ATTR_PAD,
include/uapi/linux/openvswitch.h +1 -0
+       OVS_DP_ATTR_MASKS_CACHE_SIZE,
        __OVS_DP_ATTR_MAX
 };
net/openvswitch/datapath.c +0 -0
index a54df1fe3ec4..114b2ddb8037 100644
@@ -1498,6 +1498,7 @@ static size_t ovs_dp_cmd_msg_size(void)
        msgsize += nla_total_size_64bit(sizeof(struct 
 ovs_dp_stats));
        msgsize += nla_total_size_64bit(sizeof(struct
 ovs_dp_megaflow_stats));
        msgsize += nla_total_size(sizeof(u32)); /*

OVS_DP_ATTR_USER_FEATURES */
+ msgsize += nla_total_size(sizeof(u32)); /*
OVS_DP_ATTR_MASKS_CACHE_SIZE */

        return msgsize;

}

net/openvswitch/datapath.c +4 -0
@@ -1535,6 +1536,10 @@ static int ovs_dp_cmd_fill_info(struct
 datapath *dp, struct sk_buff *skb,
        if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES,
 dp->user_features))
                goto nla_put_failure;
 
+       if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
+                       ovs_flow_tbl_masks_cache_size(&dp->table)))
+               goto nla_put_failure;
+
        genlmsg_end(skb, ovs_header);
        return 0;
net/openvswitch/datapath.c +0 -0
@@ -1599,6 +1604,13 @@ static int ovs_dp_change(struct datapath 
 *dp,
 struct nlattr *a[])
 #endif
        }
 
Quoted 9 lines
Quoted 7 lines
net/openvswitch/datapath.c +5 -0
+       if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
+               u32 cache_size;
+
+               cache_size =
 nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
+               ovs_flow_tbl_masks_cache_resize(&dp->table,

cache_size);

Do we should return error code, if we can't change the "mask_cache" size ? for example, -EINVAL, -ENOMEM

Initially, I did not do this due to the fact the new value is reported, and on failure, the old value is shown.
However thinking about it again, it makes more sense to return an error. Will sent a v4 with the following to return:

(quoted diff) +0 -2
-
-void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size)
+int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size)

  {
         struct mask_cache *mc = rcu_dereference(table->mask_cache);
         struct mask_cache *new;
Quoted 14 lines

Review

Quoted 6 lines
         if (size == mc->cache_size)
(quoted diff) +4 -1
-               return;
+               return 0;
+
+       if (!is_power_of_2(size) && size != 0)
+               return -EINVAL;

add check as below, and we can remove the check in tbl_mask_cache_alloc function. That
function will only return NULL, if there is no memory. And we can comment for tbl_mask_cache_alloc, to indicate that size should be a power of 2.
if ((!is_power_of_2(size) && size != 0) ||
(size * sizeof(struct mask_cache_entry)) >
PCPU_MIN_UNIT_SIZE)

I do not think the extra check hurts as it’s not in any fast path.
The comment would easily be missed, especially if someone changes the default value, so I would prefer to keep it as is.

Quoted 293 lines

Thanks Eelco.

Quoted 286 lines
         new = tbl_mask_cache_alloc(size);
         if (!new)
(quoted diff) +3 -1
-               return;
+               return -ENOMEM;
 
         rcu_assign_pointer(table->mask_cache, new);
         call_rcu(&mc->rcu, mask_cache_rcu_cb);
+
+       return 0;
  }
Quoted 41 lines
Quoted 41 lines
net/openvswitch/datapath.c +4 -0
+       }
+
        dp->user_features = user_features;
 
        if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
@@ -1894,6 +1906,8 @@ static const struct nla_policy
 datapath_policy[OVS_DP_ATTR_MAX + 1] = {
        [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len =
 IFNAMSIZ - 1 },
        [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
        [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
+       [OVS_DP_ATTR_MASKS_CACHE_SIZE] =  NLA_POLICY_RANGE(NLA_U32,
 0,
+               PCPU_MIN_UNIT_SIZE / sizeof(struct

mask_cache_entry)),
};

static const struct genl_ops dp_datapath_genl_ops[] = {

net/openvswitch/flow_table.c +1 -1
index a5912ea05352..5280aeeef628 100644
@@ -38,8 +38,8 @@
 #define MASK_ARRAY_SIZE_MIN    16
 #define REHASH_INTERVAL                (10 * 60 * HZ)
 
+#define MC_DEFAULT_HASH_ENTRIES        256
 #define MC_HASH_SHIFT          8
-#define MC_HASH_ENTRIES                (1u << MC_HASH_SHIFT)
 #define MC_HASH_SEGS           ((sizeof(uint32_t) * 8) /
 MC_HASH_SHIFT)
 

static struct kmem_cache *flow_cache;

net/openvswitch/flow_table.c +0 -0
@@ -341,15 +341,75 @@ static void flow_mask_remove(struct 

flow_table
*tbl, struct sw_flow_mask *mask)

        }

}

Quoted 5 lines

net/openvswitch/flow_table.c static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) · line 347

Quoted 4 lines
net/openvswitch/flow_table.c +4 -0
+static void __mask_cache_destroy(struct mask_cache *mc)
+{
+       if (mc->mask_cache)
+               free_percpu(mc->mask_cache);

free_percpu the NULL is safe. we can remove the "if".

Makes sense, will remove the if() check.

Quoted 226 lines
Quoted 219 lines
net/openvswitch/flow_table.c +6 -0
+       kfree(mc);
+}
+
+static void mask_cache_rcu_cb(struct rcu_head *rcu)
+{
+       struct mask_cache *mc = container_of(rcu, struct 

mask_cache,
rcu);

net/openvswitch/flow_table.c +10 -0
+
+       __mask_cache_destroy(mc);
+}
+
+static struct mask_cache *tbl_mask_cache_alloc(u32 size)
+{
+       struct mask_cache_entry __percpu *cache = NULL;
+       struct mask_cache *new;
+
+       /* Only allow size to be 0, or a power of 2, and does not

exceed

net/openvswitch/flow_table.c +4 -0
+        * percpu allocation size.
+        */
+       if ((!is_power_of_2(size) && size != 0) ||
+           (size * sizeof(struct mask_cache_entry)) >

PCPU_MIN_UNIT_SIZE)

net/openvswitch/flow_table.c +8 -0
+               return NULL;
+       new = kzalloc(sizeof(*new), GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       new->cache_size = size;
+       if (new->cache_size > 0) {
+               cache = __alloc_percpu(array_size(sizeof(struct

mask_cache_entry),

net/openvswitch/flow_table.c +2 -0
+                                                 new->cache_size),
+                                      __alignof__(struct

mask_cache_entry));

net/openvswitch/flow_table.c +11 -0
+               if (!cache) {
+                       kfree(new);
+                       return NULL;
+               }
+       }
+
+       new->mask_cache = cache;
+       return new;
+}
+
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size)

net/openvswitch/flow_table.c +16 -1
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_cache *new;
+
+       if (size == mc->cache_size)
+               return;
+
+       new = tbl_mask_cache_alloc(size);
+       if (!new)
+               return;
+
+       rcu_assign_pointer(table->mask_cache, new);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+}
+
 int ovs_flow_tbl_init(struct flow_table *table)
 {
        struct table_instance *ti, *ufid_ti;
+       struct mask_cache *mc;
        struct mask_array *ma;
 
-       table->mask_cache = __alloc_percpu(sizeof(struct

mask_cache_entry) *

net/openvswitch/flow_table.c +0 -2
-                                          MC_HASH_ENTRIES,
-                                          __alignof__(struct

mask_cache_entry));

net/openvswitch/flow_table.c +8 -4
-       if (!table->mask_cache)
+       mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);
+       if (!mc)
                return -ENOMEM;
 
        ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
@@ -367,6 +427,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
        rcu_assign_pointer(table->ti, ti);
        rcu_assign_pointer(table->ufid_ti, ufid_ti);
        rcu_assign_pointer(table->mask_array, ma);
+       rcu_assign_pointer(table->mask_cache, mc);
        table->last_rehash = jiffies;
        table->count = 0;
        table->ufid_count = 0;
@@ -377,7 +438,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
 free_mask_array:
        __mask_array_destroy(ma);
 free_mask_cache:
-       free_percpu(table->mask_cache);
+       __mask_cache_destroy(mc);
        return -ENOMEM;
 }
 
@@ -453,9 +514,11 @@ void ovs_flow_tbl_destroy(struct flow_table
 *table)
 {
        struct table_instance *ti = rcu_dereference_raw(table->ti);
        struct table_instance *ufid_ti =
 rcu_dereference_raw(table->ufid_ti);
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_array *ma =
 rcu_dereference_ovsl(table->mask_array);
 
-       free_percpu(table->mask_cache);
-       call_rcu(&table->mask_array->rcu, mask_array_rcu_cb);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+       call_rcu(&ma->rcu, mask_array_rcu_cb);
        table_instance_destroy(table, ti, ufid_ti, false);

}

net/openvswitch/flow_table.c +1 -0
@@ -724,6 +787,7 @@ struct sw_flow 
 *ovs_flow_tbl_lookup_stats(struct
 flow_table *tbl,
                                          u32 *n_mask_hit,
                                          u32 *n_cache_hit)
 {
+       struct mask_cache *mc = rcu_dereference(tbl->mask_cache);
        struct mask_array *ma = rcu_dereference(tbl->mask_array);
        struct table_instance *ti = rcu_dereference(tbl->ti);
        struct mask_cache_entry *entries, *ce;
net/openvswitch/flow_table.c +1 -1
@@ -733,7 +797,7 @@ struct sw_flow 
 *ovs_flow_tbl_lookup_stats(struct
 flow_table *tbl,
 
        *n_mask_hit = 0;
        *n_cache_hit = 0;
-       if (unlikely(!skb_hash)) {
+       if (unlikely(!skb_hash || mc->cache_size == 0)) {
                u32 mask_index = 0;
                u32 cache = 0;
net/openvswitch/flow_table.c +2 -2
@@ -749,11 +813,11 @@ struct sw_flow
 *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 
        ce = NULL;
        hash = skb_hash;
-       entries = this_cpu_ptr(tbl->mask_cache);
+       entries = this_cpu_ptr(mc->mask_cache);
 
        /* Find the cache entry 'ce' to operate on. */
        for (seg = 0; seg < MC_HASH_SEGS; seg++) {
-               int index = hash & (MC_HASH_ENTRIES - 1);
+               int index = hash & (mc->cache_size - 1);
                struct mask_cache_entry *e;
 
                e = &entries[index];
net/openvswitch/flow_table.c +7 -0
@@ -867,6 +931,13 @@ int ovs_flow_tbl_num_masks(const struct
 flow_table *table)
        return READ_ONCE(ma->count);
 }
 
+u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table)
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+
+       return READ_ONCE(mc->cache_size);
+}
+
 static struct table_instance *table_instance_expand(struct
 table_instance *ti,

bool ufid)
{

net/openvswitch/flow_table.c +0 -0
@@ -1095,8 +1166,8 @@ void ovs_flow_masks_rebalance(struct 

flow_table
*table)

        for (i = 0; i < masks_entries; i++) {
                int index = masks_and_count[i].index;
net/openvswitch/flow_table.c +2 -2
-               new->masks[new->count++] =
-                       rcu_dereference_ovsl(ma->masks[index]);
+               if (ovsl_dereference(ma->masks[index]))
+                       new->masks[new->count++] = 
ma->masks[index];
        }

        rcu_assign_pointer(table->mask_array, new);
net/openvswitch/flow_table.h +9 -1
index 325e939371d8..f2dba952db2f 100644
@@ -27,6 +27,12 @@ struct mask_cache_entry {
        u32 mask_index;
 };
 
+struct mask_cache {
+       struct rcu_head rcu;
+       u32 cache_size;  /* Must be ^2 value. */
+       struct mask_cache_entry __percpu *mask_cache;
+};
+
 struct mask_count {
        int index;
        u64 counter;
@@ -53,7 +59,7 @@ struct table_instance {
 struct flow_table {
        struct table_instance __rcu *ti;
        struct table_instance __rcu *ufid_ti;
-       struct mask_cache_entry __percpu *mask_cache;
+       struct mask_cache __rcu *mask_cache;
        struct mask_array __rcu *mask_array;
        unsigned long last_rehash;
        unsigned int count;
@@ -77,6 +83,8 @@ int ovs_flow_tbl_insert(struct flow_table *table,
 struct sw_flow *flow,
                        const struct sw_flow_mask *mask);
 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow
 *flow);
 int  ovs_flow_tbl_num_masks(const struct flow_table *table);
+u32  ovs_flow_tbl_masks_cache_size(const struct flow_table 
 *table);
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size);
struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance
*table,

Quoted 3 lines
                                       u32 *bucket, u32 *idx);

struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,

--
Best regards, Tonghao

Signature · 1 line
Best regards, Tonghao
#7 lore raw

Re: [PATCH net-next v3 2/2] net: openvswitch: make masks cache size configurable

Quoted 133 lines · Eelco Chaudron
Quoted 113 lines · Tonghao Zhang
Quoted 111 lines · Eelco Chaudron

Thanks Tonghao for the review, see comments inline below!

If I get no more reviews by the end of the week I’ll send out a v4.

//Eelco

Quoted 75 lines · Tonghao Zhang
Quoted 73 lines · Eelco Chaudron

This patch makes the masks cache size configurable, or with a size of 0, disable it.

---
Changes in v3:
- Use is_power_of_2() function
- Use array_size() function
- Fix remaining sparse errors

Changes in v2:
- Fix sparse warnings
- Fix netlink policy items reported by Florian Westphal

 include/uapi/linux/openvswitch.h |    1
 net/openvswitch/datapath.c       |   14 +++++
 net/openvswitch/flow_table.c     |   97
+++++++++++++++++++++++++++++++++-----
 net/openvswitch/flow_table.h     |   10 ++++
 4 files changed, 108 insertions(+), 14 deletions(-)
include/uapi/linux/openvswitch.h +0 -0
index 7cb76e5ca7cf..8300cc29dec8 100644
@@ -86,6 +86,7 @@ enum ovs_datapath_attr {
        OVS_DP_ATTR_MEGAFLOW_STATS,     /* struct

ovs_dp_megaflow_stats */

        OVS_DP_ATTR_USER_FEATURES,      /* OVS_DP_F_*  */
        OVS_DP_ATTR_PAD,
include/uapi/linux/openvswitch.h +1 -0
+       OVS_DP_ATTR_MASKS_CACHE_SIZE,
        __OVS_DP_ATTR_MAX
 };
net/openvswitch/datapath.c +0 -0
index a54df1fe3ec4..114b2ddb8037 100644
@@ -1498,6 +1498,7 @@ static size_t ovs_dp_cmd_msg_size(void)
        msgsize += nla_total_size_64bit(sizeof(struct
 ovs_dp_stats));
        msgsize += nla_total_size_64bit(sizeof(struct
 ovs_dp_megaflow_stats));
        msgsize += nla_total_size(sizeof(u32)); /*

OVS_DP_ATTR_USER_FEATURES */
+ msgsize += nla_total_size(sizeof(u32)); /*
OVS_DP_ATTR_MASKS_CACHE_SIZE */

        return msgsize;

}

net/openvswitch/datapath.c +4 -0
@@ -1535,6 +1536,10 @@ static int ovs_dp_cmd_fill_info(struct
 datapath *dp, struct sk_buff *skb,
        if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES,
 dp->user_features))
                goto nla_put_failure;
 
+       if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
+                       ovs_flow_tbl_masks_cache_size(&dp->table)))
+               goto nla_put_failure;
+
        genlmsg_end(skb, ovs_header);
        return 0;
net/openvswitch/datapath.c +0 -0
@@ -1599,6 +1604,13 @@ static int ovs_dp_change(struct datapath
 *dp,
 struct nlattr *a[])
 #endif
        }
 
Quoted 9 lines
Quoted 7 lines
net/openvswitch/datapath.c +5 -0
+       if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
+               u32 cache_size;
+
+               cache_size =
 nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
+               ovs_flow_tbl_masks_cache_resize(&dp->table,

cache_size);

Do we should return error code, if we can't change the "mask_cache" size ? for example, -EINVAL, -ENOMEM

Initially, I did not do this due to the fact the new value is reported,
and on failure, the old value is shown.
However thinking about it again, it makes more sense to return an error.
Will sent a v4 with the following to return:

(quoted diff) +0 -2
-
-void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size)
+int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size)

  {
         struct mask_cache *mc = rcu_dereference(table->mask_cache);
         struct mask_cache *new;
Quoted 14 lines

Review

Quoted 6 lines
         if (size == mc->cache_size)
(quoted diff) +4 -1
-               return;
+               return 0;
+
+       if (!is_power_of_2(size) && size != 0)
+               return -EINVAL;

add check as below, and we can remove the check in tbl_mask_cache_alloc function. That
function will only return NULL, if there is no memory. And we can comment for tbl_mask_cache_alloc, to indicate that size should be a power of 2.
if ((!is_power_of_2(size) && size != 0) ||
(size * sizeof(struct mask_cache_entry)) >
PCPU_MIN_UNIT_SIZE)

Quoted 1 line

I do not think the extra check hurts as it’s not in any fast path.

Hi Eelco.
Yes, I agree. Sorry for not being clear. I mean that:
if we don't check the "(size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)" in the ovs_flow_tbl_masks_cache_resize. tbl_mask_cache_alloc (ovs_flow_tbl_masks_cache_resize will invoke this function)will return the NULL, ovs_flow_tbl_masks_cache_resize will return -ENOMEM. I guess we should return -EINVAL in that case.

Quoted 297 lines

The comment would easily be missed, especially if someone changes the default value, so I would prefer to keep it as is.

Quoted 293 lines

Thanks Eelco.

Quoted 51 lines
         new = tbl_mask_cache_alloc(size);
         if (!new)
(quoted diff) +3 -1
-               return;
+               return -ENOMEM;
 
         rcu_assign_pointer(table->mask_cache, new);
         call_rcu(&mc->rcu, mask_cache_rcu_cb);
+
+       return 0;
  }
Quoted 41 lines
Quoted 41 lines
net/openvswitch/datapath.c +4 -0
+       }
+
        dp->user_features = user_features;
 
        if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
@@ -1894,6 +1906,8 @@ static const struct nla_policy
 datapath_policy[OVS_DP_ATTR_MAX + 1] = {
        [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len =
 IFNAMSIZ - 1 },
        [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
        [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
+       [OVS_DP_ATTR_MASKS_CACHE_SIZE] =  NLA_POLICY_RANGE(NLA_U32,
 0,
+               PCPU_MIN_UNIT_SIZE / sizeof(struct

mask_cache_entry)),
};

static const struct genl_ops dp_datapath_genl_ops[] = {

net/openvswitch/flow_table.c +1 -1
index a5912ea05352..5280aeeef628 100644
@@ -38,8 +38,8 @@
 #define MASK_ARRAY_SIZE_MIN    16
 #define REHASH_INTERVAL                (10 * 60 * HZ)
 
+#define MC_DEFAULT_HASH_ENTRIES        256
 #define MC_HASH_SHIFT          8
-#define MC_HASH_ENTRIES                (1u << MC_HASH_SHIFT)
 #define MC_HASH_SEGS           ((sizeof(uint32_t) * 8) /
 MC_HASH_SHIFT)
 

static struct kmem_cache *flow_cache;

net/openvswitch/flow_table.c +0 -0
@@ -341,15 +341,75 @@ static void flow_mask_remove(struct

flow_table
*tbl, struct sw_flow_mask *mask)

        }

}

Quoted 235 lines
Quoted 5 lines

net/openvswitch/flow_table.c static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) · line 347

Quoted 4 lines
net/openvswitch/flow_table.c +4 -0
+static void __mask_cache_destroy(struct mask_cache *mc)
+{
+       if (mc->mask_cache)
+               free_percpu(mc->mask_cache);

free_percpu the NULL is safe. we can remove the "if".

Makes sense, will remove the if() check.

Quoted 226 lines
Quoted 219 lines
net/openvswitch/flow_table.c +6 -0
+       kfree(mc);
+}
+
+static void mask_cache_rcu_cb(struct rcu_head *rcu)
+{
+       struct mask_cache *mc = container_of(rcu, struct

mask_cache,
rcu);

net/openvswitch/flow_table.c +10 -0
+
+       __mask_cache_destroy(mc);
+}
+
+static struct mask_cache *tbl_mask_cache_alloc(u32 size)
+{
+       struct mask_cache_entry __percpu *cache = NULL;
+       struct mask_cache *new;
+
+       /* Only allow size to be 0, or a power of 2, and does not

exceed

net/openvswitch/flow_table.c +4 -0
+        * percpu allocation size.
+        */
+       if ((!is_power_of_2(size) && size != 0) ||
+           (size * sizeof(struct mask_cache_entry)) >

PCPU_MIN_UNIT_SIZE)

net/openvswitch/flow_table.c +8 -0
+               return NULL;
+       new = kzalloc(sizeof(*new), GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       new->cache_size = size;
+       if (new->cache_size > 0) {
+               cache = __alloc_percpu(array_size(sizeof(struct

mask_cache_entry),

net/openvswitch/flow_table.c +2 -0
+                                                 new->cache_size),
+                                      __alignof__(struct

mask_cache_entry));

net/openvswitch/flow_table.c +11 -0
+               if (!cache) {
+                       kfree(new);
+                       return NULL;
+               }
+       }
+
+       new->mask_cache = cache;
+       return new;
+}
+
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size)

net/openvswitch/flow_table.c +16 -1
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_cache *new;
+
+       if (size == mc->cache_size)
+               return;
+
+       new = tbl_mask_cache_alloc(size);
+       if (!new)
+               return;
+
+       rcu_assign_pointer(table->mask_cache, new);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+}
+
 int ovs_flow_tbl_init(struct flow_table *table)
 {
        struct table_instance *ti, *ufid_ti;
+       struct mask_cache *mc;
        struct mask_array *ma;
 
-       table->mask_cache = __alloc_percpu(sizeof(struct

mask_cache_entry) *

net/openvswitch/flow_table.c +0 -2
-                                          MC_HASH_ENTRIES,
-                                          __alignof__(struct

mask_cache_entry));

net/openvswitch/flow_table.c +8 -4
-       if (!table->mask_cache)
+       mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);
+       if (!mc)
                return -ENOMEM;
 
        ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
@@ -367,6 +427,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
        rcu_assign_pointer(table->ti, ti);
        rcu_assign_pointer(table->ufid_ti, ufid_ti);
        rcu_assign_pointer(table->mask_array, ma);
+       rcu_assign_pointer(table->mask_cache, mc);
        table->last_rehash = jiffies;
        table->count = 0;
        table->ufid_count = 0;
@@ -377,7 +438,7 @@ int ovs_flow_tbl_init(struct flow_table *table)
 free_mask_array:
        __mask_array_destroy(ma);
 free_mask_cache:
-       free_percpu(table->mask_cache);
+       __mask_cache_destroy(mc);
        return -ENOMEM;
 }
 
@@ -453,9 +514,11 @@ void ovs_flow_tbl_destroy(struct flow_table
 *table)
 {
        struct table_instance *ti = rcu_dereference_raw(table->ti);
        struct table_instance *ufid_ti =
 rcu_dereference_raw(table->ufid_ti);
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+       struct mask_array *ma =
 rcu_dereference_ovsl(table->mask_array);
 
-       free_percpu(table->mask_cache);
-       call_rcu(&table->mask_array->rcu, mask_array_rcu_cb);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+       call_rcu(&ma->rcu, mask_array_rcu_cb);
        table_instance_destroy(table, ti, ufid_ti, false);

}

net/openvswitch/flow_table.c +1 -0
@@ -724,6 +787,7 @@ struct sw_flow
 *ovs_flow_tbl_lookup_stats(struct
 flow_table *tbl,
                                          u32 *n_mask_hit,
                                          u32 *n_cache_hit)
 {
+       struct mask_cache *mc = rcu_dereference(tbl->mask_cache);
        struct mask_array *ma = rcu_dereference(tbl->mask_array);
        struct table_instance *ti = rcu_dereference(tbl->ti);
        struct mask_cache_entry *entries, *ce;
net/openvswitch/flow_table.c +1 -1
@@ -733,7 +797,7 @@ struct sw_flow
 *ovs_flow_tbl_lookup_stats(struct
 flow_table *tbl,
 
        *n_mask_hit = 0;
        *n_cache_hit = 0;
-       if (unlikely(!skb_hash)) {
+       if (unlikely(!skb_hash || mc->cache_size == 0)) {
                u32 mask_index = 0;
                u32 cache = 0;
net/openvswitch/flow_table.c +2 -2
@@ -749,11 +813,11 @@ struct sw_flow
 *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 
        ce = NULL;
        hash = skb_hash;
-       entries = this_cpu_ptr(tbl->mask_cache);
+       entries = this_cpu_ptr(mc->mask_cache);
 
        /* Find the cache entry 'ce' to operate on. */
        for (seg = 0; seg < MC_HASH_SEGS; seg++) {
-               int index = hash & (MC_HASH_ENTRIES - 1);
+               int index = hash & (mc->cache_size - 1);
                struct mask_cache_entry *e;
 
                e = &entries[index];
net/openvswitch/flow_table.c +7 -0
@@ -867,6 +931,13 @@ int ovs_flow_tbl_num_masks(const struct
 flow_table *table)
        return READ_ONCE(ma->count);
 }
 
+u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table)
+{
+       struct mask_cache *mc = rcu_dereference(table->mask_cache);
+
+       return READ_ONCE(mc->cache_size);
+}
+
 static struct table_instance *table_instance_expand(struct
 table_instance *ti,

bool ufid)
{

net/openvswitch/flow_table.c +0 -0
@@ -1095,8 +1166,8 @@ void ovs_flow_masks_rebalance(struct

flow_table
*table)

        for (i = 0; i < masks_entries; i++) {
                int index = masks_and_count[i].index;
net/openvswitch/flow_table.c +2 -2
-               new->masks[new->count++] =
-                       rcu_dereference_ovsl(ma->masks[index]);
+               if (ovsl_dereference(ma->masks[index]))
+                       new->masks[new->count++] =
ma->masks[index];
        }

        rcu_assign_pointer(table->mask_array, new);
net/openvswitch/flow_table.h +9 -1
index 325e939371d8..f2dba952db2f 100644
@@ -27,6 +27,12 @@ struct mask_cache_entry {
        u32 mask_index;
 };
 
+struct mask_cache {
+       struct rcu_head rcu;
+       u32 cache_size;  /* Must be ^2 value. */
+       struct mask_cache_entry __percpu *mask_cache;
+};
+
 struct mask_count {
        int index;
        u64 counter;
@@ -53,7 +59,7 @@ struct table_instance {
 struct flow_table {
        struct table_instance __rcu *ti;
        struct table_instance __rcu *ufid_ti;
-       struct mask_cache_entry __percpu *mask_cache;
+       struct mask_cache __rcu *mask_cache;
        struct mask_array __rcu *mask_array;
        unsigned long last_rehash;
        unsigned int count;
@@ -77,6 +83,8 @@ int ovs_flow_tbl_insert(struct flow_table *table,
 struct sw_flow *flow,
                        const struct sw_flow_mask *mask);
 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow
 *flow);
 int  ovs_flow_tbl_num_masks(const struct flow_table *table);
+u32  ovs_flow_tbl_masks_cache_size(const struct flow_table
 *table);
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size);
struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance
*table,

Quoted 3 lines
                                       u32 *bucket, u32 *idx);

struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,

--
Best regards, Tonghao

--
Best regards, Tonghao

--
Best regards, Tonghao

author maintainer
#8 lore raw

Re: [PATCH net-next v3 2/2] net: openvswitch: make masks cache size configurable

Quoted 137 lines · Tonghao Zhang
Quoted 135 lines · Eelco Chaudron
Quoted 115 lines · Tonghao Zhang
Quoted 113 lines · Eelco Chaudron

Thanks Tonghao for the review, see comments inline below!

If I get no more reviews by the end of the week I’ll send out a v4.

//Eelco

Quoted 76 lines · Tonghao Zhang
Quoted 74 lines · Eelco Chaudron

This patch makes the masks cache size configurable, or with a size of 0, disable it.

---
Changes in v3:
- Use is_power_of_2() function
- Use array_size() function
- Fix remaining sparse errors

Changes in v2:
- Fix sparse warnings
- Fix netlink policy items reported by Florian Westphal

 include/uapi/linux/openvswitch.h |    1
 net/openvswitch/datapath.c       |   14 +++++
 net/openvswitch/flow_table.c     |   97
+++++++++++++++++++++++++++++++++-----
 net/openvswitch/flow_table.h     |   10 ++++
 4 files changed, 108 insertions(+), 14 deletions(-)
include/uapi/linux/openvswitch.h +0 -0
index 7cb76e5ca7cf..8300cc29dec8 100644
@@ -86,6 +86,7 @@ enum ovs_datapath_attr {
        OVS_DP_ATTR_MEGAFLOW_STATS,     /* struct

ovs_dp_megaflow_stats */

        OVS_DP_ATTR_USER_FEATURES,      /* OVS_DP_F_*  */
        OVS_DP_ATTR_PAD,
include/uapi/linux/openvswitch.h +1 -0
+       OVS_DP_ATTR_MASKS_CACHE_SIZE,
        __OVS_DP_ATTR_MAX
 };
net/openvswitch/datapath.c +0 -0
index a54df1fe3ec4..114b2ddb8037 100644
@@ -1498,6 +1498,7 @@ static size_t ovs_dp_cmd_msg_size(void)
        msgsize += nla_total_size_64bit(sizeof(struct
 ovs_dp_stats));
        msgsize += nla_total_size_64bit(sizeof(struct
 ovs_dp_megaflow_stats));
        msgsize += nla_total_size(sizeof(u32)); /*

OVS_DP_ATTR_USER_FEATURES */
+ msgsize += nla_total_size(sizeof(u32)); /*
OVS_DP_ATTR_MASKS_CACHE_SIZE */

        return msgsize;

}

net/openvswitch/datapath.c +4 -0
@@ -1535,6 +1536,10 @@ static int ovs_dp_cmd_fill_info(struct
 datapath *dp, struct sk_buff *skb,
        if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES,
 dp->user_features))
                goto nla_put_failure;
 
+       if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
+                       
 ovs_flow_tbl_masks_cache_size(&dp->table)))
+               goto nla_put_failure;
+
        genlmsg_end(skb, ovs_header);
        return 0;
net/openvswitch/datapath.c +0 -0
@@ -1599,6 +1604,13 @@ static int ovs_dp_change(struct datapath
 *dp,
 struct nlattr *a[])
 #endif
        }
 
Quoted 9 lines
Quoted 7 lines
net/openvswitch/datapath.c +5 -0
+       if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
+               u32 cache_size;
+
+               cache_size =
 nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
+               ovs_flow_tbl_masks_cache_resize(&dp->table,

cache_size);

Do we should return error code, if we can't change the "mask_cache" size ? for example, -EINVAL, -ENOMEM

Initially, I did not do this due to the fact the new value is reported,
and on failure, the old value is shown.
However thinking about it again, it makes more sense to return an error.
Will sent a v4 with the following to return:

(quoted diff) +0 -2
-
-void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32

size)
+int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size)
{
struct mask_cache *mc =

rcu_dereference(table->mask_cache);
         struct mask_cache *new;
Quoted 14 lines

Review

Quoted 6 lines
         if (size == mc->cache_size)
(quoted diff) +4 -1
-               return;
+               return 0;
+
+       if (!is_power_of_2(size) && size != 0)
+               return -EINVAL;

add check as below, and we can remove the check in tbl_mask_cache_alloc function. That
function will only return NULL, if there is no memory. And we can comment for tbl_mask_cache_alloc, to indicate that size should be a power of 2.
if ((!is_power_of_2(size) && size != 0) ||
(size * sizeof(struct mask_cache_entry)) >
PCPU_MIN_UNIT_SIZE)

Quoted 1 line

I do not think the extra check hurts as it’s not in any fast path.

Quoted 8 lines

Hi Eelco.
Yes, I agree. Sorry for not being clear. I mean that:
if we don't check the "(size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)" in the ovs_flow_tbl_masks_cache_resize. tbl_mask_cache_alloc (ovs_flow_tbl_masks_cache_resize will invoke this function)will return the NULL, ovs_flow_tbl_masks_cache_resize will return -ENOMEM. I guess we should return -EINVAL in that case.

Thanks got it now, will add the additional check, and not remove it from tbl_mask_cache_alloc().

Quoted 314 lines
Quoted 310 lines

The comment would easily be missed, especially if someone changes the default value, so I would prefer to keep it as is.

Quoted 306 lines

Thanks Eelco.

Quoted 52 lines
         new = tbl_mask_cache_alloc(size);
         if (!new)
(quoted diff) +3 -1
-               return;
+               return -ENOMEM;
 
         rcu_assign_pointer(table->mask_cache, new);
         call_rcu(&mc->rcu, mask_cache_rcu_cb);
+
+       return 0;
  }
Quoted 42 lines
Quoted 42 lines
net/openvswitch/datapath.c +3 -0
+       }
+
        dp->user_features = user_features;
 
        if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
@@ -1894,6 +1906,8 @@ static const struct nla_policy
 datapath_policy[OVS_DP_ATTR_MAX + 1] = {
        [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len =
 IFNAMSIZ - 1 },
        [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
        [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
+       [OVS_DP_ATTR_MASKS_CACHE_SIZE] =  
 NLA_POLICY_RANGE(NLA_U32,

0,
+ PCPU_MIN_UNIT_SIZE / sizeof(struct
mask_cache_entry)),
};

static const struct genl_ops dp_datapath_genl_ops[] = {

net/openvswitch/flow_table.c +1 -1
index a5912ea05352..5280aeeef628 100644
@@ -38,8 +38,8 @@
 #define MASK_ARRAY_SIZE_MIN    16
 #define REHASH_INTERVAL                (10 * 60 * HZ)
 
+#define MC_DEFAULT_HASH_ENTRIES        256
 #define MC_HASH_SHIFT          8
-#define MC_HASH_ENTRIES                (1u << MC_HASH_SHIFT)
 #define MC_HASH_SEGS           ((sizeof(uint32_t) * 8) /
 MC_HASH_SHIFT)
 

static struct kmem_cache *flow_cache;

net/openvswitch/flow_table.c +0 -0
@@ -341,15 +341,75 @@ static void flow_mask_remove(struct

flow_table
*tbl, struct sw_flow_mask *mask)

        }

}

Quoted 247 lines
Quoted 5 lines

net/openvswitch/flow_table.c static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) · line 347

Quoted 4 lines
net/openvswitch/flow_table.c +4 -0
+static void __mask_cache_destroy(struct mask_cache *mc)
+{
+       if (mc->mask_cache)
+               free_percpu(mc->mask_cache);

free_percpu the NULL is safe. we can remove the "if".

Makes sense, will remove the if() check.

Quoted 238 lines
Quoted 231 lines
net/openvswitch/flow_table.c +6 -0
+       kfree(mc);
+}
+
+static void mask_cache_rcu_cb(struct rcu_head *rcu)
+{
+       struct mask_cache *mc = container_of(rcu, struct

mask_cache,
rcu);

net/openvswitch/flow_table.c +10 -0
+
+       __mask_cache_destroy(mc);
+}
+
+static struct mask_cache *tbl_mask_cache_alloc(u32 size)
+{
+       struct mask_cache_entry __percpu *cache = NULL;
+       struct mask_cache *new;
+
+       /* Only allow size to be 0, or a power of 2, and does not

exceed

net/openvswitch/flow_table.c +4 -0
+        * percpu allocation size.
+        */
+       if ((!is_power_of_2(size) && size != 0) ||
+           (size * sizeof(struct mask_cache_entry)) >

PCPU_MIN_UNIT_SIZE)

net/openvswitch/flow_table.c +8 -0
+               return NULL;
+       new = kzalloc(sizeof(*new), GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       new->cache_size = size;
+       if (new->cache_size > 0) {
+               cache = __alloc_percpu(array_size(sizeof(struct

mask_cache_entry),
+
new->cache_size),
+ __alignof__(struct
mask_cache_entry));

net/openvswitch/flow_table.c +11 -0
+               if (!cache) {
+                       kfree(new);
+                       return NULL;
+               }
+       }
+
+       new->mask_cache = cache;
+       return new;
+}
+
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, 

u32
size)

net/openvswitch/flow_table.c +2 -0
+{
+       struct mask_cache *mc = 

rcu_dereference(table->mask_cache);

net/openvswitch/flow_table.c +14 -1
+       struct mask_cache *new;
+
+       if (size == mc->cache_size)
+               return;
+
+       new = tbl_mask_cache_alloc(size);
+       if (!new)
+               return;
+
+       rcu_assign_pointer(table->mask_cache, new);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+}
+
 int ovs_flow_tbl_init(struct flow_table *table)
 {
        struct table_instance *ti, *ufid_ti;
+       struct mask_cache *mc;
        struct mask_array *ma;
 
-       table->mask_cache = __alloc_percpu(sizeof(struct

mask_cache_entry) *

net/openvswitch/flow_table.c +0 -2
-                                          MC_HASH_ENTRIES,
-                                          __alignof__(struct

mask_cache_entry));

net/openvswitch/flow_table.c +3 -1
-       if (!table->mask_cache)
+       mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);
+       if (!mc)
                return -ENOMEM;
 
        ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
@@ -367,6 +427,7 @@ int ovs_flow_tbl_init(struct flow_table 
 *table)
        rcu_assign_pointer(table->ti, ti);
        rcu_assign_pointer(table->ufid_ti, ufid_ti);
        rcu_assign_pointer(table->mask_array, ma);
+       rcu_assign_pointer(table->mask_cache, mc);
        table->last_rehash = jiffies;
        table->count = 0;
        table->ufid_count = 0;
net/openvswitch/flow_table.c +6 -3
@@ -377,7 +438,7 @@ int ovs_flow_tbl_init(struct flow_table 
 *table)
 free_mask_array:
        __mask_array_destroy(ma);
 free_mask_cache:
-       free_percpu(table->mask_cache);
+       __mask_cache_destroy(mc);
        return -ENOMEM;
 }
@@ -453,9 +514,11 @@ void ovs_flow_tbl_destroy(struct flow_table
 *table)
 {
        struct table_instance *ti = 
 rcu_dereference_raw(table->ti);
        struct table_instance *ufid_ti =
 rcu_dereference_raw(table->ufid_ti);
+       struct mask_cache *mc = 
 rcu_dereference(table->mask_cache);
+       struct mask_array *ma =
 rcu_dereference_ovsl(table->mask_array);
 
-       free_percpu(table->mask_cache);
-       call_rcu(&table->mask_array->rcu, mask_array_rcu_cb);
+       call_rcu(&mc->rcu, mask_cache_rcu_cb);
+       call_rcu(&ma->rcu, mask_array_rcu_cb);
        table_instance_destroy(table, ti, ufid_ti, false);
 }
@@ -724,6 +787,7 @@ struct sw_flow
 *ovs_flow_tbl_lookup_stats(struct
 flow_table *tbl,
                                          u32 *n_mask_hit,
                                          u32 *n_cache_hit)
 {
+       struct mask_cache *mc = rcu_dereference(tbl->mask_cache);
        struct mask_array *ma = rcu_dereference(tbl->mask_array);
        struct table_instance *ti = rcu_dereference(tbl->ti);
        struct mask_cache_entry *entries, *ce;
net/openvswitch/flow_table.c +1 -1
@@ -733,7 +797,7 @@ struct sw_flow
 *ovs_flow_tbl_lookup_stats(struct
 flow_table *tbl,
 
        *n_mask_hit = 0;
        *n_cache_hit = 0;
-       if (unlikely(!skb_hash)) {
+       if (unlikely(!skb_hash || mc->cache_size == 0)) {
                u32 mask_index = 0;
                u32 cache = 0;
net/openvswitch/flow_table.c +2 -2
@@ -749,11 +813,11 @@ struct sw_flow
 *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
 
        ce = NULL;
        hash = skb_hash;
-       entries = this_cpu_ptr(tbl->mask_cache);
+       entries = this_cpu_ptr(mc->mask_cache);
 
        /* Find the cache entry 'ce' to operate on. */
        for (seg = 0; seg < MC_HASH_SEGS; seg++) {
-               int index = hash & (MC_HASH_ENTRIES - 1);
+               int index = hash & (mc->cache_size - 1);
                struct mask_cache_entry *e;
 
                e = &entries[index];
net/openvswitch/flow_table.c +7 -0
@@ -867,6 +931,13 @@ int ovs_flow_tbl_num_masks(const struct
 flow_table *table)
        return READ_ONCE(ma->count);
 }
 
+u32 ovs_flow_tbl_masks_cache_size(const struct flow_table 
 *table)
+{
+       struct mask_cache *mc = 
 rcu_dereference(table->mask_cache);
+
+       return READ_ONCE(mc->cache_size);
+}
+

static struct table_instance *table_instance_expand(struct
table_instance *ti,
bool ufid)
{

net/openvswitch/flow_table.c +0 -0
@@ -1095,8 +1166,8 @@ void ovs_flow_masks_rebalance(struct

flow_table
*table)

        for (i = 0; i < masks_entries; i++) {
                int index = masks_and_count[i].index;
net/openvswitch/flow_table.c +2 -2
-               new->masks[new->count++] =
-                       rcu_dereference_ovsl(ma->masks[index]);
+               if (ovsl_dereference(ma->masks[index]))
+                       new->masks[new->count++] =
ma->masks[index];
        }

        rcu_assign_pointer(table->mask_array, new);
net/openvswitch/flow_table.h +7 -1
index 325e939371d8..f2dba952db2f 100644
@@ -27,6 +27,12 @@ struct mask_cache_entry {
        u32 mask_index;
 };
 
+struct mask_cache {
+       struct rcu_head rcu;
+       u32 cache_size;  /* Must be ^2 value. */
+       struct mask_cache_entry __percpu *mask_cache;
+};
+
 struct mask_count {
        int index;
        u64 counter;
@@ -53,7 +59,7 @@ struct table_instance {
 struct flow_table {
        struct table_instance __rcu *ti;
        struct table_instance __rcu *ufid_ti;
-       struct mask_cache_entry __percpu *mask_cache;
+       struct mask_cache __rcu *mask_cache;
        struct mask_array __rcu *mask_array;
        unsigned long last_rehash;
        unsigned int count;
@@ -77,6 +83,8 @@ int ovs_flow_tbl_insert(struct flow_table 
 *table,
 struct sw_flow *flow,
                        const struct sw_flow_mask *mask);
 void ovs_flow_tbl_remove(struct flow_table *table, struct 

sw_flow
*flow);
int ovs_flow_tbl_num_masks(const struct flow_table *table);
+u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table);
+void ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32
size);
struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance
*table,

Quoted 3 lines
                                       u32 *bucket, u32 *idx);

struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *,

--
Best regards, Tonghao

--
Best regards, Tonghao

--
Best regards, Tonghao