Skip to content

Commit 8184ee1

Browse files
softwareckifabiobaltieri
authored andcommitted
logging: Add missing compiler barrier after k_is_user_context
Remove the k_is_user_context call from the Z_LOG_LEVEL_ALL_CHECK macro. Add the Z_LOG_LEVEL_ALL_CHECK_BREAK macro, which safely checks whether logging is performed in user context before checking the dynamic log level that requires access to kernel structure. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent fab9ced commit 8184ee1

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

include/zephyr/logging/log_core.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ extern "C" {
182182
* @retval false Drop that message.
183183
*/
184184
#define Z_LOG_DYNAMIC_LEVEL_CHECK(_level, _source) \
185-
(!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) || k_is_user_context() || \
185+
(!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) || \
186186
((_level) <= Z_LOG_RUNTIME_FILTER(((struct log_source_dynamic_data *)_source)->filters)))
187187

188188
/** @brief Check if message shall be created.
@@ -199,7 +199,30 @@ extern "C" {
199199
#define Z_LOG_LEVEL_ALL_CHECK(_level, _inst, _source) \
200200
(Z_LOG_CONST_LEVEL_CHECK(_level) && \
201201
Z_LOG_STATIC_INST_LEVEL_CHECK(_level, _inst, _source) && \
202-
Z_LOG_DYNAMIC_LEVEL_CHECK(_level, _source))
202+
COND_CODE_0(IS_ENABLED(CONFIG_USERSPACE), \
203+
(Z_LOG_DYNAMIC_LEVEL_CHECK(_level, _source)), \
204+
(true)) \
205+
)
206+
207+
/** @brief Check if the message shall be created, otherwise break.
208+
*
209+
* Aggregate all checks into a single one. Calls break to skip message creation. Userspace safe.
210+
*
211+
* @param _level Log level.
212+
* @param _inst 1 is source is the instance of a module.
213+
* @param _source Data associated with the source.
214+
*/
215+
#define Z_LOG_LEVEL_ALL_CHECK_BREAK(_level, _inst, _source) \
216+
if (!Z_LOG_LEVEL_ALL_CHECK((_level), (_inst), (_source))) { \
217+
break; \
218+
} \
219+
IF_ENABLED(CONFIG_USERSPACE, ( \
220+
if (!k_is_user_context()) { \
221+
compiler_barrier(); \
222+
if (!Z_LOG_DYNAMIC_LEVEL_CHECK((_level), (_source))) { \
223+
break; \
224+
} \
225+
}))
203226

204227
/** @brief Get current module data that is used for source id retrieving.
205228
*
@@ -291,9 +314,7 @@ static inline char z_log_minimal_level_to_char(int level)
291314
#define Z_LOG2(_level, _inst, _source, ...) \
292315
TOOLCHAIN_DISABLE_CLANG_WARNING(TOOLCHAIN_WARNING_USED_BUT_MARKED_UNUSED) \
293316
do { \
294-
if (!Z_LOG_LEVEL_ALL_CHECK(_level, _inst, _source)) { \
295-
break; \
296-
} \
317+
Z_LOG_LEVEL_ALL_CHECK_BREAK(_level, _inst, _source) \
297318
if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
298319
Z_LOG_TO_PRINTK(_level, __VA_ARGS__); \
299320
break; \
@@ -346,9 +367,7 @@ static inline char z_log_minimal_level_to_char(int level)
346367
#define Z_LOG_HEXDUMP2(_level, _inst, _source, _data, _len, ...) \
347368
TOOLCHAIN_DISABLE_CLANG_WARNING(TOOLCHAIN_WARNING_USED_BUT_MARKED_UNUSED) \
348369
do { \
349-
if (!Z_LOG_LEVEL_ALL_CHECK(_level, _inst, _source)) { \
350-
break; \
351-
} \
370+
Z_LOG_LEVEL_ALL_CHECK_BREAK(_level, _inst, _source) \
352371
const char *_str = GET_ARG_N(1, __VA_ARGS__); \
353372
if (IS_ENABLED(CONFIG_LOG_MODE_MINIMAL)) { \
354373
Z_LOG_TO_PRINTK(_level, "%s", _str); \

subsys/logging/frontends/stmesp/zephyr_custom_log.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ extern "C" {
4040
*/
4141
#define Z_LOG_STMESP_0(_level, _source, ...) \
4242
do { \
43-
if (!Z_LOG_LEVEL_ALL_CHECK(_level, __log_current_const_data, _source)) { \
44-
break; \
45-
} \
43+
Z_LOG_LEVEL_ALL_CHECK_BREAK(_level, __log_current_const_data, _source) \
4644
LOG_FRONTEND_STMESP_LOG0(_source, STRINGIFY(_level) __VA_ARGS__); \
4745
} while (0)
4846

@@ -72,9 +70,7 @@ extern "C" {
7270
(Z_LOG(_level, __VA_ARGS__))); \
7371
break; \
7472
} \
75-
if (!Z_LOG_LEVEL_ALL_CHECK(_level, __log_current_const_data, _source)) { \
76-
break; \
77-
} \
73+
Z_LOG_LEVEL_ALL_CHECK_BREAK(_level, __log_current_const_data, _source) \
7874
LOG_FRONTEND_STMESP_LOG1(_source, STRINGIFY(_level) __VA_ARGS__, dummy); \
7975
} while (0)
8076

0 commit comments

Comments
 (0)