From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Patrick McHardy <kaber@trash.net>
Cc: herbert@gondor.apana.org.au, tgraf@suug.ch, davem@davemloft.net,
David.Laight@ACULAB.COM, ying.xue@windriver.com,
paulmck@linux.vnet.ibm.com, netdev@vger.kernel.org,
netfilter-devel@vger.kernel.org
Subject: Re: [PATCH 3/9] nftables: nft_rbtree: fix locking
Date: Fri, 30 Jan 2015 11:52:17 +0100 [thread overview]
Message-ID: <20150130105217.GA4093@salvia> (raw)
In-Reply-To: <1422603994-5836-4-git-send-email-kaber@trash.net>
Hi Patrick,
On Fri, Jan 30, 2015 at 07:46:28AM +0000, Patrick McHardy wrote:
> Fix a race condition and unnecessary locking:
>
> * the root rb_node must only be accessed under the lock in nft_rbtree_lookup()
> * the lock is not needed in lookup functions in netlink contexts
>
> Signed-off-by: Patrick McHardy <kaber@trash.net>
> ---
> net/netfilter/nft_rbtree.c | 12 +++---------
> 1 file changed, 3 insertions(+), 9 deletions(-)
>
> diff --git a/net/netfilter/nft_rbtree.c b/net/netfilter/nft_rbtree.c
> index 46214f2..417796f 100644
> --- a/net/netfilter/nft_rbtree.c
> +++ b/net/netfilter/nft_rbtree.c
> @@ -37,10 +37,11 @@ static bool nft_rbtree_lookup(const struct nft_set *set,
> {
> const struct nft_rbtree *priv = nft_set_priv(set);
> const struct nft_rbtree_elem *rbe, *interval = NULL;
> - const struct rb_node *parent = priv->root.rb_node;
> + const struct rb_node *parent;
> int d;
>
> spin_lock_bh(&nft_rbtree_lock);
> + parent = priv->root.rb_node;
Good catch.
> while (parent != NULL) {
> rbe = rb_entry(parent, struct nft_rbtree_elem, node);
>
> @@ -158,7 +159,6 @@ static int nft_rbtree_get(const struct nft_set *set, struct nft_set_elem *elem)
> struct nft_rbtree_elem *rbe;
> int d;
>
> - spin_lock_bh(&nft_rbtree_lock);
> while (parent != NULL) {
> rbe = rb_entry(parent, struct nft_rbtree_elem, node);
>
> @@ -173,11 +173,9 @@ static int nft_rbtree_get(const struct nft_set *set, struct nft_set_elem *elem)
> !(rbe->flags & NFT_SET_ELEM_INTERVAL_END))
> nft_data_copy(&elem->data, rbe->data);
> elem->flags = rbe->flags;
> - spin_unlock_bh(&nft_rbtree_lock);
> return 0;
> }
> }
> - spin_unlock_bh(&nft_rbtree_lock);
> return -ENOENT;
this chunk looks fine to me, we always hold the nfnetlink mutex.
> }
> @@ -190,7 +188,6 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,
> struct nft_set_elem elem;
> struct rb_node *node;
>
> - spin_lock_bh(&nft_rbtree_lock);
> for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) {
> if (iter->count < iter->skip)
> goto cont;
> @@ -203,14 +200,11 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,
> elem.flags = rbe->flags;
>
> iter->err = iter->fn(ctx, set, iter, &elem);
> - if (iter->err < 0) {
> - spin_unlock_bh(&nft_rbtree_lock);
> + if (iter->err < 0)
> return;
> - }
> cont:
> iter->count++;
> }
> - spin_unlock_bh(&nft_rbtree_lock);
> }
>
I think that _walk still needs the lock there. This is called from
nf_tables_dump_set() for each recvmsg() in netlink, and IIRC unlike
rtnetlink the dump path in nfnetlink is lockless.
next prev parent reply other threads:[~2015-01-30 10:52 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-30 7:46 [PATCH 0/9 WIP] nf_tables: set extensions and dynamic updates Patrick McHardy
2015-01-30 7:46 ` [PATCH 1/9] rhashtable: simplify rhashtable_remove() Patrick McHardy
2015-01-30 16:36 ` Thomas Graf
2015-01-30 7:46 ` [PATCH 2/9] nftables: reject NFT_SET_ELEM_INTERVAL_END flag for non-interval sets Patrick McHardy
2015-01-30 17:31 ` Pablo Neira Ayuso
2015-01-30 17:55 ` Patrick McHardy
2015-01-30 18:00 ` Pablo Neira Ayuso
2015-01-30 7:46 ` [PATCH 3/9] nftables: nft_rbtree: fix locking Patrick McHardy
2015-01-30 10:52 ` Pablo Neira Ayuso [this message]
2015-01-30 7:46 ` [PATCH 4/9] netfilter: nf_tables: add set extensions Patrick McHardy
2015-01-30 7:46 ` [PATCH 5/9] netfilter: nf_tables: convert hash and rbtree to " Patrick McHardy
2015-01-30 7:46 ` [PATCH 6/9] netfilter: nf_tables: add set timeout support Patrick McHardy
2015-01-30 7:46 ` [PATCH 7/9] netfilter: nft_hash: add support for timeouts Patrick McHardy
2015-01-31 4:29 ` Herbert Xu
2015-01-31 12:16 ` Patrick McHardy
2015-01-30 7:46 ` [PATCH 8/9] netfilter: nft_lookup: add missing attribute validation for NFTA_LOOKUP_SET_ID Patrick McHardy
2015-01-30 7:46 ` [PATCH 9/9] netfilter: nf_tables: add support for dynamic set updates Patrick McHardy
2015-01-30 9:28 ` Herbert Xu
2015-01-30 10:08 ` Patrick McHardy
2015-01-30 10:18 ` Herbert Xu
2015-01-30 11:29 ` Herbert Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150130105217.GA4093@salvia \
--to=pablo@netfilter.org \
--cc=David.Laight@ACULAB.COM \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=kaber@trash.net \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=paulmck@linux.vnet.ibm.com \
--cc=tgraf@suug.ch \
--cc=ying.xue@windriver.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.