git: 1d640e613584 - stable/13 - Implement GET_STACK_USAGE on remaining archs

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Tue, 07 Dec 2021 18:14:28 UTC
The branch stable/13 has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=1d640e61358469c17fb0ce340f78104a50b26959

commit 1d640e61358469c17fb0ce340f78104a50b26959
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2021-11-25 16:01:11 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2021-12-07 18:13:47 +0000

    Implement GET_STACK_USAGE on remaining archs
    
    This definition enables callers to estimate remaining space on the
    kstack, and take action on it. Notably, it enables optimizations in the
    GEOM and netgraph subsystems to directly dispatch work items when there
    is sufficient stack space, rather than queuing them for a worker thread.
    
    Implement it for riscv, arm, and mips. Remove the #ifdefs, so it will
    not go unimplemented elsewhere.
    
    PR:             259157
    Reviewed by:    mav, kib, markj (previous version)
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D32580
    
    (cherry picked from commit 0d2224733e970aaa67a4e1af7b340044adda92f6)
---
 sys/arm/include/proc.h   | 11 +++++++++++
 sys/geom/geom_io.c       |  8 --------
 sys/mips/include/proc.h  | 11 +++++++++++
 sys/netgraph/ng_base.c   |  3 +--
 sys/riscv/include/proc.h | 11 +++++++++++
 5 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/sys/arm/include/proc.h b/sys/arm/include/proc.h
index cdd64479c851..9e3cdf142205 100644
--- a/sys/arm/include/proc.h
+++ b/sys/arm/include/proc.h
@@ -71,4 +71,15 @@ struct syscall_args {
 	register_t args[MAXARGS];
 } __aligned(8);
 
+#ifdef _KERNEL
+#include <machine/pcb.h>
+
+/* Get the current kernel thread stack usage. */
+#define	GET_STACK_USAGE(total, used) do {				\
+	struct thread *td = curthread;					\
+	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
+	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
+} while (0)
+
+#endif  /* _KERNEL */
 #endif /* !_MACHINE_PROC_H_ */
diff --git a/sys/geom/geom_io.c b/sys/geom/geom_io.c
index 31213f0f2b22..2536549762f3 100644
--- a/sys/geom/geom_io.c
+++ b/sys/geom/geom_io.c
@@ -559,7 +559,6 @@ g_io_request(struct bio *bp, struct g_consumer *cp)
 	atomic_add_int(&cp->nstart, 1);
 #endif
 
-#ifdef GET_STACK_USAGE
 	direct = (cp->flags & G_CF_DIRECT_SEND) != 0 &&
 	    (pp->flags & G_PF_DIRECT_RECEIVE) != 0 &&
 	    !g_is_geom_thread(curthread) &&
@@ -573,9 +572,6 @@ g_io_request(struct bio *bp, struct g_consumer *cp)
 		if (su * 2 > st)
 			direct = 0;
 	}
-#else
-	direct = 0;
-#endif
 
 	if (direct) {
 		error = g_io_check(bp);
@@ -655,7 +651,6 @@ g_io_deliver(struct bio *bp, int error)
 	bp->bio_bcount = bp->bio_length;
 	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
 
-#ifdef GET_STACK_USAGE
 	direct = (pp->flags & G_PF_DIRECT_SEND) &&
 		 (cp->flags & G_CF_DIRECT_RECEIVE) &&
 		 !g_is_geom_thread(curthread);
@@ -666,9 +661,6 @@ g_io_deliver(struct bio *bp, int error)
 		if (su * 2 > st)
 			direct = 0;
 	}
-#else
-	direct = 0;
-#endif
 
 	/*
 	 * The statistics collection is lockless, as such, but we
diff --git a/sys/mips/include/proc.h b/sys/mips/include/proc.h
index 0cb1d433387c..8f6f46b86513 100644
--- a/sys/mips/include/proc.h
+++ b/sys/mips/include/proc.h
@@ -95,4 +95,15 @@ struct syscall_args {
 #define	KINFO_PROC_SIZE 816
 #endif
 
+#ifdef _KERNEL
+#include <machine/pcb.h>
+
+/* Get the current kernel thread stack usage. */
+#define	GET_STACK_USAGE(total, used) do {				\
+	struct thread *td = curthread;					\
+	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
+	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
+} while (0)
+
+#endif  /* _KERNEL */
 #endif	/* !_MACHINE_PROC_H_ */
diff --git a/sys/netgraph/ng_base.c b/sys/netgraph/ng_base.c
index ba60acb53502..7f3b103aeaeb 100644
--- a/sys/netgraph/ng_base.c
+++ b/sys/netgraph/ng_base.c
@@ -2281,7 +2281,7 @@ ng_snd_item(item_p item, int flags)
 		queue = 1;
 	} else {
 		queue = 0;
-#ifdef GET_STACK_USAGE
+
 		/*
 		 * Most of netgraph nodes have small stack consumption and
 		 * for them 25% of free stack space is more than enough.
@@ -2296,7 +2296,6 @@ ng_snd_item(item_p item, int flags)
 		    ((node->nd_flags & NGF_HI_STACK) || (hook &&
 		    (hook->hk_flags & HK_HI_STACK)))))
 			queue = 1;
-#endif
 	}
 
 	if (queue) {
diff --git a/sys/riscv/include/proc.h b/sys/riscv/include/proc.h
index 4b5ae9ebe3ed..b0497a4523f2 100644
--- a/sys/riscv/include/proc.h
+++ b/sys/riscv/include/proc.h
@@ -52,4 +52,15 @@ struct syscall_args {
 	register_t args[MAXARGS];
 };
 
+#ifdef _KERNEL
+#include <machine/pcb.h>
+
+/* Get the current kernel thread stack usage. */
+#define	GET_STACK_USAGE(total, used) do {				\
+	struct thread *td = curthread;					\
+	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
+	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
+} while (0)
+
+#endif  /* _KERNEL */
 #endif /* !_MACHINE_PROC_H_ */