From nobody Sun Feb 8 19:39:52 2026 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 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 1581880346566237.29591306010002; Sun, 16 Feb 2020 11:12:26 -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-242-MszegoZQMjm00qHotk96_Q-1; Sun, 16 Feb 2020 14:12:22 -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 4CB6D107ACCA; Sun, 16 Feb 2020 19:12:17 +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 E857F19488; Sun, 16 Feb 2020 19:12:16 +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 EE19018089CE; Sun, 16 Feb 2020 19:12:15 +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 01GJCFaS010732 for ; Sun, 16 Feb 2020 14:12:15 -0500 Received: by smtp.corp.redhat.com (Postfix) id 3F54210EE788; Sun, 16 Feb 2020 19:12:15 +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 399B510EE789 for ; Sun, 16 Feb 2020 19:12:13 +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 0C3E885A316 for ; Sun, 16 Feb 2020 19:12:13 +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-113-Y5sYeL5EOjG06Ii31smT5Q-1; Sun, 16 Feb 2020 14:12:08 -0500 Received: by mail-qv1-f68.google.com with SMTP id y2so6644218qvu.13 for ; Sun, 16 Feb 2020 11:12:08 -0800 (PST) Received: from localhost.localdomain ([2804:431:c7cb:c465:a057:c890:16dd:11aa]) by smtp.gmail.com with ESMTPSA id c21sm4054063qkj.130.2020.02.16.11.12.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Feb 2020 11:12:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581880345; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=GE9Jd4nJqUq8qJd4qYCxfaFaT/V8jZZ+aumajbCAzbs=; b=e/Lvzt8NH8wGSO1EmBbZyMB3ZsXwADlrSf+V2Jz8T+YMXg9c9w5MkAgXBO6qILEVlTcjy0 y9aKZXY+tgyyFqKBpODVCuK9wDngUj/ZGrXYF0mwnYeNhfyDoy3mDW+9NDeXvpoqiLHVGh u4ZH5/hp2qmTmZX/pXg0Vw6ZiyYPbVQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bFJMo0phn1txywI08Fl1ofv4vLKpkkyMx/mRwcZBpaA=; b=MXaAHeu8stWb4xDDI3VVtNmOweROX6+YdCiaKa1z+b+equYl2rYN3z/12YqAF22QVq /c3PgS4mCDKK1Df4gjrwdDkR7/++Ig9fvfSKb/AGCYvX6cqsGwyryGiHMT8TPHmhLDD6 pN7ghykQO8uONgBRPi9Gfyhltx7h3fk3q5JknrE6+YXDFvGuPprNu6avc5/XnzL6CdW4 iEzXB+xWJE/cozBhIfUcHUbwlDEjtQxn11NGVLCQaGqpSqmirtqLXWKDzz5jl2B3EUne ti7nqqcjF/31xAUAgwgbCdWRYfqF6YmFeAhkXTUkpu5DAZoeCd6hHrZjlS1EQJE13kui Dtkw== X-Gm-Message-State: APjAAAXK6AaSO9tT5jU9Cq6+BwhZKgtPxypMtEnmt3AAAPiEwwnHqeD5 iM+0hafg55HJ5ARv3ezkBnYeB2sr X-Google-Smtp-Source: APXvYqzNdhx7lLyVu/u6Lmv/ofcMOFrjdyoB5iLY8EEUrpXtf12i+hqYy4sMRi1ruwh/aQQ02mbmKA== X-Received: by 2002:a0c:f28f:: with SMTP id k15mr9870958qvl.76.1581880327607; Sun, 16 Feb 2020 11:12:07 -0800 (PST) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH 3/4] lxc: Implement virtual /proc/cpuinfo via LXC fuse Date: Sun, 16 Feb 2020 16:11:47 -0300 Message-Id: <20200216191148.17262-4-jcfaracco@gmail.com> In-Reply-To: <20200216191148.17262-1-jcfaracco@gmail.com> References: <20200216191148.17262-1-jcfaracco@gmail.com> MIME-Version: 1.0 X-MC-Unique: Y5sYeL5EOjG06Ii31smT5Q-1 X-MC-Unique: MszegoZQMjm00qHotk96_Q-1 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 01GJCFaS010732 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.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 tries to fix a 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 automatic 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 | 15 ++++++++ src/lxc/lxc_fuse.c | 78 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 120 insertions(+), 4 deletions(-) diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index 470337e675..a6c73d9d55 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -59,6 +59,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) @@ -76,6 +104,9 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr d= ef, goto cleanup; /* free mask to make sure we won't use it in a wrong way later */ VIR_FREE(mask); + } 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..1a2c97c9f4 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -999,6 +999,7 @@ static int lxcContainerMountProcFuse(virDomainDefPtr de= f, { int ret; char *meminfo_path =3D NULL; + char *cpuinfo_path =3D NULL; =20 VIR_DEBUG("Mount /proc/meminfo stateDir=3D%s", stateDir); =20 @@ -1013,7 +1014,21 @@ static int lxcContainerMountProcFuse(virDomainDefPtr= def, meminfo_path); } =20 + VIR_DEBUG("Mount /proc/cpuinfo stateDir=3D%s", stateDir); + + cpuinfo_path =3D g_strdup_printf("/.oldroot/%s/%s.fuse/cpuinfo", + stateDir, + def->name); + + if ((ret =3D mount(cpuinfo_path, "/proc/cpuinfo", + NULL, MS_BIND, NULL)) < 0) { + virReportSystemError(errno, + _("Failed to mount %s on /proc/cpuinfo"), + cpuinfo_path); + } + VIR_FREE(meminfo_path); + VIR_FREE(cpuinfo_path); return ret; } #else diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c index 44f240a0b5..12fa69d494 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -37,6 +37,7 @@ #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) { @@ -54,7 +55,8 @@ static int lxcProcGetattr(const char *path, struct stat *= stbuf) if (STREQ(path, "/")) { stbuf->st_mode =3D S_IFDIR | 0755; stbuf->st_nlink =3D 2; - } else if (STREQ(path, fuse_meminfo_path)) { + } else if (STREQ(path, fuse_meminfo_path) || + STREQ(path, fuse_cpuinfo_path)) { if (stat(mempath, &sb) < 0) { res =3D -errno; goto cleanup; @@ -90,6 +92,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; } @@ -97,7 +100,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) @@ -125,7 +129,7 @@ static int lxcProcHostRead(char *path, char *buf, size_= t size, off_t offset) static int lxcProcReadMeminfo(char *hostpath, virDomainDefPtr def, char *buf, size_t size, off_t offset) { - int res; + int res =3D -1; FILE *fd =3D NULL; char *line =3D NULL; size_t n; @@ -151,7 +155,6 @@ static int lxcProcReadMeminfo(char *hostpath, virDomain= DefPtr def, goto cleanup; } =20 - res =3D -1; while (getline(&line, &n, fd) > 0) { char *ptr =3D strchr(line, ':'); if (!ptr) @@ -235,6 +238,70 @@ static int lxcProcReadMeminfo(char *hostpath, virDomai= nDefPtr def, return res; } =20 + +static int lxcProcReadCpuinfo(char *hostpath, virDomainDefPtr def, + char *buf, size_t size, off_t offset) +{ + int res =3D -1; + FILE *fd =3D NULL; + char *line =3D NULL; + size_t n; + virBuffer buffer =3D VIR_BUFFER_INITIALIZER; + virBufferPtr new_cpuinfo =3D &buffer; + size_t cpu; + size_t nvcpu; + size_t curcpu =3D 0; + bool get_proc =3D false; + + fd =3D fopen(hostpath, "r"); + if (fd =3D=3D NULL) { + virReportSystemError(errno, _("Cannot open %s"), hostpath); + res =3D -errno; + goto cleanup; + } + + /* /proc/cpuinfo does not support fseek */ + if (offset > 0) { + res =3D 0; + goto cleanup; + } + + nvcpu =3D virDomainDefGetVcpus(def); + while (getline(&line, &n, fd) > 0) { + if (sscanf(line, "processor\t: %zu", &cpu) =3D=3D 1) { + virDomainVcpuDefPtr vcpu =3D virDomainDefGetVcpu(def, cpu); + /* VCPU is mapped */ + if (vcpu) { + if (curcpu =3D=3D nvcpu) + break; + + 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) + virBufferAdd(new_cpuinfo, line, -1); + } + } + + res =3D strlen(virBufferCurrentContent(new_cpuinfo)); + if (res > size) + res =3D size; + memcpy(buf, virBufferCurrentContent(new_cpuinfo), res); + + cleanup: + VIR_FREE(line); + virBufferFreeAndReset(new_cpuinfo); + VIR_FORCE_FCLOSE(fd); + return res; +} + + static int lxcProcRead(const char *path G_GNUC_UNUSED, char *buf G_GNUC_UNUSED, size_t size G_GNUC_UNUSED, @@ -254,6 +321,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 VIR_FREE(hostpath); --=20 2.20.1