From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.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 BE7353D1CB4 for ; Mon, 20 Jul 2026 09:12:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784538725; cv=none; b=Xstxj0OSMjY+5qJ0VmHR/4gFw+Cavig7kqBAKZ+eSmuIyT0s8VSKptzDEUcw+v/aFlNqwArRXiN2COrF5qDWrz3iLLSi3M3VWcJ1DXqv5awNT/cJQhq6uFNUM+2dhWNzlF+N9Eej3j/dy6cBizm5J2FLamgpAqwat9NII3kWDXQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784538725; c=relaxed/simple; bh=so2cN7c57pPw3rIf80EHhXfyO121fao1DhkzK1kYoQI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=eU7dWhBL6aW1oHLx5puXl2fHd9RzqMr/LRm/fL2kjS7dBy7Yb5ptHCU+HAPC2wzp74/qzMS7XURf90H/H+QlatWP5A1rUZNeTLIEeqtx75pn93dYsvdyDHYUbwryLGtLfoROyTQvFcfjfz3VvxMEQJLssCCUP/enBDXjJu9lPAk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bntGIfo1; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bntGIfo1" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-ca97d139d5fso6916067a12.0 for ; Mon, 20 Jul 2026 02:12:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784538723; x=1785143523; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=bIMIMnD9F07/WlLx80PJuXQoWwWGI+wP42qXxuV/w+E=; b=bntGIfo1nWQzZzXCfb/H69hoypQtrkVV4APKmoixBBXzV5gWZJti8z4t0MbD4j4CY+ YOJ6wivCtTBpGJOZCoKJN0CHlQuxdNfedcGyq0AKlTxCsHFQDcQev+smdTu1OPoxj041 JDiTVLTjmySn/BK3czQr0FD6N9d0kg6CETcYRBuosMimJ3ho+gEK+By9wBw7MGclnDPD 6Tj+bh9taz04yRWlbgMViiSzdMqhr3xcEH6bW55f+51pY/jGWd3zFNbilnnUNzaMexBZ HFVMqI7UNuBgTkSCNOQMlfAuqGFNNT1thDfMO+4En/Py1Ji+F1abhJLkR2KSiPApltGK 50MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784538723; x=1785143523; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=bIMIMnD9F07/WlLx80PJuXQoWwWGI+wP42qXxuV/w+E=; b=jFPx+4XkSIjmx3gBQNkPMBj3zuff13oX/nCF4fOgp669Ja5sKtdOMuRR4lB/nZoS77 aAn2S8l+0TS7yxqhX4cQhnH9UlbV/V03fIxgreugXICI7Dyw0pMV1fPgpmUUxaBraxjf XCF/ezdYU7rJ8SEn1JCG3s6gvjI9gjNaHE6eRkx9jp7+ryfNfAWdGQSJwGj6FWBVGRjy 4AJBQyCCMpMkYdrOCPFS5TINwzuponDtw9KlruQYT0/ib0PGrzurrJt0/fmif9eSI+v8 vMizlgeTdPUdmt/ElwjnFd+cMDsGvB3Q58ydn0hZgEyB84JL321f37tbgIbx+ocxSW5p gY1w== X-Forwarded-Encrypted: i=1; AHgh+Ro72jHIDlnkxeb+VoIHlvS8FyULPsKTNVpE/KRz0OQ1kwcX/hrz2WCuAzNGXbZDbcGUdA2j9l54rtSs8mo=@vger.kernel.org X-Gm-Message-State: AOJu0YwSg49D0W7tHM7Nf6a4Qv8W4FMfS4SWr8TmL36igUi/LjVeLCcJ ObjTd+ELlY9BF60/lJMXyyRCpODjFiBsm9N8J5ktfo/8XmOUEpaYoUSt X-Gm-Gg: AfdE7cnYkhdxqXV/UnriTcqPUdvQ7RUjmFnRZelUEYYQXOMAq29j0sWxAxg/NnITEQj qWW+sKNicZOnAHaXrHjBcan7fxeKWFleDnXLzWC//ZCrnv+PbVIBThnZYFmQm7Y91Lw93CdsAST Nsp+bqivo/9B9xF3iuEkIqMYvh94tAdYR10VRU3GICqqtD8UuodFux8iQkzbyYs/ByVUJNapH+u U+RhiwUfpavZhflZvFMBrU2q+1N7dHjqqzhYALn5dgcyukdeEpxnjxxb/TIoQ0BhQ0oJTDZc6tq /BYEdPXUHOoSC1T+AeaK5P0HnDkPY5kfwfM0gUCHC5OgMO+UE68GZalQFnZAdI8YOUIvjZsu6X+ 1Gx+cw/aoJYSz1HUWEhdImK0268RvI3Pqds6KdZFXgrnXr9fOAQSk+g0euKf6v1P7001aIxjgnQ 7/wpfkdGoIIdcA5sR/Wj0FqKIz7URHbpUu2a9fvVYwPwvalaM= X-Received: by 2002:a05:6a21:4c0c:b0:3c3:9cf4:a982 with SMTP id adf61e73a8af0-3c3ad8ecbbfmr13496010637.37.1784538723072; Mon, 20 Jul 2026 02:12:03 -0700 (PDT) Received: from KASONG-MC4 ([43.132.141.24]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cb51a1cb3ecsm4625716a12.29.2026.07.20.02.12.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jul 2026 02:12:02 -0700 (PDT) Date: Mon, 20 Jul 2026 17:11:58 +0800 From: Kairui Song To: Kemeng Shi Cc: chrisl@kernel.org, kasong@tencent.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Message-ID: References: <20260720071342.50742-1-shikemeng@huaweicloud.com> <20260720071342.50742-2-shikemeng@huaweicloud.com> 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: <20260720071342.50742-2-shikemeng@huaweicloud.com> On Mon, Jul 20, 2026 at 03:13:39PM +0800, Kemeng Shi wrote: > The root cause of this issue is because multi-tables are updated in non > atomic context. To be more specific, the issue could be triggerred as > following: > > swap_alloc_fast swap_cluster_populate() > /* Try a sleep allocation */ > spin_unlock(&ci->lock); > swap_cluster_alloc_table() > rcu_assign_pointer(ci->table, table); > > ci = swap_cluster_lock(si, offset) > cluster_is_usable(ci, order) > if (!cluster_table_is_alloced(ci)) // ok > alloc_swap_scan_cluster() > cluster_scan_range() > __swap_table_get() > > /* free table when more table allocation fails */ > ci->memcg_table = kzalloc_obj(*ci->memcg_table, > gfp); > if (!ci->memcg_table) > swap_cluster_free_table() > rcu_assign_pointer(ci->table, NULL); > > table = rcu_dereference_check(ci->table, lockdep_is_held(&ci->lock)); > atomic_long_read(&table[off]); // NULL dereference > > Fix the issue by updating allocated tables in atomic context. Thanks, that's a really tricky one, good catch. > > Fixes: 2fe7a6f5024b8 ("mm/memcg, swap: store cgroup id in cluster table directly") This commit doesn't exist, you mean b197d41462c2 right? > Signed-off-by: Kemeng Shi > --- > mm/swapfile.c | 21 ++++++++++++++++++++- > 1 file changed, 20 insertions(+), 1 deletion(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 615d90867111..d29062d9c3cd 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -490,6 +490,20 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp) > return 0; > } > > +static void swap_cluster_copy_table(struct swap_cluster_info *d_ci, > + struct swap_cluster_info *s_ci) Nit: The name is a bit misleading, it not copying the table content, just filled the d_ci with s_ci. And do we really need a helper for this? > +{ > + rcu_assign_pointer(d_ci->table, rcu_access_pointer(s_ci->table)); > + > +#ifdef CONFIG_MEMCG > + d_ci->memcg_table = s_ci->memcg_table; > +#endif > + > +#if !SWAP_TABLE_HAS_ZEROFLAG > + d_ci->zero_bitmap = s_ci->zero_bitmap; > +#endif > +} > + > /* > * Sanity check to ensure nothing leaked, and the specified range is empty. > * One special case is that bad slots can't be freed, so check the number of > @@ -527,6 +541,7 @@ static struct swap_cluster_info * > swap_cluster_populate(struct swap_info_struct *si, > struct swap_cluster_info *ci) > { > + struct swap_cluster_info tmp_ci; > int ret; > > /* > @@ -552,7 +567,8 @@ swap_cluster_populate(struct swap_info_struct *si, > spin_unlock(&si->global_cluster_lock); > local_unlock(&percpu_swap_cluster.lock); > > - ret = swap_cluster_alloc_table(ci, __GFP_HIGH | __GFP_NOMEMALLOC | > + tmp_ci = *ci; > + ret = swap_cluster_alloc_table(&tmp_ci, __GFP_HIGH | __GFP_NOMEMALLOC | > GFP_KERNEL); This allocation could be largely simplified with mempool, the reason I didn't use that is due to lack of RCU support, which can be done later to clean this up. Please fix the Fixes tags, other parts are mostly fine, thanks agian!