Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2981502rwb; Mon, 15 Aug 2022 15:23:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR5jwzrar4gUwHym5/vJmkQWkfNX4VHwxPbsxJlL951xA7lFY17NcFgayfiaU9v6Lp+JCMht X-Received: by 2002:a17:907:d0f:b0:731:5c2:a9a5 with SMTP id gn15-20020a1709070d0f00b0073105c2a9a5mr11041822ejc.413.1660602210366; Mon, 15 Aug 2022 15:23:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660602210; cv=none; d=google.com; s=arc-20160816; b=b6hSuETAwO/Y6VdCgCsboVZrOT0BzVo2qqOghsTZn+XiY9Qz+ZH+hIXT5wH1iGtVim rozv+rBWH+td6fjCjckdCU9YCVoNxpsqu2svoFIbtHCmu5Q+YCylH/swbu9El/KBzS97 WxRd3Qu8duQxpGcrOs3XvGSsIeQ212e5OQmHfoXNsrNCAVgk0d7mjzVuqIUKYk8qft3J gLYQvceC6n1lOjdGRSTAXcGf/mqVcnunBU3hS88BuweiaQg9sLghnRlcu2RKOHSZ/We6 nWgtiyMAClIm+ugrHtP+uIhRojfKV7rWelAOKkZxAjqbPsQ5faVAgGndm55KaS3grJaa jE+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=A5VvbI2U+J+pt4GmX17B3NTelqxVR1qscFNNsGkuo9s=; b=NflyaYLIEgIbDx4ts/KnH48UtRsfdAh03mhlev2pd4Mh1HyphTNL78du0GajhOCtyx 9GScVqDfU5DxVharQQjTjD0OjUCltzMIxgh0PvcifFCQmurG1CcywHUTssZJ6ED6qDaY lu2g/nrG5KiWuIlENZGw8SJZCrc7i70Y0gqlcW0FvBhfgic2bQSEaUYdecSze/zvvAR4 MNYCOhq2Zrwk1XbAbWyUbHVHkmC98XydIXb5HwK7DKbWF25YnEENsIBNY13whoRkARjw cODRqjAMJ2NvYG4JhjKmB77ikIPrH855Wi5Qk1AmXUQ1EHtp88C4kIYtjBRvIdj+zCTJ 0+5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=nEF8YDVo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h21-20020a170906111500b007307ca55581si7468360eja.47.2022.08.15.15.23.02; Mon, 15 Aug 2022 15:23:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=nEF8YDVo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348493AbiHOWVE (ORCPT + 99 others); Mon, 15 Aug 2022 18:21:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348853AbiHOWQw (ORCPT ); Mon, 15 Aug 2022 18:16:52 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CCF66542; Mon, 15 Aug 2022 12:40:27 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id CD149B80EB2; Mon, 15 Aug 2022 19:40:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A491C433D6; Mon, 15 Aug 2022 19:40:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660592406; bh=tcRxpjtw1HiPUhUHNS8nboJEdUsZZ/FIKajP6IXJOMo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nEF8YDVo47QBqSdb058a5Q181r5+gNnbyn80C2LnM5ZRLemadsYrhf4VAkvut7B6L HocJJAzv9bqBtnDduKRyqWTDN23lrxrmhSC20FKzHp+Ww0dNvC/ckKy6YbPdrBTzmL m+biQy5+rlFkbZPlgh6BDmqfkAcYBxFk1UzWWi+Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Antonio Borneo , John Ogness , Petr Mladek , Sasha Levin Subject: [PATCH 5.18 0782/1095] scripts/gdb: fix lx-dmesg on 32 bits arch Date: Mon, 15 Aug 2022 20:03:01 +0200 Message-Id: <20220815180501.616913399@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Antonio Borneo [ Upstream commit e3c8d33e0d62175c31ca7ab7ab01b18f0b6318d3 ] The type atomic_long_t can have size 4 or 8 bytes, depending on CONFIG_64BIT; it's only content, the field 'counter', is either an int or a s64 value. Current code incorrectly uses the fixed size utils.read_u64() to read the field 'counter' inside atomic_long_t. On 32 bits architectures reading the last element 'tail_id' of the struct prb_desc_ring: struct prb_desc_ring { ... atomic_long_t tail_id; }; causes the utils.read_u64() to access outside the boundary of the struct and the gdb command 'lx-dmesg' exits with error: Python Exception : index out of range Error occurred in Python: index out of range Query the really used atomic_long_t counter type size. Link: https://lore.kernel.org/r/20220617143758.137307-1-antonio.borneo@foss.st.com Fixes: e60768311af8 ("scripts/gdb: update for lockless printk ringbuffer") Signed-off-by: Antonio Borneo [pmladek@suse.com: Query the really used atomic_long_t counter type size] Tested-by: Antonio Borneo Reviewed-by: John Ogness Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20220719122831.19890-1-pmladek@suse.com Signed-off-by: Sasha Levin --- scripts/gdb/linux/dmesg.py | 9 +++------ scripts/gdb/linux/utils.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/scripts/gdb/linux/dmesg.py b/scripts/gdb/linux/dmesg.py index d5983cf3db7d..c771831eb077 100644 --- a/scripts/gdb/linux/dmesg.py +++ b/scripts/gdb/linux/dmesg.py @@ -22,7 +22,6 @@ prb_desc_type = utils.CachedType("struct prb_desc") prb_desc_ring_type = utils.CachedType("struct prb_desc_ring") prb_data_ring_type = utils.CachedType("struct prb_data_ring") printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer") -atomic_long_type = utils.CachedType("atomic_long_t") class LxDmesg(gdb.Command): """Print Linux kernel log buffer.""" @@ -68,8 +67,6 @@ class LxDmesg(gdb.Command): off = prb_data_ring_type.get_type()['data'].bitpos // 8 text_data_addr = utils.read_ulong(text_data_ring, off) - counter_off = atomic_long_type.get_type()['counter'].bitpos // 8 - sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8 off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8 @@ -89,9 +86,9 @@ class LxDmesg(gdb.Command): # read in tail and head descriptor ids off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8 - tail_id = utils.read_u64(desc_ring, off + counter_off) + tail_id = utils.read_atomic_long(desc_ring, off) off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8 - head_id = utils.read_u64(desc_ring, off + counter_off) + head_id = utils.read_atomic_long(desc_ring, off) did = tail_id while True: @@ -102,7 +99,7 @@ class LxDmesg(gdb.Command): desc = utils.read_memoryview(inf, desc_addr + desc_off, desc_sz).tobytes() # skip non-committed record - state = 3 & (utils.read_u64(desc, sv_off + counter_off) >> desc_flags_shift) + state = 3 & (utils.read_atomic_long(desc, sv_off) >> desc_flags_shift) if state != desc_committed and state != desc_finalized: if did == head_id: break diff --git a/scripts/gdb/linux/utils.py b/scripts/gdb/linux/utils.py index ff7c1799d588..1553f68716cc 100644 --- a/scripts/gdb/linux/utils.py +++ b/scripts/gdb/linux/utils.py @@ -35,13 +35,12 @@ class CachedType: long_type = CachedType("long") - +atomic_long_type = CachedType("atomic_long_t") def get_long_type(): global long_type return long_type.get_type() - def offset_of(typeobj, field): element = gdb.Value(0).cast(typeobj) return int(str(element[field].address).split()[0], 16) @@ -129,6 +128,17 @@ def read_ulong(buffer, offset): else: return read_u32(buffer, offset) +atomic_long_counter_offset = atomic_long_type.get_type()['counter'].bitpos +atomic_long_counter_sizeof = atomic_long_type.get_type()['counter'].type.sizeof + +def read_atomic_long(buffer, offset): + global atomic_long_counter_offset + global atomic_long_counter_sizeof + + if atomic_long_counter_sizeof == 8: + return read_u64(buffer, offset + atomic_long_counter_offset) + else: + return read_u32(buffer, offset + atomic_long_counter_offset) target_arch = None -- 2.35.1