mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: "feng.zhou" <realsummitzhou@gmail.com>
Cc: linux-kernel@vger.kernel.org, senozhatsky@chromium.org,
	rostedt@goodmis.org, john.ogness@linutronix.de
Subject: Re: [PATCH] printk: Fix _DESCS_COUNT type for 64-bit systems
Date: Tue, 10 Mar 2026 17:37:32 +0100	[thread overview]
Message-ID: <abBIzKZaMD1Zx4n_@pathway.suse.cz> (raw)
In-Reply-To: <20260202094140.9518-1-realsummitzhou@gmail.com>

On Mon 2026-02-02 17:41:40, feng.zhou wrote:
> The _DESCS_COUNT macro currently uses 1U (32-bit unsigned) instead of
> 1UL (unsigned long), which breaks the intended overflow testing design
> on 64-bit systems.
> 
> Problem Analysis:
> ----------------
> The printk_ringbuffer uses a deliberate design choice to initialize
> descriptor IDs near the maximum 62-bit value to trigger overflow early
> in the system's lifetime. This is documented in printk_ringbuffer.h:
> 
>   "initial values are chosen that map to the correct initial array
>    indexes, but will result in overflows soon."
> 
> The DESC0_ID macro calculates:
>   DESC0_ID(ct_bits) = DESC_ID(-(_DESCS_COUNT(ct_bits) + 1))
> 
> On 64-bit systems with typical configuration (descbits=16):
> - Current buggy behavior: DESC0_ID = 0xfffeffff
> - Expected behavior:      DESC0_ID = 0x3ffffffffffeffff
> 
> The buggy version only uses 32 bits, which means:
> 1. The initial ID is nowhere near 2^62
> 2. It would take ~140 trillion wraps to trigger 62-bit overflow
> 3. The overflow handling code is never tested in practice
> 
> Root Cause:
> ----------
> The issue is in this line:
>   #define _DESCS_COUNT(ct_bits)    (1U << (ct_bits))
> 
> When _DESCS_COUNT(16) is calculated:
>   1U << 16 = 0x10000 (32-bit value)
>   -(0x10000 + 1) = -0x10001 = 0xFFFEFFFF (32-bit two's complement)
> 
> On 64-bit systems, this 32-bit value doesn't get extended to create
> the intended 62-bit ID near the maximum value.
> 
> Impact:
> ------
> While index calculations still work correctly in the short term, this
> bug has several implications:
> 
> 1. Violates the design intention documented in the code
> 2. Overflow handling code paths remain untested
> 3. ABA detection code doesn't get exercised under overflow conditions
> 4. In extreme long-term running scenarios (though unlikely), could
>    potentially cause issues when ID actually reaches 2^62
> 
> Verification:
> ------------
> Tested on ARM64 system with CONFIG_LOG_BUF_SHIFT=20 (descbits=15):
> - Before fix: DESC0_ID(16) = 0xfffeffff
> - After fix:  DESC0_ID(16) = 0x3fffffffffff7fff
> 
> The fix aligns _DESCS_COUNT with _DATA_SIZE, which already correctly
> uses 1UL:
>   #define _DATA_SIZE(sz_bits)    (1UL << (sz_bits))
> 
> Signed-off-by: feng.zhou <realsummitzhou@gmail.com>

JFYI, the patch has been committed into printk/linux.git,
branch rework/prb-fixes.

Best Regards,
Petr

  parent reply	other threads:[~2026-03-10 16:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-02  9:41 feng.zhou
2026-02-02 11:41 ` John Ogness
2026-02-02 15:07   ` [RESEND] " feng.zhou
2026-02-03 17:24     ` Petr Mladek
2026-03-03 16:52   ` Petr Mladek
2026-03-10 16:37 ` Petr Mladek [this message]
2026-03-10 21:40   ` John Ogness
2026-03-12  9:02     ` Petr Mladek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=abBIzKZaMD1Zx4n_@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=realsummitzhou@gmail.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®