From nobody Sat May 18 09:48:34 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1713881916443875.7046488670741; Tue, 23 Apr 2024 07:18:36 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 653292201; Tue, 23 Apr 2024 10:18:35 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 18FB521D7; Tue, 23 Apr 2024 10:16:40 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id CA20921A3; Tue, 23 Apr 2024 10:16:32 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 7B67C1D55 for ; Tue, 23 Apr 2024 10:16:28 -0400 (EDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-208-sGBF7XLhNuSYUExEEiFEhg-1; Tue, 23 Apr 2024 10:16:26 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 687E481F309 for ; Tue, 23 Apr 2024 14:16:26 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 13F061033C62 for ; Tue, 23 Apr 2024 14:16:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 X-MC-Unique: sGBF7XLhNuSYUExEEiFEhg-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH v2 1/4] virbitmap: Introduce virBitmapParseUnlimitedAllowEmpty() Date: Tue, 23 Apr 2024 16:16:21 +0200 Message-ID: <448e911055f5f0f2791eda7c9396c6dd2f8a3799.1713881661.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Message-ID-Hash: 3CFADPHMDJRGOAEGGKMMD35QROWJEQYO X-Message-ID-Hash: 3CFADPHMDJRGOAEGGKMMD35QROWJEQYO X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8"; x-default="true" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1713881918099100001 Some sysfs files contain either string representation of a bitmap or just a newline character. An example of such file is: /sys/devices/system/cpu/isolated. Our current implementation of virBitmapParseUnlimited() fails in the latter case, unfortunately. Introduce a slightly modified version that accepts empty files. Signed-off-by: Michal Privoznik Reviewed-by: Pavel Hrdina --- src/libvirt_private.syms | 1 + src/util/virbitmap.c | 40 +++++++++++++++++++++++++++++++++++----- src/util/virbitmap.h | 3 +++ tests/virbitmaptest.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 839fe4f545..0ba6183010 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1935,6 +1935,7 @@ virBitmapNextSetBit; virBitmapOverlaps; virBitmapParse; virBitmapParseUnlimited; +virBitmapParseUnlimitedAllowEmpty; virBitmapSetAll; virBitmapSetBit; virBitmapSetBitExpand; diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index e48224d709..775bbf1532 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -368,6 +368,7 @@ virBitmapFormat(virBitmap *bitmap) * @str: points to a string representing a human-readable bitmap * @bitmap: a bitmap populated from @str * @limited: Don't use self-expanding APIs, report error if bit exceeds bi= tmap size + * @allowEmpty: Allow @str to be empty string or contain nothing but spaces * * This function is the counterpart of virBitmapFormat. This function crea= tes * a bitmap, in which bits are set according to the content of @str. @@ -381,7 +382,8 @@ virBitmapFormat(virBitmap *bitmap) static int virBitmapParseInternal(const char *str, virBitmap *bitmap, - bool limited) + bool limited, + bool allowEmpty) { bool neg =3D false; const char *cur =3D str; @@ -389,13 +391,19 @@ virBitmapParseInternal(const char *str, size_t i; int start, last; =20 - if (!str) + if (!str) { + if (allowEmpty) + return 0; goto error; + } =20 virSkipSpaces(&cur); =20 - if (*cur =3D=3D '\0') + if (*cur =3D=3D '\0') { + if (allowEmpty) + return 0; goto error; + } =20 while (*cur !=3D 0) { /* @@ -505,7 +513,7 @@ virBitmapParse(const char *str, { g_autoptr(virBitmap) tmp =3D virBitmapNew(bitmapSize); =20 - if (virBitmapParseInternal(str, tmp, true) < 0) + if (virBitmapParseInternal(str, tmp, true, false) < 0) return -1; =20 *bitmap =3D g_steal_pointer(&tmp); @@ -534,7 +542,29 @@ virBitmapParseUnlimited(const char *str) { g_autoptr(virBitmap) tmp =3D virBitmapNew(0); =20 - if (virBitmapParseInternal(str, tmp, false) < 0) + if (virBitmapParseInternal(str, tmp, false, false) < 0) + return NULL; + + return g_steal_pointer(&tmp); +} + + +/** + * virBitmapParseUnlimitedAllowEmpty: + * @str: points to a string representing a human-readable bitmap + * + * Just like virBitmapParseUnlimited() except when the input string @str is + * empty (or contains just spaces) an empty bitmap is returned instead of = an + * error. + * + * Returns @bitmap on success, or NULL in cas of error + */ +virBitmap * +virBitmapParseUnlimitedAllowEmpty(const char *str) +{ + g_autoptr(virBitmap) tmp =3D virBitmapNew(0); + + if (virBitmapParseInternal(str, tmp, false, true) < 0) return NULL; =20 return g_steal_pointer(&tmp); diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h index a9cf309884..a9f9d97fd0 100644 --- a/src/util/virbitmap.h +++ b/src/util/virbitmap.h @@ -84,6 +84,9 @@ int virBitmapParse(const char *str, virBitmap * virBitmapParseUnlimited(const char *str); =20 +virBitmap * +virBitmapParseUnlimitedAllowEmpty(const char *str); + virBitmap *virBitmapNewCopy(virBitmap *src) ATTRIBUTE_NONNULL(1); =20 virBitmap *virBitmapNewData(const void *data, int len) ATTRIBUTE_NONNULL(1= ); diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c index f4fadb7c8a..adc956ca3d 100644 --- a/tests/virbitmaptest.c +++ b/tests/virbitmaptest.c @@ -705,6 +705,43 @@ test16(const void *opaque G_GNUC_UNUSED) } =20 =20 +/* virBitmapParseUnlimitedAllowEmpty */ +static int +test17(const void *opaque G_GNUC_UNUSED) +{ + g_autoptr(virBitmap) map1 =3D NULL; + g_autoptr(virBitmap) map2 =3D NULL; + g_autofree char *map1_str =3D NULL; + g_autofree char *map2_str =3D NULL; + + if (!(map1 =3D virBitmapParseUnlimitedAllowEmpty(NULL))) { + fprintf(stderr, "Expected success, got failure\n"); + return -1; + } + + if (!(map2 =3D virBitmapParseUnlimitedAllowEmpty(" "))) { + fprintf(stderr, "Expected success, got failure\n"); + return -1; + } + + if (!virBitmapIsAllClear(map1) || + !virBitmapIsAllClear(map2) || + !virBitmapEqual(map1, map2)) { + fprintf(stderr, "empty maps should equal\n"); + return -1; + } + + if (!(map1_str =3D virBitmapFormat(map1)) || + !(map2_str =3D virBitmapFormat(map2)) || + STRNEQ(map1_str, map2_str)) { + fprintf(stderr, "maps don't equal after format to string\n"); + return -1; + } + + return 0; +} + + #define TESTBINARYOP(A, B, RES, FUNC) \ testBinaryOpData.a =3D A; \ testBinaryOpData.b =3D B; \ @@ -781,6 +818,9 @@ mymain(void) if (virTestRun("test16", test16, NULL) < 0) ret =3D -1; =20 + if (virTestRun("test17", test17, NULL) < 0) + ret =3D -1; + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 --=20 2.43.2 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Sat May 18 09:48:34 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1713881849596986.4882395785751; Tue, 23 Apr 2024 07:17:29 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 4C0D321BC; Tue, 23 Apr 2024 10:17:28 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 851C01F8D; Tue, 23 Apr 2024 10:16:31 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 633E11E3E; Tue, 23 Apr 2024 10:16:29 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id CC2AD1DE1 for ; Tue, 23 Apr 2024 10:16:28 -0400 (EDT) Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-274-2GM0c8EBPKGsS-4pLod57A-1; Tue, 23 Apr 2024 10:16:27 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F0F301C03154 for ; Tue, 23 Apr 2024 14:16:26 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9BCE01033C63 for ; Tue, 23 Apr 2024 14:16:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 X-MC-Unique: 2GM0c8EBPKGsS-4pLod57A-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH v2 2/4] virfile: Introduce virFileReadValueBitmapAllowEmpty() Date: Tue, 23 Apr 2024 16:16:22 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Message-ID-Hash: EVOUCYL7QKWJHRAGKNM22A2UREBNVB7R X-Message-ID-Hash: EVOUCYL7QKWJHRAGKNM22A2UREBNVB7R X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8"; x-default="true" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1713881849849100001 Some sysfs files contain either string representation of a bitmap or just a newline character. An example of such file is: /sys/devices/system/cpu/isolated. Our current implementation of virFileReadValueBitmap() fails in the latter case, unfortunately. Introduce a slightly modified version that accepts empty files. Signed-off-by: Michal Privoznik Reviewed-by: Pavel Hrdina --- src/libvirt_private.syms | 1 + src/util/virfile.c | 81 ++++++++++++++++++++++++++++++---------- src/util/virfile.h | 2 + 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0ba6183010..2c7e4b45d3 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2360,6 +2360,7 @@ virFileReadHeaderFD; virFileReadHeaderQuiet; virFileReadLimFD; virFileReadValueBitmap; +virFileReadValueBitmapAllowEmpty; virFileReadValueInt; virFileReadValueScaledInt; virFileReadValueString; diff --git a/src/util/virfile.c b/src/util/virfile.c index deaf4555fd..c769f7d650 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -4365,26 +4365,12 @@ virFileReadValueScaledInt(unsigned long long *value= , const char *format, ...) * used for small, interface-like files, so it should not be huge (subject= ive) */ #define VIR_FILE_READ_VALUE_STRING_MAX 4096 =20 -/** - * virFileReadValueBitmap: - * @value: pointer to virBitmap * to be allocated and filled in with the v= alue - * @format, ...: file to read from - * - * Read int from @format and put it into @value. - * - * Return -2 for non-existing file, -1 on other errors and 0 if everything= went - * fine. - */ -int -virFileReadValueBitmap(virBitmap **value, const char *format, ...) +static int +virFileReadValueBitmapImpl(virBitmap **value, + const char *path, + bool allowEmpty) { g_autofree char *str =3D NULL; - g_autofree char *path =3D NULL; - va_list ap; - - va_start(ap, format); - path =3D g_strdup_vprintf(format, ap); - va_end(ap); =20 if (!virFileExists(path)) return -2; @@ -4394,13 +4380,70 @@ virFileReadValueBitmap(virBitmap **value, const cha= r *format, ...) =20 virStringTrimOptionalNewline(str); =20 - *value =3D virBitmapParseUnlimited(str); + if (allowEmpty) { + *value =3D virBitmapParseUnlimitedAllowEmpty(str); + } else { + *value =3D virBitmapParseUnlimited(str); + } + if (!*value) return -1; =20 return 0; } =20 + +/** + * virFileReadValueBitmap: + * @value: pointer to virBitmap * to be allocated and filled in with the v= alue + * @format, ...: file to read from + * + * Read int from @format and put it into @value. + * + * Returns: -2 for non-existing file, + * -1 on other errors (with error reported), + * 0 otherwise. + */ +int +virFileReadValueBitmap(virBitmap **value, const char *format, ...) +{ + g_autofree char *path =3D NULL; + va_list ap; + + va_start(ap, format); + path =3D g_strdup_vprintf(format, ap); + va_end(ap); + + return virFileReadValueBitmapImpl(value, path, false); +} + + +/** + * virFileReadValueBitmapAllowEmpty: + * @value: pointer to virBitmap * to be allocated and filled in with the v= alue + * @format, ...: file to read from + * + * Just like virFileReadValueBitmap(), except if the file is empty or cont= ains + * nothing but spaces an empty bitmap is returned instead of an error. + * + * Returns: -2 for non-existing file, + * -1 on other errors (with error reported), + * 0 otherwise. + */ +int +virFileReadValueBitmapAllowEmpty(virBitmap **value, const char *format, ..= .) +{ + g_autofree char *path =3D NULL; + va_list ap; + + va_start(ap, format); + path =3D g_strdup_vprintf(format, ap); + va_end(ap); + + return virFileReadValueBitmapImpl(value, path, true); +} + + /** * virFileReadValueString: * @value: pointer to char * to be allocated and filled in with the value diff --git a/src/util/virfile.h b/src/util/virfile.h index 56fe309bce..7df3fcb840 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -354,6 +354,8 @@ int virFileReadValueUllongQuiet(unsigned long long *val= ue, const char *format, . G_GNUC_PRINTF(2, 3); int virFileReadValueBitmap(virBitmap **value, const char *format, ...) G_GNUC_PRINTF(2, 3); +int virFileReadValueBitmapAllowEmpty(virBitmap **value, const char *format= , ...) + G_GNUC_PRINTF(2, 3); int virFileReadValueScaledInt(unsigned long long *value, const char *forma= t, ...) G_GNUC_PRINTF(2, 3); int virFileReadValueString(char **value, const char *format, ...) --=20 2.43.2 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Sat May 18 09:48:34 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1713881957121502.7757472985928; Tue, 23 Apr 2024 07:19:17 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id F046D214B; Tue, 23 Apr 2024 10:19:15 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 4C43721E8; Tue, 23 Apr 2024 10:16:44 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 165F71DE1; Tue, 23 Apr 2024 10:16:33 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 8D0761DE1 for ; Tue, 23 Apr 2024 10:16:29 -0400 (EDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-443-A6VfMQlSPZmmrB59nKLLVA-1; Tue, 23 Apr 2024 10:16:27 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 83DB29D1640 for ; Tue, 23 Apr 2024 14:16:27 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F9DA1121312 for ; Tue, 23 Apr 2024 14:16:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 X-MC-Unique: A6VfMQlSPZmmrB59nKLLVA-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH v2 3/4] virhostcpu: Introduce virHostCPUGetIsolated() Date: Tue, 23 Apr 2024 16:16:23 +0200 Message-ID: <92a5725249130a7b60a27adab3b4d8263df79176.1713881661.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Message-ID-Hash: U2V7VNVLJSRISBAC3S45NBJCLL4CO3GZ X-Message-ID-Hash: U2V7VNVLJSRISBAC3S45NBJCLL4CO3GZ X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8"; x-default="true" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1713881958168100001 This is a helper that parses /sys/devices/system/cpu/isolated into a virBitmap. It's going to be needed soon. Signed-off-by: Michal Privoznik Reviewed-by: Pavel Hrdina --- src/libvirt_private.syms | 1 + src/util/virhostcpu.c | 31 +++++++++++++++++++++++++++++++ src/util/virhostcpu.h | 1 + 3 files changed, 33 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2c7e4b45d3..0f2d5db883 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2504,6 +2504,7 @@ virHostCPUGetCount; virHostCPUGetCPUID; virHostCPUGetHaltPollTime; virHostCPUGetInfo; +virHostCPUGetIsolated; virHostCPUGetKVMMaxVCPUs; virHostCPUGetMap; virHostCPUGetMicrocodeVersion; diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index 01de69c0d1..b6d1db2302 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -1152,6 +1152,37 @@ virHostCPUGetAvailableCPUsBitmap(void) } =20 =20 +/** + * virHostCPUGetIsolated: + * @isolated: returned bitmap of isolated CPUs + * + * Sets @isolated to point to a bitmap of isolated CPUs (e.g. those passed= to + * isolcpus=3D kernel cmdline). If the file doesn't exist, @isolated is se= t to + * NULL and success is returned. If the file does exist but it's empty, + * @isolated is set to an empty bitmap an success is returned. + * + * Returns: 0 on success, + * -1 otherwise (with error reported). + */ +int +virHostCPUGetIsolated(virBitmap **isolated) +{ + g_autoptr(virBitmap) bitmap =3D NULL; + int rc; + + rc =3D virFileReadValueBitmapAllowEmpty(&bitmap, "%s/cpu/isolated", SY= SFS_SYSTEM_PATH); + if (rc =3D=3D -2) { + *isolated =3D NULL; + return 0; + } else if (rc < 0) { + return -1; + } + + *isolated =3D g_steal_pointer(&bitmap); + return 0; +} + + #if WITH_LINUX_KVM_H && defined(KVM_CAP_PPC_SMT) =20 /* Get the number of threads per subcore. diff --git a/src/util/virhostcpu.h b/src/util/virhostcpu.h index d7e09bff22..1f47634c33 100644 --- a/src/util/virhostcpu.h +++ b/src/util/virhostcpu.h @@ -43,6 +43,7 @@ bool virHostCPUHasBitmap(void); virBitmap *virHostCPUGetPresentBitmap(void); virBitmap *virHostCPUGetOnlineBitmap(void); virBitmap *virHostCPUGetAvailableCPUsBitmap(void); +int virHostCPUGetIsolated(virBitmap **isolated); =20 int virHostCPUGetCount(void); int virHostCPUGetThreadsPerSubcore(virArch arch) G_NO_INLINE; --=20 2.43.2 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Sat May 18 09:48:34 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1713882026685190.32406068325645; Tue, 23 Apr 2024 07:20:26 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 6ABCA1D70; Tue, 23 Apr 2024 10:20:25 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 9415A2187; Tue, 23 Apr 2024 10:16:52 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 774D51F0D; Tue, 23 Apr 2024 10:16:34 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 564E81E36 for ; Tue, 23 Apr 2024 10:16:30 -0400 (EDT) Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-501-CCx9-Sl3OZC2Tp0KOo7D8g-1; Tue, 23 Apr 2024 10:16:28 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 17B3A29AC011 for ; Tue, 23 Apr 2024 14:16:28 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id B76601121312 for ; Tue, 23 Apr 2024 14:16:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 X-MC-Unique: CCx9-Sl3OZC2Tp0KOo7D8g-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH v2 4/4] qemu: Substract isolcpus from all online affinity Date: Tue, 23 Apr 2024 16:16:24 +0200 Message-ID: <6ffe240e65a0a2958392cc8eb87644308c3eeade.1713881661.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Message-ID-Hash: DJOTIM72BQWH7HN2RPSVE7SDPCHSRKDJ X-Message-ID-Hash: DJOTIM72BQWH7HN2RPSVE7SDPCHSRKDJ X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8"; x-default="true" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1713882028473100001 When starting a domain and there's no vCPU/emulator pinning set, we query the list of all online physical CPUs and set affinity of the child process (which eventually becomes QEMU) to that list. We can't assume libvirtd itself had affinity to all online CPUs and since affinity of the child process is inherited, we should fix it afterwards. But that's not necessarily correct. Users might isolate some physical CPUs and we should avoid touching them unless explicitly told so (i.e. vCPU/emulator pinning told us so). Therefore, when attempting to set affinity to all online CPUs subtract the isolated ones. Before this commit: root@localhost:~# cat /sys/devices/system/cpu/isolated 19,21,23 root@virtlab414:~# taskset -cp $(pgrep qemu) pid 14835's current affinity list: 0-23 After: root@virtlab414:~# taskset -cp $(pgrep qemu) pid 17153's current affinity list: 0-18,20,22 Resolves: https://issues.redhat.com/browse/RHEL-33082 Signed-off-by: Michal Privoznik Reviewed-by: Pavel Hrdina --- src/qemu/qemu_process.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index da2b024f92..521598471f 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -2334,6 +2334,8 @@ qemuProcessDetectIOThreadPIDs(virDomainObj *vm, static int qemuProcessGetAllCpuAffinity(virBitmap **cpumapRet) { + g_autoptr(virBitmap) isolCpus =3D NULL; + *cpumapRet =3D NULL; =20 if (!virHostCPUHasBitmap()) @@ -2342,6 +2344,13 @@ qemuProcessGetAllCpuAffinity(virBitmap **cpumapRet) if (!(*cpumapRet =3D virHostCPUGetOnlineBitmap())) return -1; =20 + if (virHostCPUGetIsolated(&isolCpus) < 0) + return -1; + + if (isolCpus) { + virBitmapSubtract(*cpumapRet, isolCpus); + } + return 0; } =20 --=20 2.43.2 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org