geom->access problem and workaround

Warner Losh imp at bsdimp.com
Mon Mar 12 17:11:42 UTC 2018


On Mon, Mar 12, 2018 at 7:17 AM, Andriy Gapon <avg at freebsd.org> wrote:

>
> According to Poul-Henning (phk@), the principal author of GEOM, a GEOM
> class's
> access method was intended to be a light-weight operation involving mostly
> access counts.  That is, it should be (have been) close in spirit to what
> g_access() function does.  The method is only called from g_access and it
> is
> always done under the GEOM topology lock (like with most GEOM "control
> plane"
> methods).  The lock ensures that the method and the function operate on a
> consistent state of the topology and all geoms in it.
>
> In reality, many classes have their access method do a lot more than just
> checking and modifying access bits.  And often, what the method does is
> incompatible with the topology lock.
>
> Some examples.
> g_mirror_access() has to drop and reacquire the topology lock to avoid a
> LOR
> (deadlock) because the method needs to use the class's internal sc_lock.
>
> zvol_geom_access() also has to drop and reacquire the topology lock when it
> interacts with ZFS internals involving many locks.  The main issue here is
> that
> ZFS is both above the GEOM when ZFS uses GEOM for the storage access and
> it is
> "below" the GEOM when ZFS is accessed through the ZVOL provider.
>
> g_disk_access() -> daopen().  In this case the topology lock is never
> dropped,
> but the operation issues multiple SCSI commands and waits for their
> completion.
> So, if something goes wrong and takes a long time to complete then the
> whole
> topology will be frozen for all that time.
> [Perhaps doing the lock dance would be a better alternative]
>
> But, of course, dropping the lock does not come free.
> It opens races where two (at least) sets of incompatible access counts may
> get
> granted.  Or a special action, that should be done only on a first access
> to a
> geom, could be executed more than once.
>
> Bringing everything to conformance with the original design would be an
> ideal
> solution, but it will take a lot of work both in the individual
> nonconforming
> classes and in at least some of their consumers.  It seems to require
> moving all
> the complex operations from access methods to the GEOM "data plane".  E.g,
> doing
> those things upon the first I/O operation.  Or having a new special
> BIO_GETATTR
> (kind of) operation that could be executed after g_access() but before the
> actual I/O is allowed.
>
> I am proposing an interim solution, so really a workaround, for the
> problem of
> dropping the topology lock:
>
> https://reviews.freebsd.org/D14533
>
> That workaround cannot guarantee, of course, the complete stability of the
> topology, but it prevents concurrent calls to access methods.
> The idea is very simple.  Before calling a geom's access method the geom is
> marked with a special flag unless the flag is already set in which case
> the code
> waits until the flag is cleared.  The flag is cleared after the call, of
> course.
> The topology lock is released while waiting for the flag.
>
> I think that having this new flag may help to get more visibility into the
> problem.
>
> P.S.
> The workaround does not help daopen() at all.


The storage layer generally doesn't expect higher-level locks around calls
to it, and feels that it's free to sleep in the open routine for resources
to become available. This is true across most 'open' routines (eg, tty will
wait for the right signals, etc). In a world of removable media, I'm not
sure that one can avoid this.

But I'm not sure that calling open on the underlying device is at all
compatible with the design goal of access being cheap. I think you can't
have both: either you open the device, and cope with the fact that open may
sleep, or it looks like you'll have broken code. Once we've updated the
access counts, we can drop the topology lock to call open. If it succeeds,
all is good. If it fails, then we have to reacquire it to "unaccess" the
device after the failure...  However, that doesn't help with the concurrent
attempts to do first open for the device. g_disk_access will still have
issues of sleeping indefinitely, which of course can lead to deadlock in
complicated geometry situations (I say of course, but I'm not 100% sure).

The whole reason that daopen may (but not always) sleep is that it may need
to do I/O to the device to get it's media-status / size / SN if it' s a
removable device... Just like with the RO flag, we'd want the open routine
to fail if it can't reasonably access the device.

Warner


More information about the freebsd-arch mailing list