Received: by 2002:ab2:2441:0:b0:1f3:1f8c:d0c6 with SMTP id k1csp158744lqe; Thu, 4 Apr 2024 02:52:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVehJlAymHzyiCIShBO2GW5O5ZciqPBh1fzOAsFRK9plSJEHYEuHlymctPqukSlHKEYehO/U4i1UbGwZSA/eKAG5iqOC7LnQQuFuGTWjw== X-Google-Smtp-Source: AGHT+IG9Nl5NQevK4RpPSVIywYEFy2lO7bl0vVFvxIMJx5l9M4OVapPLHViWUpFyHmyMJ1dRmrSr X-Received: by 2002:a05:6808:209d:b0:3c4:f5c2:6c11 with SMTP id s29-20020a056808209d00b003c4f5c26c11mr1857736oiw.56.1712224375820; Thu, 04 Apr 2024 02:52:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712224375; cv=pass; d=google.com; s=arc-20160816; b=uRkFMK5g5WEMKhHy5SiiiSIEPSxhsyQYETvOq2onA7BtTVGmv9MUC3Hh6pMffW7vaf Evqzsml5FUQGVZWwV/i7KkNzCAQ6pL38Yp60lCjjZ7Ky8uRRBFjiZZJGWZU16Z92fIeV Bv+rDvSufQ5bcFodt8E6bpgVwsQ7vNt4pU2/6V4zKZICxNULdn/g9fCP0vUDmLA42iC/ Onv2+tSGRGCFDV/GTYrLVRjmcFuXQ3eYE8XBF9n7QKp2VPnWueNRykttId9eK9XU+Kzv JEWGkSdNPicX9mFAMsv1xnnb6d1rZ6aCBzbUJSM6qPSrUgaEoC3N1r/tcdYUVGyZw0b+ biZA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=QoqlN3nosT+/1c7Je8c7SBGZJC3Xjll6GHoyVL0bqyA=; fh=OKsBDAkKoLS5kQ14wxZ246LXA4UQoc4rpSladBm+CN8=; b=ZKqUpAfVOAd9Cz5hiuRJA7lig7r2KeSGcT702SdxoO/rkiieb7O3As005Z1XvcZpuL 01fUYDAaXYYj92qM8YGFB6dgCNUNPFtcSzZXAprQgT/HgzvNETki3L4IObJ14diO483H b0iUDQH2YU9yYBC8K6rlf8vC5Am35yi7W4FkRfOy6TxwIdGiqvtmIxWRS/x3q/EcHCi3 NTPEXmZrmG7DSCX8pwnIKk5Tcwuo+GzemI37NuUMfKipLrJYyJVcqyyU4flA4Yc3WnKB /H93fz0CrDWamQnCXl2czNEtrRI+6xUtuUQXC+S3iWEdd5bB3R4xquJoUfvtp8rldaub MhIw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=aL+qng30; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-131199-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131199-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s11-20020a056a00194b00b006ea8b233549si15305248pfk.328.2024.04.04.02.52.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 02:52:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-131199-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=aL+qng30; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-131199-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131199-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id C4D0CB25316 for ; Thu, 4 Apr 2024 09:42:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 33ED2757F5; Thu, 4 Apr 2024 09:42:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aL+qng30" Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 710D45D734 for ; Thu, 4 Apr 2024 09:42:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712223747; cv=none; b=LPJHW0zfCq/pPDzP7gbVp+jq7zWhEx9ufrhPqdWHdaUNkeJF1NfA+26m6b97oitosNj8BZ4llxH4NX7Uz4/9ktvzU5Qy7wD3gtZRStFEiesGII1FFO7pWPgHVSc55iOvikMFPaSbhtZiJ4Te+QjXrsN60SXd3rEqTOIr2xDfHFc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712223747; c=relaxed/simple; bh=My06F/QUqqtWT73Ads4As4Qj3pMI0Oj1iaKsOVtCsWM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jbiLv2hEDaKo36KbipnjX9fGrHEHrtmNXgVlextAGevcyQ7KE1F9LbAUJaAbRSBFnx+CSKpfl7fbCzZdHsgtsjmsf16HajLntzJM3dpwTaBx3/B9paZbF/zg+yQvB2PeoIoFQ8Lx2xhWXSAMc4sNk+9HOnNn+u0UPTM2YI8lI+Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aL+qng30; arc=none smtp.client-ip=209.85.218.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a4644bde1d4so107586466b.3 for ; Thu, 04 Apr 2024 02:42:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712223744; x=1712828544; darn=vger.kernel.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=QoqlN3nosT+/1c7Je8c7SBGZJC3Xjll6GHoyVL0bqyA=; b=aL+qng30405JYv1CWMY+WSAGaDfSs4xoT0cxgXPVdqzcigeBPlwq6XUpfeui9QTPep CgKGqTf0WcVvdBq3gUctpYCmmlfABTvSZG/Et8UnmO8Ca0qRDTbr25hQBKv7fjbp26L8 dnxDiAmvEF/JM91hhxMWDvtjZgbzL7ZhfQpmqeercGHY8GUPlzwe5mebjlga5pzeYldH LVZQUZQlyj02pOfSE4XkEsPuSYnNQ5DC88HTBXeG8al/vpSuHBJlqXv7MWfP8hm+EpgW uJEGzyar+OEuXHkhKb55LXJEJ0PiXDXkyQwpwAd/Th3JLVyACVoqCzM2kIzA0MRvwaCm ItFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712223744; x=1712828544; 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=QoqlN3nosT+/1c7Je8c7SBGZJC3Xjll6GHoyVL0bqyA=; b=VCBqrmwIZGVH8DZ10hrjIK2362f4kPJYOi2HkK2rf9iIpySuzDXYPv+gHEx3qJx96d Gnbgb11/WzaRsUXiZr7vRlM9f5TzrcZeJ8GeTMroQfsTTRWpPlv7X9xQNm1NNAlh4LNz d/E01+TvRm3flUi5ww6VJKS98G95faxpVQZtNmUI9tg50LQ1y4t/KtdWVfUohM9WCj7i LaSLfONP6etHtJZbbNPKXSK/ylngJeK8ckBmF2WCY4gBpIsXoltg2KsggiYt27OHsFHi 3IjDN3zFh9QsGYxab8cboIgDLuQSJgrPIdgcZnm0QWpRbdwCDje0e4piWIh466ncqYmM RTzA== X-Forwarded-Encrypted: i=1; AJvYcCX+VQEeMNZYKYI774T70oXdtuUFvD7+Q1CwNykK4ovkbbORR1N5zixTWKB3m13pSbQHow58CHUSBHb+0ZmEOYe/D0y92Qt4slPZ120r X-Gm-Message-State: AOJu0Yw8JI3c3JAlzGKtQ+A4FDzYdHVOwGLWgQUhGVuQBJIgq1vEq5l8 qlnaoYKueTUj1XAg9mjj0v3EWln+G17hw21XDpxR6doidhGzzKzs X-Received: by 2002:a17:906:a386:b0:a51:7999:3d11 with SMTP id k6-20020a170906a38600b00a5179993d11mr1235940ejz.15.1712223743778; Thu, 04 Apr 2024 02:42:23 -0700 (PDT) Received: from fedora.iskraemeco.si ([193.77.86.250]) by smtp.gmail.com with ESMTPSA id l1-20020a1709061c4100b00a5194b5b932sm62362ejg.16.2024.04.04.02.42.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 02:42:22 -0700 (PDT) From: Uros Bizjak To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Ingo Molnar , Andy Lutomirski , Brian Gerst , Denys Vlasenko , "H . Peter Anvin" , Linus Torvalds , Peter Zijlstra , Thomas Gleixner , Josh Poimboeuf Subject: [PATCH v2 2/3] x86/percpu: Rewrite x86_this_cpu_test_bit() and friends as macros Date: Thu, 4 Apr 2024 11:42:02 +0200 Message-ID: <20240404094218.448963-2-ubizjak@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240404094218.448963-1-ubizjak@gmail.com> References: <20240404094218.448963-1-ubizjak@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Rewrite the whole family of x86_this_cpu_test_bit() functions as macros, so standard __my_cpu_var() and raw_cpu_read() macros can be used on percpu variables. This approach considerably simplifies implementation of functions and also introduces standard checks on accessed percpu variables. No functional changes intended. Signed-off-by: Uros Bizjak Cc: Ingo Molnar Cc: Andy Lutomirski Cc: Brian Gerst Cc: Denys Vlasenko Cc: H. Peter Anvin Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Josh Poimboeuf --- v2: Split from the original v1 patch. --- arch/um/include/asm/cpufeature.h | 3 +- arch/x86/include/asm/cpufeature.h | 3 +- arch/x86/include/asm/percpu.h | 54 +++++++++++++------------------ 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/arch/um/include/asm/cpufeature.h b/arch/um/include/asm/cpufeature.h index 66fe06db872f..1eb8b834fbec 100644 --- a/arch/um/include/asm/cpufeature.h +++ b/arch/um/include/asm/cpufeature.h @@ -38,8 +38,7 @@ extern const char * const x86_bug_flags[NBUGINTS*32]; #define this_cpu_has(bit) \ (__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \ - x86_this_cpu_test_bit(bit, \ - (unsigned long __percpu *)&cpu_info.x86_capability)) + x86_this_cpu_test_bit(bit, cpu_info.x86_capability)) /* * This macro is for detection of features which need kernel diff --git a/arch/x86/include/asm/cpufeature.h b/arch/x86/include/asm/cpufeature.h index f95e1c8b7c01..7d527467ab9c 100644 --- a/arch/x86/include/asm/cpufeature.h +++ b/arch/x86/include/asm/cpufeature.h @@ -127,8 +127,7 @@ extern const char * const x86_bug_flags[NBUGINTS*32]; #define this_cpu_has(bit) \ (__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \ - x86_this_cpu_test_bit(bit, \ - (unsigned long __percpu *)&cpu_info.x86_capability)) + x86_this_cpu_test_bit(bit, cpu_info.x86_capability)) /* * This macro is for detection of features which need kernel diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h index cbfbbe836ee2..d6ff0db32209 100644 --- a/arch/x86/include/asm/percpu.h +++ b/arch/x86/include/asm/percpu.h @@ -96,7 +96,7 @@ #endif /* CONFIG_SMP */ #define __my_cpu_type(var) typeof(var) __percpu_seg_override -#define __my_cpu_ptr(ptr) (__my_cpu_type(*ptr)*)(__force uintptr_t)(ptr) +#define __my_cpu_ptr(ptr) (__my_cpu_type(*(ptr))*)(__force uintptr_t)(ptr) #define __my_cpu_var(var) (*__my_cpu_ptr(&(var))) #define __percpu_arg(x) __percpu_prefix "%" #x #define __force_percpu_arg(x) __force_percpu_prefix "%" #x @@ -568,37 +568,29 @@ do { \ #define this_cpu_read_stable_8(pcp) ({ BUILD_BUG(); (typeof(pcp))0; }) #endif -static __always_inline bool x86_this_cpu_constant_test_bit(unsigned int nr, - const unsigned long __percpu *addr) -{ - unsigned long __percpu *a = - (unsigned long __percpu *)addr + nr / BITS_PER_LONG; +#define x86_this_cpu_constant_test_bit(_nr, _var) \ +({ \ + unsigned long __percpu *addr__ = \ + (unsigned long __percpu *)&(_var) + ((_nr) / BITS_PER_LONG); \ + !!((1UL << ((_nr) % BITS_PER_LONG)) & raw_cpu_read(*addr__)); \ +}) -#ifdef CONFIG_X86_64 - return ((1UL << (nr % BITS_PER_LONG)) & raw_cpu_read_8(*a)) != 0; -#else - return ((1UL << (nr % BITS_PER_LONG)) & raw_cpu_read_4(*a)) != 0; -#endif -} - -static inline bool x86_this_cpu_variable_test_bit(int nr, - const unsigned long __percpu *addr) -{ - bool oldbit; - - asm volatile("btl %[nr], " __percpu_arg([var]) - CC_SET(c) - : CC_OUT(c) (oldbit) - : [var] "m" (*__my_cpu_ptr((unsigned long __percpu *)(addr))), - [nr] "Ir" (nr)); - - return oldbit; -} - -#define x86_this_cpu_test_bit(nr, addr) \ - (__builtin_constant_p((nr)) \ - ? x86_this_cpu_constant_test_bit((nr), (addr)) \ - : x86_this_cpu_variable_test_bit((nr), (addr))) +#define x86_this_cpu_variable_test_bit(_nr, _var) \ +({ \ + bool oldbit; \ + \ + asm volatile("btl %[nr], " __percpu_arg([var]) \ + CC_SET(c) \ + : CC_OUT(c) (oldbit) \ + : [var] "m" (__my_cpu_var(_var)), \ + [nr] "rI" (_nr)); \ + oldbit; \ +}) + +#define x86_this_cpu_test_bit(_nr, _var) \ + (__builtin_constant_p(_nr) \ + ? x86_this_cpu_constant_test_bit(_nr, _var) \ + : x86_this_cpu_variable_test_bit(_nr, _var)) #include -- 2.44.0