From patchwork Mon Jan 13 21:44:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13938105 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 625ABC02180 for ; Mon, 13 Jan 2025 21:45:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD91F6B008C; Mon, 13 Jan 2025 16:45:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D897F6B0092; Mon, 13 Jan 2025 16:45:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C50B96B0093; Mon, 13 Jan 2025 16:45:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A766E6B008C for ; Mon, 13 Jan 2025 16:45:04 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4FB16120376 for ; Mon, 13 Jan 2025 21:45:04 +0000 (UTC) X-FDA: 83003759328.25.6C5CEC9 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf23.hostedemail.com (Postfix) with ESMTP id 8615514000D for ; Mon, 13 Jan 2025 21:45:02 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FUCRdPlW; spf=pass (imf23.hostedemail.com: domain of 3XYmFZwoKCMgC265Cov0sru22uzs.q20zw18B-00y9oqy.25u@flex--yosryahmed.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3XYmFZwoKCMgC265Cov0sru22uzs.q20zw18B-00y9oqy.25u@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736804702; a=rsa-sha256; cv=none; b=zQfBREZYlDXBVpz1VK7qiQ9oNCFvpITgv28PbyLyMDkrEoPY1edjSK1AmtoM9aa9SQOJ59 PzV443Ek1fxEZjz+uTuoekVxDvCchAtPGLyGsEV/jir1NCsSXBaEcRsFJZW2rBDk+ljRdJ 8IyczaTQePI+PPRFb7X2XWv67SUGDEU= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FUCRdPlW; spf=pass (imf23.hostedemail.com: domain of 3XYmFZwoKCMgC265Cov0sru22uzs.q20zw18B-00y9oqy.25u@flex--yosryahmed.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3XYmFZwoKCMgC265Cov0sru22uzs.q20zw18B-00y9oqy.25u@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736804702; 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:in-reply-to: references:dkim-signature; bh=1k/4ZQBA6kAtaFMix8Jlwq35c0dl3BB3kwSA0fYZuyI=; b=TMIruujgDzj906dH/lMZ33ck6MEKJQC5u3qy59vtRxJgpOprU7HdmrfwDjJxmn37UoLt1/ a7OWnXLi6FEQx2n8Cw8YpwxqfaDZmLEFEbGCkYp9IWgZBhEVXeFWQ4pzx7oyuEp7b3vy/o FO/mOnHmtewJvzdC+sTaQxLKlzNNzDc= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21681a2c0d5so84898835ad.2 for ; Mon, 13 Jan 2025 13:45:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736804701; x=1737409501; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=1k/4ZQBA6kAtaFMix8Jlwq35c0dl3BB3kwSA0fYZuyI=; b=FUCRdPlWMfNH+XmJZ3fNIPBgLC8iOhAvKZu9Lm2I8Wk9P4PEnVSffbuOnboH4lAylS q/wq8hYuFS9Pk4NhhvcQhkWVJlG/sZuXBsouSgaIR+RgT43X38p9xnQxxMEqM/66aDl6 O2orhZTC6bUTaXeP75jFMxKuGqblZIwf+d09w6mrbCzg/SSahf57pu/gM+GtE6inaO4z KVZHhowwC8GPOoTKj5z8PswQPyECYX158xDcTE3ycoabus3LExYCEHoZBYCH+KSZAvqS W5XsOpIdeu7+kUSBvk3VxP8t9AT9FfSFfqLkIE5a5uiMWi+WYvoGcN1OAURTX4WS3wnM ljyg== 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=J3x1tiLjjho/rmzmqiqLH0mHW+jFspK3oW1dZw4hTEvFhgc/0ruFRUQvJie7ZAKdOf pcQGCYkesbguTn6iHtOkIA9QK7Q8Hph9UUzh6DEavt6MAvtT/xia/ADLxSZVcJkT9o74 DwL26i5uVq/kWm2H1mP1ll4OQioQX1NJBt0Aj/eMdLb8ZNkuLKc/C9b8+QmDtdDk7KOd rWQAibzAj3aBsRqnkE4cDcGlQ60bm64Pq2d7OuOUBRgPyNaN1PUvYovGNxHAV/Rcs6ic B8xMeLRml/jxsyYBXxTFII+Fg7Qp8dV1DtdRWfy3FTTBmOJHYVqfqwq3Cn3y0dQ9t/ft bWTg== X-Forwarded-Encrypted: i=1; AJvYcCWBKQpyQzcshQFqRK4RjoL3GQgpuyUXLoQ4rX+P5z3MbuIUiBD7Tf5e+ydWRMuFxA2jZ+NpNsEImA==@kvack.org X-Gm-Message-State: AOJu0Yw0+z3JbzZwNhd6+fL4c3psuaSGKu4nFLEYDVkSgRkwndl9bEAf GbU1MFID9jbb3lQKEjpK0uCLc5wVfwuwVVDUb37MZ8FirCwEzqLnro76/esGPiE1O/tG7zfDI87 dYOJZMMExma6tcl9T3g== 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 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 X-Rspamd-Queue-Id: 8615514000D X-Stat-Signature: ybcehqotzh4apr96ko356zdpdphba8ap X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1736804702-600533 X-HE-Meta: U2FsdGVkX19Wim2d8sEMD+1+nn8EVKDqIr/MWGsLgxG7wzd3TS+5EcRGSr2A4F3vt5KasGHW2Dgo6bcGM9nn6d1byS5gnWwDVncVtape5ULUsWfwgv9j/1RayarL1/BeWOyLXoYd2GY97IarEBVsCWs07SyLsGeUa5JGXT2afi5q6Z0IcWYN/RU7fDaiErV3lli6YZY6PnH08+eieaQJa7GGkIcg17Y+BZIIzc/VVtOeBPKuceIu71uVuC7/pCm+TC9lIo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Fa321zYJhKRqsPyTzPXm6frf/Bb32xNArz4h/PabI38U084rpECs5AkNhYVYVmcKVUuKUgNcf85JtkmffLWogf2Ag9bvIW9w2myQDdeQx2ddPr1kNXWhFCypuBQ2L1HO1xy8vaHO6igksJUg6z8o/7Y6/eCN4HOjtPvMRJNsMyQe1ynaVPW3VRRDGC7ySDBV/4MZhBTyfyWL18M7i7va3x7kzimXTFvcHTUWo8OyrsSSs+KvRs2EC 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: 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 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, 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; }