Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp864690rwi; Wed, 19 Oct 2022 04:04:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5W15M2biBK668O/0mWisYZOa9UPXYHWOShC+s4hZS3wpJ4owhj7gJRBYiAkSzuBV9j6/Pe X-Received: by 2002:a17:907:2da6:b0:78d:3cf1:9132 with SMTP id gt38-20020a1709072da600b0078d3cf19132mr6188350ejc.299.1666177453692; Wed, 19 Oct 2022 04:04:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666177453; cv=none; d=google.com; s=arc-20160816; b=a3FatrbbVJPtXjzjHbp6PIvLaRh3Ikh2dmSD6aXPQzBcIYnkkkzmm8UTZ5O+VoVXMt 03nc+knKUEFKsNboJxVPYV5o1SzAJ6obkbaqBrBa3qAQBrCYBFhu5YQP1Dna4ym13PIh RuKwUXZEhGobVQn1Ma6G0zNHIICCzrnKOaOwSIr2q+PvMl6oV3mJ+DukYbbv01f2+ElT E4iX0CLdqYiMk5pFo0Q6C5U393P70O6zjPv/m5s9znEmysNyXwM6FfrNunyrY9ZvfZpJ bQmQ4yFrhbsWy86gfevgO4f2Ni3b91YJfkPyeV7rzRmk7EpxJFbUcqRmvDYB+KD3DeMa v/5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sb6t8jSooG1cuMhZqRJMkZG8COa6//vm9IH/41TbkSo=; b=vR4psuD77KHQrjchdb0HDinQqWo69VvC+L/W4s2wiqiJ2zEC5leBlWHOVmWgMsmnG6 UNzCtE4/nMve6P+0DuBy/pVq43/Hs69P1bvx9YtsbXtGCMzSIdcFvRAT+OuzObmZ4b9U R6Dc8uhy1T8n0ZG4KWvt9H42n9ohybaNxXY+MB8k3RX3pxPsOXs1X8+hYa37K7ytJGod 0FWQiG3QB7xcnAUjPpIij981JYjvXoBiSDQ621fCRuu0P+qK8Waz9CbLYjbRwOIitiWP qx3gSene8aOq9I7RpQNBVaXRhBVpFsSUJdyMBb+nqUggFMekyHJDAKKlusyU3/lrKLM0 n1Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TfPLgsHH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s17-20020a056402521100b004523826ed6dsi14643268edd.554.2022.10.19.04.03.33; Wed, 19 Oct 2022 04:04:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TfPLgsHH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233605AbiJSJbL (ORCPT + 99 others); Wed, 19 Oct 2022 05:31:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233541AbiJSJ1V (ORCPT ); Wed, 19 Oct 2022 05:27:21 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C7B5E6F76; Wed, 19 Oct 2022 02:12:17 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 14F7361738; Wed, 19 Oct 2022 09:01:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21F17C433C1; Wed, 19 Oct 2022 09:01:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666170062; bh=9DW2B3GfccMQLd9+7/WwNZWZFVrCRAP6iFxZJ8fPyXQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TfPLgsHHNLWQT+iZFvGYO7f8/ZMzPz7MVPNm+msJ9T9hbM4wNi0eAbdXcp0bRGyfV UYHQ3MYpTed35ikixLHmIqFaCrPZUQNsSpydISW8Th+X4EqI+PAzuFyJkBIa0qA8EC VnycnET23AvjgkbmMDrEmXPb+4XpMwdSnxOOjw2I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vincent Whitchurch , Andy Shevchenko , Jonathan Cameron , Sasha Levin Subject: [PATCH 6.0 511/862] iio: Use per-device lockdep class for mlock Date: Wed, 19 Oct 2022 10:29:58 +0200 Message-Id: <20221019083312.561138991@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Vincent Whitchurch [ Upstream commit 2bc9cd66eb25d0fefbb081421d6586495e25840e ] If an IIO driver uses callbacks from another IIO driver and calls iio_channel_start_all_cb() from one of its buffer setup ops, then lockdep complains due to the lock nesting, as in the below example with lmp91000. Since the locks are being taken on different IIO devices, there is no actual deadlock. Fix the warning by telling lockdep to use a different class for each iio_device. ============================================ WARNING: possible recursive locking detected -------------------------------------------- python3/23 is trying to acquire lock: (&indio_dev->mlock){+.+.}-{3:3}, at: iio_update_buffers but task is already holding lock: (&indio_dev->mlock){+.+.}-{3:3}, at: enable_store other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(&indio_dev->mlock); lock(&indio_dev->mlock); *** DEADLOCK *** May be due to missing lock nesting notation 5 locks held by python3/23: #0: (sb_writers#5){.+.+}-{0:0}, at: ksys_write #1: (&of->mutex){+.+.}-{3:3}, at: kernfs_fop_write_iter #2: (kn->active#14){.+.+}-{0:0}, at: kernfs_fop_write_iter #3: (&indio_dev->mlock){+.+.}-{3:3}, at: enable_store #4: (&iio_dev_opaque->info_exist_lock){+.+.}-{3:3}, at: iio_update_buffers Call Trace: __mutex_lock iio_update_buffers iio_channel_start_all_cb lmp91000_buffer_postenable __iio_update_buffers enable_store Fixes: 67e17300dc1d76 ("iio: potentiostat: add LMP91000 support") Signed-off-by: Vincent Whitchurch Reviewed-by: Andy Shevchenko Link: https://lore.kernel.org/r/20220829091840.2791846-1-vincent.whitchurch@axis.com Signed-off-by: Jonathan Cameron Signed-off-by: Sasha Levin --- drivers/iio/industrialio-core.c | 5 +++++ include/linux/iio/iio-opaque.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 0f4dbda3b9d3..921d8e8643a2 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -1621,6 +1621,8 @@ static void iio_dev_release(struct device *device) iio_device_detach_buffers(indio_dev); + lockdep_unregister_key(&iio_dev_opaque->mlock_key); + ida_free(&iio_ida, iio_dev_opaque->id); kfree(iio_dev_opaque); } @@ -1680,6 +1682,9 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) INIT_LIST_HEAD(&iio_dev_opaque->buffer_list); INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers); + lockdep_register_key(&iio_dev_opaque->mlock_key); + lockdep_set_class(&indio_dev->mlock, &iio_dev_opaque->mlock_key); + return indio_dev; } EXPORT_SYMBOL(iio_device_alloc); diff --git a/include/linux/iio/iio-opaque.h b/include/linux/iio/iio-opaque.h index 6b3586b3f952..d1f8b30a7c8b 100644 --- a/include/linux/iio/iio-opaque.h +++ b/include/linux/iio/iio-opaque.h @@ -11,6 +11,7 @@ * checked by device drivers but should be considered * read-only as this is a core internal bit * @driver_module: used to make it harder to undercut users + * @mlock_key: lockdep class for iio_dev lock * @info_exist_lock: lock to prevent use during removal * @trig_readonly: mark the current trigger immutable * @event_interface: event chrdevs associated with interrupt lines @@ -42,6 +43,7 @@ struct iio_dev_opaque { int currentmode; int id; struct module *driver_module; + struct lock_class_key mlock_key; struct mutex info_exist_lock; bool trig_readonly; struct iio_event_interface *event_interface; -- 2.35.1