From patchwork Fri May 24 20:03:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673595 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A2EB4C41513 for ; Fri, 24 May 2024 20:03:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729744.1134991 (Exim 4.92) (envelope-from ) id 1sAb8l-00077K-6I; Fri, 24 May 2024 20:03:47 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729744.1134991; Fri, 24 May 2024 20:03:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8l-00077D-1y; Fri, 24 May 2024 20:03:47 +0000 Received: by outflank-mailman (input) for mailman id 729744; Fri, 24 May 2024 20:03:45 +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 1sAb8j-00076x-Ka for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:45 +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 b96d3871-1a08-11ef-90a1-e314d9c70b13; Fri, 24 May 2024 22:03:44 +0200 (CEST) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-57857e0f462so1170233a12.0 for ; Fri, 24 May 2024 13:03:44 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:43 -0700 (PDT) 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: b96d3871-1a08-11ef-90a1-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581024; x=1717185824; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0cmvQAA1/fqugFr137dsLZT1PnHQ0umZh9dz+pm+nU8=; b=VPdJVyqgwasHqrUATPyBDmILeCRS/BnJotEdwvxpS8wB7ARW0hFTX2+ZwVAsTDFHhB ytswLBFA8+4GpToqoV7r8hzNQpjyWTTuza3yPdRzX/szY1UNQGS2SiEQnILFPK/QRJgZ +rF0NfkIYOGoeccIzU13c8zjB9/5CPak4BOts= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581024; x=1717185824; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0cmvQAA1/fqugFr137dsLZT1PnHQ0umZh9dz+pm+nU8=; b=S4za0inlRGdPqdmNHN3fgK9depTk1vshPnH0MomCsJEHfmjQaTBGYuz6KiszduLQ64 pAwyI80Jhepcp4Ty6AYuBInmfTQV0GHUGZ9BYxclci8swoojpou2dlf0XZ8XXxjHVTOQ Eq0AqOWOpdg+dzjPfO9XTzSlMFjwnRnM1hoOxrW8vpYWp2cavYe+vKfucKQaZ/auMIt/ sJHxT0Bwp0QFAgMtxuq5zjNNq5zDzmjJmCmQlnysKS4wzP0K8OX1lAUd6vztt0sY3rl1 05nrzzq2+91J0i/dwViPmWaV2/58jZcDyww1O81Ef6tnwMg9xdicISKV+7YhVclOpqpe IxQw== X-Gm-Message-State: AOJu0YwO84DycTcod+1811+cGFZLJdAXCdAloztrAam9xjurdpmjd6Uc 1pRux1QwHlrFVfNFu/MB4FHNIZ+1iuZdfuhDYF8iNjhY7evfMXE9rBmaHTLHS8dfUbw4ueXCMeu ln7Y= X-Google-Smtp-Source: AGHT+IFBtXPfaDlofqCV/C7m0DlQIHtiBrnlQHz5V05sHUxMp24nS1J5nbmw96zUOc2AFoBrVjV2mw== X-Received: by 2002:a17:906:3683:b0:a59:a431:f951 with SMTP id a640c23a62f3a-a6265148b8amr226929166b.48.1716581023916; Fri, 24 May 2024 13:03:43 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 01/13] ppc/boot: Run constructors on boot Date: Fri, 24 May 2024 21:03:26 +0100 Message-Id: <20240524200338.1232391-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 PPC collects constructors, but doesn't run them yet. Do so. They'll shortly be used to confirm correct behaviour of the bitops primitives. Signed-off-by: Andrew Cooper Acked-by: Shawn Anastasio --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini CI: https://gitlab.com/xen-project/people/andyhhp/xen/-/jobs/6931084695 v2: * New RISC-V collects them too, but can't call init_constructors() until lib/ctors.c is included in the build. Constructors is the only way to get these tests working on PPC/RISC-V as neither suvivie boot with initcalls() active. Then again, initcalls() are just a not-invented-here constructor, and we'd probably do well to move them over.. --- xen/arch/ppc/setup.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xen/arch/ppc/setup.c b/xen/arch/ppc/setup.c index 101bdd8bb648..7fe06aa4bfb0 100644 --- a/xen/arch/ppc/setup.c +++ b/xen/arch/ppc/setup.c @@ -39,6 +39,8 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4, setup_initial_pagetables(); + init_constructors(); + early_printk("Hello, ppc64le!\n"); for ( ; ; ) From patchwork Fri May 24 20:03:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673592 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 95B8DC27C42 for ; Fri, 24 May 2024 20:03:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729747.1135020 (Exim 4.92) (envelope-from ) id 1sAb8n-0007nn-3L; Fri, 24 May 2024 20:03:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729747.1135020; Fri, 24 May 2024 20:03:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8m-0007mt-UA; Fri, 24 May 2024 20:03:48 +0000 Received: by outflank-mailman (input) for mailman id 729747; Fri, 24 May 2024 20:03:47 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8l-000773-Ln for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:47 +0000 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [2a00:1450:4864:20::62f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id ba408386-1a08-11ef-b4bb-af5377834399; Fri, 24 May 2024 22:03:46 +0200 (CEST) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-a6265d3ccbdso119940266b.0 for ; Fri, 24 May 2024 13:03:46 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:44 -0700 (PDT) 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: ba408386-1a08-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581025; x=1717185825; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TaUcCJcK4BqMwPMepfH6t6Qsv4SxK/nVUJrRoe6o34w=; b=mcHmkHjUoslQCFVUUDVK61dolAGHbnYX1FUg2gGl/K3AJY6Nw6Kggp2e0+AsUSYZRs oSfwJz6/aP0o3Wmqmvpfpc2EE669FIYhXACVEgM641/p8Q460Pn38PKfKYska7TungVt gYmSp9V66Kdn2T2JUjQzlX9p2x/ESKoY1FO7c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581025; x=1717185825; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TaUcCJcK4BqMwPMepfH6t6Qsv4SxK/nVUJrRoe6o34w=; b=OJ9gibelfdSO8X4kgtWJoEF0hU9FKEz/oclUvDmNaidzEQ78iwpAoIGMLzveRBST+2 EEHFnrdGBdwHQ6XZ1SrkUX+8dAADAG4AueRkBn50jIAYpNnNkPjdlsmHx+rZP2kV7l1a rg5doEm/6KKUDYXkbDA4uHLCf/xvN/fqEU726MZxRYTwpmnIjQ+WU5yQ3NkKlrhfT9sg po4fGjPpeJMX3TqHRxwsy0XehJvbyfeF4f+A5sZ6Rk0JM7J6cACCjZmZPBRXJIhD4/Ar 2Es3AK9cHxz/TC4UoquMkUUiCnSD+fAbLAXkOTOMep0XLBgmSwZ0IWPlKCmusVH1zfkb vw0A== X-Gm-Message-State: AOJu0Yzne5SbKxM73+nUpfrd7YFGFiOhci0D0cNYa028m1NIo7RpHyA0 KXnpbWIT2UEx8r+cfpRwkG5C7LSOJVxW8ShhDKB/zNy1CTTwSxbDlRZi5VeK+XOGW3OYYgPN6+r M3g4= X-Google-Smtp-Source: AGHT+IEBtxw2KlfDn/D6rOsVW7w/BH+oTZGKvt35ZLp5TbEGg+n73+jDyDgG5CyxaxQOKjliu8EmvQ== X-Received: by 2002:a17:906:1d4c:b0:a5c:e2ea:ba59 with SMTP id a640c23a62f3a-a62642dab49mr169622666b.29.1716581025156; Fri, 24 May 2024 13:03:45 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 02/13] xen/bitops: Cleanup ahead of rearrangements Date: Fri, 24 May 2024 21:03:27 +0100 Message-Id: <20240524200338.1232391-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 * Rename __attribute_pure__ to just __pure before it gains users. * Introduce __constructor which is going to be used in lib/, and is unconditionally cf_check. * Identify the areas of xen/bitops.h which are a mess. * Introduce xen/boot-check.h as helpers for compile and boot time testing. This provides a statement of the ABI, and a confirmation that arch-specific implementations behave as expected. Sadly Clang 7 and older isn't happy with the compile time checks. Skip them, and just rely on the runtime checks. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * Break macros out into a header as they're going to be used elsewhere too * Use panic() rather than BUG_ON() to be more helpful when something fails * Brackets in HIDE() * Alignment adjustments * Skip COMPILE_CHECK() for Clang < 8 --- xen/include/xen/bitops.h | 13 ++++++-- xen/include/xen/boot-check.h | 60 ++++++++++++++++++++++++++++++++++++ xen/include/xen/compiler.h | 3 +- 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 xen/include/xen/boot-check.h diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index e3c5a4ccf321..9b40f20381a2 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -1,5 +1,7 @@ -#ifndef _LINUX_BITOPS_H -#define _LINUX_BITOPS_H +#ifndef XEN_BITOPS_H +#define XEN_BITOPS_H + +#include #include /* @@ -103,8 +105,13 @@ static inline int generic_flsl(unsigned long x) * Include this here because some architectures need generic_ffs/fls in * scope */ + +/* --------------------- Please tidy above here --------------------- */ + #include +/* --------------------- Please tidy below here --------------------- */ + #ifndef find_next_bit /** * find_next_bit - find the next set bit in a memory region @@ -294,4 +301,4 @@ static inline __u32 ror32(__u32 word, unsigned int shift) #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) -#endif +#endif /* XEN_BITOPS_H */ diff --git a/xen/include/xen/boot-check.h b/xen/include/xen/boot-check.h new file mode 100644 index 000000000000..250f9a40d3b0 --- /dev/null +++ b/xen/include/xen/boot-check.h @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * Helpers for boot-time checks of basic logic, including confirming that + * examples which should be calculated by the compiler are. + */ +#ifndef XEN_BOOT_CHECK_H +#define XEN_BOOT_CHECK_H + +#include + +/* Hide a value from the optimiser. */ +#define HIDE(x) \ + ({ typeof(x) _x = (x); asm volatile ( "" : "+r" (_x) ); _x; }) + +/* + * Check that fn(val) can be calcuated by the compiler, and that it gives the + * expected answer. + * + * Clang < 8 can't fold constants through static inlines, causing this to + * fail. Simply skip it for incredibly old compilers. + */ +#if !CONFIG_CC_IS_CLANG || CONFIG_CLANG_VERSION >= 80000 +#define COMPILE_CHECK(fn, val, res) \ + do { \ + typeof(fn(val)) real = fn(val); \ + \ + if ( !__builtin_constant_p(real) ) \ + asm ( ".error \"'" STR(fn(val)) "' not compile-time constant\"" ); \ + else if ( real != res ) \ + asm ( ".error \"Compile time check '" STR(fn(val) == res) "' failed\"" ); \ + } while ( 0 ) +#else +#define COMPILE_CHECK(fn, val, res) +#endif + +/* + * Check that Xen's runtime logic for fn(val) gives the expected answer. This + * requires using HIDE() to prevent the optimiser from collapsing the logic + * into a constant. + */ +#define RUNTIME_CHECK(fn, val, res) \ + do { \ + typeof(fn(val)) real = fn(HIDE(val)); \ + \ + if ( real != res ) \ + panic("%s: %s(%s) expected %u, got %u\n", \ + __func__, #fn, #val, real, res); \ + } while ( 0 ) + +/* + * Perform compiletime and runtime checks for fn(val) == res. + */ +#define CHECK(fn, val, res) \ + do { \ + COMPILE_CHECK(fn, val, res); \ + RUNTIME_CHECK(fn, val, res); \ + } while ( 0 ) + +#endif /* XEN_BOOT_CHECK_H */ diff --git a/xen/include/xen/compiler.h b/xen/include/xen/compiler.h index 179ff23e62c5..444bf80142c7 100644 --- a/xen/include/xen/compiler.h +++ b/xen/include/xen/compiler.h @@ -86,7 +86,8 @@ #define inline inline __init #endif -#define __attribute_pure__ __attribute__((__pure__)) +#define __constructor __attribute__((__constructor__)) cf_check +#define __pure __attribute__((__pure__)) #define __attribute_const__ __attribute__((__const__)) #define __transparent__ __attribute__((__transparent_union__)) From patchwork Fri May 24 20:03:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673593 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 938D2C25B7D for ; Fri, 24 May 2024 20:03:58 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729746.1135003 (Exim 4.92) (envelope-from ) id 1sAb8l-0007Jn-T5; Fri, 24 May 2024 20:03:47 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729746.1135003; Fri, 24 May 2024 20:03:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8l-0007Hm-Mc; Fri, 24 May 2024 20:03:47 +0000 Received: by outflank-mailman (input) for mailman id 729746; Fri, 24 May 2024 20:03:47 +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 1sAb8l-00076x-5Z for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:47 +0000 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [2a00:1450:4864:20::635]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id bab09028-1a08-11ef-90a1-e314d9c70b13; Fri, 24 May 2024 22:03:46 +0200 (CEST) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-a626ac4d299so130911666b.3 for ; Fri, 24 May 2024 13:03:46 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:45 -0700 (PDT) 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: bab09028-1a08-11ef-90a1-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581026; x=1717185826; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rnH7+DLTFV7y8fHOYV2l/9gV4Q+4MdvhQykzDmFzA1Q=; b=BXxkz8TrhETVdIlnQW5HMSmfVEsfaRN3l//4D2li3A3D1stDBnxa98ECMXVDsjVB2d Rq0LXvRoUpXz3VM6u0IZ5wK73LPSX8aTfC9ORpdaSvfWzdrspAgD+lfLzlv8qigzsEBv 7BBImx+RFrAaqgOy8yOBcKW15JtnGs5DTCz6o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581026; x=1717185826; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rnH7+DLTFV7y8fHOYV2l/9gV4Q+4MdvhQykzDmFzA1Q=; b=vi21uvdPX0YLhvrV93YXtIBciV8s/2f8ufX5pn5MVEHP2KSdYTw8DDAVTqHv7T58wb CGrNDwGDfrGqowxB89JLWR+mDPZbakE1HlM6wG7hRQaGB4AIlWxYYrRGEyG/KAA2tCra hxpRFTzxN3Maw+ZMXsuBOyz+SZ0FEZesCk74S/9ch5+2Gn+JrM59f7w2FPvv3HMUT1zY m1odqGMofvzKX1pWpn3Wz4vO6ZmKJiXmdfEFVCOmVXsk38alTl52eoBbovWRL3FJZEtP ywokkiuXr2pThthSqlKrrcG86a3vlDJCvPHzxXGDRx1zQuGhpgpg8cCjdcon5286JAmC +JPw== X-Gm-Message-State: AOJu0Yz1ngR92wJJDAzVPbXFQVSotggbni2Ma5Cp6O7YUkVCdsVxTKFm 2+i/u5lvRTXux9tYeX1lXdkrWqzHGr2FglXRdZcy10zENCFfndckxtqd2Ll2IJks68Em9mxVPsm U9Ks= X-Google-Smtp-Source: AGHT+IFQfNCCCpPoRMWhGk3Fjgu6D6H4jwZJNkukRDDsfp149c37Pbw35L5hdzTbWaMHsYivMRl++A== X-Received: by 2002:a17:906:f9c9:b0:a5a:f16:32b1 with SMTP id a640c23a62f3a-a62641cfb1dmr238210266b.31.1716581026128; Fri, 24 May 2024 13:03:46 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 03/13] ARM/bitops: Change find_first_set_bit() to be a define Date: Fri, 24 May 2024 21:03:28 +0100 Message-Id: <20240524200338.1232391-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 This is in order to maintain bisectability through the subsequent changes, as the order of definitions is altered. Signed-off-by: Andrew Cooper Acked-by: Stefano Stabellini --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * New --- xen/arch/arm/include/asm/bitops.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/xen/arch/arm/include/asm/bitops.h b/xen/arch/arm/include/asm/bitops.h index ab030b6cb032..199252201291 100644 --- a/xen/arch/arm/include/asm/bitops.h +++ b/xen/arch/arm/include/asm/bitops.h @@ -167,10 +167,7 @@ static inline int fls(unsigned int x) * Returns the bit-number of the first set bit (first bit being 0). * The input must *not* be zero. */ -static inline unsigned int find_first_set_bit(unsigned long word) -{ - return ffsl(word) - 1; -} +#define find_first_set_bit(w) (ffsl(w) - 1) /** * hweightN - returns the hamming weight of a N-bit word From patchwork Fri May 24 20:03:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673589 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E69A6C25B79 for ; Fri, 24 May 2024 20:03:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729748.1135025 (Exim 4.92) (envelope-from ) id 1sAb8n-0007rh-CP; Fri, 24 May 2024 20:03:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729748.1135025; Fri, 24 May 2024 20:03:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8n-0007r0-7c; Fri, 24 May 2024 20:03:49 +0000 Received: by outflank-mailman (input) for mailman id 729748; Fri, 24 May 2024 20:03:48 +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 1sAb8m-00076x-7T for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:48 +0000 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [2a00:1450:4864:20::22d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id bb530fd6-1a08-11ef-90a1-e314d9c70b13; Fri, 24 May 2024 22:03:47 +0200 (CEST) Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2e576057c2bso137040041fa.1 for ; Fri, 24 May 2024 13:03:47 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:46 -0700 (PDT) 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: bb530fd6-1a08-11ef-90a1-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581027; x=1717185827; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HcvFli469CD4OUU+qKqIt2aUdVZOLqd4ksEwvUfVXhg=; b=tMjwoj/WoBzXXrRXI2vSSvfnBCJUWWz2JveF3LyAps+Pya3YK+JxJBS6kXaBfBZeN/ xwpVYmd255e+fgjFbdMkTNpVmNeZgfSt/QJpJ54YSxj2G/6bqgv+/2ue7gFY8Pr9slWf VANQO/Rex4kK+QKsF3EQfMDB3lPTApNOdUqIk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581027; x=1717185827; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HcvFli469CD4OUU+qKqIt2aUdVZOLqd4ksEwvUfVXhg=; b=n9LqQtZqtqt8J12rNKWvpCLDqcRJQ2rPm7CgOU2xMFA8gOtJaMGpCeLarlwIfRErtj ApLX5wTbLxO7XYFn72P6/KpzRP+Ju5u1C0RMtQ9j92gU9kvu4p6K66XBCPUniE9CJDUE 6m5XldV8uUCPygnY+2ar/fWFnwRvSv6+KgaIf2xYPQ/Sw1BZ31uWBMG9KsemRFcedlDr YHzt9eGP+TL9RvoaSS2HBUQhZ2qwRSmRERvVyA1RvxQla8F6yObamWnyx/r5qaJ2SL+Z UwyCUoMVKPpsmsphynTEzcE6BI97oaqy1Y6wZcWqzPaxfOZEp8NIXX33UE8W1GJgmb31 OA9w== X-Gm-Message-State: AOJu0YxuS15NSPVwp/73eGplKVLxIaLWqw46gsusG1yYHnKFZyEEfFoV vkVoJe8lBkMOHbUHv9sjzNrNxN0n9kC/83ZLM01AF0+RyC7hdZXMgm0nbqNp0EI0iAdd96xudeJ 4LNs= X-Google-Smtp-Source: AGHT+IFW77E6sOaTPxeY1Anh235eDcr+kQYnA1qSCVR/tfE2QVye//bW0h/5/1gXeVXq88hbfi6MTA== X-Received: by 2002:a2e:8910:0:b0:2e2:64d:6849 with SMTP id 38308e7fff4ca-2e95b27b09bmr24937071fa.50.1716581026945; Fri, 24 May 2024 13:03:46 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 04/13] xen/page_alloc: Coerce min(flsl(), foo) expressions to being unsigned Date: Fri, 24 May 2024 21:03:29 +0100 Message-Id: <20240524200338.1232391-5-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 This is in order to maintain bisectability through the subsequent changes, where flsl() changes sign-ness non-atomically by architecture. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * New --- xen/common/page_alloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 7c1bdfc046bf..8d3342e95236 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -1842,7 +1842,7 @@ static void _init_heap_pages(const struct page_info *pg, * Note that the value of ffsl() and flsl() starts from 1 so we need * to decrement it by 1. */ - unsigned int inc_order = min(MAX_ORDER, flsl(e - s) - 1); + unsigned int inc_order = min(MAX_ORDER + 0U, flsl(e - s) - 1U); if ( s ) inc_order = min(inc_order, ffsl(s) - 1U); @@ -2266,7 +2266,7 @@ void __init xenheap_max_mfn(unsigned long mfn) ASSERT(!first_node_initialised); ASSERT(!xenheap_bits); BUILD_BUG_ON((PADDR_BITS - PAGE_SHIFT) >= BITS_PER_LONG); - xenheap_bits = min(flsl(mfn + 1) - 1 + PAGE_SHIFT, PADDR_BITS); + xenheap_bits = min(flsl(mfn + 1) - 1U + PAGE_SHIFT, PADDR_BITS + 0U); printk(XENLOG_INFO "Xen heap: %u bits\n", xenheap_bits); } From patchwork Fri May 24 20:03:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673594 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B89BCC25B7F for ; Fri, 24 May 2024 20:03:58 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729749.1135040 (Exim 4.92) (envelope-from ) id 1sAb8p-0008Ma-R9; Fri, 24 May 2024 20:03:51 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729749.1135040; Fri, 24 May 2024 20:03:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8p-0008MR-N6; Fri, 24 May 2024 20:03:51 +0000 Received: by outflank-mailman (input) for mailman id 729749; Fri, 24 May 2024 20:03:49 +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 1sAb8n-00076x-DI for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:49 +0000 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [2a00:1450:4864:20::636]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id bbbae797-1a08-11ef-90a1-e314d9c70b13; Fri, 24 May 2024 22:03:48 +0200 (CEST) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a626ac4d299so130913866b.3 for ; Fri, 24 May 2024 13:03:48 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:47 -0700 (PDT) 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: bbbae797-1a08-11ef-90a1-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581028; x=1717185828; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bu+ePM1Qu+LYpA9RNu9XXOypr94Ji2YBQcexG1JyIro=; b=TbjXv1tHzR34Fx5UGQJaxQolIWj9YEHixaD1SyQBMNID3ug8TGtF8/0E/7LHD6Lrud eCtUXM02q/AnGODv4nbSGDcCNC1P04MjVLsVp3IdS3u2e//2sIaDoYkvzBI6rbS1AdP5 A6hVosMtYeKceyhHvAzG1apaLakpjd5SXMdSQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581028; x=1717185828; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bu+ePM1Qu+LYpA9RNu9XXOypr94Ji2YBQcexG1JyIro=; b=ietkOnjL9OekdGIH8rzK4gBJDxvdcFMBSLT8qo/1/2G+78ncJy1p4wOSgQK1tTI1fU TOC7LL+zsVSxmUd8G58L/6Bbpf0HFRxnQyUzaKzpIWVfRe6W7uXg2pRMcsSr8tRO0fVE XYfAvIsQfgh187fTtaYTC7FdZAn69xsEWqlvdjtvugC7DuSkjgG35gQtROPIl0CKl8ww g41EHQazrotoxzhqE9+SoLjO1obBMMvyog9gX0wTYEgGITxipka+DUfpnXTd3/2ij8j3 Wv3OA0QDM0urIaNSeSqTXep3n7KGwKrHroyEYUqesPbv0lRuGjwRNWiGakaYoK/5WkGj 8agw== X-Gm-Message-State: AOJu0YySZuK8L+BmQyPM4u7mh9vRXrg83WGYTFxZgt5mhHRiqVwDrQCa y/0PqSRJDlZsxbarxhbnBabuNkn+n7MWPRQy52NMuhciSXPRb8HXeCYwIP4Pr+XnwZlkdIcg1CE HVWg= X-Google-Smtp-Source: AGHT+IGl+jcja4m/MU00cdn/ZAVvrCiijYwwRz1no2Yal4kdantqZIbqF2IKRw0UMtOqBz/1vjY/fg== X-Received: by 2002:a17:906:e907:b0:a59:bd78:34ca with SMTP id a640c23a62f3a-a62643e2443mr220690166b.36.1716581027828; Fri, 24 May 2024 13:03:47 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 05/13] xen/bitops: Implement generic_f?sl() in lib/ Date: Fri, 24 May 2024 21:03:30 +0100 Message-Id: <20240524200338.1232391-6-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 generic_f?s() being static inline is the cause of lots of the complexity between the common and arch-specific bitops.h They appear to be static inline for constant-folding reasons (ARM uses them for this), but there are better ways to achieve the same effect. It is presumptuous that an unrolled binary search is the right algorithm to use on all microarchitectures. Indeed, it's not for the eventual users, but that can be addressed at a later point. It is also nonsense to implement the int form as the base primitive and construct the long form from 2x int in 64-bit builds, when it's just one extra step to operate at the native register width. Therefore, implement generic_f?sl() in lib/. They're not actually needed in x86/ARM/PPC by the end of the cleanup (i.e. the functions will be dropped by the linker), and they're only expected be needed by RISC-V on hardware which lacks the Zbb extension. Implement generic_fls() in terms of generic_flsl() for now, but this will be cleaned up in due course. Provide basic runtime testing using __constructor inside the lib/ file. This is important, as it means testing runs if and only if generic_f?sl() are used elsewhere in Xen. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich Acked-by: Stefano Stabellini --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * New I suspect we want to swap CONFIG_DEBUG for CONFIG_BOOT_UNIT_TESTS in due course. These ought to be able to be used in a release build too. --- xen/arch/arm/include/asm/bitops.h | 2 +- xen/arch/ppc/include/asm/bitops.h | 2 +- xen/include/xen/bitops.h | 89 ++----------------------------- xen/lib/Makefile | 2 + xen/lib/generic-ffsl.c | 65 ++++++++++++++++++++++ xen/lib/generic-flsl.c | 68 +++++++++++++++++++++++ 6 files changed, 142 insertions(+), 86 deletions(-) create mode 100644 xen/lib/generic-ffsl.c create mode 100644 xen/lib/generic-flsl.c diff --git a/xen/arch/arm/include/asm/bitops.h b/xen/arch/arm/include/asm/bitops.h index 199252201291..ec1cf7b9b323 100644 --- a/xen/arch/arm/include/asm/bitops.h +++ b/xen/arch/arm/include/asm/bitops.h @@ -150,7 +150,7 @@ static inline int fls(unsigned int x) int ret; if (__builtin_constant_p(x)) - return generic_fls(x); + return generic_flsl(x); asm("clz\t%"__OP32"0, %"__OP32"1" : "=r" (ret) : "r" (x)); return 32 - ret; diff --git a/xen/arch/ppc/include/asm/bitops.h b/xen/arch/ppc/include/asm/bitops.h index bea655796d64..ab692d01717b 100644 --- a/xen/arch/ppc/include/asm/bitops.h +++ b/xen/arch/ppc/include/asm/bitops.h @@ -172,7 +172,7 @@ static inline int __test_and_clear_bit(int nr, volatile void *addr) } #define flsl(x) generic_flsl(x) -#define fls(x) generic_fls(x) +#define fls(x) generic_flsl(x) #define ffs(x) ({ unsigned int t_ = (x); fls(t_ & -t_); }) #define ffsl(x) ({ unsigned long t_ = (x); flsl(t_ & -t_); }) diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index 9b40f20381a2..cd405df96180 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -15,91 +15,12 @@ (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LLONG - 1 - (h)))) /* - * ffs: find first bit set. This is defined the same way as - * the libc and compiler builtin ffs routines, therefore - * differs in spirit from the above ffz (man ffs). - */ - -static inline int generic_ffs(unsigned int x) -{ - int r = 1; - - if (!x) - return 0; - if (!(x & 0xffff)) { - x >>= 16; - r += 16; - } - if (!(x & 0xff)) { - x >>= 8; - r += 8; - } - if (!(x & 0xf)) { - x >>= 4; - r += 4; - } - if (!(x & 3)) { - x >>= 2; - r += 2; - } - if (!(x & 1)) { - x >>= 1; - r += 1; - } - return r; -} - -/* - * fls: find last bit set. + * Find First/Last Set bit. + * + * Bits are labelled from 1. Returns 0 if given 0. */ - -static inline int generic_fls(unsigned int x) -{ - int r = 32; - - if (!x) - return 0; - if (!(x & 0xffff0000u)) { - x <<= 16; - r -= 16; - } - if (!(x & 0xff000000u)) { - x <<= 8; - r -= 8; - } - if (!(x & 0xf0000000u)) { - x <<= 4; - r -= 4; - } - if (!(x & 0xc0000000u)) { - x <<= 2; - r -= 2; - } - if (!(x & 0x80000000u)) { - x <<= 1; - r -= 1; - } - return r; -} - -#if BITS_PER_LONG == 64 - -static inline int generic_ffsl(unsigned long x) -{ - return !x || (u32)x ? generic_ffs(x) : generic_ffs(x >> 32) + 32; -} - -static inline int generic_flsl(unsigned long x) -{ - u32 h = x >> 32; - - return h ? generic_fls(h) + 32 : generic_fls(x); -} - -#else -# define generic_ffsl generic_ffs -# define generic_flsl generic_fls -#endif +unsigned int __pure generic_ffsl(unsigned long x); +unsigned int __pure generic_flsl(unsigned long x); /* * Include this here because some architectures need generic_ffs/fls in diff --git a/xen/lib/Makefile b/xen/lib/Makefile index e63798e1d452..a48541596470 100644 --- a/xen/lib/Makefile +++ b/xen/lib/Makefile @@ -4,6 +4,8 @@ lib-y += bsearch.o lib-y += ctors.o lib-y += ctype.o lib-y += find-next-bit.o +lib-y += generic-ffsl.o +lib-y += generic-flsl.o lib-y += list-sort.o lib-y += memchr.o lib-y += memchr_inv.o diff --git a/xen/lib/generic-ffsl.c b/xen/lib/generic-ffsl.c new file mode 100644 index 000000000000..804cbd752efe --- /dev/null +++ b/xen/lib/generic-ffsl.c @@ -0,0 +1,65 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include +#include + +unsigned int generic_ffsl(unsigned long x) +{ + unsigned int r = 1; + + if ( !x ) + return 0; + +#if BITS_PER_LONG > 32 + if ( !(x & 0xffffffffU) ) + { + x >>= 32; + r += 32; + } +#endif + if ( !(x & 0xffff) ) + { + x >>= 16; + r += 16; + } + if ( !(x & 0xff) ) + { + x >>= 8; + r += 8; + } + if ( !(x & 0xf) ) + { + x >>= 4; + r += 4; + } + if ( !(x & 3) ) + { + x >>= 2; + r += 2; + } + if ( !(x & 1) ) + { + x >>= 1; + r += 1; + } + + return r; +} + +#ifdef CONFIG_DEBUG +static void __init __constructor test_generic_ffsl(void) +{ + RUNTIME_CHECK(generic_ffsl, 0, 0); + RUNTIME_CHECK(generic_ffsl, 1, 1); + RUNTIME_CHECK(generic_ffsl, 3, 1); + RUNTIME_CHECK(generic_ffsl, 7, 1); + RUNTIME_CHECK(generic_ffsl, 6, 2); + + RUNTIME_CHECK(generic_ffsl, 1UL << (BITS_PER_LONG - 1), BITS_PER_LONG); +#if BITS_PER_LONG > 32 + RUNTIME_CHECK(generic_ffsl, 1UL << 32, 33); + RUNTIME_CHECK(generic_ffsl, 1UL << 63, 64); +#endif +} +#endif /* CONFIG_DEBUG */ diff --git a/xen/lib/generic-flsl.c b/xen/lib/generic-flsl.c new file mode 100644 index 000000000000..e4543aeaf100 --- /dev/null +++ b/xen/lib/generic-flsl.c @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include +#include + +/* Mask of type UL with the upper x bits set. */ +#define UPPER_MASK(x) (~0UL << (BITS_PER_LONG - (x))) + +unsigned int generic_flsl(unsigned long x) +{ + unsigned int r = BITS_PER_LONG; + + if ( !x ) + return 0; + +#if BITS_PER_LONG > 32 + if ( !(x & UPPER_MASK(32)) ) + { + x <<= 32; + r -= 32; + } +#endif + if ( !(x & UPPER_MASK(16)) ) + { + x <<= 16; + r -= 16; + } + if ( !(x & UPPER_MASK(8)) ) + { + x <<= 8; + r -= 8; + } + if ( !(x & UPPER_MASK(4)) ) + { + x <<= 4; + r -= 4; + } + if ( !(x & UPPER_MASK(2)) ) + { + x <<= 2; + r -= 2; + } + if ( !(x & UPPER_MASK(1)) ) + { + x <<= 1; + r -= 1; + } + + return r; +} + +#ifdef CONFIG_DEBUG +static void __init __constructor test_generic_flsl(void) +{ + RUNTIME_CHECK(generic_flsl, 0, 0); + RUNTIME_CHECK(generic_flsl, 1, 1); + RUNTIME_CHECK(generic_flsl, 3, 2); + RUNTIME_CHECK(generic_flsl, 7, 3); + RUNTIME_CHECK(generic_flsl, 6, 3); + + RUNTIME_CHECK(generic_flsl, 1 | (1UL << (BITS_PER_LONG - 1)), BITS_PER_LONG); +#if BITS_PER_LONG > 32 + RUNTIME_CHECK(generic_flsl, 1 | (1UL << 32), 33); + RUNTIME_CHECK(generic_flsl, 1 | (1UL << 63), 64); +#endif +} +#endif /* CONFIG_DEBUG */ From patchwork Fri May 24 20:03:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673596 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A2B79C27C43 for ; Fri, 24 May 2024 20:03:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729751.1135059 (Exim 4.92) (envelope-from ) id 1sAb8r-0000Ot-HW; Fri, 24 May 2024 20:03:53 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729751.1135059; Fri, 24 May 2024 20:03:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8r-0000Nd-Ce; Fri, 24 May 2024 20:03:53 +0000 Received: by outflank-mailman (input) for mailman id 729751; Fri, 24 May 2024 20:03:52 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8q-000773-2L for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:52 +0000 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [2a00:1450:4864:20::52f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bcd8def2-1a08-11ef-b4bb-af5377834399; Fri, 24 May 2024 22:03:50 +0200 (CEST) Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-578517c7ae9so1585194a12.3 for ; Fri, 24 May 2024 13:03:50 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:48 -0700 (PDT) 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: bcd8def2-1a08-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581029; x=1717185829; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kIl6ox/sphrOz6UFI6xM1xqU962sO2kBKKT4aLgRbJg=; b=hZfnLyQsdBdRA5tWILs1WM3bIsDtlcwqINDLfI4JpCMKKtsBWOAFSiOlMgaRUZSDQH FzrqPEa8yMfRKCm1Nbjc96gv11s4VpsjugV0pM3l6QC8wLYo/aUL1VzHxN/qmpm2BZjB anmgeeG0KCXFRZReYOZ+PAdHzExgfNo4lQzI4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581029; x=1717185829; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kIl6ox/sphrOz6UFI6xM1xqU962sO2kBKKT4aLgRbJg=; b=n62X7eyHznajFpCF94oATfA9z43bUfDvB2VV1IfFo3sbnQNaGNco1KqYVuWHfHrWpJ 9/nHvBKjZ61NHD29ZHCuGFdODQCgE6hrmnsMIkGK5zBtEk26jhbFpntgmnYDEXOIGELP izZglrg3acJxtNVtjhEWQ5BnYwcOA71cQeGAbC+KrjB/ertbJuEexfFohvnWbat+ot2p ih/hVL0BH5jdPvnkjrXpC4wvF6jNXnKd9+ShKXdSNQoOFIs+mtOUlMiA3RCw0lqTJtRW 674VyyIdG5M7LQXyuLXS1xlbkQcSMMjwaSDeDOdU9QKoTjmIykixMmHnXo+hwQ5ssfXL C5Pg== X-Gm-Message-State: AOJu0Yz/fOwOt5blhYpg9wQjoEsODu8o6AdC9wS8ybYoqHXfK2VphgAq U/YknOlKy69ymLRiC2qUdxZojAKyP6ONyW0q1fffwm30fTzjv0eO2DOH2POdSa2aq41Z3Js6Wp2 X3VA= X-Google-Smtp-Source: AGHT+IEUVNV4EpZfmOgPpyYnZ5/OwkBVDwrdSNszXAXbvrntU8FqgakDCrtsvjr4fA673YXJNwZzFw== X-Received: by 2002:a17:907:7844:b0:a5e:612e:fd58 with SMTP id a640c23a62f3a-a6264f126demr219304566b.51.1716581029010; Fri, 24 May 2024 13:03:49 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 06/13] xen/bitops: Implement ffs() in common logic Date: Fri, 24 May 2024 21:03:31 +0100 Message-Id: <20240524200338.1232391-7-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Perform constant-folding unconditionally, rather than having it implemented inconsistency between architectures. Confirm the expected behaviour with compile time and boot time tests. For non-constant inputs, use arch_ffs() if provided but fall back to generic_ffsl() if not. In particular, RISC-V doesn't have a builtin that works in all configurations. For x86, rename ffs() to arch_ffs() and adjust the prototype. For PPC, __builtin_ctz() is 1/3 of the size of size of the transform to generic_fls(). Drop the definition entirely. ARM too benefits in the general case by using __builtin_ctz(), but less dramatically because it using optimised asm(). Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich Reviewed-by: Stefano Stabellini --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * Fall back to generic, not builtin. * Extend the testing with multi-bit values. * Use always_inline for x86 * Defer x86 optimisation to a later change --- xen/arch/arm/include/asm/bitops.h | 2 +- xen/arch/ppc/include/asm/bitops.h | 2 +- xen/arch/x86/include/asm/bitops.h | 3 ++- xen/common/Makefile | 1 + xen/common/bitops.c | 19 +++++++++++++++++++ xen/include/xen/bitops.h | 17 +++++++++++++++++ 6 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 xen/common/bitops.c diff --git a/xen/arch/arm/include/asm/bitops.h b/xen/arch/arm/include/asm/bitops.h index ec1cf7b9b323..a88ec2612e16 100644 --- a/xen/arch/arm/include/asm/bitops.h +++ b/xen/arch/arm/include/asm/bitops.h @@ -157,7 +157,7 @@ static inline int fls(unsigned int x) } -#define ffs(x) ({ unsigned int __t = (x); fls(ISOLATE_LSB(__t)); }) +#define arch_ffs(x) ((x) ? 1 + __builtin_ctz(x) : 0) #define ffsl(x) ({ unsigned long __t = (x); flsl(ISOLATE_LSB(__t)); }) /** diff --git a/xen/arch/ppc/include/asm/bitops.h b/xen/arch/ppc/include/asm/bitops.h index ab692d01717b..5c36a6cc0ce3 100644 --- a/xen/arch/ppc/include/asm/bitops.h +++ b/xen/arch/ppc/include/asm/bitops.h @@ -173,7 +173,7 @@ static inline int __test_and_clear_bit(int nr, volatile void *addr) #define flsl(x) generic_flsl(x) #define fls(x) generic_flsl(x) -#define ffs(x) ({ unsigned int t_ = (x); fls(t_ & -t_); }) +#define arch_ffs(x) ((x) ? 1 + __builtin_ctz(x) : 0) #define ffsl(x) ({ unsigned long t_ = (x); flsl(t_ & -t_); }) /** diff --git a/xen/arch/x86/include/asm/bitops.h b/xen/arch/x86/include/asm/bitops.h index 5a71afbc89d5..122767fc0d10 100644 --- a/xen/arch/x86/include/asm/bitops.h +++ b/xen/arch/x86/include/asm/bitops.h @@ -430,7 +430,7 @@ static inline int ffsl(unsigned long x) return (int)r+1; } -static inline int ffs(unsigned int x) +static always_inline unsigned int arch_ffs(unsigned int x) { int r; @@ -440,6 +440,7 @@ static inline int ffs(unsigned int x) "1:" : "=r" (r) : "rm" (x)); return r + 1; } +#define arch_ffs arch_ffs /** * fls - find last bit set diff --git a/xen/common/Makefile b/xen/common/Makefile index d512cad5243f..21a4fb4c7166 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -1,5 +1,6 @@ obj-$(CONFIG_ARGO) += argo.o obj-y += bitmap.o +obj-bin-$(CONFIG_DEBUG) += bitops.init.o obj-$(CONFIG_GENERIC_BUG_FRAME) += bug.o obj-$(CONFIG_HYPFS_CONFIG) += config_data.o obj-$(CONFIG_CORE_PARKING) += core_parking.o diff --git a/xen/common/bitops.c b/xen/common/bitops.c new file mode 100644 index 000000000000..8c161b8ea7fa --- /dev/null +++ b/xen/common/bitops.c @@ -0,0 +1,19 @@ +#include +#include +#include + +static void __init test_ffs(void) +{ + /* unsigned int ffs(unsigned int) */ + CHECK(ffs, 0, 0); + CHECK(ffs, 1, 1); + CHECK(ffs, 3, 1); + CHECK(ffs, 7, 1); + CHECK(ffs, 6, 2); + CHECK(ffs, 0x80000000U, 32); +} + +static void __init __constructor test_bitops(void) +{ + test_ffs(); +} diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index cd405df96180..f7e90a2893a5 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -31,6 +31,23 @@ unsigned int __pure generic_flsl(unsigned long x); #include +/* + * Find First/Last Set bit (all forms). + * + * Bits are labelled from 1. Returns 0 if given 0. + */ +static always_inline __pure unsigned int ffs(unsigned int x) +{ + if ( __builtin_constant_p(x) ) + return __builtin_ffs(x); + +#ifdef arch_ffs + return arch_ffs(x); +#else + return generic_ffsl(x); +#endif +} + /* --------------------- Please tidy below here --------------------- */ #ifndef find_next_bit From patchwork Fri May 24 20:03:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673591 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C6F97C25B7E for ; Fri, 24 May 2024 20:03:58 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729750.1135046 (Exim 4.92) (envelope-from ) id 1sAb8q-0008Qz-BB; Fri, 24 May 2024 20:03:52 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729750.1135046; Fri, 24 May 2024 20:03:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8q-0008Q7-0z; Fri, 24 May 2024 20:03:52 +0000 Received: by outflank-mailman (input) for mailman id 729750; Fri, 24 May 2024 20:03:51 +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 1sAb8p-00076x-4k for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:51 +0000 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [2a00:1450:4864:20::533]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id bd08fe0b-1a08-11ef-90a1-e314d9c70b13; Fri, 24 May 2024 22:03:50 +0200 (CEST) Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-57857e0f465so1148359a12.1 for ; Fri, 24 May 2024 13:03:50 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:49 -0700 (PDT) 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: bd08fe0b-1a08-11ef-90a1-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581030; x=1717185830; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=s4dvCcdXKh6qx9C0SJSN8op2mWkgo8zuNPTidlZTBXU=; b=reUBOJmf1e5428vJskaJS1GtdgRJ7z02uxzxYV1OCXvzkopMKeVvnOrkE2L8n8tYc9 oETtHEk8gD2P40lb1QmZ2WagdYLSyeUdMAt5IRPPV+vh25DWgk4drijRtLL3akvCrV3u 9ZIOXjH0Gxbw/IZ1wLuRB2WD627VaEKDw3GCU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581030; x=1717185830; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s4dvCcdXKh6qx9C0SJSN8op2mWkgo8zuNPTidlZTBXU=; b=wl7Etr7+vgdJBonoBoFVHQ9z2M7DpAdlBR+LwdVNw/oUYiu0nwC9EYnDNdQbdcfSNZ daFn7zlZCWW/QdDzLxyqsae/WF5ztLadihc8/DBIpI+6XX7BrWxFM8CIBgJQK1o2oIWC wBEBPuT59WjR3uOylwwXhzv8BZBY2lRFRMmmW+vm2YylJmhlB8OZ6NgpdgZaD/H5hv7K Ixvenn+VkAP9vWZ/9VcaqegWWOTKQvc8Gp98Ro/I3FbKjJ2l+NvMLQYnTBP3LbC59d4v 3c1+uE6Px/mJ+M4fokGXuSxfhv5K6MWafvaJ+ePEa+q/QPdGaSmMeu9Ixjqt5ga2KYT4 bxFw== X-Gm-Message-State: AOJu0YxWRaUfSwAckQh2KVkGucuzwqprVsa5tM2MZ0ii0pDtpRFyJohT fBKL5k4bNBGIw28S+2/Rjmv1SBrUTyGJ4kVvmFq1If+FTqYw40zuqiFv3VOtKCg7FKQKnsS3pAX 5F+k= X-Google-Smtp-Source: AGHT+IGpbR8qQpWJhSYJH93pbLDUKTmP1Mf1+5HUrKKu6ErvFgLI74jMTr42QIaJ6HBcUl8sMDuvhw== X-Received: by 2002:a17:906:c0e:b0:a62:49ba:8242 with SMTP id a640c23a62f3a-a62651118admr194060366b.77.1716581029839; Fri, 24 May 2024 13:03:49 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 07/13] x86/bitops: Improve arch_ffs() in the general case Date: Fri, 24 May 2024 21:03:32 +0100 Message-Id: <20240524200338.1232391-8-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 The asm in arch_ffs() is safe but inefficient. CMOV would be an improvement over a conditional branch, but for 64bit CPUs both Intel and AMD have provided enough details about the behaviour for a zero input. It is safe to pre-load the destination register with -1 and drop the conditional logic. However, it is common to find ffs() in a context where the optimiser knows that x in nonzero even if it the value isn't known precisely, and in that case it's safe to drop the preload of -1 too. There are only a handful of uses of ffs() in the x86 build, and all of them improve as a result of this: add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-31 (-31) Function old new delta mask_write 114 107 -7 xmem_pool_alloc 1063 1039 -24 Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * New. * Use __builtin_constant_p(x > 0) to optimise better. --- xen/arch/x86/include/asm/bitops.h | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/xen/arch/x86/include/asm/bitops.h b/xen/arch/x86/include/asm/bitops.h index 122767fc0d10..1d7aea6065ef 100644 --- a/xen/arch/x86/include/asm/bitops.h +++ b/xen/arch/x86/include/asm/bitops.h @@ -432,12 +432,28 @@ static inline int ffsl(unsigned long x) static always_inline unsigned int arch_ffs(unsigned int x) { - int r; + unsigned int r; + + if ( __builtin_constant_p(x > 0) && x > 0 ) + { + /* Safe, when the compiler knows that x is nonzero. */ + asm ( "bsf %[val], %[res]" + : [res] "=r" (r) + : [val] "rm" (x) ); + } + else + { + /* + * The AMD manual states that BSF won't modify the destination + * register if x=0. The Intel manual states that the result is + * undefined, but the architects have said that the register is + * written back with it's old value (zero extended as normal). + */ + asm ( "bsf %[val], %[res]" + : [res] "=r" (r) + : [val] "rm" (x), "[res]" (-1) ); + } - asm ( "bsf %1,%0\n\t" - "jnz 1f\n\t" - "mov $-1,%0\n" - "1:" : "=r" (r) : "rm" (x)); return r + 1; } #define arch_ffs arch_ffs From patchwork Fri May 24 20:03:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673597 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7406BC25B74 for ; Fri, 24 May 2024 20:04:03 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729752.1135070 (Exim 4.92) (envelope-from ) id 1sAb8t-0000mB-61; Fri, 24 May 2024 20:03:55 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729752.1135070; Fri, 24 May 2024 20:03:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8t-0000lM-0c; Fri, 24 May 2024 20:03:55 +0000 Received: by outflank-mailman (input) for mailman id 729752; Fri, 24 May 2024 20:03:53 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8r-000773-GY for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:53 +0000 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [2a00:1450:4864:20::52f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bdc52165-1a08-11ef-b4bb-af5377834399; Fri, 24 May 2024 22:03:51 +0200 (CEST) Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-57863a8f4b2so697576a12.0 for ; Fri, 24 May 2024 13:03:51 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:50 -0700 (PDT) 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: bdc52165-1a08-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581031; x=1717185831; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nXTYEDY4WICxpm22K4ZAGRtYdV5nmr0pV6oeY/cb0HI=; b=svZVCs81BDol0Nqi7hrpXxJqPkVkkSFCmLJJCc240DBF13QxqFCT7f2N51das48CRM BbWvagSsw/7+U+vxw8jB0vGF3Q2B7mVPq6kbx3V/zkV0X7El6ej/lOWYhnA2S7RsZVfF gPSWHE+9TWy2IOXRQQGSAFGdSDUNTxwBhI0dU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581031; x=1717185831; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nXTYEDY4WICxpm22K4ZAGRtYdV5nmr0pV6oeY/cb0HI=; b=dndMvJkb8jP+dmoEXIe3+if+gEbmR1+gIzYkdz8NYkuEZdMGCbkVOQcry4lwX35ZMh 0niO8G/Nw/0Q4O8vM70uOtyLDa0g/8nuygLKRLEA0KP0cY+ivhZuNUkiR0Wfhd0BYGDX GB21mfzzAw5swWjfqkiNJU+6ItGVFhuWRySlUtY97ohlNHlN37G94iSodczeFWuWLvlc lYkZJnAuj0CAPJH856yWskG0MLWKn7M8VrxkvKeId9j1RQFW/FwDU82+M62JjvnagXDv DNl7Ihc0HK4sL3lXl7HJZAkCXXHDgUdqzuf7TJjEEsS+FdnD979yoLTScY3bMQwZsTjb yDIA== X-Gm-Message-State: AOJu0YyCA6Kw/734pZruKaeKrRnDn3WLtQDOzYZ0qWt9twzvRVd4Uk+8 QRytzUCmLRLGPfNebGt8WC94gqfg8DwA8/dcc0oNw7Ajf1PFZfoDPiiDsEyV/Onup3eozdJ9LRm wC4k= X-Google-Smtp-Source: AGHT+IFb3M2wCqEZnAg0P7Oi7/XtKFSPQytPacHnXNOWFHuZBirGiXxcH2/y/aqny8HR74UaaGekqQ== X-Received: by 2002:a17:906:3ed1:b0:a59:c23d:85d8 with SMTP id a640c23a62f3a-a6264f15e2cmr222340566b.51.1716581031018; Fri, 24 May 2024 13:03:51 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 08/13] xen/bitops: Implement ffsl() in common logic Date: Fri, 24 May 2024 21:03:33 +0100 Message-Id: <20240524200338.1232391-9-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Just like ffs() in the previous changes. Express the upper bound of the testing in terms of BITS_PER_LONG as it varies between architectures. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich Acked-by: Stefano Stabellini --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * Swap to #if BITS_PER_LONG > 32 to avoid a compile error on arm32 * Changes to mirror ffs() v2. --- xen/arch/arm/include/asm/bitops.h | 2 +- xen/arch/ppc/include/asm/bitops.h | 2 +- xen/arch/x86/include/asm/bitops.h | 35 ++++++++++++++++--------------- xen/common/bitops.c | 13 ++++++++++++ xen/include/xen/bitops.h | 12 +++++++++++ 5 files changed, 45 insertions(+), 19 deletions(-) diff --git a/xen/arch/arm/include/asm/bitops.h b/xen/arch/arm/include/asm/bitops.h index a88ec2612e16..ba39802c9de3 100644 --- a/xen/arch/arm/include/asm/bitops.h +++ b/xen/arch/arm/include/asm/bitops.h @@ -158,7 +158,7 @@ static inline int fls(unsigned int x) #define arch_ffs(x) ((x) ? 1 + __builtin_ctz(x) : 0) -#define ffsl(x) ({ unsigned long __t = (x); flsl(ISOLATE_LSB(__t)); }) +#define arch_ffsl(x) ((x) ? 1 + __builtin_ctzl(x) : 0) /** * find_first_set_bit - find the first set bit in @word diff --git a/xen/arch/ppc/include/asm/bitops.h b/xen/arch/ppc/include/asm/bitops.h index 5c36a6cc0ce3..ce0f6436f727 100644 --- a/xen/arch/ppc/include/asm/bitops.h +++ b/xen/arch/ppc/include/asm/bitops.h @@ -174,7 +174,7 @@ static inline int __test_and_clear_bit(int nr, volatile void *addr) #define flsl(x) generic_flsl(x) #define fls(x) generic_flsl(x) #define arch_ffs(x) ((x) ? 1 + __builtin_ctz(x) : 0) -#define ffsl(x) ({ unsigned long t_ = (x); flsl(t_ & -t_); }) +#define arch_ffsl(x) ((x) ? 1 + __builtin_ctzl(x) : 0) /** * hweightN - returns the hamming weight of a N-bit word diff --git a/xen/arch/x86/include/asm/bitops.h b/xen/arch/x86/include/asm/bitops.h index 1d7aea6065ef..51d3c0f40473 100644 --- a/xen/arch/x86/include/asm/bitops.h +++ b/xen/arch/x86/include/asm/bitops.h @@ -413,23 +413,6 @@ static inline unsigned int find_first_set_bit(unsigned long word) return (unsigned int)word; } -/** - * ffs - find first bit set - * @x: the word to search - * - * This is defined the same way as the libc and compiler builtin ffs routines. - */ -static inline int ffsl(unsigned long x) -{ - long r; - - asm ( "bsf %1,%0\n\t" - "jnz 1f\n\t" - "mov $-1,%0\n" - "1:" : "=r" (r) : "rm" (x)); - return (int)r+1; -} - static always_inline unsigned int arch_ffs(unsigned int x) { unsigned int r; @@ -458,6 +441,24 @@ static always_inline unsigned int arch_ffs(unsigned int x) } #define arch_ffs arch_ffs +static always_inline unsigned int arch_ffsl(unsigned long x) +{ + unsigned int r; + + /* See arch_ffs() for safety discussions. */ + if ( __builtin_constant_p(x > 0) && x > 0 ) + asm ( "bsf %[val], %q[res]" + : [res] "=r" (r) + : [val] "rm" (x) ); + else + asm ( "bsf %[val], %q[res]" + : [res] "=r" (r) + : [val] "rm" (x), "[res]" (-1) ); + + return r + 1; +} +#define arch_ffsl arch_ffsl + /** * fls - find last bit set * @x: the word to search diff --git a/xen/common/bitops.c b/xen/common/bitops.c index 8c161b8ea7fa..b3813f818198 100644 --- a/xen/common/bitops.c +++ b/xen/common/bitops.c @@ -11,6 +11,19 @@ static void __init test_ffs(void) CHECK(ffs, 7, 1); CHECK(ffs, 6, 2); CHECK(ffs, 0x80000000U, 32); + + /* unsigned int ffsl(unsigned long) */ + CHECK(ffsl, 0, 0); + CHECK(ffsl, 1, 1); + CHECK(ffsl, 3, 1); + CHECK(ffsl, 7, 1); + CHECK(ffsl, 6, 2); + + CHECK(ffsl, 1UL << (BITS_PER_LONG - 1), BITS_PER_LONG); +#if BITS_PER_LONG > 32 + CHECK(ffsl, 1UL << 32, 33); + CHECK(ffsl, 1UL << 63, 64); +#endif } static void __init __constructor test_bitops(void) diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index f7e90a2893a5..88cf27a88bcf 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -48,6 +48,18 @@ static always_inline __pure unsigned int ffs(unsigned int x) #endif } +static always_inline __pure unsigned int ffsl(unsigned long x) +{ + if ( __builtin_constant_p(x) ) + return __builtin_ffsl(x); + +#ifdef arch_ffs + return arch_ffsl(x); +#else + return generic_ffsl(x); +#endif +} + /* --------------------- Please tidy below here --------------------- */ #ifndef find_next_bit From patchwork Fri May 24 20:03:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673598 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EE7BAC25B7D for ; Fri, 24 May 2024 20:04:04 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729753.1135079 (Exim 4.92) (envelope-from ) id 1sAb8u-00012E-Go; Fri, 24 May 2024 20:03:56 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729753.1135079; Fri, 24 May 2024 20:03:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8u-00011V-Ay; Fri, 24 May 2024 20:03:56 +0000 Received: by outflank-mailman (input) for mailman id 729753; Fri, 24 May 2024 20:03:55 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8t-000773-3P for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:55 +0000 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [2a00:1450:4864:20::631]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id be7c9349-1a08-11ef-b4bb-af5377834399; Fri, 24 May 2024 22:03:53 +0200 (CEST) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-a5a89787ea4so1184828466b.2 for ; Fri, 24 May 2024 13:03:53 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:51 -0700 (PDT) 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: be7c9349-1a08-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581032; x=1717185832; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=h4H8slafbFD5MZ8pWJsbqpEr4l2E5HCz8G8Qyr9VoPE=; b=Z/CNqc6CpHqjwoOREq64r09/7yOcRxQblHLWOt3tTFmMwrrAovbMMUuBr++ejXxYid b1dzew5fMHnG9wWnUp2aRVjKUCkb4ZodzX9hyDvYTsu9U0Ua5dxJdNVJUkuTXmKm/R28 NQdRB7Pz1GHhLIX6A1t0V1XGyEYqHa2nELA4U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581032; x=1717185832; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=h4H8slafbFD5MZ8pWJsbqpEr4l2E5HCz8G8Qyr9VoPE=; b=bkZ3C3IGJqRn91vfuYFBKZ9ukstM+WR/Lfd6aZ+53s4xTOWEJ52S63SLOyz6A5K1o3 s/YfFK0croI5/ceyLU/dy28e1HE2+Cl1EiYUpc5Xa8uHDFn3GSXS1ZYVmsDI8JAaBM8/ fL6OnG8WFWzLz3lqvUZyWTRDL/5uG5AHPXNdGG0s1NEwIhIbYI+wMJcCs8RQ6WFTl+50 BxqTey3TbeUZHdFUCzEwGgG3vwtWe4F6mDUMukYcCX1REKm2yY9+dH8T44qtuOyp8HV6 KW3QwLniDimRsD1BRnuxj8hpMdD98coPxwKbwLXfzqHSqUSjVmv8FPdxdBNgbxpKcAHT QQ5w== X-Gm-Message-State: AOJu0Yzi30Zlvj5NO8+YUs2AG3ydCzjF0t+qQsM1TRIyE++HAK+AZAOp CtnlhDU/2bG0W3pEbNB7okkr1cx1qMrBQisbLO7BgFgQA/hi1JQI6Lsevyi8v9Mgv4EaPgoauaF O+xs= X-Google-Smtp-Source: AGHT+IEFnNtUkkX+5j+8K47eW4QSiYZz+ttxOCsa+8nNZwxQCIEI2xbVmGmD+ZqUVhiUQomAbDPrRg== X-Received: by 2002:a17:906:c214:b0:a5a:8b8c:6203 with SMTP id a640c23a62f3a-a62646d7f32mr215106166b.45.1716581032090; Fri, 24 May 2024 13:03:52 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 09/13] xen/bitops: Replace find_first_set_bit() with ffsl() - 1 Date: Fri, 24 May 2024 21:03:34 +0100 Message-Id: <20240524200338.1232391-10-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 find_first_set_bit() is a Xen-ism which has undefined behaviour with a 0 input. The latter is well defined with an input of 0, and is a found outside of Xen too. _init_heap_pages() is the one special case here, comparing the LSB of two different addresses. The -1 cancels off both sides of the expression. No functional change. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * Reorder from later in the series to keep ARM bisectable In an x86 build, we get the following delta: add/remove: 0/0 grow/shrink: 2/4 up/down: 39/-52 (-13) Function old new delta hpet_write 2183 2206 +23 init_heap_pages 1222 1238 +16 dom0_construct_pvh 3959 3958 -1 mapping_order 139 126 -13 guest_physmap_mark_populate_on_demand 1301 1285 -16 vcpumask_to_pcpumask 525 503 -22 so the optimiser improvements for ffsl() really do speak for themselves. I'm surprised by the increase in hpet_write(), but looking at the code, it very clearly wants the same treatment as: commit 188fa82305e72b725473db9146e20cc9abf7bff3 Author: Andrew Cooper Date: Fri Mar 15 11:31:33 2024 xen/vpci: Improve code generation in mask_write() which I'm confident will end up as a net improvement. --- xen/arch/x86/guest/xen/xen.c | 4 ++-- xen/arch/x86/hvm/dom0_build.c | 2 +- xen/arch/x86/hvm/hpet.c | 8 ++++---- xen/arch/x86/include/asm/pt-contig-markers.h | 2 +- xen/arch/x86/mm.c | 2 +- xen/arch/x86/mm/p2m-pod.c | 4 ++-- xen/common/page_alloc.c | 2 +- xen/common/softirq.c | 2 +- xen/drivers/passthrough/amd/iommu_map.c | 2 +- xen/drivers/passthrough/iommu.c | 4 ++-- xen/drivers/passthrough/x86/iommu.c | 4 ++-- 11 files changed, 18 insertions(+), 18 deletions(-) diff --git a/xen/arch/x86/guest/xen/xen.c b/xen/arch/x86/guest/xen/xen.c index d9768cc9527d..7484b3f73ad3 100644 --- a/xen/arch/x86/guest/xen/xen.c +++ b/xen/arch/x86/guest/xen/xen.c @@ -168,14 +168,14 @@ static void cf_check xen_evtchn_upcall(void) while ( pending ) { - unsigned int l1 = find_first_set_bit(pending); + unsigned int l1 = ffsl(pending) - 1; unsigned long evtchn = xchg(&XEN_shared_info->evtchn_pending[l1], 0); __clear_bit(l1, &pending); evtchn &= ~XEN_shared_info->evtchn_mask[l1]; while ( evtchn ) { - unsigned int port = find_first_set_bit(evtchn); + unsigned int port = ffsl(evtchn) - 1; __clear_bit(port, &evtchn); port += l1 * BITS_PER_LONG; diff --git a/xen/arch/x86/hvm/dom0_build.c b/xen/arch/x86/hvm/dom0_build.c index b0cb96c3bc76..68c08bbe94f7 100644 --- a/xen/arch/x86/hvm/dom0_build.c +++ b/xen/arch/x86/hvm/dom0_build.c @@ -139,7 +139,7 @@ static int __init pvh_populate_memory_range(struct domain *d, order = get_order_from_pages(end - start + 1); order = min(order ? order - 1 : 0, max_order); /* The order allocated and populated must be aligned to the address. */ - order = min(order, start ? find_first_set_bit(start) : MAX_ORDER); + order = min(order, start ? ffsl(start) - 1 : MAX_ORDER); page = alloc_domheap_pages(d, order, dom0_memflags | MEMF_no_scrub); if ( page == NULL ) { diff --git a/xen/arch/x86/hvm/hpet.c b/xen/arch/x86/hvm/hpet.c index 12b00b770257..37e765e97df9 100644 --- a/xen/arch/x86/hvm/hpet.c +++ b/xen/arch/x86/hvm/hpet.c @@ -335,7 +335,7 @@ static void timer_sanitize_int_route(HPETState *h, unsigned int tn) * enabled pick the first irq. */ timer_config(h, tn) |= - MASK_INSR(find_first_set_bit(timer_int_route_cap(h, tn)), + MASK_INSR(ffsl(timer_int_route_cap(h, tn)) - 1, HPET_TN_ROUTE); } @@ -409,7 +409,7 @@ static int cf_check hpet_write( { bool active; - i = find_first_set_bit(new_val); + i = ffsl(new_val) - 1; if ( i >= HPET_TIMER_NUM ) break; __clear_bit(i, &new_val); @@ -535,14 +535,14 @@ static int cf_check hpet_write( /* stop/start timers whos state was changed by this write. */ while (stop_timers) { - i = find_first_set_bit(stop_timers); + i = ffsl(stop_timers) - 1; __clear_bit(i, &stop_timers); hpet_stop_timer(h, i, guest_time); } while (start_timers) { - i = find_first_set_bit(start_timers); + i = ffsl(start_timers) - 1; __clear_bit(i, &start_timers); hpet_set_timer(h, i, guest_time); } diff --git a/xen/arch/x86/include/asm/pt-contig-markers.h b/xen/arch/x86/include/asm/pt-contig-markers.h index b3c1fe803534..e8c8157d605f 100644 --- a/xen/arch/x86/include/asm/pt-contig-markers.h +++ b/xen/arch/x86/include/asm/pt-contig-markers.h @@ -60,7 +60,7 @@ static bool pt_update_contig_markers(uint64_t *pt, unsigned int idx, /* Step 1: Reduce markers in lower numbered entries. */ while ( i ) { - b = find_first_set_bit(i); + b = ffsl(i) - 1; i &= ~(1U << b); if ( GET_MARKER(pt[i]) <= b ) break; diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index d968bbbc7315..2a84bdae670b 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -3424,7 +3424,7 @@ static int vcpumask_to_pcpumask( { unsigned int cpu; - vcpu_id = find_first_set_bit(vmask); + vcpu_id = ffsl(vmask) - 1; vmask &= ~(1UL << vcpu_id); vcpu_id += vcpu_bias; if ( (vcpu_id >= d->max_vcpus) ) diff --git a/xen/arch/x86/mm/p2m-pod.c b/xen/arch/x86/mm/p2m-pod.c index 1c727c78c833..bd84fe9e27ee 100644 --- a/xen/arch/x86/mm/p2m-pod.c +++ b/xen/arch/x86/mm/p2m-pod.c @@ -684,7 +684,7 @@ unsigned long p2m_pod_decrease_reservation(struct domain *d, gfn_t gfn, unsigned int order) { unsigned long left = 1UL << order, ret = 0; - unsigned int chunk_order = find_first_set_bit(gfn_x(gfn) | left); + unsigned int chunk_order = ffsl(gfn_x(gfn) | left) - 1; do { ret += decrease_reservation(d, gfn, chunk_order); @@ -1393,7 +1393,7 @@ guest_physmap_mark_populate_on_demand(struct domain *d, unsigned long gfn, unsigned int order) { unsigned long left = 1UL << order; - unsigned int chunk_order = find_first_set_bit(gfn | left); + unsigned int chunk_order = ffsl(gfn | left) - 1; int rc; if ( !paging_mode_translate(d) ) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 8d3342e95236..054b7edb3989 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -1819,7 +1819,7 @@ static void _init_heap_pages(const struct page_info *pg, if ( unlikely(!avail[nid]) ) { bool use_tail = IS_ALIGNED(s, 1UL << MAX_ORDER) && - (find_first_set_bit(e) <= find_first_set_bit(s)); + (ffsl(e) <= ffsl(s)); unsigned long n; n = init_node_heap(nid, s, nr_pages, &use_tail); diff --git a/xen/common/softirq.c b/xen/common/softirq.c index 321d26902d37..bee4a82009c3 100644 --- a/xen/common/softirq.c +++ b/xen/common/softirq.c @@ -48,7 +48,7 @@ static void __do_softirq(unsigned long ignore_mask) || cpu_is_offline(cpu) ) break; - i = find_first_set_bit(pending); + i = ffsl(pending) - 1; clear_bit(i, &softirq_pending(cpu)); (*softirq_handlers[i])(); } diff --git a/xen/drivers/passthrough/amd/iommu_map.c b/xen/drivers/passthrough/amd/iommu_map.c index e0f4fe736a8d..f1061bfc798c 100644 --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -137,7 +137,7 @@ static void set_iommu_ptes_present(unsigned long pt_mfn, ASSERT(!pde->u); if ( pde > table ) - ASSERT(pde->ign0 == find_first_set_bit(pde - table)); + ASSERT(pde->ign0 == ffsl(pde - table) - 1); else ASSERT(pde->ign0 == CONTIG_LEVEL_SHIFT); diff --git a/xen/drivers/passthrough/iommu.c b/xen/drivers/passthrough/iommu.c index ba18136c461c..50bfd62553ae 100644 --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -305,7 +305,7 @@ static unsigned int mapping_order(const struct domain_iommu *hd, { unsigned long res = dfn_x(dfn) | mfn_x(mfn); unsigned long sizes = hd->platform_ops->page_sizes; - unsigned int bit = find_first_set_bit(sizes), order = 0; + unsigned int bit = ffsl(sizes) - 1, order = 0; ASSERT(bit == PAGE_SHIFT); @@ -313,7 +313,7 @@ static unsigned int mapping_order(const struct domain_iommu *hd, { unsigned long mask; - bit = find_first_set_bit(sizes); + bit = ffsl(sizes) - 1; mask = (1UL << bit) - 1; if ( nr <= mask || (res & mask) ) break; diff --git a/xen/drivers/passthrough/x86/iommu.c b/xen/drivers/passthrough/x86/iommu.c index 304a2f5480c7..cc0062b02712 100644 --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -641,7 +641,7 @@ struct page_info *iommu_alloc_pgtable(struct domain_iommu *hd, if ( contig_mask ) { /* See pt-contig-markers.h for a description of the marker scheme. */ - unsigned int i, shift = find_first_set_bit(contig_mask); + unsigned int i, shift = ffsl(contig_mask) - 1; ASSERT((CONTIG_LEVEL_SHIFT & (contig_mask >> shift)) == CONTIG_LEVEL_SHIFT); @@ -652,7 +652,7 @@ struct page_info *iommu_alloc_pgtable(struct domain_iommu *hd, for ( i = 4; i < PAGE_SIZE / sizeof(*p); i += 4 ) { - p[i + 0] = (find_first_set_bit(i) + 0ULL) << shift; + p[i + 0] = (ffsl(i) - 1ULL) << shift; p[i + 1] = 0; p[i + 2] = 1ULL << shift; p[i + 3] = 0; From patchwork Fri May 24 20:03:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673599 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7340EC25B79 for ; Fri, 24 May 2024 20:04:07 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729754.1135084 (Exim 4.92) (envelope-from ) id 1sAb8v-00019e-DS; Fri, 24 May 2024 20:03:57 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729754.1135084; Fri, 24 May 2024 20:03:57 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8u-000199-Um; Fri, 24 May 2024 20:03:56 +0000 Received: by outflank-mailman (input) for mailman id 729754; Fri, 24 May 2024 20:03:55 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8t-000773-H2 for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:55 +0000 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [2a00:1450:4864:20::635]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bef910bf-1a08-11ef-b4bb-af5377834399; Fri, 24 May 2024 22:03:53 +0200 (CEST) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-a626ac4d299so130919566b.3 for ; Fri, 24 May 2024 13:03:53 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:52 -0700 (PDT) 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: bef910bf-1a08-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581033; x=1717185833; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=maIVB65jYfI2PBFJy7D4AeslonYmTipzI0EBcOkzwDs=; b=sSaqMABUIPyLRyf6r687lQ1thueAYWaRJs9DlJbq1+yOmHENekFQ5FZxIjxyrcfHNx KALPRB72LhM1GdiCsX6lpwPPn34KK8kfSogSWTy0sVhA5Ah1KC036viIFktdANOwxTvZ rZ6kbdfncTnc3a4aG8YxUuLnB4r0gCDQR4NLQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581033; x=1717185833; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=maIVB65jYfI2PBFJy7D4AeslonYmTipzI0EBcOkzwDs=; b=JdUk45ey20tgqN1zew/P3VN/7TswOr+gKjxheoc5R10b/XI1Qyn05qF3vs8kSf1/7y jqweRteMzvxiaKoyoMJWfgK7neDZnOTzezNFKzIhw+KdqOZoLRpESnjjz0FXr94+40Vh R2RUptBq2dQfa/7rTgIDagcHJfV24ei/bgwU9Ui+32m0DoCxW1PwoSRMD9B0oKsnoCka wEz+b3c3CvdgSmijIghoOLqPUUFvG6Wtt6BAKUaECU6aUzs2uitbwo9OxK6VIuzDJdYO 1eR73iuqXU3KNE5iA1DYqyVhUAGuYbb5YCxQ3W0ErrsWTBzA8upIVq9VlyTJUA/EvweM J0Jw== X-Gm-Message-State: AOJu0Yx0CtKp+Ohfix2xZqWEaJDjAWk2ruhfkPAp33QdSBuZ3vianH1L ASKNj955FW7FgF/RIy6SnIYSCOP4XGxlj+uHKOXpmvQFwOnSvaRqSw0eMhf0CxG83/ghGMvDOlu v+yU= X-Google-Smtp-Source: AGHT+IHQSVGsB2hFnOlOMuOQReZaremOY3IsH9dioCeOBypWLpouROaY+1nCR6o1hiTm3xLeTealKA== X-Received: by 2002:a17:906:c44c:b0:a5a:3a6c:8b56 with SMTP id a640c23a62f3a-a6264179f89mr213603866b.11.1716581033309; Fri, 24 May 2024 13:03:53 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 10/13] xen/bitops: Delete find_first_set_bit() Date: Fri, 24 May 2024 21:03:35 +0100 Message-Id: <20240524200338.1232391-11-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 No more users. Signed-off-by: Andrew Cooper Acked-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * Reorder from later in the series to keep ARM bisectable --- xen/arch/arm/include/asm/bitops.h | 9 --------- xen/arch/ppc/include/asm/bitops.h | 9 --------- xen/arch/x86/include/asm/bitops.h | 12 ------------ 3 files changed, 30 deletions(-) diff --git a/xen/arch/arm/include/asm/bitops.h b/xen/arch/arm/include/asm/bitops.h index ba39802c9de3..d30ba44598e3 100644 --- a/xen/arch/arm/include/asm/bitops.h +++ b/xen/arch/arm/include/asm/bitops.h @@ -160,15 +160,6 @@ static inline int fls(unsigned int x) #define arch_ffs(x) ((x) ? 1 + __builtin_ctz(x) : 0) #define arch_ffsl(x) ((x) ? 1 + __builtin_ctzl(x) : 0) -/** - * find_first_set_bit - find the first set bit in @word - * @word: the word to search - * - * Returns the bit-number of the first set bit (first bit being 0). - * The input must *not* be zero. - */ -#define find_first_set_bit(w) (ffsl(w) - 1) - /** * hweightN - returns the hamming weight of a N-bit word * @x: the word to weigh diff --git a/xen/arch/ppc/include/asm/bitops.h b/xen/arch/ppc/include/asm/bitops.h index ce0f6436f727..761361291e6f 100644 --- a/xen/arch/ppc/include/asm/bitops.h +++ b/xen/arch/ppc/include/asm/bitops.h @@ -187,13 +187,4 @@ static inline int __test_and_clear_bit(int nr, volatile void *addr) #define hweight16(x) __builtin_popcount((uint16_t)(x)) #define hweight8(x) __builtin_popcount((uint8_t)(x)) -/** - * find_first_set_bit - find the first set bit in @word - * @word: the word to search - * - * Returns the bit-number of the first set bit (first bit being 0). - * The input must *not* be zero. - */ -#define find_first_set_bit(x) (ffsl(x) - 1) - #endif /* _ASM_PPC_BITOPS_H */ diff --git a/xen/arch/x86/include/asm/bitops.h b/xen/arch/x86/include/asm/bitops.h index 51d3c0f40473..830e488f33a0 100644 --- a/xen/arch/x86/include/asm/bitops.h +++ b/xen/arch/x86/include/asm/bitops.h @@ -401,18 +401,6 @@ static always_inline unsigned int __scanbit(unsigned long val, unsigned int max) r__; \ }) -/** - * find_first_set_bit - find the first set bit in @word - * @word: the word to search - * - * Returns the bit-number of the first set bit. The input must *not* be zero. - */ -static inline unsigned int find_first_set_bit(unsigned long word) -{ - asm ( "rep; bsf %1,%0" : "=r" (word) : "rm" (word) ); - return (unsigned int)word; -} - static always_inline unsigned int arch_ffs(unsigned int x) { unsigned int r; From patchwork Fri May 24 20:03:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673602 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ACA73C25B7F for ; Fri, 24 May 2024 20:04:08 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729755.1135094 (Exim 4.92) (envelope-from ) id 1sAb8x-0001ZF-21; Fri, 24 May 2024 20:03:59 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729755.1135094; Fri, 24 May 2024 20:03:59 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8w-0001XB-Qw; Fri, 24 May 2024 20:03:58 +0000 Received: by outflank-mailman (input) for mailman id 729755; Fri, 24 May 2024 20:03:57 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8u-000773-TT for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:56 +0000 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [2a00:1450:4864:20::52b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bf9fe58b-1a08-11ef-b4bb-af5377834399; Fri, 24 May 2024 22:03:55 +0200 (CEST) Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-57857e0f464so1184464a12.0 for ; Fri, 24 May 2024 13:03:55 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:53 -0700 (PDT) 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: bf9fe58b-1a08-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581034; x=1717185834; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BArFVTq1bFwBqGRKgiR15fKQGJoXMQ04ywKksSWcrsw=; b=nce6VDKq+A7CD02EOSHb4AOw7H264K5pwaW25ZnM3aasO4mUoSAUz8Xg99jXVkyUxp WdkxEvouGhx14pxxNTTC8t04OUh1F9nXBwY8fJCNs7o2bKnqcxYs6sr2/HBQtR5pX+YN ZJNCMx6+UW+5+I0MAbZhtfSg3fgtSUGd8oApc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581034; x=1717185834; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BArFVTq1bFwBqGRKgiR15fKQGJoXMQ04ywKksSWcrsw=; b=UvIwFnqnUxtHAvnJomBst7ofJMhQJkwzq4OVPaHkqVXi0NHczF7JGLoeWvIVOKEXBF 3PP1+JzwVDkmuI/VZAKbnmX8OCcLkhy7TYkV4NMjjhOQyIqNgdIv7oXlzDTD/05vfSjD y3fAgiz9usW++eb0Yw8GdjFXP4gB/PxQPfvleqQMFIJiftoBDu6tnMSTDj+DBM8IvmkO cCk5X5WPLq0UOTedsCtj++g4lxpVOHgXJo6FEQaEJ9VnpE1IHUmyG5UxJhyvPjSwEJTl u1Qp0ZBj9HP6uhITwRCvba08Lxw2ms5KprDFcflhSE5XhRqaG+Shny8uNm92CzqHF+sJ BwoQ== X-Gm-Message-State: AOJu0YwtlVGiqB/Nei71T2EuusE7moY+qYMnJ/n7ZjlR5C6VP69w2BOU J9rsT/t39zGJVVe4x/RuPjPHEgdS+WdV2fGju8o7zCx4n4JJGtO01BAzNNY/3ZqSoD9/9X24V8r /0j4= X-Google-Smtp-Source: AGHT+IFS0sRKjwwnSdc3AQTMI7Dh0SyIHhDvuOE50/zkVG/FaN3PWKT6+IRAPUcfHZtNjlNVcg8Ydg== X-Received: by 2002:a17:907:6d07:b0:a58:c80e:edd9 with SMTP id a640c23a62f3a-a626525da3bmr218463666b.77.1716581034101; Fri, 24 May 2024 13:03:54 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Oleksii Kurochko , Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 11/13] xen/bitops: Implement fls()/flsl() in common logic Date: Fri, 24 May 2024 21:03:36 +0100 Message-Id: <20240524200338.1232391-12-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 From: Oleksii Kurochko This is most easily done together because of how arm32 is currently structured, but it does just mirror the existing ffs()/ffsl() work. Introduce compile and boot time testing. Signed-off-by: Oleksii Kurochko Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * New, incorperated from Oleksii's RISC-V series and adjusted. for x86: add/remove: 0/0 grow/shrink: 3/17 up/down: 28/-153 (-125) Function old new delta pci_enable_msi 1033 1049 +16 vlapic_lowest_prio 330 338 +8 kexec_early_calculations 53 57 +4 pci_restore_msi_state 1159 1157 -2 arch_hwdom_irqs 61 59 -2 control_read 132 129 -3 pci_enable_msi.cold 121 117 -4 arch_get_dma_bitsize 173 169 -4 xmem_pool_alloc 1039 1032 -7 xenheap_max_mfn 49 42 -7 mba_sanitize_thrtl 83 76 -7 xstate_init 807 799 -8 offline_page 965 957 -8 apicid_to_socket 160 152 -8 vlapic_find_highest_vector 61 48 -13 xmem_pool_free 983 967 -16 iommu_alloc 935 919 -16 free_heap_pages 1512 1496 -16 detect_ht 318 302 -16 alloc_heap_pages 1569 1553 -16 showing that the optimiser can now do a better job in most cases. --- xen/arch/arm/include/asm/arm32/bitops.h | 2 -- xen/arch/arm/include/asm/arm64/bitops.h | 12 ------- xen/arch/arm/include/asm/bitops.h | 19 ++-------- xen/arch/ppc/include/asm/bitops.h | 4 +-- xen/arch/x86/include/asm/bitops.h | 46 +++++++++++++++---------- xen/common/bitops.c | 25 ++++++++++++++ xen/include/xen/bitops.h | 24 +++++++++++++ 7 files changed, 80 insertions(+), 52 deletions(-) diff --git a/xen/arch/arm/include/asm/arm32/bitops.h b/xen/arch/arm/include/asm/arm32/bitops.h index d0309d47c188..0d7bb12d5c19 100644 --- a/xen/arch/arm/include/asm/arm32/bitops.h +++ b/xen/arch/arm/include/asm/arm32/bitops.h @@ -1,8 +1,6 @@ #ifndef _ARM_ARM32_BITOPS_H #define _ARM_ARM32_BITOPS_H -#define flsl fls - /* * Little endian assembly bitops. nr = 0 -> byte 0 bit 0. */ diff --git a/xen/arch/arm/include/asm/arm64/bitops.h b/xen/arch/arm/include/asm/arm64/bitops.h index 906d84e5f295..a6135838dcfa 100644 --- a/xen/arch/arm/include/asm/arm64/bitops.h +++ b/xen/arch/arm/include/asm/arm64/bitops.h @@ -1,18 +1,6 @@ #ifndef _ARM_ARM64_BITOPS_H #define _ARM_ARM64_BITOPS_H -static inline int flsl(unsigned long x) -{ - uint64_t ret; - - if (__builtin_constant_p(x)) - return generic_flsl(x); - - asm("clz\t%0, %1" : "=r" (ret) : "r" (x)); - - return BITS_PER_LONG - ret; -} - /* Based on linux/include/asm-generic/bitops/find.h */ #ifndef CONFIG_GENERIC_FIND_FIRST_BIT diff --git a/xen/arch/arm/include/asm/bitops.h b/xen/arch/arm/include/asm/bitops.h index d30ba44598e3..8f4bdc09d128 100644 --- a/xen/arch/arm/include/asm/bitops.h +++ b/xen/arch/arm/include/asm/bitops.h @@ -140,25 +140,10 @@ static inline int test_bit(int nr, const volatile void *addr) return 1UL & (p[BITOP_WORD(nr)] >> (nr & (BITOP_BITS_PER_WORD-1))); } -/* - * On ARMv5 and above those functions can be implemented around - * the clz instruction for much better code efficiency. - */ - -static inline int fls(unsigned int x) -{ - int ret; - - if (__builtin_constant_p(x)) - return generic_flsl(x); - - asm("clz\t%"__OP32"0, %"__OP32"1" : "=r" (ret) : "r" (x)); - return 32 - ret; -} - - #define arch_ffs(x) ((x) ? 1 + __builtin_ctz(x) : 0) #define arch_ffsl(x) ((x) ? 1 + __builtin_ctzl(x) : 0) +#define arch_fls(x) ((x) ? 32 - __builtin_clz(x) : 0) +#define arch_flsl(x) ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0) /** * hweightN - returns the hamming weight of a N-bit word diff --git a/xen/arch/ppc/include/asm/bitops.h b/xen/arch/ppc/include/asm/bitops.h index 761361291e6f..8119b5ace877 100644 --- a/xen/arch/ppc/include/asm/bitops.h +++ b/xen/arch/ppc/include/asm/bitops.h @@ -171,10 +171,10 @@ static inline int __test_and_clear_bit(int nr, volatile void *addr) return (old & mask) != 0; } -#define flsl(x) generic_flsl(x) -#define fls(x) generic_flsl(x) #define arch_ffs(x) ((x) ? 1 + __builtin_ctz(x) : 0) #define arch_ffsl(x) ((x) ? 1 + __builtin_ctzl(x) : 0) +#define arch_fls(x) ((x) ? 32 - __builtin_clz(x) : 0) +#define arch_flsl(x) ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0) /** * hweightN - returns the hamming weight of a N-bit word diff --git a/xen/arch/x86/include/asm/bitops.h b/xen/arch/x86/include/asm/bitops.h index 830e488f33a0..fc9fe73ad5ba 100644 --- a/xen/arch/x86/include/asm/bitops.h +++ b/xen/arch/x86/include/asm/bitops.h @@ -447,33 +447,41 @@ static always_inline unsigned int arch_ffsl(unsigned long x) } #define arch_ffsl arch_ffsl -/** - * fls - find last bit set - * @x: the word to search - * - * This is defined the same way as ffs. - */ -static inline int flsl(unsigned long x) +static always_inline unsigned int arch_fls(unsigned int x) { - long r; + unsigned int r; + + /* See arch_ffs() for safety discussions. */ + if ( __builtin_constant_p(x > 0) && x > 0 ) + asm ( "bsr %[val], %[res]" + : [res] "=r" (r) + : [val] "rm" (x) ); + else + asm ( "bsr %[val], %[res]" + : [res] "=r" (r) + : [val] "rm" (x), "[res]" (-1) ); - asm ( "bsr %1,%0\n\t" - "jnz 1f\n\t" - "mov $-1,%0\n" - "1:" : "=r" (r) : "rm" (x)); - return (int)r+1; + return r + 1; } +#define arch_fls arch_fls -static inline int fls(unsigned int x) +static always_inline unsigned int arch_flsl(unsigned long x) { - int r; + unsigned int r; + + /* See arch_ffs() for safety discussions. */ + if ( __builtin_constant_p(x > 0) && x > 0 ) + asm ( "bsr %[val], %q[res]" + : [res] "=r" (r) + : [val] "rm" (x) ); + else + asm ( "bsr %[val], %q[res]" + : [res] "=r" (r) + : [val] "rm" (x), "[res]" (-1) ); - asm ( "bsr %1,%0\n\t" - "jnz 1f\n\t" - "mov $-1,%0\n" - "1:" : "=r" (r) : "rm" (x)); return r + 1; } +#define arch_flsl arch_flsl /** * hweightN - returns the hamming weight of a N-bit word diff --git a/xen/common/bitops.c b/xen/common/bitops.c index b3813f818198..b4845d9e84d1 100644 --- a/xen/common/bitops.c +++ b/xen/common/bitops.c @@ -26,7 +26,32 @@ static void __init test_ffs(void) #endif } +static void __init test_fls(void) +{ + /* unsigned int fls(unsigned int) */ + CHECK(fls, 0, 0); + CHECK(fls, 1, 1); + CHECK(fls, 3, 2); + CHECK(fls, 7, 3); + CHECK(fls, 6, 3); + CHECK(fls, 0x80000000U, 32); + + /* unsigned int flsl(unsigned long) */ + CHECK(flsl, 0, 0); + CHECK(flsl, 1, 1); + CHECK(flsl, 3, 2); + CHECK(flsl, 7, 3); + CHECK(flsl, 6, 3); + + CHECK(flsl, 1 | (1UL << (BITS_PER_LONG - 1)), BITS_PER_LONG); +#if BITS_PER_LONG > 32 + CHECK(flsl, 1 | (1UL << 32), 33); + CHECK(flsl, 1 | (1UL << 63), 64); +#endif +} + static void __init __constructor test_bitops(void) { test_ffs(); + test_fls(); } diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index 88cf27a88bcf..e7df6377372d 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -60,6 +60,30 @@ static always_inline __pure unsigned int ffsl(unsigned long x) #endif } +static always_inline __pure unsigned int fls(unsigned int x) +{ + if ( __builtin_constant_p(x) ) + return x ? 32 - __builtin_clz(x) : 0; + +#ifdef arch_fls + return arch_fls(x); +#else + return generic_flsl(x); +#endif +} + +static always_inline __pure unsigned int flsl(unsigned long x) +{ + if ( __builtin_constant_p(x) ) + return x ? BITS_PER_LONG - __builtin_clzl(x) : 0; + +#ifdef arch_fls + return arch_flsl(x); +#else + return generic_flsl(x); +#endif +} + /* --------------------- Please tidy below here --------------------- */ #ifndef find_next_bit From patchwork Fri May 24 20:03:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673600 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 94455C25B74 for ; Fri, 24 May 2024 20:04:08 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729756.1135101 (Exim 4.92) (envelope-from ) id 1sAb8x-0001f0-RS; Fri, 24 May 2024 20:03:59 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729756.1135101; Fri, 24 May 2024 20:03:59 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8x-0001e3-AW; Fri, 24 May 2024 20:03:59 +0000 Received: by outflank-mailman (input) for mailman id 729756; Fri, 24 May 2024 20:03:57 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8v-000773-Eb for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:57 +0000 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id c01d67a2-1a08-11ef-b4bb-af5377834399; Fri, 24 May 2024 22:03:55 +0200 (CEST) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a59a609dd3fso649410766b.0 for ; Fri, 24 May 2024 13:03:55 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:54 -0700 (PDT) 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: c01d67a2-1a08-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581035; x=1717185835; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0OUrbEItmp/7smeXVfEuek5P5UjrbBJNpLNlS999tow=; b=u+aF8XMjIi9Fnwe7rnYtGFtvSyasc/4Qvlnxsuz4Ubw+8Y4TfXwt7RvTNBKwerXNB+ zVlnRRtoGYJdivfzyYAg/kzQFenKpxMMOv7VL+gdZt5bNPiUZqf2+5YS+xgt2B4hAtjQ Q72uhwQ7EJxf1QX1BfUhhrZMf9UdnIAXsAfyE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581035; x=1717185835; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0OUrbEItmp/7smeXVfEuek5P5UjrbBJNpLNlS999tow=; b=c9sRBk9pReIdHVEmlzB3wAluzBEeWXksyATgeV0zxOFN2CjranK1Z6cHQ1yqxEnFt2 equzeV+cmwB8REz0HOWA0rPFUMlA2NyFSYuN/39JzLw0mFKm+MsIBur47tamaz5lBvIU rUwOrlBX/i+wI7mg5lDQFtW0V0MpJpEhRharIsRKQ7e4N/kgnARiZqS9GS0itTUosSU2 w9OYP3B9p+6WVe+iXUuZrZ7TEMn/VtC578GGghKmO7W/LObHedwt7Pzo52EnOmLPwrht 62SpIs+NkZTEJP4Zik3YNhemaxt05cLPhjJbrLm/YvF39cauSLTZc2zWj2sKadL4XrNg 8qvQ== X-Gm-Message-State: AOJu0YyXLGoJ9to+O3CBg5HLoh6A5HvBbKylEsTspt9skiYHqmY5z2Du dr2GxnIQW0HBFsz9ZDWY9jxR7j8x6x/qTqMLWXFEYtjA2dPyu/k6PYzUbOSwbix4zVvsdzP7rj+ 4HA8= X-Google-Smtp-Source: AGHT+IFDepIWI/VDxApQZRZYemyfm2VE+hQJWb/SbSRzW6+QTax//5EAno8CL93TfAhRjnW5+HYXQQ== X-Received: by 2002:a17:906:a04e:b0:a62:44e1:3f56 with SMTP id a640c23a62f3a-a6244e14068mr444618466b.37.1716581035180; Fri, 24 May 2024 13:03:55 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 12/13] xen/bitops: Clean up ffs64()/fls64() definitions Date: Fri, 24 May 2024 21:03:37 +0100 Message-Id: <20240524200338.1232391-13-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Implement ffs64() and fls64() as plain static inlines, dropping the ifdefary and intermediate generic_f?s64() forms. Add tests for all interesting bit positions at 32bit boundaries. No functional change. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * Use ULL rather than a uint64_t cast. * Extend to fls64() too. --- xen/common/bitops.c | 32 ++++++++++++++++++++++++++++++ xen/include/xen/bitops.h | 42 +++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/xen/common/bitops.c b/xen/common/bitops.c index b4845d9e84d1..5482e5a1218d 100644 --- a/xen/common/bitops.c +++ b/xen/common/bitops.c @@ -24,6 +24,22 @@ static void __init test_ffs(void) CHECK(ffsl, 1UL << 32, 33); CHECK(ffsl, 1UL << 63, 64); #endif + + /* + * unsigned int ffs64(uint64_t) + * + * 32-bit builds of Xen have to split this into two adjacent operations, + * so test all interesting bit positions across the divide. + */ + CHECK(ffs64, 0, 0); + CHECK(ffs64, 1, 1); + CHECK(ffs64, 3, 1); + CHECK(ffs64, 7, 1); + CHECK(ffs64, 6, 2); + + CHECK(ffs64, 0x8000000080000000ULL, 32); + CHECK(ffs64, 0x8000000100000000ULL, 33); + CHECK(ffs64, 0x8000000000000000ULL, 64); } static void __init test_fls(void) @@ -48,6 +64,22 @@ static void __init test_fls(void) CHECK(flsl, 1 | (1UL << 32), 33); CHECK(flsl, 1 | (1UL << 63), 64); #endif + + /* + * unsigned int ffl64(uint64_t) + * + * 32-bit builds of Xen have to split this into two adjacent operations, + * so test all interesting bit positions across the divide. + */ + CHECK(fls64, 0, 0); + CHECK(fls64, 1, 1); + CHECK(fls64, 3, 2); + CHECK(fls64, 7, 3); + CHECK(fls64, 6, 3); + + CHECK(fls64, 0x0000000080000001ULL, 32); + CHECK(fls64, 0x0000000100000001ULL, 33); + CHECK(fls64, 0x8000000000000001ULL, 64); } static void __init __constructor test_bitops(void) diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index e7df6377372d..c5518d2c8552 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -60,6 +60,14 @@ static always_inline __pure unsigned int ffsl(unsigned long x) #endif } +static always_inline __pure unsigned int ffs64(uint64_t x) +{ + if ( BITS_PER_LONG == 64 ) + return ffsl(x); + else + return !x || (uint32_t)x ? ffs(x) : ffs(x >> 32) + 32; +} + static always_inline __pure unsigned int fls(unsigned int x) { if ( __builtin_constant_p(x) ) @@ -84,6 +92,18 @@ static always_inline __pure unsigned int flsl(unsigned long x) #endif } +static always_inline __pure unsigned int fls64(uint64_t x) +{ + if ( BITS_PER_LONG == 64 ) + return flsl(x); + else + { + uint32_t h = x >> 32; + + return h ? fls(h) + 32 : fls(x); + } +} + /* --------------------- Please tidy below here --------------------- */ #ifndef find_next_bit @@ -134,28 +154,6 @@ extern unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size); #endif -#if BITS_PER_LONG == 64 -# define fls64 flsl -# define ffs64 ffsl -#else -# ifndef ffs64 -static inline int generic_ffs64(__u64 x) -{ - return !x || (__u32)x ? ffs(x) : ffs(x >> 32) + 32; -} -# define ffs64 generic_ffs64 -# endif -# ifndef fls64 -static inline int generic_fls64(__u64 x) -{ - __u32 h = x >> 32; - - return h ? fls(h) + 32 : fls(x); -} -# define fls64 generic_fls64 -# endif -#endif - static inline int get_bitmask_order(unsigned int count) { int order; From patchwork Fri May 24 20:03:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13673601 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9E12DC25B7D for ; Fri, 24 May 2024 20:04:08 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.729757.1135107 (Exim 4.92) (envelope-from ) id 1sAb8y-0001q4-Tw; Fri, 24 May 2024 20:04:00 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 729757.1135107; Fri, 24 May 2024 20:04:00 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8y-0001nD-6u; Fri, 24 May 2024 20:04:00 +0000 Received: by outflank-mailman (input) for mailman id 729757; Fri, 24 May 2024 20:03:58 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sAb8w-000773-8d for xen-devel@lists.xenproject.org; Fri, 24 May 2024 20:03:58 +0000 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [2a00:1450:4864:20::634]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id c09c0368-1a08-11ef-b4bb-af5377834399; Fri, 24 May 2024 22:03:56 +0200 (CEST) Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-a6265d3ba8fso122388366b.0 for ; Fri, 24 May 2024 13:03:56 -0700 (PDT) Received: from andrewcoop.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626c9377d8sm173553066b.55.2024.05.24.13.03.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:03:55 -0700 (PDT) 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: c09c0368-1a08-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1716581036; x=1717185836; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GlC1OJvyxmbXQCbGIy6Vwf8C5QyN1N/4JG+CgGnlpRU=; b=W4Vn8jeMcf3e3Cf/cA2EbswwJAAdH3KK18eI/yxPtwDuKoqzUGRjtN3B0aseVZk9rK d1wPt2Z+onl0uBjpQB304H5v59sdyYfF0at5/rWFQP1U+rVMPPu0d5D/uRjhSthuwrZg M68n9HH+In3Qcx3hW6+KkVaycHp0jIQvGN+pk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581036; x=1717185836; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GlC1OJvyxmbXQCbGIy6Vwf8C5QyN1N/4JG+CgGnlpRU=; b=fyPa+BHgkBftErxLTBSL8tlwlmE4HcMJhKUFWKI1CJmZfcxzdWRVevv/ozlhbTM/a1 ePxJxG5CkYFWc+vMbhx+XXC33cqJlOxB1TJa3SvQvBuZ6SJX3CMyTh/adHDvJ9Q9Dmi1 Nkw8+Aadyg+i98tn+JqGwwQxxOerVfZ3jbAflMJMM0n5wbe7DzsTW4kw3vjKttPjWbUA ktCf+We0OSvrRYg9TiDF1BxrXmR/B1JEJ9pU7+CEkT1Ec2kqd1UMf9WzxkiKIQNyxWOo /K+KM+91nGhHCMbN10VthXq9qE5qCMlYqEXARS2GcH+o+JRvSJfBhL0cF+/RT4iJDHfz NDLQ== X-Gm-Message-State: AOJu0Yx5PvClYnxw99THLKLfQOJEB7vR/sByZ86HSnUXcsrvjfHq87SP aRQTSz0+pLiwfBRYpNHpISsPZy6p6A55k77beqfU7ga2v6bO3OWevvdTN3pgn7te7cyrPNic78A aVWE= X-Google-Smtp-Source: AGHT+IF8O0rWqo0YlTUhztmqD2tpEsu9ss1HcoDAnauTBrFqCne9vXBOwm/Ri806Jb93zik3jeTNJw== X-Received: by 2002:a17:906:4ac1:b0:a5a:8bc4:f503 with SMTP id a640c23a62f3a-a62641dfe3fmr206635666b.25.1716581036021; Fri, 24 May 2024 13:03:56 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Bertrand Marquis , Michal Orzel , Oleksii Kurochko , Shawn Anastasio , "consulting @ bugseng . com" , Simone Ballarin , Federico Serafini , Nicola Vetrini Subject: [PATCH v2 13/13] xen/bitops: Rearrange the top of xen/bitops.h Date: Fri, 24 May 2024 21:03:38 +0100 Message-Id: <20240524200338.1232391-14-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240524200338.1232391-1-andrew.cooper3@citrix.com> References: <20240524200338.1232391-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 The #include can move to the top of the file now now that generic_f?s() have been untangled. Signed-off-by: Andrew Cooper Acked-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monné CC: Wei Liu CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Bertrand Marquis CC: Michal Orzel CC: Oleksii Kurochko CC: Shawn Anastasio CC: consulting@bugseng.com CC: Simone Ballarin CC: Federico Serafini CC: Nicola Vetrini v2: * New --- xen/include/xen/bitops.h | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index c5518d2c8552..6a5e28730a25 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -4,6 +4,8 @@ #include #include +#include + /* * Create a contiguous bitmask starting at bit position @l and ending at * position @h. For example GENMASK(30, 21) gives us 0x7fe00000ul. @@ -15,27 +17,13 @@ (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LLONG - 1 - (h)))) /* - * Find First/Last Set bit. + * Find First/Last Set bit (all forms). * * Bits are labelled from 1. Returns 0 if given 0. */ unsigned int __pure generic_ffsl(unsigned long x); unsigned int __pure generic_flsl(unsigned long x); -/* - * Include this here because some architectures need generic_ffs/fls in - * scope - */ - -/* --------------------- Please tidy above here --------------------- */ - -#include - -/* - * Find First/Last Set bit (all forms). - * - * Bits are labelled from 1. Returns 0 if given 0. - */ static always_inline __pure unsigned int ffs(unsigned int x) { if ( __builtin_constant_p(x) )