Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c7e9ad7d authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull perf fixes from Ingo Molnar:

 - fix the perf build, by fixing the rbtree.c sharing bug between kernel
   and tools/perf by creating a local copy of rbtree.c (more will be
   done for v4.3)

 - fix an AUX buffer (Intel-PT support) refcounting bug

 - fix copy_from_user_nmi() return value"

* 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  perf/x86: Fix copy_from_user_nmi() return if range is not ok
  perf: Fix AUX buffer refcounting
  tools: Copy rbtree_augmented.h from the kernel
  tools: Move rbtree.h from tools/perf/
  tools: Copy lib/rbtree.c to tools/lib/
  perf tools: Copy rbtree.h from the kernel
  tools: Adopt {READ,WRITE_ONCE} from the kernel
parents 1c4c7159 ebf2d268
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
	unsigned long ret;

	if (__range_not_ok(from, n, TASK_SIZE))
		return 0;
		return n;

	/*
	 * Even though this function is typically called from NMI/IRQ context
+0 −8
Original line number Diff line number Diff line
@@ -4358,14 +4358,6 @@ static void ring_buffer_wakeup(struct perf_event *event)
	rcu_read_unlock();
}

static void rb_free_rcu(struct rcu_head *rcu_head)
{
	struct ring_buffer *rb;

	rb = container_of(rcu_head, struct ring_buffer, rcu_head);
	rb_free(rb);
}

struct ring_buffer *ring_buffer_get(struct perf_event *event)
{
	struct ring_buffer *rb;
+10 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
struct ring_buffer {
	atomic_t			refcount;
	struct rcu_head			rcu_head;
	struct irq_work			irq_work;
#ifdef CONFIG_PERF_USE_VMALLOC
	struct work_struct		work;
	int				page_order;	/* allocation order  */
@@ -55,6 +56,15 @@ struct ring_buffer {
};

extern void rb_free(struct ring_buffer *rb);

static inline void rb_free_rcu(struct rcu_head *rcu_head)
{
	struct ring_buffer *rb;

	rb = container_of(rcu_head, struct ring_buffer, rcu_head);
	rb_free(rb);
}

extern struct ring_buffer *
rb_alloc(int nr_pages, long watermark, int cpu, int flags);
extern void perf_event_wakeup(struct perf_event *event);
+25 −2
Original line number Diff line number Diff line
@@ -221,6 +221,8 @@ void perf_output_end(struct perf_output_handle *handle)
	rcu_read_unlock();
}

static void rb_irq_work(struct irq_work *work);

static void
ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
{
@@ -241,6 +243,16 @@ ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)

	INIT_LIST_HEAD(&rb->event_list);
	spin_lock_init(&rb->event_lock);
	init_irq_work(&rb->irq_work, rb_irq_work);
}

static void ring_buffer_put_async(struct ring_buffer *rb)
{
	if (!atomic_dec_and_test(&rb->refcount))
		return;

	rb->rcu_head.next = (void *)rb;
	irq_work_queue(&rb->irq_work);
}

/*
@@ -319,7 +331,7 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,
	rb_free_aux(rb);

err:
	ring_buffer_put(rb);
	ring_buffer_put_async(rb);
	handle->event = NULL;

	return NULL;
@@ -370,7 +382,7 @@ void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,

	local_set(&rb->aux_nest, 0);
	rb_free_aux(rb);
	ring_buffer_put(rb);
	ring_buffer_put_async(rb);
}

/*
@@ -557,7 +569,18 @@ static void __rb_free_aux(struct ring_buffer *rb)
void rb_free_aux(struct ring_buffer *rb)
{
	if (atomic_dec_and_test(&rb->aux_refcount))
		irq_work_queue(&rb->irq_work);
}

static void rb_irq_work(struct irq_work *work)
{
	struct ring_buffer *rb = container_of(work, struct ring_buffer, irq_work);

	if (!atomic_read(&rb->aux_refcount))
		__rb_free_aux(rb);

	if (rb->rcu_head.next == (void *)rb)
		call_rcu(&rb->rcu_head, rb_free_rcu);
}

#ifndef CONFIG_PERF_USE_VMALLOC
+58 −0
Original line number Diff line number Diff line
@@ -41,4 +41,62 @@

#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))

#include <linux/types.h>

static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
{
	switch (size) {
	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;
	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;
	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;
	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;
	default:
		barrier();
		__builtin_memcpy((void *)res, (const void *)p, size);
		barrier();
	}
}

static __always_inline void __write_once_size(volatile void *p, void *res, int size)
{
	switch (size) {
	case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
	case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
	case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
	case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
	default:
		barrier();
		__builtin_memcpy((void *)p, (const void *)res, size);
		barrier();
	}
}

/*
 * Prevent the compiler from merging or refetching reads or writes. The
 * compiler is also forbidden from reordering successive instances of
 * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
 * compiler is aware of some particular ordering.  One way to make the
 * compiler aware of ordering is to put the two invocations of READ_ONCE,
 * WRITE_ONCE or ACCESS_ONCE() in different C statements.
 *
 * In contrast to ACCESS_ONCE these two macros will also work on aggregate
 * data types like structs or unions. If the size of the accessed data
 * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
 * READ_ONCE() and WRITE_ONCE()  will fall back to memcpy and print a
 * compile-time warning.
 *
 * Their two major use cases are: (1) Mediating communication between
 * process-level code and irq/NMI handlers, all running on the same CPU,
 * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
 * mutilate accesses that either do not require ordering or that interact
 * with an explicit memory barrier or atomic instruction that provides the
 * required ordering.
 */

#define READ_ONCE(x) \
	({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })

#define WRITE_ONCE(x, val) \
	({ union { typeof(x) __val; char __c[1]; } __u = { .__val = (val) }; __write_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })

#endif /* _TOOLS_LINUX_COMPILER_H */
Loading