Skip to contentSkip to conversation

xen/xenoprof: avoid division by 0

PATCH In review Tim Deegan 3 participants 7 messages Feb 14, 2013 – Feb 15, 2013

lore
Share & tools
lore

Tim Deegan

author
#0 lore raw

PATCH 1/1 xen/xenoprof: avoid division by 0

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1360858577 0
# Node ID 68308aac7872c07631cb8424367907a11811ec3d # Parent 5a84cc531072378e6e5ff89b4c0e9a35000dc56f xen/xenoprof: avoid division by 0.

xen/common/xenoprof.c +1 -1
diff -r 5a84cc531072 -r 68308aac7872 xen/common/xenoprof.c
@@ -225,7 +225,7 @@ static int alloc_xenoprof_struct(
 #endif
 
     /* reduce max_samples if necessary to limit pages allocated */
-    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / nvcpu;
+    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / (nvcpu ?: 1);
     max_max_samples = ( (max_bufsize - bufsize) / i ) + 1;
     if ( (unsigned)max_samples > max_max_samples )
         max_samples = max_max_samples;
#1 lore raw

xen/common/xenoprof.c static int alloc_xenoprof_struct( · line 231

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
xen/common/xenoprof.c +1 -1
@@ -225,7 +225,7 @@ static int alloc_xenoprof_struct(
 #endif
 
     /* reduce max_samples if necessary to limit pages allocated */
-    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / nvcpu;
+    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / (nvcpu ?: 1);
     max_max_samples = ( (max_bufsize - bufsize) / i ) + 1;
     if ( (unsigned)max_samples > max_max_samples )
         max_samples = max_max_samples;

I think the function would better return an error in that case. After all there's little point in setting up anything when we for sure don't know how many vCPU-s a domain is going to have.

Jan

author
#2 lore raw
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
xen/common/xenoprof.c +0 -0
@@ -225,7 +225,7 @@ static int alloc_xenoprof_struct(
 #endif
 
Quoted 10 lines

xen/common/xenoprof.c static int alloc_xenoprof_struct( · line 231

Quoted 6 lines
xen/common/xenoprof.c +1 -1
     /* reduce max_samples if necessary to limit pages allocated */
-    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / nvcpu;
+    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / (nvcpu ?: 1);
     max_max_samples = ( (max_bufsize - bufsize) / i ) + 1;
     if ( (unsigned)max_samples > max_max_samples )
         max_samples = max_max_samples;

I think the function would better return an error in that case. After all there's little point in setting up anything when we for sure don't know how many vCPU-s a domain is going to have.

Grand so:

# HG changeset patch
# Parent 5a84cc531072378e6e5ff89b4c0e9a35000dc56f
xen/xenoprof: avoid division by 0.

xen/common/xenoprof.c +3 -0
diff -r 5a84cc531072 xen/common/xenoprof.c
@@ -213,6 +213,9 @@ static int alloc_xenoprof_struct(
     for_each_vcpu ( d, v )
         nvcpu++;
 
+    if ( !nvcpu )
+        return -EINVAL;
+
     bufsize = sizeof(struct xenoprof_buf);
     i = sizeof(struct event_log);
 #ifdef CONFIG_COMPAT
#3 lore raw
Quoted 31 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
xen/common/xenoprof.c +0 -0
@@ -225,7 +225,7 @@ static int alloc_xenoprof_struct(
 #endif
 
Quoted 10 lines

xen/common/xenoprof.c static int alloc_xenoprof_struct( · line 231

Quoted 6 lines
xen/common/xenoprof.c +1 -1
     /* reduce max_samples if necessary to limit pages allocated */
-    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / nvcpu;
+    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / (nvcpu ?: 1);
     max_max_samples = ( (max_bufsize - bufsize) / i ) + 1;
     if ( (unsigned)max_samples > max_max_samples )
         max_samples = max_max_samples;

I think the function would better return an error in that case. After all there's little point in setting up anything when we for sure don't know how many vCPU-s a domain is going to have.

Grand so:

# HG changeset patch
# Parent 5a84cc531072378e6e5ff89b4c0e9a35000dc56f
xen/xenoprof: avoid division by 0.

diff -r 5a84cc531072 xen/common/xenoprof.c

xen/common/xenoprof.c static int alloc_xenoprof_struct( · line 217

Quoted 5 lines
xen/common/xenoprof.c +2 -0
@@ -213,6 +213,9 @@ static int alloc_xenoprof_struct(
     for_each_vcpu ( d, v )
         nvcpu++;
 
+    if ( !nvcpu )
+        return -EINVAL;

Missing some cleanup here? Or move the preceding loop and this addition to the top of the function?

Jan

Quoted 4 lines
xen/common/xenoprof.c +1 -0
+
     bufsize = sizeof(struct xenoprof_buf);
     i = sizeof(struct event_log);
 #ifdef CONFIG_COMPAT
author
#4 lore raw
Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
xen/common/xenoprof.c +0 -0
@@ -225,7 +225,7 @@ static int alloc_xenoprof_struct(
 #endif
 
Quoted 10 lines

xen/common/xenoprof.c static int alloc_xenoprof_struct( · line 231

Quoted 6 lines
xen/common/xenoprof.c +1 -1
     /* reduce max_samples if necessary to limit pages allocated */
-    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / nvcpu;
+    max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / (nvcpu ?: 1);
     max_max_samples = ( (max_bufsize - bufsize) / i ) + 1;
     if ( (unsigned)max_samples > max_max_samples )
         max_samples = max_max_samples;

I think the function would better return an error in that case. After all there's little point in setting up anything when we for sure don't know how many vCPU-s a domain is going to have.

Grand so:

# HG changeset patch
# Parent 5a84cc531072378e6e5ff89b4c0e9a35000dc56f
xen/xenoprof: avoid division by 0.

diff -r 5a84cc531072 xen/common/xenoprof.c

xen/common/xenoprof.c +0 -0
@@ -213,6 +213,9 @@ static int alloc_xenoprof_struct(
     for_each_vcpu ( d, v )
         nvcpu++;
 
Quoted 5 lines

xen/common/xenoprof.c static int alloc_xenoprof_struct( · line 217

Quoted 2 lines
xen/common/xenoprof.c +2 -0
+    if ( !nvcpu )
+        return -EINVAL;

Missing some cleanup here? Or move the preceding loop and this addition to the top of the function?

D'oh, yes. Trying again:

# HG changeset patch
# Parent 5a84cc531072378e6e5ff89b4c0e9a35000dc56f
xen/xenoprof: avoid division by 0.

xen/common/xenoprof.c +7 -4
diff -r 5a84cc531072 xen/common/xenoprof.c
@@ -193,6 +193,13 @@ static int alloc_xenoprof_struct(
     unsigned max_max_samples;
     int i;
 
+    nvcpu = 0;
+    for_each_vcpu ( d, v )
+        nvcpu++;
+
+    if ( !nvcpu )
+        return -EINVAL;
+
     d->xenoprof = xzalloc(struct xenoprof);
     if ( d->xenoprof == NULL )
     {
@@ -209,10 +216,6 @@ static int alloc_xenoprof_struct(
         return -ENOMEM;
     }
 
-    nvcpu = 0;
-    for_each_vcpu ( d, v )
-        nvcpu++;
-
     bufsize = sizeof(struct xenoprof_buf);
     i = sizeof(struct event_log);
 #ifdef CONFIG_COMPAT
Acked-by
#5 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Tim Deegan

# HG changeset patch
# Parent 5a84cc531072378e6e5ff89b4c0e9a35000dc56f
xen/xenoprof: avoid division by 0.

Quoted 28 lines

diff -r 5a84cc531072 xen/common/xenoprof.c

xen/common/xenoprof.c +7 -4
@@ -193,6 +193,13 @@ static int alloc_xenoprof_struct(
     unsigned max_max_samples;
     int i;
 
+    nvcpu = 0;
+    for_each_vcpu ( d, v )
+        nvcpu++;
+
+    if ( !nvcpu )
+        return -EINVAL;
+
     d->xenoprof = xzalloc(struct xenoprof);
     if ( d->xenoprof == NULL )
     {
@@ -209,10 +216,6 @@ static int alloc_xenoprof_struct(
         return -ENOMEM;
     }
 
-    nvcpu = 0;
-    for_each_vcpu ( d, v )
-        nvcpu++;
-
     bufsize = sizeof(struct xenoprof_buf);
     i = sizeof(struct event_log);
 #ifdef CONFIG_COMPAT
Acked-by
#6 lore raw
Quoted 8 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Tim Deegan

# HG changeset patch
# Parent 5a84cc531072378e6e5ff89b4c0e9a35000dc56f
xen/xenoprof: avoid division by 0.

Quoted 36 lines
Quoted 18 lines

diff -r 5a84cc531072 xen/common/xenoprof.c

xen/common/xenoprof.c Thu Feb 14 17:07:41 2013 +0000 renamed from xen/common/xenoprof.c Thu Feb 14 15:46:56 2013 +0000 +7 -0
@@ -193,6 +193,13 @@ static int alloc_xenoprof_struct(
     unsigned max_max_samples;
     int i;
 
+    nvcpu = 0;
+    for_each_vcpu ( d, v )
+        nvcpu++;
+
+    if ( !nvcpu )
+        return -EINVAL;
+
     d->xenoprof = xzalloc(struct xenoprof);
     if ( d->xenoprof == NULL )
     {

xen/common/xenoprof.c Thu Feb 14 17:07:41 2013 +0000 static int alloc_xenoprof_struct( · line 221

Quoted 10 lines
xen/common/xenoprof.c Thu Feb 14 17:07:41 2013 +0000 renamed from xen/common/xenoprof.c Thu Feb 14 15:46:56 2013 +0000 +0 -4
@@ -209,10 +216,6 @@ static int alloc_xenoprof_struct(
         return -ENOMEM;
     }
 
-    nvcpu = 0;
-    for_each_vcpu ( d, v )
-        nvcpu++;
-
     bufsize = sizeof(struct xenoprof_buf);
     i = sizeof(struct event_log);
 #ifdef CONFIG_COMPAT

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