netfilter: nf_tables: annotate data-races around element expiration
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v3 6/9 netfilter: nf_tables: annotate data-races around element expiration
element expiration can be read-write locklessly, it can be written by dynset and read from netlink dump, add annotation.
---
v3: no changes
include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 2 +- net/netfilter/nft_dynset.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -831,7 +831,7 @@ static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext, u64 tstamp) {return nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION ) && -time_after_eq64 (tstamp, *nft_set_ext_expiration (ext)); +time_after_eq64 (tstamp,READ_ONCE (*nft_set_ext_expiration (ext))); }static inline bool nft_set_elem_expired (const struct nft_set_ext *ext)
net/netfilter/nf_tables_api.c +1 -1
@@ -5821,7 +5821,7 @@ static int nf_tables_fill_setelem(struct sk_buff *skb,if (nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION )) {u64 expires, now =get_jiffies_64 (); - expires = *nft_set_ext_expiration (ext); + expires =READ_ONCE (*nft_set_ext_expiration (ext));if (time_before64 (now, expires)) expires -= now;else
net/netfilter/nft_dynset.c +1 -1
@@ -96,7 +96,7 @@ void nft_dynset_eval(const struct nft_expr *expr,if (priv->op ==NFT_DYNSET_OP_UPDATE &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION )) { timeout = priv->timeout ? :READ_ONCE (set->timeout); - *nft_set_ext_expiration (ext) =get_jiffies_64 () + timeout; +WRITE_ONCE (*nft_set_ext_expiration (ext),get_jiffies_64 () + timeout); }nft_set_elem_update_expr (ext, regs, pkt);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v3 7/9 netfilter: nf_tables: consolidate timeout extension for elements
Expiration and timeout are stored in separated set element extensions, but they are tightly coupled. Consolidate them in a single extension to simplify and prepare for set element updates.
---
v3: no changes
include/net/netfilter/nf_tables.h | 18 ++++++------- net/netfilter/nf_tables_api.c | 43 ++++++++++++------------------- net/netfilter/nft_dynset.c | 13 ++++------ 3 files changed, 30 insertions(+), 44 deletions(-)
include/net/netfilter/nf_tables.h +8 -10
@@ -683,7 +683,6 @@ void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);* @NFT_SET_EXT_DATA: mapping data * @NFT_SET_EXT_FLAGS: element flags * @NFT_SET_EXT_TIMEOUT: element timeout -* @NFT_SET_EXT_EXPIRATION: element expiration time * @NFT_SET_EXT_USERDATA: user data associated with the element * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
@@ -695,7 +694,6 @@ enum nft_set_extensions {NFT_SET_EXT_DATA ,NFT_SET_EXT_FLAGS ,NFT_SET_EXT_TIMEOUT , -NFT_SET_EXT_EXPIRATION ,NFT_SET_EXT_USERDATA ,NFT_SET_EXT_EXPRESSIONS , NFT_SET_EXT_OBJREF,
@@ -807,14 +805,14 @@ static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)return nft_set_ext (ext,NFT_SET_EXT_FLAGS ); } -static inline u64 *nft_set_ext_timeout (const struct nft_set_ext *ext) -{ -return nft_set_ext (ext,NFT_SET_EXT_TIMEOUT ); -} +struct nft_timeout { +u64 timeout; +u64 expiration; +}; -static inline u64 *nft_set_ext_expiration (const struct nft_set_ext *ext) +static inline struct nft_timeout *nft_set_ext_timeout (const struct nft_set_ext *ext) { -return nft_set_ext (ext,NFT_SET_EXT_EXPIRATION ); +return nft_set_ext (ext,NFT_SET_EXT_TIMEOUT ); }static inline struct nft_userdata *nft_set_ext_userdata (const struct nft_set_ext *ext)
@@ -830,8 +828,8 @@ static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_exstatic inline bool __nft_set_elem_expired (const struct nft_set_ext *ext,u64 tstamp) { -return nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION ) && -time_after_eq64 (tstamp,READ_ONCE (*nft_set_ext_expiration (ext))); +return nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) && +time_after_eq64 (tstamp,READ_ONCE (nft_set_ext_timeout (ext)->expiration)); }static inline bool nft_set_elem_expired (const struct nft_set_ext *ext)
net/netfilter/nf_tables_api.c +17 -26
73 lines not on this page · raw message
net/netfilter/nft_dynset.c +5 -8
@@ -94,9 +94,9 @@ void nft_dynset_eval(const struct nft_expr *expr,if (set->ops->update (set, ®s->data[priv->sreg_key], nft_dynset_new, expr, regs, &ext)) {if (priv->op ==NFT_DYNSET_OP_UPDATE && -nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION )) { +nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT )) { timeout = priv->timeout ? :READ_ONCE (set->timeout); -WRITE_ONCE (*nft_set_ext_expiration (ext),get_jiffies_64 () + timeout); +WRITE_ONCE (nft_set_ext_timeout (ext)->expiration,get_jiffies_64 () + timeout); }nft_set_elem_update_expr (ext, regs, pkt);
@@ -312,12 +312,9 @@ static int nft_dynset_init(const struct nft_ctx *ctx,if (priv->num_exprs)nft_dynset_ext_add_expr (priv); -if (set->flags &NFT_SET_TIMEOUT ) { -if (timeout ||READ_ONCE (set->timeout)) { -nft_set_ext_add (&priv->tmpl,NFT_SET_EXT_TIMEOUT ); -nft_set_ext_add (&priv->tmpl,NFT_SET_EXT_EXPIRATION ); - } - } +if (set->flags &NFT_SET_TIMEOUT && + (timeout ||READ_ONCE (set->timeout))) +nft_set_ext_add (&priv->tmpl,NFT_SET_EXT_TIMEOUT ); priv->timeout = timeout;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v3 8/9 netfilter: nf_tables: zero timeout means element never times out
In reply to Pablo Neira AyusoThis patch uses zero as timeout marker for those elements that never expire when the element is created.
If userspace provides no timeout for an element, then the default set timeout applies. However, if no default set timeout is specified and timeout flag is set on, then timeout extension is allocated and timeout is set to zero to allow for future updates.
Use of zero a never timeout marker has been suggested by Phil Sutter.
Note that, in older kernels, it is already possible to define elements that never expire by declaring a set with the set timeout flag set on and no global set timeout, in this case, new element with no explicit timeout never expire do not allocate the timeout extension, hence, they never expire. This approach makes it complicated to accomodate element timeout update, because element extensions do not support reallocations. Therefore, allocate the timeout extension and use the new marker for this case, but do not expose it to userspace to retain backward compatibility in the set listing.
---
v3: - remove check for zero timeout in nf_msecs_to_jiffies64()
- remove unnecessary check for zero
per Phil Sutter.
include/net/netfilter/nf_tables.h | 7 +++-- include/uapi/linux/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 39 ++++++++++++++---------- net/netfilter/nft_dynset.c | 3 +- 4 files changed, 31 insertions(+), 20 deletions(-)
include/net/netfilter/nf_tables.h +5 -2
@@ -828,8 +828,11 @@ static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_exstatic inline bool __nft_set_elem_expired (const struct nft_set_ext *ext,u64 tstamp) { -return nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) && -time_after_eq64 (tstamp,READ_ONCE (nft_set_ext_timeout (ext)->expiration)); +if (!nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) || +nft_set_ext_timeout (ext)->timeout ==0 ) +return false; + +return time_after_eq64 (tstamp,READ_ONCE (nft_set_ext_timeout (ext)->expiration)); }static inline bool nft_set_elem_expired (const struct nft_set_ext *ext)
include/uapi/linux/netfilter/nf_tables.h +1 -1
@@ -436,7 +436,7 @@ enum nft_set_elem_flags {* @NFTA_SET_ELEM_KEY: key value (NLA_NESTED: nft_data) * @NFTA_SET_ELEM_DATA: data value of mapping (NLA_NESTED: nft_data_attributes) * @NFTA_SET_ELEM_FLAGS: bitmask of nft_set_elem_flags (NLA_U32) -* @NFTA_SET_ELEM_TIMEOUT: timeout value (NLA_U64) +* @NFTA_SET_ELEM_TIMEOUT: timeout value, zero means never times out (NLA_U64) * @NFTA_SET_ELEM_EXPIRATION: expiration time (NLA_U64) * @NFTA_SET_ELEM_USERDATA: user data (NLA_BINARY) * @NFTA_SET_ELEM_EXPR: expression (NLA_NESTED: nft_expr_attributes)
net/netfilter/nf_tables_api.c +23 -16
@@ -5809,24 +5809,31 @@ static int nf_tables_fill_setelem(struct sk_buff *skb, goto nla_put_failure;if (nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT )) { -u64 expires, now =get_jiffies_64 (); +u64 timeout =nft_set_ext_timeout (ext)->timeout; +u64 set_timeout =READ_ONCE (set->timeout); +__be64 msecs =0 ; + +if (set_timeout != timeout) { + msecs =nf_jiffies64_to_msecs (timeout); +if (nla_put_be64 (skb,NFTA_SET_ELEM_TIMEOUT , msecs, +NFTA_SET_ELEM_PAD )) + goto nla_put_failure; + } -if (nft_set_ext_timeout (ext)->timeout !=READ_ONCE (set->timeout) && -nla_put_be64 (skb,NFTA_SET_ELEM_TIMEOUT , -nf_jiffies64_to_msecs (nft_set_ext_timeout (ext)->timeout), -NFTA_SET_ELEM_PAD )) - goto nla_put_failure; +if (timeout >0 ) { +u64 expires, now =get_jiffies_64 (); - expires =READ_ONCE (nft_set_ext_timeout (ext)->expiration); -if (time_before64 (now, expires)) - expires -= now; -else - expires =0 ; + expires =READ_ONCE (nft_set_ext_timeout (ext)->expiration); +if (time_before64 (now, expires)) + expires -= now; +else + expires =0 ; -if (nla_put_be64 (skb,NFTA_SET_ELEM_EXPIRATION , -nf_jiffies64_to_msecs (expires), -NFTA_SET_ELEM_PAD )) - goto nla_put_failure; +if (nla_put_be64 (skb,NFTA_SET_ELEM_EXPIRATION , +nf_jiffies64_to_msecs (expires), +NFTA_SET_ELEM_PAD )) + goto nla_put_failure; + } }if (nft_set_ext_exists (ext,NFT_SET_EXT_USERDATA )) {
@@ -7009,7 +7016,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, goto err_parse_key_end; } -if (timeout >0 ) { +if (set->flags &NFT_SET_TIMEOUT ) { err =nft_set_ext_add (&tmpl,NFT_SET_EXT_TIMEOUT );if (err <0 ) goto err_parse_key_end;
net/netfilter/nft_dynset.c +2 -1
@@ -94,7 +94,8 @@ void nft_dynset_eval(const struct nft_expr *expr,if (set->ops->update (set, ®s->data[priv->sreg_key], nft_dynset_new, expr, regs, &ext)) {if (priv->op ==NFT_DYNSET_OP_UPDATE && -nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT )) { +nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) && +nft_set_ext_timeout (ext)->timeout !=0 ) { timeout = priv->timeout ? :READ_ONCE (set->timeout);WRITE_ONCE (nft_set_ext_timeout (ext)->expiration,get_jiffies_64 () + timeout); }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v3 9/9 netfilter: nf_tables: set element timeout update support
In reply to Pablo Neira AyusoStore new timeout and expiration in transaction object, use them to update elements from .commit path. Otherwise, discard update if .abort path is exercised.
Use update_flags in the transaction to note whether the timeout, expiration, or both need to be updated.
Annotate access to timeout extension now that it can be updated while lockless read access is possible.
Reject timeout updates on elements with no timeout extension.
Element transaction remains in the 96 bytes kmalloc slab on x86_64 after this update.
This patch requires ("netfilter: nf_tables: use timestamp to check for set element timeout") to make sure an element does not expire while transaction is ongoing.
---
v3: use .update_flags in transaction to fix expiration updates only.
include/net/netfilter/nf_tables.h | 16 ++++++++++- net/netfilter/nf_tables_api.c | 47 ++++++++++++++++++++++++++++--- net/netfilter/nft_dynset.c | 2 +- 3 files changed, 59 insertions(+), 6 deletions(-)
include/net/netfilter/nf_tables.h +15 -1
@@ -829,7 +829,7 @@ static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext, u64 tstamp) {if (!nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) || -nft_set_ext_timeout (ext)->timeout ==0 ) +READ_ONCE (nft_set_ext_timeout (ext)->timeout) ==0 )return false;return time_after_eq64 (tstamp,READ_ONCE (nft_set_ext_timeout (ext)->expiration));
@@ -1745,10 +1745,18 @@ struct nft_trans_table {#define nft_trans_table_update (trans) \ nft_trans_container_table(trans)->update +enum nft_trans_elem_flags { +NFT_TRANS_UPD_TIMEOUT = (1 <<0 ), +NFT_TRANS_UPD_EXPIRATION = (1 <<1 ), +}; +struct nft_trans_elem {struct nft_trans nft_trans;struct nft_set *set;struct nft_elem_priv *elem_priv; +u64 timeout; +u64 expiration; +u8 update_flags;bool bound; };
@@ -1758,6 +1766,12 @@ struct nft_trans_elem { nft_trans_container_elem(trans)->set#define nft_trans_elem_priv (trans) \ nft_trans_container_elem(trans)->elem_priv +#define nft_trans_elem_update_flags (trans) \ + nft_trans_container_elem(trans)->update_flags +#define nft_trans_elem_timeout (trans) \ + nft_trans_container_elem(trans)->timeout +#define nft_trans_elem_expiration (trans) \ + nft_trans_container_elem(trans)->expiration#define nft_trans_elem_set_bound (trans) \ nft_trans_container_elem(trans)->bound
net/netfilter/nf_tables_api.c +43 -4
78 lines not on this page · raw message
net/netfilter/nft_dynset.c +1 -1
@@ -95,7 +95,7 @@ void nft_dynset_eval(const struct nft_expr *expr, expr, regs, &ext)) {if (priv->op ==NFT_DYNSET_OP_UPDATE &&nft_set_ext_exists (ext,NFT_SET_EXT_TIMEOUT ) && -nft_set_ext_timeout (ext)->timeout !=0 ) { +READ_ONCE (nft_set_ext_timeout (ext)->timeout) !=0 ) { timeout = priv->timeout ? :READ_ONCE (set->timeout);WRITE_ONCE (nft_set_ext_timeout (ext)->expiration,get_jiffies_64 () + timeout); }
Signature · 1 line
2.30.2