From nobody Wed Oct 19 16:40: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 4MsxKb3JDMz4gNCd; Wed, 19 Oct 2022 16:40: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 4MsxKb2rcWz3ch2; Wed, 19 Oct 2022 16:40:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666197603; 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=eO4G8Z6DR1+dljn5DOCDHaHc1814xOHqcDmb8pBGzeM=; b=qXBlHQ0ZuIzdf9JbRUYwpnbMCLNCMy3f9c3o7+C1SJ4EckpyL8/g8UV4dH+qQtDDJGPML7 ofgHIawtSkelFoL6QAF+Ez9oqxjw/pQXQ0R4egM9XbFDYr0sHqYJppQy09Xez0ffmm4zMx uEH6I+oD/4DGQi9hE34s6oTGQDBRIQUufzs+BkwGpI2ApRlNTeYE2uYZvFL/kT/f1gLx0g 3gmpSv65yNqHVd1SSRJQSvWVFa4yzjWZTTvYGAxI6HtQdC3m+RSkgfkS9ide7SsG4Xv0FD X+KytBHwNKTaag9QnB99q6JEYJswaTDh1SaGreLDDq5PtLzBnIDaBOOAlGEiNg== 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 4MsxKb1yWbzb2S; Wed, 19 Oct 2022 16:40: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 29JGe3u8014253; Wed, 19 Oct 2022 16:40:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 29JGe33Q014251; Wed, 19 Oct 2022 16:40:03 GMT (envelope-from git) Date: Wed, 19 Oct 2022 16:40:03 GMT Message-Id: <202210191640.29JGe33Q014251@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 555a861d6826 - main - device_get_path(): take sbuf directly 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: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 555a861d6826eca8710e361ee4d0ef29b28f39d2 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666197603; 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=eO4G8Z6DR1+dljn5DOCDHaHc1814xOHqcDmb8pBGzeM=; b=sfJz1R6kxBVW0pqX3kPMW0ADnom1eSMAsMoLo8CoHaYqAYILxLRDhEwV64RPRA3lwNHMUM QnL3ghQEdYV4BZa5Gw9flis4ShDeO1TvBm3vncUMKeL4SCkyv9SA23L+NiZm+kG0ZkxT/a kNLTWldNeoIWxWurZedgxZ7Q6Zyezg5tO7Qnqz2NbRPtcTYsQKyasxeBfin0g/FOUoLL/I 5gIL668vjU1zuKF/aBh1WpX4G3emzSp3M4hCjJaGkw9W2jtBT6MUAW8AK/uYbZ1BLHrn2q MqwwFefrwK3uPNBjRTiOFlo0+fzwO/7nDC+1HwQED1P4o6vIWTkyi2sTCLCckA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1666197603; a=rsa-sha256; cv=none; b=Gi9X6DZmcdZ7sBtvYB47vcIgxbgpvTCiroHwcD4THwdtDKyZfo9Wm6Twd69GYC52yGq00d SgLTVzxaLxqN1MgGy+mbWafKHC/G/PDOADIUV5koMooDaWE8uriFFH4ZXy5d8gaO1rVU1o 3a60Mt8PwvhEqW0KlySKCGjMwYOj1RKqwPJTj22DhSlB5efdQGax6hQhs6ugZyiISWYp6n b1pt3yggXYFqH7V+NuKt4NxqVBZnWmwv8DALF8yAkfxQ5GHETGrCp5whWHypkQJv/pqlFJ oxmR0Czg73YkZLCYLjM0Qjh954jhV2B1/uxFliC4Wzs33ei5i+GG7RNR8Dg14Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=555a861d6826eca8710e361ee4d0ef29b28f39d2 commit 555a861d6826eca8710e361ee4d0ef29b28f39d2 Author: Konstantin Belousov AuthorDate: 2022-10-08 00:08:14 +0000 Commit: Konstantin Belousov CommitDate: 2022-10-19 16:39:40 +0000 device_get_path(): take sbuf directly This allows to fix a bug where sbuf allocation done in the context of dev_wired_cache_match() must use non-sleepable allocations. Suggested by: jhb Reviewed by: jhb, takawata Discussed with: imp Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D36899 --- sys/kern/subr_bus.c | 73 +++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index 74bc47174d8d..5c165419af2d 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -5305,37 +5305,24 @@ device_do_deferred_actions(void) } static int -device_get_path(device_t dev, const char *locator, char **rvp) +device_get_path(device_t dev, const char *locator, struct sbuf *sb) { - struct sbuf *sb; - char *s; device_t parent; - ssize_t len; int error; + sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND | SBUF_INCLUDENUL); parent = device_get_parent(dev); if (parent == NULL) { - *rvp = strdup_flags("/", M_BUS, M_NOWAIT); - return (*rvp == NULL ? ENOMEM : 0); - } - sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND | SBUF_INCLUDENUL); - error = BUS_GET_DEVICE_PATH(parent, dev, locator, sb); - sbuf_finish(sb); /* Note: errors checked with sbuf_len() below */ - if (error == 0) { - len = sbuf_len(sb); - if (len <= 1) { - error = EIO; - } else { - s = malloc(len, M_BUS, M_NOWAIT); - if (s == NULL) { - error = ENOMEM; - } else { - memcpy(s, sbuf_data(sb), len); - *rvp = s; - } + error = sbuf_printf(sb, "/"); + } else { + error = BUS_GET_DEVICE_PATH(parent, dev, locator, sb); + if (error == 0) { + error = sbuf_error(sb); + if (error == 0 && sbuf_len(sb) <= 1) + error = EIO; } } - sbuf_delete(sb); + sbuf_finish(sb); return (error); } @@ -5599,25 +5586,28 @@ devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, req->dr_flags); break; case DEV_GET_PATH: { + struct sbuf *sb; char locator[64]; - char *path; ssize_t len; error = copyinstr(req->dr_buffer.buffer, locator, sizeof(locator), NULL); if (error != 0) break; - error = device_get_path(dev, locator, &path); - if (error != 0) - break; - len = strlen(path) + 1; - if (req->dr_buffer.length < len) { - error = ENAMETOOLONG; - } else { - error = copyout(path, req->dr_buffer.buffer, len); + sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND | + SBUF_INCLUDENUL /* | SBUF_WAITOK */); + error = device_get_path(dev, locator, sb); + if (error == 0) { + len = sbuf_len(sb); + if (req->dr_buffer.length < len) { + error = ENAMETOOLONG; + } else { + error = copyout(sbuf_data(sb), + req->dr_buffer.buffer, len); + } + req->dr_buffer.length = len; } - req->dr_buffer.length = len; - free(path, M_BUS); + sbuf_delete(sb); break; } } @@ -5712,8 +5702,8 @@ bool dev_wired_cache_match(device_location_cache_t *dcp, device_t dev, const char *at) { + struct sbuf *sb; const char *cp; - char *path; char locator[32]; int error, len; struct device_location_node *res; @@ -5732,9 +5722,16 @@ dev_wired_cache_match(device_location_cache_t *dcp, device_t dev, /* maybe cache this inside device_t and look that up, but not yet */ res = dev_wired_cache_lookup(dcp, locator); if (res == NULL) { - error = device_get_path(dev, locator, &path); - if (error == 0) - res = dev_wired_cache_add(dcp, locator, path); + sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND | + SBUF_INCLUDENUL | SBUF_NOWAIT); + if (sb != NULL) { + error = device_get_path(dev, locator, sb); + if (error == 0) { + res = dev_wired_cache_add(dcp, locator, + sbuf_data(sb)); + } + sbuf_delete(sb); + } } if (error != 0 || res == NULL || res->dln_path == NULL) return (false);