Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp684622lqb; Fri, 15 Mar 2024 03:43:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXKBOzO/HeLbSMMgyIzV/d+zc3SvoFtQCT9+NeOIwbPUkmncJ9zytsEuUXLmgp8F5DNv9Fy2T7C9tLiaSu/5iHAZlsHed4cErnXfAnDXw== X-Google-Smtp-Source: AGHT+IEUCuDLxU11h+F1sz/T8SEsJj6HMkNlFMxWA6tKR8Djh8SNCr2xuyRiZba7oFMB8E8lwZtM X-Received: by 2002:a05:6a21:3284:b0:1a3:24e7:3280 with SMTP id yt4-20020a056a21328400b001a324e73280mr5120741pzb.61.1710499419249; Fri, 15 Mar 2024 03:43:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710499419; cv=pass; d=google.com; s=arc-20160816; b=gve2lX484X7V4Hp8FgZQdvDOR/rc9lkaIsgjSqx5NTmg2WmXGKXEIOjf6NUTHtzEws ok4wqeufwKsM+by816FRxJyQgQ4hzJiBEGm9vc0uUttE8dtAbOORu3l6V0gqEnG6t3Ys DOKbdW17ZsuTnpuQTh8vnY7zWrQaRQR4gFKXcu05hAeUOrFcW0sSBr6g2/vA1TJJtcTb 27k9/enLqEg27yoHg+ruLqpyZal6Fjd0ILfM5o+JtrJXg/2FNALY4E4HZ2UtKvQldd4i i7rF+aBiTLayZ4N3PeetiMHkYxe26uALSMasspum3aspFO48n5+oTDRbqI/KuGB2O6gO cTmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature:message-id; bh=Ddo68Jue+hIogqwzsDAktZjXHMsAm2nR/AfZDU0RU38=; fh=DDG9u79H1OPE2hJ9iyjbFGn6Dx1CHXNJ7Y6veNh+ylA=; b=J8pnn6K0ia1MG+ptdLn+uuLtf8S5hTMYNqb4Gkms0RATPMYUnYcyHbbJK0mjX4JCU7 CKkB4S4sRgd2UyGrjZ6OEUOW4R9EoxtEdQfr/nx+thlr5kuvEhbQHRi2e+hULiOTfF6K aaO3krRdsAUcZ8phGVnTA8XczHIBj311clR8hV0gzsBiIEeFbRD1jMFUzw8rjwz/lcfh 1ZhA2FS1UyA+8xLeOkl28x9yUD1SXZyL84mhBbtraHSOGjoBwNCobqodpuGf2Q5+QiMw t7qwTTd40UiwUmlTWXZKTa8pEqIrZJ6GIsziPWmK7odIqKg1GPpNzL1NmjgNcArQwRoC X2/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oT4J9jEV; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-104279-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104279-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id t17-20020a17090aba9100b0029bf6a0faa8si2309679pjr.190.2024.03.15.03.43.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 03:43:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-104279-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oT4J9jEV; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-104279-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104279-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 809782832D1 for ; Fri, 15 Mar 2024 10:43:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 551AC1B275; Fri, 15 Mar 2024 10:43:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="oT4J9jEV" Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D4DE1AADA for ; Fri, 15 Mar 2024 10:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710499402; cv=none; b=iOUPcljjfAWskraFm9n0kSkTS5ydPfeBo9gAy/De2LfQgnvTdpkwNhXQ/MOzmnl/7JDUCnVkLj8EmkBASgm/5oymMctM6QlelcsYq79vcrGBtn+q4c0501gP+hxGFdLb3+I6MYBR52wHodsWkOIxvATovaL6sX/EdEb0hXKaa2A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710499402; c=relaxed/simple; bh=b1EsDs4Yb3bZWb3zk7JALLrf6EYU2C6HpbnZcyrZKp8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=XjPlSvM68RajlNixom6UmbwsD4IjyHzyCKbpbuuaEUsO1j3SFfXnKr1YJ9SDtRpKorQtANxcWZ23KzvZW7SZuxdUFrCp9n/pQkexdWnZmI8Sq48czKVmWAGArr11Z2wK/oRE9+z6Z1haA/U3QQg5p17+jHqjcLkG6XVzQMnVFVg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=oT4J9jEV; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Message-ID: <9c5c4128-3836-4b48-8fb8-8179d04fb5f2@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1710499396; h=from:from: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; bh=Ddo68Jue+hIogqwzsDAktZjXHMsAm2nR/AfZDU0RU38=; b=oT4J9jEVTVwQlCxVb+dYiN40pAqilo3L8PFbP8C+AVBkxocD4Lg8H873Sqx2LqSkdN55jd B0Zn8RtQehA0LHgX9cYvkL/LlS/AO7LxI/Y/D6fOqdJJ7wGouBEXv59xDkNv94+8WpoldI b93CvAjsL+kZ+qiSBJq1axqho8zbVoo= Date: Fri, 15 Mar 2024 18:43:10 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH] mm: cachestat: fix two shmem bugs Content-Language: en-US To: Johannes Weiner Cc: Andrew Morton , Nhat Pham , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jann Horn References: <20240314164941.580454-1-hannes@cmpxchg.org> <1551fa14-2a95-49fd-ab1a-11c38ae29486@linux.dev> <20240315093010.GB581298@cmpxchg.org> <20240315095556.GC581298@cmpxchg.org> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou In-Reply-To: <20240315095556.GC581298@cmpxchg.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 2024/3/15 17:55, Johannes Weiner wrote: > 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 Looks good to me. Reviewed-by: Chengming Zhou Thanks. > --- > 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))