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 8C893C369D5 for ; Mon, 28 Apr 2025 18:54:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CAEDB6B00C0; Mon, 28 Apr 2025 14:54:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C5C576B00C1; Mon, 28 Apr 2025 14:54:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4AB66B00C3; Mon, 28 Apr 2025 14:54:52 -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 998D96B00C0 for ; Mon, 28 Apr 2025 14:54:52 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 08D961CC045 for ; Mon, 28 Apr 2025 18:54:53 +0000 (UTC) X-FDA: 83384354466.19.AC29B29 Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) by imf10.hostedemail.com (Postfix) with ESMTP id 223FEC000F for ; Mon, 28 Apr 2025 18:54:50 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HduJI6j5; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.167.50 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745866491; a=rsa-sha256; cv=none; b=j85x1deMm4Y6aeYSVAahTl4PCU/L/oXgWNtq63ZIlErqo5IwPBdf0uTFgg02ZwB+WssNJg gdiUHcwz+4obRiY06uSarJFD+HYZOkJ+V7Bowrx1htXEnbkrGoVk6Ew04xLpCH2QAJb5eP 867nViqy2EHCptqdQXe/F3YLcxCuQv4= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HduJI6j5; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.167.50 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745866491; 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=I43ZMkcyctLc4IXUH1xZz0lw4Q70kaopNM4Qj8qwNVU=; b=ILa7aIMw1NaOAfhGctkQsK+nI/yfLq/JwYNqGXSYfN3IYNN48dIMBj9NbDNiqSNo8zlO6u cvAhmmpfWmTaipToBUSYzXR+iHHJdJYJEdbjxBj2muWxSE1iSrXXbl9ASLKgEjWQ38vPVY ph8PNv3oJB7oXrVnYw96nCVkwHKg/zU= Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-54ac9b3ddf6so4857361e87.1 for ; Mon, 28 Apr 2025 11:54:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745866489; x=1746471289; darn=kvack.org; 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=I43ZMkcyctLc4IXUH1xZz0lw4Q70kaopNM4Qj8qwNVU=; b=HduJI6j5aYKyivqlyly7GmAQ6AqDQLxDld/NPk9uqaOh7nTElqp56qcS2ftYJsWYiL m820QLL4H9A1tIqrTnkSN8CdCnRFqVa3ghTcB32bkoc6IUVUg2YGMDN6zwWhRu0k/wqZ fs/J0VHvVKEK4cuncDWcIJ9kI2WaeV1AfqnqT/LiT35f3jhVdJPRtBniiESwifUocNvk LDAGOAGRYq6XL2Yxg9bAO3i8i1YlCDciX2V1gNg1M9kxU0y6NxmIhtEvpTCYiixbxgq9 ptkkRqTJq57hIvBbZHdoGqnpyjN9qFqGlcQ0PsVlcNVhuiBhTHbX6LKZmYa/qHkriBBP aXLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745866489; x=1746471289; 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=I43ZMkcyctLc4IXUH1xZz0lw4Q70kaopNM4Qj8qwNVU=; b=Wy9MayBn0rb7SrAcDolriGG+EKjM/5aJ+oPCFiDA1nmCWm/IPMSnSD3NFR2fQWCGj1 JRYB79EhCcTv+4iQmW0oqxlzuE811paCg7jQT7acaEIACwT5JdrfO5ZrDzXdJCcbMTqL N2jEYRkFC+V7G6M9fJCYpzyR2wwAlRTS3BETwp5h0b4OxcxH1Y/gMFdDdIWZbmUxACVb /tsZRSRlrRzViLo/kfEJaeCpy/2ktnIo45l+C13kekakqDWBynVrajT3ylt1q+hXNuR4 8lo76M2hTNd11Pl04jyH6rQHPWeoQzmGIJilqYiaJ6LRBW77C4ipbBes1pQhwvs96h// /4rA== X-Gm-Message-State: AOJu0YzDMLLZkfI/WgBl9Rm5HMOgkgCcRGrmclxyytjJg5n+SFRhsv3g rgMrwWK9bbkfr6C1EKl6zbqxTzSifGfyS4QqVfIA+iAULRO8rSPNQYGSPS0T6e1Wp8VQf60hpiY apjubErFMviQ6sRbz15AaIpfV2GE= X-Gm-Gg: ASbGncsNyGBwELI1P7REcY48FxNQInYgXzkP5x8X848niR7LK0bdDHcRjtX1TARsDYR R6vqP18yW7tMlMza4Ol39WmDrGzgqPazU/d2TOhpgkzoW6pXa03AlCkMmZB+Ofigdec+6YvBISY +ZxDwpriGwMizqqoRXa2S1VrYKV2VZZ9xp X-Google-Smtp-Source: AGHT+IF9eRxEXFP0PqUTdi5OB+NyL32hrHSn/QN5Rz+OLeR/LBUYz6Lcf+yPsBsCQWA8ErnM7X+ZLTIkTzXoCDz3N/Y= X-Received: by 2002:a05:6512:1288:b0:549:8d8d:2c62 with SMTP id 2adb3069b0e04-54e8cc0bfb1mr4067926e87.36.1745866488899; Mon, 28 Apr 2025 11:54:48 -0700 (PDT) MIME-Version: 1.0 References: <20250427185908.90450-1-ryncsn@gmail.com> <20250427185908.90450-5-ryncsn@gmail.com> In-Reply-To: From: Kairui Song Date: Tue, 29 Apr 2025 02:54:31 +0800 X-Gm-Features: ATxdqUGTKRvMzwE_TJQTNf0P4gIR58HJ_Mvs3Pnte5Lo9Mwe8Ge_8uqoDsRzE9U Message-ID: Subject: Re: [PATCH 4/6] filemap: do not use folio_contains for swap cache folios To: Matthew Wilcox Cc: linux-mm@kvack.org, Andrew Morton , David Hildenbrand , Hugh Dickins , Chris Li , Yosry Ahmed , "Huang, Ying" , Nhat Pham , Johannes Weiner , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 223FEC000F X-Stat-Signature: am3ejir77dadyehhfdrd6nf7fojeefrj X-Rspam-User: X-HE-Tag: 1745866490-113290 X-HE-Meta: U2FsdGVkX1/N1evIlrW6S1fDsKYIzlYOYiTOrT1SGDQhzixZOxqWVsGWOph6NidOFFpuu2WcSm0nSgwhxImNBAYTImZUHpQL8A6Xu7hLHzyHvm+Y1AZAPBc10mj/tFK66+SMz/A8PrhwiSAgDLr5y6hSYLmLPfiSBZG+dPyQKB5O8hezAMwEIADJ5RPCUtMrp+KciM5Q8UUQub7XPakfmZU4fAYlk26+JDrmpt/3cM0szyB03m3ItGHgpA1xw36SfefPAAN1frYqifNinS1PLZ901gSYiTnXeF4w1OkmPlfChO7d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N6euZXg6M9cqrX+RRrj6WQPTMQZSIl3tKob4RUqXpuFHdRvf0NuVn+lcM3j3Tmh1k9fm2G5MBYfVIEBlL 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 Mon, Apr 28, 2025 at 10:58=E2=80=AFAM Kairui Song wro= te: > > On Mon, Apr 28, 2025 at 8:44=E2=80=AFAM Matthew Wilcox wrote: > > > > On Mon, Apr 28, 2025 at 02:59:06AM +0800, Kairui Song wrote: > > > For filemap and truncate, folio_contains is only used for sanity chec= ks > > > to verify the folio index matches the expected lookup/invalidation ta= rget. > > > The swap cache does not utilize filemap or truncate helpers in ways t= hat > > > would trigger these checks, as it mostly implements its own cache man= agement. > > > > > > Shmem won't interact with these sanity checks either unless thing wen= t > > > wrong, it would directly trigger a BUG, because swap cache index are > > > unrelated to shmem index, and would almost certainly mismatch (unless > > > on collide). > > > > It does happen though. If shmem is writing the folio to swap at the > > same time that the file containing the folio is being truncated, we > > can hit this. > > Thanks for the info! I didn't check it in detail because that would > likley trigger a BUG_ON but so far I didn't see any BUG_ON commit from > there. > > Just checked there are two users in truncate: > > One will lock the folio and check if `folio->mapping !=3D mapping` > first, on swapout shmem removes the folio from shmem mapping so this > check will skip the folio_contains check. > > Another seems might hit the check, the time window is extremely tiny > though, only if the truncate's `xa_is_value(folio)` check passed while > another CPU is running between `folio_alloc_swap` and > `shmem_delete_from_page_cache` in shmem_writepage, then the next > VM_BUG_ON_FOLIO(!folio_contains) will fail as folio is now a > swap cache, not shmem folio anymore. Let me double check if this needs > another fix. Checking all the code path, shmem managed to avoid all possible ways to call into truncate_inode_pages_range, this is the only function that seemed may call folio_contains with a swap cache folio. (except tiny-shmem, it uses this function directly for truncate, we can ignore that as it's basically just ramfs). For truncate, shmem need to either zap a whole (large) swap/folio entry, or zero part of folio, or swapin a large folio so that part of it can be ze= ro'ed (using shmem_get_partial_folio), the swapin part is a bit special so calling generic truncate helpers might cause unexpected behaviour. Similar story for filemap lookup. So shmem won't call into the truncate helper here that may risk calling folio_contains with swap cache. Even if somehow it does, this commit won't change the BUG_ON behaviour, except now it tells the user the folio shouldn't be a swap cache at all, instead of reporting a buggy index. So I think this commit should be good to have to make the swap cache less exposed. --- List of potential call chains that may call into the truncate helper here, and not initialized from other FS / block, none will be used by shmem. do_dentry_open /* filemap_nr_thps always 0 for shmem */ truncate_inode_pages truncate_inode_pages_range dquot_disable /* No quota file for shmem */ truncate_inode_pages truncate_inode_pages_range dquot_quota_sync /* No quota file for shmem */ truncate_inode_pages truncate_inode_pages_range truncate_inode_pages_final /* Override by shmem_evict_inode */ truncate_inode_pages truncate_inode_pages_range simple_setattr /* Override by shmem_setattr */ truncate_setsize truncate_pagecache truncate_inode_pages truncate_inode_pages_range put_aio_ring_file /* AIO calls it for private file */ truncate_setsize truncate_pagecache truncate_inode_pages truncate_inode_pages_range truncate_pagecache /* No user except other fs */ truncate_inode_pages truncate_inode_pages_range truncate_pagecache_range /* No user except other fs */ truncate_inode_pages_range --- invalidate_inode_pages2 /* No user except other fs */ invalidate_inode_pages2_range filemap_invalidate_pages /* only used by block / direct io */ invalidate_inode_pages2_range filemap_invalidate_inode /* No user except other fs */ invalidate_inode_pages2_range kiocb_invalidate_post_direct_write /* only used by block / direct io */ invalidate_inode_pages2_range