zlogger: switch from kzalloc to __get_free_pages 50/280550/2
authorMateusz Majewski <m.majewski2@samsung.com>
Wed, 31 Aug 2022 11:18:51 +0000 (13:18 +0200)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Fri, 2 Sep 2022 00:52:40 +0000 (00:52 +0000)
Since we want to mmap the memory in userspace, it's important for the
memory to be consisting of full pages. Even though this has been true in
our tests, kzalloc does not guarantee this and there is a danger of this
changing in the future kernel releases. Instead, we can use
__get_free_pages, which explicitly returns a number of contiguous pages.

Change-Id: I4db57e37fbbcd17716718ed81ef2a6b79e4b1afc

kernel/zlogger/zlogger.c

index 796836dc676638c7e6ff73970450f7aaafb4bcd7..ea6c99b4dd3a9116237a4f7882b7b2f4050927d6 100644 (file)
@@ -46,6 +46,7 @@
 #define ZLOGGER_SMACK_LABEL "*"
 
 #define BLOCK_RATIO(count) (count*100/ZLOGGER_BLOCK_COUNT)
+#define MAP_ORDER (get_order(ZLOGGER_MAP_SIZE))
 
 #define ZLOG_FD_BUFER (2 * ZLOGGER_MB)
 
@@ -786,7 +787,7 @@ static int zlogger_init(void)
        hash_init(g_thread_table->data);
 
        for (g_shm_ptr_i = 0; g_shm_ptr_i < ZLOGGER_DEVICE_COUNT; g_shm_ptr_i++) {
-               g_shm_ptr[g_shm_ptr_i] = kzalloc(ZLOGGER_MAP_SIZE, GFP_KERNEL);
+               g_shm_ptr[g_shm_ptr_i] = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, MAP_ORDER);
                if (g_shm_ptr[g_shm_ptr_i] == NULL) {
                        r = -ENOMEM;
                        goto out_free_g_thread_table_g_shm_ptr;
@@ -841,7 +842,7 @@ out_free_zlog_task:
 
 out_free_g_thread_table_g_shm_ptr:
        for (i = 0; i < g_shm_ptr_i; ++i) {
-               kfree(g_shm_ptr[i]);
+               free_pages((unsigned long)g_shm_ptr[i], MAP_ORDER);
                g_shm_ptr[i] = NULL;
        }
 
@@ -872,7 +873,7 @@ static void zlogger_exit(void)
        g_thread_table = NULL;
 
        for (i = 0; i < ZLOGGER_DEVICE_COUNT; i++) {
-               kfree(g_shm_ptr[i]);
+               free_pages((unsigned long)g_shm_ptr[i], MAP_ORDER);
                g_shm_ptr[i] = NULL;
        }