Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1265620pxv; Fri, 23 Jul 2021 04:16:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwsexlSxGKgiXc47KCW7vOt8NeLIBXaptShlCy+R7HgaB3jX/qGRGi7Sx0KZANbWollGyqb X-Received: by 2002:a05:6e02:dc8:: with SMTP id l8mr3188944ilj.5.1627039014670; Fri, 23 Jul 2021 04:16:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627039014; cv=none; d=google.com; s=arc-20160816; b=XFIdrRrVn/5Ls+ER9PzQfzVjmwRTKultPu8xHsgRNhZT2XiCNb3nHWrvxsoINP4utX j5gDPfAYe+QATbNxGUFK1AbkGmD0Ro4sCxU2ZCtGmsF11Gi0R4taR3a4eqsNQeO1NDP/ ++Q12MluZfugUQEUoOLEXojp4XGhthQSY6AE72qDbPBmJlGKLi1Ph0z2jTOV3iTJMrg3 jEiqp3wKUl3/b9D8zsK75PSeRbEidmV5ETc8hAd6MXc79/jZe1tEAWHefObk5J6eBXUa w2GiJVmw3Zhqe4SU42ASRsmqV8XZGHbKeAEUcpPxDMbbUOoFY9tKK6d3BuCfxwHeNX/q W4sg== 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=ziKnuA4AwyI/G8bCGFmXnzOfE5Ek0oxpwDs4PNZyQ9o=; b=go2iYw4GI+DtvumwYnp5fhbMcWjp0DqR3HSmFRwXpfW5TzhVKfCFntWqeQYsHNcp5L +e3H+UEZqwYnUEswt5IxR9GaLdUhTAlLwJrILK7TlGEghp2B0LnMneDUBA2tbqnyWF5U l1vabEZX2p53wMl35uDvXz4lmvDptax9+VfZ7FCYZvLJLpibIglBl15fUs6JIHOmjhtK SIHmnGQL5zKOvMcibOjSX8syJm0HCAzN1at5GNwPonGLu2M/ljc8GkL4pu/jm5UA1rDY XAs5ZbScSpuzj5p/OEok92pozjYD4dX57Oy6xzA458nu6zKKhzjYv2pjcnr2qY1msrZD uVNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=NIKLySDV; 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 v1si25604677iot.7.2021.07.23.04.16.42; Fri, 23 Jul 2021 04:16:54 -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=NIKLySDV; 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 S231848AbhGWKfT (ORCPT + 99 others); Fri, 23 Jul 2021 06:35:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:60800 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231428AbhGWKfT (ORCPT ); Fri, 23 Jul 2021 06:35:19 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EB1CF60EBD; Fri, 23 Jul 2021 11:15:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627038951; bh=vPpc4EBJHfaWG3UHa1IL/ov8VeoKpBgjAjG5K6zxFA8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NIKLySDVvcFzKJ/ht2HpdPzrhRBzdM63jR4veD8E24cNoyNWHpYsFnuTJfMNOrqN/ mrE/yotkZCfG+BlWLB57UZo9Sz7yaMqUBFlRJBf75pFfZG9gNGI8/zcEn9h2fn0mkM 0AYxMggHkHJycXvRQTjJYqN/xU+1xK0f3QTLG81U= Date: Fri, 23 Jul 2021 13:15:49 +0200 From: Greg KH To: Luis Chamberlain Cc: akpm@linux-foundation.org, minchan@kernel.org, jeyu@kernel.org, ngupta@vflare.org, sergey.senozhatsky.work@gmail.com, rafael@kernel.org, axboe@kernel.dk, tj@kernel.org, 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, linux-kernel@vger.kernel.org Subject: Re: [PATCH v6 2/3] zram: fix deadlock with sysfs attribute usage and module removal Message-ID: References: <20210703001958.620899-1-mcgrof@kernel.org> <20210703001958.620899-3-mcgrof@kernel.org> <20210722221705.kyrdkpt6fwf5k56o@garbanzo> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210722221705.kyrdkpt6fwf5k56o@garbanzo> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 22, 2021 at 03:17:05PM -0700, Luis Chamberlain wrote: > On Wed, Jul 21, 2021 at 01:29:29PM +0200, Greg KH wrote: > > On Fri, Jul 02, 2021 at 05:19:57PM -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; \ > > > > I feel like this needs to be written down somewhere as I see it come up > > all the time. > > I'll go ahead and cook up a patch to do just this after I send this > email out. > > > Again, this is racy and broken code. You can NEVER try to increment > > your own module reference count unless it has already been incremented > > by someone external first. > > In the zram driver's case the sysfs files are still pegged on, because > as we noted before the kernfs active reference will ensure the store > operation still exists. How does that happen without a module lock? > If the driver removes the operation prior to > getting the active reference, the write will just fail. kernfs ensures > once a file is opened the op is not removed until the operation completes. How does it do that? > If a file is opened then, the module cannot possibly be removed. The > piece of information we realy care about is the use of module_is_live() > inside try_module_get() which does: > > static inline bool module_is_live(struct module *mod) > { > return mod->state != MODULE_STATE_GOING; > } > > The try allows module removal to trump use of the sysfs file. If > userspace wants the module removed, it gives up in favor for that > operation. I do not see the tie in kernfs to module reference counts, what am I missing? thanks, greg k-h