Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp902196pxv; Thu, 22 Jul 2021 15:38:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRTCasL9EXtTLCbVrun+a6/D023l5uzsqX78tETHuNNYSzh/eU8uGrKLQEdLfcmrrQLn1b X-Received: by 2002:a6b:8b0d:: with SMTP id n13mr1434225iod.169.1626993482022; Thu, 22 Jul 2021 15:38:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626993482; cv=none; d=google.com; s=arc-20160816; b=sf2bMblP9zosAyAiNPpgKyjJeCWVC7EzmLpOMFQPyT8SL7LV0Djautygke7anlYNDO M0H3QFjhjYkZOL1OWBE/Cd0f/QdtZUQ+t8iF4PtDjP+MY5JOjYYG8HSw8Ova1ORyPwlZ z5lM6UUrZRJPJnJrhDiful75KpFqkmqapQFLmGq+ZtQHjMhK/ri6jSAAqbuSSR1UtpHP YrKW5jhWWnVsCBL4zlr8K9dzgPes8GzocJOVz/mNLVwllqdgE7Tb2LLchbmNKiSFkLd+ UJFOIAbZeAVD14uTPjjXmuleHJfNruq/47qQZRvjl5R0fsVZMG55Ueuu4sbE35+t81OG 0VJA== 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; bh=wVoXeyrAZeRK2qfGjVQGLsO4VdyxuMrzVeqIO5OglUw=; b=tWtcv9huVcgkE6ds9/65d+P1ZiUu18ouR0aIiCHLIHsfbwMPKo5UmwnSjvq7JL+lGn Noj2LR9HtRlSX31bsi1HyZLDTFBpy+LEFQHgcbiXRIeuVq7CQThvpqZHXklMOxOxl0UD ySlAIjUi8mR7+EGxkhUVlD2tH0cxxswsVH32TgLpFcgQjCTVS5j9t5El19cPcrOCuQsq RUv940oeZWo5SHzjiV2z7BbioH+KzU52rp6n07Nqx6Xi64EPUEDQ1DlKmRK0nCpxVc+9 amOy8RyTrstTIE5bMtZjLLu7SQTfBHI3wY3D8Mqr6rZb+0Fs2ZLeOJhn47O1S8g+9phj pA4A== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si4665776iog.8.2021.07.22.15.37.50; Thu, 22 Jul 2021 15:38:02 -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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232458AbhGVVzn (ORCPT + 99 others); Thu, 22 Jul 2021 17:55:43 -0400 Received: from mail-pj1-f49.google.com ([209.85.216.49]:36637 "EHLO mail-pj1-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231596AbhGVVzm (ORCPT ); Thu, 22 Jul 2021 17:55:42 -0400 Received: by mail-pj1-f49.google.com with SMTP id ds11-20020a17090b08cbb0290172f971883bso6347633pjb.1; Thu, 22 Jul 2021 15:36:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=wVoXeyrAZeRK2qfGjVQGLsO4VdyxuMrzVeqIO5OglUw=; b=mqEyuC6ZMWktzbi4VQaqT12clfewhUfjgYjhLLJWilS/BWcujWr/Tooiw+eMT6BLqh /ENm/SLU5phzstl8LKPdTj8pRh1NhEXo9prCYCboNm3XiWnJH9WfPd22r7CoDTPyVWa2 YRgZBt2uqwUAjP78NVsxPgAAEIhFQapsvjQlUiYcYbl4bF/0bhdzgkWIJLfORzBZQxBg bUnadNs+hj5iUdke/+d3SJmYADbKjDn/T01GThJmE64eObVSWraXdtDxSARVW12+vF7D 1jPqT4CT+VKdtsmd6EHUnmThPB9vuOilGVV1RZaeBbDPxjzE9tqgFOxo90D+R8llRBCE /4EQ== X-Gm-Message-State: AOAM533HZJbmi0jZtuE3USzDYXo+y0mbQRF95vWAlO8YqTAaaheJQ5Sx eElfN4GbPRnQqDu0d/eGNiM= X-Received: by 2002:a17:902:9a92:b029:127:8aab:6a46 with SMTP id w18-20020a1709029a92b02901278aab6a46mr1851463plp.8.1626993377124; Thu, 22 Jul 2021 15:36:17 -0700 (PDT) Received: from garbanzo ([191.96.121.239]) by smtp.gmail.com with ESMTPSA id c11sm33414756pfp.0.2021.07.22.15.36.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jul 2021 15:36:16 -0700 (PDT) Date: Thu, 22 Jul 2021 15:36:12 -0700 From: Luis Chamberlain To: Greg KH 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: <20210722223612.bdkdolndhnr4yogt@garbanzo> 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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 21, 2021 at 01:33:54PM +0200, Greg KH wrote: > 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. The kernfs active reference ensures that if the file is open the module must still exist. As such, the use within sysfs files should be safe as the module is the one in charge of removing the files. Luis