Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5403046pxv; Wed, 21 Jul 2021 04:44:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpXdHg88bkcwBJhCnnKX2/Wpnw3n5E8DtCyPxjU7RKwUBq4ljcqCqFysso8ecQ5aVkKJo3 X-Received: by 2002:a05:6402:30ba:: with SMTP id df26mr47623599edb.310.1626867852998; Wed, 21 Jul 2021 04:44:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626867852; cv=none; d=google.com; s=arc-20160816; b=zz4kGtXYiWyzxcBgiHo9zJZjP4yXMBOWjGd2NR6A+fOs6DWJAv08ulOkiLpTqoAc3d 8V9Hijw/l3ESyANXEWJJFfCclJT/AbRL64MKn2zh4wHNCmFnk51CsZ8WVIblQUspy1ER ChO3CvwXNnx2f1ZhUkIlxcLTT4tQ9kJMA95ihKWAtotIOD3Y4rlQ8XIH34AVJlHhMyr/ Uzp3tyJm9brrfI4V9RhOHQhcNSyxtdWw2nMQlEGlHORWsD/WIhmlSkfSbDhK2SPzlLCl 5lJGZgdYET3W1wpS4hreqLS2qDFveugZobAFB7uPnCICprOzqmHUIuiaWI+PsQc8fwUv VBLQ== 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=f6rtG7Un4P4Xj/6vZAzbY09WAVQb9fcWbN/qI879Oms=; b=qCLDK077cYtObJYNmfo3icXu8WF0WYXLqyGrLO4tB32g04UqAG69+ykQKAo6f+Z7UJ VWxbrOzJkGT39wRtagcKlaIWjnRGwRxedgnlDUAgjrIalcZk+mrGvMH4bq6wkKgs1CC4 Lt96Gh6OrmZVjrykKTNEPkH4dD8Q7Rb65ifxIGTLVtWIW3GKBHgemKIrTax7Tz3WKmrg 7eXC1tJwnbycAaNQEN2Duxnq+nBakOrcIA77cu7Rx6aVTo/nyBhvPcV3P5Q+T+CJ/u0+ fsiYwJpAXZWILkTc+huYP1weMGYSr7Gjqxo3QjAylqkTD24ntB5JlkCf8HvAGhwkDj8+ DBLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=htZHXhbg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 1si15355353eje.566.2021.07.21.04.43.49; Wed, 21 Jul 2021 04:44:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=htZHXhbg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233692AbhGULBb (ORCPT + 99 others); Wed, 21 Jul 2021 07:01:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:49394 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238005AbhGUKxW (ORCPT ); Wed, 21 Jul 2021 06:53:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 28D6E60FD7; Wed, 21 Jul 2021 11:33:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626867236; bh=7HTyWj6nkF3axfBN153vLEf4PrH4qOV+zCpywxp9zmo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=htZHXhbgPf1QP2E2Rzp3rRqF0Sod5ysFc38xW1gQQzlxuhDIzq5aqnExEqs53bJ0B Wu7soDgPaVgyw3Xzz9jsNbLpA91aOnVGBzPHXtegXNJp/jjZ5GuctnQ2/0IpmUZb4F POFf+6LOy8OYm0LCn73tPB+EIaUnyEXWX++pAWms= Date: Wed, 21 Jul 2021 13:33:54 +0200 From: Greg KH To: Luis Chamberlain Cc: tj@kernel.org, shuah@kernel.org, akpm@linux-foundation.org, rafael@kernel.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, andriin@fb.com, daniel@iogearbox.net, atenart@kernel.org, alobakin@pm.me, weiwan@google.com, ap420073@gmail.com, jeyu@kernel.org, ngupta@vflare.org, sergey.senozhatsky.work@gmail.com, minchan@kernel.org, axboe@kernel.dk, mbenes@suse.com, jpoimboe@redhat.com, tglx@linutronix.de, keescook@chromium.org, jikos@kernel.org, rostedt@goodmis.org, peterz@infradead.org, linux-block@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 4/4] test_sysfs: demonstrate deadlock fix Message-ID: References: <20210703004632.621662-1-mcgrof@kernel.org> <20210703004632.621662-5-mcgrof@kernel.org> <20210703172828.jphifwobf3syirzi@garbanzo> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210703172828.jphifwobf3syirzi@garbanzo> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jul 03, 2021 at 10:28:28AM -0700, Luis Chamberlain wrote: > On Sat, Jul 03, 2021 at 06:49:46AM +0200, Greg KH wrote: > > On Fri, Jul 02, 2021 at 05:46:32PM -0700, Luis Chamberlain wrote: > > > +#define MODULE_DEVICE_ATTR_FUNC_STORE(_name) \ > > > +static ssize_t module_ ## _name ## _store(struct device *dev, \ > > > + struct device_attribute *attr, \ > > > + const char *buf, size_t len) \ > > > +{ \ > > > + ssize_t __ret; \ > > > + if (!try_module_get(THIS_MODULE)) \ > > > + return -ENODEV; \ > > > + __ret = _name ## _store(dev, attr, buf, len); \ > > > + module_put(THIS_MODULE); \ > > > + return __ret; \ > > > +} > > > > As I have pointed out before, doing try_module_get(THIS_MODULE) is racy > > and should not be added back to the kernel tree. We got rid of many > > instances of this "bad pattern" over the years, please do not encourage > > it to be added back as others will somehow think that it correct code. > > It is noted this is used in lieu of any agreed upon solution to > *demonstrate* how this at least does fix it. In this case (and in the > generic solution I also had suggested for kernfs a while ago), if the > try fails, we give up. If it succeeds, we now know we can rely on the > device pointer. If the refcount succeeds, can the module still not > be present? Is try_module_get() racy in that way? In what way is it > racy and where is this documented? Do we have a selftest to prove the > race? As I say in the other email where you tried to add this, think about what happens if the module is removed _right before_ you make this call. Or a few instructions before that. The race is still there, this fixes nothing except make the window smaller. thanks, greg k-h