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 CD883C77B61 for ; Mon, 24 Apr 2023 17:10:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F9BD6B0071; Mon, 24 Apr 2023 13:10:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 481B96B0074; Mon, 24 Apr 2023 13:10:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FB846B0075; Mon, 24 Apr 2023 13:10:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 193226B0071 for ; Mon, 24 Apr 2023 13:10:30 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DA5331202D1 for ; Mon, 24 Apr 2023 17:10:29 +0000 (UTC) X-FDA: 80716923378.02.38D92E6 Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 2A1DD1C0012 for ; Mon, 24 Apr 2023 17:10:26 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=cgvKgPjv; spf=pass (imf20.hostedemail.com: domain of shakeelb@google.com designates 209.85.160.172 as permitted sender) smtp.mailfrom=shakeelb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682356227; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LDRpSrlyT9FYzP1gC3JngMLit04Rwq4LBSA35igDfWg=; b=SSPvwaSKgekZM3JUNNkuHVEUr/wWzbqT+ESz4ZHh2oMcsgQKINRhviNwEQy7s+sPvgJmwo cOw6M9lbMsQzJBhtEzQVluknDs3WNJFiOHqe59IAmL4DZR/Kmi22oiBW9vcWNteZxCYw/S hcKP/UMG95jnTKyVQeuSiqJvGFIktdc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682356227; a=rsa-sha256; cv=none; b=e25S9YT2YewqorOQ1ydz3ccPOg/jmTTiZNPWSjdSM7kCEQnRRSfDWQZoV/g3vHf7z391jF 5KbKm6hCsvETa3PW+aXwgOR5i1wM3ZZ3R7Z7FJRTsIgoONWCJbwC5Z49B1i5hUCh0aq9uJ Khz0tqMpZ4XTp+B61qIH8IBYM8IaFgc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=cgvKgPjv; spf=pass (imf20.hostedemail.com: domain of shakeelb@google.com designates 209.85.160.172 as permitted sender) smtp.mailfrom=shakeelb@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f172.google.com with SMTP id d75a77b69052e-3f0a2f8216fso207351cf.0 for ; Mon, 24 Apr 2023 10:10:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682356226; x=1684948226; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=LDRpSrlyT9FYzP1gC3JngMLit04Rwq4LBSA35igDfWg=; b=cgvKgPjv2wvUQmlzs7RzonUhd4D9I89qNZY69J3xkADpMA5SDr2om76YzaJQL+n/Sb EpGHkFt5ZW43wMR3s/gMS1FKlDf5TsJ/1nOWR0hgaZfCWDmctypNV60IEQk7J5wpefIZ ZBM6a06ZujhmQvQFctyQwKAG4sm3yIH8mbyXwBoxvC485/JwK4lIAXxs1iQoZ1n34oHA gwwmMm9fUPrvajHVdqKNRs85SUGy6aS5/6hFJxUI0Ttn3zJkdS3Eolsr8h1qj0FHejfN zwC1UvOIIoVR+OpS0rweiPnmF4GIJOywuTf1FlTQrmHFKdif1lvdUzqiYV+V1Sc/pEmz L+Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682356226; x=1684948226; h=content-transfer-encoding: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=LDRpSrlyT9FYzP1gC3JngMLit04Rwq4LBSA35igDfWg=; b=YOkCz9a6B6dCC9VzYXqE0OqNZ3hbFPDG1LLiaU2dHRoUVQkWaW0Bk0koBLwejziFZZ A0nh2z5u99R1stUJhqgnzNQlpeJVxKHlIUwSf8KHFVEz2D0R7F79KJPSuULDPLKnCQ2j 7UW22Pw/KDvtIifBX9SUP54AmV/Li2MTWUhPs+9CzLAzpMYjT2eQBRylua72rzqaslWE MCTEH2huwKVI4bl7WAoANeSO+dq5bPVsEagliH/tPbNPXtcQI5Xc1l20Oy8oJVPeSKbQ gpGhOcLCJT1cgBM/nWUnUBkWLricov+zhISs/AJhw+swutgdQmGY5Ceeej4rvQTOcxgl jShQ== X-Gm-Message-State: AAQBX9fUKZgGYINsUCpgGgEEh859+jy2gy1Yj7dja6w5PRhQfD1D/w0W pAySY3gI+lEKEm/10qvJBYGC+TMrgs93Bpw+Vw+SSg== X-Google-Smtp-Source: AKy350ZyyNifJYiXT7K3Sg7viGBfJLlTP/oYwyhL6kUf3TLFz1RhlOE2ilhOwdtmAMjnK5wcPdg7UJHkFsB4jH+rYHQ= X-Received: by 2002:ac8:5908:0:b0:3ef:3083:a437 with SMTP id 8-20020ac85908000000b003ef3083a437mr637670qty.18.1682356226223; Mon, 24 Apr 2023 10:10:26 -0700 (PDT) MIME-Version: 1.0 References: <00000000000058b63f05f9d98811@google.com> <20230421174054.3434533-1-roman.gushchin@linux.dev> In-Reply-To: From: Shakeel Butt Date: Mon, 24 Apr 2023 10:10:15 -0700 Message-ID: Subject: Re: [PATCH] mm: kmem: fix a NULL pointer dereference in obj_stock_flush_required() To: Yosry Ahmed Cc: Dmitry Vyukov , Roman Gushchin , Muchun Song , Linux Memory Management List , Andrew Morton , Johannes Weiner , Michal Hocko , linux-kernel@vger.kernel.org, syzbot+774c29891415ab0fd29d@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Queue-Id: 2A1DD1C0012 X-Rspamd-Server: rspam09 X-Stat-Signature: wpgusdikhw1xk8oy4butmm7qft35gj1r X-HE-Tag: 1682356226-143060 X-HE-Meta: U2FsdGVkX19c4UYicMyjNtxlGXUdUZhtdndMkVL+jiWDoCcEhCFX50kaX9EmomnYyL8aDoZLkAYp/pZeumYPMWqTsdfovjub8G8xflAdULcS0iVd99XJiPZBWWheMUz6fTWDH7rRsZj7KlyLonghROqMIZNN7bEEnLjiubxSS7r/b1eX67SPfZRyljY1/nV5u917xYxMHpK5nXgi86XazF4uuZC1E2O4mcrQ8Gf3u1hPgVcxSFla19ofyPZmEFWRYJiRwSGWssv4p7OSKsh0mpyvdHKWUGD8QuqImKUIbnxhW7i1VgVJjnocmTgHpyR/j1Pw9DR9qRLaz5H+a+DU5S3EKQT++HChZpNq443x7tYxr1/9DNdCBncEh0m8QdTb1LIxK0Iotw/modTYD70cDyvT/w7G1aaFZbJYUHb29uXrkdn6igCTCvv3Um6Yowgiw3562dL+faHGCJmSDSlqMTCt84V22cjhRS5dP3T/DLquKpXW4emSzGfbGfxwvyrckRsUg4VVW86ksMLt/cermySgilwiYejPrnLL6Al4CUJC64VQjCpTpAFL4+bzxmiw5kcz7KK3L5cQE8AHTu3dnlqjN/bVR6HgwezkWgoDFD3xfya99esIdm7iUBo2ND1B+mnVbQcxtbuePgKkH98uSzjFdiG72ldJg2iure2GulUkKLAQqlkt7z/CXxBU+IGJ0scKdPSW1QO5XOF/hjMUvsJfCUtSWpnfdzVdh0fe3VNvpmVS5k2DpIOvVpWnSueGFg1N4QGEZH+lq44naYyqE3Uk9GEK6O5ghr4/LEqydFL/L9+jaimd8ewldMWoW9GalQV/BAcnQDcKoMRbRnMUqFayAS131wGrd5st7rLGjUGzCRo8jqPefAsYq7kk68Jee7llj6iA2gR1ynIRyVn9NVPi3er4T5uuqvqA1mM/1nblLL/wQUcpsO6FjAgA8gUogzhlkIIg3yGNjJ82B9e YDu15P4D LrFZan0nrC6rVP/NRUjTEDqbXtqAp+NsqYpJ9WiDv2IO1tnszNHKO71bIDIBdRnMaXUV/qbTFhTMs+FiEAqle3JT1K7l6pDd2BIbYv/UV5mcjCGYo3CQEJxKRgkfhCQ+N2lTX7fc+vE8uRXJzWjLgfbNNA1TFABGxnz/VrbAXXs0B4QpDqmotfajSEoOf7bce2pXZ6LZBDklVJrzqULayF7dxJStz+ceemANzgwNhsZUJ4QXXG4pXALjmlflh00G8BmA3yRQKJXGZxl1eApvVE7WrVlR1BitoXt1LjMNn6Q7YuEjpb1OcHA8rcfHX2p4Gq1NLiuEzYl8RC7ur4OVLmq0B9y9J9irIuEmPRwOXo0UouGUcQ4xvgGVx9nBvSjUj0YEb9VZ8RqYz+1xKiXkwjZqHrMymss8l5rnxAMIX94fWgEpIbKWFmgX3pmjGfPTUG/32DJxCvV3bx4ZR8J0Q+rqaYLoeE4ct0LYozG5o14OYaayl/7dfP+FmrJQ+PNpTtOkhwd2Iz4XjVvQpIQA9RC+OIsexZJbjF2J093wS5fMVs+j/NRXwyRP+yuom9U47Pn1FNC5cLDJPphzIhRzijrar7Q== 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: On Mon, Apr 24, 2023 at 2:13=E2=80=AFAM Yosry Ahmed = wrote: > > On Sun, Apr 23, 2023 at 11:51=E2=80=AFPM Dmitry Vyukov wrote: > > > > 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: > > > > > > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > > 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-syzkal= ler-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. > > > > It looks like most other accesses use memcg_stock.stock_lock for > synchronization. Based on the output of obj_stock_flush_required() > we call drain_local_stock(), which acquires that lock as well. Should > we refactor the code to extend the lock section to cover both > obj_stock_flush_required() and drain_local_stock()? > > IIUC this may unify the synchronization handling and > READ_ONCE/WRITE_ONCE may no longer be needed. This should also avoid > any inaccuracies (e.g. unnecessary flushes) that may happen if the > cached objcg changes between obj_stock_flush_required() and > drain_local_stock(). > > Did I miss anything here? Yes, drain_local_stock only works on local cpu and obj_stock_flush_required can touch the stock of all the cpus. The patch is good but I agree with Dmitry that we should add the WRITE_ONCE as well.