From 5a962adc2d9effd954e9e52502a0793f2e9a648f Mon Sep 17 00:00:00 2001 From: shenchenkai Date: Sat, 18 Dec 2021 16:53:16 +0800 Subject: [PATCH 1/2] hilog: enhance maintenance for hilog ring buffer ohos inclusion category: feature issue: #I4N4XS CVE: NA -------------------------------- enhance maintenance for hilog ring buffer 1. If hilog ring buffer failed for verification, clean ring buffer to recover. 2. Print the count of dropped hilog if ring buffer is full. 3. Add HILOG_BUFFER_SIZE in Kconfig, so user can customized hlog ring buffer size by means of modifying the value of HILOG_BUFFER_SIZE Signed-off-by: shenchenkai --- drivers/staging/hievent/hievent_driver.c | 15 +++++++++++---- drivers/staging/hilog/Kconfig | 11 ++++++----- drivers/staging/hilog/hilog.c | 14 +++++++++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/drivers/staging/hievent/hievent_driver.c b/drivers/staging/hievent/hievent_driver.c index f6b378766abc..8331a9632e62 100644 --- a/drivers/staging/hievent/hievent_driver.c +++ b/drivers/staging/hievent/hievent_driver.c @@ -178,6 +178,13 @@ static ssize_t hievent_read(struct file *file, char __user *user_buf, retval = header.len + sizeof(header); out: + if (retval == -ENOMEM) { + // clean ring buffer + hievent_dev.write_offset = 0; + hievent_dev.head_offset = 0; + hievent_dev.size = 0; + hievent_dev.count = 0; + } (void)mutex_unlock(&hievent_dev.mtx); return retval; @@ -297,7 +304,7 @@ static unsigned int hievent_poll(struct file *filep, return (POLLOUT | POLLWRNORM); } -static ssize_t hievent_write_iter(struct kiocb *iocb, struct iov_iter *from) +static ssize_t hievent_write_iter(struct kiocb *iocb, struct iov_iter *from) { int check_code = 0; unsigned char *temp_buffer = NULL; @@ -306,7 +313,7 @@ static ssize_t hievent_write_iter(struct kiocb *iocb, struct iov_iter *from) int buf_len; (void)iocb; - if (from->nr_segs != 3) { /* must contain 3 segments */ + if (from->nr_segs != 3) { /* must contain 3 segments */ retval = -EINVAL; goto out; } @@ -361,8 +368,8 @@ static ssize_t hievent_write_iter(struct kiocb *iocb, struct iov_iter *from) } static const struct file_operations hievent_fops = { - .read = hievent_read, /* read */ - .poll = hievent_poll, /* poll */ + .read = hievent_read, /* read */ + .poll = hievent_poll, /* poll */ .write_iter = hievent_write_iter, /* write_iter */ }; diff --git a/drivers/staging/hilog/Kconfig b/drivers/staging/hilog/Kconfig index 23fe9000b585..243934c4cc4c 100644 --- a/drivers/staging/hilog/Kconfig +++ b/drivers/staging/hilog/Kconfig @@ -14,8 +14,9 @@ config HILOG If unsure, say N. -if HILOG -config HI_LOG_BUFFER_SIZE - int "hi log buffer size" - default "2048" -endif +config HILOG_BUFFER_SIZE + int "hilog buffer size" + depends on HILOG + default 4096 + help + Define the default ring buffer size of hilog diff --git a/drivers/staging/hilog/hilog.c b/drivers/staging/hilog/hilog.c index dae5ab074cc8..167d4e3f359f 100644 --- a/drivers/staging/hilog/hilog.c +++ b/drivers/staging/hilog/hilog.c @@ -44,7 +44,7 @@ module_param(hilog_major, int, 0444); struct cdev g_hilog_cdev; -#define HILOG_BUFFER ((size_t)1024) +#define HILOG_BUFFER CONFIG_HILOG_BUFFER_SIZE #define HILOG_DRIVER "/dev/hilog" struct hilog_entry { @@ -187,6 +187,13 @@ static ssize_t hilog_read(struct file *file, hilog_buffer_dec(header.len); retval = header.len + sizeof(header); out: + if (retval == -ENOMEM) { + // clean ring buffer + hilog_dev.wr_off = 0; + hilog_dev.hdr_off = 0; + hilog_dev.size = 0; + hilog_dev.count = 0; + } (void)mutex_unlock(&hilog_dev.mtx); return retval; @@ -262,6 +269,7 @@ static void hilog_cover_old_log(size_t buf_len) int retval; struct hilog_entry header; size_t total_size = buf_len + sizeof(struct hilog_entry); + int drop_log_lines = 0; while (total_size + hilog_dev.size >= HILOG_BUFFER) { retval = hilog_read_ring_head_buffer((unsigned char *)&header, @@ -269,8 +277,12 @@ static void hilog_cover_old_log(size_t buf_len) if (retval < 0) break; + drop_log_lines++; hilog_buffer_dec(sizeof(header) + header.len); } + if (drop_log_lines > 0) + pr_info("hilog ringbuffer full, drop %d line(s) log", + drop_log_lines); } int hilog_write_internal(const char __user *buffer, size_t buf_len) -- Gitee From f6ee218d36a007000afa350ee32ed542cee238ba Mon Sep 17 00:00:00 2001 From: shenchenkai Date: Thu, 6 Jan 2022 20:32:03 +0800 Subject: [PATCH 2/2] hilog: fix some hilog issue ohos inclusion category: bugfix issue: #I4PH4E CVE: NA -------------------------------- 1. hilog use pid to print tgid, it is wrong, fix it. 2. fix hilog compile warning. 3. reduce print log if hilog ringbuffer is full. Signed-off-by: shenchenkai --- drivers/staging/hilog/hilog.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/staging/hilog/hilog.c b/drivers/staging/hilog/hilog.c index 167d4e3f359f..af490cdaa291 100644 --- a/drivers/staging/hilog/hilog.c +++ b/drivers/staging/hilog/hilog.c @@ -44,7 +44,7 @@ module_param(hilog_major, int, 0444); struct cdev g_hilog_cdev; -#define HILOG_BUFFER CONFIG_HILOG_BUFFER_SIZE +#define HILOG_BUFFER ((size_t)CONFIG_HILOG_BUFFER_SIZE) #define HILOG_DRIVER "/dev/hilog" struct hilog_entry { @@ -161,7 +161,7 @@ static ssize_t hilog_read(struct file *file, if (count < header.len + sizeof(header)) { pr_err("buffer too small,buf_len=%d, header.len=%d,%d\n", - count, header.len, header.header_size); + (int)count, header.len, header.header_size); retval = -ENOMEM; goto out; } @@ -260,7 +260,7 @@ static void hilog_head_init(struct hilog_entry *header, size_t len) header->len = len; header->pid = current->pid; - header->task_id = current->pid; + header->task_id = current->tgid; header->header_size = sizeof(struct hilog_entry); } @@ -269,7 +269,9 @@ static void hilog_cover_old_log(size_t buf_len) int retval; struct hilog_entry header; size_t total_size = buf_len + sizeof(struct hilog_entry); - int drop_log_lines = 0; + static int drop_log_lines; + static bool is_last_time_full; + bool is_this_time_full = false; while (total_size + hilog_dev.size >= HILOG_BUFFER) { retval = hilog_read_ring_head_buffer((unsigned char *)&header, @@ -278,11 +280,18 @@ static void hilog_cover_old_log(size_t buf_len) break; drop_log_lines++; + is_this_time_full = true; + is_last_time_full = true; hilog_buffer_dec(sizeof(header) + header.len); } - if (drop_log_lines > 0) - pr_info("hilog ringbuffer full, drop %d line(s) log", - drop_log_lines); + if (is_last_time_full && !is_this_time_full) { + /* so we can only print one log if hilog ring buffer is full in a short time */ + if (drop_log_lines > 0) + pr_info("hilog ringbuffer full, drop %d line(s) log\n", + drop_log_lines); + is_last_time_full = false; + drop_log_lines = 0; + } } int hilog_write_internal(const char __user *buffer, size_t buf_len) -- Gitee