2019-05-19 20:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* linux/kernel/profile.c
|
|
|
|
* Simple profiling. Manages a direct-mapped profile hit count buffer,
|
|
|
|
* with configurable resolution, support for restricting the cpus on
|
|
|
|
* which profiling is done, and switching between cpu time and
|
|
|
|
* schedule() calls via kernel command line parameters passed at boot.
|
|
|
|
*
|
|
|
|
* Scheduler profiling support, Arjan van de Ven and Ingo Molnar,
|
|
|
|
* Red Hat, July 2004
|
|
|
|
* Consolidation of architecture support code for profiling,
|
2012-12-06 17:39:54 +08:00
|
|
|
* Nadia Yvette Chambers, Oracle, July 2004
|
2005-04-17 06:20:36 +08:00
|
|
|
* Amortized hit count accounting via per-cpu open-addressed hashtables
|
2012-12-06 17:39:54 +08:00
|
|
|
* to resolve timer interrupt livelocks, Nadia Yvette Chambers,
|
|
|
|
* Oracle, 2004
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
2011-05-24 02:51:41 +08:00
|
|
|
#include <linux/export.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/profile.h>
|
2018-10-31 06:09:49 +08:00
|
|
|
#include <linux/memblock.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/cpumask.h>
|
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/highmem.h>
|
2006-03-23 19:00:24 +08:00
|
|
|
#include <linux/mutex.h>
|
2008-10-16 13:01:46 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/vmalloc.h>
|
2017-02-05 19:07:04 +08:00
|
|
|
#include <linux/sched/stat.h>
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/sections.h>
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
#include <asm/irq_regs.h>
|
Detach sched.h from mm.h
First thing mm.h does is including sched.h solely for can_do_mlock() inline
function which has "current" dereference inside. By dealing with can_do_mlock()
mm.h can be detached from sched.h which is good. See below, why.
This patch
a) removes unconditional inclusion of sched.h from mm.h
b) makes can_do_mlock() normal function in mm/mlock.c
c) exports can_do_mlock() to not break compilation
d) adds sched.h inclusions back to files that were getting it indirectly.
e) adds less bloated headers to some files (asm/signal.h, jiffies.h) that were
getting them indirectly
Net result is:
a) mm.h users would get less code to open, read, preprocess, parse, ... if
they don't need sched.h
b) sched.h stops being dependency for significant number of files:
on x86_64 allmodconfig touching sched.h results in recompile of 4083 files,
after patch it's only 3744 (-8.3%).
Cross-compile tested on
all arm defconfigs, all mips defconfigs, all powerpc defconfigs,
alpha alpha-up
arm
i386 i386-up i386-defconfig i386-allnoconfig
ia64 ia64-up
m68k
mips
parisc parisc-up
powerpc powerpc-up
s390 s390-up
sparc sparc-up
sparc64 sparc64-up
um-x86_64
x86_64 x86_64-up x86_64-defconfig x86_64-allnoconfig
as well as my two usual configs.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-21 05:22:52 +08:00
|
|
|
#include <asm/ptrace.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct profile_hit {
|
|
|
|
u32 pc, hits;
|
|
|
|
};
|
|
|
|
#define PROFILE_GRPSHIFT 3
|
|
|
|
#define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
|
|
|
|
#define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
|
|
|
|
#define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
|
|
|
|
|
|
|
|
static atomic_t *prof_buffer;
|
2021-09-08 10:58:21 +08:00
|
|
|
static unsigned long prof_len;
|
|
|
|
static unsigned short int prof_shift;
|
2007-01-11 15:15:38 +08:00
|
|
|
|
2006-12-07 12:37:24 +08:00
|
|
|
int prof_on __read_mostly;
|
2007-01-11 15:15:38 +08:00
|
|
|
EXPORT_SYMBOL_GPL(prof_on);
|
|
|
|
|
2009-01-01 07:42:27 +08:00
|
|
|
static cpumask_var_t prof_cpu_mask;
|
2016-03-23 05:27:26 +08:00
|
|
|
#if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
|
2005-04-17 06:20:36 +08:00
|
|
|
static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
|
|
|
|
static DEFINE_PER_CPU(int, cpu_profile_flip);
|
2006-03-23 19:00:24 +08:00
|
|
|
static DEFINE_MUTEX(profile_flip_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
2008-10-16 13:01:46 +08:00
|
|
|
int profile_setup(char *str)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2014-06-07 05:37:30 +08:00
|
|
|
static const char schedstr[] = "schedule";
|
|
|
|
static const char sleepstr[] = "sleep";
|
|
|
|
static const char kvmstr[] = "kvm";
|
2005-04-17 06:20:36 +08:00
|
|
|
int par;
|
|
|
|
|
2006-12-07 12:37:24 +08:00
|
|
|
if (!strncmp(str, sleepstr, strlen(sleepstr))) {
|
2007-10-25 00:23:50 +08:00
|
|
|
#ifdef CONFIG_SCHEDSTATS
|
2016-02-05 17:08:36 +08:00
|
|
|
force_schedstat_enabled();
|
2006-12-07 12:37:24 +08:00
|
|
|
prof_on = SLEEP_PROFILING;
|
|
|
|
if (str[strlen(sleepstr)] == ',')
|
|
|
|
str += strlen(sleepstr) + 1;
|
|
|
|
if (get_option(&str, &par))
|
2021-09-08 10:58:21 +08:00
|
|
|
prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
|
|
|
|
pr_info("kernel sleep profiling enabled (shift: %u)\n",
|
2006-12-07 12:37:24 +08:00
|
|
|
prof_shift);
|
2007-10-25 00:23:50 +08:00
|
|
|
#else
|
2014-06-07 05:37:29 +08:00
|
|
|
pr_warn("kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
|
2007-10-25 00:23:50 +08:00
|
|
|
#endif /* CONFIG_SCHEDSTATS */
|
2007-01-06 08:36:29 +08:00
|
|
|
} else if (!strncmp(str, schedstr, strlen(schedstr))) {
|
2005-04-17 06:20:36 +08:00
|
|
|
prof_on = SCHED_PROFILING;
|
2005-05-17 12:53:58 +08:00
|
|
|
if (str[strlen(schedstr)] == ',')
|
|
|
|
str += strlen(schedstr) + 1;
|
|
|
|
if (get_option(&str, &par))
|
2021-09-08 10:58:21 +08:00
|
|
|
prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
|
|
|
|
pr_info("kernel schedule profiling enabled (shift: %u)\n",
|
2005-05-17 12:53:58 +08:00
|
|
|
prof_shift);
|
2007-01-11 15:15:38 +08:00
|
|
|
} else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
|
|
|
|
prof_on = KVM_PROFILING;
|
|
|
|
if (str[strlen(kvmstr)] == ',')
|
|
|
|
str += strlen(kvmstr) + 1;
|
|
|
|
if (get_option(&str, &par))
|
2021-09-08 10:58:21 +08:00
|
|
|
prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
|
|
|
|
pr_info("kernel KVM profiling enabled (shift: %u)\n",
|
2007-01-11 15:15:38 +08:00
|
|
|
prof_shift);
|
2005-05-17 12:53:58 +08:00
|
|
|
} else if (get_option(&str, &par)) {
|
2021-09-08 10:58:21 +08:00
|
|
|
prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
prof_on = CPU_PROFILING;
|
2021-09-08 10:58:21 +08:00
|
|
|
pr_info("kernel profiling enabled (shift: %u)\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
prof_shift);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
__setup("profile=", profile_setup);
|
|
|
|
|
|
|
|
|
2008-10-30 05:01:07 +08:00
|
|
|
int __ref profile_init(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-10-16 13:01:46 +08:00
|
|
|
int buffer_bytes;
|
2008-01-26 04:08:33 +08:00
|
|
|
if (!prof_on)
|
2008-10-16 13:01:46 +08:00
|
|
|
return 0;
|
2008-01-26 04:08:33 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* only text is profiled */
|
|
|
|
prof_len = (_etext - _stext) >> prof_shift;
|
2008-10-16 13:01:46 +08:00
|
|
|
buffer_bytes = prof_len*sizeof(atomic_t);
|
|
|
|
|
2009-01-01 07:42:27 +08:00
|
|
|
if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-02-10 03:20:50 +08:00
|
|
|
cpumask_copy(prof_cpu_mask, cpu_possible_mask);
|
|
|
|
|
2009-07-30 06:04:09 +08:00
|
|
|
prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
|
2008-10-16 13:01:46 +08:00
|
|
|
if (prof_buffer)
|
|
|
|
return 0;
|
|
|
|
|
2009-07-30 06:04:09 +08:00
|
|
|
prof_buffer = alloc_pages_exact(buffer_bytes,
|
|
|
|
GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
|
2008-10-16 13:01:46 +08:00
|
|
|
if (prof_buffer)
|
|
|
|
return 0;
|
|
|
|
|
2010-10-31 03:56:26 +08:00
|
|
|
prof_buffer = vzalloc(buffer_bytes);
|
|
|
|
if (prof_buffer)
|
2008-10-16 13:01:46 +08:00
|
|
|
return 0;
|
|
|
|
|
2009-01-01 07:42:27 +08:00
|
|
|
free_cpumask_var(prof_cpu_mask);
|
2008-10-16 13:01:46 +08:00
|
|
|
return -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:27:26 +08:00
|
|
|
#if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Each cpu has a pair of open-addressed hashtables for pending
|
|
|
|
* profile hits. read_profile() IPI's all cpus to request them
|
|
|
|
* to flip buffers and flushes their contents to prof_buffer itself.
|
|
|
|
* Flip requests are serialized by the profile_flip_mutex. The sole
|
|
|
|
* use of having a second hashtable is for avoiding cacheline
|
|
|
|
* contention that would otherwise happen during flushes of pending
|
|
|
|
* profile hits required for the accuracy of reported profile hits
|
|
|
|
* and so resurrect the interrupt livelock issue.
|
|
|
|
*
|
|
|
|
* The open-addressed hashtables are indexed by profile buffer slot
|
|
|
|
* and hold the number of pending hits to that profile buffer slot on
|
|
|
|
* a cpu in an entry. When the hashtable overflows, all pending hits
|
|
|
|
* are accounted to their corresponding profile buffer slots with
|
|
|
|
* atomic_add() and the hashtable emptied. As numerous pending hits
|
|
|
|
* may be accounted to a profile buffer slot in a hashtable entry,
|
|
|
|
* this amortizes a number of atomic profile buffer increments likely
|
|
|
|
* to be far larger than the number of entries in the hashtable,
|
|
|
|
* particularly given that the number of distinct profile buffer
|
|
|
|
* positions to which hits are accounted during short intervals (e.g.
|
|
|
|
* several seconds) is usually very small. Exclusion from buffer
|
|
|
|
* flipping is provided by interrupt disablement (note that for
|
2006-12-07 12:37:24 +08:00
|
|
|
* SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
|
|
|
|
* process context).
|
2005-04-17 06:20:36 +08:00
|
|
|
* The hash function is meant to be lightweight as opposed to strong,
|
|
|
|
* and was vaguely inspired by ppc64 firmware-supported inverted
|
|
|
|
* pagetable hash functions, but uses a full hashtable full of finite
|
|
|
|
* collision chains, not just pairs of them.
|
|
|
|
*
|
2012-12-06 17:39:54 +08:00
|
|
|
* -- nyc
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
static void __profile_flip_buffers(void *unused)
|
|
|
|
{
|
|
|
|
int cpu = smp_processor_id();
|
|
|
|
|
|
|
|
per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void profile_flip_buffers(void)
|
|
|
|
{
|
|
|
|
int i, j, cpu;
|
|
|
|
|
2006-03-23 19:00:24 +08:00
|
|
|
mutex_lock(&profile_flip_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
j = per_cpu(cpu_profile_flip, get_cpu());
|
|
|
|
put_cpu();
|
2008-05-09 15:39:44 +08:00
|
|
|
on_each_cpu(__profile_flip_buffers, NULL, 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
for_each_online_cpu(cpu) {
|
|
|
|
struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
|
|
|
|
for (i = 0; i < NR_PROFILE_HIT; ++i) {
|
|
|
|
if (!hits[i].hits) {
|
|
|
|
if (hits[i].pc)
|
|
|
|
hits[i].pc = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
|
|
|
|
hits[i].hits = hits[i].pc = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-03-23 19:00:24 +08:00
|
|
|
mutex_unlock(&profile_flip_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void profile_discard_flip_buffers(void)
|
|
|
|
{
|
|
|
|
int i, cpu;
|
|
|
|
|
2006-03-23 19:00:24 +08:00
|
|
|
mutex_lock(&profile_flip_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
i = per_cpu(cpu_profile_flip, get_cpu());
|
|
|
|
put_cpu();
|
2008-05-09 15:39:44 +08:00
|
|
|
on_each_cpu(__profile_flip_buffers, NULL, 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
for_each_online_cpu(cpu) {
|
|
|
|
struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
|
|
|
|
memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
|
|
|
|
}
|
2006-03-23 19:00:24 +08:00
|
|
|
mutex_unlock(&profile_flip_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-05-27 07:26:00 +08:00
|
|
|
static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
|
|
|
|
int i, j, cpu;
|
|
|
|
struct profile_hit *hits;
|
|
|
|
|
|
|
|
pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
|
|
|
|
i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
|
|
|
|
secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
|
|
|
|
cpu = get_cpu();
|
|
|
|
hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
|
|
|
|
if (!hits) {
|
|
|
|
put_cpu();
|
|
|
|
return;
|
|
|
|
}
|
2006-12-07 12:37:24 +08:00
|
|
|
/*
|
|
|
|
* We buffer the global profiler buffer into a per-CPU
|
|
|
|
* queue and thus reduce the number of global (and possibly
|
|
|
|
* NUMA-alien) accesses. The write-queue is self-coalescing:
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
local_irq_save(flags);
|
|
|
|
do {
|
|
|
|
for (j = 0; j < PROFILE_GRPSZ; ++j) {
|
|
|
|
if (hits[i + j].pc == pc) {
|
2006-12-07 12:37:24 +08:00
|
|
|
hits[i + j].hits += nr_hits;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
} else if (!hits[i + j].hits) {
|
|
|
|
hits[i + j].pc = pc;
|
2006-12-07 12:37:24 +08:00
|
|
|
hits[i + j].hits = nr_hits;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = (i + secondary) & (NR_PROFILE_HIT - 1);
|
|
|
|
} while (i != primary);
|
2006-12-07 12:37:24 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the current hit(s) and flush the write-queue out
|
|
|
|
* to the global buffer:
|
|
|
|
*/
|
|
|
|
atomic_add(nr_hits, &prof_buffer[pc]);
|
2005-04-17 06:20:36 +08:00
|
|
|
for (i = 0; i < NR_PROFILE_HIT; ++i) {
|
|
|
|
atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
|
|
|
|
hits[i].pc = hits[i].hits = 0;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
local_irq_restore(flags);
|
|
|
|
put_cpu();
|
|
|
|
}
|
|
|
|
|
2016-07-14 01:16:59 +08:00
|
|
|
static int profile_dead_cpu(unsigned int cpu)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct page *page;
|
2016-07-14 01:16:59 +08:00
|
|
|
int i;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-12-05 08:50:50 +08:00
|
|
|
if (cpumask_available(prof_cpu_mask))
|
2016-07-14 01:16:59 +08:00
|
|
|
cpumask_clear_cpu(cpu, prof_cpu_mask);
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (per_cpu(cpu_profile_hits, cpu)[i]) {
|
|
|
|
page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[i]);
|
|
|
|
per_cpu(cpu_profile_hits, cpu)[i] = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
__free_page(page);
|
|
|
|
}
|
2016-07-14 01:16:59 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int profile_prepare_cpu(unsigned int cpu)
|
|
|
|
{
|
|
|
|
int i, node = cpu_to_mem(cpu);
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
per_cpu(cpu_profile_flip, cpu) = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (per_cpu(cpu_profile_hits, cpu)[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
page = __alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
|
|
|
|
if (!page) {
|
|
|
|
profile_dead_cpu(cpu);
|
|
|
|
return -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2016-07-14 01:16:59 +08:00
|
|
|
per_cpu(cpu_profile_hits, cpu)[i] = page_address(page);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2016-07-14 01:16:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int profile_online_cpu(unsigned int cpu)
|
|
|
|
{
|
2019-12-05 08:50:50 +08:00
|
|
|
if (cpumask_available(prof_cpu_mask))
|
2016-07-14 01:16:59 +08:00
|
|
|
cpumask_set_cpu(cpu, prof_cpu_mask);
|
|
|
|
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2016-07-14 01:16:59 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#else /* !CONFIG_SMP */
|
|
|
|
#define profile_flip_buffers() do { } while (0)
|
|
|
|
#define profile_discard_flip_buffers() do { } while (0)
|
|
|
|
|
2011-05-27 07:26:00 +08:00
|
|
|
static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long pc;
|
|
|
|
pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
|
2006-12-07 12:37:24 +08:00
|
|
|
atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
#endif /* !CONFIG_SMP */
|
2011-05-27 07:26:00 +08:00
|
|
|
|
|
|
|
void profile_hits(int type, void *__pc, unsigned int nr_hits)
|
|
|
|
{
|
|
|
|
if (prof_on != type || !prof_buffer)
|
|
|
|
return;
|
|
|
|
do_profile_hits(type, __pc, nr_hits);
|
|
|
|
}
|
2007-01-23 12:40:33 +08:00
|
|
|
EXPORT_SYMBOL_GPL(profile_hits);
|
|
|
|
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
void profile_tick(int type)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
struct pt_regs *regs = get_irq_regs();
|
|
|
|
|
2019-12-05 08:50:50 +08:00
|
|
|
if (!user_mode(regs) && cpumask_available(prof_cpu_mask) &&
|
2009-01-01 07:42:27 +08:00
|
|
|
cpumask_test_cpu(smp_processor_id(), prof_cpu_mask))
|
2005-04-17 06:20:36 +08:00
|
|
|
profile_hit(type, (void *)profile_pc(regs));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
#include <linux/proc_fs.h>
|
2009-09-19 03:57:09 +08:00
|
|
|
#include <linux/seq_file.h>
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-09-19 03:57:09 +08:00
|
|
|
static int prof_cpu_mask_proc_show(struct seq_file *m, void *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-02-14 06:38:13 +08:00
|
|
|
seq_printf(m, "%*pb\n", cpumask_pr_args(prof_cpu_mask));
|
2009-09-19 03:57:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return single_open(file, prof_cpu_mask_proc_show, NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-09-19 03:57:09 +08:00
|
|
|
static ssize_t prof_cpu_mask_proc_write(struct file *file,
|
|
|
|
const char __user *buffer, size_t count, loff_t *pos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2009-01-01 07:42:27 +08:00
|
|
|
cpumask_var_t new_value;
|
2009-09-19 03:57:09 +08:00
|
|
|
int err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2021-04-01 13:58:23 +08:00
|
|
|
if (!zalloc_cpumask_var(&new_value, GFP_KERNEL))
|
2009-01-01 07:42:27 +08:00
|
|
|
return -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-01-01 07:42:27 +08:00
|
|
|
err = cpumask_parse_user(buffer, count, new_value);
|
|
|
|
if (!err) {
|
2009-09-19 03:57:09 +08:00
|
|
|
cpumask_copy(prof_cpu_mask, new_value);
|
|
|
|
err = count;
|
2009-01-01 07:42:27 +08:00
|
|
|
}
|
|
|
|
free_cpumask_var(new_value);
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2020-02-04 09:37:17 +08:00
|
|
|
static const struct proc_ops prof_cpu_mask_proc_ops = {
|
|
|
|
.proc_open = prof_cpu_mask_proc_open,
|
|
|
|
.proc_read = seq_read,
|
|
|
|
.proc_lseek = seq_lseek,
|
|
|
|
.proc_release = single_release,
|
|
|
|
.proc_write = prof_cpu_mask_proc_write,
|
2009-09-19 03:57:09 +08:00
|
|
|
};
|
|
|
|
|
2013-04-02 08:48:34 +08:00
|
|
|
void create_prof_cpu_mask(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
/* create /proc/irq/prof_cpu_mask */
|
2020-02-04 09:37:17 +08:00
|
|
|
proc_create("irq/prof_cpu_mask", 0600, NULL, &prof_cpu_mask_proc_ops);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function accesses profiling information. The returned data is
|
|
|
|
* binary: the sampling step and the actual contents of the profile
|
|
|
|
* buffer. Use of the program readprofile is recommended in order to
|
|
|
|
* get meaningful info out of these data.
|
|
|
|
*/
|
|
|
|
static ssize_t
|
|
|
|
read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
unsigned long p = *ppos;
|
|
|
|
ssize_t read;
|
2008-01-26 04:08:33 +08:00
|
|
|
char *pnt;
|
2021-09-08 10:58:21 +08:00
|
|
|
unsigned long sample_step = 1UL << prof_shift;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
profile_flip_buffers();
|
|
|
|
if (p >= (prof_len+1)*sizeof(unsigned int))
|
|
|
|
return 0;
|
|
|
|
if (count > (prof_len+1)*sizeof(unsigned int) - p)
|
|
|
|
count = (prof_len+1)*sizeof(unsigned int) - p;
|
|
|
|
read = 0;
|
|
|
|
|
|
|
|
while (p < sizeof(unsigned int) && count > 0) {
|
2008-01-26 04:08:33 +08:00
|
|
|
if (put_user(*((char *)(&sample_step)+p), buf))
|
2006-12-07 12:36:37 +08:00
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
buf++; p++; count--; read++;
|
|
|
|
}
|
|
|
|
pnt = (char *)prof_buffer + p - sizeof(atomic_t);
|
2008-01-26 04:08:33 +08:00
|
|
|
if (copy_to_user(buf, (void *)pnt, count))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EFAULT;
|
|
|
|
read += count;
|
|
|
|
*ppos += read;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Writing to /proc/profile resets the counters
|
|
|
|
*
|
|
|
|
* Writing a 'profiling multiplier' value into it also re-sets the profiling
|
|
|
|
* interrupt frequency, on architectures that support this.
|
|
|
|
*/
|
|
|
|
static ssize_t write_profile(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SMP
|
2008-01-26 04:08:33 +08:00
|
|
|
extern int setup_profiling_timer(unsigned int multiplier);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (count == sizeof(int)) {
|
|
|
|
unsigned int multiplier;
|
|
|
|
|
|
|
|
if (copy_from_user(&multiplier, buf, sizeof(int)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (setup_profiling_timer(multiplier))
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
profile_discard_flip_buffers();
|
|
|
|
memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-02-04 09:37:17 +08:00
|
|
|
static const struct proc_ops profile_proc_ops = {
|
|
|
|
.proc_read = read_profile,
|
|
|
|
.proc_write = write_profile,
|
|
|
|
.proc_lseek = default_llseek,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2016-07-14 01:16:59 +08:00
|
|
|
int __ref create_proc_profile(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2016-07-14 01:16:59 +08:00
|
|
|
struct proc_dir_entry *entry;
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
enum cpuhp_state online_state;
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
2014-03-11 04:42:08 +08:00
|
|
|
int err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (!prof_on)
|
|
|
|
return 0;
|
2016-07-14 01:16:59 +08:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
err = cpuhp_setup_state(CPUHP_PROFILE_PREPARE, "PROFILE_PREPARE",
|
|
|
|
profile_prepare_cpu, profile_dead_cpu);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "AP_PROFILE_ONLINE",
|
|
|
|
profile_online_cpu, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto err_state_prep;
|
|
|
|
online_state = err;
|
|
|
|
err = 0;
|
|
|
|
#endif
|
2008-04-29 16:02:31 +08:00
|
|
|
entry = proc_create("profile", S_IWUSR | S_IRUGO,
|
2020-02-04 09:37:17 +08:00
|
|
|
NULL, &profile_proc_ops);
|
2008-01-26 04:08:33 +08:00
|
|
|
if (!entry)
|
2016-07-14 01:16:59 +08:00
|
|
|
goto err_state_onl;
|
2013-04-12 07:38:51 +08:00
|
|
|
proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t));
|
2014-03-11 04:42:08 +08:00
|
|
|
|
2016-07-14 01:16:59 +08:00
|
|
|
return err;
|
|
|
|
err_state_onl:
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
cpuhp_remove_state(online_state);
|
|
|
|
err_state_prep:
|
|
|
|
cpuhp_remove_state(CPUHP_PROFILE_PREPARE);
|
|
|
|
#endif
|
2014-03-11 04:42:08 +08:00
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2014-04-04 05:48:35 +08:00
|
|
|
subsys_initcall(create_proc_profile);
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif /* CONFIG_PROC_FS */
|