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 <kaleshsingh@google.com>
This commit is contained in:
Kalesh Singh
2024-02-09 11:03:00 -08:00
committed by Carlos Llamas
parent 1fb2de0c3d
commit d09cd43b3f

View File

@@ -24,6 +24,11 @@
#include <linux/align.h>
#include <linux/jump_label.h>
#include <linux/printk.h>
#include <linux/sched.h>
#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 */