From patchwork Thu Aug 3 10:17:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Leizhen (ThunderTown)" X-Patchwork-Id: 130475 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp1066619vqx; Thu, 3 Aug 2023 04:06:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlFZte5EDBLUJPtIWvMt1yY9p3BSUqqKgLMUKcbgob8bpiWfPEpUUGGiGU+JFXEl2mcrNwqm X-Received: by 2002:a17:906:18f:b0:994:4ebe:7a6d with SMTP id 15-20020a170906018f00b009944ebe7a6dmr8141861ejb.19.1691060787612; Thu, 03 Aug 2023 04:06:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691060787; cv=none; d=google.com; s=arc-20160816; b=PZ7Q9PZKDgBFA/fXBuqyVIK6e+AQen/Elvj0whCfYkpfXsSo03ceX9NBrPzTPyqneG rfFseRbJV+1Tpuqah4unFTG8AirS4hDTqtKe//CTa5HzgfVsCwk7zhc+AL5KarddQeEx i3IOhaw8H06XzsNnX9710Hzzlmjfqkz+Lo7h0VNqTVRT8TIAqpjq7k9g17+duZlS7P7+ tCmQnuLTvsfcGMaaLQJ2VjJjTSX1o2XKIXqhLnTwIa7U9xpcvsZJf4vdQYEVmMOwamCb iduTBaBhquqIt0v9UrBsG93ufiC/Nb9hfn5KP/i1hQD1riLWipEki/u6cBAtK2Zh3Bs3 I2Xw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=qH2Y3/xOMWgj3UzJQddM1wfI9s1KetFUbEB44vBr0Q0=; fh=ixpq6thPVOOZuciljZRZ5gp6/ROsGXjbN7hNRM43S+c=; b=wStTvJMiDCFQ+rH83Rnhlk+knQNdw4bmWVMgx/RGODgIvsdyKGwDuRfuJ65D1TISAb uXpEtFTk2JGYPgGp2LT6LyYLEzwlFSi2OFaf89RjwF2DrP77mnSqqiFqTnnc/Rxom787 dfHU5snVMUdMDukzCfq+AdKgK+iCcEHANcvu0humQGFHLq8dzzoNpk18DUPDNyQ/noMV CDkk0MVK8A5fTMb8fLciiNA4zbYON8LSSBCiY6YDAWzNZzDi5AnQ7V7azW4Y0+eTzsjv OlTCaF/PPdv1KOsoRS2c4R/Xf5AmZnPAIO8jIQme3WcGNEzj6wrAHdhQeaP47byXA0p/ U3Yg== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w8-20020a1709067c8800b0099351214a88si359550ejo.637.2023.08.03.04.05.49; Thu, 03 Aug 2023 04:06:27 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235405AbjHCKTl (ORCPT + 99 others); Thu, 3 Aug 2023 06:19:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235310AbjHCKTM (ORCPT ); Thu, 3 Aug 2023 06:19:12 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 559083A90; Thu, 3 Aug 2023 03:19:10 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4RGlF63xxfz4f4TJf; Thu, 3 Aug 2023 18:19:06 +0800 (CST) Received: from huaweicloud.com (unknown [10.174.178.55]) by APP4 (Coremail) with SMTP id gCh0CgD3mp4Uf8tk4+TaPQ--.35755S5; Thu, 03 Aug 2023 18:19:07 +0800 (CST) From: thunder.leizhen@huaweicloud.com To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, "Paul E . McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , rcu@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Zhen Lei Subject: [PATCH v5 1/3] mm: Remove kmem_valid_obj() Date: Thu, 3 Aug 2023 18:17:52 +0800 Message-Id: <20230803101754.1149-2-thunder.leizhen@huaweicloud.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230803101754.1149-1-thunder.leizhen@huaweicloud.com> References: <20230803101754.1149-1-thunder.leizhen@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3mp4Uf8tk4+TaPQ--.35755S5 X-Coremail-Antispam: 1UD129KBjvJXoWxZr1ftry5Jr4fXr1fKF1rZwb_yoWrCrWkpw nxGr9xJFW8Jr1xGrsrJF1kurW5Zr4kuF17Ca9aqw18Ar1UXrs7ur1kG3s2qF98GFW8XF10 ya1vkF43uryUArDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBKb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6r1S6rWUM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lw4CEc2x0rVAKj4 xxMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_ Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVW8ZVWrXwCIc40Y0x 0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8 JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIx AIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU1D73DUUUUU= = X-CM-SenderInfo: hwkx0vthuozvpl2kv046kxt4xhlfz01xgou0bp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,MAY_BE_FORGED, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773205756476488584 X-GMAIL-MSGID: 1773205756476488584 From: Zhen Lei Function kmem_dump_obj() will splat if passed a pointer to a non-slab object. So no one will call it directly. It is always necessary to call kmem_valid_obj() first to determine whether the passed pointer to a valid slab object. Then merging kmem_valid_obj() into kmem_dump_obj() will make the code more concise. So convert kmem_dump_obj() to work the same way as vmalloc_dump_obj(). After this, no one calls kmem_valid_obj() anymore, and it can be safely removed. Suggested-by: Matthew Wilcox Signed-off-by: Zhen Lei Reviewed-by: Matthew Wilcox (Oracle) Acked-by: Vlastimil Babka --- include/linux/slab.h | 5 +++-- mm/slab_common.c | 41 +++++++++++------------------------------ mm/util.c | 4 +--- 3 files changed, 15 insertions(+), 35 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 848c7c82ad5ad0b..d8ed2e810ec4448 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -244,8 +244,9 @@ DEFINE_FREE(kfree, void *, if (_T) kfree(_T)) size_t ksize(const void *objp); #ifdef CONFIG_PRINTK -bool kmem_valid_obj(void *object); -void kmem_dump_obj(void *object); +bool kmem_dump_obj(void *object); +#else +static inline bool kmem_dump_obj(void *object) { return false; } #endif /* diff --git a/mm/slab_common.c b/mm/slab_common.c index d1555ea2981ac51..ee6ed6dd7ba9fa5 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -528,26 +528,6 @@ bool slab_is_available(void) } #ifdef CONFIG_PRINTK -/** - * kmem_valid_obj - does the pointer reference a valid slab object? - * @object: pointer to query. - * - * Return: %true if the pointer is to a not-yet-freed object from - * kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer - * is to an already-freed object, and %false otherwise. - */ -bool kmem_valid_obj(void *object) -{ - struct folio *folio; - - /* Some arches consider ZERO_SIZE_PTR to be a valid address. */ - if (object < (void *)PAGE_SIZE || !virt_addr_valid(object)) - return false; - folio = virt_to_folio(object); - return folio_test_slab(folio); -} -EXPORT_SYMBOL_GPL(kmem_valid_obj); - static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab) { if (__kfence_obj_info(kpp, object, slab)) @@ -566,11 +546,11 @@ static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab * * and, if available, the slab name, return address, and stack trace from * the allocation and last free path of that object. * - * This function will splat if passed a pointer to a non-slab object. - * If you are not sure what type of object you have, you should instead - * use mem_dump_obj(). + * Return: %true if the pointer is to a not-yet-freed object from + * kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer + * is to an already-freed object, and %false otherwise. */ -void kmem_dump_obj(void *object) +bool kmem_dump_obj(void *object) { char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc"; int i; @@ -578,13 +558,13 @@ void kmem_dump_obj(void *object) unsigned long ptroffset; struct kmem_obj_info kp = { }; - if (WARN_ON_ONCE(!virt_addr_valid(object))) - return; + /* Some arches consider ZERO_SIZE_PTR to be a valid address. */ + if (object < (void *)PAGE_SIZE || !virt_addr_valid(object)) + return false; slab = virt_to_slab(object); - if (WARN_ON_ONCE(!slab)) { - pr_cont(" non-slab memory.\n"); - return; - } + if (!slab) + return false; + kmem_obj_info(&kp, object, slab); if (kp.kp_slab_cache) pr_cont(" slab%s %s", cp, kp.kp_slab_cache->name); @@ -621,6 +601,7 @@ void kmem_dump_obj(void *object) pr_info(" %pS\n", kp.kp_free_stack[i]); } + return true; } EXPORT_SYMBOL_GPL(kmem_dump_obj); #endif diff --git a/mm/util.c b/mm/util.c index dd12b9531ac4cad..ddfbb22dc1876d3 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1063,10 +1063,8 @@ void mem_dump_obj(void *object) { const char *type; - if (kmem_valid_obj(object)) { - kmem_dump_obj(object); + if (kmem_dump_obj(object)) return; - } if (vmalloc_dump_obj(object)) return; From patchwork Thu Aug 3 10:17:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Leizhen (ThunderTown)" X-Patchwork-Id: 130491 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp1082040vqx; Thu, 3 Aug 2023 04:33:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlH5K3AQJa4Emk7Xc8N/CotMbjajcyC735GEj6Tjohkqc+31WCHhtXLBUFZ5xAi8ogBTNsxf X-Received: by 2002:aa7:c582:0:b0:521:d2ab:e4df with SMTP id g2-20020aa7c582000000b00521d2abe4dfmr10907700edq.19.1691062438804; Thu, 03 Aug 2023 04:33:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691062438; cv=none; d=google.com; s=arc-20160816; b=Sw/2KFELKm+gBeF5yVLo8AtQVTEqSF8drBRrf7noL5FIq4FoyRVOy0eCSAj1eKVbEU lOr+dWmOUn2IahcH/AMGbVyAdXazsTKCYzSWq8NPOLhZJKQncYk7sMiK0ZiCKsP7VOMr iVs+6eLnNfjX52BWeTC6ZljrJcRVlvDH412CGDPNSlzWXeeGLktIAD8IASnbycnzsO4C EJN9JS7XDyR9bCvVpu19+msVWiPt7Vyr3MkkX2gFShHJmOIB7w2a7wfEFjrNO0uoTh0K OaBwBEqFB5dW1ls9wNiFefD1rmHvuEcu2ZUtWmqswDWtB1AbPnvqVQF1PIrqWcyoPdkv Lh/g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=njcyC+Y7DyPrI2hfeo2ZY81l5420qr70wW3AZzYp6G8=; fh=ixpq6thPVOOZuciljZRZ5gp6/ROsGXjbN7hNRM43S+c=; b=ZMwfVdx281BdDn+VScTHCvC4FAD7CrUIwVQ0zi6HgHUgYYXgHv25vXqYaUmEgftn8A CWJjGt5m069Md6sX9KhwfFE2+KF2aa9y/8PhqGdiOf4O+s37D0VyRPMc9OtvUI7lZBfl Df8YdhaxA56IG48Nrjwogm8Stx2jvb9Zn8piwl2EcNAAawG9D2PdKP3/KuDtdbX6QhxJ 2pXcCD91+qVk6u5uDoBV0SUq+gnL6N7tYSfHURQE822L8jR4n/pcTypkbt8F20YU/gt5 Z1ucZo/j3MNbQkwyor5rbGXeCvZYixMJqrSZ2nbuBONUHpaPvcALYvfo7hJow+fw3Kgq A76Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m17-20020aa7c491000000b005230a2b0befsi1408652edq.153.2023.08.03.04.33.27; Thu, 03 Aug 2023 04:33:58 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235424AbjHCKTt (ORCPT + 99 others); Thu, 3 Aug 2023 06:19:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235319AbjHCKTO (ORCPT ); Thu, 3 Aug 2023 06:19:14 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB21A35BF; Thu, 3 Aug 2023 03:19:11 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4RGlF81RNpz4f41m2; Thu, 3 Aug 2023 18:19:08 +0800 (CST) Received: from huaweicloud.com (unknown [10.174.178.55]) by APP4 (Coremail) with SMTP id gCh0CgD3mp4Uf8tk4+TaPQ--.35755S6; Thu, 03 Aug 2023 18:19:08 +0800 (CST) From: thunder.leizhen@huaweicloud.com To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, "Paul E . McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , rcu@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Zhen Lei Subject: [PATCH v5 2/3] rcu: Dump memory object info if callback function is invalid Date: Thu, 3 Aug 2023 18:17:53 +0800 Message-Id: <20230803101754.1149-3-thunder.leizhen@huaweicloud.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230803101754.1149-1-thunder.leizhen@huaweicloud.com> References: <20230803101754.1149-1-thunder.leizhen@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3mp4Uf8tk4+TaPQ--.35755S6 X-Coremail-Antispam: 1UD129KBjvJXoWxtF47WrW7Kw1fury3tFWrZrb_yoW7GrW3pr ykuFy7Kw4kXFyrtay7Zw18WrWUA39Ygay3Ka95Crn3Cw4Ykr10gFyqyF12qrWYqF1rK34a qF1YqF43tr40ywUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPjb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6r1S6rWUM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lw4CEc2x0rVAKj4 xxMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_ Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVW8ZVWrXwCIc40Y0x 0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWx JVW8Jr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMI IF0xvEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvjxUwzuAUUUU U X-CM-SenderInfo: hwkx0vthuozvpl2kv046kxt4xhlfz01xgou0bp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,MAY_BE_FORGED, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773088810889479563 X-GMAIL-MSGID: 1773207487751380428 From: Zhen Lei When a structure containing an RCU callback rhp is (incorrectly) freed and reallocated after rhp is passed to call_rcu(), it is not unusual for rhp->func to be set to NULL. This defeats the debugging prints used by __call_rcu_common() in kernels built with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y, which expect to identify the offending code using the identity of this function. And in kernels build without CONFIG_DEBUG_OBJECTS_RCU_HEAD=y, things are even worse, as can be seen from this splat: Unable to handle kernel NULL pointer dereference at virtual address 0 ... ... PC is at 0x0 LR is at rcu_do_batch+0x1c0/0x3b8 ... ... (rcu_do_batch) from (rcu_core+0x1d4/0x284) (rcu_core) from (__do_softirq+0x24c/0x344) (__do_softirq) from (__irq_exit_rcu+0x64/0x108) (__irq_exit_rcu) from (irq_exit+0x8/0x10) (irq_exit) from (__handle_domain_irq+0x74/0x9c) (__handle_domain_irq) from (gic_handle_irq+0x8c/0x98) (gic_handle_irq) from (__irq_svc+0x5c/0x94) (__irq_svc) from (arch_cpu_idle+0x20/0x3c) (arch_cpu_idle) from (default_idle_call+0x4c/0x78) (default_idle_call) from (do_idle+0xf8/0x150) (do_idle) from (cpu_startup_entry+0x18/0x20) (cpu_startup_entry) from (0xc01530) This commit therefore adds calls to mem_dump_obj(rhp) to output some information, for example: slab kmalloc-256 start ffff410c45019900 pointer offset 0 size 256 This provides the rough size of the memory block and the offset of the rcu_head structure, which as least provides at least a few clues to help locate the problem. If the problem is reproducible, additional slab debugging can be enabled, for example, CONFIG_DEBUG_SLAB=y, which can provide significantly more information. Signed-off-by: Zhen Lei --- kernel/rcu/rcu.h | 7 +++++++ kernel/rcu/srcutiny.c | 1 + kernel/rcu/srcutree.c | 1 + kernel/rcu/tasks.h | 1 + kernel/rcu/tiny.c | 1 + kernel/rcu/tree.c | 1 + 6 files changed, 12 insertions(+) diff --git a/kernel/rcu/rcu.h b/kernel/rcu/rcu.h index d1dcb09750efbd6..bc81582238b9846 100644 --- a/kernel/rcu/rcu.h +++ b/kernel/rcu/rcu.h @@ -10,6 +10,7 @@ #ifndef __LINUX_RCU_H #define __LINUX_RCU_H +#include #include /* @@ -248,6 +249,12 @@ static inline void debug_rcu_head_unqueue(struct rcu_head *head) } #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ +static inline void debug_rcu_head_callback(struct rcu_head *rhp) +{ + if (unlikely(!rhp->func)) + kmem_dump_obj(rhp); +} + extern int rcu_cpu_stall_suppress_at_boot; static inline bool rcu_stall_is_suppressed_at_boot(void) diff --git a/kernel/rcu/srcutiny.c b/kernel/rcu/srcutiny.c index 336af24e0fe358a..c38e5933a5d6937 100644 --- a/kernel/rcu/srcutiny.c +++ b/kernel/rcu/srcutiny.c @@ -138,6 +138,7 @@ void srcu_drive_gp(struct work_struct *wp) while (lh) { rhp = lh; lh = lh->next; + debug_rcu_head_callback(rhp); local_bh_disable(); rhp->func(rhp); local_bh_enable(); diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c index f1a905200fc2f79..833a8f848a90ae6 100644 --- a/kernel/rcu/srcutree.c +++ b/kernel/rcu/srcutree.c @@ -1710,6 +1710,7 @@ static void srcu_invoke_callbacks(struct work_struct *work) rhp = rcu_cblist_dequeue(&ready_cbs); for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) { debug_rcu_head_unqueue(rhp); + debug_rcu_head_callback(rhp); local_bh_disable(); rhp->func(rhp); local_bh_enable(); diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h index 7294be62727b12c..148ac6a464bfb12 100644 --- a/kernel/rcu/tasks.h +++ b/kernel/rcu/tasks.h @@ -538,6 +538,7 @@ static void rcu_tasks_invoke_cbs(struct rcu_tasks *rtp, struct rcu_tasks_percpu raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags); len = rcl.len; for (rhp = rcu_cblist_dequeue(&rcl); rhp; rhp = rcu_cblist_dequeue(&rcl)) { + debug_rcu_head_callback(rhp); local_bh_disable(); rhp->func(rhp); local_bh_enable(); diff --git a/kernel/rcu/tiny.c b/kernel/rcu/tiny.c index 42f7589e51e09e7..fec804b7908032d 100644 --- a/kernel/rcu/tiny.c +++ b/kernel/rcu/tiny.c @@ -97,6 +97,7 @@ static inline bool rcu_reclaim_tiny(struct rcu_head *head) trace_rcu_invoke_callback("", head); f = head->func; + debug_rcu_head_callback(head); WRITE_ONCE(head->func, (rcu_callback_t)0L); f(head); rcu_lock_release(&rcu_callback_map); diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 7c79480bfaa04e4..927c5ba0ae42269 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -2135,6 +2135,7 @@ static void rcu_do_batch(struct rcu_data *rdp) trace_rcu_invoke_callback(rcu_state.name, rhp); f = rhp->func; + debug_rcu_head_callback(rhp); WRITE_ONCE(rhp->func, (rcu_callback_t)0L); f(rhp); From patchwork Thu Aug 3 10:17:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Leizhen (ThunderTown)" X-Patchwork-Id: 130468 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp1065139vqx; Thu, 3 Aug 2023 04:04:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlGz+z6WQ6U68ZrGTM740S4b4og04PPrH0L8eXFu0Jo8TcGFLJ8Wee8wusdL86XJtEVLeEcI X-Received: by 2002:a17:903:41d1:b0:1b8:936f:c346 with SMTP id u17-20020a17090341d100b001b8936fc346mr22704576ple.30.1691060647747; Thu, 03 Aug 2023 04:04:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691060647; cv=none; d=google.com; s=arc-20160816; b=0xcsazYhvyZSuf+Ce9aUx02MCXVqVvrXtByT7iBJX1eK0CrSj2ZKklZyAVBvM8feKt AF2+Xlk9vFydW82zOo4tcpV9rNPBvc0DWxOiqGMolui7K/v+UFYQoMptrT9IcGUkFmKo ekfQexHsqmtEOi9ThheoRCPoEClnoKaDq3CFfERXCE9yYbpjU52V57UuxYTOZoeZzVUv jzCdvCNdT8ZXZIOau298mVVhcjkwAc29W7Y1oX9Qdh+4TJxqCrBhGpqOq3kb6DS4Q6n5 2qYFNCFt3kfShyrC8AgSJEH6mNkMC5BUXl5Hp7QDY32PIoC1tXJ9x5d1cmt7FBTMHyro Sb+Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=NMVaGcW27s8Y6PJmZS1W6GaXqTOirqWruPixr0mtmy0=; fh=ixpq6thPVOOZuciljZRZ5gp6/ROsGXjbN7hNRM43S+c=; b=FeR5CPY4ic7MvRav0IMrotGsyeGbS0bM5B5fbFI6uJRiQ6KdqQF5g6kDroEOQUTuHW f31ax1aO3UnVmEp949sN1FzEbq8g2Jm81RRJYeFzx4eqzcbt28wi+GTYmdE+K28Fr+kd +XTrWEN84vC8Z6R5cAWhLlCnhwx/cgLxVIltHRhrCNo3Q26qNYRRpVwr8L3EiN6VEsu7 yXOwiOK9xU04gzoWtq7zOn0c+LRzr3ss636fvByfLffNzfiDpONfT+6CzqHGd4AY0RLI 3MBsfAImaTu6WZk163TODq19B5yEmx16g288n8rtUH3dYsKzX3dpP/5jgHzdMtbwFu1V hLHw== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u17-20020a170902e5d100b001bbb56b34e8si13057320plf.432.2023.08.03.04.03.44; Thu, 03 Aug 2023 04:04:07 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235418AbjHCKTo (ORCPT + 99 others); Thu, 3 Aug 2023 06:19:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235320AbjHCKTO (ORCPT ); Thu, 3 Aug 2023 06:19:14 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AB3A35BE; Thu, 3 Aug 2023 03:19:12 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4RGlF92nt6z4f3yDV; Thu, 3 Aug 2023 18:19:09 +0800 (CST) Received: from huaweicloud.com (unknown [10.174.178.55]) by APP4 (Coremail) with SMTP id gCh0CgD3mp4Uf8tk4+TaPQ--.35755S7; Thu, 03 Aug 2023 18:19:09 +0800 (CST) From: thunder.leizhen@huaweicloud.com To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, "Paul E . McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , rcu@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Zhen Lei Subject: [PATCH v5 3/3] mm: Dump the memory of slab object in kmem_dump_obj() Date: Thu, 3 Aug 2023 18:17:54 +0800 Message-Id: <20230803101754.1149-4-thunder.leizhen@huaweicloud.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230803101754.1149-1-thunder.leizhen@huaweicloud.com> References: <20230803101754.1149-1-thunder.leizhen@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3mp4Uf8tk4+TaPQ--.35755S7 X-Coremail-Antispam: 1UD129KBjvJXoW7Ary7WFWDKrWxKF1rKFW8tFb_yoW8Kr13pr 9xGr1UK3yxArnrCrs3J3WkXF45J3ykC3WkJayavr17Zw1UJr48uF97tF92kFWUGF1Fqa9F yrZ0vFnIgryUJaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPjb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6r1S6rWUM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUWw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lw4CEc2x0rVAKj4 xxMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_ Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVW8ZVWrXwCIc40Y0x 0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWx JVW8Jr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMI IF0xvEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvjxUFR6zUUUU U X-CM-SenderInfo: hwkx0vthuozvpl2kv046kxt4xhlfz01xgou0bp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,MAY_BE_FORGED, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773205609865766170 X-GMAIL-MSGID: 1773205609865766170 From: Zhen Lei The contents of the slab object may contain some magic words and other useful information that may be helpful in locating problems such as memory corruption and use-after-free. To avoid print flooding, dump up to "16 * sizeof(int) = 64" bytes centered on argument 'ojbect'. For example: slab kmalloc-64 start ffff4043802d8b40 pointer offset 24 size 64 [8b40]: 12345678 00000000 8092d000 ffff8000 [8b50]: 00101000 00000000 8199ee00 ffff4043 [8b60]: 00000000 00000000 00000000 00000100 [8b70]: 00000000 9abcdef0 a8744de4 ffffc7fe Signed-off-by: Zhen Lei --- mm/slab_common.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index ee6ed6dd7ba9fa5..0232de9a3b29cf5 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -553,7 +553,7 @@ static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab * bool kmem_dump_obj(void *object) { char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc"; - int i; + int i, object_size = 0; struct slab *slab; unsigned long ptroffset; struct kmem_obj_info kp = { }; @@ -580,12 +580,36 @@ bool kmem_dump_obj(void *object) ptroffset = ((char *)object - (char *)kp.kp_objp) - kp.kp_data_offset; pr_cont(" pointer offset %lu", ptroffset); } - if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) - pr_cont(" size %u", kp.kp_slab_cache->object_size); + if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) { + object_size = kp.kp_slab_cache->object_size; + pr_cont(" size %u", object_size); + } if (kp.kp_ret) pr_cont(" allocated at %pS\n", kp.kp_ret); else pr_cont("\n"); + + /* Dump a small piece of memory centered on 'object' */ + if (kp.kp_objp && object_size) { + int *p = object, n = 16; + + p += n / 2; + if ((void *)p > kp.kp_objp + object_size) + p = kp.kp_objp + object_size; + + p -= n; + if ((void *)p < kp.kp_objp) + p = kp.kp_objp; + + n = min_t(int, object_size / sizeof(int), n); + for (i = 0; i < n; i++, p++) { + if (i % 4 == 0) + pr_info("[%04lx]:", 0xffff & (unsigned long)p); + pr_cont(" %08x", *p); + } + pr_cont("\n"); + } + for (i = 0; i < ARRAY_SIZE(kp.kp_stack); i++) { if (!kp.kp_stack[i]) break;