Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp1277964lqb; Thu, 18 Apr 2024 05:41:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUe6R3bfBe3re2VqskmP3Ffz/CsliB3HrfgrreSFjWPE7OSmmHP4zVTVED7Yh17HRH6yXkDKxfSpAZfL33YpA7SQDpVLG3mhzIgqD7ipQ== X-Google-Smtp-Source: AGHT+IFngU/vgdGWOhtVSUP2KD6UODK/8umi+k3oMhqSYZVNQcRpM2XYxnil2L9GcEcc+IpJcPQS X-Received: by 2002:a50:d741:0:b0:56d:f99f:a600 with SMTP id i1-20020a50d741000000b0056df99fa600mr1664378edj.17.1713444063993; Thu, 18 Apr 2024 05:41:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713444063; cv=pass; d=google.com; s=arc-20160816; b=THeIKlA4wFlXnEdGl6oVMwvwldXZM0ZjA8QlgCcPDY8F2MHO5WcR7ZRxzXppyRu/Fs vnH+56V19aBG+uNCduIDdYVZ4c2qgPbAgfYX1E980EyfFcK/C5bmVQFyi1ZMAg83ccWG Fet4wrm+ynhJYJTYyWVkMcbpbmYR5oC1mGJaNx07OD5+zXS+CYn6sBr+WhyIEsE78n3s 9/xWDUatRwcKzO8ZS1obzNuqZVFWwBwHBINb5VlmBPD9QSROt46gYW4YjTFT8aUT2OtA 0gvqIqkYbYULii0zFYRlXJWg8aBPePI8AD+tVwjnU4UYq2U7YFtWfcrQyC8+S93q/ZJ3 JCOw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=GsKNX1Jtc4wi0IxWCLI9huAtIgpytdCJy3O9dcSAXkc=; fh=38VbM246Dm+vCM1qnlosiuIN7eWQKAL9AO7OJ+dL+Js=; b=g+12ozSBtbnxEbGOxqZb9eLruJC+TaAPz3MrnCPJ5N7ra66DtWgpgde3vzqWfQjKFB 1voMA4yCxrMMdxVdPOrFEtr7wdKrxGcHP2ZgUM0CMiOgsguc2kxrlD0xmG1dd97X+0m1 9+SN+mK0nrKH+pnJwVlHKLkL8qhR10IDPElEP+iP3Usrt80PHzChXL2+1I1YmcIdqs5u uxviqqkiQxcrfMh8O8Ro5UWcK9O4D4mKgAoURetraA4PBygCEsCIJIZy6abp1Dg9Tju6 HsV67uzQH3Vqv+RCtiKoCVhn7/GyPjUCwaIo3z8OQH+EG5WC9NfGEoVtjIFkRWcuKx8Y 7N1A==; 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=XGJKaNoS; 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-150073-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-150073-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u6-20020a50eac6000000b00571c96026f1si35593edp.532.2024.04.18.05.41.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 05:41:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-150073-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=XGJKaNoS; 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-150073-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-150073-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 7C56E1F220FD for ; Thu, 18 Apr 2024 12:41:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6654838FA6; Thu, 18 Apr 2024 12:40:54 +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="XGJKaNoS" Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (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 025E71EA8F for ; Thu, 18 Apr 2024 12:40:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713444053; cv=none; b=HsZLieb27jDmA8WMaEIZanJ1E29vGChhqCF6+CjWyx8g6MtMHgeDNOqzBQKQQI12SYpEp6mDrN3XPgH2eMxXEVPhPHcXGQPWXMFS7TPIAwzrXtYsyo15VzUTDI79sUEO3h4Vhl1WLVOwhHOD20a31H2YnyHNnr5ZYKky8HLzR6o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713444053; c=relaxed/simple; bh=WIWI3yHKnVFiaW6pEn33UHdpoGSoHsOI9hbl8L7S3KU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=aONDvxh1RC2nG8+DYySPpNd/faMujNKi4lKf/+/dIO+xEl6hobbRj6ddyflW4rZiKOorhJCFmyztewZDtPDW2lvpaipVaXkjGkj9B0Luzk7PAv2Mm5YIyn0vMIJgfu3aYIfV56zwnZZNyKNOiPHjgmCEhlHl3d7YAEmW70ZWhkE= 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=XGJKaNoS; arc=none smtp.client-ip=209.85.219.178 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-yb1-f178.google.com with SMTP id 3f1490d57ef6-de45385a1b4so914640276.3 for ; Thu, 18 Apr 2024 05:40:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1713444049; x=1714048849; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=GsKNX1Jtc4wi0IxWCLI9huAtIgpytdCJy3O9dcSAXkc=; b=XGJKaNoSUHwS0aMz2Nt8GYvkHQPOunGIZtOjtSENXyxo8V67zeKLxZlJotT8d/9Uye XB00o3nMrAvnvzXEWG5kTMNr+7WoIAQl3YPIdTFg3U+q9rxqkOc9olKmKOA3QxiXGhec vul2+o3tbPtYSQRBbD8NxmU6ki317E6v5PDRM+VGKTPXKW5Nh/ER6tE7nVqiKMMDddel 1OcM7BoxzQFO5FoKkJJGOL2BOrmhpoqio8aD8bNskudrReHSbjqha0T/IYb7t71dtXb0 aS3HWU1yLq+oilAKnC8SWeeeW/QE/5hYiwToG4E2ZT487XUp0GvGBUfN3m+kxJ/hxlJP tCgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713444049; x=1714048849; h=in-reply-to:content-transfer-encoding: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=GsKNX1Jtc4wi0IxWCLI9huAtIgpytdCJy3O9dcSAXkc=; b=fDbvVJhLrTaumt8b2d7d4RxoFS306sQ/ni7lFUrPoEJb/nqk+PguNuHI/jNpnP4e7w r47SjDgxj3k5p6gBMXP5tGi2cTFPhCFW9dMzH1nJBS5ySGGSFXtGh8n0leyATxHjjpa/ 2RM9GfoRAbC/Cng9aKT2MiWnkkwmAuJUebrbeWcY7EjayHkVUQbJuYnhLuI4likgzDv4 jBVbBh+96w6oMsQAL61AsvbbDnmKXNJr3UK8WEIXjqkqL1qinvLYtdpRgHm44HCCKsPD EmFYxRyl2GdKoJ/2Mgi5dLKRYpRzbH0CeHVnonPYU5U37VHAxfAVL415y+hhA63SadDg kCqQ== X-Forwarded-Encrypted: i=1; AJvYcCVvKH35EtwdRELh2XhVqQyNOwN9kVJUYu4cS+dpfuNtuRAeO0+R7ATmF/NHHQMXOIR1AGZixjVbqMDC9zACGrd7g72sMbgLdtgboKNF X-Gm-Message-State: AOJu0Yxl49emyeyXAfyzX43b8bZYeBe9ku5g5aCpl2TJDeeE3lFvEZfr 4T3s/IzmLciZ3jZ6QNKBfE7Cgw/Q/uiC0zdQr7ANtabn6o/e/KXUhJH0zZW+Qfw= X-Received: by 2002:a25:198b:0:b0:de4:5c94:f58f with SMTP id 133-20020a25198b000000b00de45c94f58fmr2504112ybz.15.1713444048816; Thu, 18 Apr 2024 05:40:48 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:fe4f]) by smtp.gmail.com with ESMTPSA id a14-20020a0ce34e000000b0069b4fe0569fsm587500qvm.109.2024.04.18.05.40.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 05:40:48 -0700 (PDT) Date: Thu, 18 Apr 2024 08:40:43 -0400 From: Johannes Weiner To: Christian Heusel Cc: Chengming Zhou , Nhat Pham , Seth Jennings , Dan Streetman , Vitaly Wool , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, David Runge , "Richard W.M. Jones" , Mark W , regressions@lists.linux.dev, Yosry Ahmed Subject: Re: [REGRESSION] Null pointer dereference while shrinking zswap Message-ID: <20240418124043.GC1055428@cmpxchg.org> References: <3iccc6vjl5gminut3lvpl4va2lbnsgku5ei2d7ylftoofy3n2v@gcfdvtsq6dx2> <246c1f4d-af13-40fa-b968-fbaf36b8f91f@linux.dev> <20240417143324.GA1055428@cmpxchg.org> <4c3ppfjxnrqx6g52qvvhqzcc4pated2q5g4mi32l22nwtrkqfq@a4lk6s5zcwvb> 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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <4c3ppfjxnrqx6g52qvvhqzcc4pated2q5g4mi32l22nwtrkqfq@a4lk6s5zcwvb> On Wed, Apr 17, 2024 at 07:18:14PM +0200, Christian Heusel wrote: > On 24/04/17 10:33AM, Johannes Weiner wrote: > > > > Christian, can you please test the below patch on top of current > > upstream? > > > > Hey Johannes, > > I have applied your patch on top of 6.9-rc4 and it did solve the crash for > me, thanks for hacking together a fix so quickly! ???? > > Tested-By: Christian Heusel Thanks for confirming it, and sorry about the breakage. Andrew, can you please use the updated changelog below? --- From 52f67f5fab6a743c2aedfc8e04a582a9d1025c28 Mon Sep 17 00:00:00 2001 From: Johannes Weiner Date: Thu, 18 Apr 2024 08:26:28 -0400 Subject: [PATCH] mm: zswap: fix shrinker NULL crash with cgroup_disable=memory Christian reports a NULL deref in zswap that he bisected down to the zswap shrinker. The issue also cropped up in the bug trackers of libguestfs [1] and the Red Hat bugzilla [2]. The problem is that when memcg is disabled with the boot time flag, the zswap shrinker might get called with sc->memcg == NULL. This is okay in many places, like the lruvec operations. But it crashes in memcg_page_state() - which is only used due to the non-node accounting of cgroup's the zswap memory to begin with. Nhat spotted that the memcg can be NULL in the memcg-disabled case, and I was then able to reproduce the crash locally as well. [1] https://github.com/libguestfs/libguestfs/issues/139 [2] https://bugzilla.redhat.com/show_bug.cgi?id=2275252 Fixes: b5ba474f3f51 ("zswap: shrink zswap pool based on memory pressure") Cc: stable@vger.kernel.org [v6.8] Link: https://lkml.kernel.org/r/20240417143324.GA1055428@cmpxchg.org Reported-by: Christian Heusel Debugged-by: Nhat Pham Suggested-by: Nhat Pham Tested-By: Christian Heusel Signed-off-by: Johannes Weiner --- mm/zswap.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index caed028945b0..6f8850c44b61 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1331,15 +1331,22 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, if (!gfp_has_io_fs(sc->gfp_mask)) return 0; -#ifdef CONFIG_MEMCG_KMEM - mem_cgroup_flush_stats(memcg); - nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT; - nr_stored = memcg_page_state(memcg, MEMCG_ZSWAPPED); -#else - /* use pool stats instead of memcg stats */ - nr_backing = zswap_pool_total_size >> PAGE_SHIFT; - nr_stored = atomic_read(&zswap_nr_stored); -#endif + /* + * For memcg, use the cgroup-wide ZSWAP stats since we don't + * have them per-node and thus per-lruvec. Careful if memcg is + * runtime-disabled: we can get sc->memcg == NULL, which is ok + * for the lruvec, but not for memcg_page_state(). + * + * Without memcg, use the zswap pool-wide metrics. + */ + if (!mem_cgroup_disabled()) { + mem_cgroup_flush_stats(memcg); + nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT; + nr_stored = memcg_page_state(memcg, MEMCG_ZSWAPPED); + } else { + nr_backing = zswap_pool_total_size >> PAGE_SHIFT; + nr_stored = atomic_read(&zswap_nr_stored); + } if (!nr_stored) return 0; -- 2.44.0