From patchwork Wed May 4 20:15:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Rosin X-Patchwork-Id: 9018391 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7C0B1BF29F for ; Wed, 4 May 2016 20:18:44 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6FD3D203E3 for ; Wed, 4 May 2016 20:18:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 265F4203F3 for ; Wed, 4 May 2016 20:18:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755412AbcEDUSi (ORCPT ); Wed, 4 May 2016 16:18:38 -0400 Received: from mail-db5eur01on0097.outbound.protection.outlook.com ([104.47.2.97]:51128 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754491AbcEDUQY (ORCPT ); Wed, 4 May 2016 16:16:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axentiatech.onmicrosoft.com; s=selector1-axentia-se; h=From:To:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=ptoB7Yf2IMwzKuI24zxDFagWJhCb/ZRSA578iIibpVo=; b=K9IeSzIJhJ9P1RkLpKBcgQYgjdkHGhLPlXkhLHh9AnZ8rRfwM5A2JSdcxmh99teXDV/+R725f2Pxxmupi4ZxwRR/y9P4p9mkNG2jCihwyensCvre/LKR+sYLlnedochyUdtmFwB+Zom2H7RGofgjbFTl9lg6CCpugdgTBWg1Tx4= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=axentia.se; Received: from localhost.localdomain (217.210.101.82) by VI1PR02MB1312.eurprd02.prod.outlook.com (10.165.231.154) with Microsoft SMTP Server (TLS) id 15.1.485.9; Wed, 4 May 2016 20:16:15 +0000 From: Peter Rosin To: CC: Peter Rosin , Wolfram Sang , Jonathan Corbet , Peter Korsgaard , Guenter Roeck , Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald , Antti Palosaari , Mauro Carvalho Chehab , Rob Herring , Frank Rowand , Grant Likely , Andrew Morton , "David S. Miller" , Greg Kroah-Hartman , Kalle Valo , Jiri Slaby , Daniel Baluta , Lucas De Marchi , Matt Ranostay , Krzysztof Kozlowski , Hans Verkuil , Terry Heo , Arnd Bergmann , Tommi Rantala , Crestez Dan Leonard , , , , , Subject: [PATCH v9 5/9] iio: imu: inv_mpu6050: change the i2c gate to be mux-locked Date: Wed, 4 May 2016 22:15:31 +0200 Message-ID: <1462392935-28011-6-git-send-email-peda@axentia.se> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1462392935-28011-1-git-send-email-peda@axentia.se> References: <1462392935-28011-1-git-send-email-peda@axentia.se> MIME-Version: 1.0 X-Originating-IP: [217.210.101.82] X-ClientProxiedBy: AM3PR04CA0090.eurprd04.prod.outlook.com (10.163.180.144) To VI1PR02MB1312.eurprd02.prod.outlook.com (10.165.231.154) X-MS-Office365-Filtering-Correlation-Id: ae4025ad-d289-4fdf-78bd-08d37458f3e0 X-Microsoft-Exchange-Diagnostics: 1; VI1PR02MB1312; 2:ITAG1Gb0a51KHB6D/Jc5+POAQGzBlsN3NQ0cOP7hL7Iyi7SH4AqBzSHVVkUmzwK/eQI6wNdZaMNo3/RF/bKACDXfxVmgxsGmdMXUGlunV4XjWMCO9slrlU7eU1qSw2fjLdB7u7bMu3LZm3wW8YniEG/4cpBLipvdeCfMuJaGrXnIVzrFXWXuTnFjWxhXVE4L; 3:WbdsJ+JfAXciMnHsd6FlS7s5COMcjWKWiVfC+x5Gg4aiGw++nt+EviZCwNVxwbY/5gKFU/xDR/9F/1nQsjCsfYptTEry5oYWvc49INEiyvSfkmiA/0mlyZ2emMuqJrUH; 25:GLp7WsKsEQcmswcu7PVnEm5Gvhb0Z0TuSA1KhYlgvGDduBNcjyQaIFjGDsM27RxmjejmI6viUYpX5fXbtRepIyimytkE35Z3ihyb1I/Pkli1LJAXXGUUZc+KlQ39KEqc9oWv7mveXWJymfu7RKDn0W40+LdA4CnCXIKEcZyzGjTjRf/mgSqxDqtRkgv0ZGp637dKGlOsqUX31YXEvO+VZeHXldP3U7dH+Ht9GBaE7d4DqdGSGkPBzytLISjh/UhOw6H/UWD/UEH30MfKaL02JE020XDU+d3lAIMB2UK4YCT17VZDAsFVUNsud+CqTCdc2zndFjLmmpgbib9xcFBn4rt0nnZlw71lBxRZtcHHG/wjqHE6u1XIAscnhdh5/dTMBb6ARHb1rtmG66Z8NXx2t91K4nKn9ruSI34MTW7JyPqCmh3jcXfu2KNdtUc4DEv3 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:VI1PR02MB1312; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(9101528026)(9101521098)(6040130)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6041072)(6043046); SRVR:VI1PR02MB1312; BCL:0; PCL:0; RULEID:; SRVR:VI1PR02MB1312; X-Microsoft-Exchange-Diagnostics: 1; VI1PR02MB1312; 4:R3qic2LegAN4QU07LZW0iOxD9p8mNN35qHE2yM0Q495WaD+cBA2TVvALe4/ieLrftRTYP0NNId7dnRkyK2qpLL0f2It1LzryTFdjmjZc2c9gjCc8sXWE+g22QHBNW44trNWOLtuqjbBr+8D2lZ6VYfzxD28gStZ+rY3Sbe40YrNoWf95N1qd4z9t+uPTffpAFzhsq/IrKq3V7Fm6Wmxh7A77M90jMWhWYkorQXiyCI1u09ogiwVxnq6XMBKSCK3hoXS5zNKBmpuzBwrCAD6Sl9P0D6MKZkD1UHJOLCcxy5WJaEz2OISKh3rV46ZctyCP2kgqPBXuZYNXkH/G9DmLnXUdWhiDQ1IBCyKWLSlI54dH8LGVaqeMFDDAx1VZsaIRVMazAKuDlS9D/oKcA0/hxuxzZ9MAvUH82SAM2ElPzZAiK7y4PC3YdiJQ4+mqS32IEfHF5+Qn6RJS/nvRty+INn1o8pb241Ej33ML0pUTklk= X-Forefront-PRVS: 093290AD39 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(979002)(6009001)(6069001)(4326007)(19580405001)(575784001)(86362001)(2351001)(229853001)(189998001)(586003)(6116002)(3846002)(19580395003)(110136002)(33646002)(47776003)(66066001)(74482002)(81166005)(2906002)(50466002)(92566002)(48376002)(50986999)(77096005)(76176999)(5003940100001)(15975445007)(551934003)(42186005)(50226002)(5004730100002)(2950100001)(36756003)(5008740100001)(7059030)(42262002)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR02MB1312; H:localhost.localdomain; FPR:; SPF:None; MLV:ovrnspm; PTR:InfoNoRecords; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; VI1PR02MB1312; 23:a5MdPKTF9FLqciyr760CdmaZTs1WIvuaMIkih0q3c?= =?us-ascii?Q?9IWM6HPmax36LS7ekAr8+xLTri0r0uZYgXpFxSyOuEP+f1hnsdaJLvuUIpKQ?= =?us-ascii?Q?yVvE46O0U7gPpEx3iqFjvypvDmZZbNx6+jnp5bgt9igQGwVymbu6M/XORQp2?= =?us-ascii?Q?kosj7izUFUUg4+6uDjrF98faHieMw1qkH5Zv9XyUDr+XeUTTFu8fGgy65dlQ?= =?us-ascii?Q?qTZuKGFLfrPrrC5hdDnejWTfdTl2UIlR/2rQ0A/6FljIFhHVZbj3MR/nHtZM?= =?us-ascii?Q?sIBD/7cxUyarP+YeGo7HKm4uPlOpk3o8tYeJ/MfSBnJlTvymcShRjIitHE/v?= =?us-ascii?Q?7vRRjDvIzf16LxDg9KT4NyN6hqd51/jx/KoqYkXaDWYR3ipM/h/t7SNS12SP?= =?us-ascii?Q?CRCeBmiBkaMHYtU3EuV7Di8gIxfqe1E2CYW+XLTBWnCfsix2+112dUVP1bi9?= =?us-ascii?Q?hKAj8Gkg5rQQl+T4/te+yby79tRtHweoKY52ZlslPaOEFCAMx8Tdw/BngANv?= =?us-ascii?Q?mUb9DIgIu3Pw+nl8Cv79UyrY7mtERjSVqr13GC1gvG9CueP4F9ndBhak2gUT?= =?us-ascii?Q?WfGPDgEnVlAjLy+fv9JwNdtIItEZy7BXzf0hRKFZqS3yR9RpX3ETeI+7xOYR?= =?us-ascii?Q?xK5wzd6xie1p11sd1vXQ9fjiqun9Roy7iJDcyQRqd3qFMZQak9Nl65BzEFM8?= =?us-ascii?Q?htAuyTQvq6oc9MVm9onzy1p9uvkmQmUkjry/UuQ2DOS4K/4QEzodvWZpMK9H?= =?us-ascii?Q?GwVFPrZMTkTX6ylJXHI3/nTOhr5j0QsbxPI5qvn/pDCXTnjIlgvsMwEq9Wqn?= =?us-ascii?Q?pj2CuPT0w6x64Ht9Anf9HXckcPG2e4RDbn1rcWLEd/8NfFcVwqW81LCEgQ51?= =?us-ascii?Q?52MR43OMvCw+K3pvkXAbeLMVnyQsNU9zVsOep94DVWoU2nlBl4tLLQsX7aGn?= =?us-ascii?Q?CKeZQpleV1iXyppfrTssySlbNsnPUdvzzAqK8sv8/1w8jqkSgNhJxNtnpV8W?= =?us-ascii?Q?LKRx8B45bxeoHFiR94dLWgiStgagtQ8Nid4I83mYTOaebIii8w1svRieb+4f?= =?us-ascii?Q?KVDXjpMVBzTIOdcPcIroxgqFvaf8QZ66ci8KM5tO2YVnsaeT3yAItMcvZiAY?= =?us-ascii?Q?YTPqm/TF1w=3D?= X-Microsoft-Exchange-Diagnostics: 1; VI1PR02MB1312; 5:JmoT3mCqiBAeokDQW4EnaOXquOfh05aTntjk6GXBa43O1u2v89ahPZVJEAU1Sfkx51qw2mSS/qWp0fnTvuUqg3J2tjExwlCGVDGAZ7IEpqdaHqW+ZpfPBBjg1DXeEBPp8XKSatAxGuc79FoFd7SMtA==; 24:oADf4V3grPH2+EzVRdP44RK3Yn53a8dpCAd5bRE2BmteIJbVy3RpFCzEhBLf7dadHON1U0S2OTI15fzk2GZuWfjY56rwKugrdEux/OnGl5s=; 7:XsxIxEaRA7HXfFpLWQBbO6DW3ThJdn/KT0cpu1UPsB4YYWp+bJ1hJWABEtBw+5gOFKiL8HMCBsce8inLM9/GcRb8QWehzAx6FfiBeDGis/hZ+xYX80cwl/ctzB6pFxvElyykOwlyFhV583Bu7V3urxzuwE8fAlzg4kC2xiWQ0W7qZpalLZ5aBw58xsvqCXEK SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: axentia.se X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2016 20:16:15.5483 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR02MB1312 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The root i2c adapter lock is then no longer held by the i2c mux during accesses behind the i2c gate, and such accesses need to take that lock just like any other ordinary i2c accesses do. So, declare the i2c gate mux-locked, and zap the code that makes the unlocked i2c accesses and just use ordinary regmap_write accesses. This also happens to fix the deadlock described in http://patchwork.ozlabs.org/patch/584776/ authored by Adriana Reus and submitted by Daniel Baluta ----------8<---------- iio: imu: inv_mpu6050: Fix deadlock between i2c adapter lock and mpu lock This deadlock occurs if the accel/gyro and the sensor on the auxiliary I2C (in my setup it's an ak8975) are working at the same time. Scenario: T1 T2 ==== ==== inv_mpu6050_read_fifo aux sensor op (eg. ak8975_read_raw) | | mutex_lock(&indio_dev->mlock) i2c_transfer | | i2c transaction i2c adapter lock | | i2c adapter lock i2c_mux_master_xfer | inv_mpu6050_select_bypass | mutex_lock(&indio_dev->mlock) When we operate on an mpu sensor the order of locking is mpu lock followed by the i2c adapter lock. However, when we operate the auxiliary sensor the order of locking is the other way around. ... ----------8<---------- The reason this patch fixes the deadlock is that T2 does not grab the i2c adapter lock until the very end (and grabs the newfangled i2c mux lock where it previously grabbed the i2c adapter lock). Acked-by: Jonathan Cameron Acked-by: Daniel Baluta Tested-by: Crestez Dan Leonard Signed-off-by: Peter Rosin --- Documentation/i2c/i2c-topology | 2 +- drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c | 52 ++++++------------------------- 2 files changed, 11 insertions(+), 43 deletions(-) diff --git a/Documentation/i2c/i2c-topology b/Documentation/i2c/i2c-topology index 27bfd682808d..69b008518454 100644 --- a/Documentation/i2c/i2c-topology +++ b/Documentation/i2c/i2c-topology @@ -50,7 +50,7 @@ i2c-mux-pinctrl Normally parent-locked, mux-locked iff i2c-mux-reg Parent-locked In drivers/iio/ -imu/inv_mpu6050/ Parent-locked +imu/inv_mpu6050/ Mux-locked In drivers/media/ dvb-frontends/m88ds3103 Parent-locked diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c index 3a078df84224..e25f7ef7f0ea 100644 --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c @@ -24,45 +24,16 @@ static const struct regmap_config inv_mpu_regmap_config = { .val_bits = 8, }; -/* - * The i2c read/write needs to happen in unlocked mode. As the parent - * adapter is common. If we use locked versions, it will fail as - * the mux adapter will lock the parent i2c adapter, while calling - * select/deselect functions. - */ -static int inv_mpu6050_write_reg_unlocked(struct i2c_client *client, - u8 reg, u8 d) -{ - int ret; - u8 buf[2] = {reg, d}; - struct i2c_msg msg[1] = { - { - .addr = client->addr, - .flags = 0, - .len = sizeof(buf), - .buf = buf, - } - }; - - ret = __i2c_transfer(client->adapter, msg, 1); - if (ret != 1) - return ret; - - return 0; -} - static int inv_mpu6050_select_bypass(struct i2c_mux_core *muxc, u32 chan_id) { - struct i2c_client *client = i2c_mux_priv(muxc); - struct iio_dev *indio_dev = dev_get_drvdata(&client->dev); + struct iio_dev *indio_dev = i2c_mux_priv(muxc); struct inv_mpu6050_state *st = iio_priv(indio_dev); int ret = 0; /* Use the same mutex which was used everywhere to protect power-op */ mutex_lock(&indio_dev->mlock); if (!st->powerup_count) { - ret = inv_mpu6050_write_reg_unlocked(client, - st->reg->pwr_mgmt_1, 0); + ret = regmap_write(st->map, st->reg->pwr_mgmt_1, 0); if (ret) goto write_error; @@ -71,10 +42,9 @@ static int inv_mpu6050_select_bypass(struct i2c_mux_core *muxc, u32 chan_id) } if (!ret) { st->powerup_count++; - ret = inv_mpu6050_write_reg_unlocked(client, - st->reg->int_pin_cfg, - INV_MPU6050_INT_PIN_CFG | - INV_MPU6050_BIT_BYPASS_EN); + ret = regmap_write(st->map, st->reg->int_pin_cfg, + INV_MPU6050_INT_PIN_CFG | + INV_MPU6050_BIT_BYPASS_EN); } write_error: mutex_unlock(&indio_dev->mlock); @@ -84,18 +54,16 @@ write_error: static int inv_mpu6050_deselect_bypass(struct i2c_mux_core *muxc, u32 chan_id) { - struct i2c_client *client = i2c_mux_priv(muxc); - struct iio_dev *indio_dev = dev_get_drvdata(&client->dev); + struct iio_dev *indio_dev = i2c_mux_priv(muxc); struct inv_mpu6050_state *st = iio_priv(indio_dev); mutex_lock(&indio_dev->mlock); /* It doesn't really mattter, if any of the calls fails */ - inv_mpu6050_write_reg_unlocked(client, st->reg->int_pin_cfg, - INV_MPU6050_INT_PIN_CFG); + regmap_write(st->map, st->reg->int_pin_cfg, INV_MPU6050_INT_PIN_CFG); st->powerup_count--; if (!st->powerup_count) - inv_mpu6050_write_reg_unlocked(client, st->reg->pwr_mgmt_1, - INV_MPU6050_BIT_SLEEP); + regmap_write(st->map, st->reg->pwr_mgmt_1, + INV_MPU6050_BIT_SLEEP); mutex_unlock(&indio_dev->mlock); return 0; @@ -134,7 +102,7 @@ static int inv_mpu_probe(struct i2c_client *client, st = iio_priv(dev_get_drvdata(&client->dev)); st->muxc = i2c_mux_alloc(client->adapter, &client->dev, - 1, 0, 0, + 1, 0, I2C_MUX_LOCKED, inv_mpu6050_select_bypass, inv_mpu6050_deselect_bypass); if (!st->muxc) {