Netfilter: Clean up the kmod handling code in iptables.c
Subsystems (3)
PATCH 9/18 Netfilter: Clean up the kmod handling code in iptables.c
Name: Clean up the kmod handling code in iptables.c
Status: Tested under nfsim
We now have a convenient kmod macro try_then_request_module().
1) Split our current "generic" function into separate find_target_lock(), find_table_lock() and find_match_lock() functions explicitly.
2) Have those functions do try_module_get() for us, and fix up the one caller who didn't do that anyway.
3) Have the caller use try_then_request_module().
4) Remove __ipt_mutex_up() and __ipt_find_target_lock() which weren't used (even in patch-o-matic AFAICT).
This cleanup takes us closer to using standard list macros everywhere, and makes the version patch simpler.
net/ipv4/netfilter/ip_tables.c +68 -105
@@ -26,6 +26,7 @@#include <asm/uaccess.h> #include <asm/semaphore.h> #include <linux/proc_fs.h> +#include <linux/err.h> #include <linux/netfilter_ipv4/ip_tables.h>
@@ -408,85 +409,55 @@#endif } -/* If it succeeds, returns element and locks mutex */ -static inline void * -find_inlist_lock_noload (struct list_head *head, -const char *name, -int *error, -struct semaphore *mutex) -{ -void *ret; - -#if 0 -duprintf ("find_inlist: searching for `%s' in %s.\n" , - name, head == &ipt_target ?"ipt_target" - : head == &ipt_match ?"ipt_match" - : head == &ipt_tables ?"ipt_tables" :"UNKNOWN" ); -#endif - - *error =down_interruptible (mutex); -if (*error !=0 ) -return NULL ; +/* +* These are weird, but module loading must not be done with mutex +* held (since they will register), and we have to have a single +* function to use try_then_request_module(). +*/ - ret =list_named_find (head, name); -if (!ret) { - *error = -ENOENT ; -up (mutex); - } -return ret; -} +/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ +static inline struct ipt_table *find_table_lock (const char *name) +{ +struct ipt_table *t; -#ifndef CONFIG_KMOD -#define find_inlist_lock (h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m)) -#else -static void * -find_inlist_lock (struct list_head *head, -const char *name, -const char *prefix, -int *error, -struct semaphore *mutex) -{ -void *ret; - - ret =find_inlist_lock_noload (head, name, error, mutex); -if (!ret) { -duprintf ("find_inlist: loading `%s%s'.\n" , prefix, name); -request_module ("%s%s" , prefix, name); - ret =find_inlist_lock_noload (head, name, error, mutex); - } +if (down_interruptible (&ipt_mutex) !=0 ) +return ERR_PTR (-EINTR ); -return ret; +list_for_each_entry (t, &ipt_tables, list) +if (strcmp (t->name, name) ==0 &&try_module_get (t->me)) +return t; +up (&ipt_mutex); +return NULL ; } -#endif -static inline struct ipt_table * -ipt_find_table_lock (const char *name,int *error,struct semaphore *mutex) +/* Find match by name, grabs mutex & ref. Returns ERR_PTR() on error. */ +static inline struct ipt_match *find_match_lock (const char *name) { -return find_inlist_lock (&ipt_tables, name,"iptable_" , error, mutex); -} +struct ipt_match *m; -static inline struct ipt_match * -find_match_lock (const char *name,int *error,struct semaphore *mutex) -{ -return find_inlist_lock (&ipt_match, name,"ipt_" , error, mutex); -} +if (down_interruptible (&ipt_mutex) !=0 ) +return ERR_PTR (-EINTR ); -static struct ipt_target * -ipt_find_target_lock (const char *name,int *error,struct semaphore *mutex) -{ -return find_inlist_lock (&ipt_target, name,"ipt_" , error, mutex); +list_for_each_entry (m, &ipt_match, list) +if (strcmp (m->name, name) ==0 &&try_module_get (m->me)) +return m; +up (&ipt_mutex); +return NULL ; } -struct ipt_target * -__ipt_find_target_lock (const char *name,int *error) +/* Find target by name, grabs mutex & ref. Returns ERR_PTR() on error. */ +static inline struct ipt_target *find_target_lock (const char *name) { -return ipt_find_target_lock (name,error,&ipt_mutex); -} +struct ipt_target *t; -void -__ipt_mutex_up (void ) -{ +if (down_interruptible (&ipt_mutex) !=0 ) +return ERR_PTR (-EINTR ); + +list_for_each_entry (t, &ipt_target, list) +if (strcmp (t->name, name) ==0 &&try_module_get (t->me)) +return t;up (&ipt_mutex); +return NULL ; }/* All zeroes == unconditional rule. */
@@ -648,17 +619,13 @@unsigned int hookmask, unsigned int *i) { - int ret; struct ipt_match *match; - match =find_match_lock (m->u.user.name, &ret, &ipt_mutex); -if (!match) { + match =try_then_request_module (find_match_lock (m->u.user.name), +"ipt_%s" , m->u.user.name); +if (IS_ERR (match) || !match) {duprintf ("check_match: `%s' not found\n" , m->u.user.name); -return ret; - } -if (!try_module_get (match->me)) { -up (&ipt_mutex); -return -ENOENT ; +return match ?PTR_ERR (match) : -ENOENT ; } m->u.kernel.match = match;up (&ipt_mutex);
@@ -699,14 +666,11 @@ goto cleanup_matches; t =ipt_get_target (e); - target =ipt_find_target_lock (t->u.user.name, &ret, &ipt_mutex); -if (!target) { + target =try_then_request_module (find_target_lock (t->u.user.name), +"ipt_%s" , t->u.user.name); +if (IS_ERR (target) || !target) {duprintf ("check_entry: `%s' not found\n" , t->u.user.name); - goto cleanup_matches; - } -if (!try_module_get (target->me)) { -up (&ipt_mutex); - ret = -ENOENT ; + ret = target ?PTR_ERR (target) : -ENOENT ; goto cleanup_matches; } t->u.kernel.target = target;
@@ -1036,8 +1000,8 @@int ret;struct ipt_table *t; - t =ipt_find_table_lock (entries->name, &ret, &ipt_mutex); -if (t) { + t =find_table_lock (entries->name); +if (t && !IS_ERR (t)) {duprintf ("t->private->number = %u\n" , t->private->number);if (entries->size == t->private->size)
@@ -1049,10 +1013,10 @@ entries->size); ret = -EINVAL ; } +module_put (t->me);up (&ipt_mutex); }else -duprintf ("get_entries: Can't find %s!\n" , - entries->name); + ret = t ?PTR_ERR (t) : -ENOENT ;return ret; }
@@ -1103,25 +1067,21 @@duprintf ("ip_tables: Translated table\n" ); - t =ipt_find_table_lock (tmp.name, &ret, &ipt_mutex); -if (!t) + t =try_then_request_module (find_table_lock (tmp.name), +"iptable_%s" , tmp.name); +if (!t ||IS_ERR (t)) { + ret = t ?PTR_ERR (t) : -ENOENT ; goto free_newinfo_counters_untrans; + }/* You lied! */ if (tmp.valid_hooks != t->valid_hooks) {duprintf ("Valid hook crap: %08X vs %08X\n" , tmp.valid_hooks, t->valid_hooks); ret = -EINVAL ; - goto free_newinfo_counters_untrans_unlock; - } - -/* Get a reference in advance, we're not allowed fail later */ -if (!try_module_get (t->me)) { - ret = -EBUSY ; - goto free_newinfo_counters_untrans_unlock; + goto put_module; } - oldinfo =replace_table (t, tmp.num_counters, newinfo, &ret);if (!oldinfo) goto put_module;
@@ -1150,7 +1110,6 @@ put_module:module_put (t->me); - free_newinfo_counters_untrans_unlock:up (&ipt_mutex); free_newinfo_counters_untrans:IPT_ENTRY_ITERATE (newinfo->entries, newinfo->size, cleanup_entry,NULL );
@@ -1189,7 +1148,7 @@unsigned int i;struct ipt_counters_info tmp, *paddc;struct ipt_table *t; -int ret; +int ret =0 ;if (copy_from_user (&tmp, user,sizeof (tmp)) !=0 )return -EFAULT ;
@@ -1206,9 +1165,11 @@ goto free; } - t =ipt_find_table_lock (tmp.name, &ret, &ipt_mutex); -if (!t) + t =find_table_lock (tmp.name); +if (!t ||IS_ERR (t)) { + ret = t ?PTR_ERR (t) : -ENOENT ; goto free; + }write_lock_bh (&t->lock);if (t->private->number != paddc->num_counters) {
@@ -1225,6 +1186,7 @@ unlock_up_free:write_unlock_bh (&t->lock);up (&ipt_mutex); +module_put (t->me); free:vfree (paddc);
@@ -1281,8 +1243,10 @@break ; } name[IPT_TABLE_MAXNAMELEN -1 ] ='\0' ; - t =ipt_find_table_lock (name, &ret, &ipt_mutex); -if (t) { + + t =try_then_request_module (find_table_lock (name), +"iptable_%s" , name); +if (t && !IS_ERR (t)) {struct ipt_getinfo info; info.valid_hooks = t->valid_hooks;
@@ -1298,9 +1262,10 @@ ret = -EFAULT ; else ret =0 ; -up (&ipt_mutex); - } +module_put (t->me); + }else + ret = t ?PTR_ERR (t) : -ENOENT ; }break ;
@@ -1889,8 +1854,6 @@EXPORT_SYMBOL (ipt_do_table);EXPORT_SYMBOL (ipt_register_target);EXPORT_SYMBOL (ipt_unregister_target); -EXPORT_SYMBOL_GPL (__ipt_find_target_lock); -EXPORT_SYMBOL_GPL (__ipt_mutex_up);module_init (init);module_exit (fini);
include/linux/netfilter_ipv4/ip_tables.h +0 -4
@@ -413,10 +413,6 @@extern int ipt_register_match (struct ipt_match *match);extern void ipt_unregister_match (struct ipt_match *match); -extern struct ipt_target * -__ipt_find_target_lock (const char *name,int *error); -extern void __ipt_mutex_up (void ); -/* Furniture shopping... */ struct ipt_table {
* Rusty Russell <[email protected]> 2005-01-05 14:33
Quoted 5 lines
Name: Clean up the kmod handling code in
iptables.c
Status: Tested under nfsim4) Remove
__ipt_mutex_up()and__ipt_find_target_lock()which weren't used (even in patch-o-matic AFAICT).
Those are used by ipt action (net/sched/ipt.c).
Readds __ipt_find_target_lock() and __ipt_mutex_up() and adapts ipt.c to correctly use it. module must be given back properly upon cleanup. removes the comments demanding for proper module refcnt since find_target_lock() takes care of this now.
Jamal: Can you please verify?
include/linux/netfilter_ipv4/ip_tables.h +3 -0
@@ -432,6 +432,9 @@extern int ipt_register_match (struct ipt_match *match);extern void ipt_unregister_match (struct ipt_match *match); +extern struct ipt_target *__ipt_find_target_lock (const char *name,u8 rev); +extern void __ipt_mutex_up (void ); +/* Furniture shopping... */ struct ipt_table {
net/ipv4/netfilter/ip_tables.c +12 -0
@@ -488,6 +488,16 @@return ERR_PTR (-EPROTOTYPE ); } +struct ipt_target *__ipt_find_target_lock (const char *name,u8 revision) +{ +return find_target_lock (name, revision); +} + +void __ipt_mutex_up (void ) +{ +up (&ipt_mutex); +} +static int match_revfn (const char *name,u8 revision,int *bestp) {struct ipt_match *m;
@@ -1959,6 +1969,8 @@EXPORT_SYMBOL (ipt_do_table);EXPORT_SYMBOL (ipt_register_target);EXPORT_SYMBOL (ipt_unregister_target); +EXPORT_SYMBOL_GPL (__ipt_find_target_lock); +EXPORT_SYMBOL_GPL (__ipt_mutex_up);module_init (init);module_exit (fini);
net/sched/ipt.c +7 -11
@@ -60,19 +60,14 @@struct ipt_target *target;int ret =0 ;struct ipt_entry_target *t = p->t; - target =__ipt_find_target_lock (t->u.user.name, &ret); + target =__ipt_find_target_lock (t->u.user.name, t->u.user.revision); -if (!target) { +if (IS_ERR (target) || !target) {printk ("init_targ: Failed to find %s\n" , t->u.user.name);return -1 ; }DPRINTK ("init_targ: found %s\n" , target->name); -/* we really need proper ref counting -seems to be only needed for modules?? Talk to laforge */ -/* if (target->me) -__MOD_INC_USE_COUNT(target->me); -*/ t->u.kernel.target = target;__ipt_mutex_up ();
@@ -81,9 +76,6 @@ && !t->u.kernel.target->checkentry(p->tname, NULL, t->data, t->u.target_size - sizeof (*t), p->hook)) { -/* if (t->u.kernel.target->me) -__MOD_DEC_USE_COUNT(t->u.kernel.target->me); -*/ DPRINTK ("ip_tables: check failed for `%s'.\n" , t->u.kernel.target->name); ret = -EINVAL ;
@@ -235,8 +227,12 @@ {struct tcf_ipt *p; p =PRIV (a,ipt); -if (NULL != p) +if (NULL != p) { +struct ipt_entry_target *t = p->t; +if (t && t->u.kernel.target) +module_put (t->u.kernel.target->me);return tcf_hash_release (p, bind); + }return 0 ; }
Quoted 7 lines · Thomas Graf
* Rusty Russell <[email protected]> 2005-01-05 14:33
Quoted 5 lines
Name: Clean up the kmod handling code in
iptables.c
Status: Tested under nfsim4) Remove
__ipt_mutex_up()and__ipt_find_target_lock()which weren't used (even in patch-o-matic AFAICT).
Quoted 6 lines
Those are used by ipt action (
net/sched/ipt.c).Readds
__ipt_find_target_lock()and__ipt_mutex_up()and adaptsipt.cto correctly use it. module must be given back properly upon cleanup. removes the comments demanding for proper module refcnt sincefind_target_lock()takes care of this now.
OK, thanks. You missed a module_put in the check_entry-failed error path though. I looked harder at this, and I prefer this patch:
Name: Restore net/sched/ipt.c After iptables Kmod Cleanup
Status: Tested under nfsim
Thomas Graf points out that I broke net/sched/ipt.c when I removed __ipt_find_target_lock. In fact, those routines don't need to keep the lock held, so we can simplify them, and expose an interface (ipt_find_target) which does module loading correctly for net/sched/ipt.c.
As Thomas points out, this also gets the module refcounts right.
net/ipv4/netfilter/ip_tables.c +31 -32
@@ -430,62 +430,63 @@return NULL ; } -/* Find match, grabs mutex & ref. Returns ERR_PTR() on error. */ -static inline struct ipt_match *find_match_lock (const char *name,u8 revision) +/* Find match, grabs ref. Returns ERR_PTR() on error. */ +static inline struct ipt_match *find_match (const char *name,u8 revision) {struct ipt_match *m; -int found =0 ; +int err =0 ;if (down_interruptible (&ipt_mutex) !=0 )return ERR_PTR (-EINTR );list_for_each_entry (m, &ipt_match, list) {if (strcmp (m->name, name) ==0 ) { - found =1 ;if (m->revision == revision) { -if (!try_module_get (m->me)) - found =0 ; -else +if (try_module_get (m->me)) { +up (&ipt_mutex);return m; - } + } + }else + err = -EPROTOTYPE ;/* Found something. */ } }up (&ipt_mutex); - -/* Not found at all? NULL so try_then_request_module loads module. */ -if (!found) -return NULL ; - -return ERR_PTR (-EPROTOTYPE ); +return ERR_PTR (err); } -/* Find target, grabs mutex & ref. Returns ERR_PTR() on error. */ -static inline struct ipt_target *find_target_lock (const char *name,u8 revision) +/* Find target, grabs ref. Returns ERR_PTR() on error. */ +static inline struct ipt_target *find_target (const char *name,u8 revision) {struct ipt_target *t; -int found =0 ; +int err =0 ;if (down_interruptible (&ipt_mutex) !=0 )return ERR_PTR (-EINTR );list_for_each_entry (t, &ipt_target, list) {if (strcmp (t->name, name) ==0 ) { - found =1 ;if (t->revision == revision) { -if (!try_module_get (t->me)) - found =0 ; -else +if (try_module_get (t->me)) { +up (&ipt_mutex);return t; - } + } + }else + err = -EPROTOTYPE ;/* Found something. */ } }up (&ipt_mutex); +return ERR_PTR (err); +} -/* Not found at all? NULL so try_then_request_module loads module. */ -if (!found) -return NULL ; +struct ipt_target *ipt_find_target (const char *name,u8 revision) +{ +struct ipt_target *target; -return ERR_PTR (-EPROTOTYPE ); + target =try_then_request_module (find_target (name, revision), +"ipt_%s" , name); +if (IS_ERR (target) || !target) +return NULL ; +return target; }static int match_revfn (const char *name,u8 revision,int *bestp)
@@ -708,15 +709,14 @@ { struct ipt_match *match; - match = try_then_request_module(find_match_lock(m->u.user.name, - m->u.user.revision), + match = try_then_request_module(find_match(m->u.user.name, + m->u.user.revision), "ipt_%s", m->u.user.name); if (IS_ERR(match) || !match) { duprintf("check_match: `%s' not found\n", m->u.user.name); return match ? PTR_ERR(match) : -ENOENT; } m->u.kernel.match = match; - up(&ipt_mutex); if (m->u.kernel.match->checkentry && !m->u.kernel.match->checkentry(name, ip, m->data,
@@ -754,8 +754,8 @@ goto cleanup_matches; t =ipt_get_target (e); - target =try_then_request_module (find_target_lock (t->u.user.name, - t->u.user.revision), + target =try_then_request_module (find_target (t->u.user.name, + t->u.user.revision),"ipt_%s" , t->u.user.name);if (IS_ERR (target) || !target) {duprintf ("check_entry: `%s' not found\n" , t->u.user.name);
@@ -763,7 +763,6 @@ goto cleanup_matches; } t->u.kernel.target = target; -up (&ipt_mutex);if (t->u.kernel.target == &ipt_standard_target) {if (!standard_check (t, size)) {
net/sched/ipt.c +8 -12
@@ -31,6 +31,7 @@#include <linux/module.h> #include <linux/init.h> #include <linux/proc_fs.h> +#include <linux/kmod.h> #include <net/sock.h> #include <net/pkt_sched.h> #include <linux/tc_act/tc_ipt.h>
@@ -60,32 +61,23 @@struct ipt_target *target;int ret =0 ;struct ipt_entry_target *t = p->t; - target =__ipt_find_target_lock (t->u.user.name, &ret); + target =ipt_find_target (t->u.user.name, t->u.user.revision);if (!target) {printk ("init_targ: Failed to find %s\n" , t->u.user.name);return -1 ; }DPRINTK ("init_targ: found %s\n" , target->name); -/* we really need proper ref counting -seems to be only needed for modules?? Talk to laforge */ -/* if (target->me) -__MOD_INC_USE_COUNT(target->me); -*/ t->u.kernel.target = target; -__ipt_mutex_up (); -if (t->u.kernel.target->checkentry && !t->u.kernel.target->checkentry (p->tname,NULL , t->data, t->u.target_size -sizeof (*t), p->hook)) { -/* if (t->u.kernel.target->me) -__MOD_DEC_USE_COUNT(t->u.kernel.target->me); -*/ DPRINTK ("ip_tables: check failed for `%s'.\n" , t->u.kernel.target->name); +module_put (t->u.kernel.target->me); ret = -EINVAL ; }
@@ -235,8 +227,12 @@ {struct tcf_ipt *p; p =PRIV (a,ipt); -if (NULL != p) +if (NULL != p) { +struct ipt_entry_target *t = p->t; +if (t && t->u.kernel.target) +module_put (t->u.kernel.target->me);return tcf_hash_release (p, bind); + }return 0 ; }
include/linux/netfilter_ipv4/ip_tables.h +3 -0
@@ -456,6 +456,9 @@struct module *me; }; +/* net/sched/ipt.c: Gimme access to your targets! Gets target->me. */ +extern struct ipt_target *ipt_find_target (const char *name,u8 revision); +extern int ipt_register_table (struct ipt_table *table);extern void ipt_unregister_table (struct ipt_table *table); extern unsigned int ipt_do_table(struct sk_buff **pskb,
Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman
* Rusty Russell <[email protected]> 2005-01-06 12:10
Quoted 10 lines · Thomas Graf
Quoted 9 lines
* Rusty Russell <[email protected]> 2005-01-05 14:33
Quoted 5 lines
Name: Clean up the kmod handling code in
iptables.c
Status: Tested under nfsim4) Remove
__ipt_mutex_up()and__ipt_find_target_lock()which weren't used (even in patch-o-matic AFAICT).Those are used by ipt action (
net/sched/ipt.c).
Quoted 7 lines
Quoted 4 lines
Readds
__ipt_find_target_lock()and__ipt_mutex_up()and adaptsipt.cto correctly use it. module must be given back properly upon cleanup. removes the comments demanding for proper module refcnt sincefind_target_lock()takes care of this now.OK, thanks. You missed a
module_putin the check_entry-failed error path though.
Agreed, thanks.
Quoted 1 line
I looked harder at this, and I prefer this patch:
Looks fine to me.
Quoted 3 lines · Thomas Graf
Quoted 1 line
I looked harder at this, and I prefer this patch:
Looks fine to me.
Thanks Thomas for catching this.
Patch looks good from here too. Andrew caught the unexported ipt_find_target for completion.
BTW, do we have a sane IDE maintainer yet?[1] If we do, maybe its time i joined lk list again ;-> I have been waiting for a decade to fulfil that promise;->
At some point Alan took over - but Alans sanity is somewhere on the edge of genius and insanity - so i wasnt sure which side of the line he was going to end up on after he took over. As Russell King puts it in not so many words "What is it with Linux IDE and its maintainers?". Alas! my attempts to monitor Alan were short lived.
I hear the new guy may be ok - but at times conflicting sentiments cross my path about him. So still watching ...
cheers,
jamal
[1] Ok, theres actually some moral to this caffeine tale;-> netdev cc for net patches.