Received: by 2002:a05:7412:d008:b0:f9:6acb:47ec with SMTP id bd8csp228096rdb; Tue, 19 Dec 2023 15:01:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXmq7hrE6jFxK7rVYcdOafJSP2ZeHUDAd0/xqT65JbNSwkxmq2+IxxVpUhvS8m4RBsjD5R X-Received: by 2002:a05:622a:64a:b0:425:4043:5f1c with SMTP id a10-20020a05622a064a00b0042540435f1cmr29911498qtb.90.1703026910363; Tue, 19 Dec 2023 15:01:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703026910; cv=none; d=google.com; s=arc-20160816; b=aUxfJA5vjMR0551ExVNy1OiOBjX7M/aKhkkwCgdjkXxX6eM7oB9fj6WYHXZ0uUuSDd f04QPlE0XCZugVuqdQHUMGDgh5xii9MRZRhEQxCQeuTNzMjdjJ/oFQ30//qhBTZbYCoD eYM2MJzBjTEuAUO76u8JoFE7k46g5xJnAab4DECozBwNlmkYbbwrAmlDHP1dR6uI5Bh6 igDwY5QPaWwM0j//U4ph2qyUUEmoQK0Bu6+gtoyaR43ywRhhbdSajxvIv+OUnZbq/EqZ siqWOpPYdoCjVhKqJEho/XzfkfLzDJBpPcXpSxklUHCSrytTBCaGB2pbHOMOMvlPMqb3 VPHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=tJBKIVMxwZw2B/0wRnInm6AhoZ+wrkkWV1uAdEbTnzk=; fh=VgtnJUKslQWC1J4hSraUCaGGwBrlwsv9kzwfY7I3kHA=; b=w6LxM4oeGwrB7/dPgxnMecQUAPgii/zAvN/ex+CKTb7IvycZkiLYxfS7INRFINQ/AT lobrnBHIQUj5+2bt8un3255BpWU+QFgKs9kIwV2AY1ihUMkYUZ0oaHod7MPymZ0H4UIY qIk3Ua5xxJze4cIZIxS97qSAYjvVxcIalMSQKdbUeMekj5Y+tSgKy/HYqeUY0yl+x8io fpivEF6HaVUQwdlFaWE97bc8QlAJd/SoTTBlMxEN2IoTnkq3D38xVSgZblbIjL/pgNaf VzZAUoIxtNA+2A1JGCF/JySGSDW6d3PVr8Asgf0+LfQYXWoxQcxc6QBbSte8cVK6AHUY iKhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="0Abku/Gk"; spf=pass (google.com: domain of linux-kernel+bounces-6075-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6075-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 fi3-20020a05622a58c300b004237f64dec8si18697053qtb.528.2023.12.19.15.01.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 15:01:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-6075-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=@google.com header.s=20230601 header.b="0Abku/Gk"; spf=pass (google.com: domain of linux-kernel+bounces-6075-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6075-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 0E88C1C23F53 for ; Tue, 19 Dec 2023 23:01:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58D7F3D0C5; Tue, 19 Dec 2023 23:01:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0Abku/Gk" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (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 3F4313D0A4 for ; Tue, 19 Dec 2023 23:01:41 +0000 (UTC) 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-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-552eaf800abso5610a12.0 for ; Tue, 19 Dec 2023 15:01:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1703026899; x=1703631699; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=tJBKIVMxwZw2B/0wRnInm6AhoZ+wrkkWV1uAdEbTnzk=; b=0Abku/GkVyr5XL2raqIIZ1Dxk+/90pFJFIsjfG0yYdzENMDN0GaflT2vFbhqEH7m9B CbE8S5mrjt8IeKGj4sBmWP3InbOHuFxxahg/fT8lrW0sEm/cq4WzLNBDs6GvuCOEU8BE Z0saMzk51JOlWsT5clszgm8oUorw0+z9F/KEY0pCISiYFIe6IyHiSB2/PIDgCOiJWQ6s Pp69r5lTILwT3Gv8LZhzNOPDAEVTxI5mPW8+dTvR5j/txoRaFYEfLXj0rdl2uxhX2EAN 27+W4GLlSwRjce4Y+hrT9psYsfWyhN8kwN6xvv2MVrvfRA2ZciIiT0uQLs9l1aAxagtN btFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703026899; x=1703631699; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tJBKIVMxwZw2B/0wRnInm6AhoZ+wrkkWV1uAdEbTnzk=; b=VELVAz/BKAZZcJ384eFYvBBk5ddQ34l1QRZRMONQ6K8L8Rcz5XiSKSOTLu4/FDecUa AA7YEwqzW1N8J+rmKGKWQo6XcD4k0poQ3MzOCseIRqOcJNpBj9qD5uAcKea5yhq64eTS cWX5MddCbZ5Bb3wlFwg0cYsDJQtOtXYiLr/XZyO6dbS2bxk/0ObwgiiBk6DvRaiTVkjt YQQsZn0pEtB87jr6+GRB1N9h5LeXSbIfVDOL50C2rfRHP/hTE7LzdFgjG0wN9VTTZ9Rp Bw43Z69tLrHea96Ch7W47RxSUQPt4nzMfhhd5I8Fo+XUnkD26VsWC2wjLGZEUnMQrBar Gm8g== X-Gm-Message-State: AOJu0YwJaJyI4cv/ORTOaGA1JZ+Tp31MpWlyMeQwo9sVVrjQ7471fDI8 IXr6+w+kivpax3Nzogzg6+0r7aNHcrMtnllBCPzib4rapKkL X-Received: by 2002:a50:c11a:0:b0:553:6de7:43d7 with SMTP id l26-20020a50c11a000000b005536de743d7mr41886edf.6.1703026899366; Tue, 19 Dec 2023 15:01:39 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231218161044.215640-1-rf@opensource.cirrus.com> In-Reply-To: <20231218161044.215640-1-rf@opensource.cirrus.com> From: Rae Moar Date: Tue, 19 Dec 2023 18:01:27 -0500 Message-ID: Subject: Re: [PATCH 1/2] kunit: Allow passing function pointer to kunit_activate_static_stub() To: Richard Fitzgerald Cc: brendan.higgins@linux.dev, davidgow@google.com, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, patches@opensource.cirrus.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Dec 18, 2023 at 11:10=E2=80=AFAM Richard Fitzgerald wrote: > > Swap the arguments to typecheck_fn() in kunit_activate_static_stub() > so that real_fn_addr can be either the function itself or a pointer > to that function. > > This is useful to simplify redirecting static functions in a module. > Having to pass the actual function meant that it must be exported > from the module. Either making the 'static' and EXPORT_SYMBOL*() > conditional (which makes the code messy), or change it to always > exported (which increases the export namespace and prevents the > compiler inlining a trivial stub function in non-test builds). > > With the original definition of kunit_activate_static_stub() the > address of real_fn_addr was passed to typecheck_fn() as the type to > be passed. This meant that if real_fn_addr was a pointer-to-function > it would resolve to a ** instead of a *, giving an error like this: > > error: initialization of =E2=80=98int (**)(int)=E2=80=99 from incompat= ible pointer > type =E2=80=98int (*)(int)=E2=80=99 [-Werror=3Dincompatible-pointer-ty= pes] > kunit_activate_static_stub(test, add_one_fn_ptr, subtract_one); > | ^~~~~~~~~~~~ > ./include/linux/typecheck.h:21:25: note: in definition of macro > =E2=80=98typecheck_fn=E2=80=99 > 21 | ({ typeof(type) __tmp =3D function; \ > > Swapping the arguments to typecheck_fn makes it take the type of a > pointer to the replacement function. Either a function or a pointer > to function can be assigned to that. For example: > > static int some_function(int x) > { > /* whatever */ > } > > int (* some_function_ptr)(int) =3D some_function; > > static int replacement(int x) > { > /* whatever */ > } > > Then: > kunit_activate_static_stub(test, some_function, replacement); > yields: > typecheck_fn(typeof(&replacement), some_function); > > and: > kunit_activate_static_stub(test, some_function_ptr, replacement); > yields: > typecheck_fn(typeof(&replacement), some_function_ptr); > > The two typecheck_fn() then resolve to: > > int (*__tmp)(int) =3D some_function; > and > int (*__tmp)(int) =3D some_function_ptr; > > Both of these are valid. In the first case the compiler inserts > an implicit '&' to take the address of the supplied function, and > in the second case the RHS is already a pointer to the same type. > > Signed-off-by: Richard Fitzgerald Hello! This seems fine to me. I have tested it and the reasoning behind this seems sensible. However, let's see what David thinks when he returns to office as he is the expert on static stubbing. Reviewed-by: Rae Moar -Rae > --- > include/kunit/static_stub.h | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/include/kunit/static_stub.h b/include/kunit/static_stub.h > index 85315c80b303..bf940322dfc0 100644 > --- a/include/kunit/static_stub.h > +++ b/include/kunit/static_stub.h > @@ -93,7 +93,7 @@ void __kunit_activate_static_stub(struct kunit *test, > * The redirection can be disabled again with kunit_deactivate_static_st= ub(). > */ > #define kunit_activate_static_stub(test, real_fn_addr, replacement_addr)= do { \ > - typecheck_fn(typeof(&real_fn_addr), replacement_addr); = \ > + typecheck_fn(typeof(&replacement_addr), real_fn_addr); = \ > __kunit_activate_static_stub(test, real_fn_addr, replacement_addr= ); \ > } while (0) > > -- > 2.30.2 >