From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.179]) (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 AC31723098D for ; Tue, 14 Jan 2025 02:20:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736821246; cv=none; b=buSM0NsQfBy23gqq8lJNmKjfN1JOzmyKpTntyvtyCQCove3gX9xyxnhS2VQX9mM0GjKmu4HLySltq/pLanvq5dsb9UNuY0Y9xMp20mfMWjaE6uRL5OdGpZhJ01ao/iDMh0HYtQfHckb6tSSYsMoRhfMuSpV+cpZBopq+cqTZ3jE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736821246; c=relaxed/simple; bh=SrroVoqCsgx+N0Nq9gn60LnLP3rf2QQZ8+q/DOnI0p0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=HJw5RUFWyxr5NxDUdLvmesJzDX0OEn7kPdVegZoH1/hC5W+fK9t1dZ9ExV0D0lHra5/dzAMX6EMsAi8eCZ4U/BIn0r43q/BE+jqKynkfyGO0PdYM1k6nfQhq8WkH6uG48OsCdWlFRuVitrIMvl0swmntw7+TVrkhLTI5bsWRDmQ= 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=ic0R11c6; arc=none smtp.client-ip=95.215.58.179 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="ic0R11c6" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1736821241; 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=MXcgnwVyq+v+Yo8kj8L9r9woFfBis2cp/QU7eIIg75g=; b=ic0R11c6ht6LYZzSwdi08XgkNnKVmIiqjxZP2ZfB1o1JWHyf6oC+PRaeqjCIeht74InLpo W2l8YgqUmYIBRWoaqZf9jUc3WA5yxZaZBuaKkGv2bgXYLrHxQEC94C4iJYv5Ymx7wYA06r VMIjn51EsfOPtEpP9Pl9aOLUxaV94pI= Date: Tue, 14 Jan 2025 10:20:33 +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: zswap: move allocations during CPU init outside the lock To: Yosry Ahmed , Andrew Morton Cc: Johannes Weiner , Nhat Pham , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20250113214458.2123410-1-yosryahmed@google.com> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou In-Reply-To: <20250113214458.2123410-1-yosryahmed@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 2025/1/14 05:44, Yosry Ahmed wrote: > In zswap_cpu_comp_prepare(), allocations are made and assigned to > various members of acomp_ctx under acomp_ctx->mutex. However, > allocations may recurse into zswap through reclaim, trying to acquire > the same mutex and deadlocking. > > Move the allocations before the mutex critical section. Only the > initialization of acomp_ctx needs to be done with the mutex held. > > Fixes: 12dcb0ef5406 ("mm: zswap: properly synchronize freeing resources during CPU hotunplug") > Signed-off-by: Yosry Ahmed Great catch! Reviewed-by: Chengming Zhou Thanks. > --- > mm/zswap.c | 42 ++++++++++++++++++++++++------------------ > 1 file changed, 24 insertions(+), 18 deletions(-) > > diff --git a/mm/zswap.c b/mm/zswap.c > index 30f5a27a68620..b84c20d889b1b 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c > @@ -820,15 +820,15 @@ static int zswap_cpu_comp_prepare(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); > - struct crypto_acomp *acomp; > - struct acomp_req *req; > + struct crypto_acomp *acomp = NULL; > + struct acomp_req *req = NULL; > + u8 *buffer = NULL; > int ret; > > - mutex_lock(&acomp_ctx->mutex); > - acomp_ctx->buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); > - if (!acomp_ctx->buffer) { > + buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); > + if (!buffer) { > ret = -ENOMEM; > - goto buffer_fail; > + goto fail; > } > > acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu)); > @@ -836,21 +836,25 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) > pr_err("could not alloc crypto acomp %s : %ld\n", > pool->tfm_name, PTR_ERR(acomp)); > ret = PTR_ERR(acomp); > - goto acomp_fail; > + goto fail; > } > - acomp_ctx->acomp = acomp; > - acomp_ctx->is_sleepable = acomp_is_async(acomp); > > - req = acomp_request_alloc(acomp_ctx->acomp); > + req = acomp_request_alloc(acomp); > if (!req) { > pr_err("could not alloc crypto acomp_request %s\n", > pool->tfm_name); > ret = -ENOMEM; > - goto req_fail; > + goto fail; > } > - acomp_ctx->req = req; > > + /* > + * Only hold the mutex after completing allocations, otherwise we may > + * recurse into zswap through reclaim and attempt to hold the mutex > + * again resulting in a deadlock. > + */ > + mutex_lock(&acomp_ctx->mutex); > crypto_init_wait(&acomp_ctx->wait); > + > /* > * if the backend of acomp is async zip, crypto_req_done() will wakeup > * crypto_wait_req(); if the backend of acomp is scomp, the callback > @@ -859,15 +863,17 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) > acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, > crypto_req_done, &acomp_ctx->wait); > > + acomp_ctx->buffer = buffer; > + acomp_ctx->acomp = acomp; > + acomp_ctx->is_sleepable = acomp_is_async(acomp); > + acomp_ctx->req = req; > mutex_unlock(&acomp_ctx->mutex); > return 0; > > -req_fail: > - crypto_free_acomp(acomp_ctx->acomp); > -acomp_fail: > - kfree(acomp_ctx->buffer); > -buffer_fail: > - mutex_unlock(&acomp_ctx->mutex); > +fail: > + if (acomp) > + crypto_free_acomp(acomp); > + kfree(buffer); > return ret; > } >