From nobody Sat May 4 13:38:57 2024 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=1638868804; cv=none; d=zohomail.com; s=zohoarc; b=Bgd7NWswMhY5GbAbAB5u1j9vVXt7F8SLaOfph3ZEqwBH4tfsNm1CJvdpBYOuXMlkbtpqjLPrNFvbW1xN38H5fjhNN2PbrEVG4o86mxmeLHdVcwuP0gACAMj7AKRGl8/h+p4AA+9jnyE5TyQjmMKMyA+HNzfUIf50yNn3UVDVOJA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1638868804; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=u1/nm7Qs8667WGSR5vwcXwwAQk3k/bIGVQWL424Yej0=; b=nDz5rorHpYwsdKWUJFE0nydmCiXnvuA+simKC/4Ke6dv6oo+4oqtopsrqhz7jHNWcKraxakvNUSCSG+6NDTcDIe/VeBsvwh/3LVaF2QL7hXQZHQ6wVADnIz9PcHUR4F9adNBHOPxq+fyLOqqMjyE3CVF0V8wCxpOH2Z6rZKYVrA= 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 1638868804832687.4381311379055; Tue, 7 Dec 2021 01:20:04 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-434-iGbZU8vSPXe_dRllQdXWyA-1; Tue, 07 Dec 2021 04:20:01 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AFD8F190D340; Tue, 7 Dec 2021 09:19:56 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 476895DF34; Tue, 7 Dec 2021 09:19:54 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 877EB4BB7C; Tue, 7 Dec 2021 09:19:50 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1B79JnpY022280 for ; Tue, 7 Dec 2021 04:19:49 -0500 Received: by smtp.corp.redhat.com (Postfix) id F18795D740; Tue, 7 Dec 2021 09:19:48 +0000 (UTC) Received: from virval.usersys.redhat.com (unknown [10.43.2.187]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BDD975D6CF for ; Tue, 7 Dec 2021 09:19:45 +0000 (UTC) Received: by virval.usersys.redhat.com (Postfix, from userid 500) id 0AEFD243CC3; Tue, 7 Dec 2021 10:19:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1638868803; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=u1/nm7Qs8667WGSR5vwcXwwAQk3k/bIGVQWL424Yej0=; b=animqsVcxU0RX0ASBlN5F8dJrdlllGdku/yRlq8KVWuaLn+pFsur4t/EHdl9tp54dPubj0 PcAZ7GAkpUJ9viv5wsO3lyi4tKEWjy1+pykKU1aW+rKwN9D3ceZgSVyrCWUKr1bO100O9b IEI9I8K+X30z9OugCzKvkjBFM0eaYsw= X-MC-Unique: iGbZU8vSPXe_dRllQdXWyA-1 From: Jiri Denemark To: libvir-list@redhat.com Subject: [libvirt PATCH v2] qemu: Enable unprivileged userfaultfd for post-copy migration Date: Tue, 7 Dec 2021 10:19:42 +0100 Message-Id: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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: 1638868806735100001 Content-Type: text/plain; charset="utf-8" Userfaultfd is by default allowed only for privileged processes. Since libvirt runs QEMU unprivileged, we need to enable unprivileged access to userfaultfd before starting post-copy migration. Rather than providing a static sysctl configuration file, we set the sysctl knob in runtime once post-copy migration is requested. This way unprivileged_userfaultfd is only enabled once actually used. https://bugzilla.redhat.com/show_bug.cgi?id=3D1945420 Signed-off-by: Jiri Denemark Reviewed-by: Peter Krempa --- Notes: Version 2: - setting unprivileged_userfaultfd only when it is not already enabled - virReportSystemError replaced with VIR_WARN src/qemu/qemu_migration_params.c | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/qemu/qemu_migration_params.c b/src/qemu/qemu_migration_par= ams.c index dbc3219826..9ba4811242 100644 --- a/src/qemu/qemu_migration_params.c +++ b/src/qemu/qemu_migration_params.c @@ -804,6 +804,41 @@ qemuMigrationCapsToJSON(virBitmap *caps, } =20 =20 +/** + * qemuMigrationParamsEnableUserfaultfd + * + * Try to enable unprivileged userfaultfd unless it's missing or already + * enabled. Only a warning is logged when we cannot enable it, QEMU will + * report an error when enabling post-copy migration capability. + */ +static void +qemuMigrationParamsEnableUserfaultfd(void) +{ + const char *sysctl =3D "/proc/sys/vm/unprivileged_userfaultfd"; + g_autofree char *buf =3D NULL; + + if (!virFileExists(sysctl)) + return; + + if (virFileReadAll(sysctl, 10, &buf) < 0) { + VIR_WARN("Cannot read unprivileged userfaultfd state"); + return; + } + + if (STREQ(buf, "1\n")) { + VIR_DEBUG("Unprivileged userfaultfd already enabled"); + return; + } + + VIR_DEBUG("Enabling unprivileged userfaultfd for post-copy migration"); + + if (virFileWriteStr(sysctl, "1", 0) < 0) { + VIR_WARN("Failed to enable unprivileged userfaultfd: %s", + g_strerror(errno)); + } +} + + /** * qemuMigrationParamsApply * @driver: qemu driver @@ -839,6 +874,13 @@ qemuMigrationParamsApply(virQEMUDriver *driver, goto cleanup; } } else { + /* userfaultfd may only be enabled for privileged processes by def= ault, + * we need to make sure QEMU can use it before enabling post-copy + * migration */ + if (virBitmapIsBitSet(priv->migrationCaps, QEMU_MIGRATION_CAP_POST= COPY) && + virBitmapIsBitSet(migParams->caps, QEMU_MIGRATION_CAP_POSTCOPY= )) + qemuMigrationParamsEnableUserfaultfd(); + if (!(caps =3D qemuMigrationCapsToJSON(priv->migrationCaps, migPar= ams->caps))) goto cleanup; =20 --=20 2.34.1