2008-08-28 03:31:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
|
|
|
|
*
|
|
|
|
*/
|
2017-02-08 17:51:37 +00:00
|
|
|
#include <linux/sched/task_stack.h>
|
2008-08-28 03:31:01 +00:00
|
|
|
#include <linux/stacktrace.h>
|
|
|
|
#include <linux/kallsyms.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/ftrace.h>
|
|
|
|
#include <linux/module.h>
|
2008-12-17 04:06:40 +00:00
|
|
|
#include <linux/sysctl.h>
|
2008-08-28 03:31:01 +00:00
|
|
|
#include <linux/init.h>
|
2011-12-20 03:01:00 +00:00
|
|
|
|
|
|
|
#include <asm/setup.h>
|
|
|
|
|
2008-08-28 03:31:01 +00:00
|
|
|
#include "trace.h"
|
|
|
|
|
2008-08-29 20:51:43 +00:00
|
|
|
static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
|
|
|
|
{ [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
|
2015-10-30 05:25:39 +00:00
|
|
|
unsigned stack_trace_index[STACK_TRACE_ENTRIES];
|
2008-08-29 20:51:43 +00:00
|
|
|
|
2013-03-14 03:34:22 +00:00
|
|
|
/*
|
|
|
|
* Reserve one entry for the passed in ip. This will allow
|
|
|
|
* us to remove most or all of the stack size overhead
|
|
|
|
* added by the stack tracer itself.
|
|
|
|
*/
|
2015-10-30 05:25:39 +00:00
|
|
|
struct stack_trace stack_trace_max = {
|
2013-03-14 03:34:22 +00:00
|
|
|
.max_entries = STACK_TRACE_ENTRIES - 1,
|
2015-07-16 17:24:54 +00:00
|
|
|
.entries = &stack_dump_trace[0],
|
2008-08-28 03:31:01 +00:00
|
|
|
};
|
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
unsigned long stack_trace_max_size;
|
2015-11-03 19:50:15 +00:00
|
|
|
arch_spinlock_t stack_trace_max_lock =
|
2009-12-03 11:38:57 +00:00
|
|
|
(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2017-04-06 19:47:32 +00:00
|
|
|
DEFINE_PER_CPU(int, disable_stack_tracer);
|
2008-12-17 04:06:40 +00:00
|
|
|
static DEFINE_MUTEX(stack_sysctl_mutex);
|
|
|
|
|
|
|
|
int stack_tracer_enabled;
|
|
|
|
static int last_stack_tracer_enabled;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
void stack_trace_print(void)
|
2014-06-02 04:33:12 +00:00
|
|
|
{
|
|
|
|
long i;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
pr_emerg(" Depth Size Location (%d entries)\n"
|
|
|
|
" ----- ---- --------\n",
|
2015-10-30 05:25:39 +00:00
|
|
|
stack_trace_max.nr_entries);
|
2014-06-02 04:33:12 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
for (i = 0; i < stack_trace_max.nr_entries; i++) {
|
2014-06-02 04:33:12 +00:00
|
|
|
if (stack_dump_trace[i] == ULONG_MAX)
|
|
|
|
break;
|
2015-10-30 05:25:39 +00:00
|
|
|
if (i+1 == stack_trace_max.nr_entries ||
|
2014-06-02 04:33:12 +00:00
|
|
|
stack_dump_trace[i+1] == ULONG_MAX)
|
2015-10-30 05:25:39 +00:00
|
|
|
size = stack_trace_index[i];
|
2014-06-02 04:33:12 +00:00
|
|
|
else
|
2015-10-30 05:25:39 +00:00
|
|
|
size = stack_trace_index[i] - stack_trace_index[i+1];
|
2014-06-02 04:33:12 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
pr_emerg("%3ld) %8d %5d %pS\n", i, stack_trace_index[i],
|
2014-06-02 04:33:12 +00:00
|
|
|
size, (void *)stack_dump_trace[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
/*
|
2017-03-10 00:16:33 +00:00
|
|
|
* When arch-specific code overrides this function, the following
|
2015-11-03 19:50:15 +00:00
|
|
|
* data should be filled up, assuming stack_trace_max_lock is held to
|
2015-10-30 05:25:39 +00:00
|
|
|
* prevent concurrent updates.
|
|
|
|
* stack_trace_index[]
|
|
|
|
* stack_trace_max
|
|
|
|
* stack_trace_max_size
|
|
|
|
*/
|
|
|
|
void __weak
|
2013-03-14 01:25:35 +00:00
|
|
|
check_stack(unsigned long ip, unsigned long *stack)
|
2008-08-28 03:31:01 +00:00
|
|
|
{
|
2014-06-02 04:33:12 +00:00
|
|
|
unsigned long this_size, flags; unsigned long *p, *top, *start;
|
2013-03-14 03:34:22 +00:00
|
|
|
static int tracer_frame;
|
|
|
|
int frame_size = ACCESS_ONCE(tracer_frame);
|
2015-07-16 17:24:54 +00:00
|
|
|
int i, x;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2013-03-14 00:43:57 +00:00
|
|
|
this_size = ((unsigned long)stack) & (THREAD_SIZE-1);
|
2008-08-28 03:31:01 +00:00
|
|
|
this_size = THREAD_SIZE - this_size;
|
2013-03-14 03:34:22 +00:00
|
|
|
/* Remove the frame of the tracer */
|
|
|
|
this_size -= frame_size;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
if (this_size <= stack_trace_max_size)
|
2008-08-28 03:31:01 +00:00
|
|
|
return;
|
|
|
|
|
2008-10-07 01:24:18 +00:00
|
|
|
/* we do not handle interrupt stacks yet */
|
2013-03-14 00:43:57 +00:00
|
|
|
if (!object_is_on_stack(stack))
|
2008-10-07 01:24:18 +00:00
|
|
|
return;
|
|
|
|
|
2015-10-21 01:48:02 +00:00
|
|
|
/* Can't do this from NMI context (can cause deadlocks) */
|
|
|
|
if (in_nmi())
|
|
|
|
return;
|
|
|
|
|
rcu/tracing: Add rcu_disabled to denote when rcu_irq_enter() will not work
Tracing uses rcu_irq_enter() as a way to make sure that RCU is watching when
it needs to use rcu_read_lock() and friends. This is because tracing can
happen as RCU is about to enter user space, or about to go idle, and RCU
does not watch for RCU read side critical sections as it makes the
transition.
There is a small location within the RCU infrastructure that rcu_irq_enter()
itself will not work. If tracing were to occur in that section it will break
if it tries to use rcu_irq_enter().
Originally, this happens with the stack_tracer, because it will call
save_stack_trace when it encounters stack usage that is greater than any
stack usage it had encountered previously. There was a case where that
happened in the RCU section where rcu_irq_enter() did not work, and lockdep
complained loudly about it. To fix it, stack tracing added a call to be
disabled and RCU would disable stack tracing during the critical section
that rcu_irq_enter() was inoperable. This solution worked, but there are
other cases that use rcu_irq_enter() and it would be a good idea to let RCU
give a way to let others know that rcu_irq_enter() will not work. For
example, in trace events.
Another helpful aspect of this change is that it also moves the per cpu
variable called in the RCU critical section into a cache locale along with
other RCU per cpu variables used in that same location.
I'm keeping the stack_trace_disable() code, as that still could be used in
the future by places that really need to disable it. And since it's only a
static inline, it wont take up any kernel text if it is not used.
Link: http://lkml.kernel.org/r/20170405093207.404f8deb@gandalf.local.home
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
2017-04-07 16:20:36 +00:00
|
|
|
/*
|
|
|
|
* There's a slight chance that we are tracing inside the
|
|
|
|
* RCU infrastructure, and rcu_irq_enter() will not work
|
|
|
|
* as expected.
|
|
|
|
*/
|
|
|
|
if (unlikely(rcu_irq_enter_disabled()))
|
|
|
|
return;
|
|
|
|
|
2008-12-02 20:34:05 +00:00
|
|
|
local_irq_save(flags);
|
2015-11-03 19:50:15 +00:00
|
|
|
arch_spin_lock(&stack_trace_max_lock);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2015-10-20 15:38:08 +00:00
|
|
|
/*
|
|
|
|
* RCU may not be watching, make it see us.
|
|
|
|
* The stack trace code uses rcu_sched.
|
|
|
|
*/
|
|
|
|
rcu_irq_enter();
|
|
|
|
|
2013-03-14 03:34:22 +00:00
|
|
|
/* In case another CPU set the tracer_frame on us */
|
|
|
|
if (unlikely(!frame_size))
|
|
|
|
this_size -= tracer_frame;
|
|
|
|
|
2008-08-28 03:31:01 +00:00
|
|
|
/* a race could have already updated it */
|
2015-10-30 05:25:39 +00:00
|
|
|
if (this_size <= stack_trace_max_size)
|
2008-08-28 03:31:01 +00:00
|
|
|
goto out;
|
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
stack_trace_max_size = this_size;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
stack_trace_max.nr_entries = 0;
|
|
|
|
stack_trace_max.skip = 3;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
save_stack_trace(&stack_trace_max);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2015-07-16 17:24:54 +00:00
|
|
|
/* Skip over the overhead of the stack tracer itself */
|
2015-10-30 05:25:39 +00:00
|
|
|
for (i = 0; i < stack_trace_max.nr_entries; i++) {
|
2015-07-16 17:24:54 +00:00
|
|
|
if (stack_dump_trace[i] == ip)
|
|
|
|
break;
|
|
|
|
}
|
2013-03-14 01:25:35 +00:00
|
|
|
|
2016-01-29 15:22:41 +00:00
|
|
|
/*
|
|
|
|
* Some archs may not have the passed in ip in the dump.
|
|
|
|
* If that happens, we need to show everything.
|
|
|
|
*/
|
|
|
|
if (i == stack_trace_max.nr_entries)
|
|
|
|
i = 0;
|
|
|
|
|
2008-08-29 20:51:43 +00:00
|
|
|
/*
|
|
|
|
* Now find where in the stack these are.
|
|
|
|
*/
|
2015-07-16 17:24:54 +00:00
|
|
|
x = 0;
|
2013-03-14 00:43:57 +00:00
|
|
|
start = stack;
|
2008-08-29 20:51:43 +00:00
|
|
|
top = (unsigned long *)
|
|
|
|
(((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through all the entries. One of the entries may
|
|
|
|
* for some reason be missed on the stack, so we may
|
|
|
|
* have to account for them. If they are all there, this
|
|
|
|
* loop will only happen once. This code only takes place
|
|
|
|
* on a new max, so it is far from a fast path.
|
|
|
|
*/
|
2015-10-30 05:25:39 +00:00
|
|
|
while (i < stack_trace_max.nr_entries) {
|
2008-12-03 16:04:50 +00:00
|
|
|
int found = 0;
|
2008-08-29 20:51:43 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
stack_trace_index[x] = this_size;
|
2008-08-29 20:51:43 +00:00
|
|
|
p = start;
|
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
for (; p < top && i < stack_trace_max.nr_entries; p++) {
|
2015-07-16 17:24:54 +00:00
|
|
|
if (stack_dump_trace[i] == ULONG_MAX)
|
|
|
|
break;
|
2016-02-12 20:46:00 +00:00
|
|
|
/*
|
|
|
|
* The READ_ONCE_NOCHECK is used to let KASAN know that
|
|
|
|
* this is not a stack-out-of-bounds error.
|
|
|
|
*/
|
|
|
|
if ((READ_ONCE_NOCHECK(*p)) == stack_dump_trace[i]) {
|
2015-07-16 17:24:54 +00:00
|
|
|
stack_dump_trace[x] = stack_dump_trace[i++];
|
2015-10-30 05:25:39 +00:00
|
|
|
this_size = stack_trace_index[x++] =
|
2008-08-29 20:51:43 +00:00
|
|
|
(top - p) * sizeof(unsigned long);
|
2008-12-03 16:04:50 +00:00
|
|
|
found = 1;
|
2008-08-29 20:51:43 +00:00
|
|
|
/* Start the search from here */
|
|
|
|
start = p + 1;
|
2013-03-14 03:34:22 +00:00
|
|
|
/*
|
|
|
|
* We do not want to show the overhead
|
|
|
|
* of the stack tracer stack in the
|
|
|
|
* max stack. If we haven't figured
|
|
|
|
* out what that is, then figure it out
|
|
|
|
* now.
|
|
|
|
*/
|
2015-07-16 17:24:54 +00:00
|
|
|
if (unlikely(!tracer_frame)) {
|
2013-03-14 03:34:22 +00:00
|
|
|
tracer_frame = (p - stack) *
|
|
|
|
sizeof(unsigned long);
|
2015-10-30 05:25:39 +00:00
|
|
|
stack_trace_max_size -= tracer_frame;
|
2013-03-14 03:34:22 +00:00
|
|
|
}
|
2008-08-29 20:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 16:04:50 +00:00
|
|
|
if (!found)
|
|
|
|
i++;
|
2008-08-29 20:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
stack_trace_max.nr_entries = x;
|
2015-07-16 17:24:54 +00:00
|
|
|
for (; x < i; x++)
|
|
|
|
stack_dump_trace[x] = ULONG_MAX;
|
|
|
|
|
2014-09-12 13:16:18 +00:00
|
|
|
if (task_stack_end_corrupted(current)) {
|
2015-10-30 05:25:39 +00:00
|
|
|
stack_trace_print();
|
2014-06-02 04:33:12 +00:00
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2008-08-28 03:31:01 +00:00
|
|
|
out:
|
2015-10-20 15:38:08 +00:00
|
|
|
rcu_irq_exit();
|
2015-11-03 19:50:15 +00:00
|
|
|
arch_spin_unlock(&stack_trace_max_lock);
|
2008-12-02 20:34:05 +00:00
|
|
|
local_irq_restore(flags);
|
2008-08-28 03:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-09 16:50:46 +00:00
|
|
|
stack_trace_call(unsigned long ip, unsigned long parent_ip,
|
|
|
|
struct ftrace_ops *op, struct pt_regs *pt_regs)
|
2008-08-28 03:31:01 +00:00
|
|
|
{
|
2013-03-14 00:43:57 +00:00
|
|
|
unsigned long stack;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2010-06-03 13:36:50 +00:00
|
|
|
preempt_disable_notrace();
|
2008-08-28 03:31:01 +00:00
|
|
|
|
|
|
|
/* no atomic needed, we only modify this variable by this cpu */
|
2017-04-06 19:47:32 +00:00
|
|
|
__this_cpu_inc(disable_stack_tracer);
|
|
|
|
if (__this_cpu_read(disable_stack_tracer) != 1)
|
2008-08-28 03:31:01 +00:00
|
|
|
goto out;
|
|
|
|
|
2015-07-16 17:24:54 +00:00
|
|
|
ip += MCOUNT_INSN_SIZE;
|
2013-03-14 03:34:22 +00:00
|
|
|
|
|
|
|
check_stack(ip, &stack);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
|
|
|
out:
|
2017-04-06 19:47:32 +00:00
|
|
|
__this_cpu_dec(disable_stack_tracer);
|
2008-08-28 03:31:01 +00:00
|
|
|
/* prevent recursion in schedule */
|
2010-06-03 13:36:50 +00:00
|
|
|
preempt_enable_notrace();
|
2008-08-28 03:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ftrace_ops trace_ops __read_mostly =
|
|
|
|
{
|
|
|
|
.func = stack_trace_call,
|
2012-07-20 15:04:44 +00:00
|
|
|
.flags = FTRACE_OPS_FL_RECURSION_SAFE,
|
2008-08-28 03:31:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
stack_max_size_read(struct file *filp, char __user *ubuf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
unsigned long *ptr = filp->private_data;
|
|
|
|
char buf[64];
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
|
|
|
|
if (r > sizeof(buf))
|
|
|
|
r = sizeof(buf);
|
|
|
|
return simple_read_from_buffer(ubuf, count, ppos, buf, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
stack_max_size_write(struct file *filp, const char __user *ubuf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
long *ptr = filp->private_data;
|
|
|
|
unsigned long val, flags;
|
|
|
|
int ret;
|
|
|
|
|
2011-06-07 19:58:27 +00:00
|
|
|
ret = kstrtoul_from_user(ubuf, count, 10, &val);
|
|
|
|
if (ret)
|
2008-08-28 03:31:01 +00:00
|
|
|
return ret;
|
|
|
|
|
2008-12-02 20:34:05 +00:00
|
|
|
local_irq_save(flags);
|
2010-02-02 07:32:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In case we trace inside arch_spin_lock() or after (NMI),
|
|
|
|
* we will cause circular lock, so we also need to increase
|
2017-04-06 19:47:32 +00:00
|
|
|
* the percpu disable_stack_tracer here.
|
2010-02-02 07:32:09 +00:00
|
|
|
*/
|
2017-04-06 19:47:32 +00:00
|
|
|
__this_cpu_inc(disable_stack_tracer);
|
2010-02-02 07:32:09 +00:00
|
|
|
|
2015-11-03 19:50:15 +00:00
|
|
|
arch_spin_lock(&stack_trace_max_lock);
|
2008-08-28 03:31:01 +00:00
|
|
|
*ptr = val;
|
2015-11-03 19:50:15 +00:00
|
|
|
arch_spin_unlock(&stack_trace_max_lock);
|
2010-02-02 07:32:09 +00:00
|
|
|
|
2017-04-06 19:47:32 +00:00
|
|
|
__this_cpu_dec(disable_stack_tracer);
|
2008-12-02 20:34:05 +00:00
|
|
|
local_irq_restore(flags);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-12-17 04:06:40 +00:00
|
|
|
static const struct file_operations stack_max_size_fops = {
|
2008-08-28 03:31:01 +00:00
|
|
|
.open = tracing_open_generic,
|
|
|
|
.read = stack_max_size_read,
|
|
|
|
.write = stack_max_size_write,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 16:52:59 +00:00
|
|
|
.llseek = default_llseek,
|
2008-08-28 03:31:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
2009-08-17 08:53:37 +00:00
|
|
|
__next(struct seq_file *m, loff_t *pos)
|
2008-08-28 03:31:01 +00:00
|
|
|
{
|
2009-08-17 08:53:37 +00:00
|
|
|
long n = *pos - 1;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
if (n > stack_trace_max.nr_entries || stack_dump_trace[n] == ULONG_MAX)
|
2008-08-28 03:31:01 +00:00
|
|
|
return NULL;
|
|
|
|
|
2009-08-17 08:53:37 +00:00
|
|
|
m->private = (void *)n;
|
2008-08-29 20:51:43 +00:00
|
|
|
return &m->private;
|
2008-08-28 03:31:01 +00:00
|
|
|
}
|
|
|
|
|
2009-08-17 08:53:37 +00:00
|
|
|
static void *
|
|
|
|
t_next(struct seq_file *m, void *v, loff_t *pos)
|
2008-08-28 03:31:01 +00:00
|
|
|
{
|
2009-08-17 08:53:37 +00:00
|
|
|
(*pos)++;
|
|
|
|
return __next(m, pos);
|
|
|
|
}
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2009-08-17 08:53:37 +00:00
|
|
|
static void *t_start(struct seq_file *m, loff_t *pos)
|
|
|
|
{
|
2008-08-28 03:31:01 +00:00
|
|
|
local_irq_disable();
|
2010-02-02 07:32:09 +00:00
|
|
|
|
2017-04-06 19:47:32 +00:00
|
|
|
__this_cpu_inc(disable_stack_tracer);
|
2010-02-02 07:32:09 +00:00
|
|
|
|
2015-11-03 19:50:15 +00:00
|
|
|
arch_spin_lock(&stack_trace_max_lock);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
function tracing: fix wrong position computing of stack_trace
Impact: make output of stack_trace complete if buffer overruns
When read buffer overruns, the output of stack_trace isn't complete.
When printing records with seq_printf in t_show, if the read buffer
has overruned by the current record, then this record won't be
printed to user space through read buffer, it will just be dropped in
this printing.
When next printing, t_start should return the "*pos"th record, which
is the one dropped by previous printing, but it just returns
(m->private + *pos)th record.
Here we use a more sane method to implement seq_operations which can
be found in kernel code. Thus we needn't initialize m->private.
About testing, it's not easy to overrun read buffer, but we can use
seq_printf to print more padding bytes in t_show, then it's easy to
check whether or not records are lost.
This commit has been tested on both condition of overrun and non
overrun.
Signed-off-by: Liming Wang <liming.wang@windriver.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-11-21 03:00:18 +00:00
|
|
|
if (*pos == 0)
|
|
|
|
return SEQ_START_TOKEN;
|
|
|
|
|
2009-08-17 08:53:37 +00:00
|
|
|
return __next(m, pos);
|
2008-08-28 03:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void t_stop(struct seq_file *m, void *p)
|
|
|
|
{
|
2015-11-03 19:50:15 +00:00
|
|
|
arch_spin_unlock(&stack_trace_max_lock);
|
2010-02-02 07:32:09 +00:00
|
|
|
|
2017-04-06 19:47:32 +00:00
|
|
|
__this_cpu_dec(disable_stack_tracer);
|
2010-02-02 07:32:09 +00:00
|
|
|
|
2008-08-28 03:31:01 +00:00
|
|
|
local_irq_enable();
|
|
|
|
}
|
|
|
|
|
2015-04-15 23:18:22 +00:00
|
|
|
static void trace_lookup_stack(struct seq_file *m, long i)
|
2008-08-28 03:31:01 +00:00
|
|
|
{
|
2008-08-29 20:51:43 +00:00
|
|
|
unsigned long addr = stack_dump_trace[i];
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2015-04-15 23:18:22 +00:00
|
|
|
seq_printf(m, "%pS\n", (void *)addr);
|
2008-08-28 03:31:01 +00:00
|
|
|
}
|
|
|
|
|
2009-03-12 23:42:29 +00:00
|
|
|
static void print_disabled(struct seq_file *m)
|
|
|
|
{
|
|
|
|
seq_puts(m, "#\n"
|
|
|
|
"# Stack tracer disabled\n"
|
|
|
|
"#\n"
|
|
|
|
"# To enable the stack tracer, either add 'stacktrace' to the\n"
|
|
|
|
"# kernel command line\n"
|
|
|
|
"# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
|
|
|
|
"#\n");
|
|
|
|
}
|
|
|
|
|
2008-08-28 03:31:01 +00:00
|
|
|
static int t_show(struct seq_file *m, void *v)
|
|
|
|
{
|
function tracing: fix wrong position computing of stack_trace
Impact: make output of stack_trace complete if buffer overruns
When read buffer overruns, the output of stack_trace isn't complete.
When printing records with seq_printf in t_show, if the read buffer
has overruned by the current record, then this record won't be
printed to user space through read buffer, it will just be dropped in
this printing.
When next printing, t_start should return the "*pos"th record, which
is the one dropped by previous printing, but it just returns
(m->private + *pos)th record.
Here we use a more sane method to implement seq_operations which can
be found in kernel code. Thus we needn't initialize m->private.
About testing, it's not easy to overrun read buffer, but we can use
seq_printf to print more padding bytes in t_show, then it's easy to
check whether or not records are lost.
This commit has been tested on both condition of overrun and non
overrun.
Signed-off-by: Liming Wang <liming.wang@windriver.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-11-21 03:00:18 +00:00
|
|
|
long i;
|
2008-08-29 20:51:43 +00:00
|
|
|
int size;
|
|
|
|
|
function tracing: fix wrong position computing of stack_trace
Impact: make output of stack_trace complete if buffer overruns
When read buffer overruns, the output of stack_trace isn't complete.
When printing records with seq_printf in t_show, if the read buffer
has overruned by the current record, then this record won't be
printed to user space through read buffer, it will just be dropped in
this printing.
When next printing, t_start should return the "*pos"th record, which
is the one dropped by previous printing, but it just returns
(m->private + *pos)th record.
Here we use a more sane method to implement seq_operations which can
be found in kernel code. Thus we needn't initialize m->private.
About testing, it's not easy to overrun read buffer, but we can use
seq_printf to print more padding bytes in t_show, then it's easy to
check whether or not records are lost.
This commit has been tested on both condition of overrun and non
overrun.
Signed-off-by: Liming Wang <liming.wang@windriver.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-11-21 03:00:18 +00:00
|
|
|
if (v == SEQ_START_TOKEN) {
|
2009-03-13 04:00:58 +00:00
|
|
|
seq_printf(m, " Depth Size Location"
|
2008-08-29 20:51:43 +00:00
|
|
|
" (%d entries)\n"
|
2009-03-13 04:00:58 +00:00
|
|
|
" ----- ---- --------\n",
|
2015-10-30 05:25:39 +00:00
|
|
|
stack_trace_max.nr_entries);
|
2009-03-12 23:42:29 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
if (!stack_tracer_enabled && !stack_trace_max_size)
|
2009-03-12 23:42:29 +00:00
|
|
|
print_disabled(m);
|
|
|
|
|
2008-08-29 20:51:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-08-28 03:31:01 +00:00
|
|
|
|
function tracing: fix wrong position computing of stack_trace
Impact: make output of stack_trace complete if buffer overruns
When read buffer overruns, the output of stack_trace isn't complete.
When printing records with seq_printf in t_show, if the read buffer
has overruned by the current record, then this record won't be
printed to user space through read buffer, it will just be dropped in
this printing.
When next printing, t_start should return the "*pos"th record, which
is the one dropped by previous printing, but it just returns
(m->private + *pos)th record.
Here we use a more sane method to implement seq_operations which can
be found in kernel code. Thus we needn't initialize m->private.
About testing, it's not easy to overrun read buffer, but we can use
seq_printf to print more padding bytes in t_show, then it's easy to
check whether or not records are lost.
This commit has been tested on both condition of overrun and non
overrun.
Signed-off-by: Liming Wang <liming.wang@windriver.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-11-21 03:00:18 +00:00
|
|
|
i = *(long *)v;
|
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
if (i >= stack_trace_max.nr_entries ||
|
2008-08-29 20:51:43 +00:00
|
|
|
stack_dump_trace[i] == ULONG_MAX)
|
2008-08-28 03:31:01 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
if (i+1 == stack_trace_max.nr_entries ||
|
2008-08-29 20:51:43 +00:00
|
|
|
stack_dump_trace[i+1] == ULONG_MAX)
|
2015-10-30 05:25:39 +00:00
|
|
|
size = stack_trace_index[i];
|
2008-08-29 20:51:43 +00:00
|
|
|
else
|
2015-10-30 05:25:39 +00:00
|
|
|
size = stack_trace_index[i] - stack_trace_index[i+1];
|
2008-08-29 20:51:43 +00:00
|
|
|
|
2015-10-30 05:25:39 +00:00
|
|
|
seq_printf(m, "%3ld) %8d %5d ", i, stack_trace_index[i], size);
|
2008-08-29 20:51:43 +00:00
|
|
|
|
|
|
|
trace_lookup_stack(m, i);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-17 04:06:40 +00:00
|
|
|
static const struct seq_operations stack_trace_seq_ops = {
|
2008-08-28 03:31:01 +00:00
|
|
|
.start = t_start,
|
|
|
|
.next = t_next,
|
|
|
|
.stop = t_stop,
|
|
|
|
.show = t_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int stack_trace_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2009-07-23 03:28:40 +00:00
|
|
|
return seq_open(file, &stack_trace_seq_ops);
|
2008-08-28 03:31:01 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 04:06:40 +00:00
|
|
|
static const struct file_operations stack_trace_fops = {
|
2008-08-28 03:31:01 +00:00
|
|
|
.open = stack_trace_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
2009-07-23 03:28:40 +00:00
|
|
|
.release = seq_release,
|
2008-08-28 03:31:01 +00:00
|
|
|
};
|
|
|
|
|
2011-12-19 19:44:09 +00:00
|
|
|
static int
|
|
|
|
stack_trace_filter_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return ftrace_regex_open(&trace_ops, FTRACE_ITER_FILTER,
|
|
|
|
inode, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations stack_trace_filter_fops = {
|
|
|
|
.open = stack_trace_filter_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.write = ftrace_filter_write,
|
2013-12-21 22:39:40 +00:00
|
|
|
.llseek = tracing_lseek,
|
2011-12-19 19:44:09 +00:00
|
|
|
.release = ftrace_regex_release,
|
|
|
|
};
|
|
|
|
|
2008-12-17 04:06:40 +00:00
|
|
|
int
|
|
|
|
stack_trace_sysctl(struct ctl_table *table, int write,
|
2009-09-23 22:57:19 +00:00
|
|
|
void __user *buffer, size_t *lenp,
|
2008-12-17 04:06:40 +00:00
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&stack_sysctl_mutex);
|
|
|
|
|
2009-09-23 22:57:19 +00:00
|
|
|
ret = proc_dointvec(table, write, buffer, lenp, ppos);
|
2008-12-17 04:06:40 +00:00
|
|
|
|
|
|
|
if (ret || !write ||
|
2009-06-26 08:55:51 +00:00
|
|
|
(last_stack_tracer_enabled == !!stack_tracer_enabled))
|
2008-12-17 04:06:40 +00:00
|
|
|
goto out;
|
|
|
|
|
2009-06-26 08:55:51 +00:00
|
|
|
last_stack_tracer_enabled = !!stack_tracer_enabled;
|
2008-12-17 04:06:40 +00:00
|
|
|
|
|
|
|
if (stack_tracer_enabled)
|
|
|
|
register_ftrace_function(&trace_ops);
|
|
|
|
else
|
|
|
|
unregister_ftrace_function(&trace_ops);
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&stack_sysctl_mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:01:00 +00:00
|
|
|
static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
|
|
|
|
|
2008-12-17 04:06:40 +00:00
|
|
|
static __init int enable_stacktrace(char *str)
|
|
|
|
{
|
2011-12-20 03:01:00 +00:00
|
|
|
if (strncmp(str, "_filter=", 8) == 0)
|
|
|
|
strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
|
|
|
|
|
2008-12-17 14:43:00 +00:00
|
|
|
stack_tracer_enabled = 1;
|
|
|
|
last_stack_tracer_enabled = 1;
|
2008-12-17 04:06:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
__setup("stacktrace", enable_stacktrace);
|
|
|
|
|
2008-08-28 03:31:01 +00:00
|
|
|
static __init int stack_trace_init(void)
|
|
|
|
{
|
|
|
|
struct dentry *d_tracer;
|
|
|
|
|
|
|
|
d_tracer = tracing_init_dentry();
|
2015-01-20 16:14:16 +00:00
|
|
|
if (IS_ERR(d_tracer))
|
2013-04-10 00:18:12 +00:00
|
|
|
return 0;
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2009-03-26 23:25:38 +00:00
|
|
|
trace_create_file("stack_max_size", 0644, d_tracer,
|
2015-10-30 05:25:39 +00:00
|
|
|
&stack_trace_max_size, &stack_max_size_fops);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2009-03-26 23:25:38 +00:00
|
|
|
trace_create_file("stack_trace", 0444, d_tracer,
|
|
|
|
NULL, &stack_trace_fops);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
2011-12-19 19:44:09 +00:00
|
|
|
trace_create_file("stack_trace_filter", 0444, d_tracer,
|
|
|
|
NULL, &stack_trace_filter_fops);
|
|
|
|
|
2011-12-20 03:01:00 +00:00
|
|
|
if (stack_trace_filter_buf[0])
|
|
|
|
ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
|
|
|
|
|
2008-12-17 14:43:00 +00:00
|
|
|
if (stack_tracer_enabled)
|
2008-12-17 04:06:40 +00:00
|
|
|
register_ftrace_function(&trace_ops);
|
2008-08-28 03:31:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_initcall(stack_trace_init);
|