Skip to contentSkip to conversation

perf vs d-cache aliasses

RFC PATCH In review Peter Zijlstra 3 participants 5 messages Sep 21, 2009 – Sep 22, 2009 SPARC + UltraSPARC (sparc/sparc64)

lore
Share & tools
lore
#2 lore raw

RFC PATCH 2/2 perf,sparc: Use vmalloc to back the mmap() array

In reply to Peter Zijlstra

Implement vmalloc() backed storage for perf_mmap().

Alternatively we could put this code in kernel/perf_counter_vmalloc.c and conditionally compile that or something, since there really isn't anything Sparc specific about it.

LKML-Reference: <new-submission>

---
 arch/sparc/kernel/perf_counter.c |   72 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
arch/sparc/kernel/perf_counter.c +72 -0
Index: linux-2.6/arch/sparc/kernel/perf_counter.c
===================================================================
@@ -14,6 +14,7 @@
 
 #include <linux/perf_counter.h>
 #include <linux/kprobes.h>
+#include <linux/vmalloc.h>
 #include <linux/kernel.h>
 #include <linux/kdebug.h>
 #include <linux/mutex.h>
@@ -554,3 +555,74 @@ void __init init_hw_perf_counters(void)
 
 	register_die_notifier(&perf_counter_nmi_notifier);
 }
+
+struct page *perf_mmap_to_page(void *addr)
+{
+	return vmalloc_to_page(addr);
+}
+
+static void perf_mmap_unmark_page(void *addr)
+{
+	struct page *page = vmalloc_to_page(addr);
+
+	page->mapping = NULL;
+}
+
+static void perf_mmap_data_free_work(struct work_struct *work)
+{
+	struct perf_mmap_data *data;
+	void *base;
+	int i;
+
+	data = container_of(work, struct perf_mmap_data, work);
+
+	base = data->user_page;
+	for (i = 0; i < data->nr_pages + 1; i++)
+		perf_mmap_unmark_page(base + (i * PAGE_SIZE));
+
+	vfree(base);
+}
+
+void perf_mmap_data_free(struct perf_mmap_data *data)
+{
+	schedule_work(&data->work);
+}
+
+struct perf_mmap_data *
+perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
+{
+	struct perf_mmap_data *data;
+	unsigned long size;
+	void *all_buf;
+	int i;
+
+	WARN_ON(atomic_read(&counter->mmap_count));
+
+	size = sizeof(struct perf_mmap_data);
+	size += nr_pages * sizeof(void *);
+
+	data = kzalloc(size, GFP_KERNEL);
+	if (!data)
+		goto fail;
+
+	INIT_WORK(&data->work, perf_mmap_data_free_work);
+
+	all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
+	if (!all_buf)
+		goto fail_all_buf;
+
+	data->user_page = all_buf;
+
+	for (i = 0; i < nr_pages; i++)
+		data->data_pages[i] = all_buf + ((i + 1) * PAGE_SIZE);
+
+	data->nr_pages = nr_pages;
+
+	return data;
+
+fail_all_buf:
+	kfree(data);
+
+fail:
+	return NULL;
+}
#1 lore raw

RFC PATCH 1/2 perf: Provide weak interfaces to mmap() backing

In reply to Peter Zijlstra

Some architectures such as Sparc, ARM and MIPS (basically everything with flush_dcache_page()) need to deal with dcache aliases by carefully placing pages in both kernel and user maps.

These architectures typically have to use vmalloc_user() for this.

However, on other architectures, vmalloc() is not needed and has the downsides of being more restricted and slower than regular allocations.

Hence reshape the code so that we can do either by over-riding a few simply functions.

LKML-Reference: <new-submission>

---
 include/linux/perf_counter.h |   18 ++++++++++
 kernel/perf_counter.c        |   72 ++++++++++++++++++++++++++++---------------
 2 files changed, 66 insertions(+), 24 deletions(-)
include/linux/perf_counter.h +18 -0
Index: linux-2.6/include/linux/perf_counter.h
===================================================================
@@ -513,6 +513,7 @@ struct file;
 
 struct perf_mmap_data {
 	struct rcu_head			rcu_head;
+	struct work_struct		work;
 	int				nr_pages;	/* nr of data pages  */
 	int				writable;	/* are we writable   */
 	int				nr_locked;	/* nr pages mlocked  */
@@ -533,6 +534,23 @@ struct perf_mmap_data {
 	void				*data_pages[0];
 };
 
+/*
+ * The below three functions dealing with the mmap() backing are weak
+ * functions, which allow the arch implementation to over-ride them:
+ *
+ *   struct page *perf_mmap_to_page(void *addr);
+ *   struct perf_mmap_data *
+ *   perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages);
+ *   void perf_mmap_data_free(struct perf_mmap_data *data);
+ *
+ * They default to allocating memory using get_zeroed_page(GFP_KERNEL).
+ */
+
+struct page *perf_mmap_to_page(void *addr);
+struct perf_mmap_data *
+perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages);
+void perf_mmap_data_free(struct perf_mmap_data *data);
+
 struct perf_pending_entry {
 	struct perf_pending_entry *next;
 	void (*func)(struct perf_pending_entry *);
kernel/perf_counter.c +48 -24

151 lines not on this page · raw message

#0 lore raw

cover perf vs d-cache aliasses

Hi David,

based on your vmalloc code, an alternative implementation that lets x86 stay with regular allocations.

While I'm sad to have to propose having two implementations around, I see no other way to please both sides.

Alternative to placing the code in arch/sparc we could do something like

CONFIG_PERF_WANTS_VMALLOC and place both somewhere in kernel/

maintainer
#3 lore raw

From: Peter Zijlstra <[email protected]>
Date: Mon, 21 Sep 2009 16:08:48 +0200

Quoted 6 lines

based on your vmalloc code, an alternative implementation that lets x86 stay with regular allocations.

While I'm sad to have to propose having two implementations around, I see no other way to please both sides.

If you guys want to maintain two implementations of the same thing, that's fine with me :)

#4 lore raw
Quoted 4 lines · David Miller

From: Peter Zijlstra <[email protected]>
Date: Mon, 21 Sep 2009 16:08:48 +0200

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

based on your vmalloc code, an alternative implementation that lets x86 stay with regular allocations.

While I'm sad to have to propose having two implementations around, I see no other way to please both sides.

If you guys want to maintain two implementations of the same thing, that's fine with me :)

I take this as an acked-by :-)

I'll reshape these patches on top of the counters -> events rename.

Ingo