Skip to content

Commit ce003d6

Browse files
jognesspmladek
authored andcommitted
printk: ringbuffer: fix setting state in desc_read()
It is expected that desc_read() will always set at least the @state_var field. However, if the descriptor is in an inconsistent state, no fields are set. Also, the second load of @state_var is not stored in @desc_out and so might not match the state value that is returned. Always set the last loaded @state_var into @desc_out, regardless of the descriptor consistency. Fixes: b6cf8b3 ("printk: add lockless ringbuffer") Signed-off-by: John Ogness <john.ogness@linutronix.de> Reviewed-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20200914094803.27365-1-john.ogness@linutronix.de
1 parent e607683 commit ce003d6

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

‎kernel/printk/printk_ringbuffer.c‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,9 @@ static enum desc_state get_desc_state(unsigned long id,
368368
}
369369

370370
/*
371-
* Get a copy of a specified descriptor and its queried state. A descriptor
372-
* that is not in the committed or reusable state must be considered garbage
373-
* by the reader.
371+
* Get a copy of a specified descriptor and return its queried state. If the
372+
* descriptor is in an inconsistent state (miss or reserved), the caller can
373+
* only expect the descriptor's @state_var field to be valid.
374374
*/
375375
static enum desc_state desc_read(struct prb_desc_ring *desc_ring,
376376
unsigned long id, struct prb_desc *desc_out)
@@ -383,8 +383,14 @@ static enum desc_state desc_read(struct prb_desc_ring *desc_ring,
383383
/* Check the descriptor state. */
384384
state_val = atomic_long_read(state_var); /* LMM(desc_read:A) */
385385
d_state = get_desc_state(id, state_val);
386-
if (d_state != desc_committed && d_state != desc_reusable)
387-
return d_state;
386+
if (d_state == desc_miss || d_state == desc_reserved) {
387+
/*
388+
* The descriptor is in an inconsistent state. Set at least
389+
* @state_var so that the caller can see the details of
390+
* the inconsistent state.
391+
*/
392+
goto out;
393+
}
388394

389395
/*
390396
* Guarantee the state is loaded before copying the descriptor
@@ -449,9 +455,15 @@ static enum desc_state desc_read(struct prb_desc_ring *desc_ring,
449455
*/
450456
smp_rmb(); /* LMM(desc_read:D) */
451457

452-
/* Re-check the descriptor state. */
458+
/*
459+
* The data has been copied. Return the current descriptor state,
460+
* which may have changed since the load above.
461+
*/
453462
state_val = atomic_long_read(state_var); /* LMM(desc_read:E) */
454-
return get_desc_state(id, state_val);
463+
d_state = get_desc_state(id, state_val);
464+
out:
465+
atomic_long_set(&desc_out->state_var, state_val);
466+
return d_state;
455467
}
456468

457469
/*

0 commit comments

Comments
 (0)