Skip to contentSkip to conversation

batman-adv: genetlink: no longer support using static family IDs

PATCH Applied Sven Eckelmann 1 participant 6 messages Oct 29, 2016 – Nov 1, 2016 Batman advanced

lore
Share & tools
lore

Sven Eckelmann

author maintainer
#0 lore raw

PATCH 1/3 batman-adv: genetlink: no longer support using static family IDs

From: Johannes Berg <[email protected]>

Static family IDs have never really been used, the only use case was the workaround I introduced for those users that assumed their family ID was also their multicast group ID.

Additionally, because static family IDs would never be reserved by the generic netlink code, using a relatively low ID would only work for built-in families that can be registered immediately after generic netlink is started, which is basically only the control family (apart from the workaround code, which I also had to add code for so it would reserve those IDs)

Thus, anything other than GENL_ID_GENERATE is flawed and luckily not used except in the cases I mentioned. Move those workarounds into a few lines of code, and then get rid of GENL_ID_GENERATE entirely, making it more robust.

---
Patch already in net-next

 net/batman-adv/netlink.c | 1 -
 1 file changed, 1 deletion(-)
net/batman-adv/netlink.c +0 -1
index aee20a3..a951e02 100644
@@ -49,7 +49,6 @@
 #include "translation-table.h"
 
 struct genl_family batadv_netlink_family = {
-	.id = GENL_ID_GENERATE,
 	.hdrsize = 0,
 	.name = BATADV_NL_NAME,
 	.version = 1,
Signature · 1 line
2.10.1
author maintainer
#1 lore raw

PATCH 2/3 batman-adv: genetlink: statically initialize families

From: Johannes Berg <[email protected]>

Instead of providing macros/inline functions to initialize the families, make all users initialize them statically and get rid of the macros.

This reduces the kernel code size by about 1.6k on x86-64 (with allyesconfig).

---
Patch already in net-next

 compat-include/net/genetlink.h | 21 ++-------------------
 net/batman-adv/netlink.c       | 25 +++++++++++++++----------
 2 files changed, 17 insertions(+), 29 deletions(-)
compat-include/net/genetlink.h +2 -19
index 6a287e6..b057e6c 100644
@@ -96,11 +96,6 @@ batadv_genlmsg_multicast_netns(struct batadv_genl_family *family,
 #define genl_unregister_family(_family) \
 	genl_unregister_family(&(_family)->family)
 
-#define genl_register_family_with_ops_groups(family, ops, grps) \
-	batadv_genl_register_family_with_ops_grps((family), \
-						  (ops), ARRAY_SIZE(ops), \
-						  (grps), ARRAY_SIZE(grps))
-
 static inline int batadv_genl_register_family(struct genl_family *family)
 {
 	unsigned int i;
@@ -148,20 +143,8 @@ static inline int batadv_genl_register_family(struct genl_family *family)
 	return ret;
 }
 
-static inline int
-batadv_genl_register_family_with_ops_grps(struct genl_family *family,
-					  struct genl_ops *ops, size_t n_ops,
-					  struct genl_multicast_group *mcgrps,
-					  size_t n_mcgrps)
-{
-	family->ops = ops;
-	family->n_ops = n_ops;
-	family->mcgrps = mcgrps;
-	family->n_mcgrps = n_mcgrps;
-	family->module = THIS_MODULE;
-
-	return batadv_genl_register_family(family);
-}
+#define genl_register_family(family) \
+	batadv_genl_register_family((family))
 
 #define __genl_const
 
net/batman-adv/netlink.c +15 -10
index a951e02..5dfc5c4 100644
@@ -48,13 +48,7 @@
 #include "tp_meter.h"
 #include "translation-table.h"
 
-struct genl_family batadv_netlink_family = {
-	.hdrsize = 0,
-	.name = BATADV_NL_NAME,
-	.version = 1,
-	.maxattr = BATADV_ATTR_MAX,
-	.netnsok = true,
-};
+struct genl_family batadv_netlink_family;
 
 /* multicast groups */
 enum batadv_netlink_multicast_groups {
@@ -609,6 +603,19 @@ static const struct genl_ops batadv_netlink_ops[] = {
 
 };
 
+struct genl_family batadv_netlink_family = {
+	.hdrsize = 0,
+	.name = BATADV_NL_NAME,
+	.version = 1,
+	.maxattr = BATADV_ATTR_MAX,
+	.netnsok = true,
+	.module = THIS_MODULE,
+	.ops = batadv_netlink_ops,
+	.n_ops = ARRAY_SIZE(batadv_netlink_ops),
+	.mcgrps = batadv_netlink_mcgrps,
+	.n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps),
+};
+
 /**
  * batadv_netlink_register - register batadv genl netlink family
  */
@@ -616,9 +623,7 @@ void __init batadv_netlink_register(void)
 {
 	int ret;
 
-	ret = genl_register_family_with_ops_groups(&batadv_netlink_family,
-						   batadv_netlink_ops,
-						   batadv_netlink_mcgrps);
+	ret = genl_register_family(&batadv_netlink_family);
 	if (ret)
 		pr_warn("unable to register netlink family");
 }
Signature · 1 line
2.10.1
author maintainer
#3 lore raw

PATCH 3/3 batman-adv: genetlink: mark families as __ro_after_init

In reply to Sven Eckelmann

From: Johannes Berg <[email protected]>

Now genl_register_family() is the only thing (other than the users themselves, perhaps, but I didn't find any doing that) writing to the family struct.

In all families that I found, genl_register_family() is only called from __init functions (some indirectly, in which case I've add __init annotations to clarifly things), so all can actually be marked __ro_after_init.

This protects the data structure from accidental corruption.

---
Patch already in net-next

 compat-include/linux/cache.h | 35 +++++++++++++++++++++++++++++++++++
 net/batman-adv/netlink.c     |  2 +-
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 compat-include/linux/cache.h
compat-include/linux/cache.h new file +35 -0
new file mode 100644
index 0000000..93dff58
@@ -0,0 +1,35 @@
+/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
+ *
+ * Marek Lindner, Simon Wunderlich
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * This file contains macros for maintaining compatibility with older versions
+ * of the Linux kernel.
+ */
+
+#ifndef _NET_BATMAN_ADV_COMPAT_LINUX_CACHE_H
+#define _NET_BATMAN_ADV_COMPAT_LINUX_CACHE_H
+
+#include <linux/version.h>
+#include_next <linux/cache.h>
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
+
+#ifndef __ro_after_init
+#define __ro_after_init
+#endif
+
+#endif /* < KERNEL_VERSION(4, 6, 0) */
+
+#endif /* _NET_BATMAN_ADV_COMPAT_LINUX_CACHE_H */
net/batman-adv/netlink.c +1 -1
index 5dfc5c4..5d38c77 100644
@@ -603,7 +603,7 @@ static const struct genl_ops batadv_netlink_ops[] = {
 
 };
 
-struct genl_family batadv_netlink_family = {
+struct genl_family batadv_netlink_family __ro_after_init = {
 	.hdrsize = 0,
 	.name = BATADV_NL_NAME,
 	.version = 1,
Signature · 1 line
2.10.1
author maintainer Applied
#5 lore raw
In reply to Sven Eckelmann
Quoted 20 lines · On Samstag, 29. Oktober 2016 10:03:26 CET Sven Eckelmann wrote:

From: Johannes Berg <[email protected]>

Static family IDs have never really been used, the only use case was the workaround I introduced for those users that assumed their family ID was also their multicast group ID.

Additionally, because static family IDs would never be reserved by the generic netlink code, using a relatively low ID would only work for built-in families that can be registered immediately after generic netlink is started, which is basically only the control family (apart from the workaround code, which I also had to add code for so it would reserve those IDs)

Thus, anything other than GENL_ID_GENERATE is flawed and luckily not used except in the cases I mentioned. Move those workarounds into a few lines of code, and then get rid of GENL_ID_GENERATE entirely, making it more robust.

Quoted 8 lines

---
Patch already in net-next

 net/batman-adv/netlink.c | 1 -
 1 file changed, 1 deletion(-)

Applied in 83e3cb32617d07a33fb43faa0957b91b3ac622c5 [1].

Kind regards,
Sven

[1] https://git.open-mesh.org/batman-adv.git/commit/83e3cb32617d07a33fb43faa0957b91b3ac622c5

author maintainer Applied
#2 lore raw

Re: [B.A.T.M.A.N.] [2/3] batman-adv: genetlink: statically initialize families

In reply to Sven Eckelmann
Quoted 13 lines · On Samstag, 29. Oktober 2016 10:03:27 CET Sven Eckelmann wrote:

From: Johannes Berg <[email protected]>

Instead of providing macros/inline functions to initialize the families, make all users initialize them statically and get rid of the macros.

This reduces the kernel code size by about 1.6k on x86-64 (with allyesconfig).

Quoted 6 lines

---
Patch already in net-next

 compat-include/net/genetlink.h | 21 ++-------------------
 net/batman-adv/netlink.c       | 25 +++++++++++++++----------
 2 files changed, 17 insertions(+), 29 deletions(-)

Applied in f0f80621705b629aa04ca90411633b9b3dd3c61f [1].

Kind regards,
Sven

[1] https://git.open-mesh.org/batman-adv.git/commit/f0f80621705b629aa04ca90411633b9b3dd3c61f

author maintainer Applied
#4 lore raw

Re: [B.A.T.M.A.N.] [3/3] batman-adv: genetlink: mark families as __ro_after_init

In reply to Sven Eckelmann
Quoted 17 lines · On Samstag, 29. Oktober 2016 10:03:28 CET Sven Eckelmann wrote:

From: Johannes Berg <[email protected]>

Now genl_register_family() is the only thing (other than the users themselves, perhaps, but I didn't find any doing that) writing to the family struct.

In all families that I found, genl_register_family() is only called from __init functions (some indirectly, in which case I've add __init annotations to clarifly things), so all can actually be marked __ro_after_init.

This protects the data structure from accidental corruption.

Quoted 7 lines

---
Patch already in net-next

 compat-include/linux/cache.h | 35 +++++++++++++++++++++++++++++++++++
 net/batman-adv/netlink.c     |  2 +-
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 compat-include/linux/cache.h

Applied in c776b4a071d82f8ad0e35d1b9472758ecf69a75e [1].

Kind regards,
Sven

[1] https://git.open-mesh.org/batman-adv.git/commit/c776b4a071d82f8ad0e35d1b9472758ecf69a75e