Skip to contentSkip to conversation

trace: Fix incorrect number of pages used for trace metadata

PATCH In review Igor Druzhinin 3 participants 7 messages Sep 29, 2016 – Oct 4, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trace: Fix incorrect number of pages used for trace metadata

As long as t_info_first_offset is calculated in uint32_t offsets we need to multiply it by sizeof(uint32_t) in order to get the right number of pages for trace metadata. Not doing that makes it impossible to read the trace buffer correctly from userspace for some corner cases.

---
 xen/common/trace.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/trace.c +1 -1
index f651cf3..a9563cc 100644
@@ -149,7 +149,7 @@ static int calculate_tbuf_size(unsigned int pages, uint16_t t_info_first_offset)
     }
 
     t_info_words = num_online_cpus() * pages * sizeof(uint32_t);
-    t_info_pages = PFN_UP(t_info_first_offset + t_info_words);
+    t_info_pages = PFN_UP(t_info_first_offset * sizeof(uint32_t) + t_info_words);
     printk(XENLOG_INFO "xentrace: requesting %u t_info pages "
            "for %u trace pages on %u cpus\n",
            t_info_pages, pages, num_online_cpus());
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 6 lines · Igor Druzhinin

As long as t_info_first_offset is calculated in uint32_t offsets we need to multiply it by sizeof(uint32_t) in order to get the right number of pages for trace metadata. Not doing that makes it impossible to read the trace buffer correctly from userspace for some corner cases.

Hmm, looks like we actually want to revert this c/s fbf96e6, "xentrace:
correct formula to calculate t_info_pages". But that one was presumably written (and Acked by me) because the variable name there, t_info_first_offset, is confusing.

The other mistake in fbf96e6 is that before t_info_words was actually denominated in words; but after it's denominated in bytes (which is again confusing).

What about something like the attached instead? This should fix your problem while making the code clearer.

-George

From b0252cec4a96fea28faa85c47ab0f5d5997444ad Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 30 Sep 2016 15:42:56 +0100
Subject: [PATCH] xen/trace: Fix trace metadata page count calculation (revert
fbf96e6)

Changeset fbf96e6, "xentrace: correct formula to calculate t_info_pages", broke the trace metadata page count calculation, by mistaking t_info_first_offset as denominated in bytes, when in fact it is denominated in words (uint32_t).

Effectively revert that change, and put a comment there to reduce the chance that someone will make that mistake in the future.

---
CC: Olaf Hering <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Jan Beulich <[email protected]>

---
 xen/common/trace.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/common/trace.c +6 -2
index f651cf3..2f4ecca 100644
@@ -148,8 +148,12 @@ static int calculate_tbuf_size(unsigned int pages, uint16_t t_info_first_offset)
         pages = max_pages;
     }
 
-    t_info_words = num_online_cpus() * pages * sizeof(uint32_t);
-    t_info_pages = PFN_UP(t_info_first_offset + t_info_words);
+    /* 
+     * NB this calculation is correct, because t_info_first_offset is
+     * in words, not bytes, not bytes
+     */
+    t_info_words = num_online_cpus() * pages + t_info_first_offset;
+    t_info_pages = PFN_UP(t_info_words * sizeof(uint32_t));
     printk(XENLOG_INFO "xentrace: requesting %u t_info pages "
            "for %u trace pages on %u cpus\n",
            t_info_pages, pages, num_online_cpus());
Signature · 1 line
2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 52 lines · George Dunlap
Quoted 6 lines · Igor Druzhinin
Quoted 6 lines

As long as t_info_first_offset is calculated in uint32_t offsets we need to multiply it by sizeof(uint32_t) in order to get the right number of pages for trace metadata. Not doing that makes it impossible to read the trace buffer correctly from userspace for some corner cases.

Hmm, looks like we actually want to revert this c/s fbf96e6, "xentrace:
correct formula to calculate t_info_pages". But that one was presumably written (and Acked by me) because the variable name there, t_info_first_offset, is confusing.

The other mistake in fbf96e6 is that before t_info_words was actually denominated in words; but after it's denominated in bytes (which is again confusing).

What about something like the attached instead? This should fix your problem while making the code clearer.

-George

From b0252cec4a96fea28faa85c47ab0f5d5997444ad Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 30 Sep 2016 15:42:56 +0100
Subject: [PATCH] xen/trace: Fix trace metadata page count calculation (revert
fbf96e6)

Changeset fbf96e6, "xentrace: correct formula to calculate t_info_pages", broke the trace metadata page count calculation, by mistaking t_info_first_offset as denominated in bytes, when in fact it is denominated in words (uint32_t).

Effectively revert that change, and put a comment there to reduce the chance that someone will make that mistake in the future.

---
CC: Olaf Hering <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Jan Beulich <[email protected]>

---
 xen/common/trace.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/common/trace.c +0 -0
index f651cf3..2f4ecca 100644

xen/common/trace.c static int calculate_tbuf_size(unsigned int pages, uint16_t t_info_first_offset) · line 153

Quoted 8 lines
xen/common/trace.c +3 -2
@@ -148,8 +148,12 @@ static int calculate_tbuf_size(unsigned int pages, uint16_t t_info_first_offset)
         pages = max_pages;
     }
 
-    t_info_words = num_online_cpus() * pages * sizeof(uint32_t);
-    t_info_pages = PFN_UP(t_info_first_offset + t_info_words);
+    /* 
+     * NB this calculation is correct, because t_info_first_offset is
+     * in words, not bytes, not bytes

s/, not bytes$/./

~Andrew

xen/common/trace.c static int calculate_tbuf_size(unsigned int pages, uint16_t t_info_first_offset)

Quoted 7 lines
xen/common/trace.c +3 -1
+     */
+    t_info_words = num_online_cpus() * pages + t_info_first_offset;
+    t_info_pages = PFN_UP(t_info_words * sizeof(uint32_t));
     printk(XENLOG_INFO "xentrace: requesting %u t_info pages "
            "for %u trace pages on %u cpus\n",
            t_info_pages, pages, num_online_cpus());
-- 2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
In reply to George Dunlap
Quoted 13 lines · George Dunlap
Quoted 6 lines · Igor Druzhinin

As long as t_info_first_offset is calculated in uint32_t offsets we need to multiply it by sizeof(uint32_t) in order to get the right number of pages for trace metadata. Not doing that makes it impossible to read the trace buffer correctly from userspace for some corner cases.

Hmm, looks like we actually want to revert this c/s fbf96e6, "xentrace:
correct formula to calculate t_info_pages". But that one was presumably written (and Acked by me) because the variable name there, t_info_first_offset, is confusing.

Quoted 6 lines

The other mistake in fbf96e6 is that before t_info_words was actually denominated in words; but after it's denominated in bytes (which is again confusing).

What about something like the attached instead? This should fix your problem while making the code clearer.

Obviously that comment shouldn't include the second "not bytes". We're not writing folk songs here...

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author Reviewed-by
#4 lore raw
In reply to George Dunlap
Quoted 13 lines · George Dunlap
Quoted 6 lines · Igor Druzhinin

As long as t_info_first_offset is calculated in uint32_t offsets we need to multiply it by sizeof(uint32_t) in order to get the right number of pages for trace metadata. Not doing that makes it impossible to read the trace buffer correctly from userspace for some corner cases.

Hmm, looks like we actually want to revert this c/s fbf96e6, "xentrace:
correct formula to calculate t_info_pages". But that one was presumably written (and Acked by me) because the variable name there, t_info_first_offset, is confusing.

Quoted 10 lines

The other mistake in fbf96e6 is that before t_info_words was actually denominated in words; but after it's denominated in bytes (which is again confusing).

What about something like the attached instead? This should fix your problem while making the code clearer.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw
Quoted 20 lines · Igor Druzhinin
Quoted 19 lines · George Dunlap
Quoted 8 lines · Igor Druzhinin

As long as t_info_first_offset is calculated in uint32_t offsets we need to
multiply it by sizeof(uint32_t) in order to get the right number of pages
for trace metadata. Not doing that makes it impossible to read the trace buffer correctly from userspace for some corner cases.

Hmm, looks like we actually want to revert this c/s fbf96e6, "xentrace:
correct formula to calculate t_info_pages". But that one was presumably written (and Acked by me) because the variable name there, t_info_first_offset, is confusing.

The other mistake in fbf96e6 is that before t_info_words was actually denominated in words; but after it's denominated in bytes (which is again confusing).

Quoted 8 lines
Quoted 6 lines

What about something like the attached instead? This should fix your problem while making the code clearer.

-George

Thanks. Any chance I could get a Tested-by as well? :-)

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author Tested-by
#6 lore raw

Checked that.

Quoted 28 lines · George Dunlap
Quoted 27 lines · Igor Druzhinin
Quoted 25 lines · George Dunlap
Quoted 8 lines · Igor Druzhinin

As long as t_info_first_offset is calculated in uint32_t offsets we need to
multiply it by sizeof(uint32_t) in order to get the right number of pages
for trace metadata. Not doing that makes it impossible to read the trace buffer correctly from userspace for some corner cases.

Hmm, looks like we actually want to revert this c/s fbf96e6, "xentrace:
correct formula to calculate t_info_pages". But that one was presumably written (and Acked by me) because the variable name there, t_info_first_offset, is confusing.

The other mistake in fbf96e6 is that before t_info_words was actually denominated in words; but after it's denominated in bytes (which is again confusing).

What about something like the attached instead? This should fix your problem while making the code clearer.

-George

Quoted 6 lines
Quoted 1 line

Thanks. Any chance I could get a Tested-by as well? :-)

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel