From nobody Wed May 8 20:58:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1706783659; cv=none; d=zohomail.com; s=zohoarc; b=UM4gvkUcUx1m8wN1cg2d/JQ1T48E49mPG5HnSFbHJgHAa8SCk8Q6tqwsJhi/qJaOClDP/ONOrdGuEj6z4Hg7UjeTPP5WbIykgVi4TiAEPWMs3/tb0LdOUBHzhB4/J07WtUB3bDY3o3toeux6Poq0KbJNGcZ8WhaeffvLwq22qus= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1706783659; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=D8J71cORIIJRoqYhXR8/qnQiyP/PfOcpk4LGA73fgB0=; b=Uz/pkAi21WGPGLAK90CPIPrtwwYvB3rfm+7IRNdsDt90BaamZb6ooAdl+yl2sXfV8eZ3QqhebFxYDC0Gdhd5/pXLL1X95hPrKtRdXCnm569p7EUyBZkml+jmtR6BzwKJMh+gJR8DLoUzE3AzVvrWVR4G+TrV6QaTJURNPMryU5o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1706783659913581.9502503452194; Thu, 1 Feb 2024 02:34:19 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.674271.1049108 (Exim 4.92) (envelope-from ) id 1rVUO9-0006Kg-Py; Thu, 01 Feb 2024 10:33:45 +0000 Received: by outflank-mailman (output) from mailman id 674271.1049108; Thu, 01 Feb 2024 10:33:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rVUO9-0006KZ-MG; Thu, 01 Feb 2024 10:33:45 +0000 Received: by outflank-mailman (input) for mailman id 674271; Thu, 01 Feb 2024 10:33:44 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rVUO8-0006KQ-B6 for xen-devel@lists.xenproject.org; Thu, 01 Feb 2024 10:33:44 +0000 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [2a00:1450:4864:20::32c]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 5f1cbb73-c0ed-11ee-8a43-1f161083a0e0; Thu, 01 Feb 2024 11:33:43 +0100 (CET) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-40e80046264so6792725e9.0 for ; Thu, 01 Feb 2024 02:33:43 -0800 (PST) Received: from andrewcoop.citrite.net (default-46-102-197-194.interdsl.co.uk. [46.102.197.194]) by smtp.gmail.com with ESMTPSA id g10-20020a05600c4eca00b0040fbba734f3sm1395865wmq.34.2024.02.01.02.33.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 02:33:41 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 5f1cbb73-c0ed-11ee-8a43-1f161083a0e0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1706783622; x=1707388422; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=D8J71cORIIJRoqYhXR8/qnQiyP/PfOcpk4LGA73fgB0=; b=ehCcxnN5SUNiax9nWYsD+sK7dGawQ90387sRVjM8UlSA7wS3hUAHsa/Mk+LZ3W/Gkx FjwtV7eD5grH7eB+UTyayD1jW+P9IZzWpYqTjCjNqcTGqaglXG5ePiQdhKX22mM6Y7pO DMSrpuNPo6xN5Y8kAembfTXKWNaCdtNDeL0Lw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706783622; x=1707388422; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D8J71cORIIJRoqYhXR8/qnQiyP/PfOcpk4LGA73fgB0=; b=Bk9gI8tO1ePfj0P8mmgwxFq/aQaGad6ZOlCkWaYRPoItMZXWuFAFxsQObk+TOEGzum FPxgHibMhWf00+Srm1y4gI4Jpm13ex5uMsf87ZPa+/py7BhB0r50Xe/8D5X/TkJbkz5k d5JVUD/VySBgvvoufxSKOU3e6uAne0l+qfcJKksjZnFMUg98JR/fOci07uXPfZe5HRxX BtJi3CZqjv81UsHDSPK72iGBlZdm6IHjFEwK6AvWhyEcZTramtyPURRTxU98Oyqy1R0l X3ZuYfaCnLUoSA0bRIEHPI/g47aMS9syi5pf2Q75Hoe2I/k7uCs879/08MTa9mqjcpKb l4mg== X-Gm-Message-State: AOJu0Yx2PdB7byHM4PH+jcZ7cvC2xKgzHNn8sYZr4WBYLTTrNs+KeJuL MHEYhS0b01GRN8o0IISH363FG0/lnPNES2+fAOPe1otkp07j6tcsicm4T4kYdOwir3/M9v856QN w X-Google-Smtp-Source: AGHT+IHFb00cp37ppIbJJKHh4MJRADrSYXz3jmxNOyaAIEpn5stWts3uRs9zYdfitFoGEmO37ivjlQ== X-Received: by 2002:a05:600c:3d88:b0:40f:b8e7:ad51 with SMTP id bi8-20020a05600c3d8800b0040fb8e7ad51mr1510749wmb.31.1706783622063; Thu, 01 Feb 2024 02:33:42 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUBY5YRDQ0F/TzC+6OmFx9c5nokmfEOljkdq0czD5PZFyC5kwbob7tsFyYLMm35OZxSGhC8oiE4dScslf5XPYuCsev3lyimueyppHDkKLIki6MDFxZ8paolBaKusCj9sHbFiKd0zoNYtZINe4w8cYmW27AW3i3pCB6OD7bqnLJj+xd/et6WxsfwnjQx+QjnIA== From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , George Dunlap , Jan Beulich , Stefano Stabellini , Wei Liu , Julien Grall Subject: [PATCH] xen/bitmap: Consistently use unsigned bits values Date: Thu, 1 Feb 2024 10:33:39 +0000 Message-Id: <20240201103339.549307-1-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1706783662341100005 Content-Type: text/plain; charset="utf-8" Right now, most of the static inline helpers take an unsigned nbits quantit= y, and most of the library functions take a signed quanity. Because BITMAP_LAST_WORD_MASK() is expressed as a divide, the compiler is forced to emit two different paths to get the correct semantics for signed division. Swap all signed bit-counts to being unsigned bit-counts for the simple case= s. This includes the return value of bitmap_weight(). Bloat-o-meter for a random x86 build reports: add/remove: 0/0 grow/shrink: 8/19 up/down: 167/-413 (-246) which all comes from compiler not emitting "dead" logic paths for negative = bit counts. No functional change. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: George Dunlap CC: Jan Beulich CC: Stefano Stabellini CC: Wei Liu CC: Julien Grall Found when investigating negative-shift UBSAN, because each of the library functions ended up being instrumented. There is much more wanting cleaning up here, but we have to start somewhere. Some observations: * Various of the boolean-like return values have -1 for zero-length bitmap= s. I can't spot any callers which care, so this seems like a waste. * bitmap_zero() and similar clear predate us switching to use __builtin_memset(), because there's no need for bitmap_switch(). * Should we consolidate 'bits' vs 'nbits'? * The internals of these helpers want converting too. Other helpers need more than just a parameter conversion. --- xen/common/bitmap.c | 24 +++++++++++----------- xen/include/xen/bitmap.h | 43 ++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/xen/common/bitmap.c b/xen/common/bitmap.c index 7d4551f78283..c57b35f0042c 100644 --- a/xen/common/bitmap.c +++ b/xen/common/bitmap.c @@ -55,7 +55,7 @@ static void clamp_last_byte(uint8_t *bp, unsigned int nbi= ts) bp[nbits/8] &=3D (1U << remainder) - 1; } =20 -int __bitmap_empty(const unsigned long *bitmap, int bits) +int __bitmap_empty(const unsigned long *bitmap, unsigned int bits) { int k, lim =3D bits/BITS_PER_LONG; for (k =3D 0; k < lim; ++k) @@ -70,7 +70,7 @@ int __bitmap_empty(const unsigned long *bitmap, int bits) } EXPORT_SYMBOL(__bitmap_empty); =20 -int __bitmap_full(const unsigned long *bitmap, int bits) +int __bitmap_full(const unsigned long *bitmap, unsigned int bits) { int k, lim =3D bits/BITS_PER_LONG; for (k =3D 0; k < lim; ++k) @@ -86,7 +86,7 @@ int __bitmap_full(const unsigned long *bitmap, int bits) EXPORT_SYMBOL(__bitmap_full); =20 int __bitmap_equal(const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits) + const unsigned long *bitmap2, unsigned int bits) { int k, lim =3D bits/BITS_PER_LONG; for (k =3D 0; k < lim; ++k) @@ -101,7 +101,7 @@ int __bitmap_equal(const unsigned long *bitmap1, } EXPORT_SYMBOL(__bitmap_equal); =20 -void __bitmap_complement(unsigned long *dst, const unsigned long *src, int= bits) +void __bitmap_complement(unsigned long *dst, const unsigned long *src, uns= igned int bits) { int k, lim =3D bits/BITS_PER_LONG; for (k =3D 0; k < lim; ++k) @@ -113,7 +113,7 @@ void __bitmap_complement(unsigned long *dst, const unsi= gned long *src, int bits) EXPORT_SYMBOL(__bitmap_complement); =20 void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits) + const unsigned long *bitmap2, unsigned int bits) { int k; int nr =3D BITS_TO_LONGS(bits); @@ -124,7 +124,7 @@ void __bitmap_and(unsigned long *dst, const unsigned lo= ng *bitmap1, EXPORT_SYMBOL(__bitmap_and); =20 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits) + const unsigned long *bitmap2, unsigned int bits) { int k; int nr =3D BITS_TO_LONGS(bits); @@ -135,7 +135,7 @@ void __bitmap_or(unsigned long *dst, const unsigned lon= g *bitmap1, EXPORT_SYMBOL(__bitmap_or); =20 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits) + const unsigned long *bitmap2, unsigned int bits) { int k; int nr =3D BITS_TO_LONGS(bits); @@ -146,7 +146,7 @@ void __bitmap_xor(unsigned long *dst, const unsigned lo= ng *bitmap1, EXPORT_SYMBOL(__bitmap_xor); =20 void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits) + const unsigned long *bitmap2, unsigned int bits) { int k; int nr =3D BITS_TO_LONGS(bits); @@ -157,7 +157,7 @@ void __bitmap_andnot(unsigned long *dst, const unsigned= long *bitmap1, EXPORT_SYMBOL(__bitmap_andnot); =20 int __bitmap_intersects(const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits) + const unsigned long *bitmap2, unsigned int bits) { int k, lim =3D bits/BITS_PER_LONG; for (k =3D 0; k < lim; ++k) @@ -172,7 +172,7 @@ int __bitmap_intersects(const unsigned long *bitmap1, EXPORT_SYMBOL(__bitmap_intersects); =20 int __bitmap_subset(const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits) + const unsigned long *bitmap2, unsigned int bits) { int k, lim =3D bits/BITS_PER_LONG; for (k =3D 0; k < lim; ++k) @@ -187,7 +187,7 @@ int __bitmap_subset(const unsigned long *bitmap1, EXPORT_SYMBOL(__bitmap_subset); =20 #if BITS_PER_LONG =3D=3D 32 -int __bitmap_weight(const unsigned long *bitmap, int bits) +unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bit= s) { int k, w =3D 0, lim =3D bits/BITS_PER_LONG; =20 @@ -200,7 +200,7 @@ int __bitmap_weight(const unsigned long *bitmap, int bi= ts) return w; } #else -int __bitmap_weight(const unsigned long *bitmap, int bits) +unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bit= s) { int k, w =3D 0, lim =3D bits/BITS_PER_LONG; =20 diff --git a/xen/include/xen/bitmap.h b/xen/include/xen/bitmap.h index 657390e32ebc..b9f980e91930 100644 --- a/xen/include/xen/bitmap.h +++ b/xen/include/xen/bitmap.h @@ -66,25 +66,25 @@ * lib/bitmap.c provides these functions: */ =20 -extern int __bitmap_empty(const unsigned long *bitmap, int bits); -extern int __bitmap_full(const unsigned long *bitmap, int bits); -extern int __bitmap_equal(const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits); -extern void __bitmap_complement(unsigned long *dst, const unsigned long *s= rc, - int bits); -extern void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits); -extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits); -extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits); -extern void __bitmap_andnot(unsigned long *dst, const unsigned long *bitma= p1, - const unsigned long *bitmap2, int bits); -extern int __bitmap_intersects(const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits); -extern int __bitmap_subset(const unsigned long *bitmap1, - const unsigned long *bitmap2, int bits); -extern int __bitmap_weight(const unsigned long *bitmap, int bits); +int __bitmap_empty(const unsigned long *bitmap, unsigned int bits); +int __bitmap_full(const unsigned long *bitmap, unsigned int bits); +int __bitmap_equal(const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int bits); +void __bitmap_complement(unsigned long *dst, const unsigned long *src, + unsigned int bits); +void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int bits); +void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int bits); +void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int bits); +void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int bits); +int __bitmap_intersects(const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int bits); +int __bitmap_subset(const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int bits); +unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bit= s); extern void __bitmap_set(unsigned long *map, unsigned int start, int len); extern void __bitmap_clear(unsigned long *map, unsigned int start, int len= ); =20 @@ -117,7 +117,7 @@ static inline void bitmap_zero(unsigned long *dst, unsi= gned int nbits) memset(dst, 0, bitmap_bytes(nbits))); } =20 -static inline void bitmap_fill(unsigned long *dst, int nbits) +static inline void bitmap_fill(unsigned long *dst, unsigned int nbits) { size_t nlongs =3D BITS_TO_LONGS(nbits); =20 @@ -224,7 +224,8 @@ static inline int bitmap_full(const unsigned long *src,= unsigned int nbits) return __bitmap_full(src, nbits)); } =20 -static inline int bitmap_weight(const unsigned long *src, int nbits) +static inline unsigned int bitmap_weight(const unsigned long *src, + unsigned int nbits) { return __bitmap_weight(src, nbits); } --=20 2.30.2