From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BF8DE77188 for ; Wed, 8 Jan 2025 06:01:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0AAFF6B0082; Wed, 8 Jan 2025 01:01:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 03E816B0083; Wed, 8 Jan 2025 01:01:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E3DCE6B0088; Wed, 8 Jan 2025 01:01:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C561A6B0082 for ; Wed, 8 Jan 2025 01:01:02 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7B5B2120FF5 for ; Wed, 8 Jan 2025 06:01:02 +0000 (UTC) X-FDA: 82983236364.15.BD716BB Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by imf25.hostedemail.com (Postfix) with ESMTP id 7590DA000F for ; Wed, 8 Jan 2025 06:01:00 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=WL5blAvy; spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736316060; h=from:from:sender: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:dkim-signature; bh=Tz/X7zQuq6YwE0g7vrq1zjJ6XRCK9g30OEfwcIt8haI=; b=YOwrp+0pNfZaEh3/f4sL2HKRyzu/OlT0TZqlDTs+OBMSVD5Xr6ImOu0x2IqKRRIQzTCiwe mCZpoVqj10lfRApq+6MlIM2uCrKaIYlSG8qyYY8UnFPr2aAo2M5o+DIak1f5U4CxOAhXCB a0AnoldVCJzA5ZITsoWQC/URGLpKXd8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736316060; a=rsa-sha256; cv=none; b=1nZv9MGr0JLxcMic1Kq+EfX0skPnn5USiBpT6gdoc0tVzjlyp7K68pqtryHKAFkP9EybOD o3xHrjytIO91ScVkEiqG0yGjdiZCb87lNj8SuAF2g9RWN/YxzlXixo3oajz4e+iuZ/pSwG dgIBOL5LO3x5IQbXbuO6vJwBeir9zbw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=WL5blAvy; spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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 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 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 7590DA000F X-Stat-Signature: 8q7ob5d3z5ih73ktz99w34gwc1rputcx X-Rspam-User: X-HE-Tag: 1736316060-241663 X-HE-Meta: U2FsdGVkX1/wrgtcg8lcMqQOg2F1K3oER8kFuEsH/jE79ZR+iS2HFug7YIEeOlwnlZfcXOk6mjlPT/auaGnv/bmoI7BwglhMIkabTRsxgB0e4y8Czz+fLqFSuCLOBBOZVL2DJpK1rsP1LSaJDJpoPyToZmo2NA4YJ6Swggt6QbR6617k2bXHnsUWBXBACdKGx/uipL76ouwRw7XYQwF1ZxG5j8PyvSglQFV5oIavYeWvt965i4v5qgSJ37x5sTyPLnbBpqWPvNDrhbCmAZNxR+yziHuIF/yzDQowvl7H6AEbgGCg7wFiu/SScaPEZHbJT3DWiwt8+AKYyEN46oWm/BNa0Aw7HWnVUs19O9IpCBMbv1TCMEuLWylq6FqyKJJ2r33b6GRxn8PV8cSisqsidt7vTV0/xaWW9Mz3rvO/6oTSIP5ueRbC4kbwOyRyL3dcYYhnxN/HggYnqxgCbxnn04VYJskijMjXicLrB6t8Z/A8XvwmIChj9NWR6HI9wJq8bIBs2GmWrTHNlnYWx78U/NzGxOAMokGgn6W9qYf0vIpVwkp/yuByYzvRxK8AoHUNUDf4qfGZaB50Li4g9quDe5mCUMtutd5E0ksglJDIE6eZUJeFj9RDCrzHRQZNKy5UAhXnbGztwpcIEpCj7SoEqEzhwbGRRFh3Xq48ZUtk+UKbGXPphNovyaIPC7JnpwrtdllhBoj4qY0stlNjQwgmgABUcx6eTq2A+YY4DJrdmt2H7TD9jnMRWIdZO6gBcTtwrNyKB2W9/1RRiuEH/QIzlTnBAVQNhW61f4pz1yqFM55XD/fTy+PK+qj6UwdIFxzVoaPuSJrndyqk1d1tjk+cjIEvMwEP6FHRnJ9Fny5Buhu3X0bZqwU4+fsUZfjWhuFxHotj/ykPspKGMczH8mmk3bfpptKW2LivhJ73o1rfXzR77ZUfMXNQ0q/B4A/nIPJ0yUZtlDUnIrjA3CYZTLV DW2yDIBs 2TKmPJnsWGq3VRqFJvPuyW5CKx/7ik48JYxmF9m2KC8EQz5Fi1sWM+K/ZGlc89AqTB93qtWStu+EwkxZpOx+2DheIBlzdNbXdu155zwYJ1hOWEapW//FyedAy5AihujahxZjUoOHw+jJlXODlCsSkSnz1PwzHA7flLfoMwj1w03InXRUoiqkdcL5A6Ewi8IBC1Hegzuc/puMHtB5xeHKpy6jkN9unBeInBsmV7HbYBMWpmewObeJlOnl3vEMsO9jh1U7nAg356+wCtQVvuIHBML6FMN+j/XKy9q+Q X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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.