Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1105496rdb; Fri, 20 Oct 2023 08:33:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFeyV4B4HVlhv118kSghBIb4dY19/QG4JoV/T4cWp2SuEncnRtwfXhFS6PVrgKWB8OtL//R X-Received: by 2002:a05:6870:9f85:b0:1e9:b29b:b145 with SMTP id xm5-20020a0568709f8500b001e9b29bb145mr2388170oab.7.1697816023721; Fri, 20 Oct 2023 08:33:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697816023; cv=none; d=google.com; s=arc-20160816; b=RRDeXDlHBFVbRpAX/qSHKE/3/8USUXfa36KtjL/5/KZsSTxRBHcwln0F5pbWSP8o2B 1zoQmW30t2F4kG91U/tVMXfA23J34ktOtotU4LFRBj4wLnqFAO3Fh7S3TfHvERr8ll7d kpMu9yvPtA4k8bBc7mw9YEBim/+D5wdWjxyrIBNHRLMbaPibAOKP3/JdT8Z2u1yY1hOF mpm2Ryrt7WicQ8KA+HCFcV//GNGoUAEPp2O0S/7z4kKvI1bGsgXe62svCVID/v26H/t2 Y/F4Urff2bITr8Xr79sPY98bE/iRJ1g4P5KbunxmMdmk5SlQeTWSpeUz9kyus3JEksN/ qUgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=GnqCpNYVKqD4Y05R9CQvSi3YX/vX3DR9SzwQ662WqSE=; fh=KOaZlMof93TV1hSyK3azUDJNauYdEQfkmyrWkpVnOMQ=; b=KzAuTIkbRA33AMw9exP5jFFByiWrbvaPCKIlKLqG6ea7lbhgj3jktShBlSnXkPYsRQ qmLvbudlPaSH8Z6waZI3fWhO2nDLBFqqcbN3pTpfVlvd39Qi90U4wjBaAf6HOpUK9ZHt aNvADuv6hWTI+lg1ZebeSEUxKPFRX9y7Zwtj0JSqla4OgcNqexJEGyogF1dEMajehvJV OAOqEqZgGDQQ93EkLIDzs/RHbiAn3B4Ym4YpfzZ7uvHIfE2THyXMXrcIeRqm3sp0HSaJ GjfAy8udcRFoar3PRbsH6TEpLS9OeP+EUF+5+cVG6vSzxogb7twXGAdZ43o96ounoZ5e 852g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WrJ5vVTI; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id 199-20020a6301d0000000b00578af1e2f3csi2120008pgb.426.2023.10.20.08.33.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 08:33:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WrJ5vVTI; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 5CF7B8370D97; Fri, 20 Oct 2023 08:33:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377723AbjJTPdi (ORCPT + 99 others); Fri, 20 Oct 2023 11:33:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377694AbjJTPdh (ORCPT ); Fri, 20 Oct 2023 11:33:37 -0400 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFB3FB3 for ; Fri, 20 Oct 2023 08:33:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697816016; x=1729352016; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=kLmMFuvm33J5Y1y/2ynZCrXV8ariL8txDc3lcc6VFFM=; b=WrJ5vVTI0YeWUId1dF7iFhp2Ky/ZXR4w3Xq7sL8MxVa3W+LzXkY2sv3/ hT+EjDfA0fsZHCxtcyG+FvKkccgkk0Eneqx/aiSVMgj6abg/hu81NJpoX FBGt/c0ZUEyLd6XqJJu0t0RuZ2aLvBbkY9wOuMiarWuzfi1a6mQJGxoPD 0lhJTm0W37rtoFdXfgmZsVRb+Og05S+EQPz9dP9LJCm1maKcgLZOipNa4 fWMPGo8MAAarr4oBV92F7Of/g2h2HoW5a75r5Sw2VMZYrFqzCK3dAj0Wa 2mote04kkrINwu48TNF1ED5gyNgtBzMeL//0OXuLhN6sbM0jqI2JgjcZR w==; X-IronPort-AV: E=McAfee;i="6600,9927,10869"; a="5129115" X-IronPort-AV: E=Sophos;i="6.03,239,1694761200"; d="scan'208";a="5129115" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Oct 2023 08:33:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10869"; a="827760898" X-IronPort-AV: E=Sophos;i="6.03,239,1694761200"; d="scan'208";a="827760898" Received: from unknown (HELO silpixa00400314.ger.corp.intel.com) ([10.237.222.216]) by fmsmga004.fm.intel.com with ESMTP; 20 Oct 2023 08:33:33 -0700 From: Giovanni Cabiddu To: herbert@gondor.apana.org.au Cc: linux-crypto@vger.kernel.org, qat-linux@intel.com, Giovanni Cabiddu , Mikulas Patocka Subject: [PATCH] crypto: qat - fix deadlock in backlog processing Date: Fri, 20 Oct 2023 16:33:21 +0100 Message-ID: <20231020153330.70845-1-giovanni.cabiddu@intel.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Organization: Intel Research and Development Ireland Ltd - Co. Reg. #308263 - Collinstown Industrial Park, Leixlip, County Kildare - Ireland Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Fri, 20 Oct 2023 08:33:38 -0700 (PDT) If a request has the flag CRYPTO_TFM_REQ_MAY_BACKLOG set, the function qat_alg_send_message_maybacklog(), enqueues it in a backlog list if either (1) there is already at least one request in the backlog list, or (2) the HW ring is nearly full or (3) the enqueue to the HW ring fails. If an interrupt occurs right before the lock in qat_alg_backlog_req() is taken and the backlog queue is being emptied, then there is no request in the HW queues that can trigger a subsequent interrupt that can clear the backlog queue. In addition subsequent requests are enqueued to the backlog list and not sent to the hardware. Fix it by holding the lock while taking the decision if the request needs to be included in the backlog queue or not. This synchronizes the flow with the interrupt handler that drains the backlog queue. For performance reasons, the logic has been changed to try to enqueue first without holding the lock. Fixes: 386823839732 ("crypto: qat - add backlog mechanism") Reported-by: Mikulas Patocka Closes: https://lore.kernel.org/all/af9581e2-58f9-cc19-428f-6f18f1f83d54@redhat.com/T/ Signed-off-by: Giovanni Cabiddu --- .../intel/qat/qat_common/qat_algs_send.c | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/drivers/crypto/intel/qat/qat_common/qat_algs_send.c b/drivers/crypto/intel/qat/qat_common/qat_algs_send.c index bb80455b3e81..b97b678823a9 100644 --- a/drivers/crypto/intel/qat/qat_common/qat_algs_send.c +++ b/drivers/crypto/intel/qat/qat_common/qat_algs_send.c @@ -40,40 +40,44 @@ void qat_alg_send_backlog(struct qat_instance_backlog *backlog) spin_unlock_bh(&backlog->lock); } -static void qat_alg_backlog_req(struct qat_alg_req *req, - struct qat_instance_backlog *backlog) -{ - INIT_LIST_HEAD(&req->list); - - spin_lock_bh(&backlog->lock); - list_add_tail(&req->list, &backlog->list); - spin_unlock_bh(&backlog->lock); -} - -static int qat_alg_send_message_maybacklog(struct qat_alg_req *req) +static bool qat_alg_try_enqueue(struct qat_alg_req *req) { struct qat_instance_backlog *backlog = req->backlog; struct adf_etr_ring_data *tx_ring = req->tx_ring; u32 *fw_req = req->fw_req; - /* If any request is already backlogged, then add to backlog list */ + /* Check if any request is already backlogged */ if (!list_empty(&backlog->list)) - goto enqueue; + return false; - /* If ring is nearly full, then add to backlog list */ + /* Check if ring is nearly full */ if (adf_ring_nearly_full(tx_ring)) - goto enqueue; + return false; - /* If adding request to HW ring fails, then add to backlog list */ + /* Try to enqueue to HW ring */ if (adf_send_message(tx_ring, fw_req)) - goto enqueue; + return false; - return -EINPROGRESS; + return true; +} -enqueue: - qat_alg_backlog_req(req, backlog); - return -EBUSY; +static int qat_alg_send_message_maybacklog(struct qat_alg_req *req) +{ + struct qat_instance_backlog *backlog = req->backlog; + int ret = -EINPROGRESS; + + if (qat_alg_try_enqueue(req)) + return ret; + + spin_lock_bh(&backlog->lock); + if (!qat_alg_try_enqueue(req)) { + list_add_tail(&req->list, &backlog->list); + ret = -EBUSY; + } + spin_unlock_bh(&backlog->lock); + + return ret; } int qat_alg_send_message(struct qat_alg_req *req) base-commit: 2ce0d7cbc00a0fc65bb26203efed7ecdc98ba849 -- 2.41.0