From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D12B1C3ABBE for ; Thu, 8 May 2025 18:02:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BB7E6B009F; Thu, 8 May 2025 14:02:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 56D9A6B00A3; Thu, 8 May 2025 14:02:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E9276B00A4; Thu, 8 May 2025 14:02:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1C3796B00A3 for ; Thu, 8 May 2025 14:02:27 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 91CEF140DCE for ; Thu, 8 May 2025 18:02:27 +0000 (UTC) X-FDA: 83420510334.09.52181A9 Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) by imf06.hostedemail.com (Postfix) with ESMTP id 7081718000B for ; Thu, 8 May 2025 18:02:25 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G01hLgSY; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of urezki@gmail.com designates 209.85.167.47 as permitted sender) smtp.mailfrom=urezki@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746727345; a=rsa-sha256; cv=none; b=YxNw/hPrmcg7Gr/ceVh2SEh1ZsyZYa49Jq89Ts/K0MAkgZU1wnLDZ02AG6KaShBz4XiMNc RZY+AIc7DegevJm4JtHfwRNBk8JlsXr35Hp3iIIgWCtHhLVTZMn9Xp2rRJVOmu3nLwG3Zn 6hmXNwYriC9mDe6kv/BGm/TmDwv8VEw= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G01hLgSY; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of urezki@gmail.com designates 209.85.167.47 as permitted sender) smtp.mailfrom=urezki@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746727345; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aDqVjOVDSbrGoztVpPa8Pvok27RW/0weN3Ylqs8Sf+Q=; b=51+W9VIXw9VlpZjJWz/2klEk9DpYc14EGhJvvVWmoPH/MHX0HGTOelzxPtJHQsUYL09pix jB9gDn4YpkOloNp+nqNnTuUBYsPGekp//0+Xb6hO7g3+ES9kZ9v+/02xJdXJKBvz+Hkvfo CgLL14Zu4aqDt8VKMfZ/Jr1WEgCmilY= Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-54993c68ba0so1808229e87.2 for ; Thu, 08 May 2025 11:02:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1746727344; x=1747332144; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=aDqVjOVDSbrGoztVpPa8Pvok27RW/0weN3Ylqs8Sf+Q=; b=G01hLgSYqimNBYluj49OapLWOzriQAtLdSBdEnu+g3GcPb4YlwS9wscKBOCvErCNaH 12r2RKb8hNAhJMV8YKovnbdTSAizdISwXS4BICy0fwwwyi7XGDgdmxBd7rPrtLClrLqI f/EEOt7eDCofknCyNiW7PJMLsC0yhXMqYQp5+srD9a598MIVRZ++F8vR9JqciqfU1uAt y0hhEeVlejdiG1pcbddUJnFfvmyiZnzXo/xgEepvgtL2rNKHfJaHOs7aATUGbTEVw1Ir LMrxT0iHO6cHQXAO+6YhfxWY8tkmMvV15uhkHmJTFp2iRW9RMx8lFdhxdsIk+XU6U1ll 2UAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746727344; x=1747332144; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=aDqVjOVDSbrGoztVpPa8Pvok27RW/0weN3Ylqs8Sf+Q=; b=bPm9xRHJe8S14QBCc2fA/YQjAuWIrrVCvGP0ODL3VHpsv/OM18Z6v6kAVc3Fpj6KIf I5KpPLkTOqjV7x9AvjlIXbfe+LJRV4DwD/IJq92Bx1niuxj7jrRHfF65+7cC4sEJbG9N bV5/w+xVjyXLoNbdurZf9Fj6pEdwNQxuqsTgT9ZgVQ68PHu72iGnmVgz+AwJcWhx/ifv lqYXIwIw//REKy2KXszmJPZAhv9xRh5eMsVslSbaqE8R5vi0Dcbo5dzlkounWjcAu35/ YrynPx88nvPA/+fXL3S/pgysmhkHi6GBQ9Wgi7IP+HUKZazMZDm9AlAafPPDpLWZ/C/c vXxQ== X-Forwarded-Encrypted: i=1; AJvYcCXvAYhH8C/rt9xqGQmjIQwX5svqQ88lcxu/VPvJLKAkuh5+3IKknDUAEaOIDpttUdaKwIei7w2vhg==@kvack.org X-Gm-Message-State: AOJu0YwRpfIoEmYxNPL4sGyg7tRvbi+67qOuEbn0aARIUL8YIKObR+6V ICIaGiMcH2O7qqtsKlUSo5RBTM2IJOFDJXMRSRhum/KsBAKPsDQd X-Gm-Gg: ASbGncsd/k6+ffwGmr+8f3agf1utuYCOq6Vz7Piwlai3QEJ8nYaRt0KZWbbK/1cIxOd uU63uzNC6rXNkKHzC0P/rUBe/pw3c5oK8HgobEg7KLbL4z512hrgPo7FCuU0NaqXcrRmv9KImqj b1qjUSOT6p5A5LdK4+lBmnWyuPWsyrY35YLkSWKscToxTI4CLgRbDANNvogKBnFIKd6rIWXJjp5 AAC67fxH6koSe8O1KQV6W6MNklfWQ8rLkr1R8U4COtr2qpTPLTaQZsr5p6U2n2k8fE45Ao6Q4AD mDTFEfnKqOmuDKA6I3ZgFvJmQg0Z2cnN71tycLqPY7us7eKseflNojpYEn0qwJ2blMAt X-Google-Smtp-Source: AGHT+IH3iDwoZGOTMx60shDHlSpyXpsXIVI3ahXbHvH2slyiPOeQMmeYRrq2OdjvDtsUUeTqUyyOUw== X-Received: by 2002:a05:6512:3c9d:b0:54f:c3c3:beb8 with SMTP id 2adb3069b0e04-54fc67dab4amr50740e87.46.1746727342218; Thu, 08 May 2025 11:02:22 -0700 (PDT) Received: from pc636 (host-95-203-26-194.mobileonline.telia.com. [95.203.26.194]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-54fc64cc3f3sm33580e87.216.2025.05.08.11.02.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 May 2025 11:02:21 -0700 (PDT) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Thu, 8 May 2025 20:02:18 +0200 To: Jeongjun Park Cc: Jeongjun Park , akpm@linux-foundation.org, edumazet@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v6] mm/vmalloc: fix data race in show_numa_info() Message-ID: References: <20250508165620.15321-1-aha310510@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 7081718000B X-Stat-Signature: 1uuzjdeew9pbc3xdd96d6ocyuhedaya1 X-Rspam-User: X-HE-Tag: 1746727345-574311 X-HE-Meta: U2FsdGVkX18Ss/ZQ+lTuvIvV3drGB01BUqmfCzSHPGIJ+0r+6PuU+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IUF1qlVupsM3Cr1ppGD3+QcRhLte1P1nv3x0jumxgKr/BGs9mEqXM4roX+cISS8myZGVIKszE/kg+FT3l7gH6UDom3GfsZYLz5y5ggT2Jyh2OhurvGBvDK5mfWdve8XX+kjxSCMv9cDDi2rOAIrd+m/EVh7h1caZnu3ugfS0SB0Axh+4Osz7GU5YGcTBgHVid1urX7Y3oQ/gk3hqrh/JnbCaY+qCMwUMrPOmUoDMgyyyV8Wpd91EpkuvzHr2VjmNgrrPMfxkxSKFb0XPew7Z39dO5Sf9jdVdQ4iz3qwJbsDatcWMRX76Lr/CQpFv6B56080s0ZFNBMYvvFali60ShI71bDUTVSsKFxNNmck8gOS+SQsZqUOpDZJ+KimeJ90JTX79BfEQ+GrUK1Vkg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, May 08, 2025 at 07:48:55PM +0200, Uladzislau Rezki wrote: > On Fri, May 09, 2025 at 01:56:20AM +0900, Jeongjun Park wrote: > > The following data-race was found in show_numa_info(): > > > > ================================================================== > > BUG: KCSAN: data-race in vmalloc_info_show / vmalloc_info_show > > > > read to 0xffff88800971fe30 of 4 bytes by task 8289 on cpu 0: > > show_numa_info mm/vmalloc.c:4936 [inline] > > vmalloc_info_show+0x5a8/0x7e0 mm/vmalloc.c:5016 > > seq_read_iter+0x373/0xb40 fs/seq_file.c:230 > > proc_reg_read_iter+0x11e/0x170 fs/proc/inode.c:299 > > .... > > > > write to 0xffff88800971fe30 of 4 bytes by task 8287 on cpu 1: > > show_numa_info mm/vmalloc.c:4934 [inline] > > vmalloc_info_show+0x38f/0x7e0 mm/vmalloc.c:5016 > > seq_read_iter+0x373/0xb40 fs/seq_file.c:230 > > proc_reg_read_iter+0x11e/0x170 fs/proc/inode.c:299 > > .... > > > > value changed: 0x0000008f -> 0x00000000 > > ================================================================== > > > > According to this report,there is a read/write data-race because m->private > > is accessible to multiple CPUs. To fix this, instead of allocating the heap > > in proc_vmalloc_init() and passing the heap address to m->private, > > vmalloc_info_show() should allocate the heap. > > > > Fixes: a47a126ad5ea ("vmallocinfo: add NUMA information") > > Suggested-by: Eric Dumazet > > Suggested-by: Uladzislau Rezki (Sony) > > Suggested-by: Andrew Morton > > Signed-off-by: Jeongjun Park > > --- > > v6: Change CONFIG_NUMA to must be check before doing any work related to the counters array. > > - Link to v5: https://lore.kernel.org/all/20250508160800.12540-1-aha310510@gmail.com/ > > v5: Change heap to be allocated only when CONFIG_NUMA is enabled > > - Link to v4: https://lore.kernel.org/all/20250508065558.149091-1-aha310510@gmail.com/ > > v4: Change the way counters array heap is allocated, per Andrew Morton's suggestion. > > And fix it to call smp_rmb() in the correct location. > > - Link to v3: https://lore.kernel.org/all/20250507142552.9446-1-aha310510@gmail.com/ > > v3: Following Uladzislau Rezki's suggestion, we check v->flags beforehand > > to avoid printing uninitialized members of vm_struct. > > - Link to v2: https://lore.kernel.org/all/20250506082520.84153-1-aha310510@gmail.com/ > > v2: Refactoring some functions and fix patch as per Eric Dumazet suggestion > > - Link to v1: https://lore.kernel.org/all/20250505171948.24410-1-aha310510@gmail.com/ > > --- > > mm/vmalloc.c | 63 +++++++++++++++++++++++++++++----------------------- > > 1 file changed, 35 insertions(+), 28 deletions(-) > > > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > > index 3ed720a787ec..c1ea9713a1c0 100644 > > --- a/mm/vmalloc.c > > +++ b/mm/vmalloc.c > > @@ -3100,7 +3100,7 @@ static void clear_vm_uninitialized_flag(struct vm_struct *vm) > > /* > > * Before removing VM_UNINITIALIZED, > > * we should make sure that vm has proper values. > > - * Pair with smp_rmb() in show_numa_info(). > > + * Pair with smp_rmb() in vread_iter() and vmalloc_info_show(). > > */ > > smp_wmb(); > > vm->flags &= ~VM_UNINITIALIZED; > > @@ -4914,28 +4914,29 @@ bool vmalloc_dump_obj(void *object) > > #endif > > > > #ifdef CONFIG_PROC_FS > > -static void show_numa_info(struct seq_file *m, struct vm_struct *v) > > -{ > > - if (IS_ENABLED(CONFIG_NUMA)) { > > - unsigned int nr, *counters = m->private; > > - unsigned int step = 1U << vm_area_page_order(v); > > > > - if (!counters) > > - return; > > +/* > > + * Print number of pages allocated on each memory node. > > + * > > + * This function can only be called if CONFIG_NUMA is enabled > > + * and VM_UNINITIALIZED bit in v->flags is disabled. > > + */ > > +static void show_numa_info(struct seq_file *m, struct vm_struct *v, > > + unsigned int *counters) > > +{ > > + unsigned int nr; > > + unsigned int step = 1U << vm_area_page_order(v); > > > > - if (v->flags & VM_UNINITIALIZED) > > - return; > > - /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ > > - smp_rmb(); > > + if (!counters) > > + return; > > > > - memset(counters, 0, nr_node_ids * sizeof(unsigned int)); > > + memset(counters, 0, nr_node_ids * sizeof(unsigned int)); > > > > - for (nr = 0; nr < v->nr_pages; nr += step) > > - counters[page_to_nid(v->pages[nr])] += step; > > - for_each_node_state(nr, N_HIGH_MEMORY) > > - if (counters[nr]) > > - seq_printf(m, " N%u=%u", nr, counters[nr]); > > - } > > + for (nr = 0; nr < v->nr_pages; nr += step) > > + counters[page_to_nid(v->pages[nr])] += step; > > + for_each_node_state(nr, N_HIGH_MEMORY) > > + if (counters[nr]) > > + seq_printf(m, " N%u=%u", nr, counters[nr]); > > } > > > > static void show_purge_info(struct seq_file *m) > > @@ -4962,8 +4963,12 @@ static int vmalloc_info_show(struct seq_file *m, void *p) > > struct vmap_node *vn; > > struct vmap_area *va; > > struct vm_struct *v; > > + unsigned int *counters; > > int i; > > > > + if (IS_ENABLED(CONFIG_NUMA)) > > + counters = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL); > > + > > for (i = 0; i < nr_vmap_nodes; i++) { > > vn = &vmap_nodes[i]; > > > > @@ -4979,6 +4984,11 @@ static int vmalloc_info_show(struct seq_file *m, void *p) > > } > > > > v = va->vm; > > + if (v->flags & VM_UNINITIALIZED) > > + continue; > > + > > + /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ > > + smp_rmb(); > > > > seq_printf(m, "0x%pK-0x%pK %7ld", > > v->addr, v->addr + v->size, v->size); > > @@ -5013,7 +5023,9 @@ static int vmalloc_info_show(struct seq_file *m, void *p) > > if (is_vmalloc_addr(v->pages)) > > seq_puts(m, " vpages"); > > > > - show_numa_info(m, v); > > + if (IS_ENABLED(CONFIG_NUMA)) > > + show_numa_info(m, v, counters); > > + > > seq_putc(m, '\n'); > > } > > spin_unlock(&vn->busy.lock); > > @@ -5023,19 +5035,14 @@ static int vmalloc_info_show(struct seq_file *m, void *p) > > * As a final step, dump "unpurged" areas. > > */ > > show_purge_info(m); > > + if (IS_ENABLED(CONFIG_NUMA)) > > + kfree(counters); > > return 0; > > } > > > > static int __init proc_vmalloc_init(void) > > { > > - void *priv_data = NULL; > > - > > - if (IS_ENABLED(CONFIG_NUMA)) > > - priv_data = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL); > > - > > - proc_create_single_data("vmallocinfo", > > - 0400, NULL, vmalloc_info_show, priv_data); > > - > > + proc_create_single("vmallocinfo", 0400, NULL, vmalloc_info_show); > > return 0; > > } > > module_init(proc_vmalloc_init); > > -- > LGTM: > > Reviewed-by: "Uladzislau Rezki (Sony)" > > Thank you! > Also one thing i have just noticed. The "Fixes tag" should be updated to this: Fixes: 8e1d743f2c26 ("mm: vmalloc: support multiple nodes in vmallocinfo") the below one should not be blamed: Fixes: a47a126ad5ea ("vmallocinfo: add NUMA information") Thanks! -- Uladzislau Rezki