public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] netfilter: unlock xt_table earlier in __do_replace
@ 2018-02-16 10:04 Xin Long
  2018-02-16 11:02 ` Florian Westphal
  0 siblings, 1 reply; 4+ messages in thread
From: Xin Long @ 2018-02-16 10:04 UTC (permalink / raw)
  To: network dev, netfilter-devel; +Cc: davem, pablo, fw

Now it's doing cleanup_entry for oldinfo under the xt_table lock,
but it's not really necessary. After the replacement job is done
in xt_replace_table, oldinfo is not used elsewhere any more, and
it can be freed without xt_table lock safely.

The important thing is that rtnl_lock is called in some xt_target
destroy, which means rtnl_lock, a big lock is used in xt_table
lock, a smaller one. It usually could be the reason why a dead
lock may happen.

Besides, all xt_target/match checkentry is called out of xt_table
lock. It's better also to move all cleanup_entry calling out of
xt_table lock, just as do_replace_finish does for ebtables.

Signed-off-by: Xin Long <lucien.xin@gmail.com>
---
 net/ipv4/netfilter/arp_tables.c | 3 ++-
 net/ipv4/netfilter/ip_tables.c  | 3 ++-
 net/ipv6/netfilter/ip6_tables.c | 3 ++-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/net/ipv4/netfilter/arp_tables.c b/net/ipv4/netfilter/arp_tables.c
index 4ffe302..f6c7404 100644
--- a/net/ipv4/netfilter/arp_tables.c
+++ b/net/ipv4/netfilter/arp_tables.c
@@ -921,6 +921,8 @@ static int __do_replace(struct net *net, const char *name,
 	    (newinfo->number <= oldinfo->initial_entries))
 		module_put(t->me);
 
+	xt_table_unlock(t);
+
 	get_old_counters(oldinfo, counters);
 
 	/* Decrease module usage counts and free resource */
@@ -935,7 +937,6 @@ static int __do_replace(struct net *net, const char *name,
 		net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
 	}
 	vfree(counters);
-	xt_table_unlock(t);
 	return ret;
 
  put_module:
diff --git a/net/ipv4/netfilter/ip_tables.c b/net/ipv4/netfilter/ip_tables.c
index 9a71f31..a6517c1 100644
--- a/net/ipv4/netfilter/ip_tables.c
+++ b/net/ipv4/netfilter/ip_tables.c
@@ -1082,6 +1082,8 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
 	    (newinfo->number <= oldinfo->initial_entries))
 		module_put(t->me);
 
+	xt_table_unlock(t);
+
 	get_old_counters(oldinfo, counters);
 
 	/* Decrease module usage counts and free resource */
@@ -1095,7 +1097,6 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
 		net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
 	}
 	vfree(counters);
-	xt_table_unlock(t);
 	return ret;
 
  put_module:
diff --git a/net/ipv6/netfilter/ip6_tables.c b/net/ipv6/netfilter/ip6_tables.c
index af4c917..96577c9 100644
--- a/net/ipv6/netfilter/ip6_tables.c
+++ b/net/ipv6/netfilter/ip6_tables.c
@@ -1101,6 +1101,8 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
 	    (newinfo->number <= oldinfo->initial_entries))
 		module_put(t->me);
 
+	xt_table_unlock(t);
+
 	get_old_counters(oldinfo, counters);
 
 	/* Decrease module usage counts and free resource */
@@ -1114,7 +1116,6 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
 		net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
 	}
 	vfree(counters);
-	xt_table_unlock(t);
 	return ret;
 
  put_module:
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net] netfilter: unlock xt_table earlier in __do_replace
  2018-02-16 10:04 [PATCH net] netfilter: unlock xt_table earlier in __do_replace Xin Long
@ 2018-02-16 11:02 ` Florian Westphal
  2018-02-16 11:25   ` Xin Long
  0 siblings, 1 reply; 4+ messages in thread
From: Florian Westphal @ 2018-02-16 11:02 UTC (permalink / raw)
  To: Xin Long; +Cc: network dev, netfilter-devel, davem, pablo, fw

Xin Long <lucien.xin@gmail.com> wrote:
> Now it's doing cleanup_entry for oldinfo under the xt_table lock,
> but it's not really necessary. After the replacement job is done
> in xt_replace_table, oldinfo is not used elsewhere any more, and
> it can be freed without xt_table lock safely.

Right.

> The important thing is that rtnl_lock is called in some xt_target
> destroy, which means rtnl_lock, a big lock is used in xt_table
> lock, a smaller one. It usually could be the reason why a dead
> lock may happen.

In which cases do we aquire the xt table mutex from places that hold
rtnl mutex?

> Besides, all xt_target/match checkentry is called out of xt_table
> lock. It's better also to move all cleanup_entry calling out of
> xt_table lock, just as do_replace_finish does for ebtables.

Agree but I don't see how this patch fixes a bug so I would prefer if
this could simmer in nf-next first.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net] netfilter: unlock xt_table earlier in __do_replace
  2018-02-16 11:02 ` Florian Westphal
@ 2018-02-16 11:25   ` Xin Long
  2018-03-05 22:10     ` Pablo Neira Ayuso
  0 siblings, 1 reply; 4+ messages in thread
From: Xin Long @ 2018-02-16 11:25 UTC (permalink / raw)
  To: Florian Westphal; +Cc: network dev, netfilter-devel, davem, Pablo Neira Ayuso

On Fri, Feb 16, 2018 at 12:02 PM, Florian Westphal <fw@strlen.de> wrote:
> Xin Long <lucien.xin@gmail.com> wrote:
>> Now it's doing cleanup_entry for oldinfo under the xt_table lock,
>> but it's not really necessary. After the replacement job is done
>> in xt_replace_table, oldinfo is not used elsewhere any more, and
>> it can be freed without xt_table lock safely.
>
> Right.
>
>> The important thing is that rtnl_lock is called in some xt_target
>> destroy, which means rtnl_lock, a big lock is used in xt_table
>> lock, a smaller one. It usually could be the reason why a dead
>> lock may happen.
>
> In which cases do we aquire the xt table mutex from places that hold
> rtnl mutex?
Not really now.
But there was one, which though had been fixed in another way in:
https://patchwork.ozlabs.org/patch/870797/

I meant this kind of case (big lock used under small lock) have the
risk that may cause a dead lock.
Sorry for confusing.

>
>> Besides, all xt_target/match checkentry is called out of xt_table
>> lock. It's better also to move all cleanup_entry calling out of
>> xt_table lock, just as do_replace_finish does for ebtables.
>
> Agree but I don't see how this patch fixes a bug so I would prefer if
> this could simmer in nf-next first.
Sure. No bug fix, it's an improvement.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net] netfilter: unlock xt_table earlier in __do_replace
  2018-02-16 11:25   ` Xin Long
@ 2018-03-05 22:10     ` Pablo Neira Ayuso
  0 siblings, 0 replies; 4+ messages in thread
From: Pablo Neira Ayuso @ 2018-03-05 22:10 UTC (permalink / raw)
  To: Xin Long; +Cc: Florian Westphal, network dev, netfilter-devel, davem

On Fri, Feb 16, 2018 at 12:25:56PM +0100, Xin Long wrote:
> On Fri, Feb 16, 2018 at 12:02 PM, Florian Westphal <fw@strlen.de> wrote:
> > Xin Long <lucien.xin@gmail.com> wrote:
[...]
> >> Besides, all xt_target/match checkentry is called out of xt_table
> >> lock. It's better also to move all cleanup_entry calling out of
> >> xt_table lock, just as do_replace_finish does for ebtables.
> >
> > Agree but I don't see how this patch fixes a bug so I would prefer if
> > this could simmer in nf-next first.
>
> Sure. No bug fix, it's an improvement.

Applied to nf-next, thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-03-05 22:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-16 10:04 [PATCH net] netfilter: unlock xt_table earlier in __do_replace Xin Long
2018-02-16 11:02 ` Florian Westphal
2018-02-16 11:25   ` Xin Long
2018-03-05 22:10     ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox