From nobody Wed Mar 16 04:53:03 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 9311C1A2C600; Wed, 16 Mar 2022 04:53:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KJHvz3kCDz4RtT; Wed, 16 Mar 2022 04:53:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647406383; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Cfnh8PA53kaTUNL8YQ5+LbuOgf6lyjgQbZpXNsBc1oo=; b=JgKlxF4WVuxe/ekn82AfVk2Rc4lT4yVlUWl9zuG+Ivgw25MxmQ4wtoyQd94mi0kEfcakDd 9LGe3DttNPRnDEOes13WhK5tQFuOyWUt0nmCjMj8bx+l8Gse0+ysgwLx8nDJbXpAWwhsP6 ZdA3Ew7KGWzi71E39YlT38noT/pgeaulK125qokQmJXiNumcEhWXKiqsZTAiMRAAhJmoM7 ngzoZxrl06ZrlnGtmWp4pWd/lcx1jU2iGUyPhYaiz4I9hfINPcodkPa2yeS+IKemK3Y0qA PZrkiIXhCTi5757b9FrDC6psgaP2bRBpmt0N2QTX67Khui0ThLKAfM5KsRWlVA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 605437ABD; Wed, 16 Mar 2022 04:53:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 22G4r3qj036787; Wed, 16 Mar 2022 04:53:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22G4r34J036786; Wed, 16 Mar 2022 04:53:03 GMT (envelope-from git) Date: Wed, 16 Mar 2022 04:53:03 GMT Message-Id: <202203160453.22G4r34J036786@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Eugene Grosbein Subject: git: adbf7727b3a2 - main - virtio_random(8): avoid deadlock at shutdown time List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: eugen X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: adbf7727b3a2aad3c2faa6e543ee7fa7a6c9a3d5 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647406383; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Cfnh8PA53kaTUNL8YQ5+LbuOgf6lyjgQbZpXNsBc1oo=; b=CCtckCPd7i+fK+JA1bqA+nWoaFUwkxJpsO7tS4B/2088AOeHrGZGc1rJT8J7SSTQUy1hgv /0ZuY6dBhT8OVcMhMlNr2XmuD/dQ8YE3knWyCE2MivxJd5OcAmO2zTWV90+5RMdikcSP0K hVGgXWEjCfyvK3Rzvns/PErjxi+iQd9F4z2y0Eq6xEclqQUlkwWhkxJxjBS+1JIILtlqig /7cI/82/AQWUkNqlCGI+fs9p4muR2yjQX8LNlwpoloHc6GV0MNCeR7oTzAsJ5QSQ1JXtV5 HfJWFF5ZBDhLS8BNl5ti9wKXHLXXNb2k5oHmofbC1NaHi/JesUjE/B/Yuje+vw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1647406383; a=rsa-sha256; cv=none; b=GIj/13QxWmqH+7SNiWSseQ2Cahy4c+mmLrYvkEWWRlokd00k+Bta9VLLdkN/ppmUvFNEYy VCEvxP2ZK1lagi+cereEmpYZdUzokRqXWp+L++QTrcsGZFXnnY6H5+Mmjstyt60NI46EFu IcavJSWey1kzc+3wKewfXascAEqimJwSth4cXbhoswKhS0krwLqhooovacbFp3KN/Z+mzm A0e+Aj7s8hDGUWq+CsRsTbh6YZrqxd9+idWtBT/vjHYMTrpZqEHm0+kvW1ml83KGCwtfcD fCbd6K8nf3M72LnKJHuyzIk41T7knMyfttg4pBoeGfpwYwz9uDmEa9OkeG2Z2A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by eugen: URL: https://cgit.FreeBSD.org/src/commit/?id=adbf7727b3a2aad3c2faa6e543ee7fa7a6c9a3d5 commit adbf7727b3a2aad3c2faa6e543ee7fa7a6c9a3d5 Author: Eugene Grosbein AuthorDate: 2022-03-16 04:41:51 +0000 Commit: Eugene Grosbein CommitDate: 2022-03-16 04:41:51 +0000 virtio_random(8): avoid deadlock at shutdown time FreeBSD 13+ running as virtual guest may load virtio_random(8) driver by means of devd(8) unless the driver is blacklisted or disabled via device.hints(5). Currently, the driver may prevent the system from rebooting or shutting down correctly. This change deactivates virtio_random at very late stage during system shutdown sequence to avoid deadlock that results in kernel hang. PR: 253175 Tested by: tom MFC after: 3 days --- sys/dev/virtio/random/virtio_random.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/sys/dev/virtio/random/virtio_random.c b/sys/dev/virtio/random/virtio_random.c index a8553ecab287..a95dcadcddcd 100644 --- a/sys/dev/virtio/random/virtio_random.c +++ b/sys/dev/virtio/random/virtio_random.c @@ -32,6 +32,8 @@ __FBSDID("$FreeBSD$"); #include +#include +#include #include #include #include @@ -52,6 +54,8 @@ struct vtrnd_softc { device_t vtrnd_dev; uint64_t vtrnd_features; struct virtqueue *vtrnd_vq; + eventhandler_tag eh; + bool inactive; }; static int vtrnd_modevent(module_t, int, void *); @@ -59,6 +63,7 @@ static int vtrnd_modevent(module_t, int, void *); static int vtrnd_probe(device_t); static int vtrnd_attach(device_t); static int vtrnd_detach(device_t); +static int vtrnd_shutdown(device_t); static int vtrnd_negotiate_features(struct vtrnd_softc *); static int vtrnd_setup_features(struct vtrnd_softc *); @@ -86,6 +91,7 @@ static device_method_t vtrnd_methods[] = { DEVMETHOD(device_probe, vtrnd_probe), DEVMETHOD(device_attach, vtrnd_attach), DEVMETHOD(device_detach, vtrnd_detach), + DEVMETHOD(device_shutdown, vtrnd_shutdown), DEVMETHOD_END }; @@ -160,6 +166,16 @@ vtrnd_attach(device_t dev) error = EEXIST; goto fail; } + + sc->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, + vtrnd_shutdown, dev, SHUTDOWN_PRI_LAST + 1); /* ??? */ + if (sc->eh == NULL) { + device_printf(dev, "Shutdown event registration failed\n"); + error = ENXIO; + goto fail; + } + + sc->inactive = false; random_source_register(&random_vtrnd); fail: @@ -179,11 +195,27 @@ vtrnd_detach(device_t dev) atomic_load_explicit(&g_vtrnd_softc, memory_order_acquire) == sc, ("only one global instance at a time")); + sc->inactive = true; + if (sc->eh != NULL) { + EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->eh); + sc->eh = NULL; + } random_source_deregister(&random_vtrnd); atomic_store_explicit(&g_vtrnd_softc, NULL, memory_order_release); return (0); } +static int +vtrnd_shutdown(device_t dev) +{ + struct vtrnd_softc *sc; + + sc = device_get_softc(dev); + sc->inactive = true; + + return(0); +} + static int vtrnd_negotiate_features(struct vtrnd_softc *sc) { @@ -235,6 +267,9 @@ vtrnd_harvest(struct vtrnd_softc *sc, void *buf, size_t *sz) _Static_assert(sizeof(value) < PAGE_SIZE, "sglist assumption"); + if (sc->inactive) + return (EDEADLK); + sglist_init(&sg, 1, segs); error = sglist_append(&sg, value, *sz); if (error != 0)