Re: [RFC][PATCHv2 2/4] printk: move printk_safe macros to printk header

From: Peter Zijlstra
Date: Tue Oct 16 2018 - 10:22:10 EST


On Tue, Oct 16, 2018 at 02:54:15PM +0200, Peter Zijlstra wrote:
> printk will determine the current context:
>
> task, softirq, hardirq or NMI
>
> and pick the corresponding per-cpu line buffer and do the vsnprintf()

We need 4, but we don't need to do the exact context determination for
this. We can keep a simple counter:

#define MAX_IDX 4 /* task, sirq, hirq, nmi */
#define MAX_LEN 1020 /* sizeof(struct line_buffer) < 4k */

struct line_buffers {
int idx;
char line[MAX_IDX][MAX_LEN];
};

static DEFINE_PER_CPU(struct line_buffers, lbs);

static char *get_linebuf(void)
{
struct line_buffers *lbp = this_cpu_ptr(&lbs);
int idx;

idx = lbp->idx++;
return lbp->linx[idx];
}

static void put_linbuf(void)
{
this_cpu_dec(lbs.idx);
}

> thing. Then we have the actual line length and content. With the length
> we reserve the bytes from the global buffer, we memcpy into the buffer
> and commit.