[PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks

Sami Tolvanen samitolvanen at google.com
Mon Mar 9 15:31:56 PDT 2026


Hi Carlos,

On Mon, Mar 09, 2026 at 07:27:49PM +0000, Carlos Llamas wrote:
> On Sun, Feb 06, 2022 at 06:55:56PM -0800, Kees Cook wrote:
> > On Sun, Feb 06, 2022 at 09:28:52PM +0100, Peter Zijlstra wrote:
> > > On Sun, Feb 06, 2022 at 10:45:15AM -0800, Kees Cook wrote:
> > > 
> > > > I'm digging through the macros to sort this out, but IIUC, an example of
> > > > the problem is:
> > > > 
> > > 
> > > > so the caller is expecting "unsigned int (*)(void)" but the prototype
> > > > of __static_call_return0 is "long (*)(void)":
> > > > 
> > > > long __static_call_return0(void);
> > > > 
> > > > Could we simply declare a type-matched ret0 trampoline too?
> > > 
> > > That'll work for this case, but the next case the function will have
> > > arguments we'll need even more nonsense...
> > 
> > Shouldn't the typeof() work there too, though? I.e. as long as the
> > return value can hold a "0", it'd work.
> 
> I gave this a shot but then hit a wall with the arguments indeed:
> 
> typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries,
> 					   unsigned int cnt);
> [...]
> DEFINE_STATIC_CALL_RET0(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);
> 
> I can generate a stub with the matching return type using typeof() but
> the arguments have to be fixed e.g. to (void):
> 
> #define DEFINE_STATIC_CALL_RET0(name, _func)				\
> 	static inline typeof(((typeof(_func)*)0)())			\
> 		__static_call_ret0_##name(void) { return 0; }		\
> 	__DEFINE_STATIC_CALL(name, _func, __static_call_ret0_##name)
> 
> I believe this would work for most perf callbacks cases except the one
> above because the arguments would generate a different hash for CFI.

If you need a stub with a matching CFI type for an arbitrary function,
perhaps you can do something like this (arm64, untested):

  #define DEFINE_TYPED_STUB_RET0(name, reffunc)			\
	typeof(reffunc) name;					\
	__ADDRESSABLE(name);					\
	asm(							\
	"	" __ALIGN_STR "				\n"	\
	"	.4byte	__kcfi_typeid_" #name "		\n"	\
	#name ":					\n"	\
	"	bti c					\n"	\
	"	mov x0, xzr				\n"	\
	"	ret					"	\
	)

For the !CONFIG_CFI case, or architectures that implement static calls,
you can probably just point the stub to __static_call_return0:

  #define DEFINE_TYPED_STUB_RET0(name, reffunc) \
	typeof(reffunc) name __asm__("__static_call_return0")

Sami



More information about the linux-arm-kernel mailing list