Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1268634 > unrolled thread

Anonymous inode cleanup?

Started byRajat Jain <rajatxjain@gmail.com>
First post2015-11-13 06:50 +0100
Last post2015-11-13 07:40 +0100
Articles 3 — 3 participants

Back to article view | Back to linux.kernel


Contents

  Anonymous inode cleanup? Rajat Jain <rajatxjain@gmail.com> - 2015-11-13 06:50 +0100
    Re: Anonymous inode cleanup? Mateusz Guzik <mguzik@redhat.com> - 2015-11-13 07:00 +0100
    Re: Anonymous inode cleanup? Al Viro <viro@ZenIV.linux.org.uk> - 2015-11-13 07:40 +0100

#1268634 — Anonymous inode cleanup?

FromRajat Jain <rajatxjain@gmail.com>
Date2015-11-13 06:50 +0100
SubjectAnonymous inode cleanup?
Message-ID<qufpg-2lu-3@gated-at.bofh.it>
Hello,

I'm writing a module that wants to get anonymous fd [using
anon_inode_getfd()] and my code looks like this:

fd = anon_inode_getfd(...)
if (fd < 0)
    return -EINVAL;

if (foobar_fail()) {
    /* undo everything */
    return -EINVAL;
}

My question is that in case of a failure after the anon_inode_getfd(),
I want to cleanup and undo whatever needs to be done w.r.t. anodnymous
fd I just allocated. (May be put a reference, or return the fd to the
free pool or whatever). Can some one please let me know what cleanup
needs to be done?

However neither I see a cleanup function, nor I see any of the drivers
attempting
to free the fd in case of failure.

Thanks,

Thanks,

Rajat
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1268636

FromMateusz Guzik <mguzik@redhat.com>
Date2015-11-13 07:00 +0100
Message-ID<qufyW-2p0-3@gated-at.bofh.it>
In reply to#1268634
On Thu, Nov 12, 2015 at 09:43:00PM -0800, Rajat Jain wrote:
> Hello,
> 
> I'm writing a module that wants to get anonymous fd [using
> anon_inode_getfd()] and my code looks like this:
> 
> fd = anon_inode_getfd(...)
> if (fd < 0)
>     return -EINVAL;
> 
> if (foobar_fail()) {
>     /* undo everything */
>     return -EINVAL;
> }
> 
> My question is that in case of a failure after the anon_inode_getfd(),
> I want to cleanup and undo whatever needs to be done w.r.t. anodnymous
> fd I just allocated. (May be put a reference, or return the fd to the
> free pool or whatever). Can some one please let me know what cleanup
> needs to be done?
> 
> However neither I see a cleanup function, nor I see any of the drivers
> attempting
> to free the fd in case of failure.
> 

It is impossible to properly clean up in this case without serious
tinkering. In fact this code cannot realiably work without weird
locking. By the time anon_inode_getfd returns, the file could have been
closed by a different thread.

What you want instead is anon_inode_getfile. See perf_event_open for an
example how to use it.

-- 
Mateusz Guzik
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1268650

FromAl Viro <viro@ZenIV.linux.org.uk>
Date2015-11-13 07:40 +0100
Message-ID<qugbE-2S1-5@gated-at.bofh.it>
In reply to#1268634
On Thu, Nov 12, 2015 at 09:43:00PM -0800, Rajat Jain wrote:
> Hello,
> 
> I'm writing a module that wants to get anonymous fd [using
> anon_inode_getfd()] and my code looks like this:
> 
> fd = anon_inode_getfd(...)
> if (fd < 0)
>     return -EINVAL;
> 
> if (foobar_fail()) {
>     /* undo everything */
>     return -EINVAL;
> }
> 
> My question is that in case of a failure after the anon_inode_getfd(),
> I want to cleanup and undo whatever needs to be done w.r.t. anodnymous
> fd I just allocated. (May be put a reference, or return the fd to the
> free pool or whatever). Can some one please let me know what cleanup
> needs to be done?
> 
> However neither I see a cleanup function, nor I see any of the drivers
> attempting
> to free the fd in case of failure.

You can't.  As soon as it's in descriptor table, you'd better be *done*
with it.  No "I need more setup done", no "I just need to do one final
check" - the moment it hits the descriptor table, another thread might
be issuing syscalls on it.  Including dup2(), so there's no way to take
it back.  Moreover, another thread might've done dup2() over your
descriptor, so you can't even decide to close the one you'd just installed.
Yes, even in cases when the failed action would be to report the resulting
descriptor to userland.  Generally you should try to return descriptors to
userland only via the syscall return value.

_If_ you are returning them via a sucky API, the right sequence is
	reserve the descriptor(s)
	set the file(s) up
	fill whatever structure you'll be using to report descriptors to
userland and copy it to userland memory
	use fd_install() to put files into descriptor table.

See e.g. fs/pipe.c and look for pipe2 in there for example of dealing with
such APIs.

"Set the file up" primitive in case of anon_inode is anon_inode_getfile();
grep and you shall see...

Again, fd_install() is the equivalent of hitting "send" - there's no way
to make what you've published disappear.  It's the point of no return.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web