From nobody Thu May 2 09:10:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1582517414; cv=none; d=zohomail.com; s=zohoarc; b=OO+3Yz6uyWJ/KumSLiq1gPjYxrTxjsfEoUTWQ+QtEGSCSbDRxifEjIv9CmDDKRTIrekbOEjKvCGio3vroL3iz3rrr6tAklNjD0TS6m0b2fFw+aZ37A1xrCGLffSS/WwzyJEDW4N/et8pmKqDCvt4DmD3+TRaNl6xxnWxQ508UvM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582517414; h=Content-Type:Content-Transfer-Encoding:Cc: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=r8QAJ/K5WSzX8EQkTjHiFM6T6WSl/z9vpf7UX72Fy4Y=; b=WFb7RBOUx9Pvwss70/wyOZzMpzugWIYOH1N+afdeZnQg/BL/b58/5tTZzJ8iFaa4NfmjPSB16gsNzLAKDL5LbwpIBe43neyJhh7a1edmiEpBcI/Voz+b91YUh1X+o5BYE3TEsQSEPuBKHI9NwpTMSKrkzZKEPCswiXEEZseMTh4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1582517414587999.6245869430427; Sun, 23 Feb 2020 20:10:14 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-338-pfnBuzpTNu2rlqEFA1hB9w-1; Sun, 23 Feb 2020 23:10:11 -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 CE2EB107ACC4; Mon, 24 Feb 2020 04:10:05 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 952BB5D9CD; Mon, 24 Feb 2020 04:10:05 +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 DB27918089CE; Mon, 24 Feb 2020 04:10:04 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 01O4A3Zb015552 for ; Sun, 23 Feb 2020 23:10:03 -0500 Received: by smtp.corp.redhat.com (Postfix) id 3D58BAF988; Mon, 24 Feb 2020 04:10:03 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 38B8AAF98B for ; Mon, 24 Feb 2020 04:10:01 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F20C880066D for ; Mon, 24 Feb 2020 04:10:00 +0000 (UTC) Received: from mail-qk1-f193.google.com (mail-qk1-f193.google.com [209.85.222.193]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-173-7dWiSzYyO5WJFfj3CH7hyg-1; Sun, 23 Feb 2020 23:09:59 -0500 Received: by mail-qk1-f193.google.com with SMTP id u124so7558225qkh.13 for ; Sun, 23 Feb 2020 20:09:59 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id p126sm5355658qkd.108.2020.02.23.20.09.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 20:09:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582517413; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=r8QAJ/K5WSzX8EQkTjHiFM6T6WSl/z9vpf7UX72Fy4Y=; b=OtoEv5bZ2whxF/UswE6Kkf2LABXfwii53K1rmzvdtDo5sumD9crClxv1nANDqBiFcFf0Aa UizXOn4xSM5N2H6ZjRT3qU9eROFj7TCvJUUE6i5xvdURUioUT3QfqcXeyx4xdko54YObKX AZgZ4Si+2EtooUzEalUMafKpTB5afto= X-MC-Unique: pfnBuzpTNu2rlqEFA1hB9w-1 X-MC-Unique: 7dWiSzYyO5WJFfj3CH7hyg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8ms+LnZVHFXemnCQePHWGsYOsN66AV6zwoPKVGBUS1M=; b=hoGfS+bewkE4oxF3PH6+UvSUgBfiXc/QC3j/t+jaSWfhr4KTyL0csS4wbpmVm2zPmu hMOrrUPRur4okmSesPuhLywwQohTSWWjgoKVzgwyvw1W717OUVoSVxCc3QaRN1bixlps pNZxlM6uXeLo9AAftaBcGiT4O23sQqG5HNZhZXIGGMBZfkDTpEiW8Dhr00pK6IN16Cv/ 5+LqFnDLGRjTyBAv8caCEboVxEpyEH2T0l2R3mGR0zgTlm4F736/RIXWSNJZoeSeZz/D i6OYx7Euy6nSCo+dyeFGhwiwgfmuuQuwiB7yXvIs9DGv26nG7adcj8YLsoRJTnKjvJrC Q4gQ== X-Gm-Message-State: APjAAAXUnf+vOTIvZt1isXpgeEpfeGUq8mX5khMtQ5l8maagRPhnCWHo OWahMlMZC2hN5EW0/5b0zR3bR0S9 X-Google-Smtp-Source: APXvYqwlkm6TdXr51yhZ5Sfdtzt6m1Thx0mplT6gyXMBIvsn7b1Rr26+3GSLbntYR0YoA5bR8i3Xbw== X-Received: by 2002:a05:620a:545:: with SMTP id o5mr46255001qko.27.1582517398117; Sun, 23 Feb 2020 20:09:58 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v3 1/5] lxc: Add Real Time Clock device into allowed devices Date: Mon, 24 Feb 2020 01:09:39 -0300 Message-Id: <20200224040942.17695-2-jcfaracco@gmail.com> In-Reply-To: <20200224040942.17695-1-jcfaracco@gmail.com> References: <20200224040942.17695-1-jcfaracco@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 01O4A3Zb015552 X-loop: libvir-list@redhat.com Cc: danielhb413@gmail.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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" This commit share host Real Time Clock device (rtc) into LXC containers to support hardware clock. This should be available setting up a `rtc` timer under clock section. Since this option is not emulated, it should be available only for `localtime` clock. This option should be readonly due to security reasons. Before: root# hwclock --verbose hwclock from util-linux 2.32.1 System Time: 1581877557.598365 Trying to open: /dev/rtc0 Trying to open: /dev/rtc Trying to open: /dev/misc/rtc No usable clock interface found. hwclock: Cannot access the Hardware Clock via any known method. Now: root# hwclock 2020-02-16 18:23:55.374134+00:00 root# hwclock -w hwclock: ioctl(RTC_SET_TIME) to /dev/rtc to set the time failed: Permission denied Signed-off-by: Julio Faracco --- docs/formatdomain.html.in | 2 +- src/lxc/lxc_cgroup.c | 36 +++++++++++++++++++++ src/lxc/lxc_controller.c | 68 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in index 4fef2a0a97..5598bf41b4 100644 --- a/docs/formatdomain.html.in +++ b/docs/formatdomain.html.in @@ -2465,7 +2465,7 @@ being modified, and can be one of "platform" (currently unsupported), "hpet" (libxl, xen, qemu), "kvmclock" (qemu), - "pit" (qemu), "rtc" (qemu), "tsc" (libxl, qemu - + "pit" (qemu), "rtc" (qemu, lxc), "tsc" (libxl, qemu - since 3.2.0), "hypervclock" (qemu - since 1.2.2) or "armvtimer" (qemu - since 6.1.0). diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index 4ebe5ef467..6a103055a4 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -337,6 +337,42 @@ static int virLXCCgroupSetupDeviceACL(virDomainDefPtr = def, VIR_CGROUP_DEVICE_RWM) < 0) return -1; =20 + VIR_DEBUG("Allowing timers char devices"); + + /* Sync'ed with Host clock */ + if (def->clock.offset =3D=3D VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME) { + for (i =3D 0; i < def->clock.ntimers; i++) { + virDomainTimerDefPtr timer =3D def->clock.timers[i]; + + switch ((virDomainTimerNameType)timer->name) { + case VIR_DOMAIN_TIMER_NAME_PLATFORM: + case VIR_DOMAIN_TIMER_NAME_TSC: + case VIR_DOMAIN_TIMER_NAME_KVMCLOCK: + case VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK: + case VIR_DOMAIN_TIMER_NAME_PIT: + case VIR_DOMAIN_TIMER_NAME_HPET: + case VIR_DOMAIN_TIMER_NAME_ARMVTIMER: + case VIR_DOMAIN_TIMER_NAME_LAST: + break; + case VIR_DOMAIN_TIMER_NAME_RTC: + if (!timer->present) + break; + + if (virFileExists("/dev/rtc")) { + if (virCgroupAllowDevicePath(cgroup, "/dev/rtc", + VIR_CGROUP_DEVICE_READ, + false) < 0) + return -1; + } else { + VIR_DEBUG("Ignoring non-existent device /dev/rtc"); + } + break; + } + } + } else { + VIR_DEBUG("Ignoring non-localtime clock"); + } + VIR_DEBUG("Device whitelist complete"); =20 return 0; diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index c3dec0859c..eba6bfe0bf 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -1550,6 +1550,71 @@ static int virLXCControllerPopulateDevices(virLXCCon= trollerPtr ctrl) } =20 =20 +static int +virLXCControllerSetupTimers(virLXCControllerPtr ctrl) +{ + g_autofree char *path =3D NULL; + size_t i; + struct stat sb; + virDomainDefPtr def =3D ctrl->def; + + /* Not sync'ed with Host clock */ + if (def->clock.offset !=3D VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME) + return 0; + + for (i =3D 0; i < def->clock.ntimers; i++) { + dev_t dev; + virDomainTimerDefPtr timer =3D def->clock.timers[i]; + + switch ((virDomainTimerNameType)timer->name) { + case VIR_DOMAIN_TIMER_NAME_PLATFORM: + case VIR_DOMAIN_TIMER_NAME_TSC: + case VIR_DOMAIN_TIMER_NAME_KVMCLOCK: + case VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK: + case VIR_DOMAIN_TIMER_NAME_PIT: + case VIR_DOMAIN_TIMER_NAME_HPET: + case VIR_DOMAIN_TIMER_NAME_ARMVTIMER: + case VIR_DOMAIN_TIMER_NAME_LAST: + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unsupported timer type (name) '%s'"), + virDomainTimerNameTypeToString(timer->name)); + return -1; + case VIR_DOMAIN_TIMER_NAME_RTC: + if (!timer->present) + break; + + if (stat("/dev/rtc", &sb) < 0) { + if (errno =3D=3D EACCES) + return -1; + + virReportSystemError(errno, + _("Path '%s' is not accessible"), + path); + return -1; + } + + path =3D g_strdup_printf("/%s/%s.dev/%s", LXC_STATE_DIR, + ctrl->def->name, "/rtc"); + + dev =3D makedev(major(sb.st_rdev), minor(sb.st_rdev)); + if (mknod(path, S_IFCHR, dev) < 0 || + chmod(path, sb.st_mode)) { + virReportSystemError(errno, + _("Failed to make device %s"), + path); + return -1; + } + + if (lxcContainerChown(ctrl->def, path) < 0) + return -1; + break; + } + } + + return 0; +} + + static int virLXCControllerSetupHostdevSubsysUSB(virDomainDefPtr vmDef, virDomainHostdevDefPtr def, @@ -2352,6 +2417,9 @@ virLXCControllerRun(virLXCControllerPtr ctrl) if (virLXCControllerPopulateDevices(ctrl) < 0) goto cleanup; =20 + if (virLXCControllerSetupTimers(ctrl) < 0) + goto cleanup; + if (virLXCControllerSetupAllDisks(ctrl) < 0) goto cleanup; =20 --=20 2.20.1 From nobody Thu May 2 09:10:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1582517423; cv=none; d=zohomail.com; s=zohoarc; b=DdqDropucriObct+mc6ZB/F9QISskv6AXgHVyhdJNS4cBSWq/8XFn3dgq/Zg5xAWIhw8ms5Qa2EsI4DaCs09Fe7zvydqMVAuVRUcYssNXmq/hxwPYqDioAoNTnW0xB/NhID4NvJyYao3v5ervlaR0gLgc9fD1Jde5fi0ighBBfU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582517423; h=Content-Type:Content-Transfer-Encoding:Cc: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=RnSCunTpqkHkiymR7T6Zo02tC+i/xNNIP+vijgKeGcs=; b=d//8WzuMl4FZ7rAEi2bwTB87EwrEeR4E7IgeA3esxMkV3KZETIeDDDG+sV20uEl31DdvFGgoI+x4BaJzjOgMqZnptz/cpqoBP/LC8GNFC6/AWH/2zU5nM73rRbkvQ6lswbjHYDUC3gJikCP823y14Vq88jRlmeXjobew5fUCe+0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1582517423883928.4484622475769; Sun, 23 Feb 2020 20:10:23 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-456-Be4RWZE2OROSxhxCr4IxIQ-1; Sun, 23 Feb 2020 23:10:20 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 59D358018A1; Mon, 24 Feb 2020 04:10:15 +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 21DE727180; Mon, 24 Feb 2020 04:10:15 +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 9CD86860E6; Mon, 24 Feb 2020 04:10:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 01O4AD7Z015566 for ; Sun, 23 Feb 2020 23:10:13 -0500 Received: by smtp.corp.redhat.com (Postfix) id 41A9B2166B2E; Mon, 24 Feb 2020 04:10:13 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast02.extmail.prod.ext.rdu2.redhat.com [10.11.55.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3CEE22166B2B for ; Mon, 24 Feb 2020 04:10:10 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 93D368007B3 for ; Mon, 24 Feb 2020 04:10:10 +0000 (UTC) Received: from mail-qk1-f196.google.com (mail-qk1-f196.google.com [209.85.222.196]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-398-x3CYJe3qPIaJBtfnm7aXFg-1; Sun, 23 Feb 2020 23:10:08 -0500 Received: by mail-qk1-f196.google.com with SMTP id 145so1426241qkl.2 for ; Sun, 23 Feb 2020 20:10:08 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id p126sm5355658qkd.108.2020.02.23.20.10.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 20:10:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582517422; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=RnSCunTpqkHkiymR7T6Zo02tC+i/xNNIP+vijgKeGcs=; b=a9tRl0NcqqIVZYZr7cIsdld3kmQzddCJL0YnX6Z/zV/Dso1kto1cXk4WGvYwoTv36CfAKb 0P0k3qzkRf8Ov6gzwGAoG1oChIHFrASLWG7vWUEx0MfjVr7fxCLOZx5ASTlZ8JY0lYKHDk jcupgSI6GCVOaZDnq7seQGxyBa171cU= X-MC-Unique: Be4RWZE2OROSxhxCr4IxIQ-1 X-MC-Unique: x3CYJe3qPIaJBtfnm7aXFg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XLMk3zjX9KpwEoOEtgunTvjxcUgvOK/YvW9kntLWM7I=; b=p34lqsol9pYl2unY9n6wU5jxnnj98dHbrE6RmqCZRMObU9ty2kCRZAE52eXvioYAUI zOhnp35HtbyABr+keErbz6fpw9VIclrdls8J0Ho9qNVxr6RbST2pLmbrMhaCdERvwblM 3rH8DbBwTJaWFiR8HXNf5l8nRXaiQyZMy8SnZ0KFxw1u9TWJ+pZ5dJkJZHYDyb5p84C1 YO/K6Ni/QoqaNo3LolDUb9LbBtUMn1VUVBCpc4AXPmVBfwnelgTwIn8VeQC22jQsSX6w I+AEMiiZGtrWWuPeshNYK2/vbX4jOc28aAdb1jHJnSGiZIsw7V4mYVmSQiaIiqfWIs5q jJQw== X-Gm-Message-State: APjAAAXEu9FFBbt/LBthl7yC9J4NDn4iPpTiNWNSY0AaU9Q2YMjKJ0MF 6LQayruMV9NzeaIwjqIjDhPo/7+I X-Google-Smtp-Source: APXvYqxRhW5rP2p0GSvDcIb8ZqOSvLijodv+djfW8p7EtagACodgW7zVOvoa/Nb3mc/q3sO90n/HIQ== X-Received: by 2002:a37:2c41:: with SMTP id s62mr3348836qkh.433.1582517407195; Sun, 23 Feb 2020 20:10:07 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v3 2/5] lxc: Add HPET device into allowed devices Date: Mon, 24 Feb 2020 01:09:41 -0300 Message-Id: <20200224040942.17695-3-jcfaracco@gmail.com> In-Reply-To: <20200224040942.17695-1-jcfaracco@gmail.com> References: <20200224040942.17695-1-jcfaracco@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 01O4AD7Z015566 X-loop: libvir-list@redhat.com Cc: danielhb413@gmail.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.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" This commit is related to RTC timer device too. HPET is being shared from host device through `localtime` clock. This timer is available creating a new timer using `hpet` name. Signed-off-by: Julio Faracco --- docs/formatdomain.html.in | 2 +- src/lxc/lxc_cgroup.c | 17 +++++++++++++---- src/lxc/lxc_controller.c | 33 +++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in index 5598bf41b4..8571db89dc 100644 --- a/docs/formatdomain.html.in +++ b/docs/formatdomain.html.in @@ -2464,7 +2464,7 @@ The name attribute selects which timer is being modified, and can be one of "platform" (currently unsupported), - "hpet" (libxl, xen, qemu), "kvmclock" (qemu), + "hpet" (libxl, xen, qemu, lxc), "kvmclock" (qemu), "pit" (qemu), "rtc" (qemu, lxc), "tsc" (libxl, qemu - since 3.2.0), "hypervclock" (qemu - since 1.2.2) or diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index 6a103055a4..997a5c3dfa 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -344,20 +344,19 @@ static int virLXCCgroupSetupDeviceACL(virDomainDefPtr= def, for (i =3D 0; i < def->clock.ntimers; i++) { virDomainTimerDefPtr timer =3D def->clock.timers[i]; =20 + if (!timer->present) + break; + switch ((virDomainTimerNameType)timer->name) { case VIR_DOMAIN_TIMER_NAME_PLATFORM: case VIR_DOMAIN_TIMER_NAME_TSC: case VIR_DOMAIN_TIMER_NAME_KVMCLOCK: case VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK: case VIR_DOMAIN_TIMER_NAME_PIT: - case VIR_DOMAIN_TIMER_NAME_HPET: case VIR_DOMAIN_TIMER_NAME_ARMVTIMER: case VIR_DOMAIN_TIMER_NAME_LAST: break; case VIR_DOMAIN_TIMER_NAME_RTC: - if (!timer->present) - break; - if (virFileExists("/dev/rtc")) { if (virCgroupAllowDevicePath(cgroup, "/dev/rtc", VIR_CGROUP_DEVICE_READ, @@ -367,6 +366,16 @@ static int virLXCCgroupSetupDeviceACL(virDomainDefPtr = def, VIR_DEBUG("Ignoring non-existent device /dev/rtc"); } break; + case VIR_DOMAIN_TIMER_NAME_HPET: + if (virFileExists("/dev/hpet")) { + if (virCgroupAllowDevicePath(cgroup, "/dev/hpet", + VIR_CGROUP_DEVICE_READ, + false) < 0) + return -1; + } else { + VIR_DEBUG("Ignoring non-existent device /dev/hpet"); + } + break; } } } else { diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index eba6bfe0bf..518967ee83 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -1566,13 +1566,15 @@ virLXCControllerSetupTimers(virLXCControllerPtr ctr= l) dev_t dev; virDomainTimerDefPtr timer =3D def->clock.timers[i]; =20 + if (!timer->present) + continue; + switch ((virDomainTimerNameType)timer->name) { case VIR_DOMAIN_TIMER_NAME_PLATFORM: case VIR_DOMAIN_TIMER_NAME_TSC: case VIR_DOMAIN_TIMER_NAME_KVMCLOCK: case VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK: case VIR_DOMAIN_TIMER_NAME_PIT: - case VIR_DOMAIN_TIMER_NAME_HPET: case VIR_DOMAIN_TIMER_NAME_ARMVTIMER: case VIR_DOMAIN_TIMER_NAME_LAST: virReportError(VIR_ERR_CONFIG_UNSUPPORTED, @@ -1580,9 +1582,6 @@ virLXCControllerSetupTimers(virLXCControllerPtr ctrl) virDomainTimerNameTypeToString(timer->name)); return -1; case VIR_DOMAIN_TIMER_NAME_RTC: - if (!timer->present) - break; - if (stat("/dev/rtc", &sb) < 0) { if (errno =3D=3D EACCES) return -1; @@ -1605,6 +1604,32 @@ virLXCControllerSetupTimers(virLXCControllerPtr ctrl) return -1; } =20 + if (lxcContainerChown(ctrl->def, path) < 0) + return -1; + break; + case VIR_DOMAIN_TIMER_NAME_HPET: + if (stat("/dev/hpet", &sb) < 0) { + if (errno =3D=3D EACCES) + return -1; + + virReportSystemError(errno, + _("Path '%s' is not accessible"), + path); + return -1; + } + + path =3D g_strdup_printf("/%s/%s.dev/%s", LXC_STATE_DIR, + ctrl->def->name, "/hpet"); + + dev =3D makedev(major(sb.st_rdev), minor(sb.st_rdev)); + if (mknod(path, S_IFCHR, dev) < 0 || + chmod(path, sb.st_mode)) { + virReportSystemError(errno, + _("Failed to make device %s"), + path); + return -1; + } + if (lxcContainerChown(ctrl->def, path) < 0) return -1; break; --=20 2.20.1 From nobody Thu May 2 09:10:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1582517438; cv=none; d=zohomail.com; s=zohoarc; b=Rh8F71bgXWRn1N1uD8eTWS5Hkm4OAiWZNFvp5FsicLqSwSRAQYuzQP2hPqeT34+cO0VF645jiXb5KcmS6L6XVyHZp8zjv38rnZ8dyiuAIbX+tsCB12Yksto9R5yP8duncrg/OU4dzgu+wYklzCplHAX7q5L9FpVWPsuO0MLXQOE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582517438; h=Content-Type:Content-Transfer-Encoding:Cc: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=NEzyGFnFGJhU2DqYENBZi2U3oA1MQXU+0z5gPDoFLxA=; b=mA4zAG4DH6R7cmE32kISaEdsCdqH9jmY06K2sEN6ABSbmaDES+GWj27GgXFYyFaN3fBJgpsYujyCY+3nWTA5zOgKWGqvVrrz1UmRZEokY2Y6LbsAEVEYIbkw/2Fmn2anmAoMXxA9yLdn8FtFSEDC6tpfP5GS+TY/ZKAtQXd9mKU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1582517438533371.265172612137; Sun, 23 Feb 2020 20:10:38 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-323-BMP2dMfxPWWzyF71VYcw_Q-1; Sun, 23 Feb 2020 23:10:35 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B94428017DF; Mon, 24 Feb 2020 04:10:29 +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 8F8A81001B2D; Mon, 24 Feb 2020 04:10:29 +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 48A6E860E7; Mon, 24 Feb 2020 04:10:29 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 01O4ARsg015594 for ; Sun, 23 Feb 2020 23:10:28 -0500 Received: by smtp.corp.redhat.com (Postfix) id D324710EE6CD; Mon, 24 Feb 2020 04:10:27 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CDECD10EE6CC for ; Mon, 24 Feb 2020 04:10:25 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8F3D2185A790 for ; Mon, 24 Feb 2020 04:10:25 +0000 (UTC) Received: from mail-qv1-f66.google.com (mail-qv1-f66.google.com [209.85.219.66]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-374-4T_8GKfYOFCMpKaVZguq0Q-1; Sun, 23 Feb 2020 23:10:23 -0500 Received: by mail-qv1-f66.google.com with SMTP id q9so3610357qvu.7 for ; Sun, 23 Feb 2020 20:10:23 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id p126sm5355658qkd.108.2020.02.23.20.10.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 20:10:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582517437; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=NEzyGFnFGJhU2DqYENBZi2U3oA1MQXU+0z5gPDoFLxA=; b=BKwzpgOUPwGRzpwx1IFpTFpuxiWi0UlhXD6+jR1mIJA2e9QYs5yjCecXXLn6TVO7AHqrzK qCVkULmUOnIGpkx5MoYFAS6QAZdpYzV9gEbjyxmXx9U+5KVnNgQSNfzjmRWNiOcwfI3uw8 aqZQCxG6FqbKHLqMgt+C3ZiFieifxSA= X-MC-Unique: BMP2dMfxPWWzyF71VYcw_Q-1 X-MC-Unique: 4T_8GKfYOFCMpKaVZguq0Q-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Kzp07a7vm7Sic0fCCsvaEUeW1Fs/MioMoDp+nfun45U=; b=YQKVjohLyHV7gFxm0Ij8g7V7Od7DojE2YJsOj28s8yb2TTcJN050kCbY0h8imRLbRc U1599Ha4/zGeE2O2TpWJcpxVAOpDwvaikaUMFR1U4s78hGStjKNqZKdsaX0ij0tilBOO TemXDBgKCbh93u2+ousJsEzG+fFrx9Pfy8CuIxjwTzrU12FWeXo1YXfmLuOZRo095O9N DoXFfFGZGZITyrWUWfpFNoxmMKiVQ6OJFDaDUmqh6deBkPZH6tjwPoIKYOINoKjZ4vs+ ri9ZzOpQSgicdDunJ5wUWh4JNoDDkQLhCCGnexQNdmOLfIygY5aP4J3x0rPMR6pmSyRS QfRA== X-Gm-Message-State: APjAAAU+eA9JS+b1Kxd5aXKYseUHVUzzXh4b9f48VwgbUvpw5OsyZCBf 12hBvEa9h1xUK3VcO35pFYCyTf1s X-Google-Smtp-Source: APXvYqyHkFdP4h6tclFbG8kLMOHh6b3QpDkYX2nDUBhrC/w6IKOGnN4Wl+n9NLfGuaiE7Mm3mLmp2A== X-Received: by 2002:ad4:4e73:: with SMTP id ec19mr40155029qvb.78.1582517422325; Sun, 23 Feb 2020 20:10:22 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v3 3/5] lxc: Replacing default strings definitions by g_autofree statement. Date: Mon, 24 Feb 2020 01:09:43 -0300 Message-Id: <20200224040942.17695-4-jcfaracco@gmail.com> In-Reply-To: <20200224040942.17695-1-jcfaracco@gmail.com> References: <20200224040942.17695-1-jcfaracco@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 01O4ARsg015594 X-loop: libvir-list@redhat.com Cc: danielhb413@gmail.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.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" There are a lots of strings being handled inside some LXC functions. They can be moved to g_autofree to avoid declaring a return value to get proper code cleanups. This commit is changing functions from lxc_{controller,cgroup,fuse} only. Signed-off-by: Julio Faracco --- src/lxc/lxc_cgroup.c | 15 +++---- src/lxc/lxc_controller.c | 96 ++++++++++++++-------------------------- src/lxc/lxc_fuse.c | 23 +++------- 3 files changed, 44 insertions(+), 90 deletions(-) diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index 997a5c3dfa..d29b65092a 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -54,8 +54,7 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr de= f, virCgroupPtr cgroup, virBitmapPtr nodemask) { - int ret =3D -1; - char *mask =3D NULL; + g_autofree char *mask =3D NULL; virDomainNumatuneMemMode mode; =20 if (def->placement_mode !=3D VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO && @@ -66,21 +65,17 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr = def, =20 if (virDomainNumatuneGetMode(def->numa, -1, &mode) < 0 || mode =3D=3D VIR_DOMAIN_NUMATUNE_MEM_STRICT) { - ret =3D 0; - goto cleanup; + return 0; } =20 if (virDomainNumatuneMaybeFormatNodeset(def->numa, nodemask, &mask, -1) < 0) - goto cleanup; + return -1; =20 if (mask && virCgroupSetCpusetMems(cgroup, mask) < 0) - goto cleanup; + return -1; =20 - ret =3D 0; - cleanup: - VIR_FREE(mask); - return ret; + return 0; } =20 =20 diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 518967ee83..c580b17f5f 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -802,8 +802,7 @@ static int virLXCControllerGetNumadAdvice(virLXCControl= lerPtr ctrl, virBitmapPtr *mask) { virBitmapPtr nodemask =3D NULL; - char *nodeset =3D NULL; - int ret =3D -1; + g_autofree char *nodeset =3D NULL; =20 /* Get the advisory nodeset from numad if 'placement' of * either or is 'auto'. @@ -812,20 +811,17 @@ static int virLXCControllerGetNumadAdvice(virLXCContr= ollerPtr ctrl, nodeset =3D virNumaGetAutoPlacementAdvice(virDomainDefGetVcpus(ctr= l->def), ctrl->def->mem.cur_balloon= ); if (!nodeset) - goto cleanup; + return -1; =20 VIR_DEBUG("Nodeset returned from numad: %s", nodeset); =20 if (virBitmapParse(nodeset, &nodemask, VIR_DOMAIN_CPUMASK_LEN) < 0) - goto cleanup; + return -1; } =20 - ret =3D 0; *mask =3D nodemask; =20 - cleanup: - VIR_FREE(nodeset); - return ret; + return 0; } =20 =20 @@ -1434,9 +1430,8 @@ virLXCControllerSetupUsernsMap(virDomainIdMapEntryPtr= map, */ static int virLXCControllerSetupUserns(virLXCControllerPtr ctrl) { - char *uid_map =3D NULL; - char *gid_map =3D NULL; - int ret =3D -1; + g_autofree char *uid_map =3D NULL; + g_autofree char *gid_map =3D NULL; =20 /* User namespace is disabled for container */ if (ctrl->def->idmap.nuidmap =3D=3D 0) { @@ -1450,28 +1445,23 @@ static int virLXCControllerSetupUserns(virLXCContro= llerPtr ctrl) if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.uidmap, ctrl->def->idmap.nuidmap, uid_map) < 0) - goto cleanup; + return -1; =20 gid_map =3D g_strdup_printf("/proc/%d/gid_map", ctrl->initpid); =20 if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.gidmap, ctrl->def->idmap.ngidmap, gid_map) < 0) - goto cleanup; + return -1; =20 - ret =3D 0; - cleanup: - VIR_FREE(uid_map); - VIR_FREE(gid_map); - return ret; + return 0; } =20 static int virLXCControllerSetupDev(virLXCControllerPtr ctrl) { - char *mount_options =3D NULL; - char *opts =3D NULL; - char *dev =3D NULL; - int ret =3D -1; + g_autofree char *mount_options =3D NULL; + g_autofree char *opts =3D NULL; + g_autofree char *dev =3D NULL; =20 VIR_DEBUG("Setting up /dev/ for container"); =20 @@ -1488,24 +1478,18 @@ static int virLXCControllerSetupDev(virLXCControlle= rPtr ctrl) opts =3D g_strdup_printf("mode=3D755,size=3D65536%s", mount_options); =20 if (virFileSetupDev(dev, opts) < 0) - goto cleanup; + return -1; =20 if (lxcContainerChown(ctrl->def, dev) < 0) - goto cleanup; + return -1; =20 - ret =3D 0; - cleanup: - VIR_FREE(opts); - VIR_FREE(mount_options); - VIR_FREE(dev); - return ret; + return 0; } =20 static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl) { size_t i; - int ret =3D -1; - char *path =3D NULL; + g_autofree char *path =3D NULL; const struct { int maj; int min; @@ -1521,7 +1505,7 @@ static int virLXCControllerPopulateDevices(virLXCCont= rollerPtr ctrl) }; =20 if (virLXCControllerSetupDev(ctrl) < 0) - goto cleanup; + return -1; =20 /* Populate /dev/ with a few important bits */ for (i =3D 0; i < G_N_ELEMENTS(devs); i++) { @@ -1534,19 +1518,14 @@ static int virLXCControllerPopulateDevices(virLXCCo= ntrollerPtr ctrl) virReportSystemError(errno, _("Failed to make device %s"), path); - goto cleanup; + return -1; } =20 if (lxcContainerChown(ctrl->def, path) < 0) - goto cleanup; - - VIR_FREE(path); + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FREE(path); - return ret; + return 0; } =20 =20 @@ -2202,10 +2181,9 @@ virLXCControllerSetupPrivateNS(void) static int virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl) { - char *mount_options =3D NULL; - char *opts =3D NULL; - char *devpts =3D NULL; - int ret =3D -1; + g_autofree char *mount_options =3D NULL; + g_autofree char *opts =3D NULL; + g_autofree char *devpts =3D NULL; gid_t ptsgid =3D 5; =20 VIR_DEBUG("Setting up private /dev/pts"); @@ -2220,7 +2198,7 @@ virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl) virReportSystemError(errno, _("Failed to make path %s"), devpts); - goto cleanup; + return -1; } =20 if (ctrl->def->idmap.ngidmap) @@ -2239,26 +2217,20 @@ virLXCControllerSetupDevPTS(virLXCControllerPtr ctr= l) virReportSystemError(errno, _("Failed to mount devpts on %s"), devpts); - goto cleanup; + return -1; } =20 if (access(ctrl->devptmx, R_OK) < 0) { virReportSystemError(ENOSYS, "%s", _("Kernel does not support private devpts")); - goto cleanup; + return -1; } =20 if ((lxcContainerChown(ctrl->def, ctrl->devptmx) < 0) || (lxcContainerChown(ctrl->def, devpts) < 0)) - goto cleanup; - - ret =3D 0; + return -1; =20 - cleanup: - VIR_FREE(opts); - VIR_FREE(devpts); - VIR_FREE(mount_options); - return ret; + return 0; } =20 =20 @@ -2279,8 +2251,7 @@ virLXCControllerSetupConsoles(virLXCControllerPtr ctr= l, char **containerTTYPaths) { size_t i; - int ret =3D -1; - char *ttyHostPath =3D NULL; + g_autofree char *ttyHostPath =3D NULL; =20 for (i =3D 0; i < ctrl->nconsoles; i++) { VIR_DEBUG("Opening tty on private %s", ctrl->devptmx); @@ -2289,20 +2260,17 @@ virLXCControllerSetupConsoles(virLXCControllerPtr c= trl, &containerTTYPaths[i], &ttyHostPath) < 0) { virReportSystemError(errno, "%s", _("Failed to allocate tty")); - goto cleanup; + return -1; } =20 /* Change the owner of tty device to the root user of container */ if (lxcContainerChown(ctrl->def, ttyHostPath) < 0) - goto cleanup; + return -1; =20 VIR_FREE(ttyHostPath); } =20 - ret =3D 0; - cleanup: - VIR_FREE(ttyHostPath); - return ret; + return 0; } =20 =20 diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c index 44f240a0b5..8cfccdd7e0 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -40,8 +40,7 @@ static const char *fuse_meminfo_path =3D "/meminfo"; =20 static int lxcProcGetattr(const char *path, struct stat *stbuf) { - int res; - char *mempath =3D NULL; + g_autofree char *mempath =3D NULL; struct stat sb; struct fuse_context *context =3D fuse_get_context(); virDomainDefPtr def =3D (virDomainDefPtr)context->private_data; @@ -49,16 +48,12 @@ static int lxcProcGetattr(const char *path, struct stat= *stbuf) memset(stbuf, 0, sizeof(struct stat)); mempath =3D g_strdup_printf("/proc/%s", path); =20 - res =3D 0; - if (STREQ(path, "/")) { stbuf->st_mode =3D S_IFDIR | 0755; stbuf->st_nlink =3D 2; } else if (STREQ(path, fuse_meminfo_path)) { - if (stat(mempath, &sb) < 0) { - res =3D -errno; - goto cleanup; - } + if (stat(mempath, &sb) < 0) + return -errno; =20 stbuf->st_uid =3D def->idmap.uidmap ? def->idmap.uidmap[0].target = : 0; stbuf->st_gid =3D def->idmap.gidmap ? def->idmap.gidmap[0].target = : 0; @@ -71,12 +66,10 @@ static int lxcProcGetattr(const char *path, struct stat= *stbuf) stbuf->st_ctime =3D sb.st_ctime; stbuf->st_mtime =3D sb.st_mtime; } else { - res =3D -ENOENT; + return -ENOENT; } =20 - cleanup: - VIR_FREE(mempath); - return res; + return 0; } =20 static int lxcProcReaddir(const char *path, void *buf, @@ -127,7 +120,7 @@ static int lxcProcReadMeminfo(char *hostpath, virDomain= DefPtr def, { int res; FILE *fd =3D NULL; - char *line =3D NULL; + g_autofree char *line =3D NULL; size_t n; struct virLXCMeminfo meminfo; virBuffer buffer =3D VIR_BUFFER_INITIALIZER; @@ -229,7 +222,6 @@ static int lxcProcReadMeminfo(char *hostpath, virDomain= DefPtr def, memcpy(buf, virBufferCurrentContent(new_meminfo), res); =20 cleanup: - VIR_FREE(line); virBufferFreeAndReset(new_meminfo); VIR_FORCE_FCLOSE(fd); return res; @@ -242,7 +234,7 @@ static int lxcProcRead(const char *path G_GNUC_UNUSED, struct fuse_file_info *fi G_GNUC_UNUSED) { int res =3D -ENOENT; - char *hostpath =3D NULL; + g_autofree char *hostpath =3D NULL; struct fuse_context *context =3D NULL; virDomainDefPtr def =3D NULL; =20 @@ -256,7 +248,6 @@ static int lxcProcRead(const char *path G_GNUC_UNUSED, res =3D lxcProcHostRead(hostpath, buf, size, offset); } =20 - VIR_FREE(hostpath); return res; } =20 --=20 2.20.1 From nobody Thu May 2 09:10:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1582517466; cv=none; d=zohomail.com; s=zohoarc; b=W6iMjJ0SZVvLwfk6el0Dc2ZVrl9T835r0peVFuCSiFlohIBtjgCP+9UwQGcD2nvMhrIsbY/jnADnL2QmGJRdNGHUiPxx1LxvhqmcGS/peHD2n7S74ND3GE/tY695XH2p0lWocroThoEvMUViPts+gsByQaFl3kix3hihFC52LYo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582517466; h=Content-Type:Content-Transfer-Encoding:Cc: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=9kraAJsX2hQwYjyUifd5nWGqIV0OvHB4RXcasEuhv30=; b=MybY3vbTFxr+hQZSwKkZtufTh3vOV8HetM5DsI4oAUQl519GjNbv1xUIv1CBrffBdzZ3eZnJGcLgmCIufjF40MrCTl6T6GQv42qSAaX+xYcVZt2UEEa/DL0g5zAOp5h9mkNnIxIXb06isgQgOlhNwJbb0FdeVFJbwlxmswJr7zo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1582517466835381.15472992724574; Sun, 23 Feb 2020 20:11:06 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-186-ZyfT8-j3NmqXHE78EbJgzw-1; Sun, 23 Feb 2020 23:11:02 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5DDA7DB61; Mon, 24 Feb 2020 04:10:57 +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 343361001B2D; Mon, 24 Feb 2020 04:10:57 +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 E13F4860E8; Mon, 24 Feb 2020 04:10:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 01O4AtqK015635 for ; Sun, 23 Feb 2020 23:10:55 -0500 Received: by smtp.corp.redhat.com (Postfix) id 29EF810EE6CC; Mon, 24 Feb 2020 04:10:55 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast05.extmail.prod.ext.rdu2.redhat.com [10.11.55.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2509410EE6CF for ; Mon, 24 Feb 2020 04:10:53 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D5934800298 for ; Mon, 24 Feb 2020 04:10:52 +0000 (UTC) Received: from mail-qt1-f193.google.com (mail-qt1-f193.google.com [209.85.160.193]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-19-tizXSFF4OciUBvExN7MrXA-1; Sun, 23 Feb 2020 23:10:50 -0500 Received: by mail-qt1-f193.google.com with SMTP id v25so5714308qto.7 for ; Sun, 23 Feb 2020 20:10:50 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id p126sm5355658qkd.108.2020.02.23.20.10.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 20:10:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582517465; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=9kraAJsX2hQwYjyUifd5nWGqIV0OvHB4RXcasEuhv30=; b=BrD/joVVcBXLaV3gGqGOREOkw9fMrnSi1vZv95rNAjgLeSfhS1h/BW4WaLhEfeI+pC2wGM nfLMhCCOeNpbaefg9iJ8kA+UtJysxjTBwvF56/vseRCcnM1yfwmTrIR5IhAnK/SudtVmgM vi1d8zhV1Rf+KYZOjt7RT5y5niofnw0= X-MC-Unique: ZyfT8-j3NmqXHE78EbJgzw-1 X-MC-Unique: tizXSFF4OciUBvExN7MrXA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NnPC6vuq2rOklkD0Z5s+ZzyJAjtIxpBzmxdgz/Lwc0E=; b=XyamvWZzyJXYxqb8ym5CDRlIcgrLnkQQpj8sgwbPpCcRyiU54pdOng8qBh5GbAkB9k LGourSMwayrbxZQWK3B59PG304WJxkwhXBOWJWLKw/G8p/ujft9wh+Snjo8lGRaH0dQf OY3qe8NX9TGv1d9j8VFrj45HVDut/cKjGP0Sxtt9jTAUsT2QqvuxWueheJ+C5fzO0lt7 X4h6hucalaqStXQfYR9i7zqjtm0FomFzw8VU9TC4cZFeEFTC6+Guj+KHuxJY1Pwkf5IV iE8HRzRnh8M1WNUgvjdmxjC63O6Bkb6geHAPAvrDzDmy+jgozPlXuUnjWk1w/mozxiMk AzBw== X-Gm-Message-State: APjAAAUvAJbGlOO8ZyM2cCnKVRyDoRS2c5TCjjE7nV0bf6OCnh/dgKhc mix/bvgP5dY10d1CLVH+assgbxSp X-Google-Smtp-Source: APXvYqwPpvw0W521Kg0MhRCtoNE7sT9vkz7w03HBz4lvbD1epgBeuy4q8apUYapLODye+ZPFNh3i5Q== X-Received: by 2002:ac8:71c2:: with SMTP id i2mr46932153qtp.249.1582517449509; Sun, 23 Feb 2020 20:10:49 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v3 4/5] lxc: Implement virtual /proc/cpuinfo via LXC fuse Date: Mon, 24 Feb 2020 01:09:45 -0300 Message-Id: <20200224040942.17695-5-jcfaracco@gmail.com> In-Reply-To: <20200224040942.17695-1-jcfaracco@gmail.com> References: <20200224040942.17695-1-jcfaracco@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 01O4AtqK015635 X-loop: libvir-list@redhat.com Cc: danielhb413@gmail.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.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" This commit tries to fix lots of issues related to LXC VCPUs. One of them is related to /proc/cpuinfo content. If only 1 VCPU is set, LXC containers will show all CPUs available for host. The second one is related to CPU share, if an user set only 1 VCPU, the container/process will use all available CPUs. (This is not the case when `cpuset` attribute is declared). So, this commit adds a virtual cpuinfo based on VCPU mapping and it automatically limits the CPU usage according VCPU count. Example (now): LXC container - 8 CPUS with 2 VCPU: lxc-root# stress --cpu 8 On host machine, only CPU 0 and 1 have 100% usage. Signed-off-by: Julio Faracco --- src/lxc/lxc_cgroup.c | 31 +++++++++++++++ src/lxc/lxc_container.c | 39 +++++++++++------- src/lxc/lxc_fuse.c | 88 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 139 insertions(+), 19 deletions(-) diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index d29b65092a..912a252473 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -50,6 +50,34 @@ static int virLXCCgroupSetupCpuTune(virDomainDefPtr def, } =20 =20 +static int virLXCCgroupSetupVcpuAuto(virDomainDefPtr def, + virCgroupPtr cgroup) +{ + size_t i; + int vcpumax; + virBuffer buffer =3D VIR_BUFFER_INITIALIZER; + virBufferPtr cpuset =3D &buffer; + + vcpumax =3D virDomainDefGetVcpusMax(def); + for (i =3D 0; i < vcpumax; i++) { + virDomainVcpuDefPtr vcpu =3D virDomainDefGetVcpu(def, i); + /* Cgroup is smart enough to convert numbers separated + * by comma into ranges. Example: "0,1,2,5," -> "0-2,5". + * Libvirt does not need to process it here. */ + if (vcpu) + virBufferAsprintf(cpuset, "%zu,", i); + } + if (virCgroupSetCpusetCpus(cgroup, + virBufferCurrentContent(cpuset)) < 0) { + virBufferFreeAndReset(cpuset); + return -1; + } + + virBufferFreeAndReset(cpuset); + return 0; +} + + static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def, virCgroupPtr cgroup, virBitmapPtr nodemask) @@ -61,6 +89,9 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr de= f, def->cpumask && virCgroupSetupCpusetCpus(cgroup, def->cpumask) < 0) { return -1; + } else { + /* auto mode for VCPU limits */ + virLXCCgroupSetupVcpuAuto(def, cgroup); } =20 if (virDomainNumatuneGetMode(def->numa, -1, &mode) < 0 || diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 41efe43a14..88e27f3060 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -997,8 +997,8 @@ static int lxcContainerMountBasicFS(bool userns_enabled, static int lxcContainerMountProcFuse(virDomainDefPtr def, const char *stateDir) { - int ret; - char *meminfo_path =3D NULL; + g_autofree char *meminfo_path =3D NULL; + g_autofree char *cpuinfo_path =3D NULL; =20 VIR_DEBUG("Mount /proc/meminfo stateDir=3D%s", stateDir); =20 @@ -1006,15 +1006,29 @@ static int lxcContainerMountProcFuse(virDomainDefPt= r def, stateDir, def->name); =20 - if ((ret =3D mount(meminfo_path, "/proc/meminfo", - NULL, MS_BIND, NULL)) < 0) { + if (mount(meminfo_path, "/proc/meminfo", + NULL, MS_BIND, NULL) < 0) { virReportSystemError(errno, _("Failed to mount %s on /proc/meminfo"), meminfo_path); + return -1; } =20 - VIR_FREE(meminfo_path); - return ret; + VIR_DEBUG("Mount /proc/cpuinfo stateDir=3D%s", stateDir); + + cpuinfo_path =3D g_strdup_printf("/.oldroot/%s/%s.fuse/cpuinfo", + stateDir, + def->name); + + if (mount(cpuinfo_path, "/proc/cpuinfo", + NULL, MS_BIND, NULL) < 0) { + virReportSystemError(errno, + _("Failed to mount %s on /proc/cpuinfo"), + cpuinfo_path); + return -1; + } + + return 0; } #else static int lxcContainerMountProcFuse(virDomainDefPtr def G_GNUC_UNUSED, @@ -1027,8 +1041,7 @@ static int lxcContainerMountProcFuse(virDomainDefPtr = def G_GNUC_UNUSED, static int lxcContainerMountFSDev(virDomainDefPtr def, const char *stateDir) { - int ret =3D -1; - char *path =3D NULL; + g_autofree char *path =3D NULL; int flags =3D def->idmap.nuidmap ? MS_BIND : MS_MOVE; =20 VIR_DEBUG("Mount /dev/ stateDir=3D%s", stateDir); @@ -1038,7 +1051,7 @@ static int lxcContainerMountFSDev(virDomainDefPtr def, if (virFileMakePath("/dev") < 0) { virReportSystemError(errno, "%s", _("Cannot create /dev")); - goto cleanup; + return -1; } =20 VIR_DEBUG("Trying to %s %s to /dev", def->idmap.nuidmap ? @@ -1048,14 +1061,10 @@ static int lxcContainerMountFSDev(virDomainDefPtr d= ef, virReportSystemError(errno, _("Failed to mount %s on /dev"), path); - goto cleanup; + return -1; } =20 - ret =3D 0; - - cleanup: - VIR_FREE(path); - return ret; + return 0; } =20 static int lxcContainerMountFSDevPTS(virDomainDefPtr def, diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c index 8cfccdd7e0..402382b4d8 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -37,22 +37,23 @@ #if WITH_FUSE =20 static const char *fuse_meminfo_path =3D "/meminfo"; +static const char *fuse_cpuinfo_path =3D "/cpuinfo"; =20 static int lxcProcGetattr(const char *path, struct stat *stbuf) { - g_autofree char *mempath =3D NULL; + g_autofree char *procpath =3D NULL; struct stat sb; struct fuse_context *context =3D fuse_get_context(); virDomainDefPtr def =3D (virDomainDefPtr)context->private_data; =20 memset(stbuf, 0, sizeof(struct stat)); - mempath =3D g_strdup_printf("/proc/%s", path); + procpath =3D g_strdup_printf("/proc/%s", path); =20 if (STREQ(path, "/")) { stbuf->st_mode =3D S_IFDIR | 0755; stbuf->st_nlink =3D 2; } else if (STREQ(path, fuse_meminfo_path)) { - if (stat(mempath, &sb) < 0) + if (stat(procpath, &sb) < 0) return -errno; =20 stbuf->st_uid =3D def->idmap.uidmap ? def->idmap.uidmap[0].target = : 0; @@ -83,6 +84,7 @@ static int lxcProcReaddir(const char *path, void *buf, filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); filler(buf, fuse_meminfo_path + 1, NULL, 0); + filler(buf, fuse_cpuinfo_path + 1, NULL, 0); =20 return 0; } @@ -90,7 +92,8 @@ static int lxcProcReaddir(const char *path, void *buf, static int lxcProcOpen(const char *path G_GNUC_UNUSED, struct fuse_file_info *fi G_GNUC_UNUSED) { - if (STRNEQ(path, fuse_meminfo_path)) + if (STRNEQ(path, fuse_meminfo_path) && + STRNEQ(path, fuse_cpuinfo_path)) return -ENOENT; =20 if ((fi->flags & 3) !=3D O_RDONLY) @@ -227,6 +230,80 @@ static int lxcProcReadMeminfo(char *hostpath, virDomai= nDefPtr def, return res; } =20 + +static int +lxcProcReadCpuinfoParse(virDomainDefPtr def, char *base, + virBufferPtr new_cpuinfo) +{ + char *procline =3D NULL; + char *saveptr =3D base; + size_t cpu; + size_t nvcpu; + size_t curcpu =3D 0; + bool get_proc =3D false; + + nvcpu =3D virDomainDefGetVcpus(def); + while ((procline =3D strtok_r(NULL, "\n", &saveptr))) { + if (sscanf(procline, "processor\t: %zu", &cpu) =3D=3D 1) { + virDomainVcpuDefPtr vcpu =3D virDomainDefGetVcpu(def, cpu); + /* VCPU is mapped */ + if (vcpu) { + if (curcpu =3D=3D nvcpu) + break; + + if (curcpu > 0) + virBufferAddLit(new_cpuinfo, "\n"); + + virBufferAsprintf(new_cpuinfo, "processor\t: %zu\n", + curcpu); + curcpu++; + get_proc =3D true; + } else { + get_proc =3D false; + } + } else { + /* It is not a processor index */ + if (get_proc) + virBufferAsprintf(new_cpuinfo, "%s\n", procline); + } + } + + virBufferAddLit(new_cpuinfo, "\n"); + + return strlen(virBufferCurrentContent(new_cpuinfo)); +} + + +static int lxcProcReadCpuinfo(char *hostpath, virDomainDefPtr def, + char *buf, size_t size, off_t offset) +{ + virBuffer buffer =3D VIR_BUFFER_INITIALIZER; + virBufferPtr new_cpuinfo =3D &buffer; + g_autofree char *outbuf =3D NULL; + int res =3D -1; + + /* Gather info from /proc/cpuinfo */ + if (virFileReadAll(hostpath, 1024*1024, &outbuf) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to open %s"), hostpath); + return -1; + } + + /* /proc/cpuinfo does not support fseek */ + if (offset > 0) + return 0; + + res =3D lxcProcReadCpuinfoParse(def, outbuf, new_cpuinfo); + + if (res > size) + res =3D size; + memcpy(buf, virBufferCurrentContent(new_cpuinfo), res); + + virBufferFreeAndReset(new_cpuinfo); + return res; +} + + static int lxcProcRead(const char *path G_GNUC_UNUSED, char *buf G_GNUC_UNUSED, size_t size G_GNUC_UNUSED, @@ -246,6 +323,9 @@ static int lxcProcRead(const char *path G_GNUC_UNUSED, if (STREQ(path, fuse_meminfo_path)) { if ((res =3D lxcProcReadMeminfo(hostpath, def, buf, size, offset))= < 0) res =3D lxcProcHostRead(hostpath, buf, size, offset); + } else if (STREQ(path, fuse_cpuinfo_path)) { + if ((res =3D lxcProcReadCpuinfo(hostpath, def, buf, size, offset))= < 0) + res =3D lxcProcHostRead(hostpath, buf, size, offset); } =20 return res; --=20 2.20.1 From nobody Thu May 2 09:10:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1582517468; cv=none; d=zohomail.com; s=zohoarc; b=NdZEBdVq3D/Z2/2cQMApkgvh51xfS5rI4uWAyeP/j6W3xfmUSstaqB7u5zgKh6MirnAg6gqgz+xrGamik9q5dqhCcdmpWaeuI3v320Xk1ZC7KnAkoQLxBzwTcEKoqGS6cBrfBZwkav7YCV1iDEzBTrjWDaBQEcn50ei5bsm4dQQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582517468; h=Content-Type:Content-Transfer-Encoding:Cc: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=0KxKExLV8XoSIvsHrNRPkyPn5SO9ysibkBHD9fKcg04=; b=WCrQFDAzSF0owmkk6yS+pnVOgJPy9X/7BxFgIZn+Bx4BvyZuRomdga4500++O/iXk+YdAW19OSaL5KX8wWRZpv6Vu0/HKCYROkWYLJtVOjD4NRQ4e/PgY2RySCknZ0p7tcn4pNRBx4DC+JceXaz/SXFBHkOr9vTYDEk94cxNRno= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1582517468602826.9678615218585; Sun, 23 Feb 2020 20:11:08 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-372-kAAKaRu7M6ayb3YEjWiABg-1; Sun, 23 Feb 2020 23:11:05 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 11641DB75; Mon, 24 Feb 2020 04:11:00 +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 D358760BF7; Mon, 24 Feb 2020 04:10:59 +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 8C084860EA; Mon, 24 Feb 2020 04:10:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 01O4AwKS015646 for ; Sun, 23 Feb 2020 23:10:58 -0500 Received: by smtp.corp.redhat.com (Postfix) id 0F8FAAF988; Mon, 24 Feb 2020 04:10:58 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast02.extmail.prod.ext.rdu2.redhat.com [10.11.55.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0AE88AF9A5 for ; Mon, 24 Feb 2020 04:10:55 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BFDAC8007A4 for ; Mon, 24 Feb 2020 04:10:55 +0000 (UTC) Received: from mail-qt1-f193.google.com (mail-qt1-f193.google.com [209.85.160.193]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-465-05RRAZxwNwqwACP7xxpIUQ-1; Sun, 23 Feb 2020 23:10:53 -0500 Received: by mail-qt1-f193.google.com with SMTP id i14so5699834qtv.13 for ; Sun, 23 Feb 2020 20:10:53 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id p126sm5355658qkd.108.2020.02.23.20.10.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 20:10:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582517467; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=0KxKExLV8XoSIvsHrNRPkyPn5SO9ysibkBHD9fKcg04=; b=Tztl1dzh+Vn5t5HuzjpL7Dzr2t+BTrbFi4wGEEtEB4pmP7T/xG7TRwQEeo/RthewTJUG+/ fW9yu+Wu6ydqexMvZAethO1++b4L7OvmfI+y25+KRNvViRuJJU3DKBNKNRoi7k/gQnPYxh DuKWGWhek7WYbuVtpucl16wpUdIlCDk= X-MC-Unique: kAAKaRu7M6ayb3YEjWiABg-1 X-MC-Unique: 05RRAZxwNwqwACP7xxpIUQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rXptADVXU0Q8zjtlr7rTjCE8M4ADdrHTmteju02X3JI=; b=f3XpQowXQqHBm4jdB7SWHxxyT1GHJlLmpZgbMF+qbB55tt0F+AmoyCkbnwaFGEKerj zv31zqDzD//hIYP3PiHqhJEAKa+zt/uiAgD1kjyYR8vKKye4fXIoUVBPFAGtLOKGHbHz MwpG4tqd+9ovFwYJlYZ0cli7VmfEgNpabGoP56VATOK4k8GQdX1W3uHW8ZBvZr9+FwVZ JwfnoN8/d+c/+eFGxQfRD/2uuonl+vEpT4QNjDZCpVxYg6LPEVCFWsaMATW/I0dZT4um qRcRcSW9oaYusnKpVR/RXCi21afnvjCV5SDBXUbHcK6hTGif7u3AmTCCHVWx08EPwpJr PRCg== X-Gm-Message-State: APjAAAUSfZB9MoNie6+C+1Dl+eY1PvggmfoL2ulPosy3xYzQOsByKaV1 vF7RmodhlWA2DZIdYYz7F0FwN3UT X-Google-Smtp-Source: APXvYqyU3bUKiG19GPwu/MldBUWwMFBNvlqyPJzDd/6iFiSNPG1QJh6vGrEvg4m273vXlq5DvdwP+A== X-Received: by 2002:aed:36a5:: with SMTP id f34mr44341951qtb.57.1582517452400; Sun, 23 Feb 2020 20:10:52 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v3 5/5] lxc: Count max VCPUs based on cpuset.cpus in native config. Date: Mon, 24 Feb 2020 01:09:46 -0300 Message-Id: <20200224040942.17695-6-jcfaracco@gmail.com> In-Reply-To: <20200224040942.17695-1-jcfaracco@gmail.com> References: <20200224040942.17695-1-jcfaracco@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 01O4AwKS015646 X-loop: libvir-list@redhat.com Cc: danielhb413@gmail.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.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Native config files sometimes can setup cpuset.cpus to pin some CPUs. Before this, LXC was using a fixed number of 1 VCPU. After this commit, XML definition will generate a dynamic number of VCPUs based on that cgroup attribute. Signed-off-by: Julio Faracco --- src/lxc/lxc_container.c | 23 ++++++++++++++++++ src/lxc/lxc_container.h | 2 ++ src/lxc/lxc_native.c | 24 +++++++++++++++++-- .../lxcconf2xml-cpusettune.xml | 2 +- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 88e27f3060..c5788e5c32 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -2487,3 +2487,26 @@ int lxcContainerChown(virDomainDefPtr def, const cha= r *path) =20 return 0; } + + +int lxcContainerGetMaxCpusInCpuset(const char *cpuset) +{ + const char *c =3D cpuset; + int max_cpu =3D 0; + + while (c) { + int a, b, ret; + + ret =3D sscanf(c, "%d-%d", &a, &b); + if (ret =3D=3D 1) + max_cpu++; + else if (ret =3D=3D 2) + max_cpu +=3D a > b ? a - b + 1 : b - a + 1; + + if (!(c =3D strchr(c+1, ','))) + break; + c++; + } + + return max_cpu; +} diff --git a/src/lxc/lxc_container.h b/src/lxc/lxc_container.h index 94a6c5309c..6f112e0667 100644 --- a/src/lxc/lxc_container.h +++ b/src/lxc/lxc_container.h @@ -63,3 +63,5 @@ virArch lxcContainerGetAlt32bitArch(virArch arch); int lxcContainerChown(virDomainDefPtr def, const char *path); =20 bool lxcIsBasicMountLocation(const char *path); + +int lxcContainerGetMaxCpusInCpuset(const char *cpuset); diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c index 02d2bf33e4..409bf00bd2 100644 --- a/src/lxc/lxc_native.c +++ b/src/lxc/lxc_native.c @@ -993,6 +993,24 @@ lxcSetCpusetTune(virDomainDefPtr def, virConfPtr prope= rties) return 0; } =20 + +static int +lxcGetVCpuMax(virConfPtr properties) +{ + g_autofree char *value =3D NULL; + int vcpumax =3D 1; + + if (virConfGetValueString(properties, "lxc.cgroup.cpuset.cpus", + &value) > 0) { + vcpumax =3D lxcContainerGetMaxCpusInCpuset(value); + if (vcpumax > 0) + return vcpumax; + } + + return vcpumax; +} + + static int lxcBlkioDeviceWalkCallback(const char *name, virConfValuePtr value, void *= data) { @@ -1132,6 +1150,7 @@ lxcParseConfigString(const char *config, virDomainDefPtr vmdef =3D NULL; g_autoptr(virConf) properties =3D NULL; g_autofree char *value =3D NULL; + int vcpumax; =20 if (!(properties =3D virConfReadString(config, VIR_CONF_FLAG_LXC_FORMA= T))) return NULL; @@ -1155,10 +1174,11 @@ lxcParseConfigString(const char *config, =20 /* Value not handled by the LXC driver, setting to * minimum required to make XML parsing pass */ - if (virDomainDefSetVcpusMax(vmdef, 1, xmlopt) < 0) + vcpumax =3D lxcGetVCpuMax(properties); + if (virDomainDefSetVcpusMax(vmdef, vcpumax, xmlopt) < 0) goto error; =20 - if (virDomainDefSetVcpus(vmdef, 1) < 0) + if (virDomainDefSetVcpus(vmdef, vcpumax) < 0) goto error; =20 vmdef->nfss =3D 0; diff --git a/tests/lxcconf2xmldata/lxcconf2xml-cpusettune.xml b/tests/lxcco= nf2xmldata/lxcconf2xml-cpusettune.xml index 6df089d00f..a1fec12d9b 100644 --- a/tests/lxcconf2xmldata/lxcconf2xml-cpusettune.xml +++ b/tests/lxcconf2xmldata/lxcconf2xml-cpusettune.xml @@ -3,7 +3,7 @@ c7a5fdbd-edaf-9455-926a-d65c16db1809 65536 65536 - 1 + 5 --=20 2.20.1