From: Eric Dumazet <eric.dumazet@gmail.com>
To: Neshama Parhoti <pneshama@gmail.com>
Cc: netdev@vger.kernel.org
Subject: Re: lock_sock or sock_hold ?
Date: Mon, 26 Jul 2010 16:43:26 +0200 [thread overview]
Message-ID: <1280155406.2899.407.camel@edumazet-laptop> (raw)
In-Reply-To: <AANLkTim7LQ65RWvy+TQ+HtwjJM89zt4g12H6mFinQT0v@mail.gmail.com>
Le lundi 26 juillet 2010 à 17:00 +0300, Neshama Parhoti a écrit :
> hello everyone,
>
> can you please be kind and help me understand the differences between
> lock_sock and sock_hold ?
>
> i can see that lock_sock takes a spin lock (bh) to mark owned = 1, and
> then takes a mutex, whereas sock_hold only increases the atomic
> refcnt.
>
> but how do i know which of them I should use ?
>
> i'm puzzled as to when should those two different APIs be used..
sock_hold() only increments a refcount, so that you are sure nobody can
destroy the socket (and its memory) under you. But you cannot modify
socket state only with this refcount taken.
To get exclusive access to the socket you either :
1) Are in process context and use lock_sock().
2) Are in softirq context (input path for example) :
2.1) Lookup the socket in protocol hash tables, and get a refcount on
it (by sock_hold() or other atomic operation on refcnt)
2.2) Then, get semi exclusive access using bh_lock_sock()
2.3) Check if another process already is using the socket (we
interrupted this process on same CPU, or run on another cpu)
if (sock_owned_by_user(sk)) {
// queue work to socket backlog (delayed work)
} else {
// process packet right now
}
prev parent reply other threads:[~2010-07-26 14:43 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-26 14:00 lock_sock or sock_hold ? Neshama Parhoti
2010-07-26 14:43 ` Eric Dumazet [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1280155406.2899.407.camel@edumazet-laptop \
--to=eric.dumazet@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pneshama@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox