From ae166bba8ad25ad9f2128314bbdf5a0ecb80f610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konsta=20H=C3=B6ltt=C3=A4?= Date: Tue, 30 Nov 2021 10:52:41 +0200 Subject: [PATCH] gpu: nvgpu: posix: print WARN*() location MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WARN() and WARN_ON() are most useful when the log explains where they happened. The posix implementation of these prints neither that nor the warning message (if any). Extend the macros to include function name and line number, and print those plus the format string. Actually formatting the format string is problematic wrt. MISRA rules, so the arguments are not formatted. The implementation of BUG() already prints the function name and line number. Change-Id: Ie246a915f5e8420e1c606bb1555a7f9b498725fd Signed-off-by: Konsta Hölttä Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2634105 Reviewed-by: svc-mobile-coverity Reviewed-by: svc-mobile-cert Reviewed-by: svcacv Reviewed-by: Alex Waterman Reviewed-by: mobile promotions Tested-by: mobile promotions GVS: Gerrit_Virtual_Submit --- drivers/gpu/nvgpu/include/nvgpu/posix/bug.h | 8 +++++--- drivers/gpu/nvgpu/os/posix/bug.c | 9 +++++++-- userspace/units/posix/bug/posix-bug.c | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/nvgpu/include/nvgpu/posix/bug.h b/drivers/gpu/nvgpu/include/nvgpu/posix/bug.h index eba30e2b9..c15b1ea65 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/posix/bug.h +++ b/drivers/gpu/nvgpu/include/nvgpu/posix/bug.h @@ -30,10 +30,10 @@ /** Define for issuing warning on condition with message. */ #define WARN(cond, msg, arg...) \ - ((void) nvgpu_posix_warn(cond, msg, ##arg)) + ((void) nvgpu_posix_warn(__func__, __LINE__, cond, msg, ##arg)) /** Define for issuing warning on condition. */ #define WARN_ON(cond) \ - ((void) nvgpu_posix_warn(cond, "")) + ((void) nvgpu_posix_warn(__func__, __LINE__, cond, "")) #ifdef CONFIG_NVGPU_NON_FUSA /** Define for issuing warning once on condition with message. */ @@ -78,13 +78,15 @@ void nvgpu_posix_bug(const char *msg, int line_no) __attribute__ ((noreturn)); * #nvgpu_warn() and dump the stack using function #dump_stack(). Function does * not perform any validation of the parameters. * + * @param func [in] Name of the calling function. + * @param line_no [in] Line number in the file where called. * @param cond [in] Condition to check to issue warning. * @param fmt [in] Format of variable argument list. * @param ... [in] Variable length arguments. * * @return Value of \a cond is returned. */ -bool nvgpu_posix_warn(bool cond, const char *fmt, ...); +bool nvgpu_posix_warn(const char *func, int line_no, bool cond, const char *fmt, ...); #ifdef __NVGPU_UNIT_TEST__ void nvgpu_bug_cb_longjmp(void *arg); diff --git a/drivers/gpu/nvgpu/os/posix/bug.c b/drivers/gpu/nvgpu/os/posix/bug.c index b716b253b..a01e45877 100644 --- a/drivers/gpu/nvgpu/os/posix/bug.c +++ b/drivers/gpu/nvgpu/os/posix/bug.c @@ -207,13 +207,18 @@ done: pthread_exit(NULL); } -bool nvgpu_posix_warn(bool cond, const char *fmt, ...) +bool nvgpu_posix_warn(const char *func, int line_no, bool cond, const char *fmt, ...) { if (!cond) { goto done; } - nvgpu_warn(NULL, "WARNING detected!"); + /* + * MISRA-C rule 17.1 forbids stdarg.h (va_list etc) and this is shared + * with the safety build. Rule 21.6 forbids stdio.h so the warning + * cannot be formatted to a local buffer either. + */ + nvgpu_warn(NULL, "%s:%d WARNING detected %s", func, line_no, fmt); dump_stack(); diff --git a/userspace/units/posix/bug/posix-bug.c b/userspace/units/posix/bug/posix-bug.c index 8ab99e00c..1298f4e9b 100644 --- a/userspace/units/posix/bug/posix-bug.c +++ b/userspace/units/posix/bug/posix-bug.c @@ -164,12 +164,12 @@ int test_warn_msg(struct unit_module *m, { bool ret; - ret = nvgpu_posix_warn(0, ""); + ret = nvgpu_posix_warn(__func__, __LINE__, 0, ""); if (ret != 0) { unit_return_fail(m, "nvgpu_posix_warn failed for cond 0\n"); } - ret = nvgpu_posix_warn(1, ""); + ret = nvgpu_posix_warn(__func__, __LINE__, 1, ""); if (ret != 1) { unit_return_fail(m, "nvgpu_posix_warn failed for cond 1\n"); }