netfilter: ipset: keep comment extensions private on resize
Subsystems (2)
PATCH 1/1 netfilter: ipset: keep comment extensions private on resize
In reply to a message not in the archiveFrom: Zhengchuan Liang <[email protected]>
Hash resize rebuilds the table by copying live elements into a new table, while comment data is stored outside of the element body.
Recreate the comment extension for resized entries so the new table does not share comment storage with the retired table. Once resize gives each table its own comment data again, the old table can return to destroying its extensions in the normal teardown paths.
This keeps comment lifetime and accounting consistent across resize and the follow-up gc, dump, add, del and flush paths.
--- include/linux/netfilter/ipset/ip_set.h | 1 + net/netfilter/ipset/ip_set_core.c | 36 ++++++++++++++++++++++++++ net/netfilter/ipset/ip_set_hash_gen.h | 15 ++++++----- 3 files changed, 46 insertions(+), 6 deletions(-)
include/linux/netfilter/ipset/ip_set.h +1 -0
@@ -336,6 +336,7 @@ extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len, size_t align);extern int ip_set_get_extensions (struct ip_set *set,struct nlattr *tb[],struct ip_set_ext *ext); +extern int ip_set_ext_copy (struct ip_set *set,void *dst,const void *src);extern int ip_set_put_extensions (struct sk_buff *skb,const struct ip_set *set,const void *e,bool active); extern bool ip_set_match_extensions(struct ip_set *set,
net/netfilter/ipset/ip_set_core.c +36 -0
@@ -367,6 +367,42 @@ ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment, }EXPORT_SYMBOL_GPL (ip_set_init_comment); +static int +ip_set_copy_comment (struct ip_set *set,struct ip_set_comment *dst, +const struct ip_set_comment *src) +{ +struct ip_set_comment_rcu *c, *newc; +size_t len; + +RCU_INIT_POINTER (dst->c,NULL ); + + c =rcu_dereference_bh (src->c); +if (!c) +return 0 ; + + len =strlen (c->str); + newc =kmalloc (sizeof (*newc) + len +1 ,GFP_ATOMIC ); +if (unlikely (!newc)) +return -ENOMEM ; + +memcpy (newc->str, c->str, len +1 ); + set->ext_size +=sizeof (*newc) + len +1 ; +rcu_assign_pointer (dst->c, newc); + +return 0 ; +} + +int +ip_set_ext_copy (struct ip_set *set,void *dst,const void *src) +{ +if (SET_WITH_COMMENT (set)) +return ip_set_copy_comment (set,ext_comment (dst, set), +ext_comment (src, set)); + +return 0 ; +} +EXPORT_SYMBOL_GPL (ip_set_ext_copy); +/* Used only when dumping a set, protected by rcu_read_lock() */ static int ip_set_put_comment (struct sk_buff *skb,const struct ip_set_comment *comment)
net/netfilter/ipset/ip_set_hash_gen.h +9 -6
@@ -584,7 +584,7 @@ mtype_gc(struct work_struct *work)if (atomic_dec_and_test (&t->uref) &&atomic_read (&t->ref)) {pr_debug ("Table destroy after resize by expire: %p\n" , t); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true); }queue_delayed_work (system_power_efficient_wq, &gc->dwork, next_run);
@@ -743,6 +743,9 @@ mtype_resize(struct ip_set *set, bool retried) } d =ahash_data (m, m->pos, dsize);memcpy (d, data, dsize); + ret =ip_set_ext_copy (set, d, data); +if (ret <0 ) + goto cleanup;set_bit (m->pos++, m->used); t->hregion[nr].elements++;#ifdef IP_SET_HASH_WITH_NETS
@@ -778,7 +781,7 @@ mtype_resize(struct ip_set *set, bool retried)/* If there's nobody else using the table, destroy it */ if (atomic_dec_and_test (&orig->uref)) {pr_debug ("Table destroy by resize %p\n" , orig); -mtype_ahash_destroy (set, orig, false); +mtype_ahash_destroy (set, orig, true); } out:
@@ -791,7 +794,7 @@ mtype_resize(struct ip_set *set, bool retried)rcu_read_unlock_bh ();atomic_set (&orig->ref,0 );atomic_dec (&orig->uref); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true);if (ret == -EAGAIN ) goto retry; goto out;
@@ -1023,7 +1026,7 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext, out:if (atomic_dec_and_test (&t->uref) &&atomic_read (&t->ref)) {pr_debug ("Table destroy after resize by add: %p\n" , t); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true); }return ret; }
@@ -1135,7 +1138,7 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext, }if (atomic_dec_and_test (&t->uref) &&atomic_read (&t->ref)) {pr_debug ("Table destroy after resize by del: %p\n" , t); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true); }return ret; }
@@ -1341,7 +1344,7 @@ mtype_uref(struct ip_set *set, struct netlink_callback *cb, bool start)if (atomic_dec_and_test (&t->uref) &&atomic_read (&t->ref)) {pr_debug ("Table destroy after resize " " by dump: %p\n" , t); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true); } cb->args[IPSET_CB_PRIVATE ] =0 ; }
Signature · 1 line
2.39.5
Hi,
Quoted 5 lines · Ren Wei
From: Zhengchuan Liang <[email protected]>
Hash resize rebuilds the table by copying live elements into a new table, while comment data is stored outside of the element body.
Quoted 7 lines
Recreate the comment extension for resized entries so the new table does not share comment storage with the retired table. Once resize gives each table its own comment data again, the old table can return to destroying its extensions in the normal teardown paths.
This keeps comment lifetime and accounting consistent across resize and the follow-up gc, dump, add, del and flush paths.
I appreciate your patch and it solves the issue but I think it's not optimal.
Could you rework it to use the 'struct list_head ad' add|del backlist infrastructure of the hash types which handles the added/deleted entries during resizing, but the proper handling of the comment extension for deleted elements is missing. That way the unnecessary allocation/deallocation of the comment extension of all set element can be avoided.
Best regards,
Jozsef
Quoted 145 lines
Fixes f66ee0410b1c ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")Reported-by Yuan Tan <[email protected]>Reported-by Yifan Wu <[email protected]>Reported-by Juefei Pu <[email protected]>Reported-by Xin Liu <[email protected]>Signed-off-by Zhengchuan Liang <[email protected]>Signed-off-by Ren Wei <[email protected]>---
include/linux/netfilter/ipset/ip_set.h | 1 + net/netfilter/ipset/ip_set_core.c | 36 ++++++++++++++++++++++++++ net/netfilter/ipset/ip_set_hash_gen.h | 15 ++++++----- 3 files changed, 46 insertions(+), 6 deletions(-)include/linux/netfilter/ipset/ip_set.h +1
-0@@ -336,6 +336,7 @@ extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len, size_t align);extern int ip_set_get_extensions (struct ip_set *set,struct nlattr *tb[],struct ip_set_ext *ext); +extern int ip_set_ext_copy (struct ip_set *set,void *dst,const void *src);extern int ip_set_put_extensions (struct sk_buff *skb,const struct ip_set *set,const void *e,bool active); extern bool ip_set_match_extensions(struct ip_set *set,net/netfilter/ipset/ip_set_core.c +36
-0@@ -367,6 +367,42 @@ ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment, }EXPORT_SYMBOL_GPL (ip_set_init_comment); +static int +ip_set_copy_comment (struct ip_set *set,struct ip_set_comment *dst, +const struct ip_set_comment *src) +{ +struct ip_set_comment_rcu *c, *newc; +size_t len; + +RCU_INIT_POINTER (dst->c,NULL ); + + c =rcu_dereference_bh (src->c); +if (!c) +return 0 ; + + len =strlen (c->str); + newc =kmalloc (sizeof (*newc) + len +1 ,GFP_ATOMIC ); +if (unlikely (!newc)) +return -ENOMEM ; + +memcpy (newc->str, c->str, len +1 ); + set->ext_size +=sizeof (*newc) + len +1 ; +rcu_assign_pointer (dst->c, newc); + +return 0 ; +} + +int +ip_set_ext_copy (struct ip_set *set,void *dst,const void *src) +{ +if (SET_WITH_COMMENT (set)) +return ip_set_copy_comment (set,ext_comment (dst, set), +ext_comment (src, set)); + +return 0 ; +} +EXPORT_SYMBOL_GPL (ip_set_ext_copy); +/* Used only when dumping a set, protected by rcu_read_lock() */ static int
ip_set_put_comment(struct sk_buff *skb, const struct ip_set_comment *comment)net/netfilter/ipset/ip_set_hash_gen.h +5
-2@@ -584,7 +584,7 @@ mtype_gc(struct work_struct *work)if (atomic_dec_and_test (&t->uref) &&atomic_read (&t->ref)) {pr_debug ("Table destroy after resize by expire: %p\n" , t); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true); }queue_delayed_work (system_power_efficient_wq, &gc->dwork, next_run);@@ -743,6 +743,9 @@ mtype_resize(struct ip_set *set, bool retried) } d =ahash_data (m, m->pos, dsize);memcpy (d, data, dsize); + ret =ip_set_ext_copy (set, d, data); +if (ret <0 ) + goto cleanup;set_bit (m->pos++, m->used); t->hregion[nr].elements++;#ifdef IP_SET_HASH_WITH_NETS @@ -778,7 +781,7 @@ mtype_resize(struct ip_set *set, bool retried)/* If there's nobody else using the table, destroy it */ if (atomic_dec_and_test (&orig->uref)) {pr_debug ("Table destroy by resize %p\n" , orig); -mtype_ahash_destroy (set, orig, false); +mtype_ahash_destroy (set, orig, true); }out:
net/netfilter/ipset/ip_set_hash_gen.h +1
-1@@ -791,7 +794,7 @@ mtype_resize(struct ip_set *set, bool retried)rcu_read_unlock_bh ();atomic_set (&orig->ref,0 );atomic_dec (&orig->uref); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true);if (ret == -EAGAIN ) goto retry; goto out;@@ -1023,7 +1026,7 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,out:
if (atomic_dec_and_test(&t->uref) && atomic_read(&t->ref)) { pr_debug("Table destroy after resize by add: %p\n", t);net/netfilter/ipset/ip_set_hash_gen.h +1
-1-mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true);} return ret; }net/netfilter/ipset/ip_set_hash_gen.h +2
-2@@ -1135,7 +1138,7 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext, }if (atomic_dec_and_test (&t->uref) &&atomic_read (&t->ref)) {pr_debug ("Table destroy after resize by del: %p\n" , t); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true); }return ret; }@@ -1341,7 +1344,7 @@ mtype_uref(struct ip_set *set, struct netlink_callback *cb, bool start)if (atomic_dec_and_test (&t->uref) &&atomic_read (&t->ref)) {pr_debug ("Table destroy after resize " " by dump: %p\n" , t); -mtype_ahash_destroy (set, t, false); +mtype_ahash_destroy (set, t, true); } cb->args[IPSET_CB_PRIVATE ] =0 ; }Signature · 1 line
2.39.5