From nobody Sun Feb 8 19:25:36 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1652108587; cv=none; d=zohomail.com; s=zohoarc; b=LJokZGYJzZowg1QsZU2UEh2IIdKgmHRUcYWUy8txo7mg7dUp/1+MW+EGI9gvTb681I/AGsWaZAE1skTmB9G4NSvCrinQa9ePC1YQsMt2GXPyo5RGHhOQjnXpW4JDmQX6SgOD2dyRTcB/iuq3x34PHbLjCJJVdjkHuGiTFsVi218= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652108587; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hUtNHZ+LSDRnZQFNQ8cOY7jy3C2iDI7lnXJtAN/NHtM=; b=E1juxuMFskEnvYB5sxmoAULqaKd+tQAUzptR8J+TJQ07UnGSFRzAhBSwAYnR2L5GiZIpxFWEki5ogVKXLJynRcfGxmE5vqvHsSpmxX/zLwFrXKEuYQ8pyuKDvJ2OmBPqU0oIgixgHpaz0XaFIkzybtHnQ1UmBBv0LW3vDpWlRC8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1652108587741481.009284822676; Mon, 9 May 2022 08:03:07 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-502-JDOaws7KNXa8B9lZ8uKOoQ-1; Mon, 09 May 2022 11:02:52 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 68B24382ECD7; Mon, 9 May 2022 15:02:45 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 537FD14C3B46; Mon, 9 May 2022 15:02:45 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id EB3571947070; Mon, 9 May 2022 15:02:44 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 360EC1947040 for ; Mon, 9 May 2022 15:02:43 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 27268111E403; Mon, 9 May 2022 15:02:43 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.180]) by smtp.corp.redhat.com (Postfix) with ESMTP id C2EEF111E3FB for ; Mon, 9 May 2022 15:02:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652108586; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=hUtNHZ+LSDRnZQFNQ8cOY7jy3C2iDI7lnXJtAN/NHtM=; b=GZqLRmZ9yiVbe7b+t9doH2WCSWcyl2Jd1EmjCLDmfMXZKVlBCBg0gfK/+ivQ50M1oa/65D +Y4v3sm1YxhsWhYMykZJXYMo8WiMrdRM19Fy5D0Afaji1Uj533QqnQhSreRZMqh0meLhtk jZpz1w1/PNxf/9YJmEROc2+46Vsk29U= X-MC-Unique: JDOaws7KNXa8B9lZ8uKOoQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH RFC 07/10] virCommand: Introduce APIs for core scheduling Date: Mon, 9 May 2022 17:02:14 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1652108589499100001 Content-Type: text/plain; charset="utf-8"; x-default="true" There are two modes of core scheduling that are handy wrt virCommand: 1) create new trusted group when executing a virCommand 2) place freshly executed virCommand into the trusted group of another process. Therefore, implement these two new operations as new APIs: virCommandSetRunAlone() and virCommandSetRunAmong(), respectively. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 2 ++ src/util/vircommand.c | 74 ++++++++++++++++++++++++++++++++++++++++ src/util/vircommand.h | 5 +++ 3 files changed, 81 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 252d7e029f..8f2b789cee 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2079,6 +2079,8 @@ virCommandSetOutputBuffer; virCommandSetOutputFD; virCommandSetPidFile; virCommandSetPreExecHook; +virCommandSetRunAlone; +virCommandSetRunAmong; virCommandSetSELinuxLabel; virCommandSetSendBuffer; virCommandSetUID; diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 41cf552d7b..db20620f7c 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -148,6 +148,9 @@ struct _virCommand { #endif int mask; =20 + bool schedCore; + pid_t schedCorePID; + virCommandSendBuffer *sendBuffers; size_t numSendBuffers; }; @@ -434,6 +437,22 @@ virCommandHandshakeChild(virCommand *cmd) static int virExecCommon(virCommand *cmd, gid_t *groups, int ngroups) { + /* Do this before dropping capabilities. */ + if (cmd->schedCore && + virProcessSchedCoreCreate() < 0) { + virReportSystemError(errno, "%s", + _("Unable to set SCHED_CORE")); + return -1; + } + + if (cmd->schedCorePID >=3D 0 && + virProcessSchedCoreShareFrom(cmd->schedCorePID) < 0) { + virReportSystemError(errno, + _("Unable to run among %llu"), + (unsigned long long) cmd->schedCorePID); + return -1; + } + if (cmd->uid !=3D (uid_t)-1 || cmd->gid !=3D (gid_t)-1 || cmd->capabilities || (cmd->flags & VIR_EXEC_CLEAR_CAPS)) { VIR_DEBUG("Setting child uid:gid to %d:%d with caps %llx", @@ -964,6 +983,7 @@ virCommandNewArgs(const char *const*args) cmd->pid =3D -1; cmd->uid =3D -1; cmd->gid =3D -1; + cmd->schedCorePID =3D -1; =20 virCommandAddArgSet(cmd, args); =20 @@ -3437,3 +3457,57 @@ virCommandRunNul(virCommand *cmd G_GNUC_UNUSED, return -1; } #endif /* WIN32 */ + +/** + * virCommandSetRunAlone: + * + * Create new trusted group when running the command. In other words, the + * process won't be scheduled to run on a core among with processes from + * another, untrusted group. + */ +void +virCommandSetRunAlone(virCommand *cmd) +{ + if (virCommandHasError(cmd)) + return; + + if (cmd->schedCorePID >=3D 0) { + /* Can't mix these two. */ + cmd->has_error =3D -1; + VIR_DEBUG("cannot mix with virCommandSetRunAmong()"); + return; + } + + cmd->schedCore =3D true; +} + +/** + * virCommandSetRunAmong: + * @pid: pid from a trusted group + * + * When spawning the command place it into the trusted group of @pid so th= at + * these two processes can run on Hyper Threads of a single core at the sa= me + * time. + */ +void +virCommandSetRunAmong(virCommand *cmd, + pid_t pid) +{ + if (virCommandHasError(cmd)) + return; + + if (cmd->schedCore) { + /* Can't mix these two. */ + VIR_DEBUG("cannot mix with virCommandSetRunAlone()"); + cmd->has_error =3D -1; + return; + } + + if (pid < 0) { + VIR_DEBUG("invalid pid value: %lld", (long long) pid); + cmd->has_error =3D -1; + return; + } + + cmd->schedCorePID =3D pid; +} diff --git a/src/util/vircommand.h b/src/util/vircommand.h index 600806a987..0b03ea005c 100644 --- a/src/util/vircommand.h +++ b/src/util/vircommand.h @@ -225,4 +225,9 @@ int virCommandRunNul(virCommand *cmd, virCommandRunNulFunc func, void *data); =20 +void virCommandSetRunAlone(virCommand *cmd); + +void virCommandSetRunAmong(virCommand *cmd, + pid_t pid); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCommand, virCommandFree); --=20 2.35.1