From patchwork Wed Aug 21 14:43:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13771697 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6362E1B1D50 for ; Wed, 21 Aug 2024 14:43:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724251405; cv=none; b=YGHdhxY6n2Ul88QZjyoNaNakVp++f2Cp4Tz3jW2gfeuYCf/Rj+R9CAkxT5LQ+xjTEAbf1CZba/+5qyzHg+/okV29lqMWQh/MCwOAcaGUFYG13qP8nJ+u8eXnLNUNL+Tp7pxg03bKmmPJmOhuG1DWBHbVzqoNq7nBgGaGAtxRhJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724251405; c=relaxed/simple; bh=4FmpeBAoNG/9XaYQU9yKn+nz6rX9KfOZV11sxbb0Z6M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ayu7ercJX4kPSVmsNM5MUqOc5/FgWim7YfL/2S35S5dWu5aKoXHm6TEKNgqV3rDM0q8ShzBcLr9D+mupPiOrNVNZQTwNIKw8ONzmDIxyDEZFOBdlqE7SmfPl2E+WIWk/Q5Wcg+KxJoSOMgJefXgJGVAXXw/xYXGILTUpStBshO4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=N7lv+TK/; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="N7lv+TK/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724251404; x=1755787404; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4FmpeBAoNG/9XaYQU9yKn+nz6rX9KfOZV11sxbb0Z6M=; b=N7lv+TK/fQDfIGjPYky1nsYydzGGou2rFr6m0tDXAd0ezGTJs+H5/9sP u5547IjtxvGdMy/gnfNqjZgdVrmsaTe4A8M5JQlszk4j+LI8MKSL33l2o hD7yy39IubRVUdpi3qwL4PAnRH3Pri++h2AjIB8Yh2Y8gym5/yXMzMAZW ERBWDpFqHFY7jYZhMZnG/N1P1zxgWAnWzuWLcy8bv5F7mN2VKgwtiTOVU yhj3vEFDKqHvgsy5ovRYvuVozVDw7TUfAmloCOaJwnsrHFDAMeXv7/74B wzqUeZNbSrDS3co+KgOUDTNs0ySghBYyiCUgrk4t0edKjfihZnDAesEIo Q==; X-CSE-ConnectionGUID: m3X4Pi5tQO2c78j9Lem5bw== X-CSE-MsgGUID: bAIHj9iCRHS8Ux61fs9TDw== X-IronPort-AV: E=McAfee;i="6700,10204,11171"; a="22750525" X-IronPort-AV: E=Sophos;i="6.10,164,1719903600"; d="scan'208";a="22750525" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 07:43:23 -0700 X-CSE-ConnectionGUID: 7ieikMQFSz6j3WGnrLP2eQ== X-CSE-MsgGUID: mqeOvKHqSAKMM76O4y+1rA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,164,1719903600"; d="scan'208";a="65316490" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.19.248]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 07:43:21 -0700 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , Daniel Latypov , David Gow , Lucas De Marchi Subject: [PATCH 1/4] kunit: Introduce kunit_is_running() Date: Wed, 21 Aug 2024 16:43:02 +0200 Message-Id: <20240821144305.1958-2-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240821144305.1958-1-michal.wajdeczko@intel.com> References: <20240821144305.1958-1-michal.wajdeczko@intel.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Wrap uses of the static key 'kunit_running' into a helper macro to allow future checks to be placed in the code residing outside of the CONFIG_KUNIT. We will start using this in upcoming patch. Signed-off-by: Michal Wajdeczko Reviewed-by: Rae Moar Reviewed-by: Lucas De Marchi Reviewed-by: David Gow --- Cc: Daniel Latypov Cc: David Gow Cc: Lucas De Marchi --- include/kunit/test-bug.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/kunit/test-bug.h b/include/kunit/test-bug.h index 47aa8f21ccce..e8ea3bab7250 100644 --- a/include/kunit/test-bug.h +++ b/include/kunit/test-bug.h @@ -25,6 +25,13 @@ extern struct kunit_hooks_table { void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr); } kunit_hooks; +/** + * kunit_is_running() - True, if KUnit test is currently running. + * + * If CONFIG_KUNIT is not enabled, it will compile down to a false. + */ +#define kunit_is_running() static_branch_unlikely(&kunit_running) + /** * kunit_get_current_test() - Return a pointer to the currently running * KUnit test. @@ -40,7 +47,7 @@ extern struct kunit_hooks_table { */ static inline struct kunit *kunit_get_current_test(void) { - if (!static_branch_unlikely(&kunit_running)) + if (!kunit_is_running()) return NULL; return current->kunit_test; @@ -53,7 +60,7 @@ static inline struct kunit *kunit_get_current_test(void) * If a KUnit test is running in the current task, mark that test as failed. */ #define kunit_fail_current_test(fmt, ...) do { \ - if (static_branch_unlikely(&kunit_running)) { \ + if (kunit_is_running()) { \ /* Guaranteed to be non-NULL when kunit_running true*/ \ kunit_hooks.fail_current_test(__FILE__, __LINE__, \ fmt, ##__VA_ARGS__); \ @@ -64,6 +71,7 @@ static inline struct kunit *kunit_get_current_test(void) static inline struct kunit *kunit_get_current_test(void) { return NULL; } +#define kunit_is_running() false #define kunit_fail_current_test(fmt, ...) do {} while (0) #endif From patchwork Wed Aug 21 14:43:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13771698 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B05519993B for ; Wed, 21 Aug 2024 14:43:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724251406; cv=none; b=CI+Uuxx9pS+9uy8T/aBTDo1mEb+jvxmEOri0bMwGY6uxBU4NiO5ljkJuKO2TRU/DTi8oXYR2ZKD8Ahx/caJa2YUxGc5EOBD1QB/vLjhNPgHs9ZGuTEOzhdlSNRT7lSshJI4Dx0Pa+x4LOzPMosJSaR8dk686LiZINRf06YcRlUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724251406; c=relaxed/simple; bh=hHZuXR4ttBpdzttniC8/ixePUZct9Us1CSGNSWRzPok=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mtu6dpy7YK9uO4aj8Q4Odan8y5V5S69Owa65G2qdT/sawO5if99edIiHWJgade2SI8Et5HWTjXddps5AElubM+NFCqdwCpDf3F7oTu+qXL0er5cMNc/bONzPupArLIvuEca72ElyjTW4UxHTyiU8et1XyWghVqWzWg8UxiDE3Sk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=CGK0gIzr; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CGK0gIzr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724251405; x=1755787405; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hHZuXR4ttBpdzttniC8/ixePUZct9Us1CSGNSWRzPok=; b=CGK0gIzrZpV74ebQCQhIpS3dUHQdGadMYUaTtM/ppDJo8Zo8EMqUfIbv S+AQmn9A4MniXPuALHPeQx2hoRrE2xcOjzyiIld2+pnHJ8zn520qTjiv0 eceNIex3fBVXZ8QL/6IKAz4JZaaRRbR7Kc6MjwDKpZTxlbBwce1n33ZKv BYFlfI0NYTo4Y94kr/bd8R+Jsj9RnTXxK/yGcKNT47bul6YOeHvBQl6c9 Gztk9Z7UFyWwKo1VoIumrAVOe3RW/hUfq7VRZzKLzPM1rJQOSysUmigvq 05aoa5tH0tLUaiIlakmvy8Ufq/szP7Y43QJhunyg19maPwqS+YZbM43zT w==; X-CSE-ConnectionGUID: SQXzMvOjS+SNG3tCo8vVyw== X-CSE-MsgGUID: krc8QwXTTfOIuXB9bmsj2A== X-IronPort-AV: E=McAfee;i="6700,10204,11171"; a="22750534" X-IronPort-AV: E=Sophos;i="6.10,164,1719903600"; d="scan'208";a="22750534" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 07:43:25 -0700 X-CSE-ConnectionGUID: tjy0aE1XTvyNXI6NKyDe2w== X-CSE-MsgGUID: FZjujS2PT2q+mqFmBkgdgA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,164,1719903600"; d="scan'208";a="65316512" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.19.248]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 07:43:23 -0700 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , Rae Moar , David Gow , Lucas De Marchi Subject: [PATCH 2/4] kunit: Add macro to conditionally expose declarations to tests Date: Wed, 21 Aug 2024 16:43:03 +0200 Message-Id: <20240821144305.1958-3-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240821144305.1958-1-michal.wajdeczko@intel.com> References: <20240821144305.1958-1-michal.wajdeczko@intel.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The DECLARE_IF_KUNIT macro will introduces identifiers only if CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled no identifiers from the param list will be defined. Signed-off-by: Michal Wajdeczko Reviewed-by: Rae Moar Reviewed-by: David Gow --- Cc: Rae Moar Cc: David Gow Cc: Lucas De Marchi --- include/kunit/visibility.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/kunit/visibility.h b/include/kunit/visibility.h index 0dfe35feeec6..1c23773f826c 100644 --- a/include/kunit/visibility.h +++ b/include/kunit/visibility.h @@ -11,6 +11,13 @@ #define _KUNIT_VISIBILITY_H #if IS_ENABLED(CONFIG_KUNIT) + /** + * DECLARE_IF_KUNIT - A macro that introduces identifiers only if + * CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled + * no identifiers will be defined. + * @body: identifiers to be introduced conditionally + */ + #define DECLARE_IF_KUNIT(body...) body /** * VISIBLE_IF_KUNIT - A macro that sets symbols to be static if * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled @@ -26,6 +33,7 @@ #define EXPORT_SYMBOL_IF_KUNIT(symbol) EXPORT_SYMBOL_NS(symbol, \ EXPORTED_FOR_KUNIT_TESTING) #else + #define DECLARE_IF_KUNIT(body...) #define VISIBLE_IF_KUNIT static #define EXPORT_SYMBOL_IF_KUNIT(symbol) #endif From patchwork Wed Aug 21 14:43:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13771699 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EB5D19993B for ; Wed, 21 Aug 2024 14:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724251409; cv=none; b=a6hQ6wz1UyMBAWGyTa8S96DqXOJsgKummVIZLvD+7cbFaIR5bl0LOLqQfJo4PY117yQz/9iFfuNowPygebc623hQPCunCuvzp63sOHZ/r46ClAJOzvp1PRy3J6gHPrqmZ8bGPsdeBsyPW3XRfMJfn6wZzzYFnWDeHUetOibp5sw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724251409; c=relaxed/simple; bh=3SZx5oLD0aJ4PvapkZ0DRAstmTMAUxI9TaLjFWCmy2w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pbMLr2zykcGhW0lOsfshBN4VM2+f7qe8hg5NtCNi5n7hhrJR5hzd8w2mKGx4Ws+g6rJyvAaKZNDv/wqYEptR1h2UfS8zC3/3MshRivhRaIzKvjQBPZJJPPA9VbYjIPT9b9R3WFV3Sxlh54VX44piqlBxcYgFjiix3IEeXl/u9gU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=CnIOP87p; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CnIOP87p" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724251408; x=1755787408; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3SZx5oLD0aJ4PvapkZ0DRAstmTMAUxI9TaLjFWCmy2w=; b=CnIOP87pm62v0ad5QNP9jTlW+QdmQHo8TzHj4ph3TFXRTu4M22AAFQzo v3B/gD28oIhvndBgUk9pMOxDFF416kSGmObjfjhHadfER3b+PG32QmcJ8 NsnbLSqYBDHJyizujxT/Zslt20QyRLBZUl2yeA2YsF1WEeeoTBkUlV8UH cx1VmLK06W22g/3ZHTXrffP/kg1Crk5gKszF4uiba7QVWkFR6aiGwNOgH PHo2BSalqKIQ906soLSC08Xtt5ViLQS9eM2UZNAcN3I56b1YQGvxdF2ct EDUXljAZ3p0eG3mL0aNXDy1sGs/ciOeOCKbnlJfyuQj8Ej808oLX5j+2+ Q==; X-CSE-ConnectionGUID: /ym2HqV0TzqxJIaDUbV1CA== X-CSE-MsgGUID: EuFk4/OnQo6bFTJL15FMSw== X-IronPort-AV: E=McAfee;i="6700,10204,11171"; a="22750554" X-IronPort-AV: E=Sophos;i="6.10,164,1719903600"; d="scan'208";a="22750554" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 07:43:27 -0700 X-CSE-ConnectionGUID: U7uf/UzeRmiV/mASgTS2jQ== X-CSE-MsgGUID: 5mnxGtHHRQqudF/94NwybQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,164,1719903600"; d="scan'208";a="65316532" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.19.248]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 07:43:25 -0700 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , David Gow , Daniel Latypov , Lucas De Marchi Subject: [PATCH 3/4] kunit: Allow function redirection outside of the KUnit thread Date: Wed, 21 Aug 2024 16:43:04 +0200 Message-Id: <20240821144305.1958-4-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240821144305.1958-1-michal.wajdeczko@intel.com> References: <20240821144305.1958-1-michal.wajdeczko@intel.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently, the 'static stub' API only allows function redirection for calls made from the kthread of the current test, which prevents the use of this functionalty when the tested code is also used by other threads, outside of the KUnit test, like from the workqueue. Add another set of macros to allow redirection to the replacement functions, which, unlike the KUNIT_STATIC_STUB_REDIRECT, will affect all calls done during the test execution. Signed-off-by: Michal Wajdeczko --- Cc: David Gow Cc: Daniel Latypov Cc: Lucas De Marchi --- include/kunit/static_stub.h | 80 +++++++++++++++++++++++++++++++++++++ lib/kunit/static_stub.c | 21 ++++++++++ 2 files changed, 101 insertions(+) diff --git a/include/kunit/static_stub.h b/include/kunit/static_stub.h index bf940322dfc0..3dd98c8f3f1f 100644 --- a/include/kunit/static_stub.h +++ b/include/kunit/static_stub.h @@ -12,6 +12,7 @@ /* If CONFIG_KUNIT is not enabled, these stubs quietly disappear. */ #define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...) do {} while (0) +#define KUNIT_FIXED_STUB_REDIRECT(stub, args...) do {} while (0) #else @@ -109,5 +110,84 @@ void __kunit_activate_static_stub(struct kunit *test, */ void kunit_deactivate_static_stub(struct kunit *test, void *real_fn_addr); +/** + * KUNIT_FIXED_STUB_REDIRECT() - Call a fixed function stub if activated. + * @stub: The location of the function stub pointer + * @args: All of the arguments passed to this stub + * + * This is a function prologue which is used to allow calls to the current + * function to be redirected if a KUnit is running. If the stub is NULL or + * the KUnit is not running the function will continue execution as normal. + * + * The function stub pointer must be stored in a place that is accessible both + * from the test code that will activate this stub and from the function where + * we will do the redirection. + * + * Unlike the KUNIT_STATIC_STUB_REDIRECT(), this redirection will work + * even if the caller is not in a KUnit context (like a worker thread). + * + * Example: + * + * .. code-block:: c + * + * static int (*func_stub)(int n); + * + * int real_func(int n) + * { + * KUNIT_FIXED_STUB_REDIRECT(func_stub, n); + * return n + 1; + * } + * + * int replacement_func(int n) + * { + * return n + 100; + * } + * + * void example_test(struct kunit *test) + * { + * KUNIT_EXPECT_EQ(test, real_func(1), 2); + * func_stub = replacement_func; + * KUNIT_EXPECT_EQ(test, real_func(1), 101); + * } + */ +#define KUNIT_FIXED_STUB_REDIRECT(stub, args...) do { \ + typeof(stub) replacement = (stub); \ + if (kunit_is_running()) { \ + if (unlikely(replacement)) { \ + pr_info(KUNIT_SUBTEST_INDENT "# %s: calling stub %ps\n", \ + __func__, replacement); \ + return replacement(args); \ + } \ + } \ +} while (0) + +void __kunit_activate_fixed_stub(struct kunit *test, void *stub_ptr, void *replacement_func); + +/** + * kunit_activate_fixed_stub() - Setup a fixed function stub. + * @test: Test case that wants to activate a fixed function stub + * @stub: The location of the function stub pointer + * @replacement: The replacement function + * + * This helper setups a function stub with the replacement function. + * It will also automatically restore stub to NULL at the test end. + */ +#define kunit_activate_fixed_stub(test, stub, replacement) do { \ + typecheck_pointer(stub); \ + typecheck_fn(typeof(stub), replacement); \ + typeof(stub) *stub_ptr = &(stub); \ + __kunit_activate_fixed_stub((test), stub_ptr, (replacement)); \ +} while (0) + +/** + * kunit_deactivate_fixed_stub() - Disable a fixed function stub. + * @test: Test case that wants to deactivate a fixed function stub (unused for now) + * @stub: The location of the function stub pointer + */ +#define kunit_deactivate_fixed_stub(test, stub) do { \ + typecheck(struct kunit *, (test)); \ + (stub) = NULL; \ +} while (0) + #endif #endif diff --git a/lib/kunit/static_stub.c b/lib/kunit/static_stub.c index 92b2cccd5e76..1b50cf457e89 100644 --- a/lib/kunit/static_stub.c +++ b/lib/kunit/static_stub.c @@ -121,3 +121,24 @@ void __kunit_activate_static_stub(struct kunit *test, } } EXPORT_SYMBOL_GPL(__kunit_activate_static_stub); + +static void nullify_stub_ptr(void *data) +{ + void **ptr = data; + + *ptr = NULL; +} + +/* + * Helper function for kunit_activate_static_stub(). The macro does + * typechecking, so use it instead. + */ +void __kunit_activate_fixed_stub(struct kunit *test, void *stub_ptr, void *replacement_func) +{ + void **stub = stub_ptr; + + KUNIT_ASSERT_NOT_NULL(test, stub_ptr); + *stub = replacement_func; + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test, nullify_stub_ptr, stub_ptr)); +} +EXPORT_SYMBOL_GPL(__kunit_activate_fixed_stub); From patchwork Wed Aug 21 14:43:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13771700 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A90E1B250D for ; Wed, 21 Aug 2024 14:43:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724251411; cv=none; b=QrwK5U6usshtL20WmLSw+l916jCE662hyo+HA3HMouk9HlCCMP6wjumlttrHHrB31sLP4aSq/at+EZ1mdL8Hhmxc+rN8TheuMwlhcy7tvgJtQAYLXsYe9jmVwXLA9iRRRbBUpVxHbZDfhbKnxuOmGd9mGq7M4ZA4KXZqPvagEEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724251411; c=relaxed/simple; bh=BSD73sM2L6z9u+zRHPMws6dQC0WsD14nix7MPD6WDSs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eGqntBOlkkqB4b3FIERtKGv2Ur4qnGPX6WdAqXWjboaFu/urT844fMlJAkQ2FsTwmDYutQOTfJaA8TUFarjiS5r1a/sIFTSZfvk2YJN/dBf+1yDqCPnww7NfRFNph/cZlZ7UAuOsgjPLAMHOyu6IvW8q+J/uHbV+whXX2IID8ds= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Ji/oo6lh; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Ji/oo6lh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724251410; x=1755787410; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BSD73sM2L6z9u+zRHPMws6dQC0WsD14nix7MPD6WDSs=; b=Ji/oo6lhb3l86Ujev7gU5JE2r+NNgTacurOqrgKferulj+NV1HB4f9fY EH8JMgdP0ePwb6WtBUDaeXwqhKhbvXgjWvQe+BPLi6bb2/43GgrfRPbnK O862JwvRvHitYwy+ot1ODMXPdJ1KUrkTE/9H30MeUXgognLpKDZy0SGs5 a+Lbk8xpKHif+7vbyAD1ZlpR79r0DE7uimgJdTMKjM2+IeuZlJ4WpNiWK dna8meVA1oFgaPxgRlolG37covmRghY39j0zdVIDwJ83wkazZQOBBIGGd eyQXtigtgGvCsZVAjvYXAUntqEAVniAYpkevS/J6bTHUqm74Xx7uXZyhR w==; X-CSE-ConnectionGUID: /VjX3t2iRkSc/FbtqK0hsg== X-CSE-MsgGUID: zDf/FF63Rk+LRtyPkuViYA== X-IronPort-AV: E=McAfee;i="6700,10204,11171"; a="22750560" X-IronPort-AV: E=Sophos;i="6.10,164,1719903600"; d="scan'208";a="22750560" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 07:43:29 -0700 X-CSE-ConnectionGUID: xyJBUGMJSj6ukId+akVnDQ== X-CSE-MsgGUID: qiRKFl01TE+fQcSz/bl/sg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,164,1719903600"; d="scan'208";a="65316542" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.19.248]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 07:43:27 -0700 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , David Gow , Daniel Latypov , Lucas De Marchi Subject: [PATCH 4/4] kunit: Add example with alternate function redirection method Date: Wed, 21 Aug 2024 16:43:05 +0200 Message-Id: <20240821144305.1958-5-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240821144305.1958-1-michal.wajdeczko@intel.com> References: <20240821144305.1958-1-michal.wajdeczko@intel.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add example how to use KUNIT_FIXED_STUB_REDIRECT and compare its usage with the KUNIT_STATIC_STUB_REDIRECT. Also show how the DECLARE_IF_KUNIT macro could be helpful in declaring test data in the non-test data structures. Signed-off-by: Michal Wajdeczko Reviewed-by: Rae Moar Reviewed-by: David Gow --- Cc: David Gow Cc: Daniel Latypov Cc: Lucas De Marchi --- lib/kunit/kunit-example-test.c | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 3056d6bc705d..120e08d8899b 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -6,8 +6,10 @@ * Author: Brendan Higgins */ +#include #include #include +#include /* * This is the most fundamental element of KUnit, the test case. A test case @@ -221,6 +223,66 @@ static void example_static_stub_using_fn_ptr_test(struct kunit *test) KUNIT_EXPECT_EQ(test, add_one(1), 2); } +/* This could be a location of various fixed stub functions. */ +static struct { + DECLARE_IF_KUNIT(int (*add_two)(int i)); +} stubs; + +/* This is a function we'll replace with stubs. */ +static int add_two(int i) +{ + /* This will trigger the stub if active. */ + KUNIT_STATIC_STUB_REDIRECT(add_two, i); + KUNIT_FIXED_STUB_REDIRECT(stubs.add_two, i); + + return i + 2; +} + +struct add_two_async_work { + struct work_struct work; + int param; + int result; +}; + +static void add_two_async_func(struct work_struct *work) +{ + struct add_two_async_work *w = container_of(work, typeof(*w), work); + + w->result = add_two(w->param); +} + +static int add_two_async(int i) +{ + struct add_two_async_work w = { .param = i }; + + INIT_WORK_ONSTACK(&w.work, add_two_async_func); + schedule_work(&w.work); + flush_work(&w.work); + destroy_work_on_stack(&w.work); + + return w.result; +} + +/* + */ +static void example_fixed_stub_test(struct kunit *test) +{ + /* static stub redirection works only for KUnit thread */ + kunit_activate_static_stub(test, add_two, subtract_one); + KUNIT_EXPECT_EQ(test, add_two(1), subtract_one(1)); + KUNIT_EXPECT_NE_MSG(test, add_two_async(1), subtract_one(1), + "stub shouldn't be active outside KUnit thread!"); + kunit_deactivate_static_stub(test, add_two); + KUNIT_EXPECT_EQ(test, add_two(1), add_two(1)); + + /* fixed stub redirection works for KUnit and other threads */ + kunit_activate_fixed_stub(test, stubs.add_two, subtract_one); + KUNIT_EXPECT_EQ(test, add_two(1), subtract_one(1)); + KUNIT_EXPECT_EQ(test, add_two_async(1), subtract_one(1)); + kunit_deactivate_fixed_stub(test, stubs.add_two); + KUNIT_EXPECT_EQ(test, add_two(1), add_two(1)); +} + static const struct example_param { int value; } example_params_array[] = { @@ -294,6 +356,7 @@ static struct kunit_case example_test_cases[] = { KUNIT_CASE(example_all_expect_macros_test), KUNIT_CASE(example_static_stub_test), KUNIT_CASE(example_static_stub_using_fn_ptr_test), + KUNIT_CASE(example_fixed_stub_test), KUNIT_CASE(example_priv_test), KUNIT_CASE_PARAM(example_params_test, example_gen_params), KUNIT_CASE_SLOW(example_slow_test),