linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mateusz Guzik <mguzik@redhat.com>
To: Sandhya Bankar <bankarsandhya512@gmail.com>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	viro@zeniv.linux.org.uk, mawilcox@microsoft.com,
	keescook@chromium.org, adobriyan@gmail.com, re.emese@gmail.com,
	riel@surriel.com
Subject: Re: [RESEND PATCH 05/13] vfs: Replace array of file pointers with an IDR
Date: Wed, 4 Oct 2017 17:45:05 +0200	[thread overview]
Message-ID: <20171004154504.drdlu7zavtk3nk27@mguzik> (raw)
In-Reply-To: <3f984d2111ae4856bf5183173b0f9fcaaefed8c0.1493315290.git.bankarsandhya512@gmail.com>

On Tue, Jul 11, 2017 at 06:50:52PM +0530, Sandhya Bankar wrote:
> Instead of storing all the file pointers in a single array, use an
> IDR.  It is RCU-safe, and does not need to be reallocated when the
> fd array grows.  It also handles allocation of new file descriptors.
> 
> ---
>  
[snip]
> @@ -604,22 +576,9 @@ void put_unused_fd(unsigned int fd)
>  void __fd_install(struct files_struct *files, unsigned int fd,
>  		struct file *file)
>  {
> -	struct fdtable *fdt;
> -
> -	might_sleep();
> -	rcu_read_lock_sched();
> -
> -	while (unlikely(files->resize_in_progress)) {
> -		rcu_read_unlock_sched();
> -		wait_event(files->resize_wait, !files->resize_in_progress);
> -		rcu_read_lock_sched();
> -	}
> -	/* coupled with smp_wmb() in expand_fdtable() */
> -	smp_rmb();
> -	fdt = rcu_dereference_sched(files->fdt);
> -	BUG_ON(fdt->fd[fd] != NULL);
> -	rcu_assign_pointer(fdt->fd[fd], file);
> -	rcu_read_unlock_sched();
> +	rcu_read_lock();
> +	BUG_ON(idr_replace(&files->fd_idr, file, fd));
> +	rcu_read_unlock();
>  }
>  
>  void fd_install(unsigned int fd, struct file *file)
> @@ -641,10 +600,9 @@ int __close_fd(struct files_struct *files, unsigned fd)
>  	fdt = files_fdtable(files);
>  	if (fd >= fdt->max_fds)
>  		goto out_unlock;
> -	file = fdt->fd[fd];
> +	file = idr_remove(&files->fd_idr, fd);
>  	if (!file)
>  		goto out_unlock;
> -	rcu_assign_pointer(fdt->fd[fd], NULL);
>  	__clear_close_on_exec(fd, fdt);
>  	__put_unused_fd(files, fd);
>  	spin_unlock(&files->file_lock);

I have no opinions about the switch, however these 2 places make me
worried. I did not check all the changes so perhaps I missed something.

In the current code we are safe when it comes to concurrent install and
close, in particular here:

CPU0		CPU1
alloc_fd
		__close_fd
fd_install

__close_fd will either see a NULL pointer and return -EBADF or will see
an installed pointer and proceed with the close.

Your proposed patch seems to be buggy in this regard.

You call idr_remove, which from what I understand will free up the slot
no matter what. You only detect an error based on whether there was a
non-NULL pointer there or not. If so, fd_install can proceed to play
with a deallocated entry.

-- 
Mateusz Guzik

  reply	other threads:[~2017-10-04 15:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-11 11:36 [RESEND PATCH 00/13] vfs: Convert file allocation code to use the IDR Sandhya Bankar
2017-07-11 13:20 ` [RESEND PATCH 05/13] vfs: Replace array of file pointers with an IDR Sandhya Bankar
2017-10-04 15:45   ` Mateusz Guzik [this message]
2017-07-11 13:22 ` [RESEND PATCH 06/13] vfs: Remove next_fd from fd alloc code path Sandhya Bankar
2017-07-11 13:24 ` [RESEND PATCH 07/13] vfs: Remove full_fds_bits from fd allocation " Sandhya Bankar
2017-07-11 13:27 ` [RESEND PATCH 08/13] vfs: Use idr_tag_get() in fd_is_open() Sandhya Bankar
2017-07-11 13:29 ` [RESEND PATCH 09/13] vfs: Rewrite close_files() Sandhya Bankar
2017-07-11 13:32 ` [RESEND PATCH 10/13] vfs: Replace close_on_exec bitmap with an IDR tag Sandhya Bankar
2017-07-11 13:36 ` [RESEND PATCH 12/13] vfs: Convert select to use idr_get_tag_batch() Sandhya Bankar
2017-07-11 13:38 ` [RESEND PATCH 13/13] vfs: Delete struct fdtable Sandhya Bankar
2017-07-11 14:15 ` [RESEND PATCH 04/13] idr, radix-tree: Implement copy_preload Sandhya Bankar
2017-07-11 14:19 ` [RESEND PATCH 03/13] idr, radix-tree: Add get_tag_batch function Sandhya Bankar
2017-07-11 14:21 ` [RESEND PATCH 02/13] idr: Add idr_for_each_entry_tagged() Sandhya Bankar
2017-07-11 14:24 ` [RESEND PATCH 01/13] idr: Add ability to set/clear tags Sandhya Bankar
2017-07-11 14:27 ` [RESEND PATCH 11/13] vfs: Add init_task.h include Sandhya Bankar

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=20171004154504.drdlu7zavtk3nk27@mguzik \
    --to=mguzik@redhat.com \
    --cc=adobriyan@gmail.com \
    --cc=bankarsandhya512@gmail.com \
    --cc=keescook@chromium.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mawilcox@microsoft.com \
    --cc=re.emese@gmail.com \
    --cc=riel@surriel.com \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).