Message ID | 20221118003441.3980437-1-qiang1.zhang@intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp701061wrr; Thu, 17 Nov 2022 16:31:45 -0800 (PST) X-Google-Smtp-Source: AA0mqf6+8Qx/DKbfmXeQwnpmm7yd9y8bsZFRM+Joq3THY++O3I6GJZ5wg8HZh1raChc3ulPLNfPB X-Received: by 2002:a17:90b:2494:b0:211:906a:f8ef with SMTP id nt20-20020a17090b249400b00211906af8efmr5249052pjb.71.1668731505298; Thu, 17 Nov 2022 16:31:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668731505; cv=none; d=google.com; s=arc-20160816; b=mnnjHfkkIAI9A9VH9CP0lDL3A5IQ9Pk9KisMz/3nr1iqbfaHA0h0Ehinpi7dgLfs5C ETdJMUDpZz2B3pamk4T1+HZeMDQoVB39APkys6/FQBUEp4rhN4w5B+mSmOn6KFXvTqwy AImNfMntmZVqk72mTRvCB+g7IG8IMJU/K30M+7Y+gPx9WHm9xBUEXr5fD17RvVquA3Db f5xyMeuuRzFJOwbGFNtSohX0OZHe5+26RaGpDZd8YLxkPXsey4+lCZPi86EPyi2k8H3D L2kg8L5B+KIlYRA6YUbLtD6mbszHAGHO7yw0izrXcIvcNGPr2dKaXt8M29pWmvOBgXtz Mxzw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=7B9AsW7HSQOgI3ZwNT3qhvQPJMv2sIUsbpz69n2lFMY=; b=zbWj/IPpglGfHchiXeQGWwcKe1qu2H5diRMVcC+/M0O9+bpy3ZOmKAVu04XDUNYKIb /l85ZLMUVUIT1mTzoG8p7KsJ124ahcMfdwh6IEsji6/zIq/QuWXaQwbMECoxb0mtkKhu tRDsS/AM0dYb2KLWg0MLgKlUesJs4+C00o63XMWhtA1MjZ24L40tjZCZDWUW/jbuMDFS UkMYdsCDHjc6euo4kd3QW33loAww2iOqiUsw/DBcZPfmcp5DWxTJaz9D22HoP5bo3xNp kMPccjDbFb/WqQXlZV82oIeEz6ckJxOevD739Qqr8gcWzInRwJWJ/IIljdFE/cQRI+ff 3Rpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=iVV7oBaG; 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=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q16-20020a17090aa01000b00215e3cdaa32si1981355pjp.131.2022.11.17.16.31.32; Thu, 17 Nov 2022 16:31:45 -0800 (PST) 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=@intel.com header.s=Intel header.b=iVV7oBaG; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240016AbiKRA3H (ORCPT <rfc822;a1648639935@gmail.com> + 99 others); Thu, 17 Nov 2022 19:29:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240496AbiKRA2v (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 17 Nov 2022 19:28:51 -0500 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C263B70A1E; Thu, 17 Nov 2022 16:28:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668731327; x=1700267327; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=GmWPbqaX/RKcrEpQEce2Uo62AEQ/ECmJrcr2BAPP4A8=; b=iVV7oBaGYvAam04asypzkbV9TfS4KzilAvKhSlCR5HYMHFFamYfoq0b4 6XHU65iJtxNM4zmWXgX4CJ2C44guf6u7QsBvY8K+N4e+ShK7zMVmGVJ9P d1aryDNWJG1dmCb1m0IaLGqr3d1fkJs4iiqu+seyaDUOQmgJM3sXo+Im2 Iwk+Z4K+7lsjfA20K9ox0IG0hUdeSA14h2q7LlwfrJUfWh7Y5a6w9Jx07 PWMLAde+ZLq5MH2RJcc2nqwpgmtn/nUcOaT/3sgYSLXHSnqCJK8ltOUzb 719mkgK35+/bWkPGqJPKZ7HmhQ0zOH0BSILxNCvQwvN5gXSMnsWA/Zsdd A==; X-IronPort-AV: E=McAfee;i="6500,9779,10534"; a="311716973" X-IronPort-AV: E=Sophos;i="5.96,172,1665471600"; d="scan'208";a="311716973" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2022 16:28:47 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10534"; a="703539952" X-IronPort-AV: E=Sophos;i="5.96,172,1665471600"; d="scan'208";a="703539952" Received: from zq-optiplex-7090.bj.intel.com ([10.238.156.129]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2022 16:28:45 -0800 From: Zqiang <qiang1.zhang@intel.com> To: paulmck@kernel.org, akpm@linux-foundation.org, thunder.leizhen@huawei.com, frederic@kernel.org, joel@joelfernandes.org Cc: rcu@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] mm: Make vmalloc_dump_obj() call in clean context Date: Fri, 18 Nov 2022 08:34:41 +0800 Message-Id: <20221118003441.3980437-1-qiang1.zhang@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749791806930161429?= X-GMAIL-MSGID: =?utf-8?q?1749791806930161429?= |
Series |
[v3] mm: Make vmalloc_dump_obj() call in clean context
|
|
Commit Message
Zqiang
Nov. 18, 2022, 12:34 a.m. UTC
Currently, the mem_dump_obj() is invoked in call_rcu(), the
call_rcu() is maybe invoked in non-preemptive code segment,
for object allocated from vmalloc(), the following scenarios
may occur:
CPU 0
tasks context
spin_lock(&vmap_area_lock)
Interrupt context
call_rcu()
mem_dump_obj
vmalloc_dump_obj
spin_lock(&vmap_area_lock) <--deadlock
and for PREEMPT-RT kernel, the spinlock will convert to sleepable
lock, so the vmap_area_lock spinlock not allowed to get in non-preemptive
code segment. therefore, this commit make the vmalloc_dump_obj() call in
a clean context.
Signed-off-by: Zqiang <qiang1.zhang@intel.com>
---
v1->v2:
add IS_ENABLED(CONFIG_PREEMPT_RT) check.
v2->v3:
change commit message and add some comment.
mm/util.c | 4 +++-
mm/vmalloc.c | 25 +++++++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
Comments
Gently ping 😊 Thanks Zqiang >Currently, the mem_dump_obj() is invoked in call_rcu(), the >call_rcu() is maybe invoked in non-preemptive code segment, >for object allocated from vmalloc(), the following scenarios >may occur: > > CPU 0 >tasks context > spin_lock(&vmap_area_lock) > Interrupt context > call_rcu() > mem_dump_obj > vmalloc_dump_obj > spin_lock(&vmap_area_lock) <--deadlock > >and for PREEMPT-RT kernel, the spinlock will convert to sleepable >lock, so the vmap_area_lock spinlock not allowed to get in non-preemptive >code segment. therefore, this commit make the vmalloc_dump_obj() call in >a clean context. > >Signed-off-by: Zqiang <qiang1.zhang@intel.com> >--- >v1->v2: > add IS_ENABLED(CONFIG_PREEMPT_RT) check. > v2->v3: > change commit message and add some comment. > > mm/util.c | 4 +++- > mm/vmalloc.c | 25 +++++++++++++++++++++++++ > 2 files changed, 28 insertions(+), 1 deletion(-) > >diff --git a/mm/util.c b/mm/util.c >index 12984e76767e..2b0222a728cc 100644 >--- a/mm/util.c >+++ b/mm/util.c >@@ -1128,7 +1128,9 @@ void mem_dump_obj(void *object) > return; > > if (virt_addr_valid(object)) >- type = "non-slab/vmalloc memory"; >+ type = "non-slab memory"; >+ else if (is_vmalloc_addr(object)) >+ type = "vmalloc memory"; > else if (object == NULL) > type = "NULL pointer"; > else if (object == ZERO_SIZE_PTR) >diff --git a/mm/vmalloc.c b/mm/vmalloc.c >index ccaa461998f3..4351eafbe7ab 100644 >--- a/mm/vmalloc.c >+++ b/mm/vmalloc.c >@@ -4034,6 +4034,31 @@ bool vmalloc_dump_obj(void *object) > struct vm_struct *vm; > void *objp = (void *)PAGE_ALIGN((unsigned long)object); > >+ /* for non-vmalloc addr, return directly */ >+ if (!is_vmalloc_addr(objp)) >+ return false; >+ >+ /** >+ * for non-Preempt-RT kernel, return directly. otherwise not >+ * only needs to determine whether it is in the interrupt context >+ * (in_interrupt())to avoid deadlock, but also to avoid acquire >+ * vmap_area_lock spinlock in disables interrupts or preempts >+ * critical sections, because the vmap_area_lock spinlock convert >+ * to sleepable lock >+ */ >+ if (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible()) >+ return false; >+ >+ /** >+ * get here, for Preempt-RT kernel, it means that we are in >+ * preemptible context(preemptible() is true), it also means >+ * that the in_interrupt() will return false. >+ * for non-Preempt-RT kernel, only needs to determine whether >+ * it is in the interrupt context(in_interrupt()) to avoid deadlock >+ */ >+ if (in_interrupt()) >+ return false; >+ > vm = find_vm_area(objp); > if (!vm) > return false; >-- >2.25.1
On 2022/11/23 7:05, Zhang, Qiang1 wrote: > > Gently ping 😊 > > Thanks > Zqiang > >> Currently, the mem_dump_obj() is invoked in call_rcu(), the >> call_rcu() is maybe invoked in non-preemptive code segment, >> for object allocated from vmalloc(), the following scenarios >> may occur: >> >> CPU 0 >> tasks context >> spin_lock(&vmap_area_lock) >> Interrupt context >> call_rcu() >> mem_dump_obj >> vmalloc_dump_obj >> spin_lock(&vmap_area_lock) <--deadlock >> >> and for PREEMPT-RT kernel, the spinlock will convert to sleepable >> lock, so the vmap_area_lock spinlock not allowed to get in non-preemptive >> code segment. therefore, this commit make the vmalloc_dump_obj() call in >> a clean context. >> >> Signed-off-by: Zqiang <qiang1.zhang@intel.com> >> --- >> v1->v2: >> add IS_ENABLED(CONFIG_PREEMPT_RT) check. >> v2->v3: >> change commit message and add some comment. >> >> mm/util.c | 4 +++- >> mm/vmalloc.c | 25 +++++++++++++++++++++++++ >> 2 files changed, 28 insertions(+), 1 deletion(-) >> >> diff --git a/mm/util.c b/mm/util.c >> index 12984e76767e..2b0222a728cc 100644 >> --- a/mm/util.c >> +++ b/mm/util.c >> @@ -1128,7 +1128,9 @@ void mem_dump_obj(void *object) >> return; >> >> if (virt_addr_valid(object)) >> - type = "non-slab/vmalloc memory"; >> + type = "non-slab memory"; >> + else if (is_vmalloc_addr(object)) >> + type = "vmalloc memory"; >> else if (object == NULL) >> type = "NULL pointer"; >> else if (object == ZERO_SIZE_PTR) >> diff --git a/mm/vmalloc.c b/mm/vmalloc.c >> index ccaa461998f3..4351eafbe7ab 100644 >> --- a/mm/vmalloc.c >> +++ b/mm/vmalloc.c >> @@ -4034,6 +4034,31 @@ bool vmalloc_dump_obj(void *object) >> struct vm_struct *vm; >> void *objp = (void *)PAGE_ALIGN((unsigned long)object); >> >> + /* for non-vmalloc addr, return directly */ >> + if (!is_vmalloc_addr(objp)) >> + return false; >> + >> + /** >> + * for non-Preempt-RT kernel, return directly. otherwise not >> + * only needs to determine whether it is in the interrupt context >> + * (in_interrupt())to avoid deadlock, but also to avoid acquire >> + * vmap_area_lock spinlock in disables interrupts or preempts >> + * critical sections, because the vmap_area_lock spinlock convert >> + * to sleepable lock >> + */ >> + if (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible()) >> + return false; >> + >> + /** >> + * get here, for Preempt-RT kernel, it means that we are in >> + * preemptible context(preemptible() is true), it also means >> + * that the in_interrupt() will return false. >> + * for non-Preempt-RT kernel, only needs to determine whether >> + * it is in the interrupt context(in_interrupt()) to avoid deadlock >> + */ >> + if (in_interrupt()) >> + return false; We want mem_dump_obj() to work properly in the interrupt context. But with this if statement, it's impossible to work properly. Here's my test case: void *tst_p; void my_irqwork_handler(struct irq_work *work) { void *p = tst_p; printk("enter my_irqwork_handler: CPU=%d, locked=%d\n", smp_processor_id(), tst_is_locked()); mem_dump_obj(p); vfree(p); } static void test_mem_dump(void) { struct irq_work work = IRQ_WORK_INIT_HARD(my_irqwork_handler); tst_p = vmalloc(PAGE_SIZE); if (!tst_p) { printk("vmalloc failed\n"); return; } printk("enter test_mem_dump: CPU=%d\n", smp_processor_id()); //tst_lock(); irq_work_queue(&work); //tst_unlock(); printk("leave test_mem_dump: CPU=%d\n", smp_processor_id()); } Test result: [ 45.212941] enter test_mem_dump: CPU=0 [ 45.213280] enter my_irqwork_handler: CPU=0, locked=0 [ 45.213546] vmalloc memory [ 45.213996] leave test_mem_dump: CPU=0 >> + >> vm = find_vm_area(objp); >> if (!vm) >> return false; >> -- >> 2.25.1 >
On 2022/11/23 7:05, Zhang, Qiang1 wrote: > > Gently ping 😊 > > Thanks > Zqiang > >> Currently, the mem_dump_obj() is invoked in call_rcu(), the >> call_rcu() is maybe invoked in non-preemptive code segment, for >> object allocated from vmalloc(), the following scenarios may occur: >> >> CPU 0 >> tasks context >> spin_lock(&vmap_area_lock) >> Interrupt context >> call_rcu() >> mem_dump_obj >> vmalloc_dump_obj >> spin_lock(&vmap_area_lock) <--deadlock >> >> and for PREEMPT-RT kernel, the spinlock will convert to sleepable >> lock, so the vmap_area_lock spinlock not allowed to get in >> non-preemptive code segment. therefore, this commit make the >> vmalloc_dump_obj() call in a clean context. >> >> Signed-off-by: Zqiang <qiang1.zhang@intel.com> >> --- >> v1->v2: >> add IS_ENABLED(CONFIG_PREEMPT_RT) check. >> v2->v3: >> change commit message and add some comment. >> >> mm/util.c | 4 +++- >> mm/vmalloc.c | 25 +++++++++++++++++++++++++ >> 2 files changed, 28 insertions(+), 1 deletion(-) >> >> diff --git a/mm/util.c b/mm/util.c >> index 12984e76767e..2b0222a728cc 100644 >> --- a/mm/util.c >> +++ b/mm/util.c >> @@ -1128,7 +1128,9 @@ void mem_dump_obj(void *object) >> return; >> >> if (virt_addr_valid(object)) >> - type = "non-slab/vmalloc memory"; >> + type = "non-slab memory"; >> + else if (is_vmalloc_addr(object)) >> + type = "vmalloc memory"; >> else if (object == NULL) >> type = "NULL pointer"; >> else if (object == ZERO_SIZE_PTR) >> diff --git a/mm/vmalloc.c b/mm/vmalloc.c index >> ccaa461998f3..4351eafbe7ab 100644 >> --- a/mm/vmalloc.c >> +++ b/mm/vmalloc.c >> @@ -4034,6 +4034,31 @@ bool vmalloc_dump_obj(void *object) >> struct vm_struct *vm; >> void *objp = (void *)PAGE_ALIGN((unsigned long)object); >> >> + /* for non-vmalloc addr, return directly */ >> + if (!is_vmalloc_addr(objp)) >> + return false; >> + >> + /** >> + * for non-Preempt-RT kernel, return directly. otherwise not >> + * only needs to determine whether it is in the interrupt context >> + * (in_interrupt())to avoid deadlock, but also to avoid acquire >> + * vmap_area_lock spinlock in disables interrupts or preempts >> + * critical sections, because the vmap_area_lock spinlock convert >> + * to sleepable lock >> + */ >> + if (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible()) >> + return false; >> + >> + /** >> + * get here, for Preempt-RT kernel, it means that we are in >> + * preemptible context(preemptible() is true), it also means >> + * that the in_interrupt() will return false. >> + * for non-Preempt-RT kernel, only needs to determine whether >> + * it is in the interrupt context(in_interrupt()) to avoid deadlock >> + */ >> + if (in_interrupt()) >> + return false; > > >We want mem_dump_obj() to work properly in the interrupt context. But with this if statement, it's impossible to work properly. This is to avoid the following scenarios, because, call_rcu() can be invoked in hard irq or softirq context, so mem_dump_obj() not dump some details info. CPU 0 tasks context spin_lock(&vmap_area_lock) Interrupt or softirq context call_rcu() mem_dump_obj vmalloc_dump_obj spin_lock(&vmap_area_lock) <--deadlock because mem_dump_obj() only used by RCU, I'm not sure if this modification is appropriate, need to hear from Paul. Thanks Zqiang > >Here's my test case: >void *tst_p; > >void my_irqwork_handler(struct irq_work *work) { > void *p = tst_p; > > printk("enter my_irqwork_handler: CPU=%d, locked=%d\n", smp_processor_id(), tst_is_locked()); > mem_dump_obj(p); > vfree(p); >} > >static void test_mem_dump(void) >{ > struct irq_work work = IRQ_WORK_INIT_HARD(my_irqwork_handler); > > tst_p = vmalloc(PAGE_SIZE); > if (!tst_p) { > printk("vmalloc failed\n"); > return; > } > printk("enter test_mem_dump: CPU=%d\n", smp_processor_id()); > > //tst_lock(); > irq_work_queue(&work); > //tst_unlock(); > > printk("leave test_mem_dump: CPU=%d\n", smp_processor_id()); } > >Test result: >[ 45.212941] enter test_mem_dump: CPU=0 >[ 45.213280] enter my_irqwork_handler: CPU=0, locked=0 >[ 45.213546] vmalloc memory >[ 45.213996] leave test_mem_dump: CPU=0 > >> + >> vm = find_vm_area(objp); >> if (!vm) >> return false; >> -- >> 2.25.1 > > >-- >Regards, > Zhen Lei
On 2022/11/28 16:33, Zhang, Qiang1 wrote: > On 2022/11/23 7:05, Zhang, Qiang1 wrote: >> >> Gently ping 😊 >> >> Thanks >> Zqiang >> >>> Currently, the mem_dump_obj() is invoked in call_rcu(), the >>> call_rcu() is maybe invoked in non-preemptive code segment, for >>> object allocated from vmalloc(), the following scenarios may occur: >>> >>> CPU 0 >>> tasks context >>> spin_lock(&vmap_area_lock) >>> Interrupt context >>> call_rcu() >>> mem_dump_obj >>> vmalloc_dump_obj >>> spin_lock(&vmap_area_lock) <--deadlock >>> >>> and for PREEMPT-RT kernel, the spinlock will convert to sleepable >>> lock, so the vmap_area_lock spinlock not allowed to get in >>> non-preemptive code segment. therefore, this commit make the >>> vmalloc_dump_obj() call in a clean context. >>> >>> Signed-off-by: Zqiang <qiang1.zhang@intel.com> >>> --- >>> v1->v2: >>> add IS_ENABLED(CONFIG_PREEMPT_RT) check. >>> v2->v3: >>> change commit message and add some comment. >>> >>> mm/util.c | 4 +++- >>> mm/vmalloc.c | 25 +++++++++++++++++++++++++ >>> 2 files changed, 28 insertions(+), 1 deletion(-) >>> >>> diff --git a/mm/util.c b/mm/util.c >>> index 12984e76767e..2b0222a728cc 100644 >>> --- a/mm/util.c >>> +++ b/mm/util.c >>> @@ -1128,7 +1128,9 @@ void mem_dump_obj(void *object) >>> return; >>> >>> if (virt_addr_valid(object)) >>> - type = "non-slab/vmalloc memory"; >>> + type = "non-slab memory"; >>> + else if (is_vmalloc_addr(object)) >>> + type = "vmalloc memory"; >>> else if (object == NULL) >>> type = "NULL pointer"; >>> else if (object == ZERO_SIZE_PTR) >>> diff --git a/mm/vmalloc.c b/mm/vmalloc.c index >>> ccaa461998f3..4351eafbe7ab 100644 >>> --- a/mm/vmalloc.c >>> +++ b/mm/vmalloc.c >>> @@ -4034,6 +4034,31 @@ bool vmalloc_dump_obj(void *object) >>> struct vm_struct *vm; >>> void *objp = (void *)PAGE_ALIGN((unsigned long)object); >>> >>> + /* for non-vmalloc addr, return directly */ >>> + if (!is_vmalloc_addr(objp)) >>> + return false; >>> + >>> + /** >>> + * for non-Preempt-RT kernel, return directly. otherwise not >>> + * only needs to determine whether it is in the interrupt context >>> + * (in_interrupt())to avoid deadlock, but also to avoid acquire >>> + * vmap_area_lock spinlock in disables interrupts or preempts >>> + * critical sections, because the vmap_area_lock spinlock convert >>> + * to sleepable lock >>> + */ >>> + if (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible()) >>> + return false; >>> + >>> + /** >>> + * get here, for Preempt-RT kernel, it means that we are in >>> + * preemptible context(preemptible() is true), it also means >>> + * that the in_interrupt() will return false. >>> + * for non-Preempt-RT kernel, only needs to determine whether >>> + * it is in the interrupt context(in_interrupt()) to avoid deadlock >>> + */ >>> + if (in_interrupt()) >>> + return false; >> >> >> We want mem_dump_obj() to work properly in the interrupt context. But with this if statement, it's impossible to work properly. > > This is to avoid the following scenarios, because, call_rcu() can be invoked in hard irq or > softirq context, so mem_dump_obj() not dump some details info. OK. Sorry, I'm confusing your issue with what I'm doing right now. https://lkml.org/lkml/2022/11/16/913 I need "if (in_interrupt() && spin_is_locked(&vmap_area_lock))". So mem_dump_obj() can work well in interrupt, except the task was interrupted in the critical section of vmap_area_lock. > > CPU 0 > tasks context > spin_lock(&vmap_area_lock) > Interrupt or softirq context > call_rcu() > mem_dump_obj > vmalloc_dump_obj > spin_lock(&vmap_area_lock) <--deadlock > > because mem_dump_obj() only used by RCU, I'm not sure if this modification is appropriate, > need to hear from Paul. > > Thanks > Zqiang > > >> >> Here's my test case: >> void *tst_p; >> >> void my_irqwork_handler(struct irq_work *work) { >> void *p = tst_p; >> >> printk("enter my_irqwork_handler: CPU=%d, locked=%d\n", smp_processor_id(), tst_is_locked()); >> mem_dump_obj(p); >> vfree(p); >> } >> >> static void test_mem_dump(void) >> { >> struct irq_work work = IRQ_WORK_INIT_HARD(my_irqwork_handler); >> >> tst_p = vmalloc(PAGE_SIZE); >> if (!tst_p) { >> printk("vmalloc failed\n"); >> return; >> } >> printk("enter test_mem_dump: CPU=%d\n", smp_processor_id()); >> >> //tst_lock(); >> irq_work_queue(&work); >> //tst_unlock(); >> >> printk("leave test_mem_dump: CPU=%d\n", smp_processor_id()); } >> >> Test result: >> [ 45.212941] enter test_mem_dump: CPU=0 >> [ 45.213280] enter my_irqwork_handler: CPU=0, locked=0 >> [ 45.213546] vmalloc memory >> [ 45.213996] leave test_mem_dump: CPU=0 >> >>> + >>> vm = find_vm_area(objp); >>> if (!vm) >>> return false; >>> -- >>> 2.25.1 >> >> >> -- >> Regards, >> Zhen Lei
diff --git a/mm/util.c b/mm/util.c index 12984e76767e..2b0222a728cc 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1128,7 +1128,9 @@ void mem_dump_obj(void *object) return; if (virt_addr_valid(object)) - type = "non-slab/vmalloc memory"; + type = "non-slab memory"; + else if (is_vmalloc_addr(object)) + type = "vmalloc memory"; else if (object == NULL) type = "NULL pointer"; else if (object == ZERO_SIZE_PTR) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ccaa461998f3..4351eafbe7ab 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4034,6 +4034,31 @@ bool vmalloc_dump_obj(void *object) struct vm_struct *vm; void *objp = (void *)PAGE_ALIGN((unsigned long)object); + /* for non-vmalloc addr, return directly */ + if (!is_vmalloc_addr(objp)) + return false; + + /** + * for non-Preempt-RT kernel, return directly. otherwise not + * only needs to determine whether it is in the interrupt context + * (in_interrupt())to avoid deadlock, but also to avoid acquire + * vmap_area_lock spinlock in disables interrupts or preempts + * critical sections, because the vmap_area_lock spinlock convert + * to sleepable lock + */ + if (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible()) + return false; + + /** + * get here, for Preempt-RT kernel, it means that we are in + * preemptible context(preemptible() is true), it also means + * that the in_interrupt() will return false. + * for non-Preempt-RT kernel, only needs to determine whether + * it is in the interrupt context(in_interrupt()) to avoid deadlock + */ + if (in_interrupt()) + return false; + vm = find_vm_area(objp); if (!vm) return false;