Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp751778lqe; Sun, 7 Apr 2024 02:06:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUKcF/B1bYvT6mrAXbgTOtWI3bcuaO7WnqkaQ5CQkf3fXK0h4/b0cfy6jM0hASeKJda2sRRJyi1AU/crX9OftpQLsGi0k6a8rFy88I7zA== X-Google-Smtp-Source: AGHT+IHKPb57nCGbHnlbtYBgGt42wJXWnPzBZtgL2KrhlqcduEi5Qf43294cWhYjBnmlf+v6+KBJ X-Received: by 2002:a05:6830:1384:b0:6e5:23d8:a4c5 with SMTP id d4-20020a056830138400b006e523d8a4c5mr5631100otq.34.1712480799462; Sun, 07 Apr 2024 02:06:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712480799; cv=pass; d=google.com; s=arc-20160816; b=1DXtCZnN9D5oXLd8AcU6xP2/5SWl1jmGnWlXTT7WBjgW++VzQyOiEgq85AKFVwdugK SmNU5NfO/Ntf9JYcj/RYm/9G8brmv7Hw0tFHExNisx4Puh2yW98+tDtpS4y9R43zKn50 jxWXB+nCOzuh52eD09YrsvlGTS2/mVEqOVdsZR9rPU2n5KztJiEpksVPIi1lojiwUFlJ nbl/U2MBwIBq8fy5M7CRhBp8iuhI+3ktbSRanL5qCiwPR8J/8vwKsbU52rCGMEgVoT+P VmRrvpyhNHcfjIJMsT2KD9W+gMqxS1FbbDQ7A8AGHPJq+w8fRaPtSPLLYOptYdIBcXUS 76fg== 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=8pFdud31CO30bbz/KPNSHwu6wK19wUtd+HcNwJYEJfY=; fh=cgq6C5LriLlJkuu1ouYJXs5NGM5pBCPS4EUr947Ppbk=; b=W7O72U0gGV0Xq8FosVSyq9impUoWeKGWdpEME2JC8LNeuY8BEz4nh5f2RTcNMAxJEu zKSOAtP3vcp8j1G85g74Uy1S5/86F6dMPZFoad8lEfJ0mAKqHgAWx3jxPTgjuw0Ok091 s3E1roS27Nbe3RKjg6NrMv/MildNJzyMa/EGVRA81JeGOeve2ShLLC3ul3YMKfCfMFxv trB8XItz40gsiPabCR7waspU+j1dyroEo/gspIOvtDVAwFtTXZvOR5fFHeXIRDY8MzlM pj9nN6ciM3RrR1ZHqMgHAHbNgPFrY4PxFk/CuY94UhobIXOY4VDYSwyjEm5SIYHPBWtU oyCw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=U8N5sEYz; 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-134286-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134286-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id bs11-20020a05620a470b00b0078a54ab2f64si6388439qkb.273.2024.04.07.02.06.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Apr 2024 02:06:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-134286-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=U8N5sEYz; 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-134286-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134286-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 1D5071C216B5 for ; Sun, 7 Apr 2024 09:06:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF56438F94; Sun, 7 Apr 2024 09:04:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="U8N5sEYz" Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 2C3BF38DEC; Sun, 7 Apr 2024 09:04:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712480661; cv=none; b=FNuo6gIRX/c9zH7morNLEIA2PwOCt+AkL++PjdpPd7hp+WR5kQcS8TpDec7XjQlcPuUgdRzqDTvz4imt4JvxAYUpLnLdkZHnva3lUPsik8Nh3u0qfOFwaLixBxiRLMwu8t9enFb2Br3S70iQZAKXWVYkyau2owFFyZgecxbqM9Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712480661; c=relaxed/simple; bh=biS7KzzHTKcOQAMJum93/nBTbQjerhk67V4BVUndbxs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KFvkIINNQBRGw1CmZS6wMCn7zJzicxsehnVPY0BNNQcNSpioW+qZx6OuHxgBZTFrluvMgUYy/nhDnUBDokxEN09s8YdCFJBHFctl+p/2xdqUGl5113oLidwUIdVTu/Z5rhKz2qlP5N/rDgBz418krlosv7mclnaWc6DiUdxzuho= 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=U8N5sEYz; arc=none smtp.client-ip=209.85.210.176 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-pf1-f176.google.com with SMTP id d2e1a72fcca58-6ed04c91c46so1752574b3a.0; Sun, 07 Apr 2024 02:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712480659; x=1713085459; 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=8pFdud31CO30bbz/KPNSHwu6wK19wUtd+HcNwJYEJfY=; b=U8N5sEYztOMLwG/sg0hjXHkBMQbgbp9eOeMEK2mkl7XE3EDtQObQ1afeIZ5kBv9vME 3BfNA+oUm8ptdHbZcdPYlHdDPBmIZobkRsjKwX/xhphkW7sg0O65kI7aExLBZbGL0aRR EQCSmaqnUpIVQ1UGFTHWdQydM6IiVBAHFUazkMXxKrV2jAJMAzrUNpnL+uxBnboCaQhc HXiAVGOVfS4i7hycABprt5mgT91MlW5cfrVZhj9DOe81MmlbW0WeHHWU/bMzZWspYCHO SNEQU9jibNay/CrVXdF8i1zsRs5H0dLh6AASuadR4roLiSO4exZWDsTTKdATyOLEJbKR Av0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712480659; x=1713085459; 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=8pFdud31CO30bbz/KPNSHwu6wK19wUtd+HcNwJYEJfY=; b=KIFOF9iMiXd7TdPgRxWlOpZcU79uudNDa6SQDGbjKsGNJ75gXtF70lp9Amzfj8lMoU 5VgJ+Jk6WcjQFYexQV2B5C6ur74jbX7GLyFZNrRGILgRZlGDOUJAIDIAq/nX7wnVMu4u bkDatfYXUSip+Rabl1YcxgnPBS2mp7gBt88ttv2cXfHBesB+eW4o+b8NRFTyZBIa2p6v iRHG0dhqgXsGMPNtpbbFaSX84dybUlg9yx1JHrhN1DSoRpQqfLzoVKq5YfzaB1OXvX/F R9CSIIzquOl/2LS+PsxEfqGaLRFKWy0t5mSRyzFbAbQt4qNoR+/0oTd4kDIQHvBZ3QLt t7Xg== X-Gm-Message-State: AOJu0YxPbsQ3oncf7vL2b/Id46mp4CC9quLdHRUSn6Fo0B4hFTxtdrPT UqN67OPq/j7wdh9QXV/sAcCzqbpy7tvpjLF1Pp3cOYg+fZnt8svrGhRzImRB X-Received: by 2002:a05:6a21:329b:b0:1a7:1cb:3e20 with SMTP id yt27-20020a056a21329b00b001a701cb3e20mr7113512pzb.54.1712480659129; Sun, 07 Apr 2024 02:04:19 -0700 (PDT) Received: from localhost ([198.11.178.15]) by smtp.gmail.com with ESMTPSA id r6-20020aa78446000000b006eb0027f2b8sm4322191pfn.9.2024.04.07.02.04.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 07 Apr 2024 02:04:18 -0700 (PDT) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: rcu@vger.kernel.org, x86@kernel.org, Lai Jiangshan , "Paul E. McKenney" , Peter Zijlstra , Frederic Weisbecker , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Uros Bizjak , Josh Poimboeuf , Breno Leitao , Kent Overstreet , Pawan Gupta , Rick Edgecombe , Vegard Nossum , Daniel Sneddon , Nikolay Borisov Subject: [PATCH V2 10/11] x86/rcu: Add rcu_preempt_count Date: Sun, 7 Apr 2024 17:05:57 +0800 Message-Id: <20240407090558.3395-11-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20240407090558.3395-1-jiangshanlai@gmail.com> References: <20240407090558.3395-1-jiangshanlai@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 From: Lai Jiangshan Implement PCPU_RCU_PREEMPT_COUNT for x86. Mainly copied from asm/preempt.h Make rcu_read_[un]lock() inlined for rcu-preempt. Make rcu_read_lock() only one instruction. Make rcu_read_unlock() only two instructions in the fast path. Cc: "Paul E. McKenney" Cc: Peter Zijlstra Cc: Frederic Weisbecker Signed-off-by: Lai Jiangshan --- arch/x86/Kconfig | 1 + arch/x86/include/asm/current.h | 3 + arch/x86/include/asm/rcu_preempt.h | 107 +++++++++++++++++++++++++++++ arch/x86/kernel/cpu/common.c | 4 ++ 4 files changed, 115 insertions(+) create mode 100644 arch/x86/include/asm/rcu_preempt.h diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 4fff6ed46e90..e805cac3763d 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -256,6 +256,7 @@ config X86 select HAVE_OBJTOOL if X86_64 select HAVE_OPTPROBES select HAVE_PAGE_SIZE_4KB + select HAVE_PCPU_RCU_PREEMPT_COUNT select HAVE_PCSPKR_PLATFORM select HAVE_PERF_EVENTS select HAVE_PERF_EVENTS_NMI diff --git a/arch/x86/include/asm/current.h b/arch/x86/include/asm/current.h index bf5953883ec3..dcc2ef784120 100644 --- a/arch/x86/include/asm/current.h +++ b/arch/x86/include/asm/current.h @@ -24,6 +24,9 @@ struct pcpu_hot { unsigned long top_of_stack; void *hardirq_stack_ptr; u16 softirq_pending; +#ifdef CONFIG_PCPU_RCU_PREEMPT_COUNT + int rcu_preempt_count; +#endif // #ifdef CONFIG_PCPU_RCU_PREEMPT_COUNT #ifdef CONFIG_X86_64 bool hardirq_stack_inuse; #else diff --git a/arch/x86/include/asm/rcu_preempt.h b/arch/x86/include/asm/rcu_preempt.h new file mode 100644 index 000000000000..cb25ebe038a5 --- /dev/null +++ b/arch/x86/include/asm/rcu_preempt.h @@ -0,0 +1,107 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_RCU_PREEMPT_H +#define __ASM_RCU_PREEMPT_H + +#include +#include +#include + +#ifdef CONFIG_PCPU_RCU_PREEMPT_COUNT + +/* We use the MSB mostly because its available */ +#define RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED 0x80000000 + +/* + * We use the RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED bit as an inverted + * current->rcu_read_unlock_special.s such that a decrement hitting 0 + * means we can and should call rcu_read_unlock_special(). + */ +#define RCU_PREEMPT_INIT (0 + RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED) + +/* + * We mask the RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED bit so as not to + * confuse all current users that think a non-zero value indicates we + * are in a critical section. + */ +static inline int pcpu_rcu_preempt_count(void) +{ + return raw_cpu_read_4(pcpu_hot.rcu_preempt_count) & ~RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED; +} + +static inline void pcpu_rcu_preempt_count_set(int count) +{ + int old, new; + + old = raw_cpu_read_4(pcpu_hot.rcu_preempt_count); + do { + new = (old & RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED) | + (count & ~RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED); + } while (!raw_cpu_try_cmpxchg_4(pcpu_hot.rcu_preempt_count, &old, new)); +} + +/* + * We fold the RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED bit into the RCU + * preempt count such that rcu_read_unlock() can decrement and test for + * the need of unlock-special handling with a single instruction. + * + * We invert the actual bit, so that when the decrement hits 0 we know + * we both reach a quiescent state (no rcu preempt count) and need to + * handle unlock-special (the bit is cleared), normally to report the + * quiescent state immediately. + */ + +static inline void pcpu_rcu_preempt_special_set(void) +{ + raw_cpu_and_4(pcpu_hot.rcu_preempt_count, ~RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED); +} + +static inline void pcpu_rcu_preempt_special_clear(void) +{ + raw_cpu_or_4(pcpu_hot.rcu_preempt_count, RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED); +} + +static inline bool pcpu_rcu_preempt_special_test(void) +{ + return !(raw_cpu_read_4(pcpu_hot.rcu_preempt_count) & RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED); +} + +static inline void pcpu_rcu_preempt_switch(int count, bool special) +{ + if (likely(!special)) + raw_cpu_write(pcpu_hot.rcu_preempt_count, count | RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED); + else + raw_cpu_write(pcpu_hot.rcu_preempt_count, count); +} + +/* + * The various rcu_preempt_count add/sub methods + */ + +static __always_inline void pcpu_rcu_preempt_count_add(int val) +{ + raw_cpu_add_4(pcpu_hot.rcu_preempt_count, val); +} + +static __always_inline void pcpu_rcu_preempt_count_sub(int val) +{ + raw_cpu_add_4(pcpu_hot.rcu_preempt_count, -val); +} + +/* + * Because we keep RCU_PREEMPT_UNLOCK_SPECIAL_INVERTED set when we do + * _not_ need to handle unlock-special for a fast-path decrement. + */ +static __always_inline bool pcpu_rcu_preempt_count_dec_and_test(void) +{ + return GEN_UNARY_RMWcc("decl", __my_cpu_var(pcpu_hot.rcu_preempt_count), e, + __percpu_arg([var])); +} + +#define pcpu_rcu_read_unlock_special() \ +do { \ + rcu_read_unlock_special(); \ +} while (0) + +#endif // #ifdef CONFIG_PCPU_RCU_PREEMPT_COUNT + +#endif /* __ASM_RCU_PREEMPT_H */ diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 5c1e6d6be267..918b1f5cb75d 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -1995,6 +1995,10 @@ DEFINE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot) = { .current_task = &init_task, .preempt_count = INIT_PREEMPT_COUNT, .top_of_stack = TOP_OF_INIT_STACK, + +#ifdef CONFIG_PCPU_RCU_PREEMPT_COUNT + .rcu_preempt_count = RCU_PREEMPT_INIT, +#endif // #ifdef CONFIG_PCPU_RCU_PREEMPT_COUNT }; EXPORT_PER_CPU_SYMBOL(pcpu_hot); EXPORT_PER_CPU_SYMBOL(const_pcpu_hot); -- 2.19.1.6.gb485710b