From patchwork Mon Aug 26 22:20:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13778587 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 E4E4019AA56 for ; Mon, 26 Aug 2024 22:20:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710836; cv=none; b=D8ZSr8Y9vzrmHYDmh/HCZY8UzulegbsYrcfdlH8jTwBIdz7imiEykCzy+anE3lJhtkp/MHGgIaeeVNQdArmqxU2/xTo8aN3kA5/gySRVFaPXD68o1/IKC6zPIAeLZUm1DvdeuOkRYPJVd7b5x+jSR8nAQ9iV+rnp9B8BJ5P+gdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710836; c=relaxed/simple; bh=DHjTZDh5IupglN5LpuQT50PBCXxQfXP83MHvVKsDzEg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mQFdzK9zw8Cp8IBIHJsrvxtXjpxAmTN7Qp4xmsGRqSCxcYCxIt+iQR06cn/UOTZpsCYMnIHg9tArgctiSbouUs5pbRMyyRyFNnlbLUsfjDML+X+nNeH+h6zShukU7dcU0XYnaKAV+9VJojd/jsX5IgVJe2vEoXlY66yMcpj6dHM= 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=g/jjgN+p; arc=none smtp.client-ip=198.175.65.12 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="g/jjgN+p" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724710835; x=1756246835; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DHjTZDh5IupglN5LpuQT50PBCXxQfXP83MHvVKsDzEg=; b=g/jjgN+pPwXOOX/CozEKFvZgA5HDgmT+a+KHDj8gzomAbHSV5mAReGKy 3av+tVaPDZ9bWN/4R0WcGMR8QzCJRWbD4J4oGWgpCWxay+Lt7BBvGKPz6 CZtfCoODA65gr4TLpxD32VMr5bMBcR4wbkTBt0kOmvfjvc8mBqXicMNRy etqLFp5QF299MwSj74awoir7DA9pX56wx3eni32HZoWzfeuWXuC53Fx0D rYjTrJN37EBpWhBiHpVqY5c4Gv3W1sRoaOBbYWuX8Ff/btJ7qzSZGEQfL B0Mx++arBL3eYYT105lJG55ini2Eop/7ua0n6Zn8ZnvQkyoqwgwaEUBfo w==; X-CSE-ConnectionGUID: OvYUhmBRTla/tYgTbuGMRA== X-CSE-MsgGUID: wHbWz9z0RWK0p8od6MV6lg== X-IronPort-AV: E=McAfee;i="6700,10204,11176"; a="34537385" X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="34537385" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:35 -0700 X-CSE-ConnectionGUID: Q2b/PrBlQ+umJbsTxNeZ9A== X-CSE-MsgGUID: frXaX9lhReaBmyuZMDCAGw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="66992203" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.1.253]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:33 -0700 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , Rae Moar , Lucas De Marchi , David Gow Subject: [PATCH v2 1/6] kunit: Introduce kunit_is_running() Date: Tue, 27 Aug 2024 00:20:10 +0200 Message-Id: <20240826222015.1484-2-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240826222015.1484-1-michal.wajdeczko@intel.com> References: <20240826222015.1484-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 --- 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 Mon Aug 26 22:20:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13778588 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 9BDBA811E2 for ; Mon, 26 Aug 2024 22:20:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710838; cv=none; b=jmbVqbj1PRym+F8r4oySelJznbQ5EQzX1swezdYG8NLpVJ+cMz1Ruflwefkp2DYL6rycQH+NSJ/6cCC7VmNr/TCED9N5eBKYUr8TiuAtMBUqGDKl0us2ZRg5wtdldqRCQwy3yJqmvsYJdChGDPbpK6mWx3ueix3McJCDdA9Cwts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710838; c=relaxed/simple; bh=sU0ech7PhVj3cE3JKopGkDODGyiifWrk9mA0zCgcDKY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sjuHWSLQteAU1H26BWyf/WZkQ49afwCK6mva6t7ICU4B4cMeTCixR8gF1Sz3T7Powh6U/q+F5d/52lsLQnUpmykTqnr4lKaXJ/gY7FCHEHAlCFPqINVAovgYMJ7fs3jy2K7AwGcF3uXAD0rp+y4XYZTmeAoqNYd/3P5CRaWcHsc= 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=B36ueka6; arc=none smtp.client-ip=198.175.65.12 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="B36ueka6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724710837; x=1756246837; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sU0ech7PhVj3cE3JKopGkDODGyiifWrk9mA0zCgcDKY=; b=B36ueka6dFmDpZJuqHX+BydyLXslZGrPXckuxaNW758oMbzHSprKjd+A wl/HSa6j8qRisUjztZ2sqa4353iO2mztDb0byLg7iffJs78PHJ7r/cZzs EUo6+PuSFUZrKI5/DmS9u5xdfG4zIyHNTKZD7UbYRUj7MH9XlvyzHVkOP zs8j5iy8PKC9eBV63asPmuKepKBJu6IiCFcRBU4zQfUkhwT3Z1X7u2BNT oOpTpRuYP4ng7D0QfGhfx58AAYs9Fj0ZfRcT3J3E6bqq+iayJjfJRpIi2 W180MTdNnPNaNtV9VE8OsiUKff/m5iWBv5Gc4hnHJKpwfBEY2rnSUd2/O w==; X-CSE-ConnectionGUID: lX2dSN37SjiGDxwK1DhYmQ== X-CSE-MsgGUID: dud4ZTRZTeSmvNEW0SD1tA== X-IronPort-AV: E=McAfee;i="6700,10204,11176"; a="34537388" X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="34537388" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:37 -0700 X-CSE-ConnectionGUID: PYPXPQQpTIy1y88LstEC/g== X-CSE-MsgGUID: kO+HwQB+QaC3/TQa8wzGkA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="66992212" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.1.253]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:35 -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 v2 2/6] kunit: Add macro to conditionally expose declarations to tests Date: Tue, 27 Aug 2024 00:20:11 +0200 Message-Id: <20240826222015.1484-3-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240826222015.1484-1-michal.wajdeczko@intel.com> References: <20240826222015.1484-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: 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 Mon Aug 26 22:20:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13778589 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 076CA811E2 for ; Mon, 26 Aug 2024 22:20:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710840; cv=none; b=Hw43klWm/n5tjMojKlTBl1GPOIRhmNqBK7+HPOMHJLCxBJOkvRaUCI3Rnu8HVaUF1SY95DQq+l+UB6Ro0FgCAevtW7oWGab3ilNbKUGW2BQIKTrBWtI52RncJvxuHE8LxyKKaeQYQzycBDj1RIf1mFvJz7HKebBBp4wVZn47cHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710840; c=relaxed/simple; bh=LmunGQniTVddjjEhBzlKBYQi2KkdQYLohMk4k4MO/r4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Moye4/Y5OQvlR+YCHLpS794mY9SFugAcEbh7fI5OyAkNhHJFghcEWzchjh1J8LNY3aFFxUOYTgj0gorhpzTh4nK78xYtF8Ge6zJw6cLaM3xMGGWB/dWXf1jaCN0QR8IknCiXfumffxExmbx3YDUVn5aqvm3M0h32vUAQ4gq6pC0= 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=RVi2qqLZ; arc=none smtp.client-ip=198.175.65.12 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="RVi2qqLZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724710839; x=1756246839; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LmunGQniTVddjjEhBzlKBYQi2KkdQYLohMk4k4MO/r4=; b=RVi2qqLZ1FaD3TGYIbeaV9S8WoCVMoA3PPsS51iQ04GnP4d0QYKEo8dZ bCmKTQVgHAv7oXjmJqGyhUQlzWWEOUBy2u+9E8sVKVWtJn89/eCX/ig6x 3QV/wPNlkaynNo766e6c6thGGCuHuihAri7POCUtZRgCma8lCyDKfFtAL 1XLNMxpV5C3ZDTB/lf/84lkJAbmjni9zhXoQiSNvESh/2lb3Rsn8yP2pw tJ2Yaodxi0G7muCTncu2InkuieD0XLYYaLkuTOI8P3aSMwhu242q04XC6 Wla4QqcUPsic+ZmeVh4vehZ2RfDUvqvBKSMMlz5K+0LdWhws5b1ZxkUHT Q==; X-CSE-ConnectionGUID: BcLZWTIWSWeRdxCnN9Bb2A== X-CSE-MsgGUID: tk0ubk11T9u8NmDQZlpiQw== X-IronPort-AV: E=McAfee;i="6700,10204,11176"; a="34537395" X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="34537395" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:39 -0700 X-CSE-ConnectionGUID: aqmrJdR8TAukpywuVO82wA== X-CSE-MsgGUID: RbzbcP8ZRnONosNE3KlBnw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="66992226" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.1.253]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:37 -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 v2 3/6] kunit: Add macro to conditionally expose expressions to tests Date: Tue, 27 Aug 2024 00:20:12 +0200 Message-Id: <20240826222015.1484-4-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240826222015.1484-1-michal.wajdeczko@intel.com> References: <20240826222015.1484-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 ONLY_IF_KUNIT macro will add expression statement only if the CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled it will evaluate always to 0. Signed-off-by: Michal Wajdeczko --- 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 1c23773f826c..69c71eacf368 100644 --- a/include/kunit/visibility.h +++ b/include/kunit/visibility.h @@ -18,6 +18,13 @@ * @body: identifiers to be introduced conditionally */ #define DECLARE_IF_KUNIT(body...) body + /** + * ONLY_IF_KUNIT - A macro that adds expression statement only if + * CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled + * it will evaluate always to 0. + * @expr: expression to be introduced conditionally + */ + #define ONLY_IF_KUNIT(expr...) expr /** * VISIBLE_IF_KUNIT - A macro that sets symbols to be static if * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled @@ -34,6 +41,7 @@ EXPORTED_FOR_KUNIT_TESTING) #else #define DECLARE_IF_KUNIT(body...) + #define ONLY_IF_KUNIT(expr...) 0 #define VISIBLE_IF_KUNIT static #define EXPORT_SYMBOL_IF_KUNIT(symbol) #endif From patchwork Mon Aug 26 22:20:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13778590 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 0F2BC19D081 for ; Mon, 26 Aug 2024 22:20:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710842; cv=none; b=G9V+1Ii+qj5xFiivV/vADqPi37XN8uKryXNhqK599edJRUhdPzfaHBJqq9IQk+Yw3J8MCMvvrxI1thUjVNUPJq1skaqK4jdp0uihNmVVWfU4aorB54yYz9dC/sLlnASU6tOwCbRmddEbzsB82dM9hrd73bb5b2bmpVnA+ko5Jeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710842; c=relaxed/simple; bh=zB62T1yGf63JCVW/O84RcBRDb82PjABClWqaukJnNq0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=diDRxYxO6M4v4TiBjytfhhVF2mTfVrDJHskT3ci3UvwYZ2uBWY7HiWaVbtlHSsH4X1VcNNPn3amFClaC4N5DlewinkfBKsPmDVGxr2+JRmjYqQRT0AMbH3wNUqSvFXOt9ABhU2pOhhK5VKHoF6UPk1FFf/9mDkRJE+0GUdPa27w= 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=b+gaoss1; arc=none smtp.client-ip=198.175.65.12 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="b+gaoss1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724710841; x=1756246841; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zB62T1yGf63JCVW/O84RcBRDb82PjABClWqaukJnNq0=; b=b+gaoss11Cs4H/lp74oU3zyQzJPW+leb6YY7gZNUgC6nNhdFwcLlyMQ2 w4oMoLoQmV8trdwc+SJ6KHkir5YmKimb9Zs/i/96wAvh8pl4mQ8s+6xb1 y87WC9wqBtarithU9ys+w5rxGI1DKvNarrhkwjPZmHEd15mYO6HhMua2F /LLc46OZ4E71UOKShDUBoUEn6J1b+Yk4VWrtrpaW3qxLXcgjoYxFsyjUI xg8YXrcA+kFRD2EHg5q3+pRJTwq3dNRgdUOHzdXNF/JXZ8Tl+QBkW4dXu joB6FS/CQVUpul4UXxbMp9KMFlM34IHi5dEBdPInt/1JRgav3pXi5kf4B g==; X-CSE-ConnectionGUID: DD92JU1yTFS7DC8TbnpXuA== X-CSE-MsgGUID: vErO+1dBTX64m8vBgXsfDw== X-IronPort-AV: E=McAfee;i="6700,10204,11176"; a="34537399" X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="34537399" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:41 -0700 X-CSE-ConnectionGUID: LNY15QmaQeay9wXBEet40Q== X-CSE-MsgGUID: 5nO+pTXJTuCM6bM2ucYmgQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="66992240" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.1.253]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:39 -0700 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , Rae Moar , David Gow , Daniel Latypov , Lucas De Marchi Subject: [PATCH v2 4/6] kunit: Allow function redirection outside of the KUnit thread Date: Tue, 27 Aug 2024 00:20:13 +0200 Message-Id: <20240826222015.1484-5-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240826222015.1484-1-michal.wajdeczko@intel.com> References: <20240826222015.1484-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 functionality 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. These new stubs, named 'global', must be declared using dedicated KUNIT_DECLARE_GLOBAL_STUB() macro and then can be placed either as global static variables or as part of the other structures. To properly maintain stubs lifecycle, they can be activated only from the main KUnit context. Some precaution is taken to avoid changing or deactivating replacement functions if one is still used by other thread. Signed-off-by: Michal Wajdeczko --- Cc: Rae Moar Cc: David Gow Cc: Daniel Latypov Cc: Lucas De Marchi --- v2: s/FIXED_STUB/GLOBAL_STUB (David, Lucas) make it little more thread safe (Rae, David) wait until stub call finishes before test end (David) wait until stub call finishes before changing stub (David) allow stub deactivation (Rae) prefer kunit log (David) --- include/kunit/static_stub.h | 158 ++++++++++++++++++++++++++++++++++++ lib/kunit/static_stub.c | 49 +++++++++++ 2 files changed, 207 insertions(+) diff --git a/include/kunit/static_stub.h b/include/kunit/static_stub.h index bf940322dfc0..42a70dcefb56 100644 --- a/include/kunit/static_stub.h +++ b/include/kunit/static_stub.h @@ -12,12 +12,15 @@ /* If CONFIG_KUNIT is not enabled, these stubs quietly disappear. */ #define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...) do {} while (0) +#define KUNIT_GLOBAL_STUB_REDIRECT(stub_name, args...) do {} while (0) +#define KUNIT_DECLARE_GLOBAL_STUB(stub_name, stub_type) #else #include #include +#include /* for CLASS */ #include /* for {un,}likely() */ #include /* for task_struct */ @@ -109,5 +112,160 @@ void __kunit_activate_static_stub(struct kunit *test, */ void kunit_deactivate_static_stub(struct kunit *test, void *real_fn_addr); +/** + * struct kunit_global_stub - Represents a context of global function stub. + * @replacement: The address of replacement function. + * @owner: The KUnit test that owns the stub, valid only when @busy > 0. + * @busy: The stub busyness counter incremented on entry to the replacement + * function, decremented on exit, used to signal if the stub is idle. + * @idle: The completion state to indicate when the stub is idle again. + * + * This structure is for KUnit internal use only. + * See KUNIT_DECLARE_GLOBAL_STUB(). + */ +struct kunit_global_stub { + void *replacement; + struct kunit *owner; + atomic_t busy; + struct completion idle; +}; + +/** + * KUNIT_DECLARE_GLOBAL_STUB() - Declare a global function stub. + * @stub_name: The name of the stub, must be a valid identifier + * @stub_type: The type of the function that this stub will replace + * + * This macro will declare new identifier of an anonymous type that will + * represent global stub function that could be used by KUnit. It can be stored + * outside of the KUnit code. If the CONFIG_KUNIT is not enabled this will + * be evaluated to an empty statement. + * + * The anonymous type introduced by this macro is mostly a wrapper to generic + * struct kunit_global_stub but with additional dummy member, that is never + * used directly, but is needed to maintain the type of the stub function. + */ +#define KUNIT_DECLARE_GLOBAL_STUB(stub_name, stub_type) \ +union { \ + struct kunit_global_stub base; \ + typeof(stub_type) dummy; \ +} stub_name + +/* Internal struct to define guard class */ +struct kunit_global_stub_guard { + struct kunit_global_stub *stub; + void *active_replacement; +}; + +/* Internal class used to guard stub calls */ +DEFINE_CLASS(kunit_global_stub_guard, + struct kunit_global_stub_guard, + ({ + struct kunit_global_stub *stub = _T.stub; + bool active = !!_T.active_replacement; + + if (active && !atomic_dec_return(&stub->busy)) + complete_all(&stub->idle); + }), + ({ + class_kunit_global_stub_guard_t guard; + bool active = !!atomic_inc_not_zero(&stub->busy); + + guard.stub = stub; + guard.active_replacement = active ? READ_ONCE(stub->replacement) : NULL; + + guard; + }), + struct kunit_global_stub *stub) + +/** + * KUNIT_GLOBAL_STUB_REDIRECT() - Call a fixed function stub if activated. + * @stub: The function stub declared using KUNIT_DECLARE_GLOBAL_STUB() + * @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 KUnit is not + * running or stub is not yet activated the function will continue execution + * as normal. + * + * The function stub must be declared with KUNIT_DECLARE_GLOBAL_STUB() that is + * stored in a place that is accessible from both the test code, which will + * activate this stub using kunit_activate_global_stub(), and from the function, + * where we will do this redirection using KUNIT_GLOBAL_STUB_REDIRECT(). + * + * 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 + * + * KUNIT_DECLARE_GLOBAL_STUB(func_stub, int (*)(int n)); + * + * int real_func(int n) + * { + * KUNIT_GLOBAL_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); + * kunit_activate_global_stub(test, func_stub, replacement_func); + * KUNIT_EXPECT_EQ(test, real_func(1), 101); + * } + */ +#define KUNIT_GLOBAL_STUB_REDIRECT(stub, args...) do { \ + if (kunit_is_running()) { \ + typeof(stub) *__stub = &(stub); \ + CLASS(kunit_global_stub_guard, guard)(&__stub->base); \ + typeof(__stub->dummy) replacement = guard.active_replacement; \ + if (unlikely(replacement)) { \ + kunit_info(__stub->base.owner, "%s: redirecting to %ps\n", \ + __func__, replacement); \ + return replacement(args); \ + } \ + } \ +} while (0) + +void __kunit_activate_global_stub(struct kunit *test, struct kunit_global_stub *stub, + void *replacement_addr); + +/** + * kunit_activate_global_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 deactivate the stub at the test end. + * + * The redirection can be disabled with kunit_deactivate_global_stub(). + * The stub must be declared using KUNIT_DECLARE_GLOBAL_STUB(). + */ +#define kunit_activate_global_stub(test, stub, replacement) do { \ + typeof(stub) *__stub = &(stub); \ + typecheck_fn(typeof(__stub->dummy), (replacement)); \ + __kunit_activate_global_stub((test), &__stub->base, (replacement)); \ +} while (0) + +void __kunit_deactivate_global_stub(struct kunit *test, struct kunit_global_stub *stub); + +/** + * kunit_deactivate_global_stub() - Disable a fixed function stub. + * @test: Test case that wants to deactivate a fixed function stub + * @stub: The location of the function stub pointer + * + * The stub must be declared using KUNIT_DECLARE_GLOBAL_STUB(). + */ +#define kunit_deactivate_global_stub(test, stub) do { \ + typeof(stub) *__stub = &(stub); \ + __kunit_deactivate_global_stub((test), &__stub->base); \ +} while (0) + #endif #endif diff --git a/lib/kunit/static_stub.c b/lib/kunit/static_stub.c index 92b2cccd5e76..799a7271dc5b 100644 --- a/lib/kunit/static_stub.c +++ b/lib/kunit/static_stub.c @@ -121,3 +121,52 @@ void __kunit_activate_static_stub(struct kunit *test, } } EXPORT_SYMBOL_GPL(__kunit_activate_static_stub); + +static void sanitize_global_stub(void *data) +{ + struct kunit *test = kunit_get_current_test(); + struct kunit_global_stub *stub = data; + + KUNIT_EXPECT_NE(test, 0, atomic_read(&stub->busy)); + KUNIT_EXPECT_PTR_EQ(test, test, READ_ONCE(stub->owner)); + + reinit_completion(&stub->idle); + if (!atomic_dec_and_test(&stub->busy)) { + kunit_info(test, "waiting for %ps\n", stub->replacement); + KUNIT_EXPECT_EQ(test, 0, wait_for_completion_interruptible(&stub->idle)); + } + + WRITE_ONCE(stub->owner, NULL); + WRITE_ONCE(stub->replacement, NULL); +} + +/* + * Helper function for kunit_activate_global_stub(). The macro does + * typechecking, so use it instead. + */ +void __kunit_activate_global_stub(struct kunit *test, + struct kunit_global_stub *stub, + void *replacement_addr) +{ + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stub); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, replacement_addr); + if (atomic_read(&stub->busy)) + kunit_release_action(test, sanitize_global_stub, stub); + else + init_completion(&stub->idle); + WRITE_ONCE(stub->owner, test); + WRITE_ONCE(stub->replacement, replacement_addr); + KUNIT_ASSERT_EQ(test, 1, atomic_inc_return(&stub->busy)); + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test, sanitize_global_stub, stub)); +} +EXPORT_SYMBOL_GPL(__kunit_activate_global_stub); + +/* + * Helper function for kunit_deactivate_global_stub(). Use it instead. + */ +void __kunit_deactivate_global_stub(struct kunit *test, struct kunit_global_stub *stub) +{ + if (atomic_read(&stub->busy)) + kunit_release_action(test, sanitize_global_stub, stub); +} +EXPORT_SYMBOL_GPL(__kunit_deactivate_global_stub); From patchwork Mon Aug 26 22:20:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13778591 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 7ED4913A899 for ; Mon, 26 Aug 2024 22:20:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710845; cv=none; b=shmb/qfqy+UwqWG5HcJlsxK1dkWXiLDX3FeCvxanO8jfT0ZEmu3nuH/0J+O+CnZMAYVA5iX9tEWel1go158+J8EnjwqaaKLLjSNPAzjE3cOTRQS0huNHFRIRRe/SJVoptVa4Jl1JE7mE7niPXe25FTlienYiysLNSKEAYvPLzuw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710845; c=relaxed/simple; bh=WYmBj/QZma2j2gocEAvAsVAeyeb4B+d4i/LCUcUluzw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gSPMx41RehB3HsFpkapWOXiz2kuKJUvj8Y127wcL/ozyiYPsR2sF06YggnfYmbn94xrM+8CDYDuZ3oIKEYS/qXiiUQmVhUNUjUuhYAgfJls8h6EtX6ZjvUuNBXFBQ4jUke2ZmQtdjIbMEtjyxHi0nRtdcdDyy0YVMeHeNFe4IRA= 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=CdenyUOS; arc=none smtp.client-ip=198.175.65.12 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="CdenyUOS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724710844; x=1756246844; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WYmBj/QZma2j2gocEAvAsVAeyeb4B+d4i/LCUcUluzw=; b=CdenyUOS6xhfEPW/7V3KMZEnJV7MI1wUYhlxMDAOW+wMBnVh67cZprzt Hyix4o8OHIHGSEXS6cRuBCallbczcnFNxUCjb+Ebu/PKG76+SoeZVBIfi MTDCMAS+vMXMJfu4pO4g/7HwzKeE6AvgP+xlpCXt/SLSzCxAVVO0B9OAw olz/W+gwwnCq8n/8BgeHT62CtbbDEi8UQhJ0RG6AvmhjEuOsxmkDawKzH KSRl0uWSyYoLPO0SYsVJ9K18PGAvueh4tcRYxsSQ8K4LJ2ovVOWK0Veq9 jYQx5VR9jaKpLCAD+TMFKwsk3LQjCZJcidBw8wC+f8I0145T7goaWR1YG A==; X-CSE-ConnectionGUID: Uvw6tMlPS4y7puUoKKbMhg== X-CSE-MsgGUID: 3qnNtFN6TKCbpBdnThHRJw== X-IronPort-AV: E=McAfee;i="6700,10204,11176"; a="34537410" X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="34537410" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:43 -0700 X-CSE-ConnectionGUID: Bw3sv9MESLmGsdD0yJAZnw== X-CSE-MsgGUID: D9NLSc3qSWCaNRrvXId5MA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="66992257" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.1.253]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:41 -0700 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , Rae Moar , David Gow , Daniel Latypov , Lucas De Marchi Subject: [PATCH v2 5/6] kunit: Add example with alternate function redirection method Date: Tue, 27 Aug 2024 00:20:14 +0200 Message-Id: <20240826222015.1484-6-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240826222015.1484-1-michal.wajdeczko@intel.com> References: <20240826222015.1484-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 #v1 Reviewed-by: David Gow #v1 Reviewed-by: Lucas De Marchi --- Cc: Daniel Latypov Cc: Lucas De Marchi --- v2: add missing testcase description (Rae) and rebase --- lib/kunit/kunit-example-test.c | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 3056d6bc705d..146935a16883 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,70 @@ 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 global stub functions. */ +static struct { + KUNIT_DECLARE_GLOBAL_STUB(add_two, int (*)(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_GLOBAL_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; +} + +/* + * This test shows how to use KUNIT_GLOBAL_STUB_REDIRECT and compares its + * usage with the KUNIT_STATIC_STUB_REDIRECT. + */ +static void example_global_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_global_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_global_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 +360,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_global_stub_test), KUNIT_CASE(example_priv_test), KUNIT_CASE_PARAM(example_params_test, example_gen_params), KUNIT_CASE_SLOW(example_slow_test), From patchwork Mon Aug 26 22:20:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13778592 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 39EFA19D090 for ; Mon, 26 Aug 2024 22:20:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710847; cv=none; b=dUyGoa9zLMLDWjpVzvpIr/muZSs5fydDVYQgHaZs5CJEn8E87ZxpY3URsiygA9Z4ggVnkX6C6SSd0MLtpg8VkIyCCt5rKWQkfD3whgWXf/uM8jtK90nBhp7Q7xLTyf0BMfeh50+TUM2fj+99D4A6XyZspksPUOhJ6318T1hhXGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724710847; c=relaxed/simple; bh=j17FxC2q9JsZ5Sd6M61sTCz1rZvBj8On6+3nC7HiN0M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=G2wcC6QIxlh2FvhZX7flonwW5lIovk3MFCzS3THx21G4vqjKvFv4qIVI67jmdK5NtX3kLlRh1KOyvKjmPaplMTdPnFMu4Ae33xnuSV88elAAN8LWrrG1Ff6PbjnAIRNoOoToGG4vsc7FdhXGHXUGtUOrefscJrqg60e3k+o+SHA= 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=RmY7cBFG; arc=none smtp.client-ip=198.175.65.12 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="RmY7cBFG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724710846; x=1756246846; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=j17FxC2q9JsZ5Sd6M61sTCz1rZvBj8On6+3nC7HiN0M=; b=RmY7cBFGUmOLbAQRCbe2USXjEnj9XTtS5UyUg+XSuUojgS/ffB69v71X 31ZsJD94kEsQJadSBi/joA6w9f9CtSdDw8q89Ka0fBFgytyTbTx3c1hjV bgB3+Kh5UaW70T8d2W+NXyuXxxn9j1lv8HAtSDkaMQcw88W2GjK1ytb5Z mffyxiTdoZal+xib1DuHQhN1/HVVs69ykKgiFOGj1iwkP0ful+7jhW+sr bDCMa6geCR42s0kf3XRAuWOTMlmlo8bZ1RSaBcEOhxqEuxorcirvIKq8m jnrr4s0/bcyL4KAxvPt7P/qT8LvQlV5W0F0+aq6gBVWT58hDP3htp+xfy g==; X-CSE-ConnectionGUID: 5KVEkVjtQs+LGQeTLk/RFA== X-CSE-MsgGUID: dXddBe/tTg6bIXf7AVEwAA== X-IronPort-AV: E=McAfee;i="6700,10204,11176"; a="34537414" X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="34537414" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:45 -0700 X-CSE-ConnectionGUID: +0gMUt+tT0eejWL6066L1A== X-CSE-MsgGUID: QnwiZ+WBQ/ya18SEtKPudQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,178,1719903600"; d="scan'208";a="66992274" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.1.253]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2024 15:20:44 -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 v2 6/6] kunit: Add some selftests for global stub redirection macros Date: Tue, 27 Aug 2024 00:20:15 +0200 Message-Id: <20240826222015.1484-7-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20240826222015.1484-1-michal.wajdeczko@intel.com> References: <20240826222015.1484-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 While we already have few ASSERT() within the implementation, it's always better to have dedicated test cases. Add tests for: - automatic deactivation of the stubs at the test end - blocked deactivation until all active stub calls finish - blocked stub change until all active stub calls finish - safe abuse (deactivation without activation) Signed-off-by: Michal Wajdeczko --- Cc: Rae Moar Cc: David Gow Cc: Lucas De Marchi --- lib/kunit/kunit-test.c | 254 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 253 insertions(+), 1 deletion(-) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 37e02be1e710..eb1bb312ad71 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -6,8 +6,10 @@ * Author: Brendan Higgins */ #include "linux/gfp_types.h" +#include #include #include +#include #include #include @@ -866,10 +868,260 @@ static struct kunit_suite kunit_current_test_suite = { .test_cases = kunit_current_test_cases, }; +static struct { + /* this stub matches the real function */ + KUNIT_DECLARE_GLOBAL_STUB(first_stub, int (*)(int i)); + /* this stub matches only return type of the real function */ + KUNIT_DECLARE_GLOBAL_STUB(second_stub, int (*)(int bit, int data)); + /* this is an example stub that returns void */ + KUNIT_DECLARE_GLOBAL_STUB(void_stub, void (*)(void)); + /* this is an example how to store additional data for use by stubs */ + DECLARE_IF_KUNIT(int data); + DECLARE_IF_KUNIT(int counter); +} stubs = { + DECLARE_IF_KUNIT(.data = 3), +}; + +static int real_func(int i) +{ + KUNIT_GLOBAL_STUB_REDIRECT(stubs.first_stub, i); + KUNIT_GLOBAL_STUB_REDIRECT(stubs.second_stub, BIT(i), stubs.data); + + return i; +} + +struct real_work { + struct work_struct work; + int param; + int result; +}; + +static void real_work_func(struct work_struct *work) +{ + struct real_work *w = container_of(work, typeof(*w), work); + + w->result = real_func(w->param); +} + +static int real_func_async(int i) +{ + struct real_work w = { .param = i, .result = -EINPROGRESS }; + + INIT_WORK_ONSTACK(&w.work, real_work_func); + schedule_work(&w.work); + flush_work(&w.work); + destroy_work_on_stack(&w.work); + + return w.result; +} + +static int replacement_func(int i) +{ + return i + 1; +} + +static int other_replacement_func(int i) +{ + return i + 10; +} + +static int super_replacement_func(int bit, int data) +{ + return bit * data; +} + +static int slow_replacement_func(int i) +{ + schedule_timeout_interruptible(HZ / 20); + return replacement_func(i); +} + +static void real_void_func(void) +{ + KUNIT_GLOBAL_STUB_REDIRECT(stubs.void_stub); + DECLARE_IF_KUNIT(stubs.counter++); +} + +static void replacement_void_func(void) +{ + stubs.counter--; +} + +static void expect_deactivated(void *data) +{ + struct kunit *test = kunit_get_current_test(); + + KUNIT_EXPECT_NULL(test, stubs.first_stub.base.owner); + KUNIT_EXPECT_NULL(test, stubs.first_stub.base.replacement); + KUNIT_EXPECT_NULL(test, stubs.second_stub.base.owner); + KUNIT_EXPECT_NULL(test, stubs.second_stub.base.replacement); + KUNIT_EXPECT_NULL(test, stubs.void_stub.base.owner); + KUNIT_EXPECT_NULL(test, stubs.void_stub.base.replacement); +} + +static void kunit_global_stub_test_deactivate(struct kunit *test) +{ + /* make sure everything will be deactivated */ + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test, expect_deactivated, test)); + + /* deactivate without activate */ + kunit_deactivate_global_stub(test, stubs.first_stub); + + /* deactivate twice */ + kunit_deactivate_global_stub(test, stubs.first_stub); + + /* allow to skip deactivation (will be tested by expect_deactivated action) */ + kunit_activate_global_stub(test, stubs.first_stub, replacement_func); +} + +static void kunit_global_stub_test_activate(struct kunit *test) +{ + int real, replacement, other, super, i = 2; + + /* prerequisites */ + real_void_func(); + KUNIT_ASSERT_EQ(test, stubs.counter, 1); + replacement_void_func(); + KUNIT_ASSERT_EQ(test, stubs.counter, 0); + + /* prerequisites cont'd */ + KUNIT_ASSERT_EQ(test, real_func(i), real = real_func_async(i)); + KUNIT_ASSERT_NE(test, real_func(i), replacement = replacement_func(i)); + KUNIT_ASSERT_NE(test, real_func(i), other = other_replacement_func(i)); + KUNIT_ASSERT_NE(test, real_func(i), super = super_replacement_func(BIT(i), stubs.data)); + + /* make sure everything will be deactivated */ + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test, expect_deactivated, test)); + + /* allow to activate replacement */ + kunit_activate_global_stub(test, stubs.void_stub, replacement_void_func); + real_void_func(); + KUNIT_ASSERT_EQ(test, stubs.counter, -1); + + /* allow to activate replacement */ + kunit_activate_global_stub(test, stubs.first_stub, replacement_func); + KUNIT_EXPECT_EQ(test, real_func(i), replacement); + KUNIT_EXPECT_EQ(test, real_func_async(i), replacement); + + /* allow to change replacement */ + kunit_activate_global_stub(test, stubs.first_stub, other_replacement_func); + KUNIT_EXPECT_EQ(test, real_func(i), other); + KUNIT_EXPECT_EQ(test, real_func_async(i), other); + + /* allow to deactivate replacement */ + kunit_deactivate_global_stub(test, stubs.first_stub); + KUNIT_EXPECT_EQ(test, real_func(i), real); + KUNIT_EXPECT_EQ(test, real_func_async(i), real); + + /* allow to activate replacement with different arguments */ + kunit_activate_global_stub(test, stubs.second_stub, super_replacement_func); + KUNIT_EXPECT_EQ(test, real_func(i), super); + KUNIT_EXPECT_EQ(test, real_func_async(i), super); + + /* allow to deactivate twice */ + kunit_deactivate_global_stub(test, stubs.second_stub); + kunit_deactivate_global_stub(test, stubs.second_stub); + KUNIT_EXPECT_EQ(test, real_func_async(i), real); + KUNIT_EXPECT_EQ(test, real_func(i), real); +} + +static void flush_real_work(void *data) +{ + struct real_work *w = data; + + flush_work(&w->work); +} + +static void __kunit_global_stub_test_slow(struct kunit *test, bool replace) +{ + int real, replacement, other, i = replace ? 3 : 5; + struct real_work *w; + + /* prerequisites */ + KUNIT_ASSERT_EQ(test, real_func(i), real = real_func_async(i)); + KUNIT_ASSERT_NE(test, real_func(i), replacement = slow_replacement_func(i)); + KUNIT_ASSERT_NE(test, real_func(i), other = other_replacement_func(i)); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, w = kunit_kzalloc(test, sizeof(*w), GFP_KERNEL)); + INIT_WORK(&w->work, real_work_func); + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test, flush_real_work, w)); + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test, expect_deactivated, test)); + + /* allow to activate replacement */ + kunit_activate_global_stub(test, stubs.first_stub, slow_replacement_func); + KUNIT_EXPECT_EQ(test, real_func_async(i), replacement); + + w->param = i; + w->result = 0; + queue_work(system_long_wq, &w->work); + + /* wait until work starts */ + while (work_pending(&w->work)) + schedule_timeout_interruptible(HZ / 100); + KUNIT_EXPECT_NE(test, work_busy(&w->work), 0); + + /* wait until work enters the stub */ + while (atomic_read(&stubs.first_stub.base.busy) < 2) + schedule_timeout_interruptible(HZ / 100); + + /* stub should be still busy(2) at this point */ + KUNIT_EXPECT_EQ(test, 2, atomic_read(&stubs.first_stub.base.busy)); + KUNIT_EXPECT_EQ(test, w->result, 0); + + if (replace) { + /* try replace the stub, it should be just activated(1) */ + kunit_activate_global_stub(test, stubs.first_stub, other_replacement_func); + KUNIT_EXPECT_EQ(test, 1, atomic_read(&stubs.first_stub.base.busy)); + } else { + /* try to deactivate the stub, it should be disabled(0) */ + kunit_deactivate_global_stub(test, stubs.first_stub); + KUNIT_EXPECT_EQ(test, 0, atomic_read(&stubs.first_stub.base.busy)); + } + + /* and results from the worker should be available */ + KUNIT_EXPECT_EQ(test, w->result, replacement); + KUNIT_EXPECT_NE(test, w->result, real); + KUNIT_EXPECT_NE(test, w->result, other); + + if (replace) + KUNIT_EXPECT_EQ(test, real_func_async(i), other); + else + KUNIT_EXPECT_EQ(test, real_func_async(i), real); +} + +static void kunit_global_stub_test_slow_deactivate(struct kunit *test) +{ + __kunit_global_stub_test_slow(test, false); +} + +static void kunit_global_stub_test_slow_replace(struct kunit *test) +{ + __kunit_global_stub_test_slow(test, true); +} + +static int kunit_global_stub_test_init(struct kunit *test) +{ + stubs.counter = 0; + return 0; +} + +static struct kunit_case kunit_global_stub_test_cases[] = { + KUNIT_CASE(kunit_global_stub_test_activate), + KUNIT_CASE(kunit_global_stub_test_deactivate), + KUNIT_CASE_SLOW(kunit_global_stub_test_slow_deactivate), + KUNIT_CASE_SLOW(kunit_global_stub_test_slow_replace), + {} +}; + +static struct kunit_suite kunit_global_stub_suite = { + .name = "kunit_global_stub", + .init = kunit_global_stub_test_init, + .test_cases = kunit_global_stub_test_cases, +}; + kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_log_test_suite, &kunit_status_test_suite, &kunit_current_test_suite, &kunit_device_test_suite, - &kunit_fault_test_suite); + &kunit_fault_test_suite, &kunit_global_stub_suite); MODULE_DESCRIPTION("KUnit test for core test infrastructure"); MODULE_LICENSE("GPL v2");