git: 3d4b9e5fa16a - stable/13 - extres/clk: Improve sysctl dump of clocks.

From: Michal Meloun <mmel_at_FreeBSD.org>
Date: Thu, 20 Jan 2022 10:17:42 UTC
The branch stable/13 has been updated by mmel:

URL: https://cgit.FreeBSD.org/src/commit/?id=3d4b9e5fa16a7e7e72fb2573affb6365e5ba251e

commit 3d4b9e5fa16a7e7e72fb2573affb6365e5ba251e
Author:     Michal Meloun <mmel@FreeBSD.org>
AuthorDate: 2021-12-24 07:38:32 +0000
Commit:     Michal Meloun <mmel@FreeBSD.org>
CommitDate: 2022-01-20 10:14:04 +0000

    extres/clk: Improve sysctl dump of clocks.
    
    Always recalculate the frequency, the cache is lazily initialized so it is not always up to date.
    While I'm in mark sysctl as MPSAFE.
    
    Discussed with: manu, adrian
    MFC after:      1 week
    
    (cherry picked from commit 72a2f3b5e28ada60de01f08b28888f70eec0baed)
---
 sys/dev/extres/clk/clk.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/sys/dev/extres/clk/clk.c b/sys/dev/extres/clk/clk.c
index b1522eb1d4c2..839f0f842a78 100644
--- a/sys/dev/extres/clk/clk.c
+++ b/sys/dev/extres/clk/clk.c
@@ -185,6 +185,7 @@ enum clknode_sysctl_type {
 	CLKNODE_SYSCTL_PARENT,
 	CLKNODE_SYSCTL_PARENTS_LIST,
 	CLKNODE_SYSCTL_CHILDREN_LIST,
+	CLKNODE_SYSCTL_FREQUENCY,
 };
 
 static int clknode_sysctl(SYSCTL_HANDLER_ARGS);
@@ -403,7 +404,7 @@ clkdom_create(device_t dev)
 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
 	    OID_AUTO, "clocks",
-	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
 	    clkdom, 0, clkdom_sysctl, "A",
 	    "Clock list for the domain");
 
@@ -624,7 +625,7 @@ clknode_create(struct clkdom * clkdom, clknode_class_t clknode_class,
 	clknode->parent_idx = CLKNODE_IDX_NONE;
 
 	if (replaced)
-			return (clknode);
+		return (clknode);
 
 	sysctl_ctx_init(&clknode->sysctl_ctx);
 	clknode_oid = SYSCTL_ADD_NODE(&clknode->sysctl_ctx,
@@ -632,28 +633,31 @@ clknode_create(struct clkdom * clkdom, clknode_class_t clknode_class,
 	    OID_AUTO, clknode->name,
 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "A clock node");
 
-	SYSCTL_ADD_U64(&clknode->sysctl_ctx,
+	SYSCTL_ADD_PROC(&clknode->sysctl_ctx,
 	    SYSCTL_CHILDREN(clknode_oid),
 	    OID_AUTO, "frequency",
-	    CTLFLAG_RD, &clknode->freq, 0, "The clock frequency");
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
+	    clknode, CLKNODE_SYSCTL_FREQUENCY, clknode_sysctl,
+	    "A",
+	    "The clock frequency");
 	SYSCTL_ADD_PROC(&clknode->sysctl_ctx,
 	    SYSCTL_CHILDREN(clknode_oid),
 	    OID_AUTO, "parent",
-	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
 	    clknode, CLKNODE_SYSCTL_PARENT, clknode_sysctl,
 	    "A",
 	    "The clock parent");
 	SYSCTL_ADD_PROC(&clknode->sysctl_ctx,
 	    SYSCTL_CHILDREN(clknode_oid),
 	    OID_AUTO, "parents",
-	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
 	    clknode, CLKNODE_SYSCTL_PARENTS_LIST, clknode_sysctl,
 	    "A",
 	    "The clock parents list");
 	SYSCTL_ADD_PROC(&clknode->sysctl_ctx,
 	    SYSCTL_CHILDREN(clknode_oid),
 	    OID_AUTO, "childrens",
-	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
 	    clknode, CLKNODE_SYSCTL_CHILDREN_LIST, clknode_sysctl,
 	    "A",
 	    "The clock childrens list");
@@ -1612,6 +1616,7 @@ clknode_sysctl(SYSCTL_HANDLER_ARGS)
 	enum clknode_sysctl_type type = arg2;
 	struct sbuf *sb;
 	const char **parent_names;
+	uint64_t freq;
 	int ret, i;
 
 	clknode = arg1;
@@ -1635,6 +1640,13 @@ clknode_sysctl(SYSCTL_HANDLER_ARGS)
 			sbuf_printf(sb, "%s ", children->name);
 		}
 		break;
+	case CLKNODE_SYSCTL_FREQUENCY:
+		ret = clknode_get_freq(clknode, &freq);
+		if (ret == 0)
+			sbuf_printf(sb, "%ju ", (uintmax_t)freq);
+		else
+			sbuf_printf(sb, "Error: %d ", ret);
+		break;
 	}
 	CLK_TOPO_UNLOCK();