Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp250344pxb; Thu, 14 Apr 2022 21:41:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyeJsYS1gWLMFp9R6KrFcc8icbeoGPddg0lzTOocvmawL4I/bx3vSjFKwzK0a7SW5ThbGek X-Received: by 2002:a17:907:1ca0:b0:6e9:9eef:a8d2 with SMTP id nb32-20020a1709071ca000b006e99eefa8d2mr4845113ejc.719.1649997670867; Thu, 14 Apr 2022 21:41:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649997670; cv=none; d=google.com; s=arc-20160816; b=QzSsc2FIJOPJFp5y7P4DMjFjXZJKe1PWz4KwVMab8f3fzq4/7frZ4pVVHMkVysiEKk tF/FSNgrX8Qb8gwR+a+lEhyAVS1cIDovEf4ngZO/XR5nxR+SkcwfTUQ56CHeyS35+6UJ UoWYe6sutViqeMTKNFq1XwItLiT0aM93gpEkxsePNpu22fFMSd6hNSb3CJX/tl35sxP3 5UCT6AesnRae/4YNJ4EXw4uhMCy8q9XeNuy8/r62vFyMkfcWUbSBaFsOr4bijfJS7Az/ dAZYGawn1sUoWB3K9Tql3gDmhMw7voGim1jVpaJBHaXVkcfCjIRkDgfz+N/N229g3NcL hyJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=z6NO35KE2Lguj4JWi+iLnZSkryiHS70xMnjy6Usv8+0=; b=cExkcfiG9HwpZBdcAkXzrldv1qeCXTizsCim9uHUpYScbaHm8WJbIuB4qsQnqL4Bse /pph07hqYP4KYMB2AQVfSyuH6tR/UU55E6z6KltlKAZq8wYQJWF0HijtEcbFaIhUphxT HvZuZK5+z3qTFtnyq177NqbYRzHX9V8/AD+szKS9xG9zKxdj6T5J44Zzq9OCyypcc4lb g8gQEk7ssezyezj3fvne25WfnD+gL/U3WuFRdb+fwm/JFEa1nmvigqtkC7C2g3rSnnxJ jugjQN2/TVlsGamxb9GdrET26nhnA5I+KN3WDDDsv6uxtEkuvLl5eyjwag704kR2b7cX tx8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=WsFWGMnR; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id st6-20020a170907c08600b006e1409ee753si267182ejc.387.2022.04.14.21.40.43; Thu, 14 Apr 2022 21:41:10 -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=@infradead.org header.s=casper.20170209 header.b=WsFWGMnR; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241528AbiDNTga (ORCPT + 99 others); Thu, 14 Apr 2022 15:36:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345705AbiDNTg3 (ORCPT ); Thu, 14 Apr 2022 15:36:29 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A61DED902; Thu, 14 Apr 2022 12:34:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=z6NO35KE2Lguj4JWi+iLnZSkryiHS70xMnjy6Usv8+0=; b=WsFWGMnRLs2cKSk1AHbHn+6lbq GlViERsBnVWzXCVa1IxA2XFoZc/vgZT98MNb83dnSQpsXJqzuEIbnder6nkWJ5gkZEJ6QamglotGM LtOBLInohgkkYtF5kBT5SQQHRlTFtdBbERanbnRw/rnywpj5uYzoNkIVjsL25cNbuhcBg9PM85eRm fowdvJkzNi8GncNbjuLyNWsMrlAaWfZoCAoZpph4voSTsFNPS5t5/4BhxkTGPWwgrV+1upUpqPJrK eB02eypVNOVtZ08bE6Rn3d/BPAbMASBMghG6flep15moulVwr8MstTQzODLP922h/TwH2jb6gIMFD W4iihIjw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nf5E2-00FSBz-U7; Thu, 14 Apr 2022 19:33:55 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 3FA9F30027B; Thu, 14 Apr 2022 21:33:53 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 1B96B322A1704; Thu, 14 Apr 2022 21:33:53 +0200 (CEST) Date: Thu, 14 Apr 2022 21:33:53 +0200 From: Peter Zijlstra To: Dan Williams Cc: linux-cxl@vger.kernel.org, Greg Kroah-Hartman , "Rafael J. Wysocki" , Dave Jiang , Kevin Tian , Vishal L Verma , "Schofield, Alison" , Linux Kernel Mailing List , Linux NVDIMM Subject: Re: [PATCH v2 02/12] device-core: Add dev->lock_class to enable device_lock() lockdep validation Message-ID: References: <164982968798.684294.15817853329823976469.stgit@dwillia2-desk3.amr.corp.intel.com> <164982969858.684294.17819743973041389492.stgit@dwillia2-desk3.amr.corp.intel.com> <20220413084309.GV2731@worktop.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 On Thu, Apr 14, 2022 at 10:17:13AM -0700, Dan Williams wrote: > One more sanity check... So in driver subsystems there are cases where > a device on busA hosts a topology on busB. When that happens there's a > need to set the lock class late in a driver since busA knows nothing > about the locking rules of busB. I'll pretend I konw what you're talking about ;-) > Since the device has a longer lifetime than a driver when the driver > exits it must set dev->mutex back to the novalidate class, otherwise > it sets up a use after free of the static lock_class_key. I'm not following, static storage has infinite lifetime. > I came up with this and it seems to work, just want to make sure I'm > properly using the lock_set_class() API and it is ok to transition > back and forth from the novalidate case: > > diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h > index 990b6670222e..32673e1a736d 100644 > --- a/drivers/cxl/cxl.h > +++ b/drivers/cxl/cxl.h > @@ -405,6 +405,29 @@ struct cxl_nvdimm_bridge > *cxl_find_nvdimm_bridge(struct cxl_nvdimm *cxl_nvd); > #define __mock static > #endif > > +#ifdef CONFIG_PROVE_LOCKING > +static inline void cxl_lock_reset_class(void *_dev) > +{ > + struct device *dev = _dev; > + > + lock_set_class(&dev->mutex.dep_map, "__lockdep_no_validate__", > + &__lockdep_no_validate__, 0, _THIS_IP_); > +} > + > +static inline int cxl_lock_set_class(struct device *dev, const char *name, > + struct lock_class_key *key) > +{ > + lock_set_class(&dev->mutex.dep_map, name, key, 0, _THIS_IP_); > + return devm_add_action_or_reset(dev, cxl_lock_reset_class, dev); > +} > +#else > +static inline int cxl_lock_set_class(struct device *dev, const char *name, > + struct lock_class_key *key) > +{ > + return 0; > +} > +#endif Under the assumption that the lock is held (lock_set_class() will actually barf if @lock isn't held) this should indeed work as expected (although I think you got the @name part 'wrong', I think that's canonically something like "&dev->mutex" or something).