From nobody Sun Feb 8 11:45:41 2026 Received: from mail-oo1-f70.google.com (mail-oo1-f70.google.com [209.85.161.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFA88274B2A for ; Sat, 10 Jan 2026 04:04:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768017870; cv=none; b=kWdXJ9NDm1erWGRbgGggdAxcF697P/bhU84BahnpubOLRd3v4tmMx+vNmzs/R5E15bdg0BaTNV42LNqs3nvGRmk9SYU/za4I0hV950oVNToIV24YKcaf/w/Mz66kajI8iKPgdBVQGmHsSqJ5I0Z/3TQETEUhl8PQlnkS7jKUXkw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768017870; c=relaxed/simple; bh=xfBs8/iut6DLCQp1Hd4qRNR5BBVYYqteao0uAWignt8=; h=MIME-Version:Date:In-Reply-To:Message-ID:Subject:From:To: Content-Type; b=W4mU9xYOy6ROMD2QCvAEbxT3rFT176jLS5mrOMYWaZaomhmHhvOAWY0KJsFOwxCiW3OqeDqiykv1ixgLAouvU3YYVBmhx40o3neuPbE+g4vc06NcRjQ8QjQmnjArCNDJlV1smdPfslIGmX7GDSWKPCgF+5cjuTRku/ZkmVjvGEU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=syzkaller.appspotmail.com; spf=pass smtp.mailfrom=M3KW2WVRGUFZ5GODRSRYTGD7.apphosting.bounces.google.com; arc=none smtp.client-ip=209.85.161.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=syzkaller.appspotmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=M3KW2WVRGUFZ5GODRSRYTGD7.apphosting.bounces.google.com Received: by mail-oo1-f70.google.com with SMTP id 006d021491bc7-65f6588abf4so3873659eaf.1 for ; Fri, 09 Jan 2026 20:04:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768017866; x=1768622666; h=to:from:subject:message-id:in-reply-to:date:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FteNnxPBTnoOfRCP/S0O/AyPF9l7Hv7eZoXSnLiymuk=; b=cG1GmUWk0t5O3cpvlxWuttMg5K5TJyTVH6g2yORQEc2HNTJTOS0oWw1pdxnhAJVvT8 9kpqe4Q9KPUc2bo1hAKabu7/gkD1prNLNHfuK6vr8wFkK8pEJ6+VEWtxHcvbSzLgTf9b FqJJslhmzrpFsFKAiI9SI8mz9mEl+ujLljDMJyh6SeqU9J7FQvE1VcMDc8smOhpO9Pdq gb37ocUVkoZ/EoEPo30MloS11coe4jF09YFdDq6RB8veBy7gAsEyykGdFvT2fF/ofpMg qOn03rPmDOYG2LWRM2GrNczfspw3YPkrOhmuOzU0+728rYJyNB3ep1PqOmqg8dqnnMYX 3EPA== X-Gm-Message-State: AOJu0YwBZz+Vouv2rQpAHXQrrQ59HdXB/Q+piuR/Y6ht+tsredvV+EHB 5a95D80b9uR1rA7V/UckhK6OiUs0tvTlOvsX9OoHHXStCRe9KIn3UqzYy2rzO4ZuZn0Nco0c0vW 1HcNe9e9x9QEIP1K7SSgYQ0uqWF0CwELPWi2Ase40O5tY1u340oGopetiIGg= X-Google-Smtp-Source: AGHT+IEg5wtygyXgLTfA4kH/KcC1Y/f9F/dso8s0YY6OI/0GBaNoMHEZvu4ERon9WG6sNMuIMWIoakz5uCkt+azXDFX2Y6Di0ZMk Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Received: by 2002:a05:6820:4e8a:b0:659:9a49:8ecb with SMTP id 006d021491bc7-65f5509732bmr4839058eaf.79.1768017866196; Fri, 09 Jan 2026 20:04:26 -0800 (PST) Date: Fri, 09 Jan 2026 20:04:26 -0800 In-Reply-To: <6960c2f0.050a0220.1c677c.03be.GAE@google.com> X-Google-Appengine-App-Id: s~syzkaller X-Google-Appengine-App-Id-Alias: syzkaller Message-ID: <6961cfca.050a0220.318c5c.0166.GAE@google.com> Subject: Forwarded: [PATCH] mm/swap_cgroup: fix kernel BUG in swap_cgroup_record From: syzbot To: linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For archival purposes, forwarding an incoming command email to linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com. *** Subject: [PATCH] mm/swap_cgroup: fix kernel BUG in swap_cgroup_record Author: kartikey406@gmail.com #syz test: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.gi= t master When using MADV_PAGEOUT, pages can remain in swapcache with their swap entries assigned. If MADV_PAGEOUT is called again on these pages, they reuse the same swap entries, causing memcg1_swapout() to call swap_cgroup_record() with an already-recorded entry. The existing code assumes swap entries are always being recorded for the first time (oldid =3D=3D 0), triggering VM_BUG_ON when it encounters an already-recorded entry: ------------[ cut here ]------------ kernel BUG at mm/swap_cgroup.c:78! Oops: invalid opcode: 0000 [#1] SMP KASAN PTI CPU: 0 UID: 0 PID: 6176 Comm: syz.0.30 Not tainted RIP: 0010:swap_cgroup_record+0x19c/0x1c0 mm/swap_cgroup.c:78 Call Trace: memcg1_swapout+0x2fa/0x830 mm/memcontrol-v1.c:623 __remove_mapping+0xac5/0xe30 mm/vmscan.c:773 shrink_folio_list+0x2786/0x4f40 mm/vmscan.c:1528 reclaim_folio_list+0xeb/0x4e0 mm/vmscan.c:2208 reclaim_pages+0x454/0x520 mm/vmscan.c:2245 madvise_cold_or_pageout_pte_range+0x19a0/0x1ce0 mm/madvise.c:563 ... do_madvise+0x1bc/0x270 mm/madvise.c:2030 __do_sys_madvise mm/madvise.c:2039 This bug occurs because pages in swapcache can be targeted by MADV_PAGEOUT multiple times without being swapped in between. Each time, the same swap entry is reused, but swap_cgroup_record() expects to only record new, unused entries. Fix this by checking if the swap entry already has the correct cgroup ID recorded before attempting to record it. Use the existing lookup_swap_cgroup_id() to read the current cgroup ID, and return early from memcg1_swapout() if the entry is already correctly recorded. Only call swap_cgroup_record() when the entry needs to be set or updated. This approach avoids unnecessary atomic operations, reference count manipulations, and statistics updates when the entry is already correct. Link: https://syzkaller.appspot.com/bug?extid=3Dd97580a8cceb9b03c13e Reported-by: syzbot+d97580a8cceb9b03c13e@syzkaller.appspotmail.com Signed-off-by: Deepanshu Kartikey --- mm/memcontrol-v1.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c index 56d27baf93ab..982cfe5af225 100644 --- a/mm/memcontrol-v1.c +++ b/mm/memcontrol-v1.c @@ -614,6 +614,7 @@ void memcg1_swapout(struct folio *folio, swp_entry_t en= try) { struct mem_cgroup *memcg, *swap_memcg; unsigned int nr_entries; + unsigned short oldid; =20 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); @@ -630,6 +631,16 @@ void memcg1_swapout(struct folio *folio, swp_entry_t e= ntry) if (!memcg) return; =20 + /* + * Check if this swap entry is already recorded. This can happen + * when MADV_PAGEOUT is called multiple times on pages that remain + * in swapcache, reusing the same swap entries. + */ + oldid =3D lookup_swap_cgroup_id(entry); + if (oldid =3D=3D mem_cgroup_id(memcg)) + return; + VM_WARN_ON_ONCE(oldid !=3D 0); + /* * In case the memcg owning these pages has been offlined and doesn't * have an ID allocated to it anymore, charge the closest online --=20 2.43.0