Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sock_create() in kernel

195 views
Skip to first unread message

Mark

unread,
Dec 19, 2012, 10:18:32 AM12/19/12
to
Hello,

I was looking in to __sock_create() code to get better understanding of the
kernel's internal machinery and found that the kernel calls try_module_get()
twice; here is a snippet:

static int __sock_create(struct net *net, int family, int type, int
protocol, struct socket **res, int kern)
{
sock = sock_alloc();
...
if (!try_module_get(pf->owner))
goto out_release;

err = pf->create(net, sock, protocol);
...

if (!try_module_get(sock->ops->owner))
goto out_module_busy

...
}

Essentially if socket relevant callbacks are in a module, then whenever
every socket() from the user space will bump the module's reference count
twice. What's the rationale for such behaviour ?

Thanks.

Mark


Rainer Weikusat

unread,
Dec 19, 2012, 11:06:49 AM12/19/12
to
"Mark" <mark_cruz...@hotmail.com> writes:

[...]

> static int __sock_create(struct net *net, int family, int type, int
> protocol, struct socket **res, int kern)
> {
> sock = sock_alloc();
> ...
> if (!try_module_get(pf->owner))
> goto out_release;
>
> err = pf->create(net, sock, protocol);
> ...
>
> if (!try_module_get(sock->ops->owner))
> goto out_module_busy
>
> ...
> }
>
> Essentially if socket relevant callbacks are in a module, then whenever
> every socket() from the user space will bump the module's reference count
> twice. What's the rationale for such behaviour ?

Have you considered reading the comments and looking at the
surrounding code?

/*
* We will call the ->create function, that possibly is in a loadable
* module, so we have to bump that loadable module refcnt first.
*/
if (!try_module_get(pf->owner))
goto out_release;

/* Now protected by module ref count */
rcu_read_unlock();

err = pf->create(net, sock, protocol, kern);
if (err < 0)
goto out_module_put;

/*
* Now to bump the refcnt of the [loadable] module that owns this
* socket at sock_release time we decrement its refcnt.
*/
if (!try_module_get(sock->ops->owner))
goto out_module_busy;

/*
* Now that we're done with the ->create function, the [loadable]
* module can have its refcnt decremented
*/
module_put(pf->owner);
[net/socket.c]

0 new messages