git: 8f6c577c9f70 - main - bhyve(8): allow cpu pinning using N-M:X-Y ranges
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 05 Jun 2026 16:11:38 UTC
The branch main has been updated by bnovkov:
URL: https://cgit.FreeBSD.org/src/commit/?id=8f6c577c9f706aea6f138fa1bec27029d4ab587d
commit 8f6c577c9f706aea6f138fa1bec27029d4ab587d
Author: Antranig Vartanian <antranigv@freebsd.am>
AuthorDate: 2026-06-05 15:34:18 +0000
Commit: Bojan Novković <bnovkov@FreeBSD.org>
CommitDate: 2026-06-05 16:07:49 +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
---
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 10018d082f7e..7d82784131b4 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"
@@ -116,7 +117,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 538d031fd1a4..98a756551272 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"
@@ -108,7 +109,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 994f0f1fef21..b5e7a4709fb4 100644
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -357,18 +357,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);
@@ -395,6 +389,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 a90a508901bc..adbba6bcce60 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"
@@ -110,7 +111,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);
}