From d09cd43b3f4302fba4b945fa49fe6a81d8fa2295 Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Fri, 9 Feb 2024 11:03:00 -0800 Subject: [PATCH] ANDROID: 16K: Log unaligned operations __PAGE_ALIGNED() returns true if the address is aligned. __offset_in_page() returns true if the address is unaligned. __offset_in_page() is sometimes used as a boolean instead of !__PAGE_ALIGNED(). Add __offset_in_page_log() to log unaligned addresses in syscalls. Redirect the __PAGE_ALIGNED() to !__offset_in_page_log() to achieve similar logging gor !__PAGE_ALIGNED() addresses. Logging is only enabled if the system is booted in page-compat mode (page_size_compat=*). Bug: 383389337 Bug: 315325080 Bug: 302403436 Change-Id: I57a3272a49d4a27539942d9e5a8303895d210752 Signed-off-by: Kalesh Singh --- include/linux/page_size_compat.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/include/linux/page_size_compat.h b/include/linux/page_size_compat.h index c30fa771f625..4059902922b7 100644 --- a/include/linux/page_size_compat.h +++ b/include/linux/page_size_compat.h @@ -24,6 +24,11 @@ #include #include +#include +#include + +#define pgcompat_err(fmt, ...) \ + pr_err("pgcompat [%i (%s)]: " fmt, task_pid_nr(current), current->comm, ## __VA_ARGS__) DECLARE_STATIC_KEY_FALSE(page_shift_compat_enabled); extern int page_shift_compat __ro_after_init; @@ -41,7 +46,17 @@ static __always_inline unsigned __page_shift(void) #define __PAGE_MASK (~(__PAGE_SIZE-1)) #define __PAGE_ALIGN(addr) ALIGN(addr, __PAGE_SIZE) #define __PAGE_ALIGN_DOWN(addr) ALIGN_DOWN(addr, __PAGE_SIZE) -#define __PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr), __PAGE_SIZE) + #define __offset_in_page(p) ((unsigned long)(p) & ~__PAGE_MASK) +#define __offset_in_page_log(addr) \ +({ \ + if (static_branch_unlikely(&page_shift_compat_enabled) && \ + __offset_in_page(addr)) \ + pgcompat_err("%s: addr (0x%08lx) not page aligned", __func__, addr); \ + (__offset_in_page(addr)); \ +}) + +#define __PAGE_ALIGNED(addr) (!__offset_in_page_log(addr)) + #endif /* __LINUX_PAGE_SIZE_COMPAT_H */