public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.cz>
To: Anand Jain <anandsuveer@gmail.com>
Cc: dsterba@suse.cz, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 4/4] btrfs: sysfs, add devid/dev_state kobject and attribute
Date: Fri, 13 Dec 2019 17:43:32 +0100	[thread overview]
Message-ID: <20191213164332.GA3929@twin.jikos.cz> (raw)
In-Reply-To: <8bd3d9b9-11b1-4c9a-8b59-ccfe0c6d92c4@oracle.com>

On Mon, Dec 09, 2019 at 10:05:39PM +0800, Anand Jain wrote:
> On 12/6/19 9:49 PM, Anand Jain wrote:
> > 
> > 
> > On 5/12/19 11:14 PM, David Sterba wrote:
> >> On Thu, Dec 05, 2019 at 10:38:15PM +0800, Anand Jain wrote:
> >>>> So the values copy the device state macros, that's probably ok.
> >>>    Yep.
> >>
> >> Although, sysfs files should print one value per file, which makes sense
> >> in many cases, so eg. missing should exist separately too for quick
> >> checks for the most common device states. The dev_state reflects the
> >> internal state and is likely useful only for debugging.
> >>
> > 
> >   I agree. Its better to create an individual attribute for each of the
> >   device states. For instance..
> > 
> >     under the 'UUID/devinfo/<devid>' kobject
> >     attributes will be:
> >       missing
> >       in_fs_metadata
> >       replace_target
> > 
> >    cat missing
> >     0
> >    cat in_fs_metadata
> >     1
> > 
> >    ..etc
> > 
> >   which seems to be more or less standard for block devices.
> > 
> >   Will fix it in v2.
> 
> This is fixed in v2.
> 
> 
> > 
> >>>>> +static ssize_t btrfs_sysfs_dev_state_show(struct kobject *kobj,
> >>>>> +                      struct kobj_attribute *a, char *buf)
> >>>>> +{
> >>>>> +    struct btrfs_device *device = container_of(kobj, struct 
> >>>>> btrfs_device,
> >>>>> +                           devid_kobj);
> >>>>> +
> >>>>> +    btrfs_dev_state_to_str(device, buf, PAGE_SIZE);
> >>>>
> >>>> The device access is unprotected, you need at least RCU but that still
> >>>> does not prevent from the device being freed by deletion.
> >>>
> >>>    We need RCU let me fix. Device being deleted is fine, there
> >>>    is nothing to lose, another directory lookup will show that
> >>>    UUID/devinfo/<devid> is gone is the device is deleted.
> >>
> >> The device can be gone from the list but the sysfs files can still
> >> exist.
> >>
> >>      CPU1                                   CPU2
> >>
> >> btrfs_rm_device
> >>                                          open file
> >>    btrfs_sysfs_rm_device_link
> >>      btrfs_free_device
> >>        kfree(device)
> >>                                          call read, access freed device
> >>
> > 
> >    I completely missed the sysfs synchronization with device delete.
> >    As in the other email discussion, I think a new rwlock shall suffice.
> >    And as its lock is only between device delete and sysfs so it shall
> >    be light weight without affecting the other device_list_mutex holders.
> 
>   Looked into this further, actually we don't need any lock here
>   the device delete thread which calls kobject_put() makes sure
>   sysfs read is closed. So an existing sysfs read thread will have
>   to complete before device free.
> 
> 
>        CPU1                                   CPU2
> 
>   btrfs_rm_device
>                                            open file
>      btrfs_sysfs_rm_device_link
>                                            call read, access freed device
>        sysfs waits for the open file
>        to close.

How exactly does sysfs wait for the device? Is it eg wait_event checking
number of references? If the file stays open by an evil process is it
going to block the device removal indefinitelly?

  parent reply	other threads:[~2019-12-13 20:39 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-05 11:27 [PATCH 0/4] btrfs, sysfs cleanup and add dev_state Anand Jain
2019-12-05 11:27 ` [PATCH 1/4] btrfs: sysfs, use btrfs_sysfs_remove_fsid in fail return in add_fsid Anand Jain
2019-12-05 11:27 ` [PATCH 2/4] btrfs: sysfs, add UUID/devinfo kobject Anand Jain
2020-01-14 18:32   ` David Sterba
2020-02-03 10:40     ` Anand Jain
2019-12-05 11:27 ` [PATCH v2 3/4] btrfs: sysfs, rename device_link add,remove functions Anand Jain
2019-12-05 11:27 ` [PATCH 4/4] btrfs: sysfs, add devid/dev_state kobject and attribute Anand Jain
2019-12-05 14:10   ` David Sterba
2019-12-05 14:38     ` Anand Jain
2019-12-05 14:21   ` David Sterba
2019-12-05 14:38     ` Anand Jain
2019-12-05 15:14       ` David Sterba
2019-12-06 13:49         ` Anand Jain
2019-12-09 14:05           ` Anand Jain
2019-12-09 18:31             ` David Sterba
2019-12-13 16:43             ` David Sterba [this message]
2019-12-13 17:02               ` David Sterba
2019-12-14  0:26                 ` Anand Jain
2020-01-06 16:00                   ` David Sterba
2019-12-09 14:06   ` [PATCH v2 " Anand Jain
2019-12-19 10:41     ` [PATCH v3 " Anand Jain
2020-01-06 11:38   ` [PATCH v4] " Anand Jain
2020-01-09 15:20     ` [PATCH v4 4/4] " David Sterba
2020-01-10  1:03       ` Anand Jain
2019-12-05 15:00 ` [PATCH 0/4] btrfs, sysfs cleanup and add dev_state David Sterba
2019-12-06 13:46   ` Anand Jain
2019-12-09 14:09     ` Anand Jain
2019-12-09 22:48 ` Anand Jain
2019-12-10 16:41   ` David Sterba
2020-01-14 18:39 ` David Sterba
2020-01-15  8:22   ` [PATCH] btrfs: update devid after replace Anand Jain
2020-01-15 16:17     ` David Sterba
2020-01-20 19:10     ` David Sterba

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=20191213164332.GA3929@twin.jikos.cz \
    --to=dsterba@suse.cz \
    --cc=anandsuveer@gmail.com \
    --cc=linux-btrfs@vger.kernel.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