From nobody Sat May 4 10:07:07 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=1582554302; cv=none; d=zohomail.com; s=zohoarc; b=moaMXVk6eP5Pltych7eooKnmULXvo6yDCqfuxOrN8L489gFYm57zeQQylt+esu3BtAq0JhXsMc+0SOgz02HCDnVJMvG9B+fT1zJj4lfcNBN4DVp27P622uYWXj7O+dE3+5/OdMvxtBGnEPHGu/qnNdTT1O6yOcpVIYGKgxDSdMI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582554302; 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=Z9GWPFsRdM8/gfewDLDJGfE67Qa1qI5k0s4l60jN+gC25pEbRFglSI3BIiPl8AmFOYSWRRv/rJjzDRBHWVBHXe6HHx63Eb8Br10M8f8jW5L8N/l8i7edSNfMprWBlSGWKRJNcOhH8Y2/G6+/UjA9W7yRUXnQll8Mt59JPHdVo6k= 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 1582554302457747.1150217375449; Mon, 24 Feb 2020 06:25:02 -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-362-VBBIOieTPiGQII5V7Iir7g-1; Mon, 24 Feb 2020 09:24:58 -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 664C0107ACC4; Mon, 24 Feb 2020 14:24:53 +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 2FDD660BF7; Mon, 24 Feb 2020 14:24:53 +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 98D7C18089CE; Mon, 24 Feb 2020 14:24:52 +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 01OEOpMY023356 for ; Mon, 24 Feb 2020 09:24:51 -0500 Received: by smtp.corp.redhat.com (Postfix) id 0D49C10EE781; Mon, 24 Feb 2020 14:24:51 +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 08A5910EE77C for ; Mon, 24 Feb 2020 14:24:48 +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 6257885A308 for ; Mon, 24 Feb 2020 14:24:48 +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-84-j6bJKxNgNKOh6kq96JGTnw-1; Mon, 24 Feb 2020 09:24:45 -0500 Received: by mail-qt1-f196.google.com with SMTP id r5so6627647qtt.9 for ; Mon, 24 Feb 2020 06:24:45 -0800 (PST) Received: from localhost.localdomain (201-69-8-70.dial-up.telesp.net.br. [201.69.8.70]) by smtp.gmail.com with ESMTPSA id o16sm6092364qtr.28.2020.02.24.06.24.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2020 06:24:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582554301; 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=NDSa4iLqO5IXchACUcmy6XiQJqWtLAmSQ1zRL+bLr8CO3W6r0Pym20OSFakzDnOk+qhULY FyzAue0OqZFd4bqL71db6vv9HQAUgKuGmtnnIbm24KXt0DBJl92D1vPw42jKDG+Nq5zSnd nrK2HoPtUJHYqJURtQJYrrn3lj7M0QY= X-MC-Unique: VBBIOieTPiGQII5V7Iir7g-1 X-MC-Unique: j6bJKxNgNKOh6kq96JGTnw-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=lsMsnPHhivrrlFJOmdmgrNvPfUkNVT4qm9euvjwpNF7A6lIA3VZdq5claO/SOs4Kp2 332ezBNU8pSwwActYVZlxxc6bMc/s9T78iZ81VF5t4pgGFBIV5N4qQHqa4m5y7IY7r2i ouTG6mYyNKRvHalkDLYFYkgmtCd9TmReIXn5Rn/Cmc8HFutI54Yc6p5jaSuIpPjbRsgh pDBwcZD0R7Kw0qfKG1tzKa8ybHWU/sOrThRs1xg5LqNFuojFrY1tpinA6C6r5ptkLHrd ZeuJyklgh/lpLRrNBEzwSZd51vEQsQltmUvrOaZDb0I79PHbnhYsUtq+yJ0Z8k3fqwNP 4m4g== X-Gm-Message-State: APjAAAXLRxsb+rOF83Ij87wEyX+RhBtBF0i9mGk+EnMSR1F64Pu9bahD YQRmB96aZg77zavLgJV7nMjUO6bS X-Google-Smtp-Source: APXvYqwUq/3L+ASA8AFKSW3hyJPP7ac2KNnvRcoBtCRn5cF6nm0Fb7b9GxdFooWUA3YBA2jz9aCYBw== X-Received: by 2002:aed:2ae7:: with SMTP id t94mr47808898qtd.130.1582554284016; Mon, 24 Feb 2020 06:24:44 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v4 1/5] lxc: Add Real Time Clock device into allowed devices Date: Mon, 24 Feb 2020 11:24:24 -0300 Message-Id: <20200224142428.538-2-jcfaracco@gmail.com> In-Reply-To: <20200224142428.538-1-jcfaracco@gmail.com> References: <20200224142428.538-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 01OEOpMY023356 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" 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 Sat May 4 10:07:07 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=1582554310; cv=none; d=zohomail.com; s=zohoarc; b=oLL+hRzDPY6Z4EJoNaZ7KpkpJGH7FNbLf9DsKCuFkovoEPrubUA52HQqKPd4dW/lTMtiH9HjdJjQ+RPpXvr8snuXWshD/0I4738ar3XI7IZdkIBT0DTE2m8pfOy+DXg20OSyOYHB6C472J5RU3aKkAvjrRl2pzeZuZ8VqBVEPfo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582554310; 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=HS79YuNvOreN29tgb9YW/JxXzKVijyPIKL1UPaptCXSDTFT4NRcqTAE3y0LDqm34um6HGfUGX2QlHEv2vubpPagfEACnfAlGeZvCilmXGySmNTZW2+FeEZe30xS59mt0vEXq/oG6hsimSWOt1g78TgGVvVO3NNrLK7lCfnMoCt8= 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 1582554310279167.46793348596782; Mon, 24 Feb 2020 06:25:10 -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-45-vn2t3lDJO0OP7_t5Upf7vg-1; Mon, 24 Feb 2020 09:25:06 -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 680818018A3; Mon, 24 Feb 2020 14:24:59 +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 3DB13909EE; Mon, 24 Feb 2020 14:24: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 EDD0A18089CE; Mon, 24 Feb 2020 14:24:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 01OEOv4J023368 for ; Mon, 24 Feb 2020 09:24:57 -0500 Received: by smtp.corp.redhat.com (Postfix) id 686952063209; Mon, 24 Feb 2020 14:24:57 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 635A32026D68 for ; Mon, 24 Feb 2020 14:24: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 8AEAC101A55F for ; Mon, 24 Feb 2020 14:24:55 +0000 (UTC) Received: from mail-qk1-f195.google.com (mail-qk1-f195.google.com [209.85.222.195]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-123-u2aZWSCRPlOlkpVC3xVuZg-1; Mon, 24 Feb 2020 09:24:52 -0500 Received: by mail-qk1-f195.google.com with SMTP id h4so8834728qkm.0 for ; Mon, 24 Feb 2020 06:24:52 -0800 (PST) Received: from localhost.localdomain (201-69-8-70.dial-up.telesp.net.br. [201.69.8.70]) by smtp.gmail.com with ESMTPSA id o16sm6092364qtr.28.2020.02.24.06.24.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2020 06:24:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582554309; 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=GZBp2I2HpDxECInXRrTRr1+3rIBJUYlohrihYjLmBiAuCsmd2hBCt9BYPPcJMWvFoCrnve oUalxdEbAhit4CCYbbW6axsg2NYmkHjqMV7c3607dUHmONAx0bfR2vxrIRH+jZhObxDLex w8YCkTij3E0zkglCKJMrvavYlGNA6lY= X-MC-Unique: vn2t3lDJO0OP7_t5Upf7vg-1 X-MC-Unique: u2aZWSCRPlOlkpVC3xVuZg-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=dl3CZ/6ZYPBi0e02e524QqTvq2FsmRNYaWyJrx1ztFwEBhT00v9AVms8OmP2/abkUZ q9uLIbU0gaEWRgT91iLie5nFxVTiAVDRmlwsxRqDJgIAAAmbFUXiySqrGDSc7pWluKLs X1k0uvVWuhjtBta/7lLbuub4h5MKnFihvLZr/8HIc4gJsH3lB8tavUgdFMNSoT7r8ubB 7VQTI9ifjUww1DZU1jOG1u3BOjSrfvRx6tP3iNTJAAdwlBuLzpKIya3RBOeEs/K+mjDh fzb3DExXQ4nZ1xQ60JkPfbafSfqu33x1tih1DfBuWNabYQLTsHAWYmbd2bKvolxgIbJ2 PsCA== X-Gm-Message-State: APjAAAXpKYbQ2OmdTuhZLVWvoSoStHGQt4lCof4KKF1Q02zPqyK0BPe0 tY63MGyeMSf9OhMzjqeTKXutGd0z X-Google-Smtp-Source: APXvYqyrnYuwJNR4cTMOZ+BOal8STvwkXz/2GXIXeiGTZ7u1gXpWWj7+bQfO7j2era0VdTMqF6pDyA== X-Received: by 2002:a37:610d:: with SMTP id v13mr12372825qkb.484.1582554291379; Mon, 24 Feb 2020 06:24:51 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v4 2/5] lxc: Add HPET device into allowed devices Date: Mon, 24 Feb 2020 11:24:25 -0300 Message-Id: <20200224142428.538-3-jcfaracco@gmail.com> In-Reply-To: <20200224142428.538-1-jcfaracco@gmail.com> References: <20200224142428.538-1-jcfaracco@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 01OEOv4J023368 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" 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 Sat May 4 10:07:07 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=1582554325; cv=none; d=zohomail.com; s=zohoarc; b=jseQlGpglTg5gpFMb+hCVgseSPlp0JPZboVtSySIlF4KaGxBtZXVTIx/a4yODZtX7F0GXaEEGu91OeC/hH+q0VHUwOGWaCA1GeJs9USxKqs40nHLlb1lsBm9Y3T5SdQORcjYPjtj4QLSHObw+60LRTd8by0sn61XDqbVMvS+u1s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582554325; 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=eSTUV3iEKYauUOk5HxKG8g55wJy6snLptt9gfCswMKroG24/Kc153nLstKOYcTzkWu57oPgIoW2SXMyQArTRBVio/783HZnqeiiYJgqhzmIVtSHnRssmMODIqdPXJ38UDxVs/eEhzJbLB6d0vAn2skL/NQBzeXAV1FXBXm2ckS4= 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 158255432524296.1642792437425; Mon, 24 Feb 2020 06:25:25 -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-HdzGykdAMLu0lEcmb6pAQQ-1; Mon, 24 Feb 2020 09:25:20 -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 A3A1A1882CF5; Mon, 24 Feb 2020 14:25:11 +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 707B35C554; Mon, 24 Feb 2020 14:25:11 +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 16D22860D5; Mon, 24 Feb 2020 14:25:11 +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 01OEPAlM023389 for ; Mon, 24 Feb 2020 09:25:10 -0500 Received: by smtp.corp.redhat.com (Postfix) id 0B15CDBF2E; Mon, 24 Feb 2020 14:25:10 +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 06E8EDBF2B for ; Mon, 24 Feb 2020 14:25:08 +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 13537800298 for ; Mon, 24 Feb 2020 14:25:08 +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-121-qYYxpVEyN9mkaR5Lp7zUiQ-1; Mon, 24 Feb 2020 09:25:05 -0500 Received: by mail-qk1-f193.google.com with SMTP id f3so5859319qkh.3 for ; Mon, 24 Feb 2020 06:25:04 -0800 (PST) Received: from localhost.localdomain (201-69-8-70.dial-up.telesp.net.br. [201.69.8.70]) by smtp.gmail.com with ESMTPSA id o16sm6092364qtr.28.2020.02.24.06.25.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2020 06:25:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582554324; 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=EC2fomMbkqaRbWBDNIwOmdCAK6KuU9z93OWba+tI3tZSuxmsQOY5OdjgHkKTUkk0sPIUPz T+m3+xTiG31B8aB9qiu7boeJMr5jDSwKCygRw/jnIZXdHYzDMBVxzNhnILSmPU/7XuZ6co xHvgv5jA076oZiNADvwVPJGlgPepqMk= X-MC-Unique: HdzGykdAMLu0lEcmb6pAQQ-1 X-MC-Unique: qYYxpVEyN9mkaR5Lp7zUiQ-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=XaXzVRg0LHfxjn4NkkgqJajQlxxqGSzEpO0C/Tc+XB8UBzm4xbfythEUaRoM+wPuEk xE82aWUrNVpNYJKMT5rCDjBhnC5vA/Jl7gDrqbNXn9fivDTVZcy0gwecHkqpHEgT3v7+ mnpB5EpOuJOkZG05Ll2Ht/LkoYwcRrHH7GLZR7VHcviyljfQt9lVf/egKjD3mzMYw0bl exO4UJL1AgM2rV1cKvfpnNrvz+V48/Z4AnRe5d+tOK9RIVfSuP2ZaPMY5YX8uOohukVa C+shlTV98UqCkr8CRnYWfQwbqZk3QFKeAmzVkZjAEbKtvyfJRAcuke1kyAqd4l4z3Pkl XI6Q== X-Gm-Message-State: APjAAAWgMghyMCJYgBdIMediXwV4I++32tk2CXZPBQi9Y2drz1L+if4r Llo6Ni9scedRSfsEf/xYAcpsQN3K X-Google-Smtp-Source: APXvYqyiQ+mA8I7e2ZydPMcI5tts9vdxBEB/SehMJBZcpy1y5hCoYBlr/FEA7zGgraRZqNT+hmMRbw== X-Received: by 2002:ae9:e202:: with SMTP id c2mr3082495qkc.224.1582554303622; Mon, 24 Feb 2020 06:25:03 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v4 3/5] lxc: Replacing default strings definitions by g_autofree statement Date: Mon, 24 Feb 2020 11:24:27 -0300 Message-Id: <20200224142428.538-4-jcfaracco@gmail.com> In-Reply-To: <20200224142428.538-1-jcfaracco@gmail.com> References: <20200224142428.538-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 01OEPAlM023389 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 Reviewed-by: Daniel P. Berrang=C3=A9 --- 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 Sat May 4 10:07:07 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=1582554325; cv=none; d=zohomail.com; s=zohoarc; b=UAbqAhN8Uj/zUwDDYsgrYx4vAtyUufkcJ2gFI12w54clEIS6d6Gqjdq9llWVDKmpEughK6vSf8k2m66hpTOgKATEm2y8oGENPweQC8CvpfZuuEPAeDKYz5lVLEVXezVGK43HzFUbTLjVP5AceY3/cIrlt2pJFQRassNghVaIkcc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582554325; 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=9b2bSIq6yA9vV7YIO26zHanD6D+KuhJGnHFYH7Bm1pQ=; b=SIB0en8NWmodyDovnk3HKNcfqF0VkN2y/BU6qQ6PjMOK9+hnPkRtncEkJp5INH2iFZGgzsl4LIwsvKwiLVfQTiZyfnyohtPBuSVJLluocLOTyLRG2EE5EdO3/kxnD3ZqlRqcnXmgQ4m//m0Pm2LgWeIHgUCOJfS7f81g6EnwF7E= 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 1582554325693623.4629333719704; Mon, 24 Feb 2020 06:25:25 -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-129-JDZRHS5jMyqNF9xTb7Xdxg-1; Mon, 24 Feb 2020 09:25:22 -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 396528024ED; Mon, 24 Feb 2020 14:25:14 +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 08B859009D; Mon, 24 Feb 2020 14:25:14 +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 9B11F860E9; Mon, 24 Feb 2020 14:25:13 +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 01OEPBNW023404 for ; Mon, 24 Feb 2020 09:25:11 -0500 Received: by smtp.corp.redhat.com (Postfix) id F01759D44; Mon, 24 Feb 2020 14:25:10 +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 EB369DBF37 for ; Mon, 24 Feb 2020 14:25:10 +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 CA9B18007AC for ; Mon, 24 Feb 2020 14:25:10 +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-115-1c_t7SK5MvOL75vqu2J0Jw-1; Mon, 24 Feb 2020 09:25:08 -0500 Received: by mail-qk1-f194.google.com with SMTP id p7so8777472qkh.10 for ; Mon, 24 Feb 2020 06:25:08 -0800 (PST) Received: from localhost.localdomain (201-69-8-70.dial-up.telesp.net.br. [201.69.8.70]) by smtp.gmail.com with ESMTPSA id o16sm6092364qtr.28.2020.02.24.06.25.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2020 06:25:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582554324; 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=9b2bSIq6yA9vV7YIO26zHanD6D+KuhJGnHFYH7Bm1pQ=; b=hMMA4BEGQayt7BWtCjmz5YVVq+5df0E7YB2gLhYHhdRBV6701/k/SKIfhuz3O3JZcIEKsa O3Nz/Ruqm+15lG6BkhvrHUbPS72x5ZVG0TFgay9PIipPgEeF0lbO+vREuwEDh/tlyVAPqo 38ysAnnz1gBgUm7JSJuMl2Q5Q81/vqI= X-MC-Unique: JDZRHS5jMyqNF9xTb7Xdxg-1 X-MC-Unique: 1c_t7SK5MvOL75vqu2J0Jw-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=sfVNFJSYgCBsrNaWIIxxvv7YjVueH7/oa9otHcbPrMI=; b=TDmVmJ7B3TopU+8pUXC+to6XfrraAGlPx+J1wohTgvWFazbt8eEXcgl7+VvX2lsccQ b/zbHQNxOOx/QVnNl5o9JY6qC/2LEt+mKIU/YArSGA2mC1K4EQWokLO1jJ1tvvyd1XPo 1EUUYM64dXYSOhNBNCx1QtqHO/C9K1E4majQDbozGJyQqz0QkKUGKNy3cZPnpxvO0j9P eEfLDy2r8FsAf91I4rMkG2cUlV+MCJzzO3Jj6hPZ1I4lbWX751fSBH8x30vihoNRwFhJ CKEyWdEzMbkncDfPv5ARnbRaMmUNcx0Vbsg2ptEd7mspm8rgaDm5XU5iLUnM70d/yQA2 YTtQ== X-Gm-Message-State: APjAAAW8DOOYzM2lfutoj0CToMBsfP5fxrIl6YlJpVpZUf8FCnTolb3r 4v5RfxNuuZhIcNlbxEq9FK7BCow3 X-Google-Smtp-Source: APXvYqyIGoODZDlP4dOMcnCUidl2J+mOPTTKtlLiSQcJc4oYOa/2lsglA+4G6xtBTFyXr2pp3TSViA== X-Received: by 2002:a05:620a:5e9:: with SMTP id z9mr8305881qkg.255.1582554307225; Mon, 24 Feb 2020 06:25:07 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v4 4/5] lxc: Implement virtual /proc/cpuinfo via LXC fuse Date: Mon, 24 Feb 2020 11:24:28 -0300 Message-Id: <20200224142428.538-5-jcfaracco@gmail.com> In-Reply-To: <20200224142428.538-1-jcfaracco@gmail.com> References: <20200224142428.538-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 01OEPBNW023404 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" 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 | 95 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 145 insertions(+), 20 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..b2117bfa17 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -36,23 +36,29 @@ =20 #if WITH_FUSE =20 +#ifndef CPUINFO_FILE_LEN +# define CPUINFO_FILE_LEN (1024*1024) +#endif + 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) + } else if (STREQ(path, fuse_meminfo_path) || + STREQ(path, fuse_cpuinfo_path)) { + if (stat(procpath, &sb) < 0) return -errno; =20 stbuf->st_uid =3D def->idmap.uidmap ? def->idmap.uidmap[0].target = : 0; @@ -83,6 +89,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 +97,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 +235,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, CPUINFO_FILE_LEN, &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 +328,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 Sat May 4 10:07:07 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=1582554330; cv=none; d=zohomail.com; s=zohoarc; b=iJKJ1HkTKm5moMUNyQYsBgxF9MBJhdy4xdYN2rEqq3Vikzgi6O/vuvubeAi7PyySOb7kIS69L2GexgnBMspo2XdwmomXP1XKWRlTXAImWJ/I69CajfqMahz+vN7M4bLjTAeK7t0QrlfnIx60NTjpOodlHXdmT2Z1kIDJU8dk8rw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582554330; 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=avz49JMCZVSOFc5eEuqBMbJhJ1Arknczpjnuy1lYuwOa02o5VuS/YWG/npE/brQz7N0nEwIkBkR0IPKvPsXMJK+gDJmklIxMgibCnncExz4jB6XsBnFdDOPPFF4JIwnMe9ElKe2diG2zdc/XmvOb80cf+wzZC/uzMSTOvuB3mSA= 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-2.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1582554330214111.01386364181292; Mon, 24 Feb 2020 06:25:30 -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-64-vRiMgLmHNaKlKw10X0-b9Q-1; Mon, 24 Feb 2020 09:25:26 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 44494DB88; Mon, 24 Feb 2020 14:25: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 0FB0691833; Mon, 24 Feb 2020 14:25:20 +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 AA436860EF; Mon, 24 Feb 2020 14:25:19 +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 01OEPIfL023417 for ; Mon, 24 Feb 2020 09:25:18 -0500 Received: by smtp.corp.redhat.com (Postfix) id EB7749D44; Mon, 24 Feb 2020 14:25:17 +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 E7623DBF39 for ; Mon, 24 Feb 2020 14:25:17 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.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 CF11B8007D4 for ; Mon, 24 Feb 2020 14:25:17 +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-236-xmqByRQpO5SwhRy8nw_vaQ-1; Mon, 24 Feb 2020 09:25:15 -0500 Received: by mail-qv1-f68.google.com with SMTP id q9so4190392qvu.7 for ; Mon, 24 Feb 2020 06:25:12 -0800 (PST) Received: from localhost.localdomain (201-69-8-70.dial-up.telesp.net.br. [201.69.8.70]) by smtp.gmail.com with ESMTPSA id o16sm6092364qtr.28.2020.02.24.06.25.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2020 06:25:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582554329; 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=BdzM7Q5myRnQ9lEtw2M7aeMAn0U4XDD/GRaaBsiQ0B+5JH2p1w8ilTS38WZKhr/fVwPySd +nsPrg0nbgLk1hSkwW9UNPNg+hUuq0FNDVWRsqVtTn6Qs/2SIpdQA/ZBy2/qlR3SCPob5v nSErJ921XK2a5aLG8sxJRrn72iu8Qyg= X-MC-Unique: vRiMgLmHNaKlKw10X0-b9Q-1 X-MC-Unique: xmqByRQpO5SwhRy8nw_vaQ-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=LGZSdIJ2L1MNkRr/i0+ydbBN3nwkNIiR4A4LWoI5Lu9rAU/n2MVjmsJYAQe2XEqsfz hdCq9AOqce1aaPFgYGFTxhP7JJ7J3nREAC8HtVRpBX8ZwTJ4lzOTuF73OKd/f+GIzf6+ 0/vkdrJyE/GyFzsgNMlMzPycMLd5AAWSY45vj3NhS1A9Cqui375r89lne3RkjyqWnJPI u1uKMEzVaMne2cMHOzvdjPzP59SuhnEV5yK3NJ+0eEbvezw5gwReSvjEC/0rsYTpgcmp 70AYVEWtLCJyfnGlzYghkLKanSJutEuODPaAAWZd5B6Z2k43nHvw4RIpN3LiZPSAVQxL Bxxw== X-Gm-Message-State: APjAAAUyaBtRvoxU8If0viBBQJ3u6BKt+/Of+L6N/4HoOqJpN4ggytTg SqaSsaK4Lv4OBsJGd6QYwYcbmBP/ X-Google-Smtp-Source: APXvYqyfs60IuRTMr7NxkNTJFHuMCkZiWgPXptxzUr/1IuEBAdX28wBcGHMLWvNWS/vgMrWdVWnyyA== X-Received: by 2002:a05:6214:524:: with SMTP id x4mr45491336qvw.4.1582554311649; Mon, 24 Feb 2020 06:25:11 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH v4 5/5] lxc: Count max VCPUs based on cpuset.cpus in native config Date: Mon, 24 Feb 2020 11:24:29 -0300 Message-Id: <20200224142428.538-6-jcfaracco@gmail.com> In-Reply-To: <20200224142428.538-1-jcfaracco@gmail.com> References: <20200224142428.538-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 01OEPIfL023417 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.15 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