From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) (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 2315D2E40E for ; Wed, 8 Jan 2025 06:01:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736316073; cv=none; b=XwSwzUvZHy7naSdkwPraqZTG4TfM0ndk8/IGN/+w+zUxZ1mTbCupF8p9DM13djhvMoUVz5qrmXLCMlKJVX93lLpQMCGtnJF1AbmREVrDlnXfQ0e/FzBLs08Fx/yUODwXoZ/dRG3bhp3mByboxb8Gg98kz/8t6datVg24iW4t5/o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736316073; c=relaxed/simple; bh=rtR2YHwE7AV6xVlSWkJyb8bR60yi1JXO4YlbdoYiI8k=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=YfDp68wXtjFw5TbofSUyWPJgO53ldz6ID3eRkrGQ2Gnr5VrH8VCbw4OhGbhUNxTPaIr/fS9/FnUWBLnkvTHbiA6E3tgwyEMQTTqCJ2+ReQYWJ9v53DveSvm8wzqRf/s6vJNTBU2HvxjK481alvZvvnPBrXKz8lXkfwt3xmrpaw4= 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=WL5blAvy; arc=none smtp.client-ip=95.215.58.178 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="WL5blAvy" Message-ID: <038d3db8-a56c-469e-804a-c258731f3362@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1736316058; 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=Tz/X7zQuq6YwE0g7vrq1zjJ6XRCK9g30OEfwcIt8haI=; b=WL5blAvyMkK3Ncv/kDGTuLen+GVccgkpnWvRlS2J3GtcVnsb5/F3OkbgVFX7ZmwllHpTqs gUvOYBFhXKECV2a/tJO7DPr3+5h+qzE9XMJZe06uAYQOevXVCuIyVSUxvu5sCAd5yArMeL 4q15F5uW9NmibvfywVibuejVQ9kQ3Qk= Date: Wed, 8 Jan 2025 14:00:40 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx To: Yosry Ahmed , Nhat Pham Cc: Johannes Weiner , Andrew Morton , Vitaly Wool , Barry Song , Sam Sun , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" , "Sridhar, Kanchana P" References: <20250107222236.2715883-1-yosryahmed@google.com> <20250107222236.2715883-2-yosryahmed@google.com> <857acdc4-c4b7-44ea-a67d-2df83ca245ed@linux.dev> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT On 2025/1/8 13:34, Yosry Ahmed wrote: > On Tue, Jan 7, 2025 at 9:00 PM Chengming Zhou wrote: >> >> On 2025/1/8 12:46, Nhat Pham wrote: >>> On Wed, Jan 8, 2025 at 9:34 AM Yosry Ahmed wrote: >>>> >>>> >>>> Actually, using the mutex to protect against CPU hotunplug is not too >>>> complicated. The following diff is one way to do it (lightly tested). >>>> Johannes, Nhat, any preferences between this patch (disabling >>>> migration) and the following diff? >>> >>> I mean if this works, this over migration diasbling any day? :) >>> >>>> >>>> diff --git a/mm/zswap.c b/mm/zswap.c >>>> index f6316b66fb236..4d6817c679a54 100644 >>>> --- a/mm/zswap.c >>>> +++ b/mm/zswap.c >>>> @@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, >>>> struct hlist_node *node) >>>> struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); >>>> struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu); >>>> >>>> + mutex_lock(&acomp_ctx->mutex); >>>> if (!IS_ERR_OR_NULL(acomp_ctx)) { >>>> if (!IS_ERR_OR_NULL(acomp_ctx->req)) >>>> acomp_request_free(acomp_ctx->req); >>>> + acomp_ctx->req = NULL; >>>> if (!IS_ERR_OR_NULL(acomp_ctx->acomp)) >>>> crypto_free_acomp(acomp_ctx->acomp); >>>> kfree(acomp_ctx->buffer); >>>> } >>>> + mutex_unlock(&acomp_ctx->mutex); >>>> >>>> return 0; >>>> } >>>> >>>> +static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked( >>>> + struct crypto_acomp_ctx __percpu *acomp_ctx) >>>> +{ >>>> + struct crypto_acomp_ctx *ctx; >>>> + >>>> + for (;;) { >>>> + ctx = raw_cpu_ptr(acomp_ctx); >>>> + mutex_lock(&ctx->mutex); >>> >>> I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this >>> cpu-local data (including the mutex) from being invalidated under us >>> while we're sleeping and waiting for the mutex? > > Please correct me if I am wrong, but my understanding is that memory > allocated with alloc_percpu() is allocated for each *possible* CPU, > and does not go away when CPUs are offlined. We allocate the per-CPU > crypto_acomp_ctx structs with alloc_percpu() (including the mutex), so > they should not go away with CPU offlining. Ah, right! I missed that only buffer and req is dynamically allocated by the cpu online callback. Then your fix is safe to me, thanks for your explanation! > > OTOH, we allocate the crypto_acomp_ctx.acompx, crypto_acomp_ctx.req, > and crypto_acomp_ctx.buffer only for online CPUs through the CPU > hotplug notifiers (i.e. zswap_cpu_comp_prepare() and > zswap_cpu_comp_dead()). These are the resources that can go away with > CPU offlining, and what we need to protect about. > > The approach I am taking here is to hold the per-CPU mutex in the CPU > offlining code while we free these resources, and set > crypto_acomp_ctx.req to NULL. In acomp_ctx_get_cpu_locked(), we hold > the mutex of the current CPU, and check if crypto_acomp_ctx.req is > NULL. > > If it is NULL, then the CPU is offlined between raw_cpu_ptr() and > acquiring the mutex, and we retry on the new CPU that we end up on. If > it is not NULL, then we are guaranteed that the resources will not be > freed by CPU offlining until acomp_ctx_put_unlock() is called and the > mutex is unlocked. > >> >> Yeah, it's not safe, we can only use this_cpu_ptr(), which will disable >> preempt (so cpu offline can't kick in), and get refcount of ctx. Since >> we can't mutex_lock in the preempt disabled section. > > My understanding is that the purpose of this_cpu_ptr() disabling > preemption is to prevent multiple CPUs accessing per-CPU data of a > single CPU concurrently. In the zswap case, we don't really need that > because we use the mutex to protect against it (and we cannot disable > preemption anyway). Yes, your fix is correct, preemption disable is not needed in this case. Thanks.