Skip to contentSkip to conversation

net: add __netdev_alloc_pcpu_stats() to indicate gfp flags

PATCH In review Pablo Neira Ayuso 3 participants 10 messages Nov 10, 2015

Subsystems (3)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 net: add __netdev_alloc_pcpu_stats() to indicate gfp flags

nf_tables may create percpu counters from the packet path through its dynamic set instantiation infrastructure, so we need a way to allocate this through GFP_ATOMIC.

---
 include/linux/netdevice.h | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
include/linux/netdevice.h +15 -12
index 2c00772..e9d0c8a 100644
@@ -2068,20 +2068,23 @@ struct pcpu_sw_netstats {
 	struct u64_stats_sync   syncp;
 };
 
-#define netdev_alloc_pcpu_stats(type)				\
-({								\
-	typeof(type) __percpu *pcpu_stats = alloc_percpu(type); \
-	if (pcpu_stats)	{					\
-		int __cpu;					\
-		for_each_possible_cpu(__cpu) {			\
-			typeof(type) *stat;			\
-			stat = per_cpu_ptr(pcpu_stats, __cpu);	\
-			u64_stats_init(&stat->syncp);		\
-		}						\
-	}							\
-	pcpu_stats;						\
+#define __netdev_alloc_pcpu_stats(type, gfp)				\
+({									\
+	typeof(type) __percpu *pcpu_stats = alloc_percpu_gfp(type, gfp);\
+	if (pcpu_stats)	{						\
+		int __cpu;						\
+		for_each_possible_cpu(__cpu) {				\
+			typeof(type) *stat;				\
+			stat = per_cpu_ptr(pcpu_stats, __cpu);		\
+			u64_stats_init(&stat->syncp);			\
+		}							\
+	}								\
+	pcpu_stats;							\
 })
 
+#define netdev_alloc_pcpu_stats(type)					\
+	__netdev_alloc_pcpu_stats(type, GFP_KERNEL);
+
 #include <linux/notifier.h>
 
 /* netdevice notifier chain. Please remember to update the rtnetlink
Signature · 1 line
2.1.4
author maintainer
#1 lore raw

PATCH 1/1 netfilter: Fix removal of GRE expectation entries created by PPTP

From: Anthony Lineham <[email protected]>

The uninitialized tuple structure caused incorrect hash calculation and the lookup failed.

---
Original patch posted on kernel bugzilla.

 net/ipv4/netfilter/nf_nat_pptp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/netfilter/nf_nat_pptp.c +1 -1
index 657d230..b3ca21b 100644
@@ -45,7 +45,7 @@ static void pptp_nat_expected(struct nf_conn *ct,
 	struct net *net = nf_ct_net(ct);
 	const struct nf_conn *master = ct->master;
 	struct nf_conntrack_expect *other_exp;
-	struct nf_conntrack_tuple t;
+	struct nf_conntrack_tuple t = {};
 	const struct nf_ct_pptp_master *ct_pptp_info;
 	const struct nf_nat_pptp *nat_pptp_info;
 	struct nf_nat_range range;
Signature · 1 line
2.1.4
author maintainer
#2 lore raw

Re: [PATCH] netfilter: Fix removal of GRE expectation entries created by PPTP

Quoted 4 lines · Pablo Neira Ayuso

From: Anthony Lineham <[email protected]>

The uninitialized tuple structure caused incorrect hash calculation and the lookup failed.

Please, ignore this.

This patch is already in the nf tree, it just slipped through in my last git send-email.

author maintainer
#3 lore raw

PATCH 2/2 netfilter: nf_tables: add clone interface to expression operations

In reply to Pablo Neira Ayuso

With the conversion of the counter expressions to make it percpu, we need to clone the percpu memory area, otherwise we crash when using counters from flow tables.

---
 include/net/netfilter/nf_tables.h | 16 +++++++++++--
 net/netfilter/nft_counter.c       | 49 ++++++++++++++++++++++++++++++++-------
 net/netfilter/nft_dynset.c        |  5 ++--
 3 files changed, 58 insertions(+), 12 deletions(-)
include/net/netfilter/nf_tables.h +14 -2
index c9149cc..c186457 100644
@@ -630,6 +630,8 @@ struct nft_expr_ops {
 	int				(*validate)(const struct nft_ctx *ctx,
 						    const struct nft_expr *expr,
 						    const struct nft_data **data);
+	int				(*clone)(struct nft_expr *dst,
+						 const struct nft_expr *src);
 	const struct nft_expr_type	*type;
 	void				*data;
 };
@@ -660,10 +662,20 @@ void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
 		  const struct nft_expr *expr);
 
-static inline void nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
+static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
 {
+	int err;
+
 	__module_get(src->ops->type->owner);
-	memcpy(dst, src, src->ops->size);
+	if (src->ops->clone) {
+		memcpy(dst, src, sizeof(*src));
+		err = src->ops->clone(dst, src);
+		if (err < 0)
+			return err;
+	} else {
+		memcpy(dst, src, src->ops->size);
+	}
+	return 0;
 }
 
 /**
net/netfilter/nft_counter.c +41 -8

80 lines not on this page · raw message

net/netfilter/nft_dynset.c +3 -2
index 513a8ef..9dec3bd 100644
@@ -50,8 +50,9 @@ static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
 	}
 
 	ext = nft_set_elem_ext(set, elem);
-	if (priv->expr != NULL)
-		nft_expr_clone(nft_set_ext_expr(ext), priv->expr);
+	if (priv->expr != NULL &&
+	    nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
+		return NULL;
 
 	return elem;
 }
Signature · 1 line
2.1.4
#4 lore raw

Re: [PATCH nf 2/2] netfilter: nf_tables: add clone interface to expression operations

Quoted 16 lines · Pablo Neira Ayuso

With the conversion of the counter expressions to make it percpu, we need to clone the percpu memory area, otherwise we crash when using counters from flow tables.

---
 include/net/netfilter/nf_tables.h | 16 +++++++++++--
 net/netfilter/nft_counter.c       | 49 ++++++++++++++++++++++++++++++++-------
 net/netfilter/nft_dynset.c        |  5 ++--
 3 files changed, 58 insertions(+), 12 deletions(-)
include/net/netfilter/nf_tables.h +0 -0
index c9149cc..c186457 100644

include/net/netfilter/nf_tables.h struct nft_expr_ops { · line 634

Quoted 5 lines
include/net/netfilter/nf_tables.h +2 -0
@@ -630,6 +630,8 @@ struct nft_expr_ops {
 	int				(*validate)(const struct nft_ctx *ctx,
 						    const struct nft_expr *expr,
 						    const struct nft_data **data);
+	int				(*clone)(struct nft_expr *dst,
+						 const struct nft_expr *src);

The functions and data needed during runtime are deliberately kept together at the beginning of the structure to avoid having to read the entire thing. So I'd say this shoud go after ->eval().

include/net/netfilter/nf_tables.h void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); · line 671

Quoted 13 lines
include/net/netfilter/nf_tables.h +5 -2
@@ -660,10 +662,20 @@ void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
 		  const struct nft_expr *expr);
 
-static inline void nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
+static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
 {
+	int err;
+
 	__module_get(src->ops->type->owner);
-	memcpy(dst, src, src->ops->size);
+	if (src->ops->clone) {
+		memcpy(dst, src, sizeof(*src));

Why copy if we clone? The function should do a full initialization if it is present I would say.

Quoted 9 lines
include/net/netfilter/nf_tables.h +7 -0
+		err = src->ops->clone(dst, src);
+		if (err < 0)
+			return err;
+	} else {
+		memcpy(dst, src, src->ops->size);
+	}
+	return 0;
 }
 
author maintainer
#5 lore raw

Re: [PATCH nf 2/2] netfilter: nf_tables: add clone interface to expression operations

Quoted 12 lines · Patrick McHardy
Quoted 11 lines · Pablo Neira Ayuso

With the conversion of the counter expressions to make it percpu, we need to clone the percpu memory area, otherwise we crash when using counters from flow tables.

---
 include/net/netfilter/nf_tables.h | 16 +++++++++++--
 net/netfilter/nft_counter.c       | 49 ++++++++++++++++++++++++++++++++-------
 net/netfilter/nft_dynset.c        |  5 ++--
 3 files changed, 58 insertions(+), 12 deletions(-)
Quoted 14 lines

include/net/netfilter/nf_tables.h struct nft_expr_ops { · line 634

Quoted 10 lines
include/net/netfilter/nf_tables.h +2 -0
index c9149cc..c186457 100644
@@ -630,6 +630,8 @@ struct nft_expr_ops {
 	int				(*validate)(const struct nft_ctx *ctx,
 						    const struct nft_expr *expr,
 						    const struct nft_data **data);
+	int				(*clone)(struct nft_expr *dst,
+						 const struct nft_expr *src);

The functions and data needed during runtime are deliberately kept together at the beginning of the structure to avoid having to read the entire thing. So I'd say this shoud go after ->eval().

OK, I'll place this after ->eval.

Quoted 4 lines
Quoted 4 lines
include/net/netfilter/nf_tables.h +0 -0
@@ -660,10 +662,20 @@ void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
 		  const struct nft_expr *expr);
 
Quoted 12 lines

include/net/netfilter/nf_tables.h void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); · line 671

Quoted 9 lines
include/net/netfilter/nf_tables.h +5 -2
-static inline void nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
+static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
 {
+	int err;
+
 	__module_get(src->ops->type->owner);
-	memcpy(dst, src, src->ops->size);
+	if (src->ops->clone) {
+		memcpy(dst, src, sizeof(*src));

Why copy if we clone? The function should do a full initialization if it is present I would say.

This is not copying the variable length data area of the expression, just the expression head.

Quoted 9 lines
Quoted 9 lines
include/net/netfilter/nf_tables.h +7 -0
+		err = src->ops->clone(dst, src);
+		if (err < 0)
+			return err;
+	} else {
+		memcpy(dst, src, src->ops->size);
+	}
+	return 0;
 }
 
#6 lore raw

Re: [PATCH nf 2/2] netfilter: nf_tables: add clone interface to expression operations

Quoted 11 lines · Pablo Neira Ayuso
Quoted 7 lines · Patrick McHardy

include/net/netfilter/nf_tables.h void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); · line 671

Quoted 4 lines
include/net/netfilter/nf_tables.h +2 -1
 	__module_get(src->ops->type->owner);
-	memcpy(dst, src, src->ops->size);
+	if (src->ops->clone) {
+		memcpy(dst, src, sizeof(*src));

Why copy if we clone? The function should do a full initialization if it is present I would say.

This is not copying the variable length data area of the expression, just the expression head.

Ah right. But that is only ->ops. We can set this directly, should generate better code and be easier to understand.

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
include/net/netfilter/nf_tables.h +7 -0
+		err = src->ops->clone(dst, src);
+		if (err < 0)
+			return err;
+	} else {
+		memcpy(dst, src, src->ops->size);
+	}
+	return 0;
 }
author maintainer
#7 lore raw

Re: [PATCH nf 2/2] netfilter: nf_tables: add clone interface to expression operations

Quoted 10 lines · Patrick McHardy
Quoted 9 lines · Pablo Neira Ayuso
Quoted 7 lines · Patrick McHardy

include/net/netfilter/nf_tables.h void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); · line 671

Quoted 4 lines
include/net/netfilter/nf_tables.h +2 -1
 	__module_get(src->ops->type->owner);
-	memcpy(dst, src, src->ops->size);
+	if (src->ops->clone) {
+		memcpy(dst, src, sizeof(*src));

Why copy if we clone? The function should do a full initialization if it is present I would say.

Quoted 5 lines
Quoted 2 lines

This is not copying the variable length data area of the expression, just the expression head.

Ah right. But that is only ->ops. We can set this directly, should generate better code and be easier to understand.

I left the memcpy just to avoid that we forget in case we ever get more data there (unlikely). So I'll set the pointer instead.

If no further objections, will make those two changes locally and will push this upstream.

Thanks!

#8 lore raw

Re: [PATCH nf 2/2] netfilter: nf_tables: add clone interface to expression operations

Quoted 14 lines · Pablo Neira Ayuso
Quoted 13 lines · Patrick McHardy
Quoted 11 lines · Pablo Neira Ayuso
Quoted 7 lines · Patrick McHardy

include/net/netfilter/nf_tables.h void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); · line 671

Quoted 4 lines
include/net/netfilter/nf_tables.h +2 -1
 	__module_get(src->ops->type->owner);
-	memcpy(dst, src, src->ops->size);
+	if (src->ops->clone) {
+		memcpy(dst, src, sizeof(*src));

Why copy if we clone? The function should do a full initialization if it is present I would say.

This is not copying the variable length data area of the expression, just the expression head.

Quoted 8 lines
Quoted 2 lines

Ah right. But that is only ->ops. We can set this directly, should generate better code and be easier to understand.

I left the memcpy just to avoid that we forget in case we ever get more data there (unlikely). So I'll set the pointer instead.

If no further objections, will make those two changes locally and will push this upstream.

No further objections :)