[PATCH v2 2/6] core: Replace PA_PAGE_SIZE with pa_page_size()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PA_PAGE_SIZE using sysconf() may return a negative number

CID 1137925, CID 1137926, CID 1138485

instead of calling sysconf() directly, add function pa_page_size()
which uses the guestimate 4096 in case sysconf(_SC_PAGE_SIZE) fails

using PA_ONCE to only evaluate sysconf() once
---
 src/daemon/main.c           |    2 +-
 src/pulsecore/core-util.c   |   26 ++++++++++++++++++++++++--
 src/pulsecore/core-util.h   |   13 +++++++++++++
 src/pulsecore/macro.h       |   21 ---------------------
 src/pulsecore/memblock.c    |    5 +++--
 src/pulsecore/sample-util.c |    2 +-
 src/pulsecore/shm.c         |    7 ++++---
 src/pulsecore/sink-input.c  |    2 +-
 src/pulsecore/sink.c        |    2 +-
 src/tests/sigbus-test.c     |   13 +++++++------
 10 files changed, 55 insertions(+), 38 deletions(-)

diff --git a/src/daemon/main.c b/src/daemon/main.c
index a313dc5..dc5e5bc 100644
--- a/src/daemon/main.c
+++ b/src/daemon/main.c
@@ -918,7 +918,7 @@ int main(int argc, char *argv[]) {
 
     pa_log_debug("Found %u CPUs.", pa_ncpus());
 
-    pa_log_info("Page size is %lu bytes", (unsigned long) PA_PAGE_SIZE);
+    pa_log_info("Page size is %zu bytes", pa_page_size());
 
 #ifdef HAVE_VALGRIND_MEMCHECK_H
     pa_log_debug("Compiled with Valgrind support: yes");
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index da1b0c8..350f35b 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -138,6 +138,7 @@
 #include <pulsecore/strlist.h>
 #include <pulsecore/cpu-x86.h>
 #include <pulsecore/pipe.h>
+#include <pulsecore/once.h>
 
 #include "core-util.h"
 
@@ -2553,6 +2554,7 @@ void *pa_will_need(const void *p, size_t l) {
     size_t size;
     int r = ENOTSUP;
     size_t bs;
+    const size_t page_size = pa_page_size();
 
     pa_assert(p);
     pa_assert(l > 0);
@@ -2577,7 +2579,7 @@ void *pa_will_need(const void *p, size_t l) {
 #ifdef RLIMIT_MEMLOCK
     pa_assert_se(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
 
-    if (rlim.rlim_cur < PA_PAGE_SIZE) {
+    if (rlim.rlim_cur < page_size) {
         pa_log_debug("posix_madvise() failed (or doesn't exist), resource limits don't allow mlock(), can't page in data: %s", pa_cstrerror(r));
         errno = EPERM;
         return (void*) p;
@@ -2585,7 +2587,7 @@ void *pa_will_need(const void *p, size_t l) {
 
     bs = PA_PAGE_ALIGN((size_t) rlim.rlim_cur);
 #else
-    bs = PA_PAGE_SIZE*4;
+    bs = page_size*4;
 #endif
 
     pa_log_debug("posix_madvise() failed (or doesn't exist), trying mlock(): %s", pa_cstrerror(r));
@@ -3669,3 +3671,23 @@ bool pa_running_in_vm(void) {
 
     return false;
 }
+
+size_t pa_page_size(void) {
+#if defined(PAGE_SIZE)
+    return PAGE_SIZE;
+#elif defined(PAGESIZE)
+    return PAGESIZE;
+#elif defined(HAVE_SYSCONF)
+    static size_t page_size = 4096; /* Let's hope it's like x86. */
+
+    PA_ONCE_BEGIN {
+        long ret = sysconf(_SC_PAGE_SIZE);
+        if (ret > 0)
+            page_size = ret;
+    } PA_ONCE_END;
+
+    return page_size;
+#else
+    return 4096;
+#endif
+}
diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h
index 5725ca7..ede1ae2 100644
--- a/src/pulsecore/core-util.h
+++ b/src/pulsecore/core-util.h
@@ -302,4 +302,17 @@ bool pa_running_in_vm(void);
 char *pa_win32_get_toplevel(HANDLE handle);
 #endif
 
+size_t pa_page_size(void);
+
+/* Rounds down */
+static inline void* PA_PAGE_ALIGN_PTR(const void *p) {
+    return (void*) (((size_t) p) & ~(pa_page_size() - 1));
+}
+
+/* Rounds up */
+static inline size_t PA_PAGE_ALIGN(size_t l) {
+    size_t page_size = pa_page_size();
+    return (l + page_size - 1) & ~(page_size - 1);
+}
+
 #endif
diff --git a/src/pulsecore/macro.h b/src/pulsecore/macro.h
index f80b43c..2c5d5f2 100644
--- a/src/pulsecore/macro.h
+++ b/src/pulsecore/macro.h
@@ -44,17 +44,6 @@
 #endif
 #endif
 
-#if defined(PAGE_SIZE)
-#define PA_PAGE_SIZE ((size_t) PAGE_SIZE)
-#elif defined(PAGESIZE)
-#define PA_PAGE_SIZE ((size_t) PAGESIZE)
-#elif defined(HAVE_SYSCONF)
-#define PA_PAGE_SIZE ((size_t) (sysconf(_SC_PAGE_SIZE)))
-#else
-/* Let's hope it's like x86. */
-#define PA_PAGE_SIZE ((size_t) 4096)
-#endif
-
 /* Rounds down */
 static inline void* PA_ALIGN_PTR(const void *p) {
     return (void*) (((size_t) p) & ~(sizeof(void*) - 1));
@@ -65,16 +54,6 @@ static inline size_t PA_ALIGN(size_t l) {
     return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
 }
 
-/* Rounds down */
-static inline void* PA_PAGE_ALIGN_PTR(const void *p) {
-    return (void*) (((size_t) p) & ~(PA_PAGE_SIZE - 1));
-}
-
-/* Rounds up */
-static inline size_t PA_PAGE_ALIGN(size_t l) {
-    return (l + PA_PAGE_SIZE - 1) & ~(PA_PAGE_SIZE - 1);
-}
-
 #if defined(__GNUC__)
     #define PA_UNUSED __attribute__ ((unused))
 #else
diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index 17520ed..dbad32a 100644
--- a/src/pulsecore/memblock.c
+++ b/src/pulsecore/memblock.c
@@ -828,13 +828,14 @@ static void memblock_replace_import(pa_memblock *b) {
 pa_mempool *pa_mempool_new(pa_mem_type_t type, size_t size, bool per_client) {
     pa_mempool *p;
     char t1[PA_BYTES_SNPRINT_MAX], t2[PA_BYTES_SNPRINT_MAX];
+    const size_t page_size = pa_page_size();
 
     p = pa_xnew0(pa_mempool, 1);
     PA_REFCNT_INIT(p);
 
     p->block_size = PA_PAGE_ALIGN(PA_MEMPOOL_SLOT_SIZE);
-    if (p->block_size < PA_PAGE_SIZE)
-        p->block_size = PA_PAGE_SIZE;
+    if (p->block_size < page_size)
+        p->block_size = page_size;
 
     if (size <= 0)
         p->n_blocks = PA_MEMPOOL_SLOTS_MAX;
diff --git a/src/pulsecore/sample-util.c b/src/pulsecore/sample-util.c
index 8ae5db9..b2a28eb 100644
--- a/src/pulsecore/sample-util.c
+++ b/src/pulsecore/sample-util.c
@@ -39,7 +39,7 @@
 
 #include "sample-util.h"
 
-#define PA_SILENCE_MAX (PA_PAGE_SIZE*16)
+#define PA_SILENCE_MAX (pa_page_size()*16)
 
 pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) {
     void *data;
diff --git a/src/pulsecore/shm.c b/src/pulsecore/shm.c
index 1c5eb44..919d71a 100644
--- a/src/pulsecore/shm.c
+++ b/src/pulsecore/shm.c
@@ -126,7 +126,7 @@ static int privatemem_create(pa_shm *m, size_t size) {
     {
         int r;
 
-        if ((r = posix_memalign(&m->ptr, PA_PAGE_SIZE, size)) < 0) {
+        if ((r = posix_memalign(&m->ptr, pa_page_size(), size)) < 0) {
             pa_log("posix_memalign() failed: %s", pa_cstrerror(r));
             return r;
         }
@@ -300,6 +300,7 @@ finish:
 void pa_shm_punch(pa_shm *m, size_t offset, size_t size) {
     void *ptr;
     size_t o;
+    const size_t page_size = pa_page_size();
 
     pa_assert(m);
     pa_assert(m->ptr);
@@ -318,13 +319,13 @@ void pa_shm_punch(pa_shm *m, size_t offset, size_t size) {
     o = (size_t) ((uint8_t*) ptr - (uint8_t*) PA_PAGE_ALIGN_PTR(ptr));
 
     if (o > 0) {
-        size_t delta = PA_PAGE_SIZE - o;
+        size_t delta = page_size - o;
         ptr = (uint8_t*) ptr + delta;
         size -= delta;
     }
 
     /* Align the size down to multiples of page size */
-    size = (size / PA_PAGE_SIZE) * PA_PAGE_SIZE;
+    size = (size / page_size) * page_size;
 
 #ifdef MADV_REMOVE
     if (madvise(ptr, size, MADV_REMOVE) >= 0)
diff --git a/src/pulsecore/sink-input.c b/src/pulsecore/sink-input.c
index 435e63e..0dda204 100644
--- a/src/pulsecore/sink-input.c
+++ b/src/pulsecore/sink-input.c
@@ -44,7 +44,7 @@
 /* #define SINK_INPUT_DEBUG */
 
 #define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
-#define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
+#define CONVERT_BUFFER_LENGTH (pa_page_size())
 
 PA_DEFINE_PUBLIC_CLASS(pa_sink_input, pa_msgobject);
 
diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c
index a41a78a..5c6a9c6 100644
--- a/src/pulsecore/sink.c
+++ b/src/pulsecore/sink.c
@@ -50,7 +50,7 @@
 #include "sink.h"
 
 #define MAX_MIX_CHANNELS 32
-#define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
+#define MIX_BUFFER_LENGTH (pa_page_size())
 #define ABSOLUTE_MIN_LATENCY (500)
 #define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
 #define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC)
diff --git a/src/tests/sigbus-test.c b/src/tests/sigbus-test.c
index 8a660ee..dbfc3d1 100644
--- a/src/tests/sigbus-test.c
+++ b/src/tests/sigbus-test.c
@@ -33,6 +33,7 @@ START_TEST (sigbus_test) {
     void *p;
     int fd;
     pa_memtrap *m;
+    const size_t page_size = pa_page_size();
 
     pa_log_set_level(PA_LOG_DEBUG);
     pa_memtrap_install();
@@ -40,14 +41,14 @@ START_TEST (sigbus_test) {
     /* Create the memory map */
     fail_unless((fd = open("sigbus-test-map", O_RDWR|O_TRUNC|O_CREAT, 0660)) >= 0);
     fail_unless(unlink("sigbus-test-map") == 0);
-    fail_unless(ftruncate(fd, PA_PAGE_SIZE) >= 0);
-    fail_unless((p = mmap(NULL, PA_PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) != MAP_FAILED);
+    fail_unless(ftruncate(fd, page_size) >= 0);
+    fail_unless((p = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) != MAP_FAILED);
 
     /* Register memory map */
-    m = pa_memtrap_add(p, PA_PAGE_SIZE);
+    m = pa_memtrap_add(p, page_size);
 
     /* Use memory map */
-    pa_snprintf(p, PA_PAGE_SIZE, "This is a test that should work fine.");
+    pa_snprintf(p, page_size, "This is a test that should work fine.");
 
     /* Verify memory map */
     pa_log("Let's see if this worked: %s", (char*) p);
@@ -58,7 +59,7 @@ START_TEST (sigbus_test) {
     fail_unless(ftruncate(fd, 0) >= 0);
 
     /* Use memory map */
-    pa_snprintf(p, PA_PAGE_SIZE, "This is a test that should fail but get caught.");
+    pa_snprintf(p, page_size, "This is a test that should fail but get caught.");
 
     /* Verify memory map */
     pa_log("Let's see if this worked: %s", (char*) p);
@@ -66,7 +67,7 @@ START_TEST (sigbus_test) {
     fail_unless(pa_memtrap_is_good(m) == false);
 
     pa_memtrap_remove(m);
-    munmap(p, PA_PAGE_SIZE);
+    munmap(p, page_size);
     close(fd);
 }
 END_TEST
-- 
1.7.10.4



[Index of Archives]     [Linux Audio Users]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux