From patchwork Sun Sep 22 15:17:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeongjun Park X-Patchwork-Id: 13809149 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 CC2ACCF9C6C for ; Sun, 22 Sep 2024 15:17:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5FAF86B0085; Sun, 22 Sep 2024 11:17:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A9796B0088; Sun, 22 Sep 2024 11:17:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 470FC6B0089; Sun, 22 Sep 2024 11:17:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 232C26B0085 for ; Sun, 22 Sep 2024 11:17:24 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C4692120E04 for ; Sun, 22 Sep 2024 15:17:23 +0000 (UTC) X-FDA: 82592727966.01.73AF7D9 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf13.hostedemail.com (Postfix) with ESMTP id 048B920002 for ; Sun, 22 Sep 2024 15:17:21 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cmuBD4Bn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of aha310510@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=aha310510@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727018228; a=rsa-sha256; cv=none; b=U26nXlyOLOBRoYB9b8pznBj1d5r+fAb0naRGFTfg+a/3a8B96jCDqcJBARyTKvMMn4UskH 5IrpnniDTiLkhv08EKM0UaRgD7xyWM6/HXSjVY7vPp2YXZgLaSjx1U+1D9AbwkdCyredz0 Gtu4DWhKPtj6NyfL/2HGoV/0nPnKkoU= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cmuBD4Bn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of aha310510@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=aha310510@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727018228; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=Yg9rpCm4RXYexUUVsqyUtYoPeyurZbpl3+7GtcV4Rgo=; b=sTtcIiJ13oxJL52V6bSYUS2kh2ujfXLcOTBnKY4nyNzKMsmSSbfy4rEVr7nKIAOsAa6Hny 5buxTBAT+LaGANdwrKtvmRuS8qYFUlSxKaS6L064YUdnPznZRPy+aYIKIdk3w+j51aIfJw 2OFq5lzw3oOJqQyqvXJzbHrrg6MKmdU= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7d666fb3fb9so1658251a12.0 for ; Sun, 22 Sep 2024 08:17:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727018241; x=1727623041; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Yg9rpCm4RXYexUUVsqyUtYoPeyurZbpl3+7GtcV4Rgo=; b=cmuBD4BnEzVgnnIy9OqPAFXxfkTemTj66NuW3CtkWuzLigYxq0N93D25Qz/fLREycC vuyOA2iOrcya2K4Tt+UXeJE2+pLonlcGinngRzmXUknsJkmaAdxCZAGh4fXAMn+O+dNp azT0PtErLWwJqaR0IWx9LvEuG1JYjr58xQHxaU5AcaH/AHMIzHWGbNZVdKasGiLTAncw C0NvHooGNe03MstWehK/uvzc3ObCKOtTxpXTd4G/KRBVkU1q2n0iV5cf3PoMVP/Ppgqr nlWkb/4x5AGJ4/NYXrxfr3Px7sp7ZQUc8te38BOtk+J6JyIP3mzExauxTD2M0Espwlv2 cb1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727018241; x=1727623041; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Yg9rpCm4RXYexUUVsqyUtYoPeyurZbpl3+7GtcV4Rgo=; b=VriJIGdRNta0Ha4fuSWdI47EgCZhPRzp//cVW775/Co90r8Hl2suLxStA0PShcwcOQ lgGf6Wj1MHUL2oDpHhxgIX3TxVAKW/1cuWuqyzmUX8nl5fJST+dI+kGdusuve3fVbdv4 QWGSfH2XbJ6RLy+a0RlinQC81ASEcrZ/tQ+0pJvt/ZZAEjHgDbLLI5/XoexGvWPYMFUi 1WpoMCo6idLUB4OMuzN3M2SaJHsYvHtJ0SfYVTB6zi1TRquSz95q0QRmg0LT5bK75HLZ Hji1K/PlCPEMhWBOlFakaqnZCb9PyKZE7Y9rbUsxUMD7iHQ2m3iodeb+5hslWf3XgRg9 oLxA== X-Forwarded-Encrypted: i=1; AJvYcCWb/H8FyH4DLZtdNSRkbQ1GjesNVkFaFW8M/p/+4o9TaO5DtLzs6MCfKBWG8W0CTTZqD3Eo+xnShw==@kvack.org X-Gm-Message-State: AOJu0Yw3ckAsoWGkllPzt+iEgKTYhtBG7T2WVf5pzVEl17HFK1Io+0an v8NK/54zhkvTXb8TtwYFdl1gMhd/gX1cGz7LSQxFYk8pyQ/UsU0S X-Google-Smtp-Source: AGHT+IGXwmO/d3rlzE42aoJl6QCpT/8USlZLB1g/7Kyd3ZgVzZUzzBWo5L/ILlvxaPVb8SSLP0kNcw== X-Received: by 2002:a05:6a20:8410:b0:1d3:1ce:3f2d with SMTP id adf61e73a8af0-1d301ce3fc0mr19178671637.20.1727018240499; Sun, 22 Sep 2024 08:17:20 -0700 (PDT) Received: from kernelexploit-virtual-machine.localdomain ([121.185.186.233]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7db498d98adsm14100178a12.7.2024.09.22.08.17.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Sep 2024 08:17:20 -0700 (PDT) From: Jeongjun Park To: akpm@linux-foundation.org Cc: wangkefeng.wang@huawei.com, ziy@nvidia.com, willy@infradead.org, david@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, syzbot , Jeongjun Park Subject: [PATCH] mm: migrate: fix data-race in migrate_folio_unmap() Date: Mon, 23 Sep 2024 00:17:08 +0900 Message-Id: <20240922151708.33949-1-aha310510@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 6tg4ctdweh4so3xm9qbzz6ytu7j1scyu X-Rspamd-Queue-Id: 048B920002 X-Rspamd-Server: rspam02 X-HE-Tag: 1727018241-257756 X-HE-Meta: U2FsdGVkX19wzCQFr26OORK23zP4wCqCZdCr9wIn/3JCuKEqrGpj8qmFWAaEkyTe/lDPqse2zdhtN0qVo8r3Bqh7skLGMOw3tlJgxaQ9jF81V0uFZ5zU8B0mR7Mu5hhegT60VnCosQVL9jnZRY5B6nGOkZoI/Cd4oFkIZsW5ez+26WGhEDY4WsgOxkNQr5YGr56q8JnlJ4zSb+yPJkuvNsqO/85fry2EYZOYXFU23BV7PsrSvU5yNDjmVNSDOhqdiiAU3csMBTKFS0n8nI2zckl2D+j17zzRDIS46VUNDekcUiuzJBh7yfFwbSDF8DUxktGwKijDz1u+NW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bK654xKNHxU4d+ZBj4Jdpq6xKVxyOaf+HPcG5Ull+wdsHnOqNzqNqAJbILD19qqe3z8rRXRDh414Mp4s6HNNlDI 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: I found a report from syzbot [1] When __folio_test_movable() is called in migrate_folio_unmap() to read folio->mapping, a data race occurs because the folio is read without protecting it with folio_lock. This can cause unintended behavior because folio->mapping is initialized to a NULL value. Therefore, I think it is appropriate to call __folio_test_movable() under the protection of folio_lock to prevent data-race. [1] ================================================================== BUG: KCSAN: data-race in __filemap_remove_folio / migrate_pages_batch write to 0xffffea0004b81dd8 of 8 bytes by task 6348 on cpu 0: page_cache_delete mm/filemap.c:153 [inline] __filemap_remove_folio+0x1ac/0x2c0 mm/filemap.c:233 filemap_remove_folio+0x6b/0x1f0 mm/filemap.c:265 truncate_inode_folio+0x42/0x50 mm/truncate.c:178 shmem_undo_range+0x25b/0xa70 mm/shmem.c:1028 shmem_truncate_range mm/shmem.c:1144 [inline] shmem_evict_inode+0x14d/0x530 mm/shmem.c:1272 evict+0x2f0/0x580 fs/inode.c:731 iput_final fs/inode.c:1883 [inline] iput+0x42a/0x5b0 fs/inode.c:1909 dentry_unlink_inode+0x24f/0x260 fs/dcache.c:412 __dentry_kill+0x18b/0x4c0 fs/dcache.c:615 dput+0x5c/0xd0 fs/dcache.c:857 __fput+0x3fb/0x6d0 fs/file_table.c:439 ____fput+0x1c/0x30 fs/file_table.c:459 task_work_run+0x13a/0x1a0 kernel/task_work.c:228 resume_user_mode_work include/linux/resume_user_mode.h:50 [inline] exit_to_user_mode_loop kernel/entry/common.c:114 [inline] exit_to_user_mode_prepare include/linux/entry-common.h:328 [inline] __syscall_exit_to_user_mode_work kernel/entry/common.c:207 [inline] syscall_exit_to_user_mode+0xbe/0x130 kernel/entry/common.c:218 do_syscall_64+0xd6/0x1c0 arch/x86/entry/common.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f read to 0xffffea0004b81dd8 of 8 bytes by task 6342 on cpu 1: __folio_test_movable include/linux/page-flags.h:699 [inline] migrate_folio_unmap mm/migrate.c:1199 [inline] migrate_pages_batch+0x24c/0x1940 mm/migrate.c:1797 migrate_pages_sync mm/migrate.c:1963 [inline] migrate_pages+0xff1/0x1820 mm/migrate.c:2072 do_mbind mm/mempolicy.c:1390 [inline] kernel_mbind mm/mempolicy.c:1533 [inline] __do_sys_mbind mm/mempolicy.c:1607 [inline] __se_sys_mbind+0xf76/0x1160 mm/mempolicy.c:1603 __x64_sys_mbind+0x78/0x90 mm/mempolicy.c:1603 x64_sys_call+0x2b4d/0x2d60 arch/x86/include/generated/asm/syscalls_64.h:238 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xc9/0x1c0 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f value changed: 0xffff888127601078 -> 0x0000000000000000 Reported-by: syzbot Cc: stable@vger.kernel.org Fixes: 7e2a5e5ab217 ("mm: migrate: use __folio_test_movable()") Signed-off-by: Jeongjun Park Acked-by: David Hildenbrand --- mm/migrate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) -- diff --git a/mm/migrate.c b/mm/migrate.c index 923ea80ba744..e62dac12406b 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1118,7 +1118,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, int rc = -EAGAIN; int old_page_state = 0; struct anon_vma *anon_vma = NULL; - bool is_lru = !__folio_test_movable(src); + bool is_lru; bool locked = false; bool dst_locked = false; @@ -1172,6 +1172,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, locked = true; if (folio_test_mlocked(src)) old_page_state |= PAGE_WAS_MLOCKED; + is_lru = !__folio_test_movable(src); if (folio_test_writeback(src)) { /*