Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp5199701rwr; Sun, 23 Apr 2023 23:52:02 -0700 (PDT) X-Google-Smtp-Source: AKy350YqdS9Wj7ydFShZVoCfbXnyB7teDvSuuIB7QGygThleNmr0v1/Kw9r/1U0/+aMQo4HNOJJZ X-Received: by 2002:a05:6a20:c1a7:b0:f0:929c:e8c5 with SMTP id bg39-20020a056a20c1a700b000f0929ce8c5mr14602180pzb.35.1682319121811; Sun, 23 Apr 2023 23:52:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682319121; cv=none; d=google.com; s=arc-20160816; b=s39bYrUhYx5tIDJyMmfbTjlABFwjj+e+YdHJiLnWZMg21iWtkNVQADpU4nifVhOkmy CS+tdUzJPNOKE0sxn6P0/c7y+QIkg7pCJ9EsCGeMeffUOebLLcobaFLCNl+uQOe1Mj+S JOI6MZRQ2RU3qsiwOQJ4zdjqHI/pqjC7LOgat5Lvf7nQHxl1DesCOPYJMaZiO9TS9mMe zW1WatJ5xe/Io4KntvTEAC6RY77Ck+uWeICwF0l+C0JzSNPKnMsW8nUrsAdvDQ9LYvMx ZHxLJ/7THUUHwLj4Qm1U6jThfVPfYKa1Wc6+mDytCRaqrXI8q/KbrdemtbfirtZ+EqJH LqFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=m+XNRCJOCfLxyZ4cwIPK9OyK5H6tE/ktK9eo2pIRD84=; b=LdlSePkSnTnF+8/s6vMtN02xbb/93Pji1Jyc0K9r3jJsI0ZlZptPamizouvPedxsSv z8aEoUm5BQVXsFcgzwqYVmBnv9786jCp5HkGKQJrsG0MX9ICBDQWpOtnO4t9yQRXdb6n rS2T2PF0QHuTS1AY9HmfmkSslyDIa/YJuxCVa/6DVuTrvIrIrFp6buBBiHAhjIx6DVyJ 4qxDn8O+0twwsvz98z+jsbsU57nsMpYLI5gbGEZzLUqgZLaAWi5eOdOHlTgk8qf6j08j gjrWagd9rMRBwbkNUJ8Mc/bxTBIX+5j/Ft1TEzl8IwuewZZWdtXlKUh5pMP7qHeRWCGK C92w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="y774HvF/"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nk15-20020a17090b194f00b00247023fdbcbsi12957802pjb.186.2023.04.23.23.51.48; Sun, 23 Apr 2023 23:52:01 -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=@google.com header.s=20221208 header.b="y774HvF/"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230476AbjDXGvf (ORCPT + 99 others); Mon, 24 Apr 2023 02:51:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229581AbjDXGve (ORCPT ); Mon, 24 Apr 2023 02:51:34 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38BD312D for ; Sun, 23 Apr 2023 23:51:32 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-4edc63e066fso10461e87.1 for ; Sun, 23 Apr 2023 23:51:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682319090; x=1684911090; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=m+XNRCJOCfLxyZ4cwIPK9OyK5H6tE/ktK9eo2pIRD84=; b=y774HvF/D+C59aXsgi0eSltT2nCuWUYP9VPItZaP+FB2a6Z+NcEf7cf6NUnrMEE8cz mab3mboDj+X56d6oZeoSuc4XU2kywiJu41lte3PsTjqffMbtzkJA+W9lWY+Ky1Jl6aWU Oea8ZXCpaepIFZVqIUtBWjpZXKvzYA1j41OFKwgLO03+QQouKqLsdnLeWuR8N45JUzV+ Ft0rRmISzOVnzL9eTw+DjPbXJAv1aoZiThnPAUPk06AheQiXhyd9CJwZfLuHtJtCvidG mgL/qGpcw5lwbg2VD+lJKkVGyv6W7urDp1ysrRw8mn3oAm4oaTaenMpcAe7x2hMro4kI 2LBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682319090; x=1684911090; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=m+XNRCJOCfLxyZ4cwIPK9OyK5H6tE/ktK9eo2pIRD84=; b=YDPQaUe98CaxKxoewqmAFbdyavdwao0yVPT65lHHz99ncm7XwUIp8CzmLbJSC9ac1m U3N6ZmHR+i23jALwQLV5pM6SRzlKHquEhHVbQ+NhQKc7m7bQm77gbX+uGlxHnH4nPQNi xU1aTh7xrGBekz0h3Hg4c/vfj9SGO9kuBgSto8fJfVXjoMGwVyjAPguCCfzJhHsh0ayB BGEznIjIYXqwkOSAZrc9GimOdT6K8zBbwzMaJzIbzHSylhO6SQ0fxosn5QtiFqY0cXJ2 l1HzlQNboKUrz/v8e9Ujp48CJmUZxe9JkuBGq0r6sLKm5eV3zPLsipR2vL5OLuklBNb0 s3zw== X-Gm-Message-State: AAQBX9cHQogSYyrhKJS1MY1Pk2mSPGBazwCYXPECjKW2JzXuzbRwxUfp Bhmjqk+ocdDlTsWoKGE1xZI1u9nFxp782xH99O8DxO54EGELudTIlOA= X-Received: by 2002:a05:6512:ac1:b0:4ed:b131:3449 with SMTP id n1-20020a0565120ac100b004edb1313449mr199314lfu.7.1682319090294; Sun, 23 Apr 2023 23:51:30 -0700 (PDT) MIME-Version: 1.0 References: <00000000000058b63f05f9d98811@google.com> <20230421174054.3434533-1-roman.gushchin@linux.dev> In-Reply-To: From: Dmitry Vyukov Date: Mon, 24 Apr 2023 08:51:18 +0200 Message-ID: Subject: Re: [PATCH] mm: kmem: fix a NULL pointer dereference in obj_stock_flush_required() To: Muchun Song Cc: Roman Gushchin , Linux Memory Management List , Andrew Morton , Johannes Weiner , Michal Hocko , Shakeel Butt , linux-kernel@vger.kernel.org, syzbot+774c29891415ab0fd29d@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 On Sun, 23 Apr 2023 at 04:26, Muchun Song wrote: > > On Apr 22, 2023, at 01:40, Roman Gushchin wrote: > > > > KCSAN found an issue in obj_stock_flush_required(): > > stock->cached_objcg can be reset between the check and dereference: > > > > ================================================================== > > BUG: KCSAN: data-race in drain_all_stock / drain_obj_stock > > > > write to 0xffff888237c2a2f8 of 8 bytes by task 19625 on cpu 0: > > drain_obj_stock+0x408/0x4e0 mm/memcontrol.c:3306 > > refill_obj_stock+0x9c/0x1e0 mm/memcontrol.c:3340 > > obj_cgroup_uncharge+0xe/0x10 mm/memcontrol.c:3408 > > memcg_slab_free_hook mm/slab.h:587 [inline] > > __cache_free mm/slab.c:3373 [inline] > > __do_kmem_cache_free mm/slab.c:3577 [inline] > > kmem_cache_free+0x105/0x280 mm/slab.c:3602 > > __d_free fs/dcache.c:298 [inline] > > dentry_free fs/dcache.c:375 [inline] > > __dentry_kill+0x422/0x4a0 fs/dcache.c:621 > > dentry_kill+0x8d/0x1e0 > > dput+0x118/0x1f0 fs/dcache.c:913 > > __fput+0x3bf/0x570 fs/file_table.c:329 > > ____fput+0x15/0x20 fs/file_table.c:349 > > task_work_run+0x123/0x160 kernel/task_work.c:179 > > resume_user_mode_work include/linux/resume_user_mode.h:49 [inline] > > exit_to_user_mode_loop+0xcf/0xe0 kernel/entry/common.c:171 > > exit_to_user_mode_prepare+0x6a/0xa0 kernel/entry/common.c:203 > > __syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline] > > syscall_exit_to_user_mode+0x26/0x140 kernel/entry/common.c:296 > > do_syscall_64+0x4d/0xc0 arch/x86/entry/common.c:86 > > entry_SYSCALL_64_after_hwframe+0x63/0xcd > > > > read to 0xffff888237c2a2f8 of 8 bytes by task 19632 on cpu 1: > > obj_stock_flush_required mm/memcontrol.c:3319 [inline] > > drain_all_stock+0x174/0x2a0 mm/memcontrol.c:2361 > > try_charge_memcg+0x6d0/0xd10 mm/memcontrol.c:2703 > > try_charge mm/memcontrol.c:2837 [inline] > > mem_cgroup_charge_skmem+0x51/0x140 mm/memcontrol.c:7290 > > sock_reserve_memory+0xb1/0x390 net/core/sock.c:1025 > > sk_setsockopt+0x800/0x1e70 net/core/sock.c:1525 > > udp_lib_setsockopt+0x99/0x6c0 net/ipv4/udp.c:2692 > > udp_setsockopt+0x73/0xa0 net/ipv4/udp.c:2817 > > sock_common_setsockopt+0x61/0x70 net/core/sock.c:3668 > > __sys_setsockopt+0x1c3/0x230 net/socket.c:2271 > > __do_sys_setsockopt net/socket.c:2282 [inline] > > __se_sys_setsockopt net/socket.c:2279 [inline] > > __x64_sys_setsockopt+0x66/0x80 net/socket.c:2279 > > do_syscall_x64 arch/x86/entry/common.c:50 [inline] > > do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 > > entry_SYSCALL_64_after_hwframe+0x63/0xcd > > > > value changed: 0xffff8881382d52c0 -> 0xffff888138893740 > > > > Reported by Kernel Concurrency Sanitizer on: > > CPU: 1 PID: 19632 Comm: syz-executor.0 Not tainted 6.3.0-rc2-syzkaller-00387-g534293368afa #0 > > Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/02/2023 > > > > Fix it by reading the cached_objcg with READ_ONCE(). > > > > Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API") > > Reported-by: syzbot+774c29891415ab0fd29d@syzkaller.appspotmail.com > > Reported-by: Dmitry Vyukov > > Link: https://lore.kernel.org/linux-mm/CACT4Y+ZfucZhM60YPphWiCLJr6+SGFhT+jjm8k1P-a_8Kkxsjg@mail.gmail.com/T/#t > > Signed-off-by: Roman Gushchin > > Acked-by: Muchun Song > > Thanks. This improves things, but strictly speaking the write side also needs WRITE_ONCE. Ordering is always a game of two. It's not possible to order things on one side, if the other side messes up the ordering.