Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp662767lqb; Fri, 15 Mar 2024 02:56:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVc6uGfSAw4jLa0xhjeVwF0z5u7cBE10qZDF9GAjfFgDkhpUwrxE+fsDWAEqXHQy9/PQTpgQbg/B05qP57xErhX4jVQ4AQmdQXrW9uY5A== X-Google-Smtp-Source: AGHT+IGyqcB6qBQCpXX5BuE8iU8FG+6Mg3MpbwWI9KeD63SMwJ7w1zqzRVEFwB599znEIfAzQ+0h X-Received: by 2002:a05:6a20:8f28:b0:1a3:48ee:2bd0 with SMTP id b40-20020a056a208f2800b001a348ee2bd0mr3047929pzk.56.1710496596590; Fri, 15 Mar 2024 02:56:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710496596; cv=pass; d=google.com; s=arc-20160816; b=HzFhQzdjvUYYhC+AkMFUqKgtBhvOlJ5hAPlAcdPOuKD4QIxZeTqVKv1KK1ybxUnk0Y H4ViitT6TNGzE8CLPxme9KFc9KD6256mO42XEI4ECOvvhYjghtk7Q50mc76OKtqPzs0n PYh0vHx1xHTXuMBuHw8HHLTev9XaUfpd6ZQ1lD6kSUgw7EVHxhFQpcw7HMse1kjIe5oj 9G0MUbv9Vnyznkf1e6Fpfvo6U1BWNofq6wNepeCdQVREFHaBnyk2XCGN1UYdCMyELZCV aUih6thAbx2Fdi/rOldJokbnDiQtofv8bL/a0UTwqB9HIDj7riPwt5REOHHsq3Ss/XxR sToA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=k8DLBLZhMwvsJMasT6dA48of753EzUhCWq2nn2xC2I8=; fh=EaMQlYDa7qGQLKA94gPjNSzR+Xrog4AQO+YopboViqs=; b=etSps3zbhE+BJKa4WaXaCyvPg2Y6nAM6tzxC9Fi0Yp+9i8r0AKdX2ecvGRgn9t6U/n JnkDP8PoEWkE58GI2rgofaotOH4AgxfUiWSwlaDYbI77G/YuKO2ALjC7UBax5wkZ2W4i FpIx7u315L9Qx7z/rankRhWP+Qm8/JfSRUj30DmhA0es6E1LDDqRqUOItOHiM+MQ0MbJ tj7ylHIadtXz13pZDLjU2bSeX5w5ehaH3OYLCb5skac5Q6FK8dibP46WLavgNgPO6wb3 IpPGrFT2EwMCRpC7df4CvfdN1djf73rn141RWuhUwRjgNvn6I4GCmu6Iu6ilQqUYwBMI STlQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=zI25mVXR; arc=pass (i=1 spf=pass spfdomain=cmpxchg.org dkim=pass dkdomain=cmpxchg-org.20230601.gappssmtp.com dmarc=pass fromdomain=cmpxchg.org); spf=pass (google.com: domain of linux-kernel+bounces-104225-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104225-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id jw22-20020a170903279600b001dd678cb4a4si3079694plb.525.2024.03.15.02.56.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 02:56:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-104225-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=zI25mVXR; arc=pass (i=1 spf=pass spfdomain=cmpxchg.org dkim=pass dkdomain=cmpxchg-org.20230601.gappssmtp.com dmarc=pass fromdomain=cmpxchg.org); spf=pass (google.com: domain of linux-kernel+bounces-104225-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104225-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id DAF4F28413B for ; Fri, 15 Mar 2024 09:56:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B80461865C; Fri, 15 Mar 2024 09:56:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg-org.20230601.gappssmtp.com header.i=@cmpxchg-org.20230601.gappssmtp.com header.b="zI25mVXR" Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (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 F3BB218641 for ; Fri, 15 Mar 2024 09:55:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710496562; cv=none; b=b8DNQ/3qk2QSsF3ZDcgrNjw7KMTPHw1piq2RucweuZQC0EI53GSwuNFMEesxOH+JXLp3F7DZL6FKvv5qcVd3uibIGnCVD++zkJfkLTrqtSFcHD8EHj81BJSG7qZ2JdgYmlnkhmE/vRZiACLNK2lW9cOihXlVEqwXaYYjg+AIwno= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710496562; c=relaxed/simple; bh=UpjrXGq/g3tgFes1ozbu0h8lPjYOb9PiPDF8TJqBjtk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=azV0JOqKM0BQmJzCusbfhlRKc7ylbvDR37aTFgYJrsRQJn80cE34doKzZgMx+JYPf2mNhcWjDGQjBHdV8c9duD/DU9OupcoOV46X9x7fdmp32FL+egjdsZqpmS8k05HsvQEvpuBa/NPYOqmBzfPSP69dhL+loy5+bd3f19CU8Og= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass (2048-bit key) header.d=cmpxchg-org.20230601.gappssmtp.com header.i=@cmpxchg-org.20230601.gappssmtp.com header.b=zI25mVXR; arc=none smtp.client-ip=209.85.222.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-789de1f59feso94017985a.3 for ; Fri, 15 Mar 2024 02:55:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1710496558; x=1711101358; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=k8DLBLZhMwvsJMasT6dA48of753EzUhCWq2nn2xC2I8=; b=zI25mVXRtakIm2bvb1G8DKbXS4Kld4rO75/uzMZHbD/foTsUKrXeX6XKhpGWgD/+4/ hlIr4pxWlM2dTh0P0rqvGPJPXqu5O7d9mUY/yTIwMUQH0c0wLDXp9EZgmdJ4xFKnWjlr VIo3X8JIZg44jAgFIzT8O8q+j6pWjUytmKbcpwohkq44JqsPoJZ/4NVCl7J1zt66qLrK ZDCNgS96PFQd/OFfWNItL4rS96nMBRpZicMg3xjJl+WXN8cX60vADmzg4DsE29fRAjNw ejsP0hMwn1ounUA6wPed0rFFksNnijJCSIGw8A3w5D8dH9zgwvlNzBXbZ0MrV8M0oilq JD2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710496558; x=1711101358; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=k8DLBLZhMwvsJMasT6dA48of753EzUhCWq2nn2xC2I8=; b=MWxZqrjpTL7JJLtdjWliXJdMideE9h9mDBFWYO9b85M6ANpUr8v3ggdVF2+NdZ8cyT 7zSBO9NuwJm44pg9teAHAQxGgrjx9jpdzaE3PMAifhqgS45HXP+1atvpphZwF5Rcm3Nu GjKh5diO09DlR0oRJpokz2FOcJ9E1Ug4rnkNb5p81F/zt1LCnegXq6PYewhRCgykCgz5 dwwjWH2QFol891vC41CLHcndrAmISC7UuK3qYFFcwHPPVdmOlVvXUK6FhVXk7k4f7LJD cmq7ZfpNV6vDIzWpRIUSz7zDFTBET7TG+QUhY4VRvf/5mn1mRGC1wpFrMcT4ZmpNRAjd a6+A== X-Forwarded-Encrypted: i=1; AJvYcCUItclXKt+CZqWZlF0AL8Y7gQEZRqdWe8mMUUUcW+aLV6B42lexFvkIxwq5woCK1iuE1sZtVy/oXrjQVYeeOYWUE3BkOK8cpCuSnga2 X-Gm-Message-State: AOJu0YysKX17paJbj7+UBFbSFuJUMRDWT0lB3m11Ui0pfq/SL6HYHdWs oJre8mo+n40TuYzBFO2a64LfqhpD7nMNn/MYgKZa9S5sncAunZ0N3G8EydbFcGQ= X-Received: by 2002:a05:620a:1490:b0:789:d553:9233 with SMTP id w16-20020a05620a149000b00789d5539233mr4227652qkj.30.1710496558668; Fri, 15 Mar 2024 02:55:58 -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 qr3-20020a05620a390300b00789e220b7b0sm921448qkn.0.2024.03.15.02.55.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 02:55:58 -0700 (PDT) Date: Fri, 15 Mar 2024 05:55:56 -0400 From: Johannes Weiner To: Chengming Zhou Cc: Andrew Morton , Nhat Pham , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jann Horn Subject: [PATCH] mm: cachestat: fix two shmem bugs Message-ID: <20240315095556.GC581298@cmpxchg.org> References: <20240314164941.580454-1-hannes@cmpxchg.org> <1551fa14-2a95-49fd-ab1a-11c38ae29486@linux.dev> <20240315093010.GB581298@cmpxchg.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: When cachestat on shmem races with swapping and invalidation, there are two possible bugs: 1) A swapin error can have resulted in a poisoned swap entry in the shmem inode's xarray. Calling get_shadow_from_swap_cache() on it will result in an out-of-bounds access to swapper_spaces[]. Validate the entry with non_swap_entry() before going further. 2) When we find a valid swap entry in the shmem's inode, the shadow entry in the swapcache might not exist yet: swap IO is still in progress and we're before __remove_mapping; swapin, invalidation, or swapoff have removed the shadow from swapcache after we saw the 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. Bail on get_shadow_from_swap_cache() returning NULL. Fixes: cf264e1329fb ("cachestat: implement cachestat syscall") Cc: stable@vger.kernel.org [v6.5+] Reported-by: Chengming Zhou [Bug #1] Reported-by: Jann Horn [Bug #2] Signed-off-by: Johannes Weiner --- mm/filemap.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index 222adac7c9c5..0aa91bf6c1f7 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -4198,7 +4198,23 @@ static void filemap_cachestat(struct address_space *mapping, /* shmem file - in swap cache */ swp_entry_t swp = radix_to_swp_entry(folio); + /* swapin error results in poisoned entry */ + if (non_swap_entry(swp)) + goto resched; + + /* + * Getting a swap entry from the shmem + * inode means we beat + * shmem_unuse(). rcu_read_lock() + * ensures swapoff waits for us before + * freeing the swapper space. However, + * we can race with swapping and + * invalidation, so there might not be + * a shadow in the swapcache (yet). + */ shadow = get_shadow_from_swap_cache(swp); + if (!shadow) + goto resched; } #endif if (workingset_test_recent(shadow, true, &workingset)) -- 2.44.0