From nobody Thu Dec 26 19:30:18 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=1732200653; cv=none; d=zohomail.com; s=zohoarc; b=eJ17ZCtbfhg6cHVw/FIhI0adH6gWx7aXqYq56aJw4XwtljTDQvn7XtU3ujjOx7tqnJI1TAf0DoIRa5Hsecrb+GuUyV/bN/0guJQTGVfhyqa08OE2fiL7mgBKlKbLBTm3xiXBCUz2y41YfdDAdbGmAwP4Ef6EAOoumKb4iPX6Bwk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732200653; h=Content-Type: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=FLxDW78Evs4vbLxthbNP1M7wd0kSNpfLNBWorWpiKos=; b=lMmfKEaDghCWxqInnDaWTGa1or19dn/ebIHuxtX/i7r4cSBvlFElKJqyJf+nBgsQIWnDYHy5bAx7zVZuEfMyh9nn5FicQiv0T2LZQ9tISTE80AUq4KMRQFUS8yvv2k4fp8a3+E2UkuMOHGDU+CFRiAqamPLWScvC2szCSKoFx1k= 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 1732200653478189.43855087969018; Thu, 21 Nov 2024 06:50:53 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.841544.1257029 (Exim 4.92) (envelope-from ) id 1tE8VY-0008CP-0F; Thu, 21 Nov 2024 14:50:12 +0000 Received: by outflank-mailman (output) from mailman id 841544.1257029; Thu, 21 Nov 2024 14:50:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tE8VX-0008CI-Sw; Thu, 21 Nov 2024 14:50:11 +0000 Received: by outflank-mailman (input) for mailman id 841544; Thu, 21 Nov 2024 14:50:10 +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 1tE8VW-0008CA-St for xen-devel@lists.xenproject.org; Thu, 21 Nov 2024 14:50:10 +0000 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [2a00:1450:4864:20::536]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id e4d18fd4-a817-11ef-a0cc-8be0dac302b0; Thu, 21 Nov 2024 15:50:05 +0100 (CET) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5cfa1ec3b94so1255904a12.2 for ; Thu, 21 Nov 2024 06:50:05 -0800 (PST) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5cff454f905sm1918263a12.87.2024.11.21.06.50.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2024 06:50:03 -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: e4d18fd4-a817-11ef-a0cc-8be0dac302b0 X-Custom-Connection: eyJyZW1vdGVpcCI6IjJhMDA6MTQ1MDo0ODY0OjIwOjo1MzYiLCJoZWxvIjoibWFpbC1lZDEteDUzNi5nb29nbGUuY29tIn0= X-Custom-Transaction: eyJpZCI6ImU0ZDE4ZmQ0LWE4MTctMTFlZi1hMGNjLThiZTBkYWMzMDJiMCIsInRzIjoxNzMyMjAwNjA1LjE4MTYwMiwic2VuZGVyIjoiYW5kcmV3LmNvb3BlckBjbG91ZC5jb20iLCJyZWNpcGllbnQiOiJ4ZW4tZGV2ZWxAbGlzdHMueGVucHJvamVjdC5vcmcifQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1732200604; x=1732805404; 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=FLxDW78Evs4vbLxthbNP1M7wd0kSNpfLNBWorWpiKos=; b=C8TJ4FOBb80fKfsx28PRYakhufCdGZvEXmsj1ejhTbNx66nBuNVx1b0/7OoDDpulzS DhP39irY5o2OLVMEN1IjnJDclzaP69nPubQ5PycqA6WoWLY9lbgq8kzhYaxMDTma225U Iq3Ic2X3gHRK1xnJEhe9tv9fSSr2JGNWf3d1Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732200604; x=1732805404; 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=FLxDW78Evs4vbLxthbNP1M7wd0kSNpfLNBWorWpiKos=; b=ANulgsZ3B5tYAHs9QWpvvSwD8CVS5Pibs9QBG9HY+KW3GWV/0O+NvaBikDftOK8oBF 7z4nZEHnXJu/M6v0GQRjJQOlQVOMM7q7oYvSDRhcLoOasRT5jhA3zqz3mXQMul3oHwrg BFG1Xe60hAFgrOpx7Jgu+rds1EQyAfgm0kmL8NXKv3MabMuZjlrconCEkWMn22lvZ3Iq G5xLWTqzYcXUVbVcCbwDpGRTiI0NWRV98lZAPib5o3coBwXFwKLU+NWcrK67lkeZWIZy sl1Zl4KJs4vXCN8KTJGUhJtPI1/afnHCdfq2xCdY7+SlWZekMCVl7Q/XB3M+yfU17Rjg dDuA== X-Gm-Message-State: AOJu0YyOHpgHcOcCInMIm+LOeCPPLKojFjgkPcwyE4okdPEPZKb8a//i u9DeQfSjlHIL02wJSHmHWguaBJyQg4PmNPY6EI+j89sP87kM6YjEYSWJZvncCQ8zfpfR8vqpMvn X X-Gm-Gg: ASbGncvVDimULJZ86OWSZO0YsC1WdH37CAie4D/EwWpTNc8rWlTLZxl5Ixq6aCWmNv9 U3Paunt82ECQZ8423CpRvYbAM79e1A25F/fmKqNMRC6unNja6sj2lcz1pG4d65/Hnxd+814a+0Q F1w8TdGx6SiEw7kril+EJmtAXxG+LGUvAiLS03qREkEhveE+GBL8pXQv0dLmatJzufG9gTe8upR qDJc3UDsN5IGzWnjrXCIECjdstnk3omZ0B5Uv6FqFJF3Pdpn7CX9fhAqTScolhE9ogISyaSOifi X-Google-Smtp-Source: AGHT+IFNGquWo6o+yBJEmIMazjnVvxeA0Nv7KGN3vii+yokFL/TCuYl8xtfmk0/Am5op9UihAJeVjw== X-Received: by 2002:a05:6402:42c6:b0:5cf:a1c1:526e with SMTP id 4fb4d7f45d1cf-5cff4b1ffdfmr5763646a12.10.1732200604494; Thu, 21 Nov 2024 06:50:04 -0800 (PST) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Frediano Ziglio , Stefano Stabellini , Julien Grall Subject: [PATCH] xen/bitops: Fix break with in a for_each_set_bit() loop Date: Thu, 21 Nov 2024 14:50:00 +0000 Message-Id: <20241121145000.3107723-1-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.5 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1732200655610116600 for_each_set_bit()'s use of a double for loop had an accidental bug with a break in the inner loop leading to an infinite outer loop. Adjust for_each_set_bit() to avoid this behaviour, and add extend test_for_each_set_bit() with a test case for this. Fixes: ed26376f20bf ("xen/bitops: Introduce for_each_set_bit()") Signed-off-by: Andrew Cooper Reviewed-by: Frediano Ziglio Reviewed-by: Jan Beulich Reviewed-by: Roger Pau Monn=C3=A9 --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Frediano Ziglio CC: Stefano Stabellini CC: Julien Grall Both GCC and Clang seem happy with this, even at -O1: https://godbolt.org/z/o6ohjrzsY --- xen/common/bitops.c | 16 ++++++++++++++++ xen/include/xen/bitops.h | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/xen/common/bitops.c b/xen/common/bitops.c index 91ae961440af..0edd62d25c28 100644 --- a/xen/common/bitops.c +++ b/xen/common/bitops.c @@ -110,6 +110,22 @@ static void __init test_for_each_set_bit(void) =20 if ( ull !=3D ull_res ) panic("for_each_set_bit(uint64) expected %#"PRIx64", got %#"PRIx64= "\n", ull, ull_res); + + /* Check that we break from the middle of the loop */ + ui =3D HIDE(0x80001008U); + ui_res =3D 0; + for_each_set_bit ( i, ui ) + { + static __initdata unsigned int count; + + if ( count++ > 1 ) + break; + + ui_res |=3D 1U << i; + } + + if ( ui_res !=3D 0x1008 ) + panic("for_each_set_bit(break) expected 0x1008, got %#x\n", ui_res= ); } =20 static void __init test_multiple_bits_set(void) diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index 79615fb89d04..448b2d3e0937 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -299,7 +299,7 @@ static always_inline attr_const unsigned int fls64(uint= 64_t x) * A copy of @val is taken internally. */ #define for_each_set_bit(iter, val) \ - for ( typeof(val) __v =3D (val); __v; ) \ + for ( typeof(val) __v =3D (val); __v; __v =3D 0 ) \ for ( unsigned int (iter); \ __v && ((iter) =3D ffs_g(__v) - 1, true); \ __v &=3D __v - 1 ) base-commit: e0058760a0c7935ad0690d8b9babb9050eceedf0 --=20 2.39.5