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 1AF0CC77B7C for ; Wed, 2 Jul 2025 15:01:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84F0C6B008C; Wed, 2 Jul 2025 11:01:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D8806B00C7; Wed, 2 Jul 2025 11:01:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A0A26B00C8; Wed, 2 Jul 2025 11:01:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 555256B008C for ; Wed, 2 Jul 2025 11:01:07 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DBF0D1603A8 for ; Wed, 2 Jul 2025 15:01:06 +0000 (UTC) X-FDA: 83619637332.04.02659B0 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) by imf19.hostedemail.com (Postfix) with ESMTP id 51D0C1A0021 for ; Wed, 2 Jul 2025 15:01:04 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mkXsZ6zG; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of urezki@gmail.com designates 209.85.167.53 as permitted sender) smtp.mailfrom=urezki@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751468464; a=rsa-sha256; cv=none; b=OwGG3gOXKpMdGagz9ciWtXft245IofsIoKQN3En9EMVulobPDC7qEaF6N9QR+eSkX0yj3L P7BD2PtqnXWpDC9Q4OKVVunKDDRRQ8z5KYWaV38D2X5H3djuAQ7yz1UMUD+GQ80L4dXqqa BNuvpjVZMftHpZOHWbtCjqm0PMmi2TE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mkXsZ6zG; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of urezki@gmail.com designates 209.85.167.53 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=1751468464; 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=Mb9oqMZgMTBAUwaOtSamD7a+s5czkzcGJgOvI3OEyFA=; b=htntyMUvsBpss0HCieuwyP8mE5GSaEhQ0Dc84uwJd9Y/maTDmr/6o8rI+LvgXE/+zHsq7s fgebTvr9eI+EnUuLhCK0UMqQ6+quQSmZz/XGBhAuaQyMl8QlAschU8mLqEOyCRR8Lap47E dZEKBeo0HQgYXiB7+BzmL5w6m5bAe/Q= Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-553ba7f11cbso8250943e87.1 for ; Wed, 02 Jul 2025 08:01:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1751468462; x=1752073262; 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=Mb9oqMZgMTBAUwaOtSamD7a+s5czkzcGJgOvI3OEyFA=; b=mkXsZ6zGY7/bDxXSrvYZ5D5ybMtkhtOyOcSmkcPI3d+kop0GJSrcWZfDCdfI+5x52O T/GONKTuo0wVl7LwVolQcGWhM3qJdeO1a2Ns4tuKTwuXv6239TkYBDyOG6RwkpofJrsF BO9laVtP5XMdczrDnaE5y0wMSxHoyu22y8nn+71cjK/f4Nv6nRAcmaH0Yz+HqdLvm4v0 Qm1kjLwErTy/uXD9rMvyngsNCLD+daS6w2ksL2sfPdKLLK/u08OsgvK8HIq62s3x16QS M1t/gCIRhxXvFL1N0POfrCvtsVTrSJc45qpMefK3dIczfz/+K5/m1OxE6h35aQYlbm9W qFJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751468462; x=1752073262; 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=Mb9oqMZgMTBAUwaOtSamD7a+s5czkzcGJgOvI3OEyFA=; b=uQdXDn2St62FHA0gWORJ6dpOUIyQnFc5BBPwkSAGOCLEnkViuTFPsRHwNaMEQ1hsto PIIS/EQJMNOC/GG+JIlx81BTXTFzUzn9Q52at9eyWfk8XkEmljxjqMqhTwC3C0vLZ/9z S9boR6miYqtthw7tM8eqlUK3Hqmhih/puk2FgfMCeQJHCQWgLtByspYkNwEdHBCPG7c3 yKLfTId7n5bWezlXV0Ezy4q3WupBwAvJFgaQ5XRmCnEYI3b9/X8xTtsxfvSHyJRtyysa aLlIBOEr4glkT2OuVvKk+oPKP6RLorXGbguGHwG9ose5NpP1szC+t6Xgmcxnp5D5jVbI CzbQ== X-Forwarded-Encrypted: i=1; AJvYcCU04gyKqUQdY/Lcw1lL2j8GbYbAqa4/clnpsCxWj5PowCfvOCNhXym3cUzQnopTc0K6z3RDt9XG4Q==@kvack.org X-Gm-Message-State: AOJu0Yx1ryZvxkmr8W5tPfCWZ7sKST7y5A1dsd0M19pKblcQPPnhPU39 gZWCifvl7aEpbyOOGMSfS1NELHFbJu4VP/X24msw1xA1w7XKXoI3VeDQ X-Gm-Gg: ASbGnctwxC6tepLbdtUIGr3g2+BN2t1URGyhYhREV0VK/107e3vqQMxy2yRhFa3eghu y7fN97r+TXcKKdo37pNpGJwL9ESnFWcfI7f4Xcu1xCIW7JQRLpNRn2lVQfm3M+0DlnDR/9BszwG u4mKgpwcRgecXLUO2HUC60uROVIxIJJRznU15G3K/4hz5faadytGiIX/z4f2MbdGbsC6Q+xxDt5 AOrNA+iJuPwFdooGs7ki8gJu4R7BC3DkPo4JT1tKt/F77ByovMWLO4S2kN4XTByOP0ckNVkALL7 G1dsARKstwElB481ZtgTHH68DkvGjzroB29MxZ5Xbqqipu6MSIEvsKZ3iQ== X-Google-Smtp-Source: AGHT+IEP9JGw0k8/AP3WA/Ob7waQ2MI68kC4hrHLxC8UG8+x0WnIlp6PUM/dnUad4yg8FsA12xibvw== X-Received: by 2002:a05:6512:2310:b0:553:2cc1:2bba with SMTP id 2adb3069b0e04-5562832eba1mr1391418e87.31.1751468461572; Wed, 02 Jul 2025 08:01:01 -0700 (PDT) Received: from pc638.lan ([2001:9b1:d5a0:a500:2d8:61ff:fec9:d743]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5550b2cec41sm2151493e87.185.2025.07.02.08.01.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Jul 2025 08:01:00 -0700 (PDT) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Wed, 2 Jul 2025 17:00:59 +0200 To: Jeongjun Park Cc: Uladzislau Rezki , 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: rspam09 X-Rspamd-Queue-Id: 51D0C1A0021 X-Stat-Signature: kxgffwhiniczgpzy95x6qien6n3iw7o6 X-Rspam-User: X-HE-Tag: 1751468464-153838 X-HE-Meta: U2FsdGVkX1/1a+BKGCQad7lVBUtXGrlT8TTXduwkgqGjn2uHJfH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2tEP8S7sMlsLFilKo9u889eg90RvarcJPaGnT4JOw0Q8ys4HOUOvkeE+8WGXzCAZQRtKjBLW/nzMeBxabdk9sNrEIyKVZ9DO0MXWmVPQKgmAIOFIeEw/intDWyF0R8RYP3oaik8Ew0ifLg3vPusRbuVHHmeoHrk0m8JqgNvr01vkbxE7MhbDpXJFchrtB03RpLZ668qBT3Gr3YovmIqqpc8i20EZx2rq8qV0Sdap29PbL1rr+ayPICAZFoXZFg7EHAW/LXV9L8CoEFkXd+ibbpzLR66HQCXrRnZht6unLKqo6Q9kbHaB9m9ntPuejj2W43vOKBSokO5WZRuJT3+ZnYxQCGR0sLnsXuEbRnTyAshf9pVYNYPwGTrI9EqJSQASg5hUB22wAk3N38h+Le86SwZx8u/65Asi73FNoffQPKPR 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 Wed, Jul 02, 2025 at 11:01:23PM +0900, Jeongjun Park wrote: > Uladzislau Rezki wrote: > > > > On Wed, Jul 02, 2025 at 01:01:13AM +0900, Jeongjun Park wrote: > > > Uladzislau Rezki wrote: > > > > > > > > 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 > > > > > > I've been looking through the stable tree and it seems like this issue > > > still exists in the stable tree, so I think this patch needs to be > > > backported. > > > > > > What do you think? > > > > > If the stable does not have it, we should backport it. But it has to be > > done only for those where a vmap-node logic is in place. It was delivered > > starting from the v6.9 kernel version. > > > > Then I guess I should backport to 6.12, 6.15. Thanks for letting me know! > You are welcome! -- Uladzislau Rezki