Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1261478yba; Wed, 24 Apr 2019 18:37:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqxm8BZHWreycXqW6bh2kkl8AQe6mVblKUeWMlrI4wd6OJwmpHWiYf3qAN/jmz9w0RplV5z7 X-Received: by 2002:a63:d146:: with SMTP id c6mr114543pgj.452.1556156248868; Wed, 24 Apr 2019 18:37:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556156248; cv=none; d=google.com; s=arc-20160816; b=NOL8qBxjHNSqMtYO8MnAff+P6di7+FxjOZzTXaZt4NexfTMTAlfs/+Ezdvz9qZXevc 2sbP1slTgQtVfeBQZWYuibmNhX1i7VVnZJI6kn0r50lnvAu86hzKXUycBRmW/UlGw/yj zjgHEYHMTl4SjSKc/wS5GLxbzSMwV4AYAreUX5Ne+8YJg9KjgbmYtllCZcjXy489/GuN urFinitpYBuHEAKXM9nhFHnZPYMox8Kd8eD9kdNtt/Ldi18FVUK6BPzYB7sLpUHqC5fR rm4AblSvDWHkDd6ygtdxmY5tzoxI67fdyGDAFHcFEbOfLSlI9QAtgGxIOv6WrQvbStTX t+LQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=B+1msXTa39yBtrA27UlP42MHAJFVroPMjvTsxqHW6ug=; b=aGROzzFR77VBcuRPz34BjesMkwoBOvw0MmdF36sfFnYTqcvg60ah8KMWAg0zdRmoVY 2AnMD4w21SPL3SUCJH5ZpK3ZN9hivBb3L8cPOcprzW1oM+ub1ZWSwALHkyobdYJPxO/k BdXfiLN+TvPbf/EJXSUIxAEvitu8X9hQmS3nsXE5vgTpOcAEQGvwPP0+oO1R1HXq88NJ zoZZ9c+YvhZKdn/claG4K5DXKKlaJJrqttMMEtlG9a9ubWggWy51H5vHBKj63bH7Locu WpAaoLnBUj1ywducpIz3MqXkbk3PBqIRXG4a6zeqOJE+DArpjqMBbrWpMRsggCQpjh2K BDVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gvDrYVUV; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ay3si9867292plb.336.2019.04.24.18.37.13; Wed, 24 Apr 2019 18:37:28 -0700 (PDT) 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=gvDrYVUV; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391485AbfDXRdY (ORCPT + 99 others); Wed, 24 Apr 2019 13:33:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:60314 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391119AbfDXRdV (ORCPT ); Wed, 24 Apr 2019 13:33:21 -0400 Received: from localhost (62-193-50-229.as16211.net [62.193.50.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 854FF2054F; Wed, 24 Apr 2019 17:33:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556127201; bh=F9dKgoC6fKxA8fR+H/jiUkscnN/0FMd4x7aMQN5aGZA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gvDrYVUVZZcg1C2QwypF2qFaj/bvDYrvVDLVa+IUM/aYh/GCIbVzU4OpcLYgoc72h 3mtlkO0G9mRdaBsF1hMG42bj3PF5zBJA3I2+8wfCsv0Q+f5I/IRweZOjX9q5ApCP9/ ZeMFQwKclp54kyc7dludsn2sco62w8DT8CP3E8Xg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Fabrice Gasnier , Stable@vger.kernel.org, Jonathan Cameron Subject: [PATCH 4.19 45/96] iio: core: fix a possible circular locking dependency Date: Wed, 24 Apr 2019 19:09:50 +0200 Message-Id: <20190424170922.909409220@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190424170919.829037226@linuxfoundation.org> References: <20190424170919.829037226@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Fabrice Gasnier commit 7f75591fc5a123929a29636834d1bcb8b5c9fee3 upstream. This fixes a possible circular locking dependency detected warning seen with: - CONFIG_PROVE_LOCKING=y - consumer/provider IIO devices (ex: "voltage-divider" consumer of "adc") When using the IIO consumer interface, e.g. iio_channel_get(), the consumer device will likely call iio_read_channel_raw() or similar that rely on 'info_exist_lock' mutex. typically: ... mutex_lock(&chan->indio_dev->info_exist_lock); if (chan->indio_dev->info == NULL) { ret = -ENODEV; goto err_unlock; } ret = do_some_ops() err_unlock: mutex_unlock(&chan->indio_dev->info_exist_lock); return ret; ... Same mutex is also hold in iio_device_unregister(). The following deadlock warning happens when: - the consumer device has called an API like iio_read_channel_raw() at least once. - the consumer driver is unregistered, removed (unbind from sysfs) ====================================================== WARNING: possible circular locking dependency detected 4.19.24 #577 Not tainted ------------------------------------------------------ sh/372 is trying to acquire lock: (kn->count#30){++++}, at: kernfs_remove_by_name_ns+0x3c/0x84 but task is already holding lock: (&dev->info_exist_lock){+.+.}, at: iio_device_unregister+0x18/0x60 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (&dev->info_exist_lock){+.+.}: __mutex_lock+0x70/0xa3c mutex_lock_nested+0x1c/0x24 iio_read_channel_raw+0x1c/0x60 iio_read_channel_info+0xa8/0xb0 dev_attr_show+0x1c/0x48 sysfs_kf_seq_show+0x84/0xec seq_read+0x154/0x528 __vfs_read+0x2c/0x15c vfs_read+0x8c/0x110 ksys_read+0x4c/0xac ret_fast_syscall+0x0/0x28 0xbedefb60 -> #0 (kn->count#30){++++}: lock_acquire+0xd8/0x268 __kernfs_remove+0x288/0x374 kernfs_remove_by_name_ns+0x3c/0x84 remove_files+0x34/0x78 sysfs_remove_group+0x40/0x9c sysfs_remove_groups+0x24/0x34 device_remove_attrs+0x38/0x64 device_del+0x11c/0x360 cdev_device_del+0x14/0x2c iio_device_unregister+0x24/0x60 release_nodes+0x1bc/0x200 device_release_driver_internal+0x1a0/0x230 unbind_store+0x80/0x130 kernfs_fop_write+0x100/0x1e4 __vfs_write+0x2c/0x160 vfs_write+0xa4/0x17c ksys_write+0x4c/0xac ret_fast_syscall+0x0/0x28 0xbe906840 other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&dev->info_exist_lock); lock(kn->count#30); lock(&dev->info_exist_lock); lock(kn->count#30); *** DEADLOCK *** ... cdev_device_del() can be called without holding the lock. It should be safe as info_exist_lock prevents kernelspace consumers to use the exported routines during/after provider removal. cdev_device_del() is for userspace. Help to reproduce: See example: Documentation/devicetree/bindings/iio/afe/voltage-divider.txt sysv { compatible = "voltage-divider"; io-channels = <&adc 0>; output-ohms = <22>; full-ohms = <222>; }; First, go to iio:deviceX for the "voltage-divider", do one read: $ cd /sys/bus/iio/devices/iio:deviceX $ cat in_voltage0_raw Then, unbind the consumer driver. It triggers above deadlock warning. $ cd /sys/bus/platform/drivers/iio-rescale/ $ echo sysv > unbind Note I don't actually expect stable will pick this up all the way back into IIO being in staging, but if's probably valid that far back. Signed-off-by: Fabrice Gasnier Fixes: ac917a81117c ("staging:iio:core set the iio_dev.info pointer to null on unregister") Cc: Signed-off-by: Jonathan Cameron Signed-off-by: Greg Kroah-Hartman --- drivers/iio/industrialio-core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -1735,10 +1735,10 @@ EXPORT_SYMBOL(__iio_device_register); **/ void iio_device_unregister(struct iio_dev *indio_dev) { - mutex_lock(&indio_dev->info_exist_lock); - cdev_device_del(&indio_dev->chrdev, &indio_dev->dev); + mutex_lock(&indio_dev->info_exist_lock); + iio_device_unregister_debugfs(indio_dev); iio_disable_all_buffers(indio_dev);