Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2145668rdg; Sun, 15 Oct 2023 13:25:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdlVrmibXDQS05oz5DyPDzYAQl5wLBxORSC4AYb9OJiCqkvrx5qpyF79f6tJ8W5bLnhWGp X-Received: by 2002:a17:90a:199e:b0:27d:222c:f5eb with SMTP id 30-20020a17090a199e00b0027d222cf5ebmr9378579pji.11.1697401557794; Sun, 15 Oct 2023 13:25:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697401557; cv=none; d=google.com; s=arc-20160816; b=ldvEfqf903X/SgRP3sCWyks71m+lAUTsYCKTrkIZoLwzVOvFpxeabg+PgCVxHgwyUl 8VXRmtKmtj5oTrqA1kGIzBINAxOjiJQdv1WeFrY/T5HfrDmdPjorJPMa2/fI5FIcDG9/ Yayy8+5C7pxsyADS5M3CvDywbLEDFVy82+YkZTmWbs/t7XR8G8TQwFQhD7TQQ/OnlKUX 4RJMPPK3RPN09ROa+uLJzxmcHJfHrst0EuvP/sSss41fQRcL4eCMxHCk2jmeOsguBiLb rOI6foCfs/UQDplSHcBGIpM3/fXrAMN81EX2WoH9HrDDLriscet7gPtYrmCBX+ZC2WkE zi9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IrkrxsPn2IZuqVMHhvR4XZKurOwbK1OYZ2ExToejPfA=; fh=h3S0GvYL6qGSM0G8jg7iIXaIfYT+afvdw8eum3NN6fs=; b=hjFbLxJXbZUBiFHgx6Vws/1QgbpVeTScuWuH63WH0a1oHlQerYVXtMNnDOviEK6Xfh qXZT+CrdHTQMF+Bx2+cwUW3WkTe8umQ67QbWVKrGRiqbicVaGmYBNbfhrSttGaqCo61x yYg/w7+3qQzzyXOsXd5bMtcAMXDLCc2Y9Sgiy7M1KKAzuP0GazGb9SoVS1XGB94O0o+6 kR0jWZq1Nqft+eRBk5GXQ6+Omtz7j0sjOKaELk8lc7SUCtuYGTkQw5zogGC5gXqGj3Y9 JwZYD6OEYlAEzlii8vq1rMGxFwP7F8bgnd3le/ggtLsxHCVfM6zhdj1p07LL2i8jLDHp Tg2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mpB4g77P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id pi15-20020a17090b1e4f00b0027d12ed9e08si4967354pjb.106.2023.10.15.13.25.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 13:25:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mpB4g77P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 5C8648057E76; Sun, 15 Oct 2023 13:25:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230318AbjJOUZl (ORCPT + 99 others); Sun, 15 Oct 2023 16:25:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229641AbjJOUZi (ORCPT ); Sun, 15 Oct 2023 16:25:38 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD8F6B7 for ; Sun, 15 Oct 2023 13:25:36 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-9bda758748eso334651566b.2 for ; Sun, 15 Oct 2023 13:25:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697401535; x=1698006335; 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=IrkrxsPn2IZuqVMHhvR4XZKurOwbK1OYZ2ExToejPfA=; b=mpB4g77PyxW3ScX1NhmCWpeCneVRrX1aEGac/yxeBPTCjHJTp5s98nTuiM1uSVXW4t ofVQB41HZeUg8pdHl3wAl9jRBg4xzt5nteCQmlBj5MB4Gl8BsH1M3B6yUsHCwC3uvbl4 8bccaEjQSs4OgW6ujqwJg1kRAFlgqihNW54uXq70Kw69UbqPsufGnDgalVs2S+BgFEL5 dhP+Gw5184rrhNJoluVXQWpbxUKXkc3RZtXS9tbXKf1xYjQLUh/H90nbzNR7fLmj2woD cMIiXYBdlIFAn9BjzsrUjgwzgiCscHOHHphNShp2oieG5Gpf4mR2q11HSjZtNn0svfS2 9I9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697401535; x=1698006335; 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=IrkrxsPn2IZuqVMHhvR4XZKurOwbK1OYZ2ExToejPfA=; b=Ip6rJGiu6TqgAgH91iwOwZ8GuzUYfVklcImo2/pJ3n8+gnwbzMxuS2qX2OwLCVpisL C2n0RuU+Iqkf/AunwQz3ZTOW/qXdxV0cMdHZxD+uUnRwsJKx4ekjjBsBjbEehmeXqk/p ppKSRdysWDaNK4mN/UtU4dyhGVsCmx9igTmz2oCujHmAoJH9kh3O09X+JrZNCHBQS+nc ColKT9MdsmU8dZJpMPMIMzr24jiTH35zogTgxylST+xWMhRZZlZz9GO79tqBAb0SGFmx 2mb+TOvzAFh812357mQRoCBTKQL+TANaeXLb/BUkBt34TnBLHKqguu8giNnI56uNwIjL e9Fw== X-Gm-Message-State: AOJu0YxJtygNh8t16etfEr/y7C1tZQPLsL/IG/1S04E6rGX4ttIno/wG KAH98iHtoQvqd3roRkb8huU= X-Received: by 2002:a17:907:5c6:b0:9c4:6893:ccc5 with SMTP id wg6-20020a17090705c600b009c46893ccc5mr375301ejb.57.1697401535225; Sun, 15 Oct 2023 13:25:35 -0700 (PDT) Received: from localhost.localdomain ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id v23-20020a1709067d9700b009b97d9ae329sm2686943ejo.198.2023.10.15.13.25.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 13:25:34 -0700 (PDT) From: Uros Bizjak To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Andy Lutomirski , Brian Gerst , Denys Vlasenko , "H . Peter Anvin" , Linus Torvalds , Peter Zijlstra , Thomas Gleixner , Josh Poimboeuf , Nadav Amit Subject: [PATCH -tip 2/3] x86/percpu: Use C for arch_raw_cpu_ptr() Date: Sun, 15 Oct 2023 22:24:40 +0200 Message-ID: <20231015202523.189168-2-ubizjak@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231015202523.189168-1-ubizjak@gmail.com> References: <20231015202523.189168-1-ubizjak@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sun, 15 Oct 2023 13:25:55 -0700 (PDT) Implement arch_raw_cpu_ptr() in C to allow the compiler to perform better optimizations, such as setting an appropriate base to compute the address. The compiler is free to choose either MOV or ADD from this_cpu_off address to construct the optimal final address. There are some other issues when memory access to the percpu area is implemented with an asm. Compilers can not eliminate asm common subexpressions over basic block boundaries, but are extremely good at optimizing memory access. By implementing arch_raw_cpu_ptr() in C, the compiler can eliminate additional redundant loads from this_cpu_off, further reducing the number of percpu offset reads from 1646 to 1631. 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 Co-developed-by: Nadav Amit Signed-off-by: Nadav Amit Signed-off-by: Uros Bizjak --- v2: Use CONFIG_USE_X86_SEG_SUPPORT to handle cases where KASAN is enabled. v3: Split the patch to the part that introduces MOV with tcp_ptr__ += (unsigned long)(ptr) and the part that introduces named address spaces. --- arch/x86/include/asm/percpu.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h index 915675f3ad60..54746903b8c3 100644 --- a/arch/x86/include/asm/percpu.h +++ b/arch/x86/include/asm/percpu.h @@ -49,6 +49,21 @@ #define __force_percpu_prefix "%%"__stringify(__percpu_seg)":" #define __my_cpu_offset this_cpu_read(this_cpu_off) +#ifdef CONFIG_USE_X86_SEG_SUPPORT +/* + * Efficient implementation for cases in which the compiler supports + * named address spaces. Allows the compiler to perform additional + * optimizations that can save more instructions. + */ +#define arch_raw_cpu_ptr(ptr) \ +({ \ + unsigned long tcp_ptr__; \ + tcp_ptr__ = __raw_cpu_read(, this_cpu_off); \ + \ + tcp_ptr__ += (unsigned long)(ptr); \ + (typeof(*(ptr)) __kernel __force *)tcp_ptr__; \ +}) +#else /* CONFIG_USE_X86_SEG_SUPPORT */ /* * Compared to the generic __my_cpu_offset version, the following * saves one instruction and avoids clobbering a temp register. @@ -63,6 +78,8 @@ tcp_ptr__ += (unsigned long)(ptr); \ (typeof(*(ptr)) __kernel __force *)tcp_ptr__; \ }) +#endif /* CONFIG_USE_X86_SEG_SUPPORT */ + #else /* CONFIG_SMP */ #define __percpu_seg_override #define __percpu_prefix "" -- 2.41.0