Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp193193pxb; Thu, 27 Jan 2022 18:56:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJxyjzUgO1oTcn1hswAIO+tipQMVG3X2G+RS9/nhbOUQRjQIxjxtAsFieh32zAPduD7g28kF X-Received: by 2002:a05:6402:4256:: with SMTP id g22mr6246026edb.78.1643338600082; Thu, 27 Jan 2022 18:56:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643338600; cv=none; d=google.com; s=arc-20160816; b=hqbYiR2kGPNTJb2YCs3/7n7g9dMvk6lokQU+WbABNnBfXMMF63ICkqN92FjPGyxD9r aiUi5JOrRvMzq4/nPvC4fpzpJljoSjeg8+ro46+di0xAd4xm26ij4UCA0HX6hfB3G2XU sWCrHwpa5sHCY212MfeQZr1NOvIb9H0TXUMkgmKkU1nNVawr03++c1mG5y9HDj5/l8WT VO8qDA8nh7495bsO4KICo/j6o9HoJL+apISGOLr7iYUVIR6dq5xDqryJVf0y4vFBB/at 5mJlFzRca4c0r1FuGBQQEW+mIR9DsxeJDY9aoqZpf/Fu8Tnp0zdd+edcP2JuKiq1FX4F 79PA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=gpG5FUGC3Cpy1RC0evzp63PhRx3VOlrhGvpB+aRHchI=; b=qBG6N6cxtakiR2/Ivy8Gl1Oheu6siMl+C08nY1s6Gno3Jy7615udVqLdtkagk1aJZJ 2hqCv8A/03Q4znLwoqOjFCOPKtWspqa/eNaWGQK+azy8vTCktVvvnNbs7EC7Uz09/Q7p bU/LBxufpfYVsYojDwsfKZS7tuMcDi+KzN6gI9Po65w86eETS/OYJYoUtIPJa7CuKmyg j3Hljf2YB0joAh/XNW8zPqP556KN1q0YJuKl9mzCRDv9j9YbgO/luZhe+Wx1l010Sghb iVFTcUIUoPpeXgeYN1u04oNFSpG863mVwKcXxXI74i4Fv3fb+btgRact2ZOyc0QPnBcg pCaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rFkBWMy4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cw12si2390877ejc.992.2022.01.27.18.56.15; Thu, 27 Jan 2022 18:56:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rFkBWMy4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240139AbiA0N7r (ORCPT + 99 others); Thu, 27 Jan 2022 08:59:47 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:54524 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236884AbiA0N7q (ORCPT ); Thu, 27 Jan 2022 08:59:46 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9645261C65 for ; Thu, 27 Jan 2022 13:59:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0466FC340E4 for ; Thu, 27 Jan 2022 13:59:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643291985; bh=C8J8iNsm2mOjo9Dm060FJB/YllzVO4ruYOFz/KHhAH4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=rFkBWMy4q7FrCVE/6vBfbjwGtNmsaorxpQllsGRpplinVT38CTUEh7vKsd2Ww0Aqn 17+1kRNv6dqo30enI5q6HHbly8xsEC/VgpIDkcBsIYFyIhRdGeVB2TJPccKxfFSoPE HXQnzOc7X7Leg266+fHMbWmXgaHCenTcNHmv8yR8WVtyQp37kvN4ZYAFE0MjFBEfbY UXwmawkZct7uSK+0aj51vx4lDNNcV2M3mznaKZIt0dC/4osdBiifOMgs4D+nwWPzJj T22jGPMoWwcvQfhMr+aQAe2gmOB0lsO1NUDALy2WAYkJ6ueX/JoQ0Yi4OeAXdXVQ2F 7LSrnhWtctfZA== Received: by mail-wr1-f42.google.com with SMTP id s9so4966865wrb.6 for ; Thu, 27 Jan 2022 05:59:44 -0800 (PST) X-Gm-Message-State: AOAM530C7n98SK0jRxXbbB22zXq7qXYpgBu2jU3EbyWQsIB93iN0nc2D stUGsnN7AdQ4aFp7P18H6uI2UCgPKdK9Y9NwW20= X-Received: by 2002:a05:6000:15ca:: with SMTP id y10mr3043731wry.417.1643291983359; Thu, 27 Jan 2022 05:59:43 -0800 (PST) MIME-Version: 1.0 References: <20220124114548.30241947@gandalf.local.home> <0fa0daec-881a-314b-e28b-3828e80bbd90@linux.alibaba.com> In-Reply-To: From: Ard Biesheuvel Date: Thu, 27 Jan 2022 14:59:31 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [powerpc] ftrace warning kernel/trace/ftrace.c:2068 with code-patching selftests To: Mark Rutland Cc: Yinan Liu , Steven Rostedt , "open list:LINUX FOR POWERPC (32-BIT AND 64-BIT)" , Sachin Sant , Linux Kernel Mailing List , Kees Cook Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 27 Jan 2022 at 14:24, Mark Rutland wrote: > > On Thu, Jan 27, 2022 at 02:07:03PM +0100, Ard Biesheuvel wrote: > > On Thu, 27 Jan 2022 at 13:59, Mark Rutland wrote: > > > > > > On Thu, Jan 27, 2022 at 01:22:17PM +0100, Ard Biesheuvel wrote: > > > > On Thu, 27 Jan 2022 at 13:20, Mark Rutland wrote: > > > > > On Thu, Jan 27, 2022 at 01:03:34PM +0100, Ard Biesheuvel wrote: > > > > > > > > > > > These architectures use place-relative extables for the same reason: > > > > > > place relative references are resolved at build time rather than at > > > > > > runtime during relocation, making a build time sort feasible. > > > > > > > > > > > > arch/alpha/include/asm/extable.h:#define ARCH_HAS_RELATIVE_EXTABLE > > > > > > arch/arm64/include/asm/extable.h:#define ARCH_HAS_RELATIVE_EXTABLE > > > > > > arch/ia64/include/asm/extable.h:#define ARCH_HAS_RELATIVE_EXTABLE > > > > > > arch/parisc/include/asm/uaccess.h:#define ARCH_HAS_RELATIVE_EXTABLE > > > > > > arch/powerpc/include/asm/extable.h:#define ARCH_HAS_RELATIVE_EXTABLE > > > > > > arch/riscv/include/asm/extable.h:#define ARCH_HAS_RELATIVE_EXTABLE > > > > > > arch/s390/include/asm/extable.h:#define ARCH_HAS_RELATIVE_EXTABLE > > > > > > arch/x86/include/asm/extable.h:#define ARCH_HAS_RELATIVE_EXTABLE > > > > > > > > > > > > Note that the swap routine becomes something like the below, given > > > > > > that the relative references need to be fixed up after the entry > > > > > > changes place in the sorted list. > > > > > > > > > > > > static void swap_ex(void *a, void *b, int size) > > > > > > { > > > > > > struct exception_table_entry *x = a, *y = b, tmp; > > > > > > int delta = b - a; > > > > > > > > > > > > tmp = *x; > > > > > > x->insn = y->insn + delta; > > > > > > y->insn = tmp.insn - delta; > > > > > > ... > > > > > > } > > > > > > > > > > > > As a bonus, the resulting footprint of the table in the image is > > > > > > reduced by 8x, given that every 8 byte pointer has an accompanying 24 > > > > > > byte RELA record, so we go from 32 bytes to 4 bytes for every call to > > > > > > __gnu_mcount_mc. > > > > > > > > > > Absolutely -- it'd be great if we could do that for the callsite locations; the > > > > > difficulty is that the entries are generated by the compiler itself, so we'd > > > > > either need some build/link time processing to convert each absolute 64-bit > > > > > value to a relative 32-bit offset, or new compiler options to generate those as > > > > > relative offsets from the outset. > > > > > > > > Don't we use scripts/recordmcount.pl for that? > > > > > > Not quite -- we could adjust it to do so, but today it doesn't consider > > > existing mcount_loc entries, and just generates new ones where the compiler has > > > generated calls to mcount, which it finds by scanning the instructions in the > > > binary. Today it is not used: > > > > > > * On arm64 when we default to using `-fpatchable-function-entry=N`. That makes > > > the compiler insert 2 NOPs in the function prologue, and log the location of > > > that NOP sled to a section called. `__patchable_function_entries`. > > > > > > We need the compiler to do that since we can't reliably identify 2 NOPs in a > > > function prologue as being intended to be a patch site, as e.g. there could > > > be notrace functions where the compiler had to insert NOPs for alignment of a > > > subsequent brnach or similar. > > > > > > * On architectures with `-nop-mcount`. On these, it's necessary to use > > > `-mrecord-mcount` to have the compiler log the patch-site, for the same > > > reason as with `-fpatchable-function-entry`. > > > > > > * On architectures with `-mrecord-mcount` generally, since this avoids the > > > overhead of scanning each object. > > > > > > * On x86 when objtool is used. > > > > > > > Right. > > > > I suppose that on arm64, we can work around this by passing > > --apply-dynamic-relocs to the linker, so that all R_AARCH64_RELATIVE > > targets are prepopulated with the link time value of the respective > > addresses. It does cause some bloat, which is why we disable that > > today, but we could make that dependent on ftrace being enabled. > > We'd also need to teach the build-time sort to update the relocations, unless > you mean to also change the boot-time reloc code to RMW with the offset? > Why would that be necessary? Every RELA entry has the same effect on its target address, as it just adds a fixed offset. > I think for right now the best thing is to disable the build-time sort for > arm64, but maybe something like that is the right thing to do longer term. > Fair enough. > > I do wonder how much over head we accumulate, though, by having all > > these relocations, but I suppose that is the situation today in any > > case. > > Yeah; I suspect if we want to do something about that we want to do it more > generally, and would probably need to do something like the x86 approach and > rewrite the relocs at build-time to something more compressed. If we applied > the dynamic relocs with the link-time address we'd only need 4 bytes to > identify each pointer to apply an offset to. > > I'm not exactly sure how we could do that, nor what the trade-off look like in > practice. > It would make sense for -fpic codegen to use relative offsets in __mcount_loc, but since we don't actually use -fpic on arm64, that doesn't really help :-)