zlogger: move userspace-shared definitions to UAPI 05/280405/4
authorMateusz Majewski <m.majewski2@samsung.com>
Mon, 29 Aug 2022 12:36:06 +0000 (14:36 +0200)
committerArkadiusz Nowak <a.nowak3@samsung.com>
Tue, 30 Aug 2022 11:51:39 +0000 (13:51 +0200)
This commit also changes types to the UAPI-friendly ones, and names not
to litter the global namespace too much. Additionally, pid_t occurrences
are changed into __s32, as we don't want to have pid_t in kernel ABI.

Change-Id: I7658f0874e07bfb63384a401efe8f3427c7d0af3
Signed-off-by: Mateusz Majewski <m.majewski2@samsung.com>
include/uapi/linux/zlogger.h
kernel/zlogger/zlogger.c
tests/zlog_stdout/zlog_stdout.c

index 9cd11c5..68bcd38 100644 (file)
@@ -1,8 +1,43 @@
 #ifndef _ZLOGGER_H
 #define _ZLOGGER_H
 
-#define IOCTL_COMMAND_ALLOC (20745321)
-#define SET_DEFAULT_PRIORITY _IOW('a','a', char*)
-#define SET_DEFAULT_TAG _IOW('a','b', char*)
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+#define ZLOGGER_MB (1 << 20)
+#define ZLOGGER_KB (1 << 10)
+
+#define ZLOGGER_DEVICE_COUNT (8)
+#define ZLOGGER_MAP_SIZE (4 * ZLOGGER_MB)
+#define ZLOGGER_BUFFER_SIZE (ZLOGGER_DEVICE_COUNT * ZLOGGER_MAP_SIZE)
+#define ZLOGGER_BLOCK_SIZE (2 * ZLOGGER_KB)
+#define ZLOGGER_BLOCK_COUNT (ZLOGGER_BUFFER_SIZE / ZLOGGER_BLOCK_SIZE)
+#define ZLOGGER_DATA_MAX (ZLOGGER_BLOCK_SIZE - sizeof(struct zlogger_header))
+
+#define ZLOGGER_TAG_MAX (32)
+#define ZLOGGER_MSG_MAX (140)
+
+#define ZLOGGER_IOCTL_COMMAND_ALLOC (20745321)
+#define ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY _IOW('a','a', char*)
+#define ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_TAG _IOW('a','b', char*)
+
+struct zlogger_entry {
+       __u64 time;
+       __u16 CRC;
+       __u16 len;
+       char data[];
+};
+
+struct zlogger_header {
+       __u64 ts;
+       __s32 pid;
+       __s32 tid;
+       __u16 offset;
+};
+
+struct zlogger_block {
+       struct zlogger_header head;
+       char data[ZLOGGER_DATA_MAX];
+};
 
 #endif
index 2ccc100..e87814d 100644 (file)
 #include <linux/compat.h>
 #include <uapi/linux/zlogger.h>
 
-#define MB (1 << 20)
-#define KB (1 << 10)
-
-#define DEVICE_COUNT (8)
-#define MAP_SIZE (4 * MB)
-#define BUFFER_SIZE (DEVICE_COUNT * MAP_SIZE)
-#define V_BLOCK_SIZE (2 * KB)
-#define BLOCK_COUNT (BUFFER_SIZE / V_BLOCK_SIZE)
-#define DATA_MAX (V_BLOCK_SIZE - sizeof(struct head_t))
 #define MS_PER_SEC (1000)
 #define NS_PER_SEC (1000000000UL)
 
 #define ZLOGGER_DEVICE_NAME "zlogger"
 #define ZLOGGER_SMACK_LABEL "*"
 
-#define MAX_TAG_SIZE (32)
-#define MAX_MSG_SIZE (140)
+#define BLOCK_RATIO(count) (count*100/ZLOGGER_BLOCK_COUNT)
 
-#define BLOCK_RATIO(count) (count*100/BLOCK_COUNT)
-
-#define ZLOG_FD_BUFER (2 * MB)
+#define ZLOG_FD_BUFER (2 * ZLOGGER_MB)
 
 struct thread_t {
        uint16_t block;
 };
 
-struct entry_t {
-       uint64_t time;
-       uint16_t CRC;
-       uint16_t len;
-       char data[0];
-};
-
-struct head_t {
-       uint64_t ts;
-       pid_t pid;
-       pid_t tid;
-       uint16_t offset;
-};
-
-struct block_t {
-       struct head_t head;
-       char data[DATA_MAX];
-};
-
 struct queue_t {
        char name[5];
        uint16_t front;
@@ -124,8 +93,8 @@ typedef enum {
 const char default_tag[] = "STDOUT";
 typedef struct {
        char prio;
-       char tag[MAX_TAG_SIZE];
-       char msg[MAX_MSG_SIZE];
+       char tag[ZLOGGER_TAG_MAX];
+       char msg[ZLOGGER_MSG_MAX];
        char *buffer;
        size_t buffer_len;
 } zlog_file_t;
@@ -135,7 +104,7 @@ typedef struct {
 static struct miscdevice zlogger_device;
 
 static int g_init;
-static char *g_shm_ptr[DEVICE_COUNT];
+static char *g_shm_ptr[ZLOGGER_DEVICE_COUNT];
 static struct thread_table *g_thread_table;
 
 static struct mutex g_block_mutex;
@@ -206,7 +175,7 @@ static void set_thread_table(pid_t tid, bool is_stdout, uint16_t blk)
 
 static inline char *get_shared_memory(int dev_index)
 {
-       if (dev_index < 0 || dev_index >= DEVICE_COUNT) {
+       if (dev_index < 0 || dev_index >= ZLOGGER_DEVICE_COUNT) {
                pr_debug("Invalid index: %d\n", dev_index);
                return NULL;
        }
@@ -214,16 +183,16 @@ static inline char *get_shared_memory(int dev_index)
        return g_shm_ptr[dev_index];
 }
 
-static inline struct block_t *get_block(uint16_t block_index)
+static inline struct zlogger_block *get_block(uint16_t block_index)
 {
        uint16_t index = block_index - 1;
-       int offset = index & (V_BLOCK_SIZE - 1);
-       char *p = get_shared_memory(index / V_BLOCK_SIZE);
+       int offset = index & (ZLOGGER_BLOCK_SIZE - 1);
+       char *p = get_shared_memory(index / ZLOGGER_BLOCK_SIZE);
 
        if (!p)
                return NULL;
 
-       return (struct block_t *)(p + (offset * V_BLOCK_SIZE));
+       return (struct zlogger_block *)(p + (offset * ZLOGGER_BLOCK_SIZE));
 }
 
 static inline void queue_init(struct queue_t *q, const char *name, uint16_t capacity)
@@ -339,7 +308,7 @@ static long alloc_block_for_thread(bool is_stdout)
        pid_t pid = current->tgid;
        pid_t tid = current->pid;
        uint16_t blk;
-       struct block_t *block;
+       struct zlogger_block *block;
 
        if (g_max_thread_id < tid)
                g_max_thread_id = tid;
@@ -373,7 +342,7 @@ static long alloc_block_for_thread(bool is_stdout)
        return (long)blk;
 }
 
-static inline struct block_t *get_valid_block(int tid, size_t len, bool is_stdout)
+static inline struct zlogger_block *get_valid_block(int tid, size_t len, bool is_stdout)
 {
        uint16_t blk = 0;
        long r;
@@ -381,12 +350,12 @@ static inline struct block_t *get_valid_block(int tid, size_t len, bool is_stdou
        blk = get_thread_table(tid, is_stdout);
 
        if (blk != 0) {
-               struct block_t *block = get_block(blk);
+               struct zlogger_block *block = get_block(blk);
 
                if (!block)
                        return NULL;
 
-               if (block->head.offset + len < DATA_MAX)
+               if (block->head.offset + len < ZLOGGER_DATA_MAX)
                        return block;
        }
 
@@ -423,14 +392,14 @@ static int zlogger_release(struct inode *ignored, struct file *file)
 
 static int zlogger_mmap(struct file *filep, struct vm_area_struct *vma)
 {
-       const int PAGES_PER_MAP = MAP_SIZE / PAGE_SIZE;
+       const int PAGES_PER_MAP = ZLOGGER_MAP_SIZE / PAGE_SIZE;
        int dev_index = (int)vma->vm_pgoff / PAGES_PER_MAP;
        unsigned long offset = vma->vm_pgoff % PAGES_PER_MAP;
        unsigned long size = vma->vm_end - vma->vm_start;
        char *p;
        struct page *page;
 
-       if (dev_index > DEVICE_COUNT || offset != 0 || size > MAP_SIZE) {
+       if (dev_index > ZLOGGER_DEVICE_COUNT || offset != 0 || size > ZLOGGER_MAP_SIZE) {
                pr_err("mmap failed: dev(%d) offset(%lu), size(%lu), pgoff(%lu)\n", dev_index, offset, size, vma->vm_pgoff);
                return -EINVAL;
        }
@@ -488,23 +457,23 @@ static int _zlog_write(const unsigned char prio, const char *tag, const char *ms
        uint64_t ts = ktime_get_ns();
        uint16_t *slt = (uint16_t *)&ts;
        const pid_t tid = current->pid;
-       size_t hd_size = sizeof(struct entry_t);
+       size_t hd_size = sizeof(struct zlogger_entry);
        size_t prio_size = 1;
-       size_t tag_size = strnlen(tag, MAX_TAG_SIZE) + 1;
-       size_t msg_size = strnlen(msg, MAX_MSG_SIZE) + 1;
+       size_t tag_size = strnlen(tag, ZLOGGER_TAG_MAX) + 1;
+       size_t msg_size = strnlen(msg, ZLOGGER_MSG_MAX) + 1;
        size_t entry_size = hd_size + prio_size + tag_size + msg_size;
-       struct block_t *block = get_valid_block(tid, entry_size, true);
-       struct entry_t *entry;
-       struct entry_t tmp;
+       struct zlogger_block *block = get_valid_block(tid, entry_size, true);
+       struct zlogger_entry *entry;
+       struct zlogger_entry tmp;
 
        if (block == NULL) {
                pr_err("_zlog_write: no memory\n");
                return -ENOMEM;
        }
 
-       entry = (struct entry_t *)(block->data + block->head.offset);
+       entry = (struct zlogger_entry *)(block->data + block->head.offset);
 
-       if ((char *)entry + entry_size > (char *)block + V_BLOCK_SIZE) {
+       if ((char *)entry + entry_size > (char *)block + ZLOGGER_BLOCK_SIZE) {
                // TODO: This is not unexpected: the offset can change at any time,
                // as it is userspace writable.
                pr_err("[%d] block:%p(tid:%d offset:%x) entry:%p(%zu)\n",
@@ -610,7 +579,7 @@ static long update_prio(zlog_file_t *zlog_file_data,  void __user *argp)
 
 static long zlogger_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
-       if (cmd == IOCTL_COMMAND_ALLOC)
+       if (cmd == ZLOGGER_IOCTL_COMMAND_ALLOC)
                return alloc_block_for_thread(false);
 
        #ifdef CONFIG_COMPAT
@@ -619,7 +588,7 @@ static long zlogger_ioctl(struct file *file, unsigned int cmd, unsigned long arg
         void __user *argp = (void __user *)arg;
     #endif
 
-       if (cmd == SET_DEFAULT_PRIORITY) {
+       if (cmd == ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY) {
                if( (!file->private_data ) && init_file_zlog_tag_data(file)) {
                        pr_err("zlogger_write init zlog tag data failed\n");
                        return -ENOMEM;
@@ -630,7 +599,7 @@ static long zlogger_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                return update_prio(zlog_file_data, argp);
        }
 
-       if(cmd == SET_DEFAULT_TAG) {
+       if(cmd == ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_TAG) {
                if( (!file->private_data ) && init_file_zlog_tag_data(file)) {
                        pr_err("zlogger_write init zlog tag data failed\n");
                        return -ENOMEM;
@@ -638,7 +607,7 @@ static long zlogger_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 
                zlog_file_t *zlog_file_data = (zlog_file_t *)file->private_data;
 
-               return copy_from_user(&zlog_file_data->tag, (char *)(uintptr_t)argp, strnlen((char *)(uintptr_t)argp, MAX_TAG_SIZE));
+               return copy_from_user(&zlog_file_data->tag, (char *)(uintptr_t)argp, strnlen((char *)(uintptr_t)argp, ZLOGGER_TAG_MAX));
        }
 
        return -EINVAL;
@@ -700,7 +669,7 @@ static ssize_t block_store(struct device *dev,
                return -EINVAL;
        }
 
-       if (value < 1 || value > BLOCK_COUNT)
+       if (value < 1 || value > ZLOGGER_BLOCK_COUNT)
                return -EINVAL;
 
        g_block = value;
@@ -711,14 +680,14 @@ static ssize_t block_store(struct device *dev,
 static ssize_t block_show(struct device *dev,
                struct device_attribute *attr, char *buf)
 {
-       struct block_t *block = get_block((uint16_t)g_block);
+       struct zlogger_block *block = get_block((uint16_t)g_block);
 
        if (!block)
                return snprintf(buf, PAGE_SIZE, "[%d] Invalid block\n", g_block);
 
        return snprintf(buf, PAGE_SIZE, "[%d] pid:%u tid:%u offset:%u %llu %llu\n",
                        g_block, block->head.pid, block->head.tid, block->head.offset,
-                       ((struct entry_t *)(block->data))->time, block->head.ts);
+                       ((struct zlogger_entry *)(block->data))->time, block->head.ts);
 }
 
 static DEVICE_ATTR_RW(block);
@@ -798,8 +767,8 @@ int __init zlogger_init(void)
                return -ENOMEM;
        hash_init(g_thread_table->data);
 
-       for (g_shm_ptr_i = 0; g_shm_ptr_i < DEVICE_COUNT; g_shm_ptr_i++) {
-               g_shm_ptr[g_shm_ptr_i] = kzalloc(MAP_SIZE, GFP_KERNEL);
+       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);
                if (g_shm_ptr[g_shm_ptr_i] == NULL) {
                        r = -ENOMEM;
                        goto out_free_g_thread_table_g_shm_ptr;
@@ -811,8 +780,8 @@ int __init zlogger_init(void)
 
        init_completion(&g_completion);
 
-       queue_init(&g_free_q, "free", BLOCK_COUNT);
-       for (i = 1; i <= BLOCK_COUNT; i++)
+       queue_init(&g_free_q, "free", ZLOGGER_BLOCK_COUNT);
+       for (i = 1; i <= ZLOGGER_BLOCK_COUNT; i++)
                queue_push(&g_free_q, i);
 
 #ifdef CONFIG_TRACE_CLOCK
@@ -854,7 +823,7 @@ static void __exit zlogger_exit(void)
        kfree(g_thread_table);
        g_thread_table = NULL;
 
-       for (i = 0; i < DEVICE_COUNT; i++) {
+       for (i = 0; i < ZLOGGER_DEVICE_COUNT; i++) {
                kfree(g_shm_ptr[i]);
                g_shm_ptr[i] = NULL;
        }
index 95dfed4..92ed20c 100644 (file)
@@ -43,58 +43,58 @@ int main(int argc, char *argv[])
                return -1;
        }
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_DEFAULT);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_DEFAULT);
        write(fd, prio_default, strlen(prio_default));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_VERBOSE);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_VERBOSE);
        write(fd, prio_verbose, strlen(prio_verbose));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_DEBUG);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_DEBUG);
        write(fd, prio_debug, strlen(prio_debug));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_INFO);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_INFO);
        write(fd, prio_info, strlen(prio_info));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_WARN);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_WARN);
        write(fd, prio_warn, strlen(prio_warn));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_ERROR);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_ERROR);
        write(fd, prio_error, strlen(prio_error));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_FATAL);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_FATAL);
        write(fd, prio_fatal, strlen(prio_fatal));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_SILENT);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_SILENT);
        write(fd, prio_silent, strlen(prio_silent));
 
-       ioctl(fd, SET_DEFAULT_TAG, "TESTPROGRAM_1");
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_TAG, "TESTPROGRAM_1");
        write(fd, test_message, strlen(test_message));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_DEFAULT);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_DEFAULT);
        write(fd, prio_default, strlen(prio_default));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_VERBOSE);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_VERBOSE);
        write(fd, prio_verbose, strlen(prio_verbose));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_DEBUG);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_DEBUG);
        write(fd, prio_debug, strlen(prio_debug));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_INFO);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_INFO);
        write(fd, prio_info, strlen(prio_info));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_WARN);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_WARN);
        write(fd, prio_warn, strlen(prio_warn));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_ERROR);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_ERROR);
        write(fd, prio_error, strlen(prio_error));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_FATAL);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_FATAL);
        write(fd, prio_fatal, strlen(prio_fatal));
 
-       ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_SILENT);
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_SILENT);
        write(fd, prio_silent, strlen(prio_silent));
 
-       ioctl(fd, SET_DEFAULT_TAG, "TESTPROGRAM_2");
+       ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_TAG, "TESTPROGRAM_2");
        write(fd, test_message, strlen(test_message));
        return 0;
 }