Skip to contentSkip to conversation

kfree_skb: unnecessary check ?

DISCUSSION Patrick McHardy 3 participants 4 messages Jun 19, 2002

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

Hi everyone.

In skbuff.h one sees:

static inline void kfree_skb(struct sk_buff *skb)

{
       if (atomic_read(&skb->users) == 1 || 
atomic_dec_and_test(&skb->users))
               __kfree_skb(skb);
}

Is atomic_dec_and_test really necessary ? atomic_dec_and_test only returns true if its argument has hit zero, so it was 1 before. If atomic_dec is only a bit cheaper than atomic_dec_and_test (which i guess it is), wouldn't it make more sense to use something like this:

static inline void kfree_skb(struct sk_buff *skb)
{
if (atomic_read(&skb->users) == 1)

       __kfree(skb);
   else
       atomic_dec(&skb->users);
}

Bye,
Patrick

#1 lore raw

patrick,

--snip/snip

Quoted 2 lines

static inline void kfree_skb(struct sk_buff *skb)
{

- if (atomic_read(&skb->users) == 1)
+ if (likely(atomic_read(&skb->users) == 1))

Quoted 4 lines
        __kfree(skb);
    else
        atomic_dec(&skb->users);
}

is even faster for the likely case by 1 2 jmps (gcc3.1). (well, is it the likely case?)

tm

Signature · 1 line
in some way i do, and in some way i don't.
#2 lore raw
In reply to Patrick McHardy

Hello!

Quoted 4 lines

Is atomic_dec_and_test really necessary ? atomic_dec_and_test only returns true if its argument has hit zero, so it was 1 before. If atomic_dec is only a bit cheaper than atomic_dec_and_test (which i guess it is), wouldn't it make more sense to use something like this:

Does the prefix "atomic" not scare you? :-)

Quoted 7 lines

static inline void kfree_skb(struct sk_buff *skb)

{
    if (atomic_read(&skb->users) == 1)
        __kfree(skb);
    else
        atomic_dec(&skb->users);
}
      else if (atomic_dec_and_test(&skb->users))
	   __kfree_skb(skb);

If you still do not understand think why it does not look like:

static inline void kfree_skb(struct sk_buff *skb)

{
    int users = skb->users;
    if (users == 1)
        __kfree(skb);
    else
        skb->users = users - 1;
}

Alexey

#3 lore raw
Quoted 32 lines · [email protected] wrote:

Hello!

Quoted 3 lines

Is atomic_dec_and_test really necessary ? atomic_dec_and_test only returns true if its argument has hit zero, so it was 1 before. If atomic_dec is only a bit cheaper than atomic_dec_and_test (which i guess it is), wouldn't it make more sense to use something like this:

Does the prefix "atomic" not scare you? :-)

Quoted 8 lines

static inline void kfree_skb(struct sk_buff *skb)

{
   if (atomic_read(&skb->users) == 1)
       __kfree(skb);
   else
       atomic_dec(&skb->users);
}
     else if (atomic_dec_and_test(&skb->users))
	   __kfree_skb(skb);

If you still do not understand think why it does not look like:

static inline void kfree_skb(struct sk_buff *skb)

{
   int users = skb->users;
   if (users == 1)
       __kfree(skb);
   else
       skb->users = users - 1;
}
Quoted 2 lines

Alexey

Hehe got it :)
Thanks
Patrick