Skip to contentSkip to conversation

Fix check of XENOPROF_get_buffer and XENOPROF_set_passive argument max_samples

PATCH New Markus Armbruster 1 participant 1 message Oct 14, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix check of XENOPROF_get_buffer and XENOPROF_set_passive argument max_samples

Fix code limiting XENOPROF_get_buffer and XENOPROF_set_passive argument max_samples so that no more than MAX_OPROF_SHARED_PAGES are used.

---
A few quick tests comparing old to new code:

old code new code
arg checked npages checked npages
0 0 1 0 1
1 1 1 1 1
5457 5457 32 5457 32
5458 5457 32 5457 32
2000000000 2000000000 184415 5457 32
2147483647 2147483647 1 5457 32
-1 -1 1 5457 32

xen/arch/x86/oprofile/xenoprof.c +8 -9
diff -r efea4c3b8bcc xen/arch/x86/oprofile/xenoprof.c
@@ -122,6 +122,7 @@ int alloc_xenoprof_struct(struct domain 
 {
     struct vcpu *v;
     int nvcpu, npages, bufsize, max_bufsize;
+    unsigned max_max_samples;
     int i;
 
     d->xenoprof = xmalloc(struct xenoprof);
@@ -139,17 +140,15 @@ int alloc_xenoprof_struct(struct domain 
     for_each_vcpu ( d, v )
         nvcpu++;
 
-    /* reduce buffer size if necessary to limit pages allocated */
+    /* reduce max_samples if necessary to limit pages allocated */
+    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / nvcpu;
+    max_max_samples = ( (max_bufsize - sizeof(struct xenoprof_buf)) /
+                        sizeof(struct event_log) ) + 1;
+    if ( (unsigned)max_samples > max_max_samples )
+        max_samples = max_max_samples;
+
     bufsize = sizeof(struct xenoprof_buf) +
         (max_samples - 1) * sizeof(struct event_log);
-    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / nvcpu;
-    if ( bufsize > max_bufsize )
-    {
-        bufsize = max_bufsize;
-        max_samples = ( (max_bufsize - sizeof(struct xenoprof_buf)) /
-                        sizeof(struct event_log) ) + 1;
-    }
-
     npages = (nvcpu * bufsize - 1) / PAGE_SIZE + 1;
     
     d->xenoprof->rawbuf = alloc_xenoprof_buf(is_passive ? dom0 : d, npages);