From 3c3629c9c0bec9c34e55b2aa86cd692d30f4dc04 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Thu, 18 Jul 2024 10:43:17 +0200 Subject: [PATCH] rasdaemon: move type macros to a separate header (types.h) That makes easier to use/maintain it, without needing to include ras-record.h when all it is needed are common macros. Signed-off-by: Mauro Carvalho Chehab --- non-standard-ampere.c | 2 +- non-standard-hisi_hip08.c | 2 +- non-standard-hisilicon.c | 2 +- non-standard-jaguarmicro.c | 2 +- non-standard-yitian.c | 2 +- ras-aer-handler.c | 2 +- ras-arm-handler.c | 2 +- ras-cxl-handler.c | 3 +- ras-devlink-handler.c | 2 +- ras-diskerror-handler.c | 2 +- ras-events.c | 1 + ras-events.h | 6 +- ras-extlog-handler.c | 2 +- ras-logger.h | 2 +- ras-mc-handler.c | 2 +- ras-mce-handler.c | 2 +- ras-memory-failure-handler.c | 2 +- ras-non-standard-handler.c | 2 +- ras-non-standard-handler.h | 1 + ras-page-isolation.h | 2 +- ras-record.c | 1 + ras-record.h | 7 +- ras-report.h | 3 +- rasdaemon.c | 3 +- rbtree.h | 5 -- types.h | 139 +++++++++++++++++++++++++++++++++++ 26 files changed, 167 insertions(+), 34 deletions(-) create mode 100644 types.h diff --git a/non-standard-ampere.c b/non-standard-ampere.c index 0790323..b96e5c2 100644 --- a/non-standard-ampere.c +++ b/non-standard-ampere.c @@ -12,7 +12,7 @@ #include #include #include -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" #include "ras-non-standard-handler.h" diff --git a/non-standard-hisi_hip08.c b/non-standard-hisi_hip08.c index d90c6bf..4776832 100644 --- a/non-standard-hisi_hip08.c +++ b/non-standard-hisi_hip08.c @@ -11,7 +11,7 @@ #include #include #include -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" #include "ras-non-standard-handler.h" diff --git a/non-standard-hisilicon.c b/non-standard-hisilicon.c index 00388ec..3d9fc30 100644 --- a/non-standard-hisilicon.c +++ b/non-standard-hisilicon.c @@ -11,7 +11,7 @@ #include #include #include -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" #include "non-standard-hisilicon.h" diff --git a/non-standard-jaguarmicro.c b/non-standard-jaguarmicro.c index fb887d3..745b656 100644 --- a/non-standard-jaguarmicro.c +++ b/non-standard-jaguarmicro.c @@ -12,7 +12,7 @@ #include #include #include -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" #include "ras-non-standard-handler.h" diff --git a/non-standard-yitian.c b/non-standard-yitian.c index daadb95..d3dae38 100644 --- a/non-standard-yitian.c +++ b/non-standard-yitian.c @@ -12,7 +12,7 @@ #include #include #include -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" #include "ras-non-standard-handler.h" diff --git a/ras-aer-handler.c b/ras-aer-handler.c index 43334a9..8cfc663 100644 --- a/ras-aer-handler.c +++ b/ras-aer-handler.c @@ -21,7 +21,7 @@ #include #include #include "ras-aer-handler.h" -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "bitfield.h" #include "ras-report.h" diff --git a/ras-arm-handler.c b/ras-arm-handler.c index 01122f3..5764746 100644 --- a/ras-arm-handler.c +++ b/ras-arm-handler.c @@ -17,7 +17,7 @@ #include #include #include "ras-arm-handler.h" -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" #include "ras-non-standard-handler.h" diff --git a/ras-cxl-handler.c b/ras-cxl-handler.c index cbad347..02942bd 100644 --- a/ras-cxl-handler.c +++ b/ras-cxl-handler.c @@ -18,8 +18,9 @@ #include #include #include "ras-cxl-handler.h" -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" +#include "ras-record.h" #include "ras-report.h" #include diff --git a/ras-devlink-handler.c b/ras-devlink-handler.c index e281cd0..dfe2272 100644 --- a/ras-devlink-handler.c +++ b/ras-devlink-handler.c @@ -23,7 +23,7 @@ #include #include #include "ras-devlink-handler.h" -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" diff --git a/ras-diskerror-handler.c b/ras-diskerror-handler.c index 0fd27ae..527f7dd 100644 --- a/ras-diskerror-handler.c +++ b/ras-diskerror-handler.c @@ -27,7 +27,7 @@ #include #include #include "ras-diskerror-handler.h" -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" diff --git a/ras-events.c b/ras-events.c index 89896a0..a2a3773 100644 --- a/ras-events.c +++ b/ras-events.c @@ -45,6 +45,7 @@ #include "ras-memory-failure-handler.h" #include "ras-non-standard-handler.h" #include "ras-page-isolation.h" +#include "ras-events.h" #include "ras-record.h" #include "trigger.h" diff --git a/ras-events.h b/ras-events.h index 75ca506..135219e 100644 --- a/ras-events.h +++ b/ras-events.h @@ -19,15 +19,13 @@ #ifndef __RAS_EVENTS_H #define __RAS_EVENTS_H -#include "ras-record.h" - #include #include -#define MAX_PATH 1024 -#define STR(x) #x +#include "types.h" struct mce_priv; +struct ras_mc_offline_event; enum { MC_EVENT, diff --git a/ras-extlog-handler.c b/ras-extlog-handler.c index 6ffe906..ba48f11 100644 --- a/ras-extlog-handler.c +++ b/ras-extlog-handler.c @@ -27,7 +27,7 @@ #include "ras-extlog-handler.h" #include "ras-logger.h" -#include "ras-record.h" +#include "types.h" #include "ras-report.h" static char *err_type(int etype) diff --git a/ras-logger.h b/ras-logger.h index dbf1cb0..6afe41f 100644 --- a/ras-logger.h +++ b/ras-logger.h @@ -20,7 +20,7 @@ #include -#include "ras-record.h" +#include "types.h" /* * Logging macros diff --git a/ras-mc-handler.c b/ras-mc-handler.c index 5062f9f..74d8850 100644 --- a/ras-mc-handler.c +++ b/ras-mc-handler.c @@ -27,7 +27,7 @@ #include "ras-mc-handler.h" #include "ras-logger.h" #include "ras-page-isolation.h" -#include "ras-record.h" +#include "types.h" #include "ras-report.h" #include "trigger.h" diff --git a/ras-mce-handler.c b/ras-mce-handler.c index fc08988..aaf766f 100644 --- a/ras-mce-handler.c +++ b/ras-mce-handler.c @@ -26,7 +26,7 @@ #include #include "ras-mce-handler.h" -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" diff --git a/ras-memory-failure-handler.c b/ras-memory-failure-handler.c index 5627961..a93b034 100644 --- a/ras-memory-failure-handler.c +++ b/ras-memory-failure-handler.c @@ -19,7 +19,7 @@ #include "ras-logger.h" #include "ras-memory-failure-handler.h" -#include "ras-record.h" +#include "types.h" #include "ras-report.h" #include "trigger.h" diff --git a/ras-non-standard-handler.c b/ras-non-standard-handler.c index 19dd0c9..887ab14 100644 --- a/ras-non-standard-handler.c +++ b/ras-non-standard-handler.c @@ -18,7 +18,7 @@ #include #include #include "ras-non-standard-handler.h" -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-report.h" diff --git a/ras-non-standard-handler.h b/ras-non-standard-handler.h index 913ce00..5c6746d 100644 --- a/ras-non-standard-handler.h +++ b/ras-non-standard-handler.h @@ -15,6 +15,7 @@ #define __RAS_NON_STANDARD_HANDLER_H #include "ras-events.h" +#include "ras-record.h" #include struct ras_ns_ev_decoder { diff --git a/ras-page-isolation.h b/ras-page-isolation.h index 7ccfca1..7da68c0 100644 --- a/ras-page-isolation.h +++ b/ras-page-isolation.h @@ -18,7 +18,7 @@ #include #include -#include "ras-record.h" +#include "types.h" #include "rbtree.h" #define PAGE_SHIFT 12 diff --git a/ras-record.c b/ras-record.c index 8341ae8..e363137 100644 --- a/ras-record.c +++ b/ras-record.c @@ -28,6 +28,7 @@ #include #include #include "ras-events.h" +#include "ras-record.h" #include "ras-mc-handler.h" #include "ras-aer-handler.h" #include "ras-mce-handler.h" diff --git a/ras-record.h b/ras-record.h index 06777a2..27e484b 100644 --- a/ras-record.h +++ b/ras-record.h @@ -23,11 +23,7 @@ #include #include #include "config.h" - -#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) - -#define BIT(nr) (1UL << (nr)) -#define BIT_ULL(nr) (1ULL << (nr)) +#include "types.h" extern long user_hz; @@ -145,7 +141,6 @@ struct ras_cxl_poison_event { char overflow_ts[64]; }; -#define SZ_512 0x200 #define CXL_HEADERLOG_SIZE SZ_512 #define CXL_HEADERLOG_SIZE_U32 (SZ_512 / sizeof(uint32_t)) #define CXL_EVENT_RECORD_DATA_LENGTH 0x50 diff --git a/ras-report.h b/ras-report.h index 1cddc92..0c66446 100644 --- a/ras-report.h +++ b/ras-report.h @@ -14,8 +14,9 @@ #ifndef __RAS_REPORT_H #define __RAS_REPORT_H -#include "ras-record.h" +#include "types.h" #include "ras-events.h" +#include "ras-record.h" #include "ras-mc-handler.h" #include "ras-mce-handler.h" #include "ras-aer-handler.h" diff --git a/rasdaemon.c b/rasdaemon.c index 447ac0b..6f3a272 100644 --- a/rasdaemon.c +++ b/rasdaemon.c @@ -22,9 +22,10 @@ #include #include -#include "ras-record.h" +#include "types.h" #include "ras-logger.h" #include "ras-events.h" +#include "ras-record.h" /* * Arguments(argp) handling logic and main diff --git a/rbtree.h b/rbtree.h index bf861e5..398d318 100644 --- a/rbtree.h +++ b/rbtree.h @@ -97,11 +97,6 @@ #include -#define container_of(ptr, type, member) ({ \ - const typeof(((type *)0)->member) *__mptr = (ptr); \ - (type *)((char *)__mptr - offsetof(type, member)); \ -}) - struct rb_node { unsigned long rb_parent_color; #define RB_RED 0 diff --git a/types.h b/types.h new file mode 100644 index 0000000..1cf7556 --- /dev/null +++ b/types.h @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2013 Mauro Carvalho Chehab + * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __TYPES_H +#define __TYPES_H + +#include +#include +#include +#include + +#define STR(x) #x + +/* Please keep the macros as much as possible alined with Linux Kernel ones */ + +/* + * Force a compilation error if condition is true, but also produce a + * result (of value 0 and type int), so the expression can be used + * e.g. in a structure initializer (or where-ever else comma expressions + * aren't permitted). + */ +#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); }))) + +/* Are two types/vars the same type (ignoring qualifiers)? */ +#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) + +/* &a[0] degrades to a pointer: a different type from an array */ +#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) + +/* + * This returns a constant expression while determining if an argument is + * a constant expression, most importantly without evaluating the argument. + * Glory to Martin Uecker + * + * Details: + * - sizeof() return an integer constant expression, and does not evaluate + * the value of its operand; it only examines the type of its operand. + * - The results of comparing two integer constant expressions is also + * an integer constant expression. + * - The first literal "8" isn't important. It could be any literal value. + * - The second literal "8" is to avoid warnings about unaligned pointers; + * this could otherwise just be "1". + * - (long)(x) is used to avoid warnings about 64-bit types on 32-bit + * architectures. + * - The C Standard defines "null pointer constant", "(void *)0", as + * distinct from other void pointers. + * - If (x) is an integer constant expression, then the "* 0l" resolves + * it into an integer constant expression of value 0. Since it is cast to + * "void *", this makes the second operand a null pointer constant. + * - If (x) is not an integer constant expression, then the second operand + * resolves to a void pointer (but not a null pointer constant: the value + * is not an integer constant 0). + * - The conditional operator's third operand, "(int *)8", is an object + * pointer (to type "int"). + * - The behavior (including the return type) of the conditional operator + * ("operand1 ? operand2 : operand3") depends on the kind of expressions + * given for the second and third operands. This is the central mechanism + * of the macro: + * - When one operand is a null pointer constant (i.e. when x is an integer + * constant expression) and the other is an object pointer (i.e. our + * third operand), the conditional operator returns the type of the + * object pointer operand (i.e. "int *"). Here, within the sizeof(), we + * would then get: + * sizeof(*((int *)(...)) == sizeof(int) == 4 + * - When one operand is a void pointer (i.e. when x is not an integer + * constant expression) and the other is an object pointer (i.e. our + * third operand), the conditional operator returns a "void *" type. + * Here, within the sizeof(), we would then get: + * sizeof(*((void *)(...)) == sizeof(void) == 1 + * - The equality comparison to "sizeof(int)" therefore depends on (x): + * sizeof(int) == sizeof(int) (x) was a constant expression + * sizeof(int) != sizeof(void) (x) was not a constant expression + */ +#define __is_constexpr(x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) + +/** + * ARRAY_SIZE - get the number of elements in array @arr + * @arr: array to be sized + */ +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) + +/* BIT handling */ + +#define _AC(X,Y) (X##Y) + +#define _UL(x) (_AC(x, UL)) +#define _ULL(x) (_AC(x, ULL)) + +#define GENMASK_INPUT_CHECK(h, l) \ + (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ + __is_constexpr((l) > (h)), (l) > (h), 0))) + +#define GENMASK(h, l) \ + (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l)) +#define GENMASK_ULL(h, l) \ + (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l)) + +#define BIT(nr) (_UL(1) << (nr)) +#define BIT_ULL(nr) (_ULL(1) << (nr)) + +/* Useful constants */ + +#define MAX_PATH 1024 +#define SZ_512 0x200 + +/** + * container_of - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + * + * WARNING: any const qualifier of @ptr is lost. + */ +#define container_of(ptr, type, member) ({ \ + void *__mptr = (void *)(ptr); \ + static_assert(__same_type(*(ptr), ((type *)0)->member) || \ + __same_type(*(ptr), void), \ + "pointer type mismatch in container_of()"); \ + ((type *)(__mptr - offsetof(type, member))); }) + +#endif -- 2.49.0