Re: memleak in net_rx protocol handling? or my misuse of dev_add_pack?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



this list indeed appears to be dead. Anyway, I figured it out for myself in 
the end and thought I'd post it here for google to pick up.

net protocol handlers indeed get sk_buffs with increased reference counts. To 
be precise, dev.c:netif_receive_skb selects the protocol handler to forward 
to and then calls dev.c:deliver_skb, which increments the reference count, as 
can be seen below.

static __inline__ int deliver_skb(struct sk_buff *skb, struct packet_type 
*pt_prev)
{
        atomic_inc(&skb->users);
        return pt_prev->func(skb, skb->dev, pt_prev);
}

thus, decrease reference count manually in your network protocol handler if 
you value your RAM.

by the way, I'm referring to 2.6.11 here 'cause that's on lxr.linux.no, but as 
the dev.c code is fairly static I expect it to be true for most kernels.

cheers,

  Willem

ps: I have a feeling that I'm only talking to myself here. Ah well, I love 
good company :)
> hi,
>
>   for a kernel networking project I have to fetch packets from netfilter
> and/or netrx. For this I use either netfilter_register_hook or
> dev_add_pack. When my function gets called I -- for debugging -- directly
> return NF_ACCEPT resp. NET_RX_SUCCESS, without any processing.
>
>   the weird thing is: with netfilter everything works, but with netrx I get
> a gigantic memory leak. I suspect that no sk_buff gets deallocated. So, my
> question is: does netif expect me to decrease the skb reference count
> manually? Or did I truly notice something peculiar?
>
>   for inspection I added my processing code below. And I tested this on
> 2.6.14.5. If anyone has an idea what I'm doing wrong, thanks. I understand
> that traffic is somewhat slow (ahem) on this list, but guess that net-dev
> is not the proper place for these questions.
>
> cheers,
>
>   Willem de Bruijn
>
> -----------START-OF-CODE------------------
> static inline int process(struct sk_buff *skb, const struct net_device
> *dev) {
> 	return 1;
> }
>
> ///// netfilter specific code
>
> #ifdef CONFIG_NETFILTER
> static unsigned int hook_netfilter (unsigned int hook, struct sk_buff
> **pskb, const struct net_device *indev,
> 					const struct net_device *outdev,
> 					int (*okfn)(struct sk_buff *))
> {
> 	return process(*pskb, indev) ? NF_ACCEPT : NF_DROP;
> }
>
> static struct nf_hook_ops nfops = {
> 	.hook		= &hook_netfilter,
> 	.owner		= THIS_MODULE,
> 	.pf		= PF_INET,
> 	.hooknum	= NF_IP_PRE_ROUTING,
> 	.priority	= 0,
> };
> #endif
>
> ///// netrx specific code
>
> int hook_netrx(	struct sk_buff *skb,
> 		struct net_device *dev,
> 		struct packet_type *pt
> #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
> 		, struct net_device *otherdev
> #endif
> 		)
> {
> 	return process(skb, dev) ? NET_RX_SUCCESS : NET_RX_DROP;
> }
>
> static struct packet_type nrops =
> {
> 	.type			= __constant_htons(ETH_P_ALL),
> 	.dev			= NULL,
> 	.func			= hook_netrx,
> 	.list			= {NULL,NULL},
> 	.af_packet_priv = NULL,
> };
>
> -
> : send the line "unsubscribe linux-net" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux