The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Sergei Zviagintsev <sergei@s15v.net>
To: David Herrmann <dh.herrmann@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Daniel Mack <daniel@zonque.org>,
	Djalal Harouni <tixxdz@opendz.org>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 43/44] kdbus: Give up on failed fd allocation
Date: Fri, 9 Oct 2015 21:49:41 +0300	[thread overview]
Message-ID: <20151009184941.GN2189@localhost.localdomain> (raw)
In-Reply-To: <CANq1E4SD31NtTvQnYq4mDbDZ7WReJp8F5vRJ7JsTEeuhDFVo9Q@mail.gmail.com>

Hi,

On Thu, Oct 08, 2015 at 05:14:24PM +0200, David Herrmann wrote:
> Hi
> 
> On Thu, Oct 8, 2015 at 1:32 PM, Sergei Zviagintsev <sergei@s15v.net> wrote:
> > If we failed to allocate a file descriptor, do not try to do it again on
> > the next iteration. There are few chances that we will have success, and
> > we can simplify the algorithm assuming that valid fd numbers are not
> > mixed with -1 values.
> 
> Why? This is no a fast-path, and the penalty is accounted on the
> actual culprit (the caller). I don't see why we should optimize for
> that case. If the fd-allocation fails, you clearly did something
> wrong.

I agree, but this is optimization for readability and not for speed.
If we don't care whether fd-allocation failed or not, why should we keep
less readable variant which handles those -1 values mixed with valid fds
numbers?

> 
> Thanks
> David
> 
> > Signed-off-by: Sergei Zviagintsev <sergei@s15v.net>
> > ---
> >  ipc/kdbus/message.c | 42 ++++++++++++++++++++++++------------------
> >  1 file changed, 24 insertions(+), 18 deletions(-)
> >
> > diff --git a/ipc/kdbus/message.c b/ipc/kdbus/message.c
> > index da685049d66c..75e6213e7ed5 100644
> > --- a/ipc/kdbus/message.c
> > +++ b/ipc/kdbus/message.c
> > @@ -123,7 +123,7 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
> >  {
> >         bool incomplete_fds = false;
> >         struct kvec kvec;
> > -       size_t i, n_fds;
> > +       size_t i, n_fds, n_memfds;
> >         int ret, *fds;
> >
> >         if (!gaps) {
> > @@ -140,25 +140,31 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
> >         }
> >
> >         fds = kmalloc_array(n_fds, sizeof(*fds), GFP_TEMPORARY);
> > -       n_fds = 0;
> >         if (!fds)
> >                 return -ENOMEM;
> >
> >         /* 1) allocate fds and copy them over */
> >
> > +       n_fds = 0; /* n_fds now tracks the number of allocated fds */
> >         if (gaps->n_fds > 0) {
> >                 for (i = 0; i < gaps->n_fds; ++i) {
> >                         int fd;
> >
> >                         fd = get_unused_fd_flags(O_CLOEXEC);
> > -                       if (fd < 0)
> > +                       if (fd < 0) {
> >                                 incomplete_fds = true;
> > +                               break;
> > +                       }
> >
> >                         WARN_ON(!gaps->fd_files[i]);
> >
> > -                       fds[n_fds++] = fd < 0 ? -1 : fd;
> > +                       fds[n_fds++] = fd;
> >                 }
> >
> > +               /* If we couldn't allocate a fd, fill the rest with -1 */
> > +               for ( ; i < gaps->n_fds; ++i)
> > +                       fds[i] = -1;
> > +
> >                 /*
> >                  * The file-descriptor array can only be present once per
> >                  * message. Hence, prepare all fds and then copy them over with
> > @@ -175,18 +181,18 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
> >                         goto exit;
> >         }
> >
> > -       for (i = 0; i < gaps->n_memfds; ++i) {
> > +       n_memfds = 0;
> > +       for (i = 0; i < gaps->n_memfds && !incomplete_fds; ++i) {
> >                 int memfd;
> >
> >                 memfd = get_unused_fd_flags(O_CLOEXEC);
> >                 if (memfd < 0) {
> >                         incomplete_fds = true;
> > -                       fds[n_fds++] = -1;
> >                         /* memfds are initialized to -1, skip copying it */
> > -                       continue;
> > +                       break;
> >                 }
> >
> > -               fds[n_fds++] = memfd;
> > +               fds[gaps->n_fds + n_memfds++] = memfd;
> >
> >                 /*
> >                  * memfds have to be copied individually as they each are put
> > @@ -208,21 +214,21 @@ int kdbus_gaps_install(struct kdbus_gaps *gaps, struct kdbus_pool_slice *slice,
> >
> >         /* 2) install fds now that everything was successful */
> >
> > -       for (i = 0; i < gaps->n_fds; ++i)
> > -               if (fds[i] >= 0)
> > -                       fd_install(fds[i], get_file(gaps->fd_files[i]));
> > -       for (i = 0; i < gaps->n_memfds; ++i)
> > -               if (fds[gaps->n_fds + i] >= 0)
> > -                       fd_install(fds[gaps->n_fds + i],
> > -                                  get_file(gaps->memfd_files[i]));
> > +       for (i = 0; i < n_fds; ++i)
> > +               fd_install(fds[i], get_file(gaps->fd_files[i]));
> > +       for (i = 0; i < n_memfds; ++i)
> > +               fd_install(fds[gaps->n_fds + i],
> > +                          get_file(gaps->memfd_files[i]));
> >
> >         ret = 0;
> >
> >  exit:
> > -       if (ret < 0)
> > +       if (ret < 0) {
> >                 for (i = 0; i < n_fds; ++i)
> > -                       if (fds[i] >= 0)
> > -                               put_unused_fd(fds[i]);
> > +                       put_unused_fd(fds[i]);
> > +               for (i = 0; i < n_memfds; ++i)
> > +                       put_unused_fd(fds[gaps->n_fds + i]);
> > +       }
> >         kfree(fds);
> >         *out_incomplete = incomplete_fds;
> >         return ret;
> > --
> > 1.8.3.1
> >

  reply	other threads:[~2015-10-09 18:49 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-08 11:31 [PATCH 00/44] kdbus cleanups Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 01/44] Documentation/kdbus: Document new name registry flags Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 02/44] uapi: kdbus.h: Kernel-doc fixes Sergei Zviagintsev
2015-10-08 13:42   ` David Herrmann
2015-10-08 11:31 ` [PATCH 03/44] kdbus: Kernel-docs and comments trivial fixes Sergei Zviagintsev
2015-10-08 13:46   ` David Herrmann
2015-10-08 11:31 ` [PATCH 04/44] kdbus: Update kernel-doc for struct kdbus_pool Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 05/44] kdbus: Add comment on merging free pool slices Sergei Zviagintsev
2015-10-08 13:50   ` David Herrmann
2015-10-09 18:11     ` Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 06/44] kdbus: Fix kernel-doc for struct kdbus_gaps Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 07/44] kdbus: Fix comment on translation of caps between namespaces Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 08/44] kdbus: Rename var in kdbus_meta_export_caps() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 09/44] kdbus: Remove unused KDBUS_MSG_MAX_SIZE constant Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 10/44] kdbus: Use conditional operator Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 11/44] kdbus: Cosmetic fix of kdbus_name_is_valid() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 12/44] kdbus: Use conventional list macros in __kdbus_pool_slice_release() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 13/44] kdbus: Use list_next_entry() in kdbus_queue_entry_unlink() Sergei Zviagintsev
2015-10-08 14:09   ` David Herrmann
2015-10-08 11:31 ` [PATCH 14/44] kdbus: Simplify expression in kdbus_get_memfd() Sergei Zviagintsev
2015-10-08 14:21   ` David Herrmann
2015-10-08 11:31 ` [PATCH 15/44] kdbus: Simplify bitwise expression in kdbus_meta_get_mask() Sergei Zviagintsev
2015-10-08 14:24   ` David Herrmann
2015-10-09 17:50     ` Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 16/44] kdbus: Drop redundant code from kdbus_name_acquire() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 17/44] kdbus: Drop duplicated code from kdbus_pool_slice_alloc() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 18/44] kdbus: Add var initialization to kdbus_conn_entry_insert() Sergei Zviagintsev
2015-10-08 14:28   ` David Herrmann
2015-10-09 17:52     ` Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 19/44] kdbus: Drop useless initialization from kdbus_conn_reply() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 20/44] kdbus: Drop useless initialization from kdbus_cmd_hello() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 21/44] kdbus: Cleanup tests in kdbus_cmd_send() Sergei Zviagintsev
2015-10-08 14:30   ` David Herrmann
2015-10-09 18:07     ` Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 22/44] kdbus: Cleanup error path in kdbus_staging_new_user() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 23/44] kdbus: Cleanup kdbus_conn_call() Sergei Zviagintsev
2015-10-08 14:32   ` David Herrmann
2015-10-09 18:15     ` Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 24/44] kdbus: Cleanup kdbus_conn_unicast() Sergei Zviagintsev
2015-10-08 14:34   ` David Herrmann
2015-10-09 18:32     ` Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 25/44] kdbus: Cleanup kdbus_cmd_conn_info() Sergei Zviagintsev
2015-10-08 14:38   ` David Herrmann
2015-10-09 18:45     ` Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 26/44] kdbus: Cleanup kdbus_pin_dst() Sergei Zviagintsev
2015-10-08 14:40   ` David Herrmann
2015-10-09 18:46     ` Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 27/44] kdbus: Cleanup kdbus_conn_new() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 28/44] kdbus: Cleanup kdbus_queue_entry_new() Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 29/44] kdbus: Improve tests on incrementing quota Sergei Zviagintsev
2015-10-08 11:31 ` [PATCH 30/44] kdbus: Cleanup kdbus_meta_proc_mask() Sergei Zviagintsev
2015-10-08 14:47   ` David Herrmann
2015-10-08 11:32 ` [PATCH 31/44] kdbus: Cleanup kdbus_conn_move_messages() Sergei Zviagintsev
2015-10-08 14:50   ` David Herrmann
2015-10-09 18:47     ` Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 32/44] kdbus: Remove duplicated code from kdbus_conn_lock2() Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 33/44] kdbus: Improve kdbus_staging_reserve() Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 34/44] kdbus: Improve kdbus_conn_entry_sync_attach() Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 35/44] kdbus: Drop goto from kdbus_queue_entry_link() Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 36/44] kdbus: Improve kdbus_name_release() Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 37/44] kdbus: Fix error path in kdbus_meta_proc_collect_cgroup() Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 38/44] kdbus: Fix error path in kdbus_user_lookup() Sergei Zviagintsev
2015-10-08 15:06   ` David Herrmann
2015-10-09 18:48     ` Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 39/44] kdbus: Cleanup kdbus_user_lookup() Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 40/44] kdbus: Cleanup kdbus_item_validate_name() Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 41/44] kdbus: Fix memfd install algorithm Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 42/44] kdbus: Check if fd is allocated before trying to free it Sergei Zviagintsev
2015-10-08 11:32 ` [PATCH 43/44] kdbus: Give up on failed fd allocation Sergei Zviagintsev
2015-10-08 15:14   ` David Herrmann
2015-10-09 18:49     ` Sergei Zviagintsev [this message]
2015-10-08 11:32 ` [PATCH 44/44] kdbus: Cleanup kdbus_gaps_install() Sergei Zviagintsev
2015-10-08 15:20 ` [PATCH 00/44] kdbus cleanups David Herrmann
2015-10-09  7:28   ` Sergei Zviagintsev

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=20151009184941.GN2189@localhost.localdomain \
    --to=sergei@s15v.net \
    --cc=daniel@zonque.org \
    --cc=dh.herrmann@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tixxdz@opendz.org \
    /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