2023-01-05 10:44:41

by John Ogness

[permalink] [raw]
Subject: [PATCH printk v4 7/8] printk: use printk_buffers for devkmsg

Replace the individual @text_buf and @buf buffers in struct
devkmsg_user with a struct printk_buffers. This reduces the number
of buffers to keep track of. Also, since @text_buf is only used for
ringbuffer reading and @buf is only used for output, it maps
perfectly to the purpose and fields of struct printk_buffers.

As a side-effect, @text_buf was 8kB large, even though it only
needed to be the max size of a ringbuffer record. By switching to
struct printk_buffers, ~7kB less memory is allocated when opening
/dev/kmsg.

And since struct printk_buffers will be used now, reduce duplicate
code by calling printk_get_next_message() to handle the record
reading and formatting.

Signed-off-by: John Ogness <[email protected]>
---
kernel/printk/printk.c | 32 ++++++++++++++------------------
1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 4fb7d29fb05d..e28a3736a399 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -695,15 +695,16 @@ static ssize_t msg_print_ext_body(char *buf, size_t size,
return len;
}

+static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, bool is_extended);
+
/* /dev/kmsg - userspace message inject/listen interface */
struct devkmsg_user {
atomic64_t seq;
struct ratelimit_state rs;
struct mutex lock;
- char buf[CONSOLE_EXT_LOG_MAX];

+ struct printk_buffers pbufs;
struct printk_info info;
- char text_buf[CONSOLE_EXT_LOG_MAX];
struct printk_record record;
};

@@ -787,7 +788,10 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
{
struct devkmsg_user *user = file->private_data;
struct printk_record *r = &user->record;
- size_t len;
+ char *outbuf = &user->pbufs.outbuf[0];
+ struct printk_message pmsg = {
+ .pbufs = &user->pbufs,
+ };
ssize_t ret;

if (!user)
@@ -797,7 +801,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
if (ret)
return ret;

- if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) {
+ if (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true)) {
if (file->f_flags & O_NONBLOCK) {
ret = -EAGAIN;
goto out;
@@ -820,30 +824,25 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
goto out;
}

- if (r->info->seq != atomic64_read(&user->seq)) {
+ if (pmsg.dropped) {
/* our last seen message is gone, return error and reset */
- atomic64_set(&user->seq, r->info->seq);
+ atomic64_set(&user->seq, pmsg.seq);
ret = -EPIPE;
goto out;
}

- len = info_print_ext_header(user->buf, sizeof(user->buf), r->info);
- len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len,
- &r->text_buf[0], r->info->text_len,
- &r->info->dev_info);
-
- atomic64_set(&user->seq, r->info->seq + 1);
+ atomic64_set(&user->seq, pmsg.seq + 1);

- if (len > count) {
+ if (pmsg.outbuf_len > count) {
ret = -EINVAL;
goto out;
}

- if (copy_to_user(buf, user->buf, len)) {
+ if (copy_to_user(buf, outbuf, pmsg.outbuf_len)) {
ret = -EFAULT;
goto out;
}
- ret = len;
+ ret = pmsg.outbuf_len;
out:
mutex_unlock(&user->lock);
return ret;
@@ -937,9 +936,6 @@ static int devkmsg_open(struct inode *inode, struct file *file)

mutex_init(&user->lock);

- prb_rec_init_rd(&user->record, &user->info,
- &user->text_buf[0], sizeof(user->text_buf));
-
atomic64_set(&user->seq, prb_first_valid_seq(prb));

file->private_data = user;
--
2.30.2


2023-01-05 12:26:26

by John Ogness

[permalink] [raw]
Subject: Re: [PATCH printk v4 7/8] printk: use printk_buffers for devkmsg

On 2023-01-05, John Ogness <[email protected]> wrote:
> /* /dev/kmsg - userspace message inject/listen interface */
> struct devkmsg_user {
> atomic64_t seq;
> struct ratelimit_state rs;
> struct mutex lock;
> - char buf[CONSOLE_EXT_LOG_MAX];
>
> + struct printk_buffers pbufs;
> struct printk_info info;
> - char text_buf[CONSOLE_EXT_LOG_MAX];
> struct printk_record record;
> };

There is also no need for the printk_info and printk_record
structs. printk_get_next_message() uses its own on the stack.

> @@ -797,7 +801,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
> if (ret)
> return ret;
>
> - if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) {
> + if (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true)) {

This needs to be a while loop because printk_get_next_message() should
be the only function reading from the ringbuffer. The
wait_event_interruptible() in this loop should just wait for the
existence of the new record. It should not be the one reading it.

I will reply to this message with the correct patch to replace this
patch.

John Ogness

2023-01-05 12:34:57

by John Ogness

[permalink] [raw]
Subject: [PATCH printk v4 7/8 v2] printk: use printk_buffers for devkmsg

Replace the buffers in struct devkmsg_user with a struct
printk_buffers. This reduces the number of buffers to track.

As a side-effect, @text_buf was 8kB large, even though it only
needed to be the max size of a ringbuffer record. By switching to
struct printk_buffers, ~7kB less memory is allocated when opening
/dev/kmsg.

And since struct printk_buffers will be used now, reduce duplicate
code by calling printk_get_next_message() to handle the record
reading and formatting.

Signed-off-by: John Ogness <[email protected]>
---
kernel/printk/printk.c | 40 ++++++++++++++++------------------------
1 file changed, 16 insertions(+), 24 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 4fb7d29fb05d..32996b0c5d03 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -695,16 +695,14 @@ static ssize_t msg_print_ext_body(char *buf, size_t size,
return len;
}

+static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, bool is_extended);
+
/* /dev/kmsg - userspace message inject/listen interface */
struct devkmsg_user {
atomic64_t seq;
struct ratelimit_state rs;
struct mutex lock;
- char buf[CONSOLE_EXT_LOG_MAX];
-
- struct printk_info info;
- char text_buf[CONSOLE_EXT_LOG_MAX];
- struct printk_record record;
+ struct printk_buffers pbufs;
};

static __printf(3, 4) __cold
@@ -786,8 +784,10 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
struct devkmsg_user *user = file->private_data;
- struct printk_record *r = &user->record;
- size_t len;
+ char *outbuf = &user->pbufs.outbuf[0];
+ struct printk_message pmsg = {
+ .pbufs = &user->pbufs,
+ };
ssize_t ret;

if (!user)
@@ -797,7 +797,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
if (ret)
return ret;

- if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) {
+ while (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true)) {
if (file->f_flags & O_NONBLOCK) {
ret = -EAGAIN;
goto out;
@@ -814,36 +814,31 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
* This pairs with __wake_up_klogd:A.
*/
ret = wait_event_interruptible(log_wait,
- prb_read_valid(prb,
- atomic64_read(&user->seq), r)); /* LMM(devkmsg_read:A) */
+ prb_read_valid(prb, atomic64_read(&user->seq),
+ NULL)); /* LMM(devkmsg_read:A) */
if (ret)
goto out;
}

- if (r->info->seq != atomic64_read(&user->seq)) {
+ if (pmsg.dropped) {
/* our last seen message is gone, return error and reset */
- atomic64_set(&user->seq, r->info->seq);
+ atomic64_set(&user->seq, pmsg.seq);
ret = -EPIPE;
goto out;
}

- len = info_print_ext_header(user->buf, sizeof(user->buf), r->info);
- len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len,
- &r->text_buf[0], r->info->text_len,
- &r->info->dev_info);
+ atomic64_set(&user->seq, pmsg.seq + 1);

- atomic64_set(&user->seq, r->info->seq + 1);
-
- if (len > count) {
+ if (pmsg.outbuf_len > count) {
ret = -EINVAL;
goto out;
}

- if (copy_to_user(buf, user->buf, len)) {
+ if (copy_to_user(buf, outbuf, pmsg.outbuf_len)) {
ret = -EFAULT;
goto out;
}
- ret = len;
+ ret = pmsg.outbuf_len;
out:
mutex_unlock(&user->lock);
return ret;
@@ -937,9 +932,6 @@ static int devkmsg_open(struct inode *inode, struct file *file)

mutex_init(&user->lock);

- prb_rec_init_rd(&user->record, &user->info,
- &user->text_buf[0], sizeof(user->text_buf));
-
atomic64_set(&user->seq, prb_first_valid_seq(prb));

file->private_data = user;
--
2.30.2

2023-01-06 10:58:21

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH printk v4 7/8 v2] printk: use printk_buffers for devkmsg

On Thu 2023-01-05 13:22:16, John Ogness wrote:
> Replace the buffers in struct devkmsg_user with a struct
> printk_buffers. This reduces the number of buffers to track.
>
> As a side-effect, @text_buf was 8kB large, even though it only
> needed to be the max size of a ringbuffer record. By switching to
> struct printk_buffers, ~7kB less memory is allocated when opening
> /dev/kmsg.
>
> And since struct printk_buffers will be used now, reduce duplicate
> code by calling printk_get_next_message() to handle the record
> reading and formatting.

> Signed-off-by: John Ogness <[email protected]>
> ---
> kernel/printk/printk.c | 40 ++++++++++++++++------------------------
> 1 file changed, 16 insertions(+), 24 deletions(-)
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 4fb7d29fb05d..32996b0c5d03 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -695,16 +695,14 @@ static ssize_t msg_print_ext_body(char *buf, size_t size,
> return len;
> }
>
> +static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, bool is_extended);
> +
> /* /dev/kmsg - userspace message inject/listen interface */
> struct devkmsg_user {
> atomic64_t seq;
> struct ratelimit_state rs;
> struct mutex lock;
> - char buf[CONSOLE_EXT_LOG_MAX];
> -
> - struct printk_info info;
> - char text_buf[CONSOLE_EXT_LOG_MAX];
> - struct printk_record record;
> + struct printk_buffers pbufs;
> };
>
> static __printf(3, 4) __cold
> @@ -786,8 +784,10 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
> size_t count, loff_t *ppos)
> {
> struct devkmsg_user *user = file->private_data;
> - struct printk_record *r = &user->record;
> - size_t len;
> + char *outbuf = &user->pbufs.outbuf[0];
> + struct printk_message pmsg = {
> + .pbufs = &user->pbufs,
> + };
> ssize_t ret;
>
> if (!user)
> @@ -797,7 +797,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
> if (ret)
> return ret;
>
> - if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) {
> + while (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true)) {

A problem is that printk_get_next_message() does not format the
message when it shoud get supressed on the console.

I would solve it be adding a parameter to printk_get_next_message() that
will tell whether to suppress or not, e.g. @can_suppress.

> if (file->f_flags & O_NONBLOCK) {
> ret = -EAGAIN;
> goto out;
> @@ -814,36 +814,31 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
> * This pairs with __wake_up_klogd:A.
> */
> ret = wait_event_interruptible(log_wait,
> - prb_read_valid(prb,
> - atomic64_read(&user->seq), r)); /* LMM(devkmsg_read:A) */
> + prb_read_valid(prb, atomic64_read(&user->seq),
> + NULL)); /* LMM(devkmsg_read:A) */

The above change from "if" to "while" could be avoided if we use
printk_get_next_message() here as well. It looks slightly more
strightfoward to me. Or do I miss something, please?


> if (ret)
> goto out;
> }
>

Otherwise, I like this change. It simplifies the code.

Best Regards,
Petr

2023-01-08 21:29:10

by John Ogness

[permalink] [raw]
Subject: Re: [PATCH printk v4 7/8 v2] printk: use printk_buffers for devkmsg

On 2023-01-06, Petr Mladek <[email protected]> wrote:
>> - if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) {
>> + while (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true)) {
>
> A problem is that printk_get_next_message() does not format the
> message when it shoud get supressed on the console.

Nice catch. I missed that.

> I would solve it be adding a parameter to printk_get_next_message()
> that will tell whether to suppress or not, e.g. @can_suppress.

OK.

>> if (file->f_flags & O_NONBLOCK) {
>> ret = -EAGAIN;
>> goto out;
>> @@ -814,36 +814,31 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
>> * This pairs with __wake_up_klogd:A.
>> */
>> ret = wait_event_interruptible(log_wait,
>> - prb_read_valid(prb,
>> - atomic64_read(&user->seq), r)); /* LMM(devkmsg_read:A) */
>> + prb_read_valid(prb, atomic64_read(&user->seq),
>> + NULL)); /* LMM(devkmsg_read:A) */
>
> The above change from "if" to "while" could be avoided if we use
> printk_get_next_message() here as well. It looks slightly more
> strightfoward to me.

Yes, that is better. A loop is overkill here.

John