git: 09af4bf2c987 - main - zfs: Fix SPA sysctl handlers

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 01 Jan 2024 19:00:06 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=09af4bf2c987f6f57804162cef8aeee05575ad1d

commit 09af4bf2c987f6f57804162cef8aeee05575ad1d
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-01-01 18:54:15 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-01-01 18:59:03 +0000

    zfs: Fix SPA sysctl handlers
    
    sbuf_cpy() resets the sbuf state, which is wrong for sbufs allocated by
    sbuf_new_for_sysctl().  In particular, this code triggers an assertion
    failure in sbuf_clear().
    
    Simplify by just using sysctl_handle_string() for both reading and
    setting the tunable.
    
    Apply to FreeBSD directly since this bug causes "sysctl -a" to crash the
    kernel.
    
    PR:             276039
    Reported by:    pho
    Reviewed by:    mav
    Pull Request:   https://github.com/openzfs/zfs/pull/15719
---
 sys/contrib/openzfs/module/zfs/spa.c | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)

diff --git a/sys/contrib/openzfs/module/zfs/spa.c b/sys/contrib/openzfs/module/zfs/spa.c
index a21b0decf6a3..dc8a99e7bd25 100644
--- a/sys/contrib/openzfs/module/zfs/spa.c
+++ b/sys/contrib/openzfs/module/zfs/spa.c
@@ -1445,8 +1445,6 @@ spa_taskq_write_param_get(char *buf, zfs_kernel_param_t *kp)
 	return (spa_taskq_param_get(ZIO_TYPE_WRITE, buf));
 }
 #else
-#include <sys/sbuf.h>
-
 /*
  * On FreeBSD load-time parameters can be set up before malloc() is available,
  * so we have to do all the parsing work on the stack.
@@ -1457,19 +1455,11 @@ static int
 spa_taskq_read_param(ZFS_MODULE_PARAM_ARGS)
 {
 	char buf[SPA_TASKQ_PARAM_MAX];
-	int err = 0;
-
-	if (req->newptr == NULL) {
-		int len = spa_taskq_param_get(ZIO_TYPE_READ, buf);
-		struct sbuf *s = sbuf_new_for_sysctl(NULL, NULL, len+1, req);
-		sbuf_cpy(s, buf);
-		err = sbuf_finish(s);
-		sbuf_delete(s);
-		return (err);
-	}
+	int err;
 
+	(void) spa_taskq_param_get(ZIO_TYPE_READ, buf);
 	err = sysctl_handle_string(oidp, buf, sizeof (buf), req);
-	if (err)
+	if (err || req->newptr == NULL)
 		return (err);
 	return (spa_taskq_param_set(ZIO_TYPE_READ, buf));
 }
@@ -1478,19 +1468,11 @@ static int
 spa_taskq_write_param(ZFS_MODULE_PARAM_ARGS)
 {
 	char buf[SPA_TASKQ_PARAM_MAX];
-	int err = 0;
-
-	if (req->newptr == NULL) {
-		int len = spa_taskq_param_get(ZIO_TYPE_WRITE, buf);
-		struct sbuf *s = sbuf_new_for_sysctl(NULL, NULL, len+1, req);
-		sbuf_cpy(s, buf);
-		err = sbuf_finish(s);
-		sbuf_delete(s);
-		return (err);
-	}
+	int err;
 
+	(void) spa_taskq_param_get(ZIO_TYPE_WRITE, buf);
 	err = sysctl_handle_string(oidp, buf, sizeof (buf), req);
-	if (err)
+	if (err || req->newptr == NULL)
 		return (err);
 	return (spa_taskq_param_set(ZIO_TYPE_WRITE, buf));
 }