git: f760059545b1 - stable/15 - bhyve(8): allow cpu pinning using N-M:X-Y ranges

Bojan Novković <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src,gmane.os.freebsd.devel.stable.scm
Message-ID <[email protected]>
The branch stable/15 has been updated by bnovkov:

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

commit f760059545b178313dec7bc9528016d7520bac6d
Author:     Antranig Vartanian <[email protected]>
AuthorDate: 2026-06-05 15:34:18 +0000
Commit:     Bojan Novković <[email protected]>
CommitDate: 2026-08-10 16:04:18 +0000

    bhyve(8): allow cpu pinning using N-M:X-Y ranges
    
    bhyve's -p allows to pin guest's virtual CPU vcpu to hostcpu, however
    this becomes very tedious work when you have to pin more than a single
    CPU.
    
    This allows to pass a range to -p, e.g. -p 0-3:4-7 which will pin the
    cpus 0:4, 1:5, 2:6, 3:7. The ranges must be equal and the CPU numbers
    must be ascending.
    
    Sponsored by: Armenian Bioinformatics Institute
    Reviewed by:    corvink, markj
    Tested by:      bnovkov
    MFC after:      3 weeks
    Differential Revision:  https://reviews.freebsd.org/D54937
    
    (cherry picked from commit 8f6c577c9f706aea6f138fa1bec27029d4ab587d)
---
 usr.sbin/bhyve/aarch64/bhyverun_machdep.c |  5 ++--
 usr.sbin/bhyve/amd64/bhyverun_machdep.c   |  5 ++--
 usr.sbin/bhyve/bhyverun.c                 | 38 ++++++++++++++++++++++++-------
 usr.sbin/bhyve/riscv/bhyverun_machdep.c   |  5 ++--
 4 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/usr.sbin/bhyve/aarch64/bhyverun_machdep.c b/usr.sbin/bhyve/aarch64/bhyverun_machdep.c
index 06fe85f96e0a..b1dd906da254 100644
--- a/usr.sbin/bhyve/aarch64/bhyverun_machdep.c
+++ b/usr.sbin/bhyve/aarch64/bhyverun_machdep.c
@@ -100,7 +100,8 @@ bhyve_usage(int code)
 	    "Usage: %s [-CDHhSW]\n"
 	    "       %*s [-c [[cpus=]numcpus][,sockets=n][,cores=n][,threads=n]]\n"
 	    "       %*s [-k config_file] [-m mem] [-o var=value]\n"
-	    "       %*s [-p vcpu:hostcpu] [-r file] [-s pci] [-U uuid] vmname\n"
+	    "       %*s [-p vcpuN[-vcpuM]]:hostcpuX[-hostcpuY]\n"
+	    "       %*s [-r file] [-s pci] [-U uuid] vmname\n"
 	    "       -C: include guest memory in core file\n"
 	    "       -c: number of CPUs and/or topology specification\n"
 	    "       -D: destroy on power-off\n"
@@ -115,7 +116,7 @@ bhyve_usage(int code)
 	    "       -U: UUID\n"
 	    "       -W: force virtio to use single-vector MSI\n",
 	    progname, (int)strlen(progname), "", (int)strlen(progname), "",
-	    (int)strlen(progname), "");
+	    (int)strlen(progname), "", (int)strlen(progname), "");
 	exit(code);
 }
 
diff --git a/usr.sbin/bhyve/amd64/bhyverun_machdep.c b/usr.sbin/bhyve/amd64/bhyverun_machdep.c
index dad8f1e52e4e..a31c1512dbc3 100644
--- a/usr.sbin/bhyve/amd64/bhyverun_machdep.c
+++ b/usr.sbin/bhyve/amd64/bhyverun_machdep.c
@@ -78,7 +78,8 @@ bhyve_usage(int code)
 	    "Usage: %s [-aCDeHhPSuWwxY]\n"
 	    "       %*s [-c [[cpus=]numcpus][,sockets=n][,cores=n][,threads=n]]\n"
 	    "       %*s [-G port] [-k config_file] [-l lpc] [-m mem] [-o var=value]\n"
-	    "       %*s [-p vcpu:hostcpu] [-r file] [-s pci] [-U uuid] vmname\n"
+	    "       %*s [-p vcpuN[-vcpuM]]:hostcpuX[-hostcpuY]\n"
+	    "       %*s [-r file] [-s pci] [-U uuid] vmname\n"
 	    "       -a: local apic is in xAPIC mode (deprecated)\n"
 	    "       -C: include guest memory in core file\n"
 	    "       -c: number of CPUs and/or topology specification\n"
@@ -107,7 +108,7 @@ bhyve_usage(int code)
 	    "       -x: local APIC is in x2APIC mode\n"
 	    "       -Y: disable MPtable generation\n",
 	    progname, (int)strlen(progname), "", (int)strlen(progname), "",
-	    (int)strlen(progname), "");
+	    (int)strlen(progname), "", (int)strlen(progname), "");
 	exit(code);
 }
 
diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
index bfc0b949a75d..d3b5de553fc7 100644
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -355,18 +355,12 @@ calc_topology(void)
 		guest_ncpus = ncpus;
 }
 
-int
-bhyve_pincpu_parse(const char *opt)
+static int
+bhyve_pincpu(int vcpu, int pcpu)
 {
 	const char *value;
 	char *newval;
 	char key[16];
-	int vcpu, pcpu;
-
-	if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
-		fprintf(stderr, "invalid format: %s\n", opt);
-		return (-1);
-	}
 
 	if (vcpu < 0) {
 		fprintf(stderr, "invalid vcpu '%d'\n", vcpu);
@@ -393,6 +387,34 @@ bhyve_pincpu_parse(const char *opt)
 	return (0);
 }
 
+int
+bhyve_pincpu_parse(const char *opt)
+{
+	int vcpu_first, vcpu_last, pcpu_first, pcpu_last;
+	int vcpu, pcpu;
+
+	if (sscanf(opt, "%d-%d:%d-%d", &vcpu_first, &vcpu_last, &pcpu_first, &pcpu_last) == 4) {
+		if (vcpu_first > vcpu_last || pcpu_first > pcpu_last) {
+			fprintf(stderr, "invalid range (must be ascending): %s\n", opt);
+			return (-1);
+		}
+		if ((vcpu_last - vcpu_first) != (pcpu_last - pcpu_first)) {
+			fprintf(stderr, "range sizes do not match: %s\n", opt);
+			return (-1);
+		}
+		for (vcpu = vcpu_first, pcpu = pcpu_first; vcpu <= vcpu_last; vcpu++, pcpu++)
+			if (bhyve_pincpu(vcpu, pcpu) != 0)
+				return (-1);
+		return (0);
+	}
+
+	if (sscanf(opt, "%d:%d", &vcpu, &pcpu) == 2)
+		return (bhyve_pincpu(vcpu, pcpu));
+
+	fprintf(stderr, "invalid format: %s\n", opt);
+	return (-1);
+}
+
 static void
 parse_cpuset(int vcpu, const char *list, cpuset_t *set)
 {
diff --git a/usr.sbin/bhyve/riscv/bhyverun_machdep.c b/usr.sbin/bhyve/riscv/bhyverun_machdep.c
index 8ee505e86679..942044157bbb 100644
--- a/usr.sbin/bhyve/riscv/bhyverun_machdep.c
+++ b/usr.sbin/bhyve/riscv/bhyverun_machdep.c
@@ -95,7 +95,8 @@ bhyve_usage(int code)
 	    "Usage: %s [-CDHhSW]\n"
 	    "       %*s [-c [[cpus=]numcpus][,sockets=n][,cores=n][,threads=n]]\n"
 	    "       %*s [-k config_file] [-m mem] [-o var=value]\n"
-	    "       %*s [-p vcpu:hostcpu] [-r file] [-s pci] [-U uuid] vmname\n"
+	    "       %*s [-p vcpuN[-vcpuM]]:hostcpuX[-hostcpuY]\n"
+	    "       %*s [-r file] [-s pci] [-U uuid] vmname\n"
 	    "       -C: include guest memory in core file\n"
 	    "       -c: number of CPUs and/or topology specification\n"
 	    "       -D: destroy on power-off\n"
@@ -109,7 +110,7 @@ bhyve_usage(int code)
 	    "       -U: UUID\n"
 	    "       -W: force virtio to use single-vector MSI\n",
 	    progname, (int)strlen(progname), "", (int)strlen(progname), "",
-	    (int)strlen(progname), "");
+	    (int)strlen(progname), "", (int)strlen(progname), "");
 	exit(code);
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.