From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 3A39A379982 for ; Mon, 29 Jun 2026 02:38:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782700733; cv=none; b=qZFBFpFLxNyxlcCgmVPHbtjNYSdWaRdeTEw585gbrjfiIC33+WyndVurcu9DEM31K2dL+NJm2oVbOXCJ/337BhSOkzRL6dsRlvgroYAHMHq6lTZL07nuzjegqayg2xZohZasWoiyGaRqaa44uy/Mg56PUIYDPnX9Uo5xuUiGTpk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782700733; c=relaxed/simple; bh=2XanIgMQF7FbF77eiEwNF8U6Cy3OAoH2Y2kGqFdSdBs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KZc7bYKVgoB3zzRC749CmA0jHOXW1I0OILa3HCgmbb3PdjixHveXlISk9E5u1+oeHPM2sTmHkPLNDwm87nd0Rsh87rNCFOycIVuNp9yjcO1mQoSwfZeHVkeRS70Tk6p4fq8kicFqV2u/iPHI0/oN147dFDIt3V4Q2G5NuamfKgU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mb7K0fzV; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mb7K0fzV" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-845537740ddso1506088b3a.0 for ; Sun, 28 Jun 2026 19:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782700730; x=1783305530; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Tbxicf9cOISRpJE96mgO+H/y2TlKZ0WdbLHi50c/C7k=; b=mb7K0fzV+Jq2s7LAwbQWN5KGInfo85iFhwRjJuj/AbYAC+sC9EZ/j9d5gob5ac2wjn EbID/8tSvXzuF2IFPlLu32k23l7Q+2SqrtwwWaCSjHE/OsbMVTLF86cWXRnR8qqz00se vWDPTuoQbAC3W73jxGL7giNQJSNt81SBpaWR5xYca5WD7rLiHaYNDNteW5ZjtVA2Jq9l QyMe33CQ3MmApTU4DywQ7pl28X98P2oAn+nKX1fstvdrczxgarfEYdcW2cTU5znlnxEv GREuz39OqEcNAkve9umyy2Sk8MmX5FjAmz7t5IJPMIWztjekCCtZMq+Sdpy/p9mIJuQZ CYrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782700730; x=1783305530; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Tbxicf9cOISRpJE96mgO+H/y2TlKZ0WdbLHi50c/C7k=; b=rXKhK8dohDxdV01ftZKJWiR2NOrbdf0jdrN/CS7uRyskMzzIajg4p88to5PkGthADi yjhyE8jntGh0mE/OIhnqrefVzu1GHQge3a/BMqT33VdcpOlrVhBIj8q/yoytMXEa0q1g Xu/n4EQMy+gn3NaJ3tHhQnSv7w93gBk5mcqu94Afwknisfr6NbG9N0CsUJIZJSq5WZDQ pu/2WgFGEzG837la/rc1W5tv2p3pXFzX/9puBdT8HYWS9RP3SKmJ5mAO3LTLLCCylwXj n8EbabjVTqYICoa+ULhfHtl9qTLDTNa4IT7h0rpC4a5SmFIaiUEEZsYuz1dHFb5KIpc+ EuXQ== X-Forwarded-Encrypted: i=1; AFNElJ9x2IwuKKCYSRJ1TQpxHx2orBlM41m11xiWhC9NAyc/76Wm6yjfareYN6j1AQlCp+DXE/Kzv1dYeZTyPfc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz8Q0LohbpaEJ7SPNVywIJ5nRHLl0RGgUFcc9fD2vSGor/wNJu7 n18YtYgoC7GJL3P+Au0KpBndKO0WWHw+XKwigeehI8/YbjTzSYSnAJUO X-Gm-Gg: AfdE7ckiH4bMkeMD9FGEJ+CVyup5RIK1JaqvzjLUjzCHC51xdSqjs7btg030Z5kYhhh HI6bTOjLo6ylhBelhce5pYVsHbDYwWz/SgTc+kSWVRebX4nB8jWCHS4Thl8iYYDPE6yh0BBslRF J496l89Nu7J0HZi6qfqSmwzSOgJMHXPY4baf3hTheRnq15CKEpQ84pYYarHkQKDqpe1Oi1KwaPA Y9qqSi0CrYuS+HbRDUzDzahVQQoBfgQ4PIADIBkhb/fyXdA1NBrNq7SjhOIkRBpiPrYp8UuyS5k uUtCDEKDoEPoyW0F0BKaGLQKBNqI9aCzWw308Vuu7eQRkCWQtznF7HzgyTvqSrs+YGD2uVjIyXl cVE+EMh1dur9AMnKBu63xka8Swsk4axdBMusI+gQzh4HSXklLhoJdmb6kIRCTM1lUih+CCDWJzo OdC2YtFVt3AcVesnir7pBsZA== X-Received: by 2002:a05:6a00:9493:b0:845:e4d6:bd2b with SMTP id d2e1a72fcca58-845e4d6bfaamr3829675b3a.48.1782700730418; Sun, 28 Jun 2026 19:38:50 -0700 (PDT) Received: from archermind.. ([182.150.55.91]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c92b9dc216csm6914869a12.9.2026.06.28.19.38.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jun 2026 19:38:49 -0700 (PDT) From: Liem To: carlos.song@oss.nxp.com Cc: andi.shyti@kernel.org, biwen.li@nxp.com, festevam@gmail.com, frank.li@nxp.com, frank.li@oss.nxp.com, imx@lists.linux.dev, kernel@pengutronix.de, liem16213@gmail.com, linux-arm-kernel@lists.infradead.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, o.rempel@pengutronix.de, s.hauer@pengutronix.de, stable@vger.kernel.org, wsa@kernel.org Subject: [PATCH v4 1/2] i2c: imx: Fix slave registration race and error handling Date: Mon, 29 Jun 2026 10:38:28 +0800 Message-Id: <20260629023829.152651-2-liem16213@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260629023829.152651-1-liem16213@gmail.com> References: <20260629023829.152651-1-liem16213@gmail.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 In i2c_imx_reg_slave(), the slave pointer was assigned before pm_runtime_resume_and_get(). If pm_runtime_resume_and_get() failed, the error path returned without clearing i2c_imx->slave, leaving it non-NULL and causing all subsequent registration attempts to fail with -EBUSY. Additionally, because this driver uses a shared IRQ, the interrupt handler i2c_imx_isr() can execute concurrently and, after acquiring slave_lock, dereference i2c_imx->slave. The previous fix attempt added a lockless i2c_imx->slave = NULL on the error path, but that could race with the ISR under the lock and still cause a NULL pointer dereference. Fix both issues by deferring the assignment of i2c_imx->slave and i2c_imx->last_slave_event to after a successful resume, and by performing the assignment inside the slave_lock critical section. This guarantees that the slave pointer is never left stale on the error path and is always valid when observed by the interrupt handler. Fixes: f7414cd6923f ("i2c: imx: support slave mode for imx I2C driver") Cc: stable@vger.kernel.org Signed-off-by: Liem --- v3 -> v4: - Instead of clearing the slave pointer on error, defer the assignment until after pm_runtime_resume_and_get() succeeds, and take slave_lock to avoid racing with the shared IRQ handler. Suggested by Sashiko and Carlos Song --- drivers/i2c/busses/i2c-imx.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c index 28313d0fad37..2398c406e913 100644 --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c @@ -930,9 +930,6 @@ static int i2c_imx_reg_slave(struct i2c_client *client) if (i2c_imx->slave) return -EBUSY; - i2c_imx->slave = client; - i2c_imx->last_slave_event = I2C_SLAVE_STOP; - /* Resume */ ret = pm_runtime_resume_and_get(i2c_imx->adapter.dev.parent); if (ret < 0) { @@ -940,6 +937,11 @@ static int i2c_imx_reg_slave(struct i2c_client *client) return ret; } + scoped_guard(spinlock_irqsave, &i2c_imx->slave_lock) { + i2c_imx->slave = client; + i2c_imx->last_slave_event = I2C_SLAVE_STOP; + } + i2c_imx_slave_init(i2c_imx); return 0; -- 2.34.1