xentrace: simplify map_page function in xenctx
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.
--- tools/xentrace/xenctx.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
tools/xentrace/xenctx.c +10 -11
@@ -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
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. Variableprevious_mfnwas 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
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. Variableprevious_mfnwas set but never used.
Quoted 8 lines
mapped and
previous_mfnare 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; andprevious_mfnis 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