Skip to contentSkip to conversation

perf/x86/intel/bts: Small fixes

PATCH Applied Alexander Shishkin 3 participants 10 messages Dec 5, 2019 – Dec 17, 2019

Subsystems (2)
lore
Share & tools
lore

Alexander Shishkin

author reviewer
#0 lore raw

cover perf/x86/intel/bts: Small fixes

Hi Peter and Ingo,

Here are two small fixes that resulted from running perf_fuzzer on a !KPTI kernel. One is a misguided and unannotated warning and another is a sketchy use of page_private(). The choice between deleting the BTS driver and fixing it is not obvious, though. It may theoretically still have users.

Alexander Shishkin (2):
perf/x86/intel/bts: Remove a silly warning perf/x86/intel/bts: Fix the use of page_private()

 arch/x86/events/intel/bts.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
Signature · 1 line
2.24.0
author reviewer
#1 lore raw

PATCH 1/2 perf/x86/intel/bts: Remove a silly warning

Commit

8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")

brought in a warning with the BTS buffer initialization that doesn't make sense, but is easily tripped with (assuming KPTI is disabled):

instantly throwing:

Quoted 9 lines
------------[ cut here ]------------
WARNING: CPU: 2 PID: 326 at arch/x86/events/intel/bts.c:86 bts_buffer_setup_aux+0x117/0x3d0
Modules linked in:
CPU: 2 PID: 326 Comm: perf Not tainted 5.4.0-rc8-00291-gceb9e77324fa #904
RIP: 0010:bts_buffer_setup_aux+0x117/0x3d0
Call Trace:
 rb_alloc_aux+0x339/0x550
 perf_mmap+0x607/0xc70
 mmap_region+0x76b/0xbd0

...

There is no comment or record anywhere that would explain the train of thought that went into this warning, it probably tried to make sure that the high order allocations indeed happened in the ring buffer code.

The rest of the driver handles multiple order-0 pages in the buffer just fine. Therefore, get rid the warning. This was accidentally discovered by Vince's perf_fuzzer.

Fixes 8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")
Cc Vince Weaver <[email protected]>
---
 arch/x86/events/intel/bts.c | 2 --
 1 file changed, 2 deletions(-)
arch/x86/events/intel/bts.c +0 -2
index 38de4a7f6752..d53b4fb86d87 100644
@@ -83,8 +83,6 @@ bts_buffer_setup_aux(struct perf_event *event, void **pages,
 	/* count all the high order buffers */
 	for (pg = 0, nbuf = 0; pg < nr_pages;) {
 		page = virt_to_page(pages[pg]);
-		if (WARN_ON_ONCE(!PagePrivate(page) && nr_pages > 1))
-			return NULL;
 		pg += 1 << page_private(page);
 		nbuf++;
 	}
Signature · 1 line
2.24.0
maintainer
#2 lore raw

Re: [PATCH 1/2] perf/x86/intel/bts: Remove a silly warning

Quoted 8 lines · Alexander Shishkin

There is no comment or record anywhere that would explain the train of thought that went into this warning, it probably tried to make sure that the high order allocations indeed happened in the ring buffer code.

arch/x86/events/intel/bts.c bts_buffer_setup_aux(struct perf_event *event, void **pages, · line 86

Quoted 6 lines
arch/x86/events/intel/bts.c +0 -2
@@ -83,8 +83,6 @@ bts_buffer_setup_aux(struct perf_event *event, void **pages,
 	/* count all the high order buffers */
 	for (pg = 0, nbuf = 0; pg < nr_pages;) {
 		page = virt_to_page(pages[pg]);
-		if (WARN_ON_ONCE(!PagePrivate(page) && nr_pages > 1))
-			return NULL;
 		pg += 1 << page_private(page);

I'm thinking that because ^^^^ uses page_private(), it wants to make sure PagePrivate().

I haven't checked the current rules, but using page_private() without PagePrivate() seems dodgy.

Also consider:

+ __nr_pages = PagePrivate(page) ? 1 << page_private(page) : 1;

Quoted 5 lines
arch/x86/events/intel/bts.c +0 -0
 		nbuf++;
 	}
Signature · 1 line
2.24.0
#3 lore raw

[tip: perf/urgent] perf/x86/intel/bts: Fix the use of page_private()

In reply to Alexander Shishkin
The following commit has been merged into the perf/urgent branch of tip:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     ff61541cc6c1962957758ba433c574b76f588d23
Gitweb:        https://git.kernel.org/tip/ff61541cc6c1962957758ba433c574b76f588d23
Author:        Alexander Shishkin <[email protected]>
AuthorDate:    Thu, 05 Dec 2019 17:28:52 +03:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 17 Dec 2019 13:32:46 +01:00

perf/x86/intel/bts: Fix the use of page_private()

Commit

8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")

brought in a warning with the BTS buffer initialization that is easily tripped with (assuming KPTI is disabled):

instantly throwing:

Quoted 9 lines
------------[ cut here ]------------
WARNING: CPU: 2 PID: 326 at arch/x86/events/intel/bts.c:86 bts_buffer_setup_aux+0x117/0x3d0
Modules linked in:
CPU: 2 PID: 326 Comm: perf Not tainted 5.4.0-rc8-00291-gceb9e77324fa #904
RIP: 0010:bts_buffer_setup_aux+0x117/0x3d0
Call Trace:
 rb_alloc_aux+0x339/0x550
 perf_mmap+0x607/0xc70
 mmap_region+0x76b/0xbd0

...

It appears to assume (for lost raisins) that PagePrivate() is set, while later it actually tests for PagePrivate() before using page_private().

Make it consistent and always check PagePrivate() before using page_private().

Fixes 8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")
Cc Jiri Olsa <[email protected]>
Cc Vince Weaver <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 arch/x86/events/intel/bts.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
arch/x86/events/intel/bts.c +11 -5
index 38de4a7..6a3b599 100644
@@ -63,9 +63,17 @@ struct bts_buffer {
 
 static struct pmu bts_pmu;
 
+static int buf_nr_pages(struct page *page)
+{
+	if (!PagePrivate(page))
+		return 1;
+
+	return 1 << page_private(page);
+}
+
 static size_t buf_size(struct page *page)
 {
-	return 1 << (PAGE_SHIFT + page_private(page));
+	return buf_nr_pages(page) * PAGE_SIZE;
 }
 
 static void *
@@ -83,9 +91,7 @@ bts_buffer_setup_aux(struct perf_event *event, void **pages,
 	/* count all the high order buffers */
 	for (pg = 0, nbuf = 0; pg < nr_pages;) {
 		page = virt_to_page(pages[pg]);
-		if (WARN_ON_ONCE(!PagePrivate(page) && nr_pages > 1))
-			return NULL;
-		pg += 1 << page_private(page);
+		pg += buf_nr_pages(page);
 		nbuf++;
 	}
 
@@ -109,7 +115,7 @@ bts_buffer_setup_aux(struct perf_event *event, void **pages,
 		unsigned int __nr_pages;
 
 		page = virt_to_page(pages[pg]);
-		__nr_pages = PagePrivate(page) ? 1 << page_private(page) : 1;
+		__nr_pages = buf_nr_pages(page);
 		buf->buf[nbuf].page = page;
 		buf->buf[nbuf].offset = offset;
 		buf->buf[nbuf].displacement = (pad ? BTS_RECORD_SIZE - pad : 0);
author reviewer
#4 lore raw

PATCH 2/2 perf/x86/intel/bts: Fix the use of page_private()

In reply to Alexander Shishkin

Commit

8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")

uses page_private(page) without checking the PagePrivate(page) first, which seems like a potential bug, considering that page->private aliases with other stuff in struct page.

Fix this by checking PagePrivate() first.

Fixes 8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")
---
 arch/x86/events/intel/bts.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/x86/events/intel/bts.c +11 -3
index d53b4fb86d87..9e4da1c5a129 100644
@@ -63,9 +63,17 @@ struct bts_buffer {
 
 static struct pmu bts_pmu;
 
+static int buf_nr_pages(struct page *page)
+{
+	if (!PagePrivate(page))
+		return 1;
+
+	return 1 << page_private(page);
+}
+
 static size_t buf_size(struct page *page)
 {
-	return 1 << (PAGE_SHIFT + page_private(page));
+	return 1 << (PAGE_SHIFT + buf_nr_pages(page));
 }
 
 static void *
@@ -83,7 +91,7 @@ bts_buffer_setup_aux(struct perf_event *event, void **pages,
 	/* count all the high order buffers */
 	for (pg = 0, nbuf = 0; pg < nr_pages;) {
 		page = virt_to_page(pages[pg]);
-		pg += 1 << page_private(page);
+		pg += buf_nr_pages(page);
 		nbuf++;
 	}
 
@@ -107,7 +115,7 @@ bts_buffer_setup_aux(struct perf_event *event, void **pages,
 		unsigned int __nr_pages;
 
 		page = virt_to_page(pages[pg]);
-		__nr_pages = PagePrivate(page) ? 1 << page_private(page) : 1;
+		__nr_pages = buf_nr_pages(page);
 		buf->buf[nbuf].page = page;
 		buf->buf[nbuf].offset = offset;
 		buf->buf[nbuf].displacement = (pad ? BTS_RECORD_SIZE - pad : 0);
Signature · 1 line
2.24.0
maintainer
#5 lore raw

Re: [PATCH 2/2] perf/x86/intel/bts: Fix the use of page_private()

Quoted 5 lines · Alexander Shishkin

Commit

8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")

uses page_private(page) without checking the PagePrivate(page) first,

Well, arguably it did check it, but you didn't like that WARN ;-)

Quoted 16 lines

which seems like a potential bug, considering that page->private aliases with other stuff in struct page.

Fix this by checking PagePrivate() first.

Fixes 8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")
---
 arch/x86/events/intel/bts.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/x86/events/intel/bts.c +0 -0
index d53b4fb86d87..9e4da1c5a129 100644

arch/x86/events/intel/bts.c struct bts_buffer { · line 72

Quoted 10 lines
arch/x86/events/intel/bts.c +7 -0
@@ -63,9 +63,17 @@ struct bts_buffer {
 
 static struct pmu bts_pmu;
 
+static int buf_nr_pages(struct page *page)
+{
+	if (!PagePrivate(page))
+		return 1;
+
+	return 1 << page_private(page);
+}

Yes, that seems like a sensible helper.

maintainer
#6 lore raw

Re: [PATCH 2/2] perf/x86/intel/bts: Fix the use of page_private()

In reply to Alexander Shishkin
Quoted 26 lines · Alexander Shishkin

Commit

8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")

uses page_private(page) without checking the PagePrivate(page) first, which seems like a potential bug, considering that page->private aliases with other stuff in struct page.

Fix this by checking PagePrivate() first.

Fixes 8062382c8dbe2 ("perf/x86/intel/bts: Add BTS PMU driver")
---
 arch/x86/events/intel/bts.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/x86/events/intel/bts.c +2 -0
index d53b4fb86d87..9e4da1c5a129 100644
@@ -63,9 +63,17 @@ struct bts_buffer {
 
 static struct pmu bts_pmu;
 
+static int buf_nr_pages(struct page *page)
+{

arch/x86/events/intel/bts.c struct bts_buffer { · line 76

Quoted 10 lines
arch/x86/events/intel/bts.c +7 -1
+	if (!PagePrivate(page))
+		return 1;
+
+	return 1 << page_private(page);
+}
+
 static size_t buf_size(struct page *page)
 {
-	return 1 << (PAGE_SHIFT + page_private(page));
+	return 1 << (PAGE_SHIFT + buf_nr_pages(page));

Hurmph, shouldn't that be:

	return buf_nr_pages(page) * PAGE_SIZE;

?

author reviewer
#7 lore raw

Re: [PATCH 2/2] perf/x86/intel/bts: Fix the use of page_private()

Quoted 10 lines · Peter Zijlstra

arch/x86/events/intel/bts.c struct bts_buffer { · line 76

Quoted 4 lines

static size_t buf_size(struct page *page)
{

arch/x86/events/intel/bts.c +1 -1
-	return 1 << (PAGE_SHIFT + page_private(page));
+	return 1 << (PAGE_SHIFT + buf_nr_pages(page));

Hurmph, shouldn't that be:

	return buf_nr_pages(page) * PAGE_SIZE;

?

True, that one's broken.

maintainer
#8 lore raw
In reply to Alexander Shishkin
Quoted 10 lines · Alexander Shishkin

Hi Peter and Ingo,

Here are two small fixes that resulted from running perf_fuzzer on a !KPTI kernel. One is a misguided and unannotated warning and another is a sketchy use of page_private(). The choice between deleting the BTS driver and fixing it is not obvious, though. It may theoretically still have users.

Alexander Shishkin (2):
perf/x86/intel/bts: Remove a silly warning perf/x86/intel/bts: Fix the use of page_private()

I'll squash the pair, that makes more sense to me.

author reviewer
#9 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Alexander Shishkin

Hi Peter and Ingo,

Here are two small fixes that resulted from running perf_fuzzer on a !KPTI kernel. One is a misguided and unannotated warning and another is a sketchy use of page_private(). The choice between deleting the BTS driver and fixing it is not obvious, though. It may theoretically still have users.

Quoted 5 lines
Quoted 3 lines

Alexander Shishkin (2):
perf/x86/intel/bts: Remove a silly warning perf/x86/intel/bts: Fix the use of page_private()

I'll squash the pair, that makes more sense to me.

Thanks!