Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

printk_ringbuffer: Create a helper function to decide whether more space is needed

The decision whether some more space is needed is tricky in the printk
ring buffer code:

1. The given lpos values might overflow. A subtraction must be used
instead of a simple "lower than" check.

2. Another CPU might reuse the space in the mean time. It can be
detected when the subtraction is bigger than DATA_SIZE(data_ring).

3. There is exactly enough space when the result of the subtraction
is zero. But more space is needed when the result is exactly
DATA_SIZE(data_ring).

Add a helper function to make sure that the check is done correctly
in all situations. Also it helps to make the code consistent and
better documented.

Suggested-by: John Ogness <john.ogness@linutronix.de>
Link: https://lore.kernel.org/r/87tsz7iea2.fsf@jogness.linutronix.de
Reviewed-by: John Ogness <john.ogness@linutronix.de>
Link: https://patch.msgid.link/20251107194720.1231457-3-pmladek@suse.com
[pmladek@suse.com: Updated wording as suggested by John]
Signed-off-by: Petr Mladek <pmladek@suse.com>

+28 -4
+28 -4
kernel/printk/printk_ringbuffer.c
··· 411 411 return to_blk_size(size) <= DATA_SIZE(data_ring) / 2; 412 412 } 413 413 414 + /* 415 + * Compare the current and requested logical position and decide 416 + * whether more space is needed. 417 + * 418 + * Return false when @lpos_current is already at or beyond @lpos_target. 419 + * 420 + * Also return false when the difference between the positions is bigger 421 + * than the size of the data buffer. It might happen only when the caller 422 + * raced with another CPU(s) which already made and used the space. 423 + */ 424 + static bool need_more_space(struct prb_data_ring *data_ring, 425 + unsigned long lpos_current, 426 + unsigned long lpos_target) 427 + { 428 + return lpos_target - lpos_current - 1 < DATA_SIZE(data_ring); 429 + } 430 + 414 431 /* Query the state of a descriptor. */ 415 432 static enum desc_state get_desc_state(unsigned long id, 416 433 unsigned long state_val) ··· 594 577 unsigned long id; 595 578 596 579 /* Loop until @lpos_begin has advanced to or beyond @lpos_end. */ 597 - while ((lpos_end - lpos_begin) - 1 < DATA_SIZE(data_ring)) { 580 + while (need_more_space(data_ring, lpos_begin, lpos_end)) { 598 581 blk = to_block(data_ring, lpos_begin); 599 582 600 583 /* ··· 685 668 * sees the new tail lpos, any descriptor states that transitioned to 686 669 * the reusable state must already be visible. 687 670 */ 688 - while ((lpos - tail_lpos) - 1 < DATA_SIZE(data_ring)) { 671 + while (need_more_space(data_ring, tail_lpos, lpos)) { 689 672 /* 690 673 * Make all descriptors reusable that are associated with 691 674 * data blocks before @lpos. ··· 1165 1148 1166 1149 next_lpos = get_next_lpos(data_ring, blk_lpos->begin, size); 1167 1150 1168 - /* If the data block does not increase, there is nothing to do. */ 1169 - if (head_lpos - next_lpos < DATA_SIZE(data_ring)) { 1151 + /* 1152 + * Use the current data block when the size does not increase, i.e. 1153 + * when @head_lpos is already able to accommodate the new @next_lpos. 1154 + * 1155 + * Note that need_more_space() could never return false here because 1156 + * the difference between the positions was bigger than the data 1157 + * buffer size. The data block is reopened and can't get reused. 1158 + */ 1159 + if (!need_more_space(data_ring, head_lpos, next_lpos)) { 1170 1160 if (wrapped) 1171 1161 blk = to_block(data_ring, 0); 1172 1162 else