Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3557198pxm; Tue, 1 Mar 2022 00:14:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJxosYeSQajb4O+n1sYSlceE/jdDI/yfBJ6/rEDnKe5EgrIseind0md5RFoSz/c3ca9XlCno X-Received: by 2002:a63:aa08:0:b0:373:cc0b:5b71 with SMTP id e8-20020a63aa08000000b00373cc0b5b71mr20670547pgf.599.1646122462641; Tue, 01 Mar 2022 00:14:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646122462; cv=none; d=google.com; s=arc-20160816; b=VfKOlHFyqj6JD459JGCr8FfpMCED9h2LJd6jm9pSDMLiIzJfZx7+Q9ZxBXqW2Fsicw pAjdmIQSFwkoFmaxFdvt/td2Je8DJBwZLlDVhtVdY5TaDns3sWmXb44nZ1WZTvydRK27 2rv0G1bIKw+RXKxPcRAiJt1b7q73Ks44egqAS6xMZSaoVIJiMX5TYuHul786MUole/L6 ooDcCOOnzHZsFYJByT69H5P0X5AlP/QRwtKCeDGdW6hxtEh37Zr7bvg8LfMpxlnlLSmZ poW4ndS4sNwyZj7w9yyRkPKIu6hxeLKpk7tsl2i0thVef/3tzL7msdq911rllChh6ifw FWXw== 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:cc:to:from :subject:dkim-signature; bh=/uqHdG0sY0jieWS0+bXGh+cunkbUgTVX3iyiMF7Hkl0=; b=1ItVL7euaEJEwZ2YIxnMxqoPLnRQlpIrj1PEhj1h0DuUS5jGQwMfrzfpvQY/o0pWgV Q208zs6YUjOU8lLQPQ6kWeydOXfhcSf96hudHzaz044ZSa7ks+XLTfjwDgRACBqxHPYT lmHkYkGRQdl0O9t3hPMgU83d9hCv0jjB2CpZ2a7NyvdQMsRDa4ZCG148/ce7SvoNeD1d Q0Z51tF/fDnhKdAjpENj6UvS4jPFAWytFcVi5XTTb/UXbAPhd+XK0Ey6uQ0Nx8vFNKsA M1QymhCTkYgigztEyAttjIkfoX3G8hCY8ZOvAhrOXTpkI6jNsCL1C/utruIdTqioHQb1 vqiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fWGsMJCs; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k15-20020a170902ce0f00b0014f62e9d082si11650835plg.591.2022.03.01.00.14.07; Tue, 01 Mar 2022 00:14:22 -0800 (PST) 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=@intel.com header.s=Intel header.b=fWGsMJCs; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231405AbiCACtj (ORCPT + 99 others); Mon, 28 Feb 2022 21:49:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231434AbiCACtf (ORCPT ); Mon, 28 Feb 2022 21:49:35 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 767AB47072; Mon, 28 Feb 2022 18:48:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646102935; x=1677638935; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5s4C1XoxbbWcTd1M501STQqQFqAZkTdnQMPZcJvB8f4=; b=fWGsMJCsANPrSCiuVce5fXp1pSpU/sP2qcEh1t9Rdhddbo3hEG/Y8Aw6 7VBnkwui6rClZNDYje/IeWiuDCU8NKo2tEaOWTlGEKVXUrPHEpbpr0KMH 3LeXT9Ln1n67slk2/h4hTZTUGog1BLvtdm8LWbSGtq8TCS1JgHGxyOZt5 F93XHeQ6S8K1jlhaZV7QmCazagCM1QjoQD3cMnW0I4doxV26JaBAD+o/k vaBrOVXdKYreSUhHy6CoF8CLXO5viF5b1WLVaw7vuIw+C3Gli+r6LI0AN cQL6lh5rpv/2dWDgkbAMz/BAgLy9CEgyS5WrEOS9GFCnakIW+EfFJLOXH A==; X-IronPort-AV: E=McAfee;i="6200,9189,10272"; a="253232806" X-IronPort-AV: E=Sophos;i="5.90,144,1643702400"; d="scan'208";a="253232806" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2022 18:48:55 -0800 X-IronPort-AV: E=Sophos;i="5.90,144,1643702400"; d="scan'208";a="544942973" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.25]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2022 18:48:54 -0800 Subject: [PATCH 01/11] device-core: Enable lockdep validation From: Dan Williams To: gregkh@linuxfoundation.org, rafael.j.wysocki@intel.com Cc: "Rafael J. Wysocki" , vishal.l.verma@intel.com, alison.schofield@intel.com, linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org, nvdimm@lists.linux.dev Date: Mon, 28 Feb 2022 18:48:54 -0800 Message-ID: <164610293458.2682974.15975217569862336908.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <164610292916.2682974.12924748003366352335.stgit@dwillia2-desk3.amr.corp.intel.com> References: <164610292916.2682974.12924748003366352335.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-3-g996c MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.5 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_NONE,T_SCC_BODY_TEXT_LINE 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 The @lockdep_mutex attribute of 'struct device' was introduced to allow subsystems to wrap their usage of device_lock() with a local definition that adds nested annotations and lockdep validation. However, that approach leaves lockdep blind to the device_lock usage in the device-core. Instead of requiring the subsystem to replace device_lock() teach the core device_lock() to consider a subsystem specified lock class. While this enables increased coverage of the device_lock() it is still limited to one subsystem at a time unless / until a unique "lockdep_mutex" is added for each subsystem that wants a distinct lock class number space. Cc: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" Signed-off-by: Dan Williams --- drivers/base/core.c | 1 + include/linux/device.h | 73 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 7bb957b11861..96430fa5152e 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -2866,6 +2866,7 @@ void device_initialize(struct device *dev) mutex_init(&dev->mutex); #ifdef CONFIG_PROVE_LOCKING mutex_init(&dev->lockdep_mutex); + dev->lock_class = -1; #endif lockdep_set_novalidate_class(&dev->mutex); spin_lock_init(&dev->devres_lock); diff --git a/include/linux/device.h b/include/linux/device.h index 93459724dcde..e313ff21d670 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -402,6 +402,7 @@ struct dev_msi_info { * @mutex: Mutex to synchronize calls to its driver. * @lockdep_mutex: An optional debug lock that a subsystem can use as a * peer lock to gain localized lockdep coverage of the device_lock. + * @lock_class: per-subsystem annotated device lock class * @bus: Type of bus device is on. * @driver: Which driver has allocated this * @platform_data: Platform data specific to the device. @@ -501,6 +502,7 @@ struct device { dev_set_drvdata/dev_get_drvdata */ #ifdef CONFIG_PROVE_LOCKING struct mutex lockdep_mutex; + int lock_class; #endif struct mutex mutex; /* mutex to synchronize calls to * its driver. @@ -762,6 +764,12 @@ static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags) return !!(dev->power.driver_flags & flags); } +static inline void device_lock_assert(struct device *dev) +{ + lockdep_assert_held(&dev->mutex); +} + +#ifndef CONFIG_PROVE_LOCKING static inline void device_lock(struct device *dev) { mutex_lock(&dev->mutex); @@ -782,10 +790,71 @@ static inline void device_unlock(struct device *dev) mutex_unlock(&dev->mutex); } -static inline void device_lock_assert(struct device *dev) +static inline void device_set_lock_class(struct device *dev, int lock_class) { - lockdep_assert_held(&dev->mutex); } +#else +static inline void device_lock(struct device *dev) +{ + lockdep_assert_not_held(&dev->lockdep_mutex); + + mutex_lock(&dev->mutex); + if (dev->lock_class >= 0) + mutex_lock_nested(&dev->lockdep_mutex, dev->lock_class); +} + +static inline int device_lock_interruptible(struct device *dev) +{ + int rc = mutex_lock_interruptible(&dev->mutex); + + if (rc || dev->lock_class < 0) + return rc; + + return mutex_lock_interruptible_nested(&dev->lockdep_mutex, + dev->lock_class); +} + +static inline int device_trylock(struct device *dev) +{ + if (mutex_trylock(&dev->mutex)) { + if (dev->lock_class >= 0) + mutex_lock_nested(&dev->lockdep_mutex, dev->lock_class); + return 1; + } + + return 0; +} + +static inline void device_unlock(struct device *dev) +{ + if (dev->lock_class >= 0) + mutex_unlock(&dev->lockdep_mutex); + mutex_unlock(&dev->mutex); +} + +static inline void device_set_lock_class(struct device *dev, int lock_class) +{ + if (dev->lock_class < 0 && lock_class > 0) { + if (mutex_is_locked(&dev->mutex)) { + /* + * device_unlock() will unlock lockdep_mutex now that + * lock_class is set, so take the paired lock now + */ + mutex_lock_nested(&dev->lockdep_mutex, lock_class); + } + } else if (dev->lock_class >= 0 && lock_class < 0) { + if (mutex_is_locked(&dev->mutex)) { + /* + * device_unlock() will no longer drop lockdep_mutex now + * that lock_class is disabled, so drop the paired lock + * now. + */ + mutex_unlock(&dev->lockdep_mutex); + } + } + dev->lock_class = lock_class; +} +#endif static inline struct device_node *dev_of_node(struct device *dev) {