Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
config MTPROF
tristate "Enable MTK profiler/debugger tool"
default n
help
CONFIG_MTPROF define if mtk profiling/debugger tools should be
built-in, module or not. It includes bootprof logger which is for
boot time profiling. If you are not sure about whether to
enable it or not, please set n.
config MTK_SCHED_MONITOR
bool "mtk scheduler monitor"
help
MTK_SCHED_MONITOR is used to monitor ISR/SOFTIRQ/TASKLET/HRTIMER
/STIMER/RCU processing duration and IRQ disabled duration. Say Y
here to enable monitor. If you are not sure about whether to enable
it or not, please set n.
config MTK_IRQ_COUNT_TRACER
bool "mtk irq count tracer"
depends on MTK_SCHED_MONITOR
help
Enable this to check the increment of irq counts in a period of
time. If the increment is above a threshold, a warning message
is printed for reference information. This feature is helpful
to catch the irq that triggered abnormally.
If unsure, say N.
config MTK_IRQ_OFF_TRACER
bool "mtk irq off tracer"
depends on MTK_SCHED_MONITOR
select TRACE_IRQFLAGS
help
Enable this to check the irq off time. If the time is above a
threshold, a warning message is printed for reference information.
This feature is helpful to catch the task that disable irq for a
long time.
If unsure, say N.
config MTK_PREEMPT_TRACER
bool "mtk preempt off tracer"
depends on MTK_SCHED_MONITOR
select PREEMPT_TRACER
help
Enable this to check the irq off time. If the time is above a
threshold, a warning message is printed for reference information.
This feature is helpful to catch the task that disable irq for a
long time.
If unsure, say N.
+20
View File
@@ -0,0 +1,20 @@
#
# Copyright (C) 2015 MediaTek Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
MTK_PLATFORM := $(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += -Idrivers/misc/mediatek/mtprof
subdir-ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat
LINUXINCLUDE += -include $(srctree)/kernel/sched/sched.h
obj-$(CONFIG_MTPROF) = bootprof.o
obj-$(CONFIG_MTK_SCHED_MONITOR) += common.o prof_main.o sched_monitor.o sched_monitor_test.o
+516
View File
@@ -0,0 +1,516 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kallsyms.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/proc_fs.h>
#include <linux/printk.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/utsname.h>
#include <linux/uaccess.h>
#ifdef MODULE
#include <linux/tracepoint.h>
#include <trace/events/initcall.h>
#endif
#define BOOT_STR_SIZE 256
#define BUF_COUNT 12
#define LOGS_PER_BUF 80
#define MSG_SIZE 128
struct log_t {
/* task cmdline for first 16 bytes
* and boot event for the rest
*/
char *comm_event;
pid_t pid;
u64 timestamp;
};
static struct log_t *bootprof[BUF_COUNT];
static unsigned long log_count;
static DEFINE_MUTEX(bootprof_lock);
static bool enabled;
static int bootprof_lk_t, bootprof_pl_t, bootprof_logo_t;
static u64 timestamp_on, timestamp_off;
static bool boot_finish;
#ifdef CONFIG_BOOTPROF_THRESHOLD_MS
#define BOOTPROF_THRESHOLD (CONFIG_BOOTPROF_THRESHOLD_MS*1000000)
#else
#define BOOTPROF_THRESHOLD 15000000
#endif
#ifdef MODULE
static unsigned long long start_time;
/* Data structures to store tracepoints information */
struct bf_tp {
const char *name;
void *func;
struct tracepoint *tp;
void *data;
bool init;
};
#else /*Build-in*/
module_param_named(pl_t, bootprof_pl_t, int, 0644);
module_param_named(lk_t, bootprof_lk_t, int, 0644);
module_param_named(logo_t, bootprof_logo_t, int, 0644);
#endif
static long long msec_high(unsigned long long nsec)
{
if ((long long)nsec < 0) {
nsec = -nsec;
do_div(nsec, 1000000);
return -nsec;
}
do_div(nsec, 1000000);
return nsec;
}
static unsigned long msec_low(unsigned long long nsec)
{
if ((long long)nsec < 0)
nsec = -nsec;
return do_div(nsec, 1000000);
}
bool mt_boot_finish(void)
{
return boot_finish;
}
EXPORT_SYMBOL_GPL(mt_boot_finish);
void bootprof_log_boot(char *str)
{
unsigned long long ts;
struct log_t *p = NULL;
size_t n;
if (!str) {
pr_info("[BOOTPROF] Null buffer. Skip log.\n");
return;
}
if (!enabled)
return;
n = strlen(str) + 1;
ts = sched_clock();
pr_info("BOOTPROF:%10lld.%06ld:%s\n", msec_high(ts), msec_low(ts), str);
mutex_lock(&bootprof_lock);
if (log_count >= (LOGS_PER_BUF * BUF_COUNT)) {
pr_info("[BOOTPROF] not enuough bootprof buffer\n");
goto out;
} else if (log_count && !(log_count % LOGS_PER_BUF)) {
bootprof[log_count / LOGS_PER_BUF] =
kcalloc(LOGS_PER_BUF, sizeof(struct log_t),
GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN);
}
if (!bootprof[log_count / LOGS_PER_BUF]) {
pr_info("no memory for bootprof\n");
goto out;
}
p = &bootprof[log_count / LOGS_PER_BUF][log_count % LOGS_PER_BUF];
p->timestamp = ts;
p->pid = current->pid;
n += TASK_COMM_LEN;
p->comm_event = kzalloc(n, GFP_ATOMIC | __GFP_NORETRY |
__GFP_NOWARN);
if (!p->comm_event) {
enabled = false;
goto out;
}
memcpy(p->comm_event, current->comm, TASK_COMM_LEN);
memcpy(p->comm_event + TASK_COMM_LEN, str, n - TASK_COMM_LEN);
log_count++;
out:
mutex_unlock(&bootprof_lock);
}
EXPORT_SYMBOL_GPL(bootprof_log_boot);
static void bootprof_bootloader(void)
{
struct device_node *node;
int err = 0;
node = of_find_node_by_name(NULL, "bootprof");
if (node) {
err |= of_property_read_s32(node, "pl_t", &bootprof_pl_t);
err |= of_property_read_s32(node, "lk_t", &bootprof_lk_t);
err |= of_property_read_s32(node, "lk_logo_t",
&bootprof_logo_t);
pr_info("BOOTPROF: DT(Err:0x%x) pl_t=%d, lk_t=%d, lk_logo_t=%d\n",
err, bootprof_pl_t, bootprof_lk_t, bootprof_logo_t);
}
}
void bootprof_initcall(initcall_t fn, unsigned long long ts)
{
/* log more than threshold initcalls */
unsigned long msec_rem;
char msgbuf[MSG_SIZE];
int len;
if (ts > BOOTPROF_THRESHOLD) {
msec_rem = do_div(ts, NSEC_PER_MSEC);
len = scnprintf(msgbuf, sizeof(msgbuf),
"initcall: %ps %5llu.%06lums",
fn, ts, msec_rem);
if (len < 0)
pr_info("BOOTPROF: initcall - Invalid argument.\n");
bootprof_log_boot(msgbuf);
}
}
#ifndef MODULE
/*Build-in*/
void bootprof_probe(unsigned long long ts, struct device *dev,
struct device_driver *drv, unsigned long probe)
{
/* log more than threshold probes*/
unsigned long msec_rem;
char msgbuf[MSG_SIZE];
int pos, len;
if (ts <= BOOTPROF_THRESHOLD)
return;
msec_rem = do_div(ts, NSEC_PER_MSEC);
pos = scnprintf(msgbuf, sizeof(msgbuf), "probe: probe=%ps",
(void *)probe);
if (pos < 0)
pos = 0;
if (drv) {
len = scnprintf(msgbuf + pos, sizeof(msgbuf) - pos,
" drv=%s(%ps)", drv->name ? drv->name : "",
(void *)drv);
if (len >= 0)
pos += len;
}
if (dev && dev->init_name) {
len = scnprintf(msgbuf + pos, sizeof(msgbuf) - pos,
" dev=%s(%ps)", dev->init_name, (void *)dev);
if (len >= 0)
pos += len;
}
scnprintf(msgbuf + pos, sizeof(msgbuf) - pos,
" %5llu.%06lums", ts, msec_rem);
bootprof_log_boot(msgbuf);
}
void bootprof_pdev_register(unsigned long long ts, struct platform_device *pdev)
{
/* log more than threshold register*/
unsigned long msec_rem;
char msgbuf[MSG_SIZE];
int len;
if (ts <= BOOTPROF_THRESHOLD || !pdev)
return;
msec_rem = do_div(ts, NSEC_PER_MSEC);
len = scnprintf(msgbuf, sizeof(msgbuf),
"probe: pdev=%s(%ps) %5llu.%06lums",
pdev->name, (void *)pdev, ts, msec_rem);
if (len < 0)
pr_info("BOOTPROF: pdev - Invalid argument.\n");
bootprof_log_boot(msgbuf);
}
static void bootup_finish(void)
{
initcall_debug = 0;
}
#endif /*MODULE END*/
static void mt_bootprof_switch(int on)
{
mutex_lock(&bootprof_lock);
if (enabled ^ on) {
unsigned long long ts = sched_clock();
pr_info("BOOTPROF:%10lld.%06ld: %s\n",
msec_high(ts), msec_low(ts), on ? "ON" : "OFF");
if (on) {
enabled = 1;
timestamp_on = ts;
} else {
enabled = 0;
timestamp_off = ts;
if (!boot_finish)
boot_finish = true;
#ifndef MODULE
bootup_finish();
#endif
}
}
mutex_unlock(&bootprof_lock);
}
static ssize_t
mt_bootprof_write(struct file *filp, const char *ubuf, size_t cnt, loff_t *data)
{
char buf[BOOT_STR_SIZE];
size_t copy_size = cnt;
if (cnt >= sizeof(buf))
copy_size = BOOT_STR_SIZE - 1;
if (copy_from_user(&buf, ubuf, copy_size))
return -EFAULT;
if (cnt == 1 && buf[0] == '1') {
mt_bootprof_switch(1);
return 1;
} else if (cnt == 1 && buf[0] == '0') {
mt_bootprof_switch(0);
return 1;
}
buf[copy_size] = 0;
bootprof_log_boot(buf);
return cnt;
}
static int mt_bootprof_show(struct seq_file *m, void *v)
{
int i;
struct log_t *p;
if (!m) {
pr_info("seq_file is Null.\n");
return 0;
}
seq_puts(m, "----------------------------------------\n");
seq_printf(m, "%d BOOT PROF (unit:msec)\n", enabled);
seq_puts(m, "----------------------------------------\n");
if (bootprof_pl_t > 0 && bootprof_lk_t > 0) {
seq_printf(m, "%10d : %s\n", bootprof_pl_t, "preloader");
if (bootprof_logo_t > 0) {
seq_printf(m, "%10d : %s (%s: %d)\n",
bootprof_lk_t, "lk", "Start->Show logo",
bootprof_logo_t);
} else {
seq_printf(m, "%10d : %s\n",
bootprof_lk_t, "lk");
}
seq_puts(m, "----------------------------------------\n");
}
seq_printf(m, "%10lld.%06ld : ON (Threshold:%5lldms)\n",
msec_high(timestamp_on), msec_low(timestamp_on),
msec_high(BOOTPROF_THRESHOLD));
for (i = 0; i < log_count; i++) {
p = &bootprof[i / LOGS_PER_BUF][i % LOGS_PER_BUF];
if (!p->comm_event)
continue;
seq_printf(m, "%10llu.%06lu :%5d-%-16s: %s\n",
msec_high(p->timestamp),
msec_low(p->timestamp),
p->pid, p->comm_event,
p->comm_event + TASK_COMM_LEN);
}
seq_printf(m, "%10lld.%06ld : OFF\n",
msec_high(timestamp_off), msec_low(timestamp_off));
seq_puts(m, "----------------------------------------\n");
return 0;
}
/*** Seq operation of mtprof ****/
static int mt_bootprof_open(struct inode *inode, struct file *file)
{
return single_open(file, mt_bootprof_show, inode->i_private);
}
static const struct file_operations mt_bootprof_fops = {
.open = mt_bootprof_open,
.write = mt_bootprof_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
#ifdef MODULE
/* initcalls tracepoint cb if initcall_debug=1 */
static __init_or_module void
tp_initcall_start_cb(void *data, initcall_t fn)
{
unsigned long long *start_ts = (unsigned long long *)data;
*start_ts = sched_clock();
}
static __init_or_module void
tp_initcall_finish_cb(void *data, initcall_t fn, int ret)
{
unsigned long long *start_ts = (unsigned long long *)data;
unsigned long long end_ts, duration;
/*For bootprof module without initcall_start*/
if (*start_ts == 0) {
bootprof_log_boot("Kernel_init_done");
return;
}
end_ts = sched_clock();
duration = end_ts - *start_ts;
bootprof_initcall(fn, duration);
*start_ts = 0;
}
static struct bf_tp tp_table[] = {
{.name = "initcall_start", .func = tp_initcall_start_cb,
.data = &start_time},
{.name = "initcall_finish", .func = tp_initcall_finish_cb,
.data = &start_time},
};
/* Find the struct tracepoint* associated with a given tracepoint */
/* name. */
static void tp_lookup(struct tracepoint *tp, void *ignore)
{
int i;
if (!tp || !tp->name)
return;
for (i = 0; i < sizeof(tp_table) / sizeof(struct bf_tp); i++) {
if (strcmp(tp_table[i].name, tp->name) == 0)
tp_table[i].tp = tp;
}
}
/* claen up initcalls tracepoints */
static void tp_cleanup(void)
{
int i;
for (i = 0; i < sizeof(tp_table) / sizeof(struct bf_tp); i++) {
if (tp_table[i].init) {
tracepoint_probe_unregister(tp_table[i].tp,
tp_table[i].func, tp_table[i].data);
tp_table[i].init = false;
}
}
}
/* Register initcalls tracepoints */
static void tp_init(void)
{
int i;
/* Install the tracepoints */
for_each_kernel_tracepoint(tp_lookup, NULL);
for (i = 0; i < sizeof(tp_table) / sizeof(struct bf_tp); i++) {
if (!tp_table[i].tp) {
pr_info("[BOOTPROF]TP: %s not found\n",
tp_table[i].name);
/* Unload previously loaded */
tp_cleanup();
return;
}
tracepoint_probe_register(tp_table[i].tp, tp_table[i].func,
tp_table[i].data);
tp_table[i].init = true;
}
}
static int __init bootprof_init(void)
{
struct proc_dir_entry *pe;
memset(bootprof, 0, sizeof(struct log_t *) * BUF_COUNT);
bootprof[0] = kcalloc(LOGS_PER_BUF, sizeof(struct log_t),
GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN);
if (!bootprof[0])
goto fail;
pe = proc_create("bootprof", 0664, NULL, &mt_bootprof_fops);
if (!pe)
return -ENOMEM;
bootprof_bootloader();
tp_init();
mt_bootprof_switch(1);
fail:
return 0;
}
static void __exit bootprof_exit(void)
{
struct log_t *p = NULL;
int i;
enabled = 0;
tp_cleanup();
if (log_count > 0) {
mutex_lock(&bootprof_lock);
for (i = 0; i < log_count; i++) {
p = &bootprof[i / LOGS_PER_BUF][i % LOGS_PER_BUF];
kfree(p->comm_event);
}
for (i = 0; i < ((log_count / LOGS_PER_BUF) + 1); i++)
kfree(bootprof[i]);
mutex_unlock(&bootprof_lock);
}
remove_proc_entry("bootprof", NULL);
pr_info("bootprof module exit.\n");
}
module_init(bootprof_init);
module_exit(bootprof_exit);
MODULE_DESCRIPTION("MEDIATEK BOOT TIME PROFILING");
MODULE_LICENSE("GPL v2");
#else /*Build-in*/
static int __init init_boot_prof(void)
{
struct proc_dir_entry *pe;
pe = proc_create("bootprof", 0664, NULL, &mt_bootprof_fops);
if (!pe)
return -ENOMEM;
return 0;
}
static int __init init_bootprof_buf(void)
{
memset(bootprof, 0, sizeof(struct log_t *) * BUF_COUNT);
bootprof[0] = kcalloc(LOGS_PER_BUF, sizeof(struct log_t),
GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN);
if (!bootprof[0])
goto fail;
bootprof_bootloader();
mt_bootprof_switch(1);
fail:
return 0;
}
early_initcall(init_bootprof_buf);
device_initcall(init_boot_prof);
#endif /*MODULE END*/
+61
View File
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2015 MediaTek Inc.
*/
#include <linux/irq.h>
#include <linux/irqnr.h>
#include <linux/interrupt.h>
#include "internal.h"
long long msec_high(unsigned long long nsec)
{
if ((long long)nsec < 0) {
nsec = -nsec;
do_div(nsec, 1000000);
return -nsec;
}
do_div(nsec, 1000000);
return nsec;
}
unsigned long msec_low(unsigned long long nsec)
{
if ((long long)nsec < 0)
nsec = -nsec;
return do_div(nsec, 1000000);
}
long long usec_high(unsigned long long nsec)
{
if ((long long)nsec < 0) {
nsec = -nsec;
do_div(nsec, 1000);
return -nsec;
}
do_div(nsec, 1000);
return nsec;
}
long long sec_high(unsigned long long nsec)
{
if ((long long)nsec < 0) {
nsec = -nsec;
do_div(nsec, 1000000000);
return -nsec;
}
do_div(nsec, 1000000000);
return nsec;
}
unsigned long sec_low(unsigned long long nsec)
{
if ((long long)nsec < 0)
nsec = -nsec;
/* remove nsec partition */
return do_div(nsec, 1000000000) / 1000;
}
+88
View File
@@ -0,0 +1,88 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2015 MediaTek Inc.
*/
/* common and private utility for mtprof */
#include <linux/seq_file.h>
#include <linux/sched.h>
#define SEQ_printf(m, x...) \
do { \
if (m) \
seq_printf(m, x); \
else \
pr_info(x); \
} while (0)
#define MT_DEBUG_ENTRY(name) \
void mt_##name##_switch(int on); \
static int mt_##name##_show(struct seq_file *m, void *v);\
static ssize_t mt_##name##_write(struct file *filp, const char *ubuf, \
size_t cnt, loff_t *data);\
static int mt_##name##_open(struct inode *inode, struct file *file) \
{ \
return single_open(file, mt_##name##_show, inode->i_private); \
} \
static const struct file_operations mt_##name##_fops = { \
.open = mt_##name##_open, \
.write = mt_##name##_write, \
.read = seq_read, \
.llseek = seq_lseek, \
.release = single_release, \
}
#define DEFINE_SCHED_MON_OPS(param, type, min, max) \
static ssize_t sched_mon_##param##_write(struct file *filp, \
const char *ubuf, size_t count, loff_t *data) \
{ \
char buf[32]; \
unsigned int val = 0; \
\
if (!sched_mon_door) \
return -EPERM; \
\
if (count >= sizeof(buf) || count < 1) \
return -EINVAL; \
\
if (copy_from_user(&buf, ubuf, count)) \
return -EFAULT; \
\
buf[count] = 0; \
if (kstrtouint(buf, 10, &val)) \
return -EINVAL; \
\
if (val < min || val > max) \
return -EINVAL; \
\
param = (type)val; \
\
return count; \
} \
static int sched_mon_##param##_show(struct seq_file *s, void *p) \
{ \
seq_printf(s, "%d\n", param); \
return 0; \
} \
static int sched_mon_##param##_open(struct inode *inode, struct file *file) \
{ \
return single_open(file, sched_mon_##param##_show, inode->i_private); \
} \
static const struct file_operations sched_mon_##param##_fops = { \
.open = sched_mon_##param##_open, \
.write = sched_mon_##param##_write, \
.read = seq_read, \
.llseek = seq_lseek, \
.release = single_release, \
}
/* for bootprof.c */
unsigned int gpt_boot_time(void);
long long msec_high(unsigned long long nsec);
unsigned long msec_low(unsigned long long nsec);
long long usec_high(unsigned long long nsec);
long long sec_high(unsigned long long nsec);
unsigned long sec_low(unsigned long long nsec);
void mt_sched_monitor_test_init(struct proc_dir_entry *dir);
@@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#undef TRACE_SYSTEM
#define TRACE_SYSTEM sched_mon
#if !defined(_TRACE_SCHED_MON_H) || defined(TRACE_HEADER_MULTI_READ)
#define _TRACE_SCHED_MON_H
#include <linux/tracepoint.h>
TRACE_EVENT(sched_mon_msg,
TP_PROTO(const char *buf),
TP_ARGS(buf),
TP_STRUCT__entry(
__string(mbuf, buf)
),
TP_fast_assign(
__assign_str(mbuf, buf);
),
TP_printk("%s", __get_str(mbuf))
);
#endif /* _TRACE_SCHED_MON_H */
#undef TRACE_INCLUDE_PATH
#define TRACE_INCLUDE_PATH .
#undef TRACE_INCLUDE_FILE
#define TRACE_INCLUDE_FILE mtk_sched_mon_trace
/* This part must be outside protection */
#include <trace/define_trace.h>
+349
View File
@@ -0,0 +1,349 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2015 MediaTek Inc.
*/
#define DEBUG 1
#include <linux/slab.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/kallsyms.h>
#include <linux/utsname.h>
#include <linux/uaccess.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/pid.h>
#include "internal.h"
#include <linux/signal.h>
#include <trace/events/signal.h>
//#define MTK_DEATH_SIGNAL_LOG
//#define MTK_FORK_EXIT_LOG
#define STORE_SIGINFO(_errno, _code, info) \
do { \
if (info == SEND_SIG_NOINFO || \
info == SEND_SIG_FORCED) { \
_errno = 0; \
_code = SI_USER; \
} else if (info == SEND_SIG_PRIV) { \
_errno = 0; \
_code = SI_KERNEL; \
} else { \
_errno = info->si_errno; \
_code = info->si_code; \
} \
} while (0)
#define SIGNAL_DELIVER_RES_LEN 5
static const char * const signal_deliver_results[] = {
"delivered",
"ignored",
"already_pending",
"overflow_fail",
"lost_info",
};
/* 7. signal logs */
MT_DEBUG_ENTRY(signal_log);
enum {
SI_GENERATE = (1 << 0),
SI_DELIVER = (1 << 1),
} SI_LOG_MASK;
#ifndef TASK_STATE_TO_CHAR_STR
#define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"
#endif
static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
static unsigned int enabled_signal_log;
static void probe_signal_generate(void *ignore, int sig, struct siginfo *info,
struct task_struct *task, int group, int result)
{
unsigned int state = task->state ? __ffs(task->state) + 1 : 0;
int errno, code;
bool res_state = true;
/*
* only log delivered signals
*/
STORE_SIGINFO(errno, code, info);
if (result < 0 || result >= SIGNAL_DELIVER_RES_LEN)
res_state = false;
printk_deferred("[signal][%d:%s]generate sig %d to [%d:%s:%c] errno=%d code=%d grp=%d res=%s\n",
current->pid, current->comm, sig,
task->pid, task->comm,
state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?',
errno, code, group,
res_state ? signal_deliver_results[result] : "?");
}
static void probe_signal_deliver(void *ignore, int sig, struct siginfo *info,
struct k_sigaction *ka)
{
int errno, code;
STORE_SIGINFO(errno, code, info);
printk_deferred("[signal]sig %d delivered to [%d:%s] errno=%d code=%d sa_handler=%lx sa_flags=%lx\n",
sig, current->pid, current->comm, errno, code,
(unsigned long)ka->sa.sa_handler, ka->sa.sa_flags);
}
#ifdef MTK_DEATH_SIGNAL_LOG
static void probe_death_signal(void *ignore, int sig, struct siginfo *info,
struct task_struct *task, int _group, int result)
{
struct signal_struct *signal = task->signal;
unsigned int state;
int group;
/*
* all action will cause process coredump or terminate
* kernel log reduction: only print delivered signals
*/
if (sig_fatal(task, sig) && result == TRACE_SIGNAL_DELIVERED) {
signal = task->signal;
group = _group ||
(signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP));
/*
* kernel log reduction
* skip SIGRTMIN because it's used as timer signal
* skip if the target thread is already dead
*/
if (sig == SIGRTMIN ||
(task->state & (TASK_DEAD | EXIT_DEAD | EXIT_ZOMBIE)))
return;
/*
* Global init gets no signals it doesn't want.
* Container-init gets no signals it doesn't want from same
* container.
*
* Note that if global/container-init sees a sig_kernel_only()
* signal here, the signal must have been generated internally
* or must have come from an ancestor namespace. In either
* case, the signal cannot be dropped.
*/
if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
!sig_kernel_only(sig))
return;
/*
* kernel log reduction
* only print process instead of all threads
*/
if (group && (task != task->group_leader))
return;
state = task->state ? __ffs(task->state) + 1 : 0;
printk_deferred("[signal][%d:%s]send death sig %d to[%d:%s:%c]\n",
current->pid, current->comm,
sig, task->pid, task->comm,
state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
} else if ((sig_kernel_stop(sig) && result == TRACE_SIGNAL_DELIVERED) ||
sig == SIGCONT) {
/*
* kernel log reduction
* only print process instead of all threads
*/
if (_group && (task != task->group_leader))
return;
state = task->state ? __ffs(task->state) + 1 : 0;
printk_deferred("[signal][%d:%s]send %s sig %d to[%d:%s:%c]\n",
current->pid, current->comm,
(sig == SIGCONT) ? "continue" : "stop",
sig, task->pid, task->comm,
state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
}
}
#endif
static int mt_signal_log_show(struct seq_file *m, void *v)
{
SEQ_printf(m, "%d: debug message for signal being generated\n",
SI_GENERATE);
SEQ_printf(m, "%d: debug message for signal being delivered\n",
SI_DELIVER);
SEQ_printf(m, "%d: enable all logs\n", SI_GENERATE | SI_DELIVER);
SEQ_printf(m, "%d\n", enabled_signal_log);
return 0;
}
static ssize_t mt_signal_log_write(struct file *filp, const char *ubuf,
size_t cnt, loff_t *data)
{
unsigned long val = 0;
unsigned long update;
int ret;
ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
if (ret)
return ret;
update = enabled_signal_log ^ val;
if (update & SI_GENERATE) {
if (val & SI_GENERATE)
register_trace_signal_generate(probe_signal_generate,
NULL);
else
unregister_trace_signal_generate(probe_signal_generate,
NULL);
}
if (update & SI_DELIVER) {
if (val & SI_DELIVER)
register_trace_signal_deliver(probe_signal_deliver,
NULL);
else
unregister_trace_signal_deliver(probe_signal_deliver,
NULL);
}
enabled_signal_log = val;
return cnt;
}
static void __init init_signal_log(void)
{
if (enabled_signal_log & SI_GENERATE)
register_trace_signal_generate(probe_signal_generate, NULL);
if (enabled_signal_log & SI_DELIVER)
register_trace_signal_deliver(probe_signal_deliver, NULL);
#ifdef MTK_DEATH_SIGNAL_LOG
register_trace_signal_generate(probe_death_signal, NULL);
#endif
}
#ifdef MTK_FORK_EXIT_LOG
/* 8. fork & exit logs */
#include <trace/events/sched.h>
MT_DEBUG_ENTRY(fork_exit_log);
enum {
DO_FORK = (1 << 0),
DO_EXIT = (1 << 1),
} FORK_EXIT_LOG_MASK;
static unsigned int enabled_fork_exit_log;
static void probe_sched_fork_time(void *ignore,
struct task_struct *parent,
struct task_struct *child, unsigned long long dur)
{
char parent_comm[TASK_COMM_LEN], child_comm[TASK_COMM_LEN];
pid_t parent_pid, child_pid;
unsigned long long fork_time;
memcpy(parent_comm, parent->comm, TASK_COMM_LEN);
parent_pid = parent->pid;
memcpy(child_comm, child->comm, TASK_COMM_LEN);
child_pid = child->pid;
fork_time = dur;
pr_debug("[fork]comm=%s pid=%d fork child_comm=%s child_pid=%d, total fork time=%llu us",
parent_comm, parent_pid, child_comm, child_pid, fork_time);
}
static void probe_sched_process_exit(void *ignore, struct task_struct *p)
{
char comm[TASK_COMM_LEN];
pid_t pid;
int prio;
memcpy(comm, p->comm, TASK_COMM_LEN);
pid = p->pid;
prio = p->prio;
pr_debug("[exit]comm=%s pid=%d prio=%d exited", comm, pid, prio);
}
static int mt_fork_exit_log_show(struct seq_file *m, void *v)
{
SEQ_printf(m, "%d: debug message for fork\n", DO_FORK);
SEQ_printf(m, "%d: debug message for exit\n", DO_EXIT);
SEQ_printf(m, "%d: enable all logs\n", DO_FORK | DO_EXIT);
SEQ_printf(m, "%d\n", enabled_fork_exit_log);
return 0;
}
static ssize_t mt_fork_exit_log_write(struct file *filp, const char *ubuf,
size_t cnt, loff_t *data)
{
unsigned long val = 0;
unsigned long update;
int ret;
ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
if (ret)
return ret;
update = enabled_fork_exit_log ^ val;
if (update & DO_FORK) {
if (val & DO_FORK)
register_trace_sched_fork_time(probe_sched_fork_time,
NULL);
else
unregister_trace_sched_fork_time(probe_sched_fork_time,
NULL);
}
if (update & DO_EXIT) {
if (val & DO_EXIT)
register_trace_sched_process_exit(
probe_sched_process_exit,
NULL);
else
unregister_trace_sched_process_exit(
probe_sched_process_exit,
NULL);
}
enabled_fork_exit_log = val;
return cnt;
}
static void __init init_fork_exit_log(void)
{
if (enabled_fork_exit_log & DO_FORK)
register_trace_sched_fork_time(probe_sched_fork_time, NULL);
if (enabled_fork_exit_log & DO_EXIT)
register_trace_sched_process_exit(probe_sched_process_exit,
NULL);
}
#endif
/*-------------------------------------------------------------------*/
static int __init init_mtsched_prof(void)
{
struct proc_dir_entry *pe;
if (!proc_mkdir("mtprof", NULL))
return -1;
init_signal_log();
pe = proc_create("mtprof/signal_log", 0664, NULL, &mt_signal_log_fops);
if (!pe)
return -ENOMEM;
#ifdef MTK_FORK_EXIT_LOG
init_fork_exit_log();
pe = proc_create("mtprof/fork_exit_log", 0664, NULL,
&mt_fork_exit_log_fops);
if (!pe)
return -ENOMEM;
#endif
return 0;
}
device_initcall(init_mtsched_prof);
@@ -0,0 +1,843 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2015 MediaTek Inc.
*/
#define DEBUG 1
#include <linux/module.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
#include <linux/of.h>
#include "mtk_sched_mon.h"
#include "internal.h"
#define CREATE_TRACE_POINTS
#include "mtk_sched_mon_trace.h"
#define MAX_STACK_TRACE_DEPTH 32
static bool sched_mon_door;
bool irq_time_tracer __read_mostly;
unsigned int irq_time_th1_ms = 100; /* log */
unsigned int irq_time_th2_ms = 500; /* aee */
unsigned int irq_time_aee_limit;
#ifdef CONFIG_MTK_IRQ_COUNT_TRACER
static bool irq_count_tracer __read_mostly;
static unsigned int irq_period_th1_ns = 200000; /* log */
static unsigned int irq_period_th2_ns = 200000; /* aee */
static unsigned int irq_count_aee_limit;
/* period setting for specific irqs */
struct irq_count_period_setting {
const char *name;
unsigned int period;
} irq_count_plist[] = {
{"usb0", 16666}, /* 60000 irqs per sec*/
{"ufshcd", 10000}, /* 100000 irqs per sec*/
{"arch_timer", 50000}, /* 20000 irqs per sec*/
{"musb-hdrc", 16666} /* 60000 irqs per sec*/
};
#endif
#ifdef CONFIG_MTK_IRQ_OFF_TRACER
static bool irq_off_tracer __read_mostly;
static bool irq_off_tracer_trace;
static unsigned int irq_off_th1_ms = 9; /* trace */
static unsigned int irq_off_th2_ms = 500; /* print */
static unsigned int irq_off_th3_ms = 500; /* aee */
static unsigned int irq_off_aee_limit;
static unsigned int irq_off_aee_debounce_ms = 60000;
#endif
#ifdef CONFIG_MTK_PREEMPT_TRACER
static bool preempt_tracer __read_mostly;
static bool preempt_tracer_trace;
static unsigned int preempt_th1_ms = 60000; /* trace */
static unsigned int preempt_th2_ms = 180000; /* print */
static unsigned int preempt_th3_ms; /* aee */
static unsigned int preempt_aee_limit;
#endif
DEFINE_PER_CPU(struct irq_handle_status, irq_note);
DEFINE_PER_CPU(struct irq_handle_status, ipi_note);
DEFINE_PER_CPU(struct irq_handle_status, softirq_note);
const char *irq_to_name(int irq)
{
struct irq_desc *desc;
desc = irq_to_desc(irq);
if (desc && desc->action && desc->action->name)
return desc->action->name;
return NULL;
}
void sched_mon_msg(int out, char *buf, ...)
{
char str[128];
va_list args;
va_start(args, buf);
vsnprintf(str, sizeof(str), buf, args);
va_end(args);
if (out & TO_FTRACE)
trace_sched_mon_msg_rcuidle(str);
if (out & TO_KERNEL_LOG)
pr_info("%s\n", str);
if (out & TO_DEFERRED)
printk_deferred("%s\n", str);
if (out & TO_SRAM) {
pr_aee_sram(str);
pr_aee_sram("\n");
}
}
static ssize_t
sched_mon_door_write(struct file *filp, const char *ubuf,
size_t cnt, loff_t *data)
{
char buf[16];
if (cnt >= sizeof(buf) || cnt <= 1UL)
return cnt;
if (copy_from_user(&buf, ubuf, cnt))
return -EFAULT;
buf[cnt - 1UL] = 0;
if (!strcmp("open", buf))
sched_mon_door = 1;
if (!strcmp("close", buf))
sched_mon_door = 0;
return cnt;
}
static const struct file_operations sched_mon_door_fops = {
.open = simple_open,
.write = sched_mon_door_write,
};
DEFINE_SCHED_MON_OPS(irq_time_tracer, bool, 0, 1);
DEFINE_SCHED_MON_OPS(irq_time_th1_ms, unsigned int, 1, 1000000);
DEFINE_SCHED_MON_OPS(irq_time_th2_ms, unsigned int, 0, 1000000);
DEFINE_SCHED_MON_OPS(irq_time_aee_limit, unsigned int, 0, 100);
#ifdef CONFIG_MTK_IRQ_COUNT_TRACER
DEFINE_SCHED_MON_OPS(irq_count_tracer, bool, 0, 1);
DEFINE_SCHED_MON_OPS(irq_period_th1_ns, unsigned int, 1, 1000000000);
DEFINE_SCHED_MON_OPS(irq_period_th2_ns, unsigned int, 0, 1000000000);
DEFINE_SCHED_MON_OPS(irq_count_aee_limit, unsigned int, 0, 100);
#endif
#ifdef CONFIG_MTK_IRQ_OFF_TRACER
DEFINE_SCHED_MON_OPS(irq_off_tracer, bool, 0, 1);
DEFINE_SCHED_MON_OPS(irq_off_tracer_trace, bool, 0, 1);
DEFINE_SCHED_MON_OPS(irq_off_th1_ms, unsigned int, 1, 5000);
DEFINE_SCHED_MON_OPS(irq_off_th2_ms, unsigned int, 1, 5000);
DEFINE_SCHED_MON_OPS(irq_off_th3_ms, unsigned int, 0, 5000);
DEFINE_SCHED_MON_OPS(irq_off_aee_limit, unsigned int, 0, 100);
DEFINE_SCHED_MON_OPS(irq_off_aee_debounce_ms, unsigned int, 0, 6000000);
#endif
#ifdef CONFIG_MTK_PREEMPT_TRACER
DEFINE_SCHED_MON_OPS(preempt_tracer, bool, 0, 1);
DEFINE_SCHED_MON_OPS(preempt_tracer_trace, bool, 0, 1);
DEFINE_SCHED_MON_OPS(preempt_th1_ms, unsigned int, 1, 1000000);
DEFINE_SCHED_MON_OPS(preempt_th2_ms, unsigned int, 1, 1000000);
DEFINE_SCHED_MON_OPS(preempt_th3_ms, unsigned int, 0, 1000000);
DEFINE_SCHED_MON_OPS(preempt_aee_limit, unsigned int, 0, 100);
#endif
#define show_irq_handle(cpu, irq_type, data, output) \
sched_mon_msg(output, \
"%s%s:%d, start:%lld.%06lu, end:%lld.%06lu, dur:%lld us", \
per_cpu(data, cpu).end == 0 ? "In " : "", \
irq_type, per_cpu(data, cpu).irq, \
sec_high(per_cpu(data, cpu).start), \
sec_low(per_cpu(data, cpu).start), \
sec_high(per_cpu(data, cpu).end), \
sec_low(per_cpu(data, cpu).end), \
usec_high(per_cpu(data, cpu).end == 0 ? 0 : \
per_cpu(data, cpu).end - per_cpu(data, cpu).start))
static void __show_irq_handle_info(int output)
{
int cpu;
for_each_possible_cpu(cpu) {
sched_mon_msg(output, "CPU: %d", cpu);
show_irq_handle(cpu, "IRQ", irq_note, output);
show_irq_handle(cpu, "IPI", ipi_note, output);
show_irq_handle(cpu, "SoftIRQ", softirq_note, output);
sched_mon_msg(output, "");
}
}
void show_irq_handle_info(int output)
{
if (irq_time_tracer)
__show_irq_handle_info(output);
}
#ifdef CONFIG_MTK_IRQ_COUNT_TRACER
/*
* If a irq is frequently triggered, it could result in problems.
* The purpose of this feature is to catch the condition. When the
* average time interval of a irq is below the threshold, we judge
* the irq is triggered abnormally and print a message for reference.
*
* average time interval =
* statistics time / irq count increase during the statistics time
*/
static struct hrtimer irq_count_timer;
#define MAX_IRQ_NUM 1024
struct irq_count_stat {
struct irq_work work;
unsigned long long t_start;
unsigned long long t_end;
unsigned int count[MAX_IRQ_NUM];
};
DEFINE_PER_CPU(struct irq_count_stat, irq_count_data);
struct irq_count_all {
spinlock_t lock; /* protect this struct */
unsigned long long ts;
unsigned long long te;
unsigned int num[MAX_IRQ_NUM];
unsigned int diff[MAX_IRQ_NUM];
bool warn[MAX_IRQ_NUM];
};
#define REC_NUM 4
static struct irq_count_all irq_cpus[REC_NUM];
static unsigned int rec_indx;
static void __irq_count_tracer_work(struct irq_work *work)
{
struct irq_count_stat *irq_cnt = this_cpu_ptr(&irq_count_data);
int cpu = smp_processor_id();
int irq, irq_num, i, skip;
unsigned int count;
unsigned long long t_avg, t_diff, t_diff_ms;
char msg[128];
int list_num = ARRAY_SIZE(irq_count_plist);
/* skip checking if more than one work is blocked in queue */
if (sched_clock() - irq_cnt->t_end < 500000000ULL)
return;
/* check irq count on all cpu */
if (cpu == 0) {
unsigned int pre_idx;
unsigned int pre_num;
spin_lock(&irq_cpus[rec_indx].lock);
pre_idx = rec_indx ? rec_indx - 1 : REC_NUM - 1;
irq_cpus[rec_indx].ts = irq_cpus[pre_idx].te;
irq_cpus[rec_indx].te = sched_clock();
for (irq = 0; irq < min_t(int, nr_irqs, MAX_IRQ_NUM); irq++) {
irq_num = kstat_irqs(irq);
pre_num = irq_cpus[pre_idx].num[irq];
irq_cpus[rec_indx].num[irq] = irq_num;
irq_cpus[rec_indx].diff[irq] = irq_num - pre_num;
irq_cpus[rec_indx].warn[irq] = 0;
}
spin_unlock(&irq_cpus[rec_indx].lock);
rec_indx = (rec_indx == REC_NUM - 1) ? 0 : rec_indx + 1;
}
irq_cnt->t_start = irq_cnt->t_end;
irq_cnt->t_end = sched_clock();
t_diff = irq_cnt->t_end - irq_cnt->t_start;
for (irq = 0; irq < min_t(int, nr_irqs, MAX_IRQ_NUM); irq++) {
irq_num = kstat_irqs_cpu(irq, cpu);
count = irq_num - irq_cnt->count[irq];
/* The irq is not triggered in this period */
if (count == 0)
continue;
irq_cnt->count[irq] = irq_num;
t_avg = t_diff;
t_diff_ms = t_diff;
do_div(t_avg, count);
do_div(t_diff_ms, 1000000);
if (t_avg > irq_period_th1_ns)
continue;
if (!irq_to_name(irq))
continue;
for (i = 0, skip = 0; i < list_num && !skip; i++) {
if (!strcmp(irq_to_name(irq), irq_count_plist[i].name))
if (t_avg > irq_count_plist[i].period)
skip = 1;
}
if (skip)
continue;
snprintf(msg, sizeof(msg),
"irq:%d %s count +%d in %lld ms, from %lld.%06lu to %lld.%06lu on CPU:%d",
irq, irq_to_name(irq), count, t_diff_ms,
sec_high(irq_cnt->t_start), sec_low(irq_cnt->t_start),
sec_high(irq_cnt->t_end), sec_low(irq_cnt->t_end),
raw_smp_processor_id());
sched_mon_msg(TO_BOTH, msg);
for (i = 0; i < REC_NUM; i++) {
spin_lock(&irq_cpus[i].lock);
if (irq_cpus[i].warn[irq] || !irq_cpus[i].diff[irq]) {
spin_unlock(&irq_cpus[i].lock);
continue;
}
irq_cpus[i].warn[irq] = 1;
t_diff_ms = irq_cpus[i].te - irq_cpus[i].ts;
do_div(t_diff_ms, 1000000);
snprintf(msg, sizeof(msg),
"irq:%d %s count +%d in %lld ms, from %lld.%06lu to %lld.%06lu on all CPU",
irq, irq_to_name(irq),
irq_cpus[i].diff[irq], t_diff_ms,
sec_high(irq_cpus[i].ts),
sec_low(irq_cpus[i].ts),
sec_high(irq_cpus[i].te),
sec_low(irq_cpus[i].te));
sched_mon_msg(TO_BOTH, msg);
spin_unlock(&irq_cpus[i].lock);
}
if (irq_period_th2_ns && irq_count_aee_limit &&
t_avg < irq_period_th2_ns) {
irq_count_aee_limit--;
schedule_monitor_aee(msg, "BURST IRQ");
}
}
}
static void irq_count_tracer_work(struct irq_work *work)
{
if (irq_count_tracer)
__irq_count_tracer_work(work);
}
static enum hrtimer_restart irq_count_polling_timer(struct hrtimer *unused)
{
int cpu;
for_each_online_cpu(cpu)
irq_work_queue_on(per_cpu_ptr(&irq_count_data.work, cpu), cpu);
hrtimer_forward_now(&irq_count_timer, ms_to_ktime(1000));
return HRTIMER_RESTART;
}
static int __init init_irq_count_tracer(void)
{
int cpu, i;
for (i = 0; i < REC_NUM; i++)
spin_lock_init(&irq_cpus[i].lock);
for_each_online_cpu(cpu)
init_irq_work(per_cpu_ptr(&irq_count_data.work, cpu),
irq_count_tracer_work);
#ifdef CONFIG_MTK_ENG_BUILD
irq_count_tracer = 1;
irq_count_aee_limit = 1;
#endif
hrtimer_init(&irq_count_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
irq_count_timer.function = irq_count_polling_timer;
hrtimer_start(&irq_count_timer, ms_to_ktime(0),
HRTIMER_MODE_REL_PINNED);
return 0;
}
static void __show_irq_count_info(int output)
{
int cpu;
sched_mon_msg(output, "===== IRQ Status =====");
for_each_possible_cpu(cpu) {
struct irq_count_stat *irq_cnt;
int irq;
irq_cnt = per_cpu_ptr(&irq_count_data, cpu);
sched_mon_msg(output, "CPU: %d", cpu);
sched_mon_msg(output, "from %lld.%06lu to %lld.%06lu, %lld ms",
sec_high(irq_cnt->t_start),
sec_low(irq_cnt->t_start),
sec_high(irq_cnt->t_end), sec_low(irq_cnt->t_end),
msec_high(irq_cnt->t_end - irq_cnt->t_start));
for (irq = 0; irq < min_t(int, nr_irqs, MAX_IRQ_NUM); irq++) {
unsigned int count;
count = kstat_irqs_cpu(irq, cpu);
if (!count)
continue;
sched_mon_msg(output, " %d:%s +%d(%d)",
irq, irq_to_name(irq),
count - irq_cnt->count[irq], count);
}
sched_mon_msg(output, "");
}
}
void show_irq_count_info(int output)
{
if (irq_count_tracer)
__show_irq_count_info(output);
}
#endif /* CONFIG_MTK_IRQ_COUNT_TRACER */
#ifdef CONFIG_MTK_IRQ_OFF_TRACER
static DEFINE_PER_CPU(unsigned long long, irqsoff_timestamp);
static DEFINE_PER_CPU(struct stack_trace, irqsoff_trace);
#ifdef CONFIG_PROVE_LOCKING
static DEFINE_PER_CPU(int, tracing_irq_cpu);
#define irqsoff_tracing_lock() this_cpu_write(tracing_irq_cpu, 1)
#define irqsoff_tracing_unlock() this_cpu_write(tracing_irq_cpu, 0)
#define irqsoff_on_tracing() this_cpu_read(tracing_irq_cpu)
#define irqsoff_not_on_tracing() !this_cpu_read(tracing_irq_cpu)
#else
#define irqsoff_tracing_lock() do {} while (0)
#define irqsoff_tracing_unlock() do {} while (0)
#define irqsoff_on_tracing() 0
#define irqsoff_not_on_tracing() 0
#endif
static const char * const list_irq_disable[] = {
/* debug purpose: sched_debug_show, sysrq_sched_debug_show */
"print_cpu"
};
static int trace_hardirqs_whitelist(void)
{
struct stack_trace *trace = raw_cpu_ptr(&irqsoff_trace);
char func[64];
int i, j;
for (i = 0; i < trace->nr_entries; i++) {
snprintf(func, sizeof(func),
"%ps", (void *)trace->entries[i]);
for (j = 0; j < ARRAY_SIZE(list_irq_disable); j++)
if (!strcmp(func, list_irq_disable[j]))
return 1;
}
return 0;
}
static inline void __trace_hardirqs_off_time(void)
{
if (irqsoff_on_tracing())
return;
irqsoff_tracing_lock();
this_cpu_write(irqsoff_timestamp, sched_clock());
if (irq_off_tracer_trace) {
struct stack_trace *trace;
trace = raw_cpu_ptr(&irqsoff_trace);
trace->nr_entries = 0;
save_stack_trace(trace);
if (trace->nr_entries != 0 &&
trace->entries[trace->nr_entries - 1] == ULONG_MAX)
trace->nr_entries--;
}
}
inline void trace_hardirqs_off_time(void)
{
if (irq_off_tracer)
__trace_hardirqs_off_time();
}
static inline void __trace_hardirqs_on_time(void)
{
unsigned long long t_off, t_on, t_diff;
char msg[128];
int output = TO_FTRACE;
if (irqsoff_not_on_tracing())
return;
/* skip <idle-0> task */
if (current->pid == 0) {
irqsoff_tracing_unlock();
return;
}
t_off = this_cpu_read(irqsoff_timestamp);
t_on = sched_clock();
t_diff = t_on - t_off;
do_div(t_diff, 1000000);
if (t_diff < irq_off_th1_ms) {
irqsoff_tracing_unlock();
return;
}
if (irq_off_th2_ms && t_diff >= irq_off_th2_ms)
output = TO_BOTH_SAVE;
snprintf(msg, sizeof(msg),
"irq off monitor: dur[%lld ms] off[%lld.%06lu] on[%lld.%06lu]",
t_diff, sec_high(t_off), sec_low(t_off),
sec_high(t_on), sec_low(t_on));
sched_mon_msg(output, msg);
if (irq_off_tracer_trace) {
struct stack_trace *trace = raw_cpu_ptr(&irqsoff_trace);
char msg2[128];
int i;
sched_mon_msg(output, "call trace:");
for (i = 0; i < trace->nr_entries; i++) {
snprintf(msg2, sizeof(msg2), "[<%p>] %pS",
(void *)trace->entries[i],
(void *)trace->entries[i]);
sched_mon_msg(output, msg2);
}
}
if (irq_off_th3_ms && irq_off_aee_limit &&
t_diff > irq_off_th3_ms && !trace_hardirqs_whitelist()) {
static unsigned long long aee_ts;
unsigned long long now = sched_clock();
if (now - aee_ts > irq_off_aee_debounce_ms * 1000000ULL) {
aee_ts = now;
irq_off_aee_limit--;
schedule_monitor_aee(msg, "IRQ OFF");
}
}
irqsoff_tracing_unlock();
}
inline void trace_hardirqs_on_time(void)
{
if (irq_off_tracer)
__trace_hardirqs_on_time();
}
__init static int init_irq_off_tracer(void)
{
int cpu;
for_each_possible_cpu(cpu) {
struct stack_trace *trace;
trace = per_cpu_ptr(&irqsoff_trace, cpu);
trace->entries =
kmalloc_array(MAX_STACK_TRACE_DEPTH,
sizeof(unsigned long), GFP_KERNEL);
trace->max_entries = MAX_STACK_TRACE_DEPTH;
trace->skip = 2;
}
#ifdef CONFIG_MTK_ENG_BUILD
irq_off_tracer = 1;
irq_off_tracer_trace = 1;
#endif
return 0;
}
#endif /* CONFIG_MTK_IRQ_OFF_TRACER */
#ifdef CONFIG_MTK_PREEMPT_TRACER
static DEFINE_PER_CPU(unsigned long long, preempt_off_timestamp);
static DEFINE_PER_CPU(struct stack_trace, preempt_off_trace);
static inline void __trace_preempt_off_time(void)
{
this_cpu_write(preempt_off_timestamp, sched_clock());
if (preempt_tracer_trace) {
struct stack_trace *trace;
trace = raw_cpu_ptr(&preempt_off_trace);
trace->nr_entries = 0;
save_stack_trace(trace);
if (trace->nr_entries != 0 &&
trace->entries[trace->nr_entries - 1] == ULONG_MAX)
trace->nr_entries--;
}
}
inline void trace_preempt_off_time(void)
{
if (preempt_tracer)
__trace_preempt_off_time();
}
static inline void __trace_preempt_on_time(void)
{
unsigned long long t_off, t_on, t_diff;
char msg[128];
int output = TO_FTRACE;
/* skip <idle-0> task */
if (current->pid == 0)
return;
t_off = this_cpu_read(preempt_off_timestamp);
t_on = sched_clock();
t_diff = t_on - t_off;
do_div(t_diff, 1000000);
if (t_diff < preempt_th1_ms)
return;
if (preempt_th2_ms && t_diff >= preempt_th2_ms)
output = TO_BOTH_SAVE;
snprintf(msg, sizeof(msg),
"preempt off monitor: dur[%lld ms] off[%lld.%06lu] on[%lld.%06lu]",
t_diff, sec_high(t_off), sec_low(t_off),
sec_high(t_on), sec_low(t_on));
sched_mon_msg(output, msg);
if (preempt_tracer_trace) {
struct stack_trace *trace = raw_cpu_ptr(&preempt_off_trace);
char msg2[128];
int i;
sched_mon_msg(output, "call trace:");
for (i = 0; i < trace->nr_entries; i++) {
snprintf(msg2, sizeof(msg2), "[<%p>] %pS",
(void *)trace->entries[i],
(void *)trace->entries[i]);
sched_mon_msg(output, msg2);
}
}
if (preempt_th3_ms && preempt_aee_limit &&
t_diff > preempt_th3_ms) {
preempt_aee_limit--;
schedule_monitor_aee(msg, "PREEMPT OFF");
}
}
inline void trace_preempt_on_time(void)
{
if (preempt_tracer)
__trace_preempt_on_time();
}
__init static int init_preempt_tracer(void)
{
int cpu;
for_each_possible_cpu(cpu) {
struct stack_trace *trace;
trace = per_cpu_ptr(&preempt_off_trace, cpu);
trace->entries =
kmalloc_array(MAX_STACK_TRACE_DEPTH,
sizeof(unsigned long), GFP_KERNEL);
trace->max_entries = MAX_STACK_TRACE_DEPTH;
trace->skip = 2;
}
#ifdef CONFIG_MTK_ENG_BUILD
preempt_tracer = 1;
#endif
return 0;
}
#endif /* CONFIG_MTK_PREEMPT_TRACER */
void mt_aee_dump_sched_traces(void)
{
show_irq_handle_info(TO_SRAM);
show_irq_count_info(TO_SRAM);
}
enum sched_mon_dir_type {
ROOT,
IRQ_TIME_TRACER,
IRQ_COUNT_TRACER,
IRQ_OFF_TRACER,
PREEMPT_TRACER,
NUM_SCHED_MON_DIRS,
};
struct sched_mon_proc_file {
const char *name;
enum sched_mon_dir_type dir;
umode_t mode;
const struct file_operations *proc_fops;
};
static struct sched_mon_proc_file sched_mon_file[] = {
/* /proc/mtmon */
{"sched_mon_door", ROOT, 0220,
&sched_mon_door_fops},
{"enable", IRQ_TIME_TRACER, 0644,
&sched_mon_irq_time_tracer_fops},
{"th1_ms", IRQ_TIME_TRACER, 0644,
&sched_mon_irq_time_th1_ms_fops},
{"th2_ms", IRQ_TIME_TRACER, 0644,
&sched_mon_irq_time_th2_ms_fops},
{"aee_limit", IRQ_TIME_TRACER, 0644,
&sched_mon_irq_time_aee_limit_fops},
#ifdef CONFIG_MTK_IRQ_COUNT_TRACER
{"enable", IRQ_COUNT_TRACER, 0644,
&sched_mon_irq_count_tracer_fops},
{"period_th1_ns", IRQ_COUNT_TRACER, 0644,
&sched_mon_irq_period_th1_ns_fops},
{"period_th2_ns", IRQ_COUNT_TRACER, 0644,
&sched_mon_irq_period_th2_ns_fops},
{"aee_limit", IRQ_COUNT_TRACER, 0644,
&sched_mon_irq_count_aee_limit_fops},
#endif
#ifdef CONFIG_MTK_IRQ_OFF_TRACER
{"enable", IRQ_OFF_TRACER, 0644,
&sched_mon_irq_off_tracer_fops},
{"enable_backtrace", IRQ_OFF_TRACER, 0644,
&sched_mon_irq_off_tracer_trace_fops},
{"th1_ms", IRQ_OFF_TRACER, 0644,
&sched_mon_irq_off_th1_ms_fops},
{"th2_ms", IRQ_OFF_TRACER, 0644,
&sched_mon_irq_off_th2_ms_fops},
{"th3_ms", IRQ_OFF_TRACER, 0644,
&sched_mon_irq_off_th3_ms_fops},
{"aee_limit", IRQ_OFF_TRACER, 0644,
&sched_mon_irq_off_aee_limit_fops},
{"aee_debounce_ms", IRQ_OFF_TRACER, 0644,
&sched_mon_irq_off_aee_debounce_ms_fops},
#endif
#ifdef CONFIG_MTK_PREEMPT_TRACER
{"enable", PREEMPT_TRACER, 0644,
&sched_mon_preempt_tracer_fops},
{"enable_backtrace", PREEMPT_TRACER, 0644,
&sched_mon_preempt_tracer_trace_fops},
{"th1_ms", PREEMPT_TRACER, 0644,
&sched_mon_preempt_th1_ms_fops},
{"th2_ms", PREEMPT_TRACER, 0644,
&sched_mon_preempt_th2_ms_fops},
{"th3_ms", PREEMPT_TRACER, 0644,
&sched_mon_preempt_th3_ms_fops},
{"aee_limit", PREEMPT_TRACER, 0644,
&sched_mon_preempt_aee_limit_fops},
#endif
};
void sched_mon_device_tree(void)
{
struct device_node *node;
u32 value = 0;
node = of_find_compatible_node(NULL, NULL, "mediatek,sched_mon");
if (!node)
return;
of_property_read_u32(node, "irq_time_tracer", &value);
irq_time_tracer = (bool)value;
of_property_read_u32(node, "irq_time_th1_ms", &irq_time_th1_ms);
of_property_read_u32(node, "irq_time_th2_ms", &irq_time_th2_ms);
of_property_read_u32(node, "irq_time_aee_limit", &irq_time_aee_limit);
#ifdef CONFIG_MTK_IRQ_COUNT_TRACER
of_property_read_u32(node, "irq_count_tracer", &value);
irq_count_tracer = (bool)value;
of_property_read_u32(node, "irq_period_th1_ns", &irq_period_th1_ns);
of_property_read_u32(node, "irq_period_th2_ns", &irq_period_th2_ns);
of_property_read_u32(node, "irq_count_aee_limit", &irq_count_aee_limit);
#endif
#ifdef CONFIG_MTK_IRQ_OFF_TRACER
of_property_read_u32(node, "irq_off_tracer", &value);
irq_off_tracer = (bool)value;
of_property_read_u32(node, "irq_off_tracer_trace", &value);
irq_off_tracer_trace = (bool)value;
of_property_read_u32(node, "irq_off_th1_ms", &irq_off_th1_ms);
of_property_read_u32(node, "irq_off_th2_ms", &irq_off_th2_ms);
of_property_read_u32(node, "irq_off_th3_ms", &irq_off_th3_ms);
of_property_read_u32(node, "irq_off_aee_limit", &irq_off_aee_limit);
of_property_read_u32(node, "irq_off_aee_debounce_ms",
&irq_off_aee_debounce_ms);
#endif
#ifdef CONFIG_MTK_PREEMPT_TRACER
of_property_read_u32(node, "preempt_tracer", &value);
preempt_tracer = (bool)value;
of_property_read_u32(node, "preempt_tracer_trace", &value);
preempt_tracer_trace = (bool)value;
of_property_read_u32(node, "preempt_th1_ms", &preempt_th1_ms);
of_property_read_u32(node, "preempt_th2_ms", &preempt_th2_ms);
of_property_read_u32(node, "preempt_th3_ms", &preempt_th3_ms);
of_property_read_u32(node, "preempt_aee_limit", &preempt_aee_limit);
#endif
}
static int __init init_sched_monitor(void)
{
int i;
struct proc_dir_entry *dir[NUM_SCHED_MON_DIRS];
dir[ROOT] = proc_mkdir("mtmon", NULL);
if (!dir[ROOT])
return -1;
dir[IRQ_TIME_TRACER] = proc_mkdir("irq_time_tracer", dir[ROOT]);
if (!dir[IRQ_TIME_TRACER])
return -1;
#ifdef CONFIG_MTK_ENG_BUILD
irq_time_tracer = 1;
#endif
#ifdef CONFIG_MTK_IRQ_COUNT_TRACER
dir[IRQ_COUNT_TRACER] = proc_mkdir("irq_count_tracer", dir[ROOT]);
if (!dir[IRQ_COUNT_TRACER])
return -1;
init_irq_count_tracer();
#endif
#ifdef CONFIG_MTK_IRQ_OFF_TRACER
dir[IRQ_OFF_TRACER] = proc_mkdir("irq_off_tracer", dir[ROOT]);
if (!dir[IRQ_OFF_TRACER])
return -1;
init_irq_off_tracer();
#endif
#ifdef CONFIG_MTK_PREEMPT_TRACER
dir[PREEMPT_TRACER] = proc_mkdir("preempt_tracer", dir[ROOT]);
if (!dir[PREEMPT_TRACER])
return -1;
init_preempt_tracer();
#endif
for (i = 0; i < ARRAY_SIZE(sched_mon_file); i++) {
if (!proc_create(sched_mon_file[i].name,
sched_mon_file[i].mode,
dir[sched_mon_file[i].dir],
sched_mon_file[i].proc_fops)) {
pr_info("create [%s] failed\n", sched_mon_file[i].name);
return -ENOMEM;
}
}
sched_mon_device_tree();
mt_sched_monitor_test_init(dir[ROOT]);
return 0;
}
module_init(init_sched_monitor);
@@ -0,0 +1,179 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
/* TIMER_SOFTIRQ duration warning test */
static struct timer_list timer;
static void delayed_timer(struct timer_list *t)
{
mdelay(600);
}
void sched_mon_test_TIMER_SOFTIRQ(void)
{
timer_setup(&timer, delayed_timer, 0);
mod_timer(&timer, jiffies + msecs_to_jiffies(100));
}
/* TASKLET_SOFTIRQ duration warning test */
static struct tasklet_struct sched_mon_tasklet;
static void sched_mon_tasklet_func(unsigned long data)
{
mdelay(600);
}
void sched_mon_test_TASKLET_SOFTIRQ(void)
{
tasklet_init(&sched_mon_tasklet, sched_mon_tasklet_func, 0);
tasklet_schedule(&sched_mon_tasklet);
}
/* RCU_SOFTIRQ duration warning test */
struct sched_mon_rcu_t {
int val;
};
static struct sched_mon_rcu_t __rcu *sched_mon_rcu_g;
static struct rcu_head sched_mon_rcu_head;
static void delayed_rcu_callback(struct rcu_head *r)
{
/* RCU_SOFTIRQ handler */
mdelay(600);
}
void sched_mon_test_RCU_SOFTIRQ(void)
{
struct sched_mon_rcu_t *sched_mon_rcu;
sched_mon_rcu = kmalloc(sizeof(*sched_mon_rcu), GFP_KERNEL);
if (!sched_mon_rcu)
return;
sched_mon_rcu->val = 100;
RCU_INIT_POINTER(sched_mon_rcu_g, sched_mon_rcu);
call_rcu(&sched_mon_rcu_head, delayed_rcu_callback);
}
/* irq_work monitor test */
static struct irq_work sched_mon_irqwork;
static void sched_mon_irq_work(struct irq_work *work)
{
mdelay(600);
}
void sched_mon_test_irq_work(void)
{
init_irq_work(&sched_mon_irqwork, sched_mon_irq_work);
irq_work_queue(&sched_mon_irqwork);
}
/* IRQ disable monitor test */
void sched_mon_test_irq_disable(void)
{
unsigned long flags;
local_irq_save(flags);
mdelay(600);
local_irq_restore(flags);
/* The test will trigger KernelAPI Dump */
}
/* hrtimer monitor test */
static struct hrtimer sched_mon_hrtimer;
static enum hrtimer_restart sched_mon_hrtimer_func(struct hrtimer *unused)
{
mdelay(600);
return HRTIMER_NORESTART;
}
void sched_mon_test_HRTIMER(void)
{
hrtimer_init(&sched_mon_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
sched_mon_hrtimer.function = sched_mon_hrtimer_func;
hrtimer_start(&sched_mon_hrtimer, ms_to_ktime(0),
HRTIMER_MODE_REL_PINNED);
}
/* preempt count monitor test */
static struct hrtimer sched_mon_hrtimer2;
static enum hrtimer_restart sched_mon_hrtimer_func2(struct hrtimer *unused)
{
preempt_disable();
return HRTIMER_NORESTART;
}
void sched_mon_test_PREEMPT_COUNT(void)
{
hrtimer_init(&sched_mon_hrtimer2, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
sched_mon_hrtimer2.function = sched_mon_hrtimer_func2;
hrtimer_start(&sched_mon_hrtimer2, ms_to_ktime(0),
HRTIMER_MODE_REL_PINNED);
}
struct sched_mon_test_func {
char name[32];
void (*func)(void);
};
struct sched_mon_test_func sched_mon_test_list[] = {
{"sirq_timer_dur", sched_mon_test_TIMER_SOFTIRQ},
{"sirq_tasklet_dur", sched_mon_test_TASKLET_SOFTIRQ},
{"sirq_rcu_dur", sched_mon_test_RCU_SOFTIRQ},
{"irq_work_dur", sched_mon_test_irq_work},
{"hirq_disable", sched_mon_test_irq_disable},
{"hrtimer_dur", sched_mon_test_HRTIMER},
{"preempt_count", sched_mon_test_PREEMPT_COUNT},
};
static ssize_t
sched_mon_test_write(struct file *file, const char *ubuf,
size_t count, loff_t *ppos)
{
int i;
char buf[32];
if (count >= sizeof(buf) || count == 0)
return -EINVAL;
if (copy_from_user(&buf, ubuf, count))
return -EFAULT;
buf[count] = '\0';
for (i = 0; i < ARRAY_SIZE(sched_mon_test_list); i++) {
if (!strncmp(buf, sched_mon_test_list[i].name,
strlen(sched_mon_test_list[i].name)))
sched_mon_test_list[i].func();
}
return count;
}
static const struct file_operations proc_sched_monitor_test_fops = {
.open = simple_open,
.write = sched_mon_test_write,
};
void mt_sched_monitor_test_init(struct proc_dir_entry *dir)
{
proc_create("sched_mon_test", 0220, dir,
&proc_sched_monitor_test_fops);
}