diff mbox

[2/2] genl: Hold reference on correct module while netlink-dump.

Message ID 1377143892-20763-1-git-send-email-pshelar@nicira.com
State RFC, archived
Delegated to: David Miller
Headers show

Commit Message

Pravin B Shelar Aug. 22, 2013, 3:58 a.m. UTC
netlink dump operations take module as parameter to hold
reference for entire netlink dump duration.
Currently it holds ref only on genl module which is not correct
when we use ops registered to genl from another module.
Following patch adds module pointer to genl_ops so that netlink
can hold ref count on it.

CC: Jesse Gross <jesse@nicira.com>
CC: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: Pravin B Shelar <pshelar@nicira.com>
---
 include/net/genetlink.h |   21 ++++++++++++++++++---
 net/netlink/genetlink.c |   39 ++++++++++++++++++++++++---------------
 2 files changed, 42 insertions(+), 18 deletions(-)

Comments

Johannes Berg Aug. 22, 2013, 7:40 a.m. UTC | #1
On Wed, 2013-08-21 at 20:58 -0700, Pravin B Shelar wrote:
> netlink dump operations take module as parameter to hold
> reference for entire netlink dump duration.
> Currently it holds ref only on genl module which is not correct
> when we use ops registered to genl from another module.
> Following patch adds module pointer to genl_ops so that netlink
> can hold ref count on it.
> 
> CC: Jesse Gross <jesse@nicira.com>
> CC: Johannes Berg <johannes.berg@intel.com>
> Signed-off-by: Pravin B Shelar <pshelar@nicira.com>
> ---
>  include/net/genetlink.h |   21 ++++++++++++++++++---
>  net/netlink/genetlink.c |   39 ++++++++++++++++++++++++---------------
>  2 files changed, 42 insertions(+), 18 deletions(-)
> 
> diff --git a/include/net/genetlink.h b/include/net/genetlink.h
> index 93024a4..7f57b2c 100644
> --- a/include/net/genetlink.h
> +++ b/include/net/genetlink.h
> @@ -119,13 +119,28 @@ struct genl_ops {
>  					 struct netlink_callback *cb);
>  	int		       (*done)(struct netlink_callback *cb);
>  	struct list_head	ops_list;
> +	struct module		*module;
>  };

This may be more correct than my patch, but I'm not sure it's worth
spending the memory. Is there going to be any generic netlink family
that actually puts operations into a different module than the family
itself? I doubt that.

> @@ -605,14 +610,18 @@ static int genl_family_rcv_msg(struct genl_family *family,
>  			genl_unlock();
>  			c.data = ops;
>  			c.dump = genl_lock_dumpit;
> -			if (ops->done)
> -				c.done = genl_lock_done;
> +			c.done = genl_lock_done;
> +			c.module = THIS_MODULE;

THIS_MODULE here is useless, this code is always built-in.

> +			if (!try_module_get(ops->module))
> +				return -EPROTONOSUPPORT;

Why open-code it? You can just point c.module to the ops module here as
well (because generic netlink is built-in) and save yourself the
try_module_get stuff.

johannes

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pravin B Shelar Aug. 22, 2013, 5:44 p.m. UTC | #2
On Thu, Aug 22, 2013 at 12:40 AM, Johannes Berg
<johannes@sipsolutions.net> wrote:
> On Wed, 2013-08-21 at 20:58 -0700, Pravin B Shelar wrote:
>> netlink dump operations take module as parameter to hold
>> reference for entire netlink dump duration.
>> Currently it holds ref only on genl module which is not correct
>> when we use ops registered to genl from another module.
>> Following patch adds module pointer to genl_ops so that netlink
>> can hold ref count on it.
>>
>> CC: Jesse Gross <jesse@nicira.com>
>> CC: Johannes Berg <johannes.berg@intel.com>
>> Signed-off-by: Pravin B Shelar <pshelar@nicira.com>
>> ---
>>  include/net/genetlink.h |   21 ++++++++++++++++++---
>>  net/netlink/genetlink.c |   39 ++++++++++++++++++++++++---------------
>>  2 files changed, 42 insertions(+), 18 deletions(-)
>>
>> diff --git a/include/net/genetlink.h b/include/net/genetlink.h
>> index 93024a4..7f57b2c 100644
>> --- a/include/net/genetlink.h
>> +++ b/include/net/genetlink.h
>> @@ -119,13 +119,28 @@ struct genl_ops {
>>                                        struct netlink_callback *cb);
>>       int                    (*done)(struct netlink_callback *cb);
>>       struct list_head        ops_list;
>> +     struct module           *module;
>>  };
>
> This may be more correct than my patch, but I'm not sure it's worth
> spending the memory. Is there going to be any generic netlink family
> that actually puts operations into a different module than the family
> itself? I doubt that.
>
I tried to do same, but I do not have access to ops in
genl_lock_done() function. therefore I decided to store direct pointer
to module in ops struct.

>> @@ -605,14 +610,18 @@ static int genl_family_rcv_msg(struct genl_family *family,
>>                       genl_unlock();
>>                       c.data = ops;
>>                       c.dump = genl_lock_dumpit;
>> -                     if (ops->done)
>> -                             c.done = genl_lock_done;
>> +                     c.done = genl_lock_done;
>> +                     c.module = THIS_MODULE;
>
> THIS_MODULE here is useless, this code is always built-in.
>
>> +                     if (!try_module_get(ops->module))
>> +                             return -EPROTONOSUPPORT;
>
> Why open-code it? You can just point c.module to the ops module here as
> well (because generic netlink is built-in) and save yourself the
> try_module_get stuff.
>

In locked genl case genl-dump operation has call graph as follows:
netlink_dump() -> genl_lock_dumpit() -> ops->dump()
Therefore I need to take ref on genl module and ops->module.

In case of parallel_ops been true, genl_lock_dumpit() is not used so I
have directly set c.module to ops->module.

I know this code is bit complicated, but I could not find way to simplify it.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Johannes Berg Aug. 22, 2013, 5:49 p.m. UTC | #3
On Thu, 2013-08-22 at 10:44 -0700, Pravin Shelar wrote:

> > This may be more correct than my patch, but I'm not sure it's worth
> > spending the memory. Is there going to be any generic netlink family
> > that actually puts operations into a different module than the family
> > itself? I doubt that.
> >
> I tried to do same, but I do not have access to ops in
> genl_lock_done() function. therefore I decided to store direct pointer
> to module in ops struct.

You mean the family? I still don't like this, it'll be a massive
increase in memory for something like nl80211 that has a lot of
operations. In fact I think we should get rid of being allowed to
register single ops and just force a single array to remove the linked
list as well.

> >> +                     c.module = THIS_MODULE;
> >
> > THIS_MODULE here is useless, this code is always built-in.
> >
> >> +                     if (!try_module_get(ops->module))
> >> +                             return -EPROTONOSUPPORT;
> >
> > Why open-code it? You can just point c.module to the ops module here as
> > well (because generic netlink is built-in) and save yourself the
> > try_module_get stuff.
> >
> 
> In locked genl case genl-dump operation has call graph as follows:
> netlink_dump() -> genl_lock_dumpit() -> ops->dump()
> Therefore I need to take ref on genl module and ops->module.

There's no "genl module", generic netlink is always built in. That was
my point, you don't need to hold any reference to the "genl module"
since it doesn't exist, so you can just point to the ops (family)
module.

johannes

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pravin B Shelar Aug. 22, 2013, 6:04 p.m. UTC | #4
On Thu, Aug 22, 2013 at 10:49 AM, Johannes Berg
<johannes@sipsolutions.net> wrote:
> On Thu, 2013-08-22 at 10:44 -0700, Pravin Shelar wrote:
>
>> > This may be more correct than my patch, but I'm not sure it's worth
>> > spending the memory. Is there going to be any generic netlink family
>> > that actually puts operations into a different module than the family
>> > itself? I doubt that.
>> >
>> I tried to do same, but I do not have access to ops in
>> genl_lock_done() function. therefore I decided to store direct pointer
>> to module in ops struct.
>
> You mean the family? I still don't like this, it'll be a massive
> increase in memory for something like nl80211 that has a lot of
> operations. In fact I think we should get rid of being allowed to
> register single ops and just force a single array to remove the linked
> list as well.
>
Sorry, I meant I do not have access to genl-family in genl_lock_done().
I am using c.data to store ops pointer. Thats why module pointer was
store in ops.

But considering we do not need to hold ref on genl, I can move module
pointer to family.

>> >> +                     c.module = THIS_MODULE;
>> >
>> > THIS_MODULE here is useless, this code is always built-in.
>> >
>> >> +                     if (!try_module_get(ops->module))
>> >> +                             return -EPROTONOSUPPORT;
>> >
>> > Why open-code it? You can just point c.module to the ops module here as
>> > well (because generic netlink is built-in) and save yourself the
>> > try_module_get stuff.
>> >
>>
>> In locked genl case genl-dump operation has call graph as follows:
>> netlink_dump() -> genl_lock_dumpit() -> ops->dump()
>> Therefore I need to take ref on genl module and ops->module.
>
> There's no "genl module", generic netlink is always built in. That was
> my point, you don't need to hold any reference to the "genl module"
> since it doesn't exist, so you can just point to the ops (family)
> module.
>
ok, this make sense.
I will send updated patch.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/include/net/genetlink.h b/include/net/genetlink.h
index 93024a4..7f57b2c 100644
--- a/include/net/genetlink.h
+++ b/include/net/genetlink.h
@@ -119,13 +119,28 @@  struct genl_ops {
 					 struct netlink_callback *cb);
 	int		       (*done)(struct netlink_callback *cb);
 	struct list_head	ops_list;
+	struct module		*module;
 };
 
 extern int genl_register_family(struct genl_family *family);
-extern int genl_register_family_with_ops(struct genl_family *family,
-	struct genl_ops *ops, size_t n_ops);
+
+extern int __genl_register_family_with_ops(struct genl_family *family,
+	struct genl_ops *ops, size_t n_ops, struct module *module);
+
+static inline int genl_register_family_with_ops(struct genl_family *family,
+	struct genl_ops *ops, size_t n_ops)
+{
+	return __genl_register_family_with_ops(family, ops, n_ops, THIS_MODULE);
+}
+
 extern int genl_unregister_family(struct genl_family *family);
-extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
+extern int __genl_register_ops(struct genl_family *f, struct genl_ops *ops,
+			       struct module *module);
+static inline int genl_register_ops(struct genl_family *f, struct genl_ops *ops)
+{
+	return __genl_register_ops(f, ops, THIS_MODULE);
+}
+
 extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
 extern int genl_register_mc_group(struct genl_family *family,
 				  struct genl_multicast_group *grp);
diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c
index 3669039..23430c7 100644
--- a/net/netlink/genetlink.c
+++ b/net/netlink/genetlink.c
@@ -284,7 +284,7 @@  static void genl_unregister_mc_groups(struct genl_family *family)
 }
 
 /**
- * genl_register_ops - register generic netlink operations
+ * __genl_register_ops - register generic netlink operations
  * @family: generic netlink family
  * @ops: operations to be registered
  *
@@ -298,7 +298,8 @@  static void genl_unregister_mc_groups(struct genl_family *family)
  *
  * Returns 0 on success or a negative error code.
  */
-int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
+int __genl_register_ops(struct genl_family *family, struct genl_ops *ops,
+			struct module *module)
 {
 	int err = -EINVAL;
 
@@ -317,6 +318,7 @@  int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
 	if (ops->policy)
 		ops->flags |= GENL_CMD_CAP_HASPOL;
 
+	ops->module = module;
 	genl_lock_all();
 	list_add_tail(&ops->ops_list, &family->ops_list);
 	genl_unlock_all();
@@ -326,7 +328,7 @@  int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
 errout:
 	return err;
 }
-EXPORT_SYMBOL(genl_register_ops);
+EXPORT_SYMBOL(__genl_register_ops);
 
 /**
  * genl_unregister_ops - unregister generic netlink operations
@@ -433,7 +435,7 @@  errout:
 EXPORT_SYMBOL(genl_register_family);
 
 /**
- * genl_register_family_with_ops - register a generic netlink family
+ * __genl_register_family_with_ops - register a generic netlink family
  * @family: generic netlink family
  * @ops: operations to be registered
  * @n_ops: number of elements to register
@@ -457,8 +459,8 @@  EXPORT_SYMBOL(genl_register_family);
  *
  * Return 0 on success or a negative error code.
  */
-int genl_register_family_with_ops(struct genl_family *family,
-	struct genl_ops *ops, size_t n_ops)
+int __genl_register_family_with_ops(struct genl_family *family,
+	struct genl_ops *ops, size_t n_ops, struct module *module)
 {
 	int err, i;
 
@@ -467,7 +469,7 @@  int genl_register_family_with_ops(struct genl_family *family,
 		return err;
 
 	for (i = 0; i < n_ops; ++i, ++ops) {
-		err = genl_register_ops(family, ops);
+		err = __genl_register_ops(family, ops, module);
 		if (err)
 			goto err_out;
 	}
@@ -476,7 +478,7 @@  err_out:
 	genl_unregister_family(family);
 	return err;
 }
-EXPORT_SYMBOL(genl_register_family_with_ops);
+EXPORT_SYMBOL(__genl_register_family_with_ops);
 
 /**
  * genl_unregister_family - unregister generic netlink family
@@ -558,11 +560,14 @@  static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
 static int genl_lock_done(struct netlink_callback *cb)
 {
 	struct genl_ops *ops = cb->data;
-	int rc;
+	int rc = 0;
 
-	genl_lock();
-	rc = ops->done(cb);
-	genl_unlock();
+	if (ops->done) {
+		genl_lock();
+		rc = ops->done(cb);
+		genl_unlock();
+	}
+	module_put(ops->module);
 	return rc;
 }
 
@@ -605,14 +610,18 @@  static int genl_family_rcv_msg(struct genl_family *family,
 			genl_unlock();
 			c.data = ops;
 			c.dump = genl_lock_dumpit;
-			if (ops->done)
-				c.done = genl_lock_done;
+			c.done = genl_lock_done;
+			c.module = THIS_MODULE;
+
+			if (!try_module_get(ops->module))
+				return -EPROTONOSUPPORT;
 		} else {
 			c.dump = ops->dumpit;
 			c.done = ops->done;
+			c.module = ops->module;
 		}
 
-		rc = netlink_dump_start(net->genl_sock, skb, nlh, &c);
+		rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
 		if (!family->parallel_ops)
 			genl_lock();
 		return rc;