Skip to contentSkip to conversation

NETDEV: Find network bugs by validating the sk_buff state

RFC PATCH In review David VomLehn 2 participants 11 messages Apr 29, 2009 – Apr 30, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 NETDEV: Find network bugs by validating the sk_buff state

It can be very difficult to find the cause of networking driver bugs due to doing the wrong thing with an sk_buff, such as freeing or queuing them twice. This can lead to varied errors that don't show up until much later and cause nasty things like memory corruption. The delayed effects can make it very difficult to locate the cause of the errors. This patch adds code that checks the state of sk_buffs at a few critical points, a strategy which is lightweight and appears very effective. It also records and prints information that should help track down who did what to the buffer, making it easier to determine exactly which code has a problem.

This code has low enough overhead to enable on production systems, which is how we deploy it. It is a kernel debugging tool, however, and the default is to leave it off.

Though already working, this is still a work in progress and I know of some changes I want to make. It has twice found problems within an hour that networking device driver developers had been trying to find for days/weeks, so it might help someone fighting such problems right now.

---
 include/linux/skbuff.h |  186 +++++++++++++++++++++++++++++++++++++++++++++++-
 lib/Kconfig.debug      |   12 +++
 net/core/skbuff.c      |  162 ++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 352 insertions(+), 8 deletions(-)
include/linux/skbuff.h +184 -2
index 5fd3891..ea2fd11 100644
@@ -44,6 +44,15 @@
 #define SKB_MAX_HEAD(X)		(SKB_MAX_ORDER((X), 0))
 #define SKB_MAX_ALLOC		(SKB_MAX_ORDER(0, 2))
 
+/*
+ * Define the best-guess way to get the address of the caller.
+ */
+#ifdef	return_address
+#define	skb_return_address()	return_address()
+#else
+#define skb_return_address()	__builtin_return_address(0)
+#endif
+
 /* A. Checksumming of received packets by device.
  *
  *	NONE: device failed to checksum this packet.
@@ -251,6 +260,29 @@ typedef unsigned int sk_buff_data_t;
 typedef unsigned char *sk_buff_data_t;
 #endif
 
+/* States for a struct sk_buff */
+enum skb_check {
+	SKB_CHECK_INVALID,	/* Zero is a common corruption value, so make
+				 * an invalid value */
+	SKB_CHECK_FREE,		/* sk_buff is not allocated */
+	SKB_CHECK_ALLOCATED,	/* In used, but not queued */
+	SKB_CHECK_QUEUED,	/* On a queue */
+};
+
+/*
+ * Convert an skb_check to the corresponding mask
+ * @state:	The skb_check value to convert
+ * Returns the mask
+ */
+static inline unsigned skb_check2mask(enum skb_check state)
+{
+	return 1u << state;
+}
+
+#define SKB_CHECK_FREE_MASK		skb_check2mask(SKB_CHECK_FREE)
+#define SKB_CHECK_ALLOCATED_MASK	skb_check2mask(SKB_CHECK_ALLOCATED)
+#define SKB_CHECK_QUEUED_MASK		skb_check2mask(SKB_CHECK_QUEUED)
+
 /** 
  *	struct sk_buff - socket buffer
  *	@next: Next buffer in list
@@ -308,6 +340,10 @@ typedef unsigned char *sk_buff_data_t;
  *		done by skb DMA functions
  *	@secmark: security marking
  *	@vlan_tci: vlan tag control information
+ *	@magic: Magic number for sk_buff, should be SKB_CHECK_MAGIC
+ *	@check: State for consistency checking
+ *	@last_alloc: Address of caller who last allocated this sk_buff
+ *	@last_free: Address of caller who last freed this sk_buff
  */
 
 struct sk_buff {
@@ -398,6 +434,13 @@ struct sk_buff {
 	sk_buff_data_t		transport_header;
 	sk_buff_data_t		network_header;
 	sk_buff_data_t		mac_header;
+#ifdef	CONFIG_SKB_VALIDATE_STATE
+#define	SKB_CHECK_MAGIC		0xab1ec0de
+	__u32			magic;
+	enum skb_check		check;
+	void			*last_alloc;
+	void			*last_free;
+#endif
 	/* These elements must be at the end, see alloc_skb() for details.  */
 	sk_buff_data_t		tail;
 	sk_buff_data_t		end;
@@ -423,6 +466,123 @@ extern void skb_dma_unmap(struct device *dev, struct sk_buff *skb,
 			  enum dma_data_direction dir);
 #endif
 
+#ifdef	CONFIG_SKB_VALIDATE_STATE
+
+extern void skb_report_invalid(const struct sk_buff *skb);
+
+/**
+* Verify that the state of the sk_buff is as expected
+* @skb:	Pointer to the &struct sk_buff to validate
+* @expected:	Mask of valid states
+*/
+static inline void skb_check(const struct sk_buff *skb,
+	 unsigned expected)
+{
+	if (unlikely(skb->magic != SKB_CHECK_MAGIC ||
+		(skb_check2mask(skb->check) & expected) == 0))
+		skb_report_invalid(skb);
+}
+
+/**
+* Set the validate state of the &struct sk_buff to the given value
+* @skb:	Pointer to the &struct sk_buff whose state is to be set
+* @state:	Value to which the state should be set
+* Note that this requires that the magic number be set before setting the
+* state.
+*/
+static inline void skb_set_check(struct sk_buff *skb,
+	enum skb_check state)
+{
+	if (unlikely(skb->magic != SKB_CHECK_MAGIC))
+		skb_report_invalid(skb);
+	skb->check = state;
+}
+
+/**
+* Validate the current sk_buff state and set to a new value
+* @skb:	Pointer to the &sk_buff whose state is to be validated and set
+* @expected:	Expected state of the &sk_buff
+* @new:	New status of the &sk_buff
+*/
+static inline void skb_check_and_set(struct sk_buff *skb,
+	unsigned expected, enum skb_check new)
+{
+	skb_check(skb, expected);
+	skb_set_check(skb, new);
+}
+
+/**
+* Set the last_free element of the sk_buff
+* @skb:	Pointer to the &sk_buff whose last_free element is to be set
+* @last_free:	Address in the function that wanted to free the &sk_buff
+*/
+static inline void skb_set_last_free(struct sk_buff *skb, void *last_free)
+{
+	skb->last_free = last_free;
+}
+
+/**
+* Set the last_alloc element of the sk_buff
+* @skb:	Pointer to the &sk_buff whose last_alloc element is to be set
+* @last_alloc:	Address in the function that wanted to allocate an &sk_buff
+*/
+static inline void skb_set_last_alloc(struct sk_buff *skb, void *last_alloc)
+{
+	skb->last_alloc = last_alloc;
+}
+
+/**
+* Sets sk_buff up for state validation
+* @skb:	Pointer to the &sk_buff to be set
+*/
+static inline void skb_check_setup(struct sk_buff *skb)
+{
+	skb->magic = SKB_CHECK_MAGIC;
+	skb_set_check(skb, SKB_CHECK_ALLOCATED);
+}
+
+/**
+* Cleans state validation when the sk_buff is to be freed
+* @skb:	Poitner to the &sk_buff being freed
+*/
+static inline void skb_check_done(struct sk_buff *skb)
+{
+	skb_set_check(skb, SKB_CHECK_FREE);
+	skb->magic = 0;
+}
+#else
+static inline void skb_check(const struct sk_buff *skb,
+	unsigned expected)
+{
+}
+
+static inline void skb_set_check(struct sk_buff *skb,
+	enum skb_check state)
+{
+}
+
+static inline void skb_check_and_set(struct sk_buff *skb,
+	unsigned expected, enum skb_check new)
+{
+}
+
+static inline void skb_set_last_free(struct sk_buff *skb, void *last_free)
+{
+}
+
+static inline void skb_set_last_alloc(struct sk_buff *skb, void *last_alloc)
+{
+}
+
+static inline void skb_check_setup(struct sk_buff *skb)
+{
+}
+
+static inline void skb_check_done(struct sk_buff *skb)
+{
+}
+#endif
+
 extern void kfree_skb(struct sk_buff *skb);
 extern void consume_skb(struct sk_buff *skb);
 extern void	       __kfree_skb(struct sk_buff *skb);
@@ -431,13 +591,27 @@ extern struct sk_buff *__alloc_skb(unsigned int size,
 static inline struct sk_buff *alloc_skb(unsigned int size,
 					gfp_t priority)
 {
-	return __alloc_skb(size, priority, 0, -1);
+	struct sk_buff *res;
+
+	res = __alloc_skb(size, priority, 0, -1);
+
+	if (res)
+		skb_set_last_alloc(res, skb_return_address());
+
+	return res;
 }
 
 static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
 					       gfp_t priority)
 {
-	return __alloc_skb(size, priority, 1, -1);
+	struct sk_buff *res;
+
+	res = __alloc_skb(size, priority, 1, -1);
+
+	if (res)
+		skb_set_last_alloc(res, skb_return_address());
+
+	return res;
 }
 
 extern int skb_recycle_check(struct sk_buff *skb, int skb_size);
@@ -748,6 +922,8 @@ static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
 	struct sk_buff *list = ((struct sk_buff *)list_)->next;
 	if (list == (struct sk_buff *)list_)
 		list = NULL;
+	else
+		skb_check(list, SKB_CHECK_QUEUED_MASK);
 	return list;
 }
 
@@ -769,6 +945,8 @@ static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
 	struct sk_buff *list = ((struct sk_buff *)list_)->prev;
 	if (list == (struct sk_buff *)list_)
 		list = NULL;
+	else
+		skb_check(list, SKB_CHECK_QUEUED_MASK);
 	return list;
 }
 
@@ -831,6 +1009,8 @@ static inline void __skb_insert(struct sk_buff *newsk,
 				struct sk_buff *prev, struct sk_buff *next,
 				struct sk_buff_head *list)
 {
+	skb_check_and_set(newsk, SKB_CHECK_ALLOCATED_MASK,
+		SKB_CHECK_QUEUED);
 	newsk->next = next;
 	newsk->prev = prev;
 	next->prev  = prev->next = newsk;
@@ -985,6 +1165,8 @@ static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
 {
 	struct sk_buff *next, *prev;
 
+	skb_check_and_set(skb, SKB_CHECK_QUEUED_MASK,
+		SKB_CHECK_ALLOCATED);
 	list->qlen--;
 	next	   = skb->next;
 	prev	   = skb->prev;
lib/Kconfig.debug +12 -0
index c6e854f..cffc74a 100644
@@ -298,6 +298,18 @@ config DEBUG_OBJECTS_ENABLE_DEFAULT
         help
           Debug objects boot parameter default value
 
+config SKB_VALIDATE_STATE
+	bool "Validate state of networking buffers (struct sk_buff)"
+	depends on DEBUG_KERNEL && NET
+	default n
+	help
+	  Simple and quick checks to validate that sk_buffs are in the right
+	  state. Intended to catch errors, including, but not limited to,
+	  the following:
+	  - Double-freeing sk_buffs
+	  - Freeing sk_buffs that are still on a queue
+	  - Queuing sk_buffs that aren't allocated or that are already queued
+
 config DEBUG_SLAB
 	bool "Debug slab memory allocations"
 	depends on DEBUG_KERNEL && SLAB
net/core/skbuff.c +156 -6

282 lines not on this page · raw message

maintainer
#1 lore raw

Sorry, for this to be useful distributions are going to turn it on by default, and adding 24 bytes to struct sk_buff on 64-bit when we're trying everything in our power to make it smaller rather than larger is not acceptable.

It's a useful change, something to keep in one's back pocket when debugging something, but not for upstream, sorry.

#2 lore raw
Quoted 5 lines · David Miller

Sorry, for this to be useful distributions are going to turn it on by default, and adding 24 bytes to struct sk_buff on 64-bit when we're trying everything in our power to make it smaller rather than larger is not acceptable.

That's like saying that any of the kernel debugging options have to be on by default. I expect that most folks won't want this on unless they are looking for a bug.

Quoted 2 lines

It's a useful change, something to keep in one's back pocket when debugging something, but not for upstream, sorry.

Right. This is why it depends on KERNEL_DEBUG and why it is off by default.

David VomLehn

maintainer
#3 lore raw

From: David VomLehn <[email protected]>
Date: Wed, 29 Apr 2009 08:14:40 -0700

Quoted 10 lines · David Miller
Quoted 5 lines

Sorry, for this to be useful distributions are going to turn it on by default, and adding 24 bytes to struct sk_buff on 64-bit when we're trying everything in our power to make it smaller rather than larger is not acceptable.

That's like saying that any of the kernel debugging options have to be on by default. I expect that most folks won't want this on unless they are looking for a bug.

We don't have networking buffer structure elements that implement debugging, and it's for a reason.

Quoted 4 lines
Quoted 2 lines

It's a useful change, something to keep in one's back pocket when debugging something, but not for upstream, sorry.

Right. This is why it depends on KERNEL_DEBUG and why it is off by default.

All of this is moot if distributions decide to start turning this on, and from my experience they will.

#4 lore raw
Quoted 10 lines · David Miller

From: David VomLehn <[email protected]>
Date: Wed, 29 Apr 2009 08:14:40 -0700

Quoted 7 lines · David Miller
Quoted 5 lines

Sorry, for this to be useful distributions are going to turn it on by default, and adding 24 bytes to struct sk_buff on 64-bit when we're trying everything in our power to make it smaller rather than larger is not acceptable.

Quoted 6 lines
Quoted 3 lines

That's like saying that any of the kernel debugging options have to be on by default. I expect that most folks won't want this on unless they are looking for a bug.

We don't have networking buffer structure elements that implement debugging, and it's for a reason.

Can I assume this is due to the concern, stated below, that distributions will start turning it on?

Quoted 4 lines
Quoted 1 line

Right. This is why it depends on KERNEL_DEBUG and why it is off by default.

All of this is moot if distributions decide to start turning this on, and from my experience they will.

This puzzles me.

It will cost performance and memory if distros turn this on, and they aren't idiots. How can we know enough to determine for all distributions whether the paypack is enough to permanently enable a particular kernel debugging feature?

Still, let us back up to technical specifics. There is a need for kernel debugging features and experience shows that the networking area is no exception. So:

What criteria must network debugging features meet for kernel inclusion, considered first as a kernel debugging feature, and second, as a non-optional consistency check?

Memory and performance are obvious choices for criteria, what would the threshholds be here? Are there other criteria?

Note that I'm happy to consider making it *more* expensive to use a feature if the case can be made that:
1. The feature must not be used in production
2. The feature becomes expensive enough to prevent its use in production

David VomLehn

maintainer
#5 lore raw

From: David VomLehn <[email protected]>
Date: Wed, 29 Apr 2009 15:28:09 -0700

Quoted 1 line

Still, let us back up to technical specifics.

The specifics are that if you showed me something that added a "u16", at most, I'd go to bat for you and support your work going upstream.

We could find a way to squeeze that into the existing structure with little or even no size increase.

#6 lore raw
Quoted 11 lines · David Miller

From: David VomLehn <[email protected]>
Date: Wed, 29 Apr 2009 15:28:09 -0700

Quoted 1 line

Still, let us back up to technical specifics.

The specifics are that if you showed me something that added a "u16", at most, I'd go to bat for you and support your work going upstream.

We could find a way to squeeze that into the existing structure with little or even no size increase.

I think the size is very doable. Only two bits are needed to encode the state so the ability to check the state isn't an issue. The other piece, though, is recording enough information to be able to pinpoint the problem.

This initial patch uses two void * to record the addresses where the sk_buff allocation and freeing is done. So, the question is how to compress these. Since there are only a limited of places where sk_buffs are allocated and freed, we can, use an index. As long the two indices use a total of no more than 14 bits, the sk_buff growth will only be 16 bits.

Converting from call location index to the call location is only done when an error is detected; the performance is not that important. Converting from a call location to an index is performance critical and I'm thinking of a couple of approaches. As seems to usually be the case, the better performing solution is more complex.

David VomLehn

maintainer
#7 lore raw

From: David VomLehn <[email protected]>
Date: Thu, 30 Apr 2009 09:32:00 -0700

Quoted 4 lines

I think the size is very doable. Only two bits are needed to encode the state so the ability to check the state isn't an issue. The other piece, though, is recording enough information to be able to pinpoint the problem.

It's not my job to design this thing for you, but I guess that's what I have to do to get you to think out of the box a little bit :-/

Maintain a table, which is only recorded into when the debugging feature is enable (via sysctl or whatever) which remembers this stuff.

That way nobody eats the space or time overhead unless the debugging feature is enabled.

Wow, I had to use every brain cell I have to come up with that one!

#8 lore raw
Quoted 10 lines · David Miller

From: David VomLehn <[email protected]>
Date: Thu, 30 Apr 2009 09:32:00 -0700

Quoted 4 lines

I think the size is very doable. Only two bits are needed to encode the state so the ability to check the state isn't an issue. The other piece, though, is recording enough information to be able to pinpoint the problem.

It's not my job to design this thing for you, but I guess that's what I have to do to get you to think out of the box a little bit :-/

Uh, perhaps you missed the rest of the email, where I said I'm thinking of a couple of approaches. The purpose of the email was to let you know briefly that I am looking at approaches that will meet your 16-bit space constraint.

Quoted 5 lines

Maintain a table, which is only recorded into when the debugging feature is enable (via sysctl or whatever) which remembers this stuff.

That way nobody eats the space or time overhead unless the debugging feature is enabled.

Your description is general enough that I may misunderstand your idea, but it sounds like, if debugging was on, you'd be recording every call to alloc_skb and kfree_skb in your table. This would use a lot of memory. You can't simply have a circular list, because an sk_buff might be allocated for a very long time before being used and you would overwrite its entry. This would reduce the reliability of the information, which is the last thing you want in a debugging tool. So, I'm considering approaches without this drawback.

Quoted 1 line

Wow, I had to use every brain cell I have to come up with that one!

David VomLehn

maintainer
#9 lore raw

From: David VomLehn <[email protected]>
Date: Thu, 30 Apr 2009 10:59:13 -0700

Quoted 8 lines

Your description is general enough that I may misunderstand your idea, but it sounds like, if debugging was on, you'd be recording every call to alloc_skb and kfree_skb in your table. This would use a lot of memory. You can't simply have a circular list, because an sk_buff might be allocated for a very long time before being used and you would overwrite its entry. This would reduce the reliability of the information, which is the last thing you want in a debugging tool. So, I'm considering approaches without this drawback.

A developer trying to diagnose something is explicitly turning this on then trying to reproduce. Since it's only on when wanted we can either 1) make it really large or 2) allow the sysctl itself to specify the table size.

#10 lore raw
Quoted 12 lines · David Miller

From: David VomLehn <[email protected]>
Date: Thu, 30 Apr 2009 10:59:13 -0700

Quoted 8 lines

Your description is general enough that I may misunderstand your idea, but it sounds like, if debugging was on, you'd be recording every call to alloc_skb and kfree_skb in your table. This would use a lot of memory. You can't simply have a circular list, because an sk_buff might be allocated for a very long time before being used and you would overwrite its entry. This would reduce the reliability of the information, which is the last thing you want in a debugging tool. So, I'm considering approaches without this drawback.

Quoted 4 lines

A developer trying to diagnose something is explicitly turning this on then trying to reproduce. Since it's only on when wanted we can either 1) make it really large or 2) allow the sysctl itself to specify the table size.

There are two risks:
1. You allocate so much memory that you perturb the behavior of the
system and the bug you are looking for doesn't occur.
2. You lose the data you are looking for, which is almost guaranteed to
be something that is difficult to reproduce.

An approach that can avoid these from the start seems better. Right now, I think such approaches exist.

David VomLehn