From owner-svn-src-head@freebsd.org Sun May 6 00:03:24 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id BB133FBFE5A; Sun, 6 May 2018 00:03:24 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6A1FC69667; Sun, 6 May 2018 00:03:24 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 64D2724AF9; Sun, 6 May 2018 00:03:24 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w4603OjG064083; Sun, 6 May 2018 00:03:24 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w4603Ok0064082; Sun, 6 May 2018 00:03:24 GMT (envelope-from markj@FreeBSD.org) Message-Id: <201805060003.w4603Ok0064082@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Sun, 6 May 2018 00:03:24 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r333278 - head/sys/geom/mirror X-SVN-Group: head X-SVN-Commit-Author: markj X-SVN-Commit-Paths: head/sys/geom/mirror X-SVN-Commit-Revision: 333278 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 06 May 2018 00:03:25 -0000 Author: markj Date: Sun May 6 00:03:24 2018 New Revision: 333278 URL: https://svnweb.freebsd.org/changeset/base/333278 Log: Avoid dropping the topology lock in gmirror's dumpconf implementation. Doing so introduces races which can lead to a use-after-free when grabbing a snapshot of the GEOM mesh. To ensure that a mirror's disk list remains stable, change its locking protocol: both the softc lock and the topology lock are now required to modify the list, so either lock is sufficient for traversal. Tested by: pho MFC after: 2 weeks Sponsored by: Dell EMC Isilon Modified: head/sys/geom/mirror/g_mirror.c Modified: head/sys/geom/mirror/g_mirror.c ============================================================================== --- head/sys/geom/mirror/g_mirror.c Sat May 5 22:40:40 2018 (r333277) +++ head/sys/geom/mirror/g_mirror.c Sun May 6 00:03:24 2018 (r333278) @@ -277,8 +277,6 @@ g_mirror_ndisks(struct g_mirror_softc *sc, int state) struct g_mirror_disk *disk; u_int n = 0; - sx_assert(&sc->sc_lock, SX_LOCKED); - LIST_FOREACH(disk, &sc->sc_disks, d_next) { if (state == -1 || disk->d_state == state) n++; @@ -495,7 +493,9 @@ g_mirror_destroy_disk(struct g_mirror_disk *disk) sc = disk->d_softc; sx_assert(&sc->sc_lock, SX_XLOCKED); + g_topology_lock(); LIST_REMOVE(disk, d_next); + g_topology_unlock(); g_mirror_event_cancel(disk); if (sc->sc_hint == disk) sc->sc_hint = NULL; @@ -522,6 +522,8 @@ static void g_mirror_free_device(struct g_mirror_softc *sc) { + g_topology_assert(); + mtx_destroy(&sc->sc_queue_mtx); mtx_destroy(&sc->sc_events_mtx); mtx_destroy(&sc->sc_done_mtx); @@ -2626,6 +2628,7 @@ again: DISK_STATE_CHANGED(); disk->d_state = state; + g_topology_lock(); if (LIST_EMPTY(&sc->sc_disks)) LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next); else { @@ -2643,6 +2646,7 @@ again: if (dp != NULL) LIST_INSERT_AFTER(dp, disk, d_next); } + g_topology_unlock(); G_MIRROR_DEBUG(1, "Device %s: provider %s detected.", sc->sc_name, g_mirror_get_diskname(disk)); if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) @@ -3328,24 +3332,20 @@ g_mirror_dumpconf(struct sbuf *sb, const char *indent, disk = cp->private; if (disk == NULL) return; - g_topology_unlock(); - sx_xlock(&sc->sc_lock); sbuf_printf(sb, "%s%u\n", indent, (u_int)disk->d_id); if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) { sbuf_printf(sb, "%s", indent); if (disk->d_sync.ds_offset == 0) sbuf_printf(sb, "0%%"); - else { + else sbuf_printf(sb, "%u%%", (u_int)((disk->d_sync.ds_offset * 100) / - sc->sc_provider->mediasize)); - } + sc->sc_mediasize)); sbuf_printf(sb, "\n"); - if (disk->d_sync.ds_offset > 0) { + if (disk->d_sync.ds_offset > 0) sbuf_printf(sb, "%s%jd" "\n", indent, (intmax_t)disk->d_sync.ds_offset); - } } sbuf_printf(sb, "%s%u\n", indent, disk->d_sync.ds_syncid); @@ -3380,11 +3380,7 @@ g_mirror_dumpconf(struct sbuf *sb, const char *indent, disk->d_priority); sbuf_printf(sb, "%s%s\n", indent, g_mirror_disk_state2str(disk->d_state)); - sx_xunlock(&sc->sc_lock); - g_topology_lock(); } else { - g_topology_unlock(); - sx_xlock(&sc->sc_lock); sbuf_printf(sb, "%s", indent); switch (sc->sc_type) { case G_MIRROR_TYPE_AUTOMATIC: @@ -3436,8 +3432,6 @@ g_mirror_dumpconf(struct sbuf *sb, const char *indent, else sbuf_printf(sb, "%s", "DEGRADED"); sbuf_printf(sb, "\n"); - sx_xunlock(&sc->sc_lock); - g_topology_lock(); } }