Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp1369378lqt; Wed, 20 Mar 2024 01:31:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUBlDN3M4kF1DBAkaLYfhyEA75j3vleZHVX7MrK9IPkdHsvt0itziGBjpdGB+h8hh5uiKZ76ostG8QTRY4UxIB1hpbauED3UTb/w+eQVA== X-Google-Smtp-Source: AGHT+IEmgFh1mW8m9StWFPY+OevRStyIRvAk5rw5vMGjs/y/xWkr887XfXPY4s1zT/RhdVlKujcO X-Received: by 2002:a17:902:d58d:b0:1de:e5aa:518a with SMTP id k13-20020a170902d58d00b001dee5aa518amr23919048plh.32.1710923514811; Wed, 20 Mar 2024 01:31:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710923514; cv=pass; d=google.com; s=arc-20160816; b=CjB5O/Lh4+rSejbzxKq4fvt8WPbHNmLRpIlfgLwOvA6ysz/bdueNEYEUEszQM8tjXe J+9k1WoEmKbUxU6t0lW7f6y51uD+M3Kc/ZtVhe24q0lg3aZeIhT82bbYnV0DYB4EFx33 NKrnwA/UDviawf2oGzxHTbU57RLKKcQ2/G5b40BGVoFWAhsrm8QXqEmVms48aWt5TBUY A1ZjozPsx7BnviLWoTRJeTr3CcSX/V53+YPlHx01eeXfk+o5CWjhdRfZE+PapsKRirF2 I3hjvPtpuhxjzP8UHyZ37BAatIBcXElR6MhmF6MstDz0VOwwbYMjz2p/H3C9+fGMu/FE VIiA== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=8x6eiY7ZcIzOZGBmPdvL8XgY7mCZT6eT1G6y3XtGvPo=; fh=HCcE2frUtKl4TYtGqWaIb+bqDd0YSyyoR978pP+VTak=; b=APH9/cmv0yt4dyidRxSWLdQcfIGQcG1u5pP3UoUwnWVJVSK8dXIF8QORv0cd9ySTA5 z70IyV29di+fLJ+GmV473mrPaC1ndhbspzSMszP0Y5bwmXE+tJvtdbhJO07LuKIJVNtd LOarU3Rec35u4prkTk1/HMdI17KxoQy0S4MN2U3Mki9S+J3TFot/oNgyUfa7dO4Uq91o MxG6MJeGOgxi5vH2sj/MiNT76B3gujoOs79HYtOFurhoVl/geKgT/KBqVb+nJiqQ1grK kaMJrjeO60psxHjyPrQEIYc9s4ZHXgv/y0/IXsGbfchOB1sYT/FBZZufKHgir/jI0473 E4Jg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jzc2zfi4; 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-108619-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-108619-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d12-20020a170902cecc00b001db5a8f9316si12647722plg.152.2024.03.20.01.31.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 01:31:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-108619-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jzc2zfi4; 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-108619-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-108619-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 7A3B528511E for ; Wed, 20 Mar 2024 08:31:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E099239AFE; Wed, 20 Mar 2024 08:31:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jzc2zfi4" Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) (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 3FF5839AC7 for ; Wed, 20 Mar 2024 08:31:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710923495; cv=none; b=iuMzxYSOP1NQrR6eSKkLwcaxtjFU4gOJzYrMSKUG5tIVX6V+Gt4D7B9Zn9OA7UMFNv8wkk/RiYS0wtMgLG0Hv2ToizQBOyZh750GxyIXpEB1IGvPWL5xIxOOfWfaetHkjjz4QgqQoQQ//QujF8ZxzC/wHQciNnHm/Goib3BqcNE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710923495; c=relaxed/simple; bh=2gItDXZJGQHo8Mg0E3k4W+lrWdFzWtPXNUkvUUIfVRI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=MSWy0W35xcQZbwJ8zDWdHlOZ4Z5IU6RRpxenQPbh+wn2uDf5blw1HNmAiKxvWrrwqOLiSv4+z7uuBT7KeB+QVjkdgFyfQMHG5M2hR6q8S/eP54pvY7wcXTD9ZtBbWo/Q2OuQwSe+ufDIrWh1f2avFKSflUtJMShEuTZf8D0NALo= 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=jzc2zfi4; arc=none smtp.client-ip=209.85.218.48 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-f48.google.com with SMTP id a640c23a62f3a-a46ba1a05e0so424307966b.3 for ; Wed, 20 Mar 2024 01:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710923491; x=1711528291; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8x6eiY7ZcIzOZGBmPdvL8XgY7mCZT6eT1G6y3XtGvPo=; b=jzc2zfi4jBeYkwg/WZajpnoC2+LZiMGUxB2gjI39ZurioIJ7ePbeBhIUw3fhPkxVr8 ATnZWSRBS+7dM+Q3U8WdeVW+uG2q5QPqCwtic0eELFSBmBE2+H1pl3wbXYHHEv+mJ1OV YAY6QNzLcWjeNzY0pVagvHEJrb375IvLbibqnWUld9X5ERt5kHKU53ptmSOzasLqmRgx TCyfC5xVmyEW7+/MdCx1hhghvQKC6TDObTDi/NYaQl4DvPXZhZRC5nRTz1p+yxrTG77D JUOjtfQqlNYfCcG7UKJ8PsjUO43IRgYNhfjwJW4DSAENON1FUbmvFoylySgyiRzO1hyC Stwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710923491; x=1711528291; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8x6eiY7ZcIzOZGBmPdvL8XgY7mCZT6eT1G6y3XtGvPo=; b=kfqMVdrZ4OasnKsTn38F7H4v1YsZ1nRJnw09onzN+QSJOb5ijpQ1rfOtDjxolLt4Mx 73MFAxgxr0JFjPohdqQFHYjXowc4TTL3PQS/4PcBR/jPiOkv1WolRpk5kYVgrj9AE+cM BW96OzTTlXlAKMbSpcP4Ss/+YCMdgeN7uNKbadnANoIkihcenXrOSaGQGGsskopr0FJq wtmhN1LRnL9jppLzB2MBRa/Lk4M95oeiXAVSOQ6fYoeCBC5b7ZyhPulFRdIn4nVS9SKS S7UGamhE2GjIA4NqRRkk4+FV++L51DwMD9IPDcbYcDw+xkjqvxjMuMKxWDs9lTq3Yu6x RkPg== X-Forwarded-Encrypted: i=1; AJvYcCXbd0CC3ITJ8fgBZVyUFX4zAzFC4c3rRTza1GsM2O+DmXrsqJwVWClPYJNR6Z9ljHCSOuWfMGiP0fc3i/zPGND9bYSZ5V0zISHjLBg2 X-Gm-Message-State: AOJu0Yzhdj+/WNlA2dyokV5d1O/ffnkQI8byQYQvwk7SleiYjI/mfgQ/ AQ9ULYZCfPkDgJxer0dNXWxjJJrkQ6V8s+/B2Lcfya4OhGzSSYih X-Received: by 2002:a17:906:79c9:b0:a45:f705:777c with SMTP id m9-20020a17090679c900b00a45f705777cmr2864659ejo.40.1710923491208; Wed, 20 Mar 2024 01:31:31 -0700 (PDT) Received: from 123000256IE.iskraemeco.si ([193.77.86.250]) by smtp.gmail.com with ESMTPSA id ci20-20020a170906c35400b00a46e92e583bsm1170769ejb.149.2024.03.20.01.31.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 01:31:30 -0700 (PDT) From: Uros Bizjak To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH 1/2] x86/percpu: Define this_percpu_xchg_op() Date: Wed, 20 Mar 2024 09:30:40 +0100 Message-ID: <20240320083127.493250-1-ubizjak@gmail.com> X-Mailer: git-send-email 2.44.0 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 percpu_xchg_op() using generic percpu primitives instead of using asm. The new implementation is similar to local_xchg() and allows the compiler to perform various optimizations (e.g. the compiler is able to create fast path through the loop, according to likely/unlikely annotations in percpu_try_cmpxchg_op). No functional changes intended. Signed-off-by: Uros Bizjak Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" --- arch/x86/include/asm/percpu.h | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h index 44958ebaf626..de991e6d050a 100644 --- a/arch/x86/include/asm/percpu.h +++ b/arch/x86/include/asm/percpu.h @@ -230,25 +230,15 @@ do { \ }) /* - * xchg is implemented using cmpxchg without a lock prefix. xchg is - * expensive due to the implied lock prefix. The processor cannot prefetch - * cachelines if xchg is used. + * this_cpu_xchg() is implemented using cmpxchg without a lock prefix. + * xchg is expensive due to the implied lock prefix. The processor + * cannot prefetch cachelines if xchg is used. */ -#define percpu_xchg_op(size, qual, _var, _nval) \ +#define this_percpu_xchg_op(_var, _nval) \ ({ \ - __pcpu_type_##size pxo_old__; \ - __pcpu_type_##size pxo_new__ = __pcpu_cast_##size(_nval); \ - asm qual (__pcpu_op2_##size("mov", __percpu_arg([var]), \ - "%[oval]") \ - "\n1:\t" \ - __pcpu_op2_##size("cmpxchg", "%[nval]", \ - __percpu_arg([var])) \ - "\n\tjnz 1b" \ - : [oval] "=&a" (pxo_old__), \ - [var] "+m" (__my_cpu_var(_var)) \ - : [nval] __pcpu_reg_##size(, pxo_new__) \ - : "memory"); \ - (typeof(_var))(unsigned long) pxo_old__; \ + typeof(_var) pxo_old__ = this_cpu_read(_var); \ + do { } while (!this_cpu_try_cmpxchg(_var, &pxo_old__, _nval)); \ + pxo_old__; \ }) /* @@ -534,9 +524,9 @@ do { \ #define this_cpu_or_1(pcp, val) percpu_to_op(1, volatile, "or", (pcp), val) #define this_cpu_or_2(pcp, val) percpu_to_op(2, volatile, "or", (pcp), val) #define this_cpu_or_4(pcp, val) percpu_to_op(4, volatile, "or", (pcp), val) -#define this_cpu_xchg_1(pcp, nval) percpu_xchg_op(1, volatile, pcp, nval) -#define this_cpu_xchg_2(pcp, nval) percpu_xchg_op(2, volatile, pcp, nval) -#define this_cpu_xchg_4(pcp, nval) percpu_xchg_op(4, volatile, pcp, nval) +#define this_cpu_xchg_1(pcp, nval) this_percpu_xchg_op(pcp, nval) +#define this_cpu_xchg_2(pcp, nval) this_percpu_xchg_op(pcp, nval) +#define this_cpu_xchg_4(pcp, nval) this_percpu_xchg_op(pcp, nval) #define raw_cpu_add_return_1(pcp, val) percpu_add_return_op(1, , pcp, val) #define raw_cpu_add_return_2(pcp, val) percpu_add_return_op(2, , pcp, val) @@ -575,7 +565,7 @@ do { \ #define this_cpu_and_8(pcp, val) percpu_to_op(8, volatile, "and", (pcp), val) #define this_cpu_or_8(pcp, val) percpu_to_op(8, volatile, "or", (pcp), val) #define this_cpu_add_return_8(pcp, val) percpu_add_return_op(8, volatile, pcp, val) -#define this_cpu_xchg_8(pcp, nval) percpu_xchg_op(8, volatile, pcp, nval) +#define this_cpu_xchg_8(pcp, nval) this_percpu_xchg_op(pcp, nval) #define this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(8, volatile, pcp, oval, nval) #define this_cpu_try_cmpxchg_8(pcp, ovalp, nval) percpu_try_cmpxchg_op(8, volatile, pcp, ovalp, nval) #endif -- 2.44.0