From patchwork Thu Mar 14 16:49:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 13592612 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 9E0B6C54E60 for ; Thu, 14 Mar 2024 16:49:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 31927800C9; Thu, 14 Mar 2024 12:49:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A21E800B4; Thu, 14 Mar 2024 12:49:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11C61800C9; Thu, 14 Mar 2024 12:49:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id F0FC2800B4 for ; Thu, 14 Mar 2024 12:49:50 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BE89CA15A7 for ; Thu, 14 Mar 2024 16:49:50 +0000 (UTC) X-FDA: 81896231340.05.4473FAC Received: from mail-vs1-f53.google.com (mail-vs1-f53.google.com [209.85.217.53]) by imf03.hostedemail.com (Postfix) with ESMTP id 0529120023 for ; Thu, 14 Mar 2024 16:49:47 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=kWZYMWoA; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf03.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.217.53 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710434988; 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=ErQ7GCVBpqdfPQB8lRuYnFXp3OiN8dCWDu5sVH1/RbQ=; b=LAgvPzcg95d+zxqodFPZ587ps6Dw4FZnMuXYytt3Y13UOl/Dm/EjM1DqiMbl6EHLO9tbGp OfFZE59eLyRCLya/NyK6GUlVlnQIcmhDPlPvFMX5IU+snYokaWvUTzNeA8o+SazdK92BTj NXxNfEG/4l8+hKxj6swuUk4C8JuT3h8= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=kWZYMWoA; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf03.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.217.53 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710434988; a=rsa-sha256; cv=none; b=gLJsGTGKibx8f1Q/aKShZ5TL4t6q7ZXhx4e8WzePfs11GluDrZ88K99YKFaBAKSjibZFWE jSxtQNHGEf8HiHewc3I/KX6b8ltrftWB6aCZksumdkYI2EW8D9AUCCFOBF7G3wKGmYJa/o 8GsVSUpyAKdf2yh7YJqEnnEjRsnsxF0= Received: by mail-vs1-f53.google.com with SMTP id ada2fe7eead31-474c44ac8aeso306890137.3 for ; Thu, 14 Mar 2024 09:49:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1710434987; x=1711039787; 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=ErQ7GCVBpqdfPQB8lRuYnFXp3OiN8dCWDu5sVH1/RbQ=; b=kWZYMWoAhhg/UT+cxKIGfIJcxzAOn4ZghfrRZT9FDnoshVXM8/Pvo4o4ctThDdJJsq Lr0G6MwIHX8DkYU4msGkx3JBy9e46dVwRvWT/9wZ85m3OX6GCLuUxMe7b2cewew+15Fh 1ExloxiD97wQfat8FHke+bS3bRsdCrjgmiDVxfddbfMrdjOHPXeioD3XkRe8Io9xCznz sCrEBbw6gRlSQVzig1aUGr24R2AwrEC9WSetTry77OHIuPoFoCzT8B822xQjyV9kk9pC owD/ePygkT2LyzvYuSwLnJfUKnInfyq4QmX03m5uNrHqFBxtT5TeJALKWxzCbKg/fJ80 Z/0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710434987; x=1711039787; 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=ErQ7GCVBpqdfPQB8lRuYnFXp3OiN8dCWDu5sVH1/RbQ=; b=Kd+VgDenM6TQPYUO5jjQvjzcATKS2akj2GAIjpI5KeYm+bUTz2r6mmb6RYI/Oy98Hi 1t+RsXecrzzuwLdh51vnJevbf8qnybUaxSzBHtwMjJJB1DKv/xyVyYqA/uzh/hXOfq2b jk+82akzq/8EOzuiNSQErrvX8TyeKk9qfUckORkRYeiKUIJs2pwkCmk7YRwc00Pr7AVF tE/b42ihgr+buUyEcb5Y8T/m5zUvw3EiVe3Y74u5n3/RwAIZDMWRjUWEkLLbXUj976dp O0tdbr7oZUIRBUHHebfkxHAhEpce11AI9mpx33XrycESUhbDsvFt5WpCJX40UAlroi0S 99nA== X-Forwarded-Encrypted: i=1; AJvYcCWa8+5LxxfeELHPsEqGXUJkYmbJjzksKLV1lfXKT+sRlu9LeSGQPZnjD1+ymknJAXCKgYjc/2hGM6rYXZKsV8uDstA= X-Gm-Message-State: AOJu0YxD8Y+H3SZoicvnKl9kC0TmKqwGYQN9LQyn/JCaOT/hS6YcuTmO zkZaQaSSz7WJRiMnKwx1XeKW0Vi6rs6ybpm8QU3bL2B+qdtEXOpUY4Nrp3rV9Sc= X-Google-Smtp-Source: AGHT+IGZcO5lkPzd5uloNC5+PMvKOX5RvPE+9LVF86FNI33jb/OI+AIjo11gwz8uBVAabmzwXSrNaw== X-Received: by 2002:a67:f78f:0:b0:474:c306:9062 with SMTP id j15-20020a67f78f000000b00474c3069062mr2578203vso.12.1710434986979; Thu, 14 Mar 2024 09:49:46 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-da5e-d3ff-fee7-26e7.res6.spectrum.com. [2603:7000:c01:2716:da5e:d3ff:fee7:26e7]) by smtp.gmail.com with ESMTPSA id 5-20020a0562140d6500b0068f65b22b0fsm659336qvs.82.2024.03.14.09.49.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 09:49:46 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Nhat Pham , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jann Horn Subject: [PATCH] mm: cachestat: avoid bogus workingset test during swapping & invalidation races Date: Thu, 14 Mar 2024 12:49:41 -0400 Message-ID: <20240314164941.580454-1-hannes@cmpxchg.org> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 0529120023 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: f1of6bh4dx69iemun86fkwxyug3498y4 X-HE-Tag: 1710434987-985011 X-HE-Meta: U2FsdGVkX192wDXqzsBHfcYylooiGvmrbL+V16kkK3XPsX0Sx30pNr/nOH6O62CTbMm2z/HXhI0blf/EOFQ16W/CCMQZRyXTW3IjvNwlNQ9p6CpvwPpm21vNr7QmQv0ZeySxrHCW7Jr0wmuh/2Ig3RmjVaMPWXPuR4dyuNTA782vA0D0VXCPoqDtizbHM8Lk6CdQY//5uoGF9opbEHxR8yM+OQbGCEvcSydBiyRcyyNk9DGiG5uVYIDH6Nz5j00vsfpwazszynd8bwVa4sQ20kygjlCoCRBGgy4TuGhC5/sQVKV0+XE7JhbFtm8uXnV/M/VVhuU1r2KZS5Et3JfSjk5qcrGDNuVI9txBmjIIU88QFFc0A1F5Ah9DCM7urP/WvunzeC6zzo/X9nV/fx8VSsqR8UZdZdi4zdb9RKXuJVAEsXAT3ZuTqTm8VmmXVy8z39bp7+OOdsR06BVTahdJr9AiYnpEhLcig8wtH3s6usQo1mJ+/8IdCP7ufTkJ/nqNMJW0w1HvFzaQ3lDqt8u/G2vtVlCUH2Z31If3o/aRW3LEKn3iw7avwHLlS4r4FUTSPBp1WDwy3Sy4O4zVp6XimPiWDLHdz60XoucBEZOvkoNBUhQS5N3VZyaBjQazfW0O438Z7M7J54c/hvbL+Ovhpym99sM7IbwgPVbGDCtGQyeHKIk60VztHmgucwlgpgEi0rLjwjyEimZjPcRaGjLHjLS8SnU/PkdrbEGkKadx06j6Cv1L3YNiKgLliBCQw6HV5qd1SkYZ8L9+Ufw0FiLWTp6J1/oh3k0SHFUL+1IIbc7dxn5ctN1VuT2zAB++/PJCLfFHM81TLAjthUrzQbXy4LLnb6J+DX3fKWru+VOBh8Diw7c7j03qd6PJVDmzWn7dJf23j1olyJoDq1KYtP4fMMy9vpKoQAELTQ3E9uC9pvLEHu+fYBKkhQxhmoJJirNAmf2y4hTHQh+f64z9HCS AWnNAw/g bTwKuza166FeLUI94/h7P2WPF6gbtadXASoTVh9ldD5NaT/gKI9JRtYVgBCPUT9jZC2QznOw1YtZ6PjKCzQBoIYZYK1Or+PXHOlOey0uYsvnwr9hnmnC62qs+/iPO7fV30SxOMKzXVKQnnVEgzlalff7F2cE6+/WpA9PEaLLmZkTjTIc= 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: When cachestat against shmem races with swapping and invalidation, the shadow entry might not exist: swapout IO is still in progress and we're before __remove_mapping; or swapin/invalidation/swapoff has removed the shadow from swapcache after we saw a shmem swap entry. This will send a NULL to workingset_test_recent(). The latter purely operates on pointer bits, so it won't crash - node 0, memcg ID 0, eviction timestamp 0, etc. are all valid inputs - but it's a bogus test. In theory that could result in a false "recently evicted" count. Such a false positive wouldn't be the end of the world. But for code clarity and (future) robustness, be explicit about this case. Fixes: cf264e1329fb ("cachestat: implement cachestat syscall") Reported-by: Jann Horn Signed-off-by: Johannes Weiner --- mm/filemap.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index 222adac7c9c5..a07c27df7eab 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -4199,6 +4199,9 @@ static void filemap_cachestat(struct address_space *mapping, swp_entry_t swp = radix_to_swp_entry(folio); shadow = get_shadow_from_swap_cache(swp); + /* can race with swapping & invalidation */ + if (!shadow) + goto resched; } #endif if (workingset_test_recent(shadow, true, &workingset))