Re: [PATCH v6 1/3] printk: Add line-buffered printk() API.

From: Tetsuo Handa
Date: Fri Nov 02 2018 - 21:56:07 EST


On 2018/11/02 23:40, Matthew Wilcox wrote:
> On Fri, Nov 02, 2018 at 10:31:55PM +0900, Tetsuo Handa wrote:
>> get_printk_buffer() tries to assign a "struct printk_buffer" from
>> statically preallocated array. get_printk_buffer() returns NULL if
>> all "struct printk_buffer" are in use, but the caller does not need to
>> check for NULL.
>
> This seems like a great way of wasting 16kB of memory. Since you've
> already made printk_buffered() work with a NULL initial argument, what's
> the advantage over just doing kmalloc(1024, GFP_ATOMIC)?

Like "[PATCH 2/3] mm: Use line-buffered printk() for show_free_areas()."
demonstrates, kzalloc(sizeof(struct printk_buffer), GFP_ATOMIC) can fail.

And using statically preallocated buffers helps avoiding

(1) out of buffers when memory cannot be allocated

(2) kernel stack overflow when kernel stack is already tight (e.g.
a memory allocation attempt from an interrupt handler which was
invoked from deep inside call chain of a process context)

. Whether

(A) tuning the number of statically preallocated buffers

(B) allocating buffers on caller side (e.g. kzalloc() or in .bss section)

are useful is a future decision, for too much concurrent printk() will lockup
the system even if there are enough buffers. I think that starting with
statically preallocated buffers is (at least for now) a good choice for
minimizing risk of (1) (2) while offering practically acceptable result.