Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp2096680rdb; Wed, 31 Jan 2024 20:56:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHVnul+KK4UlisxwUruOIH7nLeRrITDqqTmzodxws6W9FNQ6UIC1TZ0gOtTfFRl1cOrJNcx X-Received: by 2002:a05:6808:3095:b0:3bd:c174:b234 with SMTP id bl21-20020a056808309500b003bdc174b234mr4584316oib.24.1706763366504; Wed, 31 Jan 2024 20:56:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706763366; cv=pass; d=google.com; s=arc-20160816; b=hw3KHApTKUs1uPD8rk+h3rkaAZxLwKDogLKGfd9jePopoo290e4JDwpLFjQQC1yClT IOqbbYRuM/tIVx2WY6LgYLbxexHWnqlrzJiXtSNOyxfL8ScPWqhhJ2ViP4pe6cKbVCEG 3pgHe/ag0c0/rrlfpjW2ztsntLKgUqnJaaigej44FbLu9YMsPLWAMYZvYypqHPBl5Zzz 91moyAKxDwTWnN5PMR0evjglzT0s6fCpCFF07xrTuPu2QsMmqjzVjc7KbaidcQI2hwih TLB8893lPpDQFlWqiVtIB31HGCyIKyJMEuTIMj0fzaFONXif6wQzCZfc1TlR5RZH7u0m N7Ng== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=YsqrP80NBPWxBx78FsktitHzUN/DGz3kT5ZrnUUMNWc=; fh=FC4clvCJ17qUJ14LMCLx0BTpWYp2MblQgg0L4Swn2NI=; b=iejx9HSKld2AafPEezMr+VuGcZhYV2OyArRPS5lpgOe0A7F7W+TtoXAOABTdJm3wXg n+GXKtc9XGTwOs7vhlbBs45Bx2rPT4M/0CG9PO8ps9UAOL4Ssm/hJYHarfVVOYZFvuOM DDPVAFwEYLrGp8ed+oA6qmKSQ5szmKge0t1i1ZcpI0l5kV+42DDSMHBjZ1pB6LvCaAhv 6Av1Ec/08vqSgG1S/wnnUC/npC56VCRrXoaIW8nkf71hPZuBs5ZEKY0gWz7QSVwyzuN2 sri8OQV6M3gD+XZGOvY4tK2okYRz3zGGMc5LXEsaMwi05x786YDesDWdV3fCdlk8DWqL /zmA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TJeoq46D; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47638-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47638-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCXj7vmKHBJTRa8FSSBfCos0iAqktplanxpZ7RKWhbIVbIZVIpVAGWB7edZUpqNyjMJ3Nuf0xz31N2VRHftTrEoAlBiCnhbJ9hiF3Ihzbg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id f23-20020a637557000000b005cecd95b6besi11036436pgn.685.2024.01.31.20.56.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 20:56:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47638-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=@google.com header.s=20230601 header.b=TJeoq46D; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47638-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47638-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 22AAF2889E5 for ; Thu, 1 Feb 2024 04:56:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9FA843BB4F; Thu, 1 Feb 2024 04:55:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TJeoq46D" Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 15F503BB2D for ; Thu, 1 Feb 2024 04:55:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706763358; cv=none; b=jlWXD2DYXJfacGnB1xSMCBBQ38xz/J/LfE2ZGu5cTXTgd4piozi1YQuGrtmA4HhmqmqvADu2532aEKsLCpVPhHR+Wy3ou+hWx78Zy1+l80aXLuVmErmpFe53qexhNP+yIPpRta33bKpPXzRMn9xlQboijLcBhSdsVGPY6eSjvLU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706763358; c=relaxed/simple; bh=L0yoXvq/d6omUMi6xtzN6TigsEZjzXC09EqfFo90GtI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=TwDTZwurQ/R43ICV+rKZt4lvCrOvzKlaMaZbEnXps/DpOmeTrL7ZOErS38Jo37uEsxjWIjkM7LXY1+xAUs6U209hnVMgj9IoE8iaOVnLCq5GkYyywVIfSIECIYVHzNRgRBasfzZ5wXXD9ASYEJuQLpbDxfRUUYTVVswMWaWtYno= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TJeoq46D; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1d93b982761so119425ad.0 for ; Wed, 31 Jan 2024 20:55:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706763356; x=1707368156; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=YsqrP80NBPWxBx78FsktitHzUN/DGz3kT5ZrnUUMNWc=; b=TJeoq46DV0d6lA9cshUVwSeFYmEEqkBSUW4BG4NCXqGCrpDmJ7ZwzmvW0lj5b6JgOS +bODEdibSzFkkNrrNkEJuy+WqfjMOSY/BrnduUdBefpeI2gbgkJNx8STvK99i3rjIYwg 9ijWtUF0KMJXHFMjhjp8eD8lP80HQs3AySu60rP5bPzAGlXpL/CMxryBsnmo6iDhFqn0 8PrEtje+/hiPIAyYUV1MLFR62oxB8RsNok2B5KQrCgolRTz41f0kN0BnoGGqulpKtutL V/IBmll2PzVN/9CSJpbvYTRuTre6aRlzYAt/4M8u4LehjePo7BniVyYtNtjI8t7pmrtv 0r/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706763356; x=1707368156; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=YsqrP80NBPWxBx78FsktitHzUN/DGz3kT5ZrnUUMNWc=; b=AOIvUN7As2Jgk4gjn2214NQliv0OEyIZo3N7DKf5Sw5vvYzLUQGuqB8TeafZgVkqE+ 0NeKcqc1RaPC3ajH2YmSqhRVJ3bVL4y0xteMxpj3bPApuS7/NQr696kiB6Ggqd3Q1D8/ vJRzDUvfxswxAeiZe4S5/42/dbbQjHai/WzABxyEvre8FlCswx/g7zD8ogtu2ogCOajC 7Pv+PcGIcRAUq1b9pUsKfuBwF1spNCEwSh2cyhKXT8i8BtVZZDDCZU+LnFLa5zhIf0ZE 2xSB0O7IedlF9DoszMIt8m/2AeciG0NI15FmvZolyaOdBPvhW+nj+ukLBubNbrWslUyx YxdA== X-Gm-Message-State: AOJu0Yy7/uuHZJMLu1tion0j4Z2FoZri90EdEccvNIVsWc2JFQHKDxrM Tb/yaayEYzGChmfnSadvrHrz9XW1tdCEH0iwa6o1qmceIJFk9Jz+jFLCLrWePQ== X-Received: by 2002:a17:902:d58f:b0:1d8:f3c7:fb83 with SMTP id k15-20020a170902d58f00b001d8f3c7fb83mr106370plh.9.1706763356049; Wed, 31 Jan 2024 20:55:56 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCV4Q548nt1ArvLGL2wR2E8vSlBntuR737WWtXt371mCiALATgckvigQx4dwtoGKhCMAatv2D8bgdgK02B+fOCaMn4Bf4YU8F27jYiuApzRhepfS+rHhUyzbSLKP8LIRnPbZFUD1yS1BWQL8Ewyk6oZDXFNulca7j+6S4Gs6tejvqqNk3CHRnFK3eJwLrkzePpZcAvG5g6PVOUh4lIGl5HItVTfnNu63wYd+qACW2eiF+PqyotQ8L71W+jKKENG7//L4f9hcdxI= Received: from google.com ([2620:15c:2d3:205:ce95:ce9d:3dd2:5053]) by smtp.gmail.com with ESMTPSA id o4-20020a170902d4c400b001d8a5c08277sm9616689plg.260.2024.01.31.20.55.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 20:55:55 -0800 (PST) Date: Wed, 31 Jan 2024 20:55:51 -0800 From: Fangrui Song To: Dave Martin Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org, Jisheng Zhang , llvm@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH] arm64: jump_label: use constraint "S" instead of "i" Message-ID: <20240201045551.ajg4iqcajyowl2rh@google.com> References: <20240131065322.1126831-1-maskray@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: On 2024-01-31, Dave Martin wrote: >On Wed, Jan 31, 2024 at 08:16:04AM +0100, Ard Biesheuvel wrote: >> Hello Fangrui, >> >> On Wed, 31 Jan 2024 at 07:53, Fangrui Song wrote: >> > >> > The constraint "i" seems to be copied from x86 (and with a redundant >> > modifier "c"). It works with -fno-PIE but not with -fPIE/-fPIC in GCC's >> > aarch64 port. > >(I'm not sure of the exact history, but the "c" may be inherited from >arm, where an output modifier was needed to suppress the "#" that >prefixes immediates in the traditional asm syntax. This does not >actually seem to be required for AArch64: rather while a # is allowed >and still considered good style in handwritten asm code, the syntax >doesn't require it, and the compiler doesn't emit it for "i" arguments, >AFAICT.) The aarch64 one could be inherited from arch/arm/include/asm/jump_label.h (2012), which could in turn be inherited from x86 (2010). Both the constraint "i" and the modifier "c" are generic.. For -fno-pic this combination can be used for every arch. >> > The constraint "S", which denotes a symbol reference (e.g. function, >> > global variable) or label reference, is more appropriate, and has been >> > available in GCC since 2012 and in Clang since 7.0. >> > >> > Signed-off-by: Fangrui Song >> > Link: https://maskray.me/blog/2024-01-30-raw-symbol-names-in-inline-assembly >> > --- >> > arch/arm64/include/asm/jump_label.h | 8 ++++---- >> > 1 file changed, 4 insertions(+), 4 deletions(-) >> > >> > diff --git a/arch/arm64/include/asm/jump_label.h b/arch/arm64/include/asm/jump_label.h >> > index 48ddc0f45d22..31862b3bb33d 100644 >> > --- a/arch/arm64/include/asm/jump_label.h >> > +++ b/arch/arm64/include/asm/jump_label.h >> > @@ -23,9 +23,9 @@ static __always_inline bool arch_static_branch(struct static_key * const key, >> > " .pushsection __jump_table, \"aw\" \n\t" >> > " .align 3 \n\t" >> > " .long 1b - ., %l[l_yes] - . \n\t" >> > - " .quad %c0 - . \n\t" >> > + " .quad %0 - . \n\t" >> > " .popsection \n\t" >> > - : : "i"(&((char *)key)[branch]) : : l_yes); >> > + : : "S"(&((char *)key)[branch]) : : l_yes); >> >> 'key' is not used as a raw symbol name. We should make this >> >> " .quad %0 + %1 - ." >> >> and >> >> :: "S"(key), "i"(branch) :: l_yes); >> >> if we want to really clean this up. > >This hides more logic in the asm so it's arguably more cryptic >(although the code is fairly cryptic to begin with -- I don't really >see why the argument wasn't written as the equivalent >(char *)key + branch...) I agree that using "S" and "i" would introduce complexity. Using just "S" as this patch does should be clear. All of "i" "s" "S" support a symbol or label reference and a constant offset (can be zero), (in object file, a symbol and an addend; in GCC's term, the sum of a SYMBOL_REF and a CONST_INT). >Anyway, I don't think the "i" versys "S" distinction makes any >difference without -fpic or equivalent, so it is not really relevant >for the kernel (except that "S" breaks compatibility with older >compilers...) > > >I think the main advantage of "S" is that it stops you accidentally >emitting undesirable relocations from asm code that is not written for >the -fpic case. > >But just changing "i" to "S" is not sufficient to port asms to -fpic: >the asms still need to be reviewed. > > >So unless the asm has been reviewed for position-independence, it may >anyway be better to stick with "i" so that the compiler actually chokes >if someone tries to build the code with -fpic. The asm is position-independent. This `.long sym - .` is a common metadata section pattern to support PIC:) Regarding the constraints, I've updated https://maskray.me/blog/2024-01-30-raw-symbol-names-in-inline-assembly to include more details. >Since we are not trying to run arbitraily many running kernels in a >common address space (and not likely to do that), I'm not sure that we >would ever build the kernel with -fpic except for a few special-case >bits like the EFI stub and vDSO... unless I've missed something? > >If there's another reason why "S" is advantageous though, I'm happy to >be corrected. I remember that Ard has an RFC https://lore.kernel.org/linux-arm-kernel/20220427171241.2426592-1-ardb@kernel.org/ "[RFC PATCH 0/2] arm64: use PIE code generation for KASLR kernel" and see some recent PIE codegen patches. > Building the KASLR kernel without -fpie but linking it with -pie works > in practice, but it is not something that is explicitly supported by the > toolchains - it happens to work because the default 'small' code model > used by both GCC and Clang relies mostly on ADRP+ADD/LDR to generate > symbol references. I agree that current -fno-PIE with -shared -Bsymbolic linking is a hack that works as a conincidence, not guaranteed by the toolchain. This jump_label improvement (with no object file difference) fixes an obstacle.