diff mbox series

[net-next] netlink: reduce NLA_POLICY_NESTED{,_ARRAY} arguments

Message ID 20190125090828.26361-1-johannes@sipsolutions.net (mailing list archive)
State Accepted
Delegated to: Johannes Berg
Headers show
Series [net-next] netlink: reduce NLA_POLICY_NESTED{,_ARRAY} arguments | expand

Commit Message

Johannes Berg Jan. 25, 2019, 9:08 a.m. UTC
From: Johannes Berg <johannes.berg@intel.com>

In typical cases, there's no need to pass both the maxattr
and the policy array pointer, as the maxattr should just be
ARRAY_SIZE(policy) - 1. Therefore, to be less error prone,
just remove the maxattr argument from the default macros
and deduce the size accordingly.

Leave the original macros with a leading underscore to use
here and in case somebody needs to pass a policy pointer
where the policy isn't declared in the same place and thus
ARRAY_SIZE() cannot be used.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
Dave, it looks like we're the only users of this right now,
so let me know if you'd prefer I take it through my tree.
Also, this conflicts with the fix I just made to the place
where I got it wrong, but obviously the resolution is to
just take the version without the maxattr argument.
---
 include/net/netlink.h  |  8 ++++++--
 net/wireless/nl80211.c | 15 +++++----------
 2 files changed, 11 insertions(+), 12 deletions(-)

Comments

David Miller Jan. 25, 2019, 7:04 p.m. UTC | #1
From: Johannes Berg <johannes@sipsolutions.net>
Date: Fri, 25 Jan 2019 10:08:28 +0100

> From: Johannes Berg <johannes.berg@intel.com>
> 
> In typical cases, there's no need to pass both the maxattr
> and the policy array pointer, as the maxattr should just be
> ARRAY_SIZE(policy) - 1. Therefore, to be less error prone,
> just remove the maxattr argument from the default macros
> and deduce the size accordingly.
> 
> Leave the original macros with a leading underscore to use
> here and in case somebody needs to pass a policy pointer
> where the policy isn't declared in the same place and thus
> ARRAY_SIZE() cannot be used.
> 
> Signed-off-by: Johannes Berg <johannes.berg@intel.com>
> ---
> Dave, it looks like we're the only users of this right now,
> so let me know if you'd prefer I take it through my tree.
> Also, this conflicts with the fix I just made to the place
> where I got it wrong, but obviously the resolution is to
> just take the version without the maxattr argument.

Please take it through your tree in that case.  It'll probably
be the more painless way to merge this.
Johannes Berg Jan. 25, 2019, 7:34 p.m. UTC | #2
On Fri, 2019-01-25 at 11:04 -0800, David Miller wrote:
> 
> > Dave, it looks like we're the only users of this right now,
> > so let me know if you'd prefer I take it through my tree.
> > Also, this conflicts with the fix I just made to the place
> > where I got it wrong, but obviously the resolution is to
> > just take the version without the maxattr argument.
> 
> Please take it through your tree in that case.  It'll probably
> be the more painless way to merge this.

Will do. The most painless would be if you'd merge net into net-next,
then I could pull net-next into mac80211-next and resolve the conflict
before I even apply the patch. In fact, I think either way I'll just
wait until you do that, I suppose you will eventually anyway?

johannes
David Miller Jan. 25, 2019, 7:36 p.m. UTC | #3
From: Johannes Berg <johannes@sipsolutions.net>
Date: Fri, 25 Jan 2019 20:34:57 +0100

> On Fri, 2019-01-25 at 11:04 -0800, David Miller wrote:
>> 
>> > Dave, it looks like we're the only users of this right now,
>> > so let me know if you'd prefer I take it through my tree.
>> > Also, this conflicts with the fix I just made to the place
>> > where I got it wrong, but obviously the resolution is to
>> > just take the version without the maxattr argument.
>> 
>> Please take it through your tree in that case.  It'll probably
>> be the more painless way to merge this.
> 
> Will do. The most painless would be if you'd merge net into net-next,
> then I could pull net-next into mac80211-next and resolve the conflict
> before I even apply the patch. In fact, I think either way I'll just
> wait until you do that, I suppose you will eventually anyway?

Yeah I will probably send Linus a pull request today which means the
net --> net-next merge should occur in the next 24 hours.
diff mbox series

Patch

diff --git a/include/net/netlink.h b/include/net/netlink.h
index 4c1e99303b5a..23f27b0b3cef 100644
--- a/include/net/netlink.h
+++ b/include/net/netlink.h
@@ -306,10 +306,14 @@  struct nla_policy {
 #define NLA_POLICY_ETH_ADDR		NLA_POLICY_EXACT_LEN(ETH_ALEN)
 #define NLA_POLICY_ETH_ADDR_COMPAT	NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
 
-#define NLA_POLICY_NESTED(maxattr, policy) \
+#define _NLA_POLICY_NESTED(maxattr, policy) \
 	{ .type = NLA_NESTED, .validation_data = policy, .len = maxattr }
-#define NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
+#define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
 	{ .type = NLA_NESTED_ARRAY, .validation_data = policy, .len = maxattr }
+#define NLA_POLICY_NESTED(policy) \
+	_NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)
+#define NLA_POLICY_NESTED_ARRAY(policy) \
+	_NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)
 
 #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
 #define NLA_ENSURE_INT_TYPE(tp)				\
diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c
index e5f9c9ceb6c9..dde03ec279e6 100644
--- a/net/wireless/nl80211.c
+++ b/net/wireless/nl80211.c
@@ -259,15 +259,13 @@  nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
 static const struct nla_policy
 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
 	[NL80211_PMSR_TYPE_FTM] =
-		NLA_POLICY_NESTED(NL80211_PMSR_FTM_REQ_ATTR_MAX,
-				  nl80211_pmsr_ftm_req_attr_policy),
+		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
 };
 
 static const struct nla_policy
 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
 	[NL80211_PMSR_REQ_ATTR_DATA] =
-		NLA_POLICY_NESTED(NL80211_PMSR_TYPE_MAX,
-				  nl80211_pmsr_req_data_policy),
+		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
 };
 
@@ -280,8 +278,7 @@  nl80211_psmr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
 	 */
 	[NL80211_PMSR_PEER_ATTR_CHAN] = { .type = NLA_NESTED },
 	[NL80211_PMSR_PEER_ATTR_REQ] =
-		NLA_POLICY_NESTED(NL80211_PMSR_REQ_ATTR_MAX,
-				  nl80211_pmsr_req_attr_policy),
+		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
 };
 
@@ -292,8 +289,7 @@  nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
 	[NL80211_PMSR_ATTR_PEERS] =
-		NLA_POLICY_NESTED_ARRAY(NL80211_PMSR_PEER_ATTR_MAX,
-					nl80211_psmr_peer_attr_policy),
+		NLA_POLICY_NESTED_ARRAY(nl80211_psmr_peer_attr_policy),
 };
 
 const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
@@ -555,8 +551,7 @@  const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
 	},
 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
 	[NL80211_ATTR_PEER_MEASUREMENTS] =
-		NLA_POLICY_NESTED(NL80211_PMSR_FTM_REQ_ATTR_MAX,
-				  nl80211_pmsr_attr_policy),
+		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
 };