Skip to contentSkip to conversation

xentrace: simplify map_page function in xenctx

PATCH In review Wei Liu 2 participants 3 messages Mar 3, 2016 – Mar 17, 2016

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH 1/1 xentrace: simplify map_page function in xenctx

There were several problems:

1. Variable mapped was set to NULL so the following two "if"s were useless.
2. Variable previous_mfn was set but never used.

Simplify the function by removing two "if"s and previous_mfn. Check if mfn == 0 instead. Finally remove the now unused out label.

Wrapped long lines while I was there.

---
Compile test only.

Cc George Dunlap <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Stefano Stabellini <[email protected]>
---
 tools/xentrace/xenctx.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
tools/xentrace/xenctx.c +10 -11
index e647179..ee6f800 100644
@@ -686,28 +686,27 @@ static void print_ctx(vcpu_guest_context_any_t *ctx_any)
 #ifndef NO_TRANSLATION
 static void *map_page(vcpu_guest_context_any_t *ctx, int vcpu, guest_word_t virt)
 {
-    static unsigned long previous_mfn = 0;
     static void *mapped = NULL;
-
-    unsigned long mfn = xc_translate_foreign_address(xenctx.xc_handle, xenctx.domid, vcpu, virt);
+    unsigned long mfn;
     unsigned long offset = virt & ~XC_PAGE_MASK;
 
-    if (mapped && mfn == previous_mfn)
-        goto out;
-
-    if (mapped)
-        munmap(mapped, XC_PAGE_SIZE);
 
-    previous_mfn = mfn;
+    mfn = xc_translate_foreign_address(xenctx.xc_handle, xenctx.domid,
+                                       vcpu, virt);
+    if (mfn == 0) {
+        fprintf(stderr, "\nfailed to translate "FMT_32B_WORD" into MFN.\n",
+                virt);
+        return NULL;
+    }
 
-    mapped = xc_map_foreign_range(xenctx.xc_handle, xenctx.domid, XC_PAGE_SIZE, PROT_READ, mfn);
+    mapped = xc_map_foreign_range(xenctx.xc_handle, xenctx.domid,
+                                  XC_PAGE_SIZE, PROT_READ, mfn);
 
     if (mapped == NULL) {
         fprintf(stderr, "\nfailed to map page for "FMT_32B_WORD".\n", virt);
         return NULL;
     }
 
- out:
     return (void *)(mapped + offset);
 }
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw
Quoted 5 lines · Wei Liu

There were several problems:

1. Variable mapped was set to NULL so the following two "if"s were useless.
2. Variable previous_mfn was set but never used.

mapped and previous_mfn are static, which (in this context) means they persists across invocations of the function. mapped is only set to NULL for the first call. Subsequent calls it's used to unmap the previous mapping; and previous_mfn is used to avoid unmap'ing and re-map'ing the same mfn.

This change will cause xenctx to leak virtual address space.

-George

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

author
#2 lore raw
Quoted 7 lines · George Dunlap
Quoted 5 lines · Wei Liu

There were several problems:

1. Variable mapped was set to NULL so the following two "if"s were useless.
2. Variable previous_mfn was set but never used.

Quoted 8 lines

mapped and previous_mfn are static, which (in this context) means they persists across invocations of the function. mapped is only set to NULL for the first call. Subsequent calls it's used to unmap the previous mapping; and previous_mfn is used to avoid unmap'ing and re-map'ing the same mfn.

This change will cause xenctx to leak virtual address space.

Right. I missed that static, sigh. Ignore this patch please.

Wei.

Quoted 2 lines

-George

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