Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp734738ybc; Sat, 16 Nov 2019 07:47:04 -0800 (PST) X-Google-Smtp-Source: APXvYqwI5VBLIhB0V9R5W4qD8wH9Wd/X1a6tV8g6/1EAD/UVMvlpuP0au5XbuB/vlFhBYSnJ66aa X-Received: by 2002:a05:6000:18c:: with SMTP id p12mr20802925wrx.154.1573919224619; Sat, 16 Nov 2019 07:47:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573919224; cv=none; d=google.com; s=arc-20160816; b=nZTpIPrYF9WktsWSy5h/BCuibzx0A1JJ11T+w5KS0vidRdgYg3zomJkb3xm3Kez/lB B1VUbxW+f2J1dsNKByDHYNnWkCUCAMXyMWzJ2D40I7LwsP559Mi3lzUP7H+tYuf57Y/1 CWY6zsZYaT/AYBn//693qVGvbRAPq2g8j4Adc0rz3yOkdQTnaBkA9o9jWc5fEoASsET4 Rdm6nAkhXptXdXXaiguZH883GFzipuGFBa0T0bWMvXO5g1411Q2iXIppwbJw+TuRoy+m 2lk4BBSFaVKAjnnGk35iXHIT+7QCjt+XzJM6ucC/DZwriJKqtu/O/4al7qNa9AtWmSST Y++A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jz//2/zmF1uLNex+dP9eVNW00/XTGI0+qNVcAIq846Y=; b=zangvTrhOB6+zn2hksoXGQeefJ+YZt533PhqzoOkbNPJgdvs4KAl9Gsmt4VxuInSA0 E6JAFTHKeGIrr9Fph2dlU0iRw//qSXkDjF8RU3yItYxnz8IVj0PknOl2nXmcrorX/S4+ wkx7mWykydKaLYBHutrNAsz8luGsP838dBIBlScfwiofdReTKteEmaYSQHcJI/y0OOt2 +gz8/BTgFng/sZRKf3SoNfSpyJSScHcnqTMu3z77H7xnhr7WIajVr7dypSGxEYNtkE51 hq3WQVSK9t6ITnb7WEYZFcwGm6zGpnk+vUjqxPWaw1/1+TN0GJyUyxFDDL9TDO+MlW60 47fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Gxz7qg6n; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u9si9370227edl.273.2019.11.16.07.46.40; Sat, 16 Nov 2019 07:47:04 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Gxz7qg6n; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728070AbfKPPog (ORCPT + 99 others); Sat, 16 Nov 2019 10:44:36 -0500 Received: from mail.kernel.org ([198.145.29.99]:49212 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728957AbfKPPo0 (ORCPT ); Sat, 16 Nov 2019 10:44:26 -0500 Received: from sasha-vm.mshome.net (unknown [50.234.116.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E4EDC2073B; Sat, 16 Nov 2019 15:44:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1573919065; bh=NjWy5znEkoWXqsJzxRB5cmvhQBP3xr8ifrgF/0d3lR8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Gxz7qg6n6QYXX2uVIzYXvXOEGOON53WCavqmKLZljIal9eujDJJK9X6mT5aaz1EA0 wiFwp8VMOzXlMtbZ4ZmiySiBrvDLBHc2OQrYUQpJ7Ce6INL2bnuAllE1Z0rq06bQJn qXYLrTwMYEpqAVE9YEntroOmG3+ssmy/2h4GoA3Y= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Masahiro Yamada , Wolfram Sang , Sasha Levin , linux-i2c@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 137/237] i2c: uniphier-f: make driver robust against concurrency Date: Sat, 16 Nov 2019 10:39:32 -0500 Message-Id: <20191116154113.7417-137-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191116154113.7417-1-sashal@kernel.org> References: <20191116154113.7417-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masahiro Yamada [ Upstream commit f1fdcbbdf45d9609f3d4063b67e9ea941ba3a58f ] This is unlikely to happen, but it is possible for a CPU to enter the interrupt handler just after wait_for_completion_timeout() has expired. If this happens, the hardware is accessed from multiple contexts concurrently. Disable the IRQ after wait_for_completion_timeout(), and do nothing from the handler when the IRQ is disabled. Fixes: 6a62974b667f ("i2c: uniphier_f: add UniPhier FIFO-builtin I2C driver") Signed-off-by: Masahiro Yamada Signed-off-by: Wolfram Sang Signed-off-by: Sasha Levin --- drivers/i2c/busses/i2c-uniphier-f.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/busses/i2c-uniphier-f.c b/drivers/i2c/busses/i2c-uniphier-f.c index bc26ec822e268..b9a0690b4fd73 100644 --- a/drivers/i2c/busses/i2c-uniphier-f.c +++ b/drivers/i2c/busses/i2c-uniphier-f.c @@ -98,6 +98,7 @@ struct uniphier_fi2c_priv { unsigned int flags; unsigned int busy_cnt; unsigned int clk_cycle; + spinlock_t lock; /* IRQ synchronization */ }; static void uniphier_fi2c_fill_txfifo(struct uniphier_fi2c_priv *priv, @@ -162,7 +163,10 @@ static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id) struct uniphier_fi2c_priv *priv = dev_id; u32 irq_status; + spin_lock(&priv->lock); + irq_status = readl(priv->membase + UNIPHIER_FI2C_INT); + irq_status &= priv->enabled_irqs; dev_dbg(&priv->adap.dev, "interrupt: enabled_irqs=%04x, irq_status=%04x\n", @@ -230,6 +234,8 @@ static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id) goto handled; } + spin_unlock(&priv->lock); + return IRQ_NONE; data_done: @@ -246,6 +252,8 @@ static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id) handled: uniphier_fi2c_clear_irqs(priv); + spin_unlock(&priv->lock); + return IRQ_HANDLED; } @@ -311,7 +319,7 @@ static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap, { struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap); bool is_read = msg->flags & I2C_M_RD; - unsigned long time_left; + unsigned long time_left, flags; dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, stop=%d\n", is_read ? "receive" : "transmit", msg->addr, msg->len, stop); @@ -342,6 +350,12 @@ static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap, priv->membase + UNIPHIER_FI2C_CR); time_left = wait_for_completion_timeout(&priv->comp, adap->timeout); + + spin_lock_irqsave(&priv->lock, flags); + priv->enabled_irqs = 0; + uniphier_fi2c_set_irqs(priv); + spin_unlock_irqrestore(&priv->lock, flags); + if (!time_left) { dev_err(&adap->dev, "transaction timeout.\n"); uniphier_fi2c_recover(priv); @@ -546,6 +560,7 @@ static int uniphier_fi2c_probe(struct platform_device *pdev) priv->clk_cycle = clk_rate / bus_speed; init_completion(&priv->comp); + spin_lock_init(&priv->lock); priv->adap.owner = THIS_MODULE; priv->adap.algo = &uniphier_fi2c_algo; priv->adap.dev.parent = dev; -- 2.20.1