From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-0002e601.pphosted.com (mx0b-0002e601.pphosted.com [148.163.154.28]) (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 7D3324A4839; Fri, 16 Oct 2026 12:46:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=148.163.154.28 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792154776; cv=fail; b=Sed68F2ffP3PV1kTRr5lyAmtaGY+rERxtzFliI8uGWS4CdOb5tY1fcjhBVmSoJztCtQYuwIHi2tTDgWkBZJpYa27HGPXzbqfGabLK00eoevpPuaf+x8ca1i4apIrEWnBbl1FN6KQIkQUyfQI0Pi/BBrn7hFyFyjJZoV6fQYSawI= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792154776; c=relaxed/simple; bh=Vwcbl6RlPqlxUNPVjVTyu6w2PURk76llpJfwb1dvEIY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ofWG03c7f/1q2Q0xqoj4TsNO/8n4+gS68c3qsbbgpVVyKPXDl8xrOvTgBruRfKoJwMsXC6GEYYw1gEsbUIDj2LvfHr1INlkfJY09jGRjqjiPJFlnsdVQ2Z5UOSUNLKHXfNm74+2sTpcDu6+9pUcqKGjKTp6gsdS+OyAmvk1iitk= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com; spf=pass smtp.mailfrom=ti.com; dkim=pass (2048-bit key) header.d=ti.com header.i=@ti.com header.b=SJZibTeT; dkim=pass (1024-bit key) header.d=ticloud.onmicrosoft.com header.i=@ticloud.onmicrosoft.com header.b=Y+5jqiS2; arc=fail smtp.client-ip=148.163.154.28 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ti.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ti.com header.i=@ti.com header.b="SJZibTeT"; dkim=pass (1024-bit key) header.d=ticloud.onmicrosoft.com header.i=@ticloud.onmicrosoft.com header.b="Y+5jqiS2" Received: from pps.filterd (m0374955.ppops.net [127.0.0.1]) by mx0b-0002e601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 69GBQKZX695366; Fri, 16 Oct 2026 07:46:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= proofpoint-05-2026; bh=/Bv3aNiD+UztgUtisx6llOJvpb5uLQKx03KzyThoF t0=; b=SJZibTeTy6dU2LBNhtxSwJl6+5+6qrJMH5tX2n8lw53aftK9rRSjmEv3b 3kZ6QPo8xD/vEpJYaQXyiMo9W076mK6IkAX/zk8r56fiTFgMnQpZiN0N23+dqU6O Dvbrxa2tBXRS1m5aA1OXeLPLNbBrob/bZwyx3AAu0wR/TLqeSAKx5Ko1bYiqiG8K WNpnxGxXroJhK4Kyx86hDoLrOIlDgiz00bMxla9wW8dRVt+NzC/RyAdzCsmGG4aM XmIdLy7k7y6yfEk/gi1G+g6EGhKR79QgHSPPOBob2s2CLENSCAE2XQcK7oWTyCyb 04rln+1FeBgbepMyA74Qun/btHOrQ== Received: from mw6pr02cu001.outbound.protection.outlook.com (mail-westus2azon11012058.outbound.protection.outlook.com [52.101.48.58]) by mx0b-0002e601.pphosted.com (PPS) with ESMTPS id 4hbhbd8sgr-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 16 Oct 2026 07:46:09 -0500 (CDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Gcm26BE29Ei45SG14O5Gegsx8OvVtCC+13dHlDw8boc9qlDSVjHwye7YRRjEyT6VKNeZZJPX4GOth1kh2LZLjqEoRT0TLaZILqO0Lpq5hbinT0f8EmChRNlMxlr76zc++Vh08mjIrHYKzjJ/anDSlHqbq2McXQfnLbFQe820bfPnLGPHtSVp/XTJO31KwqNyB+s8sH4/vmAMtsCYxdztjBvrY+uslP5eQKKCUpOJ+aACNpDM/trVejf133B4/QCUHgd8CWrpti/eUm5lKNcDk1ATVRpRG5dmtbrMwPtcwru4xoXP88nI4owXdU5iOp8a/9FFQhiSxAb1Zk1QpQhi+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/Bv3aNiD+UztgUtisx6llOJvpb5uLQKx03KzyThoFt0=; b=HKexitMqtXkjFHJDAA54lEcNBv90HITt7dP7NBFRiaaVuiavldmpg9ItWIKDhsCFAEq8eG829Jcr6H99truXoRqRGCT220RIBCSvPiTBw3sRGAZC/mP9VDqodeO6jwgCVkZMEBKB3NOxNlMyjbMQ7UW+gPusWRBIqZSNPe+LVk8yObHhJuyOx22k/VM4F6u+BG4PGNv6I1Ap0H01aw04g0eP5LhpLtc19YvXH7th5bJ4YRkjFcS5L8MGjEeaE2X5K8r9AMS0h+uc/LJhvgcOLzffFEii9/2ozbWMFDfZVbzj5g3kDTriy4O5ibr5/2OZdz5urcFDD4XXEN6OpnwLBA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 198.47.23.194) smtp.rcpttodomain=gondor.apana.org.au smtp.mailfrom=ti.com; dmarc=pass (p=quarantine sp=none pct=100) action=none header.from=ti.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ticloud.onmicrosoft.com; s=selector1-ticloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/Bv3aNiD+UztgUtisx6llOJvpb5uLQKx03KzyThoFt0=; b=Y+5jqiS2YoTTItaCXdAuyIyM8NhDj6TwR8HG2SMErG7CBIpaHIDY/aDwy8p25pqfnKbvZsvq//yarFkM7sFC6sAeMa/g1trNmDML01ExYAuc2wj5uOJuz3yXej+MThot1WiwgcpGAdWLf3mxNWxjQOE9T24zu09xXFtSb2xAhm4= Received: from BN9PR03CA0133.namprd03.prod.outlook.com (2603:10b6:408:fe::18) by MN6PR10MB8047.namprd10.prod.outlook.com (2603:10b6:208:4f2::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.546.7; Fri, 16 Oct 2026 12:45:53 +0000 Received: from BL02EPF00029928.namprd02.prod.outlook.com (2603:10b6:408:fe:cafe::27) by BN9PR03CA0133.outlook.office365.com (2603:10b6:408:fe::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.496.5 via Frontend Transport; Fri, 16 Oct 2026 12:45:53 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 198.47.23.194) smtp.mailfrom=ti.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ti.com; Received-SPF: Pass (protection.outlook.com: domain of ti.com designates 198.47.23.194 as permitted sender) receiver=protection.outlook.com; client-ip=198.47.23.194; helo=lewvzet200.ext.ti.com; pr=C Received: from lewvzet200.ext.ti.com (198.47.23.194) by BL02EPF00029928.mail.protection.outlook.com (10.167.249.53) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.522.6 via Frontend Transport; Fri, 16 Oct 2026 12:45:52 +0000 Received: from DLEE212.ent.ti.com (157.170.170.114) by lewvzet200.ext.ti.com (10.4.14.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 16 Oct 2026 07:45:26 -0500 Received: from DLEE214.ent.ti.com (157.170.170.117) by DLEE212.ent.ti.com (157.170.170.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 16 Oct 2026 07:45:26 -0500 Received: from lelvem-mr05.itg.ti.com (10.180.75.9) by DLEE214.ent.ti.com (157.170.170.117) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45 via Frontend Transport; Fri, 16 Oct 2026 07:45:26 -0500 Received: from pratham-Workstation-PC (pratham-workstation-pc.dhcp.ti.com [10.24.53.205]) by lelvem-mr05.itg.ti.com (8.18.1/8.18.1) with ESMTP id 69GCjPQl2531296; Fri, 16 Oct 2026 07:45:26 -0500 From: T Pratham To: T Pratham , Herbert Xu , "David S. Miller" CC: Sebin Francis , Manorit Chawdhry , Praneeth Bajjuri , Vishal Mahaveer , , Subject: [PATCH v10 04/15] crypto: ti - Fix use-after-free of dev_data on DTHEv2 driver removal Date: Fri, 16 Oct 2026 18:14:45 +0530 Message-ID: <20261016124456.1802080-5-t-pratham@ti.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261016124456.1802080-1-t-pratham@ti.com> References: <20261016124456.1802080-1-t-pratham@ti.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF00029928:EE_|MN6PR10MB8047:EE_ X-MS-Office365-Filtering-Correlation-Id: 00b59c1b-7bac-48f2-3521-08df2b8369a1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|82310400026|23010399003|1800799024|376014|6133799003|18002099003|22082099003|10067099003|261009223027099003|56012099006; X-Microsoft-Antispam-Message-Info: MogZDceWEiMT9uoWxNpgiuxWAWusr2jrLjqRaUAxhTOW0IoDq89P8MMJNW1CXyWQy3HiY5OpC5c1qYZRpRTwtOaVD0M7DA2bTmNbyLAxnE4ndpW+70/a2mnfzOZjqtjRglO3KIDzB+GGz068TDVpFzk0QBruugnmwPWWEQQSC1gl/RSR/08OJglHsflEuw8qVsx+/mt3fA+JNB0eFcrTgiVwa6FIz6BiLuVTsGE16k1QWmipzRwfDxM1Je57rWVVIzFDNeISchyYWHxUg2Opx6YnM69t/R4u9f9l35g4dbwsku2W0fIdjoK7YLXER6ydul939dyY2oe+46s161CIzwqgrls0Pv/pnLsD/tS1fvFOkzbnao7Pc70UlrHQe2snVtPRjjr6MrGnHGo7KMd1AviU6vNLPM4SY7n/hQvK7W2upIYzjJgZmjsfXLKZ89RjSo55b/Rd+FjGH2po4yR60Lnpe/9kz6WVQFjum275X71/KYKtaf8NC7iIlVAyHuAp2+d8ej3AJl6jsuGdJuyYKFAfqUylEXUIZUkpZEf5awsl+xmSJ9uWu5W0Y7U7agolm7GVaNLhT/5U+RmNkwCyvDM9Nyk+43S7Oq/hIwLINXkMrvAshuCCu7EfIomq1D4uhSvETuwWwZ9XATv2wJP2LqR3eAvLBJ3/BHqy45Hh+DhSu19RXcj2FwWms1fgt2OsQhO/ZquMkR1NrctLWVTRVg== X-Forefront-Antispam-Report: CIP:198.47.23.194;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:lewvzet200.ext.ti.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700016)(82310400026)(23010399003)(1800799024)(376014)(6133799003)(18002099003)(22082099003)(10067099003)(261009223027099003)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9wf8oV6flSUdcpQnLXrQRzaFLL/7usPDlbHs4AdfvxF5rnbsqgD9+NJSc9kGwrtIX46jrjeqMT6aGR1InV9QhDz5vBJoT0gkggFY/Ru8s0QkkyZtFMbqF3Jiwi8zR61+Dhoa692hGqyRUfTlRFT7MMQS16JLtd6BLlOyzzg+eC3qo0jFn72GZxqP9BpGsYa6jo0KMp1dY9/CgmT2ctVx60VDVjuiuHlOojn9OO3XCz4OxqGi7lfPmBZ5J4ogJCc+ZE93fKQU8fUzNs+NbuReNbyTSgj0SHm8kc0b2d0pTP9G+N+VsyleypxkNb/OUqEsBC9yxctJstp5G4yiX2Wsxa6povdcy+efSSVPnHIWDfM9KqSE7P+xE4psTV6mm/eKClu0na1SUkkm3hpsfIfoVy6xyR6MUF4c9Ov2orE56tmIv86ShtQT778wsSWGgEIj X-Exchange-RoutingPolicyChecked: LdsgAA66BI235iUmqpu1/n+JoczmmsPYkij/GcsC4p7BLmZdFNt3NwDQJXTwAszEHo9+E1zOycN0G3Dair3hxqnysMFWmzBzReWabB5cJwErEQu+bLhX61F6y55iwASAhEKikhmk2sdGCuvGuErlezLgimzo+6DfmMtSRcUuhMEYs+tPcW/KfNx4nJEGKwGNbNhRemtu95cGxQITFgh8h+GlMxoLoC/PcDhkwrgw69vuWfU7Jihb/nXddALigs8IoJyWCnhBXUf6669ApzSXb/tnbsFL4KrS+uQJArOWpd0GFb2qQBIF9QBYrQE/2nk1og2LOcAyKNGCzZhyY0FO4g== X-OriginatorOrg: ti.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2026 12:45:52.1643 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 00b59c1b-7bac-48f2-3521-08df2b8369a1 X-MS-Exchange-CrossTenant-Id: e5b49634-450b-4709-8abb-1e2b19b982b7 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=e5b49634-450b-4709-8abb-1e2b19b982b7;Ip=[198.47.23.194];Helo=[lewvzet200.ext.ti.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF00029928.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN6PR10MB8047 X-Proofpoint-Spam-Info: AW1haW4tMjYxMDE2MDA1MCBTYWx0ZWRfX4g2/vT5HVV0A x8aL79KX5X3ygavTIK5OIqv5nLSKuf7mEYL7av1xzMo8mZnWuFlqbTIl9GgSuf58WVJrSiubz3u STw7MK9et00dpG9hAUbRCqxm9SDsTFU= X-Proofpoint-ORIG-GUID: 7XbJwFkgC-KgsUoYjr1yN3x05fnjLr4H X-Authority-Analysis: v=2.4 cv=BooIUoX5 c=1 sm=1 tr=0 ts=6ad21c91 cx=c_pps a=KcDtNT9Y7DbXMZTh6VQ3dg==:117 a=WotqVVQAdb04rnGuttW3Kw==:17 a=6eWqkTHjU83fiwn7nKZWdM+Sl24=:19 a=660iZSQnnn4A:10 a=s63m1ICgrNkA:10 a=AlMIdn_sM9wA:10 a=VkNPw1HP01LnGYTKEx00:22 a=Z8NIEmU8O1QQgoT56wFK:22 a=fPAWb5peG099m5CrUpKH:22 a=sozttTNsAAAA:8 a=QiMhJC1N2krko7GUhYQA:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMDE2MDA1MCBTYWx0ZWRfX+xXibkiMEfVH GxcdSc3jNZl0msIKZ0KezX3lX/JgTePNgo0A8qHGZAIC/iwcw5m/cPzChuQOMLdUymkTjXnM7Ea CuRoqf1KM1IVyPWeH3C3GXuzZfLwAkelCkxTbfHC6IOZMeYvYS6hU0waogdk/qCuKpFyd3fN25M GfsHmYIt9AZSnrh1OZjHjw26dOKDW8XfKdq6zH8DaGBl32nLlIPrNLFuoXkVmRixTG+N2BFTOqe O19ZGq7Sxz2TsS0WP12ZS5fdWJL0OEQQYlfds2D1PqwIpR1pRKmC7X1CK+huKgrl3k/jMbH/NKA N+S1SO8saDLT4MI8ce+oWGGwWcrkevgknoppIZXYJuBiDfYmKDSpr8c1/J2X0sLiNqaCQsg3xTT LpKHpllpriXZ9xAX/l0mUam7AhgmNNO5Q7Jbzc2JOPvEEs6Fa5w7vnUJWqoP+89pRNDtb4Aa57P YiIkKhLXWUNxixkmB5g== X-Proofpoint-GUID: 7XbJwFkgC-KgsUoYjr1yN3x05fnjLr4H X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-10-16_01,2026-10-14_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 spamscore=0 malwarescore=0 lowpriorityscore=0 bulkscore=0 phishscore=0 adultscore=0 priorityscore=1501 impostorscore=0 suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2610080000 definitions=main-2610160050 Each *_init_tfm() caches a pointer to the per-instance struct dthe_data in its transform context (ctx->dev_data), but never takes a reference on it. If there are tfms in progress when dthe_remove() is called, the devm allocated dev_data gets freed. Then ctx->dev_data will point to a memory that has been freed. Move dthe_data to req_ctx structs of algorithms, and store the device pointer in tfm_ctx. Add a kref refcnt to struct dthe_data, which maintains a count of number of requests enqueued in the crypto engine queue which reference the dthe_data object. On reaching zero, this counter triggers a completion which is waited on in dthe_remove() before doing the driver teardown. Fixes: 52f641bc63a46 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)") Signed-off-by: T Pratham --- drivers/crypto/ti/dthev2-aes.c | 74 +++++++++++++++++++++---------- drivers/crypto/ti/dthev2-common.c | 49 +++++++++++++++++--- drivers/crypto/ti/dthev2-common.h | 52 ++++++++++++++++++++-- 3 files changed, 143 insertions(+), 32 deletions(-) diff --git a/drivers/crypto/ti/dthev2-aes.c b/drivers/crypto/ti/dthev2-aes.c index 4fdd24ee91637..789b336d093f4 100644 --- a/drivers/crypto/ti/dthev2-aes.c +++ b/drivers/crypto/ti/dthev2-aes.c @@ -110,12 +110,11 @@ enum aes_ctrl_mode_masks { static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm) { struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm); - struct dthe_data *dev_data = dthe_get_dev(ctx); - if (!dev_data) + ctx->dev = dthe_get_dev(); + if (!ctx->dev) return -ENODEV; - ctx->dev_data = dev_data; ctx->keylen = 0; return 0; @@ -124,20 +123,19 @@ static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm) static int dthe_cipher_init_tfm_fallback(struct crypto_skcipher *tfm) { struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm); - struct dthe_data *dev_data = dthe_get_dev(ctx); const char *alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(tfm)); - if (!dev_data) + ctx->dev = dthe_get_dev(); + if (!ctx->dev) return -ENODEV; - ctx->dev_data = dev_data; ctx->keylen = 0; - ctx->skcipher_fb = crypto_alloc_sync_skcipher(alg_name, 0, CRYPTO_ALG_NEED_FALLBACK); if (IS_ERR(ctx->skcipher_fb)) { - dev_err(dev_data->dev, "fallback driver %s couldn't be loaded\n", + dev_err(ctx->dev, "fallback driver %s couldn't be loaded\n", alg_name); + dthe_put_dev(ctx->dev); return PTR_ERR(ctx->skcipher_fb); } @@ -149,6 +147,7 @@ static void dthe_cipher_exit_tfm(struct crypto_skcipher *tfm) struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm); crypto_free_sync_skcipher(ctx->skcipher_fb); + dthe_put_dev(ctx->dev); } static int dthe_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen) @@ -225,7 +224,7 @@ static void dthe_aes_set_ctrl_key(struct dthe_tfm_ctx *ctx, struct dthe_aes_req_ctx *rctx, u32 *iv_in) { - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data = rctx->dev_data; void __iomem *aes_base_reg = dev_data->regs + DTHE_P_AES_BASE; u32 ctrl_val = 0; @@ -337,8 +336,8 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq) { struct skcipher_request *req = container_of(areq, struct skcipher_request, base); struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); - struct dthe_data *dev_data = dthe_get_dev(ctx); struct dthe_aes_req_ctx *rctx = skcipher_request_ctx(req); + struct dthe_data *dev_data = rctx->dev_data; unsigned int len = req->cryptlen; struct scatterlist *src = req->src; @@ -520,14 +519,17 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq) local_bh_disable(); crypto_finalize_skcipher_request(dev_data->engine, req, ret); local_bh_enable(); + dthe_put_drvdata(dev_data); return 0; } static int dthe_aes_crypt(struct skcipher_request *req) { struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_aes_req_ctx *rctx = skcipher_request_ctx(req); + struct dthe_data *dev_data; struct crypto_engine *engine; + int ret; /* * If data is not a multiple of AES_BLOCK_SIZE: @@ -553,8 +555,18 @@ static int dthe_aes_crypt(struct skcipher_request *req) return 0; } + dev_data = dthe_get_drvdata(ctx->dev); + if (!dev_data) + return -ENODEV; + + rctx->dev_data = dev_data; + engine = dev_data->engine; - return crypto_transfer_skcipher_request_to_engine(engine, req); + ret = crypto_transfer_skcipher_request_to_engine(engine, req); + if (ret != -EINPROGRESS && ret != -EBUSY) + dthe_put_drvdata(dev_data); + + return ret; } static int dthe_aes_encrypt(struct skcipher_request *req) @@ -576,19 +588,18 @@ static int dthe_aes_decrypt(struct skcipher_request *req) static int dthe_aead_init_tfm(struct crypto_aead *tfm) { struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm); - struct dthe_data *dev_data = dthe_get_dev(ctx); const char *alg_name = crypto_tfm_alg_name(crypto_aead_tfm(tfm)); - if (!dev_data) + ctx->dev = dthe_get_dev(); + if (!ctx->dev) return -ENODEV; - ctx->dev_data = dev_data; - ctx->aead_fb = crypto_alloc_sync_aead(alg_name, 0, CRYPTO_ALG_NEED_FALLBACK); if (IS_ERR(ctx->aead_fb)) { - dev_err(dev_data->dev, "fallback driver %s couldn't be loaded\n", + dev_err(ctx->dev, "fallback driver %s couldn't be loaded\n", alg_name); + dthe_put_dev(ctx->dev); return PTR_ERR(ctx->aead_fb); } @@ -600,6 +611,7 @@ static void dthe_aead_exit_tfm(struct crypto_aead *tfm) struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm); crypto_free_sync_aead(ctx->aead_fb); + dthe_put_dev(ctx->dev); } /** @@ -711,9 +723,9 @@ static struct scatterlist *dthe_aead_prep_crypt(struct scatterlist *sg, return crypt_sg; } -static int dthe_aead_read_tag(struct dthe_tfm_ctx *ctx, u32 *tag) +static int dthe_aead_read_tag(struct dthe_aes_req_ctx *rctx, u32 *tag) { - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data = rctx->dev_data; void __iomem *aes_base_reg = dev_data->regs + DTHE_P_AES_BASE; u32 val; int ret; @@ -734,11 +746,12 @@ static int dthe_aead_read_tag(struct dthe_tfm_ctx *ctx, u32 *tag) static int dthe_aead_enc_get_tag(struct aead_request *req) { struct dthe_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); + struct dthe_aes_req_ctx *rctx = aead_request_ctx(req); u32 tag[AES_BLOCK_WORDS]; int nents; int ret; - ret = dthe_aead_read_tag(ctx, tag); + ret = dthe_aead_read_tag(rctx, tag); if (ret) return ret; @@ -753,12 +766,13 @@ static int dthe_aead_enc_get_tag(struct aead_request *req) static int dthe_aead_dec_verify_tag(struct aead_request *req) { struct dthe_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); + struct dthe_aes_req_ctx *rctx = aead_request_ctx(req); u32 tag_out[AES_BLOCK_WORDS]; u32 tag_in[AES_BLOCK_WORDS]; int nents; int ret; - ret = dthe_aead_read_tag(ctx, tag_out); + ret = dthe_aead_read_tag(rctx, tag_out); if (ret) return ret; @@ -859,7 +873,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) struct aead_request *req = container_of(areq, struct aead_request, base); struct dthe_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); struct dthe_aes_req_ctx *rctx = aead_request_ctx(req); - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data = rctx->dev_data; unsigned int cryptlen = req->cryptlen; unsigned int assoclen = req->assoclen; @@ -1130,6 +1144,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) local_bh_disable(); crypto_finalize_aead_request(engine, req, ret); local_bh_enable(); + dthe_put_drvdata(dev_data); return 0; } @@ -1137,10 +1152,11 @@ static int dthe_aead_crypt(struct aead_request *req) { struct dthe_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); struct dthe_aes_req_ctx *rctx = aead_request_ctx(req); - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data; struct crypto_engine *engine; unsigned int cryptlen = req->cryptlen; bool is_zero_ctr = true; + int ret; /* In decryption, last authsize bytes are the TAG */ if (!rctx->enc) @@ -1191,8 +1207,18 @@ static int dthe_aead_crypt(struct aead_request *req) (ctx->aes_mode == DTHE_AES_CCM && !is_zero_ctr)) return dthe_aead_do_fallback(req); + dev_data = dthe_get_drvdata(ctx->dev); + if (!dev_data) + return -ENODEV; + + rctx->dev_data = dev_data; + engine = dev_data->engine; - return crypto_transfer_aead_request_to_engine(engine, req); + ret = crypto_transfer_aead_request_to_engine(engine, req); + if (ret != -EINPROGRESS && ret != -EBUSY) + dthe_put_drvdata(dev_data); + + return ret; } static int dthe_aead_encrypt(struct aead_request *req) diff --git a/drivers/crypto/ti/dthev2-common.c b/drivers/crypto/ti/dthev2-common.c index ca8bab4950d28..9ab97b72f7ed2 100644 --- a/drivers/crypto/ti/dthev2-common.c +++ b/drivers/crypto/ti/dthev2-common.c @@ -31,22 +31,54 @@ static struct dthe_list dthe_dev_list = { .lock = __SPIN_LOCK_UNLOCKED(dthe_dev_list.lock), }; -struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx) +struct device *dthe_get_dev(void) { struct dthe_data *dev_data; - - if (ctx->dev_data) - return ctx->dev_data; + struct device *dev = NULL; spin_lock_bh(&dthe_dev_list.lock); dev_data = list_first_entry_or_null(&dthe_dev_list.dev_list, struct dthe_data, list); - if (dev_data) + if (dev_data) { list_move_tail(&dev_data->list, &dthe_dev_list.dev_list); + dev = get_device(dev_data->dev); + } + spin_unlock_bh(&dthe_dev_list.lock); + + return dev; +} + +void dthe_put_dev(struct device *dev) +{ + put_device(dev); +} + +struct dthe_data *dthe_get_drvdata(struct device *dev) +{ + struct dthe_data *dev_data; + + spin_lock_bh(&dthe_dev_list.lock); + dev_data = dev_get_drvdata(dev); + if (dev_data) + kref_get(&dev_data->refcnt); spin_unlock_bh(&dthe_dev_list.lock); return dev_data; } +static void dthe_release_drvdata(struct kref *kref) +{ + struct dthe_data *dev_data = container_of(kref, struct dthe_data, refcnt); + + complete(&dev_data->drain_compl); +} + +void dthe_put_drvdata(struct dthe_data *dev_data) +{ + spin_lock_bh(&dthe_dev_list.lock); + kref_put(&dev_data->refcnt, dthe_release_drvdata); + spin_unlock_bh(&dthe_dev_list.lock); +} + struct scatterlist *dthe_copy_sg(struct scatterlist *dst, struct scatterlist *src, unsigned int buflen) @@ -152,6 +184,9 @@ static int dthe_probe(struct platform_device *pdev) if (IS_ERR(dev_data->regs)) return PTR_ERR(dev_data->regs); + kref_init(&dev_data->refcnt); + init_completion(&dev_data->drain_compl); + platform_set_drvdata(pdev, dev_data); spin_lock_bh(&dthe_dev_list.lock); @@ -202,10 +237,14 @@ static void dthe_remove(struct platform_device *pdev) spin_lock_bh(&dthe_dev_list.lock); list_del(&dev_data->list); + platform_set_drvdata(pdev, NULL); spin_unlock_bh(&dthe_dev_list.lock); dthe_unregister_algs(); + dthe_put_drvdata(dev_data); + wait_for_completion(&dev_data->drain_compl); + crypto_engine_exit(dev_data->engine); dma_release_channel(dev_data->dma_aes_rx); diff --git a/drivers/crypto/ti/dthev2-common.h b/drivers/crypto/ti/dthev2-common.h index 75d9a097650da..03a0b426041ac 100644 --- a/drivers/crypto/ti/dthev2-common.h +++ b/drivers/crypto/ti/dthev2-common.h @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -53,6 +54,9 @@ enum dthe_aes_mode { * @dma_aes_rx: AES Rx DMA Channel * @dma_aes_tx: AES Tx DMA Channel * @dma_sha_tx: SHA Tx DMA Channel + * @refcnt: Count of requests currently using this instance. + * @drain_compl: Completed whenever @refcnt drops to zero; waited on by + * dthe_remove(). */ struct dthe_data { struct device *dev; @@ -64,6 +68,9 @@ struct dthe_data { struct dma_chan *dma_aes_tx; struct dma_chan *dma_sha_tx; + + struct kref refcnt; + struct completion drain_compl; }; /** @@ -78,7 +85,7 @@ struct dthe_list { /** * struct dthe_tfm_ctx - Transform ctx struct containing ctx for all sub-components of DTHE V2 - * @dev_data: Device data struct pointer + * @dev: Device this transform is bound to. * @keylen: AES key length * @authsize: Authentication size for modes with authentication * @key: AES key @@ -87,7 +94,7 @@ struct dthe_list { * @skcipher_fb: Fallback crypto skcipher handle for AES-XTS mode */ struct dthe_tfm_ctx { - struct dthe_data *dev_data; + struct device *dev; unsigned int keylen; unsigned int authsize; u32 key[DTHE_MAX_KEYSIZE / sizeof(u32)]; @@ -103,16 +110,55 @@ struct dthe_tfm_ctx { * @enc: flag indicating encryption or decryption operation * @padding: padding buffer for handling unaligned data * @aes_compl: Completion variable for use in manual completion in case of DMA callback failure + * @dev_data: Device data struct pointer */ struct dthe_aes_req_ctx { int enc; u8 padding[2 * AES_BLOCK_SIZE]; struct completion aes_compl; + struct dthe_data *dev_data; }; /* Struct definitions end */ -struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx); +/** + * dthe_get_dev - Get the device pointer after increasing its reference count + * + * Description: + * Picks a device instance and gets the associated device pointer through get_device() + * to maintain its reference count by the kernel itself. Callers must ensure to call + * dthe_put_dev() at exit to decrease the refcnt. + */ +struct device *dthe_get_dev(void); + +/** + * dthe_put_dev - Decrease the reference count of the device + * @dev: Device pointer to be released + * + * Description: + * Decreases the reference count of the device pointer obtained through dthe_get_dev() + * by calling put_device(). + */ +void dthe_put_dev(struct device *dev); + +/** + * dthe_get_drvdata - Get a reference counted device driver data pointer + * @dev: Device pointer + * + * Description: + * Returns the instance's driver data if it is still bound, with @req_refcnt + * incremented, or NULL if it has been removed. Every req accepted for + * processing must call this and balance it with exactly one + * dthe_put_drvdata() when done. + */ +struct dthe_data *dthe_get_drvdata(struct device *dev); + +/** + * dthe_put_drvdata - Decrease the refcnt of the device driver data acquired through + * dthe_get_drvdata() + * @dev_data: Driver data previously returned by dthe_get_drvdata() + */ +void dthe_put_drvdata(struct dthe_data *dev_data); /** * dthe_copy_sg - Copy sg entries from src to dst -- 2.34.1