From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54AEB241665 for ; Fri, 13 Mar 2026 16:31:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773419519; cv=none; b=MPpWHxlObYeGIJUM56rIRwujiL/EwOtzvL3AFsqbvENzr/ieipYzX+gYZ9gd5OSSHJmb7At/uvnOFGtOujQ/mmyZUEy1RMJqSlnpUVkqGs/t6oEewPxlE2EIk3QWncxuAsBNAwQAQE7VkqrWGrnKD1VGRvTx/ilM7Rl1QQ/uw3w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773419519; c=relaxed/simple; bh=aCijxA+3bbY0JnNr5WP0UyOICCSNwwdlIlsH/0ZVCOM=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=ZRJZbDt8NQWo5cpbJIXrNF/6heSyh+GkQP4Ga6PH3FIjesCkNkniRuvfx2G+1+U7U2dE8aERCkqpL5hoVli/Bx8u+fNerii0GBB3+i6ny9jiNiDJ3zHH+tYuTo/MNDM+dKbtKeNPi3qcKp5DxVIay7a83IrB/SfzyeQIqWT+o00= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ELXqIPw6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=O0inF4LQ; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ELXqIPw6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="O0inF4LQ" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1773419516; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=rlsfXrctL3CFKTAg4IIoI7GrcLRv1fNuuy0tIet4oi0=; b=ELXqIPw6KRFIn+ABpTIqnInwFhrNrranaZ3/WOg5fZjZEVpJxEhY4kdiNUNyQDD8q6QtM1 tj6T7fb9r8qSJS2K4HrfEErVcdNIWcaEPjhUq9CvyECwfJ/CZrRUDwdDNZFuy4TZRXSUFr SJSL+SQz2uLFgUs53ocOIUgmNgTlomfp1tDMqmC1VNSHlSRlrpm0RSkpWHu8QTqYcVw9Yr SNmWgeS2BJ3tnP2NwdzK8Q+CWx64Yi0JNbJfTRA6cJCbUe/FJMD0K7sGeVzYhMi+ug+BBx sYQ29co3FnN/7OZjcd9kb9pBUGRMGsZA8ATZxjyvL2NxCuuIeU7HVFF++U1kMw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1773419516; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=rlsfXrctL3CFKTAg4IIoI7GrcLRv1fNuuy0tIet4oi0=; b=O0inF4LQNZJeFGzwkO16/JJM6zPN7whkp0FvgBRdQbC/LIGNyR3s2IPMlo6ZcnFa865o2e CfW5ebNO+xMndbBA== To: Petr Mladek , Jianzhou Zhao Cc: linux-kernel@vger.kernel.org, senozhatsky@chromium.org, rostedt@goodmis.org Subject: Re: KCSAN: data-race in desc_read / prb_reserve_in_last In-Reply-To: References: <1a32d127.3865.19cdaf2c166.Coremail.luckd0g@163.com> Date: Fri, 13 Mar 2026 17:37:55 +0106 Message-ID: <87ikazitc4.fsf@jogness.linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Hi Petr, On 2026-03-12, Petr Mladek wrote: > [rfc v2]: > > diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c > index 56c8e3d031f4..c080edf5636a 100644 > --- a/kernel/printk/printk_ringbuffer.c > +++ b/kernel/printk/printk_ringbuffer.c > @@ -454,11 +454,12 @@ static enum desc_state desc_read(struct prb_desc_ring *desc_ring, > struct printk_info *info = to_info(desc_ring, id); > struct prb_desc *desc = to_desc(desc_ring, id); > atomic_long_t *state_var = &desc->state_var; > + unsigned long state_val, state_val_check; > enum desc_state d_state; > - unsigned long state_val; > > /* Check the descriptor state. */ > state_val = atomic_long_read(state_var); /* LMM(desc_read:A) */ > +try_again: > d_state = get_desc_state(id, state_val); > if (d_state == desc_miss || d_state == desc_reserved) { > /* > @@ -466,7 +467,9 @@ static enum desc_state desc_read(struct prb_desc_ring *desc_ring, > * @state_var so that the caller can see the details of > * the inconsistent state. > */ > - goto out; > + if (desc_out) > + atomic_long_set(&desc_out->state_var, state_val); > + return d_state; I would keep the "goto out" so that the success scenario can also set the state_var of @desc_out. > } > > /* > @@ -542,14 +545,19 @@ static enum desc_state desc_read(struct prb_desc_ring *desc_ring, > smp_rmb(); /* LMM(desc_read:D) */ > > /* > - * The data has been copied. Return the current descriptor state, > - * which may have changed since the load above. > + * The data has been copied. Make sure that they match the given @id > + * and @d_state read at the beginning. Re-read them otherwise. > + * > + * Note that this simplifies the logic. Especially, the reader API > + * expects that all values are stable when the record is finalized. > + * But they are stable only when it stays finalized all the time. > */ > - state_val = atomic_long_read(state_var); /* LMM(desc_read:E) */ > - d_state = get_desc_state(id, state_val); > -out: > - if (desc_out) > - atomic_long_set(&desc_out->state_var, state_val); > + state_val_check = atomic_long_read(state_var); /* LMM(desc_read:E) */ > + if (state_val_check != state_val) { > + state_val = state_val_check; > + goto try_again; > + } > + The out label and code could stay as it was. Or is there some reason you do not want to set the state_var in the success case? out: if (desc_out) atomic_long_set(&desc_out->state_var, state_val); > return d_state; > } We should probably annotate the memcpy() with a wrapping data_race(). Sadly we were aware of and expected the data race, but did not realize that commit 4cfc7258f876 ("printk: ringbuffer: add finalization/extension support") made the data race a problem. John