From nobody Thu Dec 18 10:00:06 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 E85421E04B5 for ; Mon, 13 Jan 2025 21:45:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736804703; cv=none; b=ZJECVYEqfX3tyZvY2YR2NrLmRySffugFQY6CJPtglzLGuSiB/LAsZ8QyC2NIsmSzNzGl92dsPKddo9w22aYSkdViVwTOwZ3WFIZXJPRTn72ytF5dz/R08BYAyTt236Lj4BZ3tqcJZN3VxbbpvXwJL336oOGHoTjSIcPtIpZWohU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736804703; c=relaxed/simple; bh=3HfGB/lrLoUwTk2LSUfYV2WfEzLrvblDozId6GTFZmI=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=jvNxEf6k5tvhBIoXCXs30WHF/viIgr8Iba0niVECy1IR267h9X8meRzCqjxt86SLQHpR13SzYbN7lmIBZqJqjk+NhKC6HNxvsXz+OVnN3Y1vj9rjhMPepgFsBsVPxK7fGti5wJaXogEPsUHBvhY+4IAnQMzNhVb6C7V1o3R5KMw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yosryahmed.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=F/oXgijj; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yosryahmed.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="F/oXgijj" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2165433e229so91012075ad.1 for ; Mon, 13 Jan 2025 13:45:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736804701; x=1737409501; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=1k/4ZQBA6kAtaFMix8Jlwq35c0dl3BB3kwSA0fYZuyI=; b=F/oXgijjG6xNs8FMVWUCbGayd+EB6eZ68KwFnFxnG9RO040IJFUuoQMJ7U5NsJXTAe JweJm1Oma4HYBkDCU004MOdUMeJClaN0Nlfa0dS8Sc1RavjRwYO4sQkYsaDWw/GFwYey qjwiKpk660JMMgu0sZ6Gl6u8hMeAaprbjCvwcsEtthQmbQSfW8aM82GIK2880vpNnWbI QFibejA2PITGqrIuzsAQZlC92lSHU6XB5ovWfruFB4jFjG8IQ2omku192r+8DGxVHjXq aGHAeJIPcTapyTK7Rx1QBNrR7Msk8qQtQWTGtChGoFC9R7IjawfHsqhdozz0PVXRFs4B GvKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736804701; x=1737409501; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=1k/4ZQBA6kAtaFMix8Jlwq35c0dl3BB3kwSA0fYZuyI=; b=Vooj3KKzRZ9LP2xCV+OmNS/NvYJnI86x1ZjWw8G8Ya/50TyUXtLRcho3659JPp7ETm 96lF3z/rxnpbEnFHD3AP8m9lPshnuwIA81i1a69C+D+NgLylo5P8ZmL9ait1eAAmS+fe 43ZAZUI7LcJsBEVND/J8TeNzI5zZ3goqkmXYr+UUWSbEuvZNkdCUU9xGU3PTvHKp5Wg3 dOdp5hLhKnqeprXTx4lU+U+VAreRFd9sfQe389ea9XzW6ZMtJsFtzmlO0ueJghWaZcfU Y3wyqXPDhKBVWe08SdaBrvclcuHcrXg5vfbVw7E177iA2W8nXf5FQ7P7vz8oG75KCllJ i3UQ== X-Forwarded-Encrypted: i=1; AJvYcCW5VG8K3m6tNKVoDICUyxaRQqBgbh3Y/08nIG7aswzPMLUsBTgLD4zi2oJPdInJw/On3d+7s8QB1seVn+U=@vger.kernel.org X-Gm-Message-State: AOJu0YzIJqMHY3k6kkC2SBlXHQEttrDcOmX26gx9VouK575ak6teHOp1 NaYs5erbxJT52RuvqYpIKVO5W1tOlZC85PvFx0eVkqh6mdW5B7fpu5Zb24YSs6RQPi08mJdU3hA Q3tVeN3bWrgcBSTlYGg== X-Google-Smtp-Source: AGHT+IH89J64zE2NXOt1UbIsoIv8ScbC1TQBKu1V12tiHCNKpRgg8fkg8u3L50VNaE6PwBj40zcg6KnV599fFo4f X-Received: from pgbca2.prod.google.com ([2002:a05:6a02:682:b0:801:d700:ff4]) (user=yosryahmed job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:12cd:b0:1ea:df1a:f8db with SMTP id adf61e73a8af0-1eadf1afa71mr10920455637.37.1736804701144; Mon, 13 Jan 2025 13:45:01 -0800 (PST) Date: Mon, 13 Jan 2025 21:44:58 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.47.1.688.g23fc6f90ad-goog Message-ID: <20250113214458.2123410-1-yosryahmed@google.com> Subject: [PATCH] mm: zswap: move allocations during CPU init outside the lock From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Nhat Pham , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 dur= ing CPU hotunplug") Signed-off-by: Yosry Ahmed Reviewed-by: Chengming Zhou --- 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, s= truct hlist_node *node) { struct zswap_pool *pool =3D hlist_entry(node, struct zswap_pool, node); struct crypto_acomp_ctx *acomp_ctx =3D per_cpu_ptr(pool->acomp_ctx, cpu); - struct crypto_acomp *acomp; - struct acomp_req *req; + struct crypto_acomp *acomp =3D NULL; + struct acomp_req *req =3D NULL; + u8 *buffer =3D NULL; int ret; =20 - mutex_lock(&acomp_ctx->mutex); - acomp_ctx->buffer =3D kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node= (cpu)); - if (!acomp_ctx->buffer) { + buffer =3D kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu)); + if (!buffer) { ret =3D -ENOMEM; - goto buffer_fail; + goto fail; } =20 acomp =3D 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, s= truct hlist_node *node) pr_err("could not alloc crypto acomp %s : %ld\n", pool->tfm_name, PTR_ERR(acomp)); ret =3D PTR_ERR(acomp); - goto acomp_fail; + goto fail; } - acomp_ctx->acomp =3D acomp; - acomp_ctx->is_sleepable =3D acomp_is_async(acomp); =20 - req =3D acomp_request_alloc(acomp_ctx->acomp); + req =3D acomp_request_alloc(acomp); if (!req) { pr_err("could not alloc crypto acomp_request %s\n", pool->tfm_name); ret =3D -ENOMEM; - goto req_fail; + goto fail; } - acomp_ctx->req =3D req; =20 + /* + * 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, s= truct hlist_node *node) acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, crypto_req_done, &acomp_ctx->wait); =20 + acomp_ctx->buffer =3D buffer; + acomp_ctx->acomp =3D acomp; + acomp_ctx->is_sleepable =3D acomp_is_async(acomp); + acomp_ctx->req =3D req; mutex_unlock(&acomp_ctx->mutex); return 0; =20 -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; } =20 --=20 2.47.1.688.g23fc6f90ad-goog