From nobody Sun May 5 12:23:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; 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 207.211.31.81 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=1582516529; cv=none; d=zohomail.com; s=zohoarc; b=eHqDIyHFI18yLVV9CcoOYHRdOmJ3Oyn3hhUc3wWi04iu6qkgq7Nfc/b5/I9Omy2ecJsttbssO3Vs1t9KYXYw5yRM+GgKV/RU51uqdqgPJw9HhMP4WBLVL4LG7iw4BtSOpQLDmtGshBsbaLN2zX9C2yEQCsaydpHGgPGTXAFbSJg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582516529; 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=nRT4AOpD8PybJShqX0JbTeJuAY8cZDa0ML6cs5+Ln2toxDNePdOs5iZbKMfzTbPNYd4n5nHJ2OAZZb6o1C/ZQ+cxovZpr5oFF/cDvpt9gxv80q1xurqViKsV8Lf/SdYdtX/mcUUA4tkHcaEODhWPi5JrYuw7iouT2f/yqn3vdVw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1582516529607562.7343807489239; Sun, 23 Feb 2020 19:55:29 -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-451-Ld2sR-EvMgGfyElzJPAzeA-1; Sun, 23 Feb 2020 22:55:26 -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 50B45100550E; Mon, 24 Feb 2020 03:55:20 +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 0AD5E165F6; Mon, 24 Feb 2020 03:55:19 +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 1F855860E5; Mon, 24 Feb 2020 03:55:18 +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 01O3tG6p014585 for ; Sun, 23 Feb 2020 22:55:16 -0500 Received: by smtp.corp.redhat.com (Postfix) id 82556AECB0; Mon, 24 Feb 2020 03:55:16 +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 7C99CAF98B for ; Mon, 24 Feb 2020 03:55:13 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.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 BAAB18007A4 for ; Mon, 24 Feb 2020 03:55:13 +0000 (UTC) Received: from mail-qk1-f194.google.com (mail-qk1-f194.google.com [209.85.222.194]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-185-1mJZqoISMAClzIj-GXj_ag-1; Sun, 23 Feb 2020 22:55:11 -0500 Received: by mail-qk1-f194.google.com with SMTP id p7so7551572qkh.10 for ; Sun, 23 Feb 2020 19:55:11 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id 64sm2652392qkd.78.2020.02.23.19.55.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 19:55:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582516528; 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=XeCnFpKW27edHDN5j/vmtbp0rJPL+9Di++xPE9wmsuxiBjt2iPb69rIAZU7N80xZzrTHNq u7vxrU5QIp+DZSU7C/89ecmBC6Of76ehSra7hqOLtZ2h0sW8eUxWszhpYIb5pa7fP0+T2E Uc78HBCdTnq1LoJscS6l64cS9Tc8VyY= X-MC-Unique: Ld2sR-EvMgGfyElzJPAzeA-1 X-MC-Unique: 1mJZqoISMAClzIj-GXj_ag-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=TXnchLW6EbjwuUpWhpN28ycLEtuy34kWu0W7YcRcyjJvyy0NXuhwpT9EC170F8MDlA hi0QkcqzIACp8eMCUREhmPiotZBZPcI9w0D3NDwg1ELCjXm3QKZDowyQBX4BHgfLQtuP eTC2wtcQMtAuRiXIGb1RAIGxeY0vbX5t17klR8LctHPSC9WA9ZrqKQ0FCMtRw9E2dO+w b+8VtSzEYE8VyTGU4ZsWzrmonRTEIk6VuwOvd7YfALc4TUrLl7FwZ9DZeNSF1ZszlBIQ /YVVHEk/xNFgFcVV1UUeP+HkCv3wVoz341H0dElQ4ix2gJerhVKq1mydmEgMued4xeTG dbKA== X-Gm-Message-State: APjAAAWgaEv2mYmvD4HGxsAkwmKNQ2phU0Z5Z8PQDxEOfiJsVRBiLXvz C4qunL+gyOcszbE95oTO+Y2dJlAn X-Google-Smtp-Source: APXvYqyI3OY8WLlN2/tjAa/CyGFzLh/fGIzGNl1Surs1D+hQLM0JbZPwq3HpAhyyXtXM3EebjoGTBA== X-Received: by 2002:a37:5285:: with SMTP id g127mr46462888qkb.315.1582516510507; Sun, 23 Feb 2020 19:55:10 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v2 1/5] lxc: Add Real Time Clock device into allowed devices Date: Mon, 24 Feb 2020 00:54:26 -0300 Message-Id: <20200224035429.3973-2-jcfaracco@gmail.com> In-Reply-To: <20200224035429.3973-1-jcfaracco@gmail.com> References: <20200224035429.3973-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 01O3tG6p014585 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 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 Sun May 5 12:23:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; 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 207.211.31.81 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=1582516533; cv=none; d=zohomail.com; s=zohoarc; b=HH+94lRno/p/pHttRwK9FHwgcYLMynniI0NYxsGlvxQxF7o2Lq8TNYLgMwDIHPvjoO5xv6hdO8ElQl7zMxmyDiWw+ldjyaCLQV6+1ePUsUtXHYVMvDwgcv4EiJmK1I9qgNAVRF5o+ST48oL6y0UxFVCtI0m8w0yfpc9uONzn15s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582516533; 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=mOCdW8xGdhQguxKmJ+W8Zdl/GxDxAcSZRzZrNycgERb6iVhOXLiQzxqhY2Esp0BVUcguul4AZ+DBVVCV/U7ch4B0CuT9/rcDtZ21Beu2wdNwappdzpLiY32twZN4qRi8HnsrcRVPh5MHMe68WNjuunlQlLaMwIsB8NPBTf4PQCc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1582516533759352.89762763498516; Sun, 23 Feb 2020 19:55:33 -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-197-F8-DgMrqMRqw2nbVe2nBbw-1; Sun, 23 Feb 2020 22:55:30 -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 037DE1408; Mon, 24 Feb 2020 03:55:25 +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 A5C493A2; Mon, 24 Feb 2020 03:55:24 +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 5D52F18089CF; Mon, 24 Feb 2020 03:55:24 +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 01O3tK2e014596 for ; Sun, 23 Feb 2020 22:55:20 -0500 Received: by smtp.corp.redhat.com (Postfix) id 32D542166B2E; Mon, 24 Feb 2020 03:55:20 +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 2E89C2166B2B for ; Mon, 24 Feb 2020 03:55:17 +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 941D8800260 for ; Mon, 24 Feb 2020 03:55:17 +0000 (UTC) Received: from mail-qt1-f196.google.com (mail-qt1-f196.google.com [209.85.160.196]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-28-rZr_MUx3OcGbRCynBTaC3A-1; Sun, 23 Feb 2020 22:55:15 -0500 Received: by mail-qt1-f196.google.com with SMTP id l21so5690729qtr.8 for ; Sun, 23 Feb 2020 19:55:15 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id 64sm2652392qkd.78.2020.02.23.19.55.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 19:55:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582516532; 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=jHi1Qbzz7g9LP9KfsqPG9mhnLKIbTk415nq6zRX99o1ixvHauTpzJ1lJxAM9SXtaT9cZ5M hTq8Rnqs5D4xWFbrgJeXkHFXxzzQDmt/B+g3H+dvZpxdcfXY6l+gMUPcLMoBaqMFkQtt3v 7zmKAd+l+prRnfGqP0lz8EhwRaypV5w= X-MC-Unique: F8-DgMrqMRqw2nbVe2nBbw-1 X-MC-Unique: rZr_MUx3OcGbRCynBTaC3A-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=sdhXEjJeDd5YSPzmW8BUsFmEsikWzjooH6dpdpSPzgKrhH8JqgeVBG4CouD2lXSQuK WBeSYOX2vtgDWf7lr+2i1Bw4O3FvpJY+jJ8IRZSaPOaY9mz58W78bh8sD6aJgcpDNI9o o9Esm5mfDp4RDh5/sPgOuV+DINJb0U0/A7YjKc6fqx94q2Im6K+e4XqaRWRkxUNKtu3q Cjli2hsOUajsp9flnTtkLebJU9WWLumAKhjTSvWd6U2vNDqPdtVtufGEkcFljQfU2+hk lYT9a1l8IPinEc27Z6Avaeu55mVLasabRXexvCmmQEyKjroJnHsX0fU5onUdrEJmCy8q Xuog== X-Gm-Message-State: APjAAAUleu131aPkS7CRYw26mR90alfoZZGjScSqkk9OWKkzsGw49L7m pc+9Za5EPPFmv9L7wwRx7REXRXr9 X-Google-Smtp-Source: APXvYqwIjDL1sqV8E2CftEjIAISzFG/BCjiT6rh+Dfv0PzwZF1Yq8EFhLu5YS4vVLaKUaj73Bl2vuA== X-Received: by 2002:aed:2510:: with SMTP id v16mr45518708qtc.306.1582516514552; Sun, 23 Feb 2020 19:55:14 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v2 2/5] lxc: Add HPET device into allowed devices Date: Mon, 24 Feb 2020 00:54:27 -0300 Message-Id: <20200224035429.3973-3-jcfaracco@gmail.com> In-Reply-To: <20200224035429.3973-1-jcfaracco@gmail.com> References: <20200224035429.3973-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 01O3tK2e014596 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 Sun May 5 12:23:21 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=1582516543; cv=none; d=zohomail.com; s=zohoarc; b=Alb+djKuqB/ldqiq5125h/liqQspvvQLH6wulTAQgcxfybiECtcUVV3xwZh8YjNM1LUd3RzBEzelbWsPxR1NoypeG/HyxWcNPPOLHCeTod55DnBiWnS/qhE/OoPTGdj+3AsT60hycklLI1XyCIs81QtaC6JzBV2ldn1hj8A7zeA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582516543; 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=gb0I0z4VQcZel23XDn63ZCdl8jMTRhb6teF7lvJEH5XQaJ7QOGRTCPeKO5LfxwqxBgwN6NhvZGtwyYtJUkFcdZFPdDTrA3hM0qwHRxDscWd5ZbxS5mGzurv34QgkYsDF8Q3E3S4SBfdLjMlTlz/u/OR7H8Io+KDhQqDtxdXhrnE= 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 1582516543251173.79024240297838; Sun, 23 Feb 2020 19:55:43 -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-29-7Kqq93BiNVG0IspL5dmgNw-1; Sun, 23 Feb 2020 22:55:39 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B88708010E8; Mon, 24 Feb 2020 03:55:33 +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 8E1725C241; Mon, 24 Feb 2020 03:55:33 +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 435A9860E7; Mon, 24 Feb 2020 03:55:33 +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 01O3tN5p014610 for ; Sun, 23 Feb 2020 22:55:23 -0500 Received: by smtp.corp.redhat.com (Postfix) id 775D3AECB0; Mon, 24 Feb 2020 03:55:23 +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 72FDEAF990 for ; Mon, 24 Feb 2020 03:55:23 +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 5547D8007A4 for ; Mon, 24 Feb 2020 03:55:23 +0000 (UTC) Received: from mail-qv1-f68.google.com (mail-qv1-f68.google.com [209.85.219.68]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-314-DgvQiP1GMQGK0yw72gr_oA-1; Sun, 23 Feb 2020 22:55:21 -0500 Received: by mail-qv1-f68.google.com with SMTP id ek2so3621860qvb.0 for ; Sun, 23 Feb 2020 19:55:21 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id 64sm2652392qkd.78.2020.02.23.19.55.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 19:55:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582516542; 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=Fl207BkbjV/MbQOhmntEFIiK0kwGRSm75cQZUqUQ/+F9UTc7zJvmXP3kxh5C6lbtVNVBqq l3seMJkY72ynmuZMyZYK1prvFsx3X8ZE8/Em0OG1KYNCp34lGpMlTYxM7b509k75e+CUr0 bRUOmKpha53mKkJlgoYWrX3jqLDzsVU= X-MC-Unique: 7Kqq93BiNVG0IspL5dmgNw-1 X-MC-Unique: DgvQiP1GMQGK0yw72gr_oA-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=qLbHUox+InpYJg5YTyEOG8V/wX/+9eaBL7q62MEfYVQsOyLrlwlnoA/A9mF0UTT9fh u8xVHYnfSJaDgmss5yepstjsjXQCpc4CFh6gBLr0mV4PvzZz5I2bDasZtg43FeJ1Q34x VC9DXI3BIdwsWeDJSxx8JB5cjNqk/YBcZ2uFO6WKp9JnmNqpeHFk/w58DnzHtINKRv5Y qLgNLswX0S3aAopRUpta4S3msBhRHBkra1Y5g7/yLFmGXHpvz4fI1Wcd8ToF/eafUH9E PZpMN0xw/EYZmT6ECwNNCP6bUz2LDVxXukUT+jjb+GbLOVOe95i0koS+TYYZB5oEeUMs JhVw== X-Gm-Message-State: APjAAAXKmuy+HhUM9coh42iRzhKS+VkiM2V/780HKxeeqj2t9mJ9adeh QgTGeWSHppGjeM/cvhdCx433wIpc X-Google-Smtp-Source: APXvYqy+zXnmy4XpTzk74QAmXHuXne37xnySbWIThszDFQ5KGE1tgVHTLqDLy26wFedV5/bhry9x4A== X-Received: by 2002:ad4:4e34:: with SMTP id dm20mr43692847qvb.163.1582516520024; Sun, 23 Feb 2020 19:55:20 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v2 3/5] lxc: Replacing default strings definitions by g_autofree statement. Date: Mon, 24 Feb 2020 00:54:28 -0300 Message-Id: <20200224035429.3973-4-jcfaracco@gmail.com> In-Reply-To: <20200224035429.3973-1-jcfaracco@gmail.com> References: <20200224035429.3973-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 01O3tN5p014610 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.16 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 Sun May 5 12:23:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; 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 207.211.31.81 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=1582516609; cv=none; d=zohomail.com; s=zohoarc; b=MwvJ32FEN5z2o38gf6JveYdJH2XElQ86mquBXPdbIgCiS6aAWYqrspL3DAv+68Zj19wjWFdju13aPGYeZF0E3U2CBnMCDmGRm6bXmH4IbCM+Wb9Rf9bSGG7W+CZMI5msZ177kcesmgDekqInomG43dyr+qqAITWyT+YfC36TpFU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582516609; 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=3Dz38pddf780X1NlMkH/rexHyfq9tMG+UQ6p0ZaAeME=; b=D//gk3K0xm45GGFFMdk2rq3xqJZh7gkI4JHN2CNYFWQ+QQZdtEYtp04F/uhHOPCQ0C/o0SqpAprLWVq5UEkDLAkuBnR2DQWrB6gWp+AHCVOEHhAsnVTnbWIwez4vlJB0wNNjrAqXEgvApQIrVVqlfg+P6A2OACUw2IkinlL3vNk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1582516609081166.40485489413095; Sun, 23 Feb 2020 19:56:49 -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-437-tYuYrIZlOJOFkyhdxHnZAw-1; Sun, 23 Feb 2020 22:56:45 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 48EE7800D55; Mon, 24 Feb 2020 03:56:40 +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 1DF6B5C1BB; Mon, 24 Feb 2020 03:56:40 +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 C6D4718089C8; Mon, 24 Feb 2020 03:56:39 +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 01O3ucSg014682 for ; Sun, 23 Feb 2020 22:56:38 -0500 Received: by smtp.corp.redhat.com (Postfix) id 996C2AF988; Mon, 24 Feb 2020 03:56:38 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 94F1BAF996 for ; Mon, 24 Feb 2020 03:56:36 +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 E9A5F85A308 for ; Mon, 24 Feb 2020 03:56:35 +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-106-MfNUXrvzNNuW7j0fZOlELA-1; Sun, 23 Feb 2020 22:56:33 -0500 Received: by mail-qk1-f196.google.com with SMTP id f3so4631127qkh.3 for ; Sun, 23 Feb 2020 19:56:33 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id 64sm2652392qkd.78.2020.02.23.19.56.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 19:56:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582516608; 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=3Dz38pddf780X1NlMkH/rexHyfq9tMG+UQ6p0ZaAeME=; b=W3WAhmfdgYjcDSYHm+Ahh9quCIQqBvPivZWshscsdt8ajhzJ4gO0kct7K/rxDo37GDkQlz VXaqBLwiaiGwJNBW8e9as/mILcshjFYr5o3CmkJyNNkM4q1c+d9oki3+3vPDofXscw3xov qQirnDs2sAoCjIzyzQowOsaqVwlnjwM= X-MC-Unique: tYuYrIZlOJOFkyhdxHnZAw-1 X-MC-Unique: MfNUXrvzNNuW7j0fZOlELA-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=gy/o9XxZm3ez4FPBXO5D6GMLWB9dR46CI9f6/42x9pk=; b=BgG2740Hob4rA2kpGQV+jjeMmy16Uf7vFTDtxlluuldDppoqwSq4BS7696RCvciWdF 6s45CMOW2Tp+xwUSHV61ycvR6xe+2Fqzqb7cQMmOb6Jxdw48UkmoVuDSsUFKNi6Gj3t5 RvZLV5O6Vty9O5lzjJnwT74zB/ukFQQIRyVcWWlS53cDX1+E//4KdgSPk2vQu9nsIpi2 qfwy1JWWSx9KWTYIGqTwQ3+TbP/PAiQjMmCDvkgfbI2hQqrQCocOxVxnAydjXR5wy8Qb wwdxA1US0wjKGH3kF9qVGhc2Mn4vcda8NN+GOLj5RWatFac6c+GDACUU72hTeQwB7MYE nZNQ== X-Gm-Message-State: APjAAAW1U5rSYl+xrgWXIs+qM+wUaAuuSk5TcFcI/I2kIvgCU4k20N7k j6xQ/l71ZaKJPGbAlxuHvda2/mQO X-Google-Smtp-Source: APXvYqxLGiO+tAgsZSyyC4K7lnT+5Wtgm3eDYjtRvs58DNLKGPKJSwatsEgH40dSH/LX+HeLYNfeAw== X-Received: by 2002:a37:9683:: with SMTP id y125mr10128114qkd.450.1582516592730; Sun, 23 Feb 2020 19:56:32 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v2 4/5] lxc: Implement virtual /proc/cpuinfo via LXC fuse Date: Mon, 24 Feb 2020 00:54:30 -0300 Message-Id: <20200224035429.3973-5-jcfaracco@gmail.com> In-Reply-To: <20200224035429.3973-1-jcfaracco@gmail.com> References: <20200224035429.3973-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 01O3ucSg014682 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.16 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 | 37 +++++++++++------- src/lxc/lxc_fuse.c | 86 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 137 insertions(+), 17 deletions(-) diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index d29b65092a..b4b803e11e 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -61,6 +61,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 || @@ -79,6 +82,34 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr d= ef, } =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 virLXCCgroupSetupBlkioTune(virDomainDefPtr def, virCgroupPtr cgroup) { diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 41efe43a14..3b1ac881ba 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -997,8 +997,7 @@ 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; =20 VIR_DEBUG("Mount /proc/meminfo stateDir=3D%s", stateDir); =20 @@ -1006,15 +1005,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, @@ -1028,7 +1041,7 @@ 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..70dc68ec85 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -37,16 +37,17 @@ #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; @@ -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", -1); + + 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", -1); + + 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 Sun May 5 12:23:21 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=1582516614; cv=none; d=zohomail.com; s=zohoarc; b=O0mb1YWaT4T3giBZCcOJQ/nm41zOoFmPXg1zVAXYE9m7GEK9bqdN1kAGw1kbYa39SXd/b26BTBiTHBL+4BZqpUrG5CHC5JaknkdJysy5mYxT9olKu0bppGnEY1moPFC8LJY+CD4u8ES9NjBFb95lIBoWKKdzKfjCrFiT9AYgwgc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582516614; 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=e3hjBoG1GAItCC3L2BHg2uB6JhRMVz4ild6XaEX4M7k=; b=eC7v1ii0dcQARr9jxNU8GtaN/ls//rja1og6+QEAES6DAlKz0sA7OfkGK2fVeRo9GzmB/DBdHVZRXQM9lfRseYHeem3Z/Vmtc8hqXdhGIaJcQFy3Al/LBfx+0lJDgZ2e+PJhcqctpxWGSyGc4k/jTylCyHXkMRWTBc3LiVAsoKs= 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 1582516614617746.3845994359316; Sun, 23 Feb 2020 19:56:54 -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-472-V_Mu-Xg-MduMWagc7hYyRw-1; Sun, 23 Feb 2020 22:56:51 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9A86F107ACCC; Mon, 24 Feb 2020 03:56:45 +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 75F9B8ED1A; Mon, 24 Feb 2020 03:56:45 +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 274F218089CE; Mon, 24 Feb 2020 03:56:45 +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 01O3uhdq014693 for ; Sun, 23 Feb 2020 22:56:43 -0500 Received: by smtp.corp.redhat.com (Postfix) id 4003C1004C79; Mon, 24 Feb 2020 03:56:43 +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 3AD8F10EE6CC for ; Mon, 24 Feb 2020 03:56:41 +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 19DB58007AC for ; Mon, 24 Feb 2020 03:56:41 +0000 (UTC) Received: from mail-qv1-f65.google.com (mail-qv1-f65.google.com [209.85.219.65]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-275-dT7mVedoPaicgXrhom1xEQ-1; Sun, 23 Feb 2020 22:56:38 -0500 Received: by mail-qv1-f65.google.com with SMTP id ff2so3608831qvb.3 for ; Sun, 23 Feb 2020 19:56:38 -0800 (PST) Received: from localhost.localdomain ([191.254.236.241]) by smtp.gmail.com with ESMTPSA id 64sm2652392qkd.78.2020.02.23.19.56.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 19:56:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582516613; 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=e3hjBoG1GAItCC3L2BHg2uB6JhRMVz4ild6XaEX4M7k=; b=InqHUa9Q+VYWns9slg39zyH8b+in5L+fvTpzx1y1OIuB4gFVKLU0GJg2O5DKimYlrKDwjz W1WuWpwQrsrXHyZOEu0cSkEuUJtfK2grupVHct3ISJIHQx2jmq/Ls5ZHpmQcACw6rPM9HY +sXv28t4VKFeh9kJoNNlDk1MonBir8c= X-MC-Unique: V_Mu-Xg-MduMWagc7hYyRw-1 X-MC-Unique: dT7mVedoPaicgXrhom1xEQ-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=RsYEJFoYV4RTPQ0PSP9dXQixDAu5JbRtiFbprMt26Ws=; b=ASRpZS1lOh3q5Ud+tmNH4LmYoJxaiI3ZlbbaDeviVf5LFrYxHGaiQgFqJCdZUxXz3O +vqCEnxUBl/cZIjQaB4zVNHVEIPBfS3UAl1R1q8wcXHPNKKEZFhIIFDJ8ZjqoGoKi1Wv NXcDEs3f29j6taoLfCbb9a9COUbqeKGV/abQA7o0IrvYB9476amLE9IjKuX8I1Yj+6L0 vqR7RzXmLihbdpdEAKYqGkyvwDchU8TxFwZ0mo3TjpsXM0bM0hACBFONrakCdyXXRlg/ cCxSPDurFqiMhbTMcvbxWpQ090dUNvhjRnjeTdyMC/SC4h/0kzap/9BCtwQcdTqBZCjO rkhA== X-Gm-Message-State: APjAAAXjc/l+dYt66G5Vfuctikj5BuqeHVdiNEPmFvaaDXHuNTRspjCH OgmEvnpll2Ekfal75fiBzIR2zDED X-Google-Smtp-Source: APXvYqyGEhwtYnn3WdjS/P2iwwExIvtCE1CZiaaogyRrN1wdtSheF7kXhR+sU2Dqstgfa3av3RnHAQ== X-Received: by 2002:a05:6214:10c1:: with SMTP id r1mr42221798qvs.70.1582516597873; Sun, 23 Feb 2020 19:56:37 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v2 5/5] lxc: Count max VCPUs based on cpuset.cpus in native config. Date: Mon, 24 Feb 2020 00:54:31 -0300 Message-Id: <20200224035429.3973-6-jcfaracco@gmail.com> In-Reply-To: <20200224035429.3973-1-jcfaracco@gmail.com> References: <20200224035429.3973-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 01O3uhdq014693 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.13 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_fuse.c | 4 ++-- src/lxc/lxc_native.c | 24 +++++++++++++++++-- .../lxcconf2xml-cpusettune.xml | 2 +- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 3b1ac881ba..63b55c397d 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_fuse.c b/src/lxc/lxc_fuse.c index 70dc68ec85..dc888749bc 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -252,7 +252,7 @@ lxcProcReadCpuinfoParse(virDomainDefPtr def, char *base, break; =20 if (curcpu > 0) - virBufferAddLit(new_cpuinfo, "\n", -1); + virBufferAddLit(new_cpuinfo, "\n"); =20 virBufferAsprintf(new_cpuinfo, "processor\t: %zu\n", curcpu); @@ -268,7 +268,7 @@ lxcProcReadCpuinfoParse(virDomainDefPtr def, char *base, } } =20 - virBufferAddLit(new_cpuinfo, "\n", -1); + virBufferAddLit(new_cpuinfo, "\n"); =20 return strlen(virBufferCurrentContent(new_cpuinfo)); } 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