Subject: Re: [PATCH net-next 7/7] net: move skb->dropcount to skb->cb[]

From: Shmulik Ladkani <[email-masked]>
Date: Thu, 26 Feb 2015 10:49:09 +0200

On Thu, 26 Feb 2015 04:10:12 +0200 Eyal Birger <[email-masked]> wrote:
> diff --git a/include/net/sock.h b/include/net/sock.h
> index d462f5e..8807586 100644
> --- a/include/net/sock.h
> +++ b/include/net/sock.h
> @@ -2078,12 +2078,26 @@ static inline int sock_intr_errno(long timeo)
>  	return timeo == MAX_SCHEDULE_TIMEOUT ? -ERESTARTSYS : -EINTR;
>  }
>  
> +struct sock_skb_cb {
> +	u32 dropcount;
> +};
> +
> +/* Store sock_skb_cb at the end of skb->cb[] so protocol families
> + * using skb->cb[] would keep using it directly and utilize its
> + * alignement guarantee.
> + */
> +#define SOCK_SKB_CB_OFFSET ((FIELD_SIZEOF(struct sk_buff, cb) - \
> +			    sizeof(struct sock_skb_cb)))
> +
> +#define SOCK_SKB_CB(__skb) ((struct sock_skb_cb *)((__skb)->cb + \
> +			    SOCK_SKB_CB_OFFSET))
> +
>  #define sock_skb_cb_check_size(size) \
> -	BUILD_BUG_ON((size) > FIELD_SIZEOF(struct sk_buff, cb))
> +	BUILD_BUG_ON((size) > SOCK_SKB_CB_OFFSET)
>  

You didn't take care of aligning the 'sock_skb_cb'.
(Althogh in practive, dropcount is 4 and cb[] is 48 so you're golden).

How about:

struct sock_skb_cb {
	/* protocol families specifc CBs */
	u8 reserved[__SOCK_SKB_CB_OFFSET];
	struct __sock_skb_cb x;			// name me better!
};

Where '__SOCK_SKB_CB_OFFSET' and '__sock_skb_cb' are as follows:

struct __sock_skb_cb {	// name me better!
	u32 dropcount;
};

#define __SOCK_SKB_CB_OFFSET_UNALIGNED \
	(FIELD_SIZEOF(struct sk_buff, cb) - sizeof(struct __sock_skb_cb))

#define __SOCK_SKB_CB_OFFSET \
	(__SOCK_SKB_CB_OFFSET_UNALIGNED - \
	 (__SOCK_SKB_CB_OFFSET_UNALIGNED % __alignof__(struct __sock_skb_cb)))

This also takes care for alignement of '__sock_skb_cb x' within the CB.

Then,

#define sock_skb_cb_check_size(size) \
	BUILD_BUG_ON((size) > FIELD_SIZEOF(struct sock_skb_cb, reserved))

Regards,
Shmulik