Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4906395pxb; Tue, 5 Oct 2021 13:01:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7kc/Ps+qbhnkMxj62qdgCbXoQK148IQrKEc32a2QdUEXqVgxfUyekZ3DLuX9w3NBRwraq X-Received: by 2002:a50:bb67:: with SMTP id y94mr20733145ede.308.1633464115997; Tue, 05 Oct 2021 13:01:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633464115; cv=none; d=google.com; s=arc-20160816; b=xZHGLp+CRHxGEDKds7gesN8QayVV6qb9EMXNKfPMTBsIRPmWB0t/sWBkZX3s0a5hiD 0lh0JOuJgqx5n0CSuGltcERNU9niAIP77s0cttYq+kNqqPWWHQI/dcvc4pto1w5BKVoP STCJjXRhvCdE+OyF4LiuDwR/UaDznby52CpoNwebhNuLpBFooezNjF7KrIStpklPZ814 kesPlkLpz2F4/j2d5Chdp7ZQyrWNuRENtv1ce0UDCgDncxpYhHtJxWMQDZj5y05/eaVS A8hAbgAjmYzJKBVAzDiB0uRhThh2FaLHNDfoQ2lk8XLPQhuIOa43jpyK3Dtr7QfXKoR+ gADw== 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=m4F7KbIKLk052E6TxP0dKBCdgkUeYdYXEOD3CSUVTIM=; b=PtKX/1f30ljJLGGnPTxsv69rhPQb7F1l3Ob9qaBjxtvOr8yaOhBZswDzi+RHr9OjKU E34n6DnAxqv9at8te8oQ64kgf8VDfvYTc81qv0iybEsVFWUmxSZl/WzJxv88sq3Eftyn SnkhOcKWlJ/0vCYI2AWQbKpcuelnA0262e5h8aWfBbwjkl2Yl3kcOw/un/82/ucntHCK btOnawc6aZtnHKHd6GgzzMt5sr3wmSEc1kE01aBaPJJjAkZVohw3ShEvL697CHTc7Drx vZjlHKTxHDnoM9KGINqVoxVwSG8rJstA+UREUW9HJ0I2OXwCkcnyToXhozezhI6JJQGt //6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XniNzJtB; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hc43si7048044ejc.207.2021.10.05.13.01.14; Tue, 05 Oct 2021 13:01:55 -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=@chromium.org header.s=google header.b=XniNzJtB; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235830AbhJEUAm (ORCPT + 99 others); Tue, 5 Oct 2021 16:00:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235224AbhJEUAj (ORCPT ); Tue, 5 Oct 2021 16:00:39 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09557C061755 for ; Tue, 5 Oct 2021 12:58:48 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id n2so146998plk.12 for ; Tue, 05 Oct 2021 12:58:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=m4F7KbIKLk052E6TxP0dKBCdgkUeYdYXEOD3CSUVTIM=; b=XniNzJtBFvHro/VDe66WdKX8l3Za6IBuWSuno6KT9zYxbIObQx4/GX0LtST1Dn0+4w 7KNbmA5IhdFz102EuOBTy9EJBhM9WpzMsz7QEHQK4OkIN8ggyeHsXK+xZi4niG+OJynC mcaeqHjTrmLl4hn5gr+PJKQQj2Kp203ryO25c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=m4F7KbIKLk052E6TxP0dKBCdgkUeYdYXEOD3CSUVTIM=; b=ZJEfgANKdNr84fn6P2JO/UIYmHuw9LJkds0fsJCdryiC0ilC3xqgc8cqfCHcCbBOQ8 sXKAAFrVKeHPU0CPvWRQW6ImMILP/xCRPO1L58cxoum/s8N14BjVjyrRMMlzhbsKqX07 JrOTRGc3OfggdTP8bEK9FbozCH1am1tGYbKj5GLsh4UCPlUt9Djt2U2p4hQ4T4Q97/8W 4Yy6Ks0CF0HTcWG/RiVGEKfZ8YZnURY0Sw2HKSxHjXD3Y8xtcTUNe2HziqrrgL7Prh1a 8Nwe1ALX1L20C79DXIvk0xcH9Oyge9owaJtmeujOdSsnyJd+EMQS5txnKzXTfy0T8Vlc qO7w== X-Gm-Message-State: AOAM530Hyp4y0s+Byjdg34kb/I9ciNgnNqelIhf8Ppi2dG1eAHq80Ttx xC7Qwq5/5ti6XVSPxA2clZA+Rg== X-Received: by 2002:a17:90a:19d2:: with SMTP id 18mr6010010pjj.122.1633463928380; Tue, 05 Oct 2021 12:58:48 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 23sm2915094pjc.37.2021.10.05.12.58.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Oct 2021 12:58:48 -0700 (PDT) Date: Tue, 5 Oct 2021 12:58:47 -0700 From: Kees Cook To: Luis Chamberlain Cc: tj@kernel.org, gregkh@linuxfoundation.org, akpm@linux-foundation.org, minchan@kernel.org, jeyu@kernel.org, shuah@kernel.org, bvanassche@acm.org, dan.j.williams@intel.com, joe@perches.com, tglx@linutronix.de, rostedt@goodmis.org, linux-spdx@vger.kernel.org, linux-doc@vger.kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v8 06/12] kernel/module: add documentation for try_module_get() Message-ID: <202110051252.790B3F2F0@keescook> References: <20210927163805.808907-1-mcgrof@kernel.org> <20210927163805.808907-7-mcgrof@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210927163805.808907-7-mcgrof@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 27, 2021 at 09:37:59AM -0700, Luis Chamberlain wrote: > There is quite a bit of tribal knowledge around proper use of > try_module_get() and that it must be used only in a context which > can ensure the module won't be gone during the operation. Document > this little bit of tribal knowledge. > > I'm extending this tribal knowledge with new developments which it > seems some folks do not yet believe to be true: we can be sure a > module will exist during the lifetime of a sysfs file operation. > For proof, refer to test_sysfs test #32: > > ./tools/testing/selftests/sysfs/sysfs.sh -t 0032 > > Without this being true, the write would fail or worse, > a crash would happen, in this test. It does not. > > Signed-off-by: Luis Chamberlain > --- > include/linux/module.h | 34 ++++++++++++++++++++++++++++++++-- > 1 file changed, 32 insertions(+), 2 deletions(-) > > diff --git a/include/linux/module.h b/include/linux/module.h > index c9f1200b2312..22eacd5e1e85 100644 > --- a/include/linux/module.h > +++ b/include/linux/module.h > @@ -609,10 +609,40 @@ void symbol_put_addr(void *addr); > to handle the error case (which only happens with rmmod --wait). */ > extern void __module_get(struct module *module); > > -/* This is the Right Way to get a module: if it fails, it's being removed, > - * so pretend it's not there. */ > +/** > + * try_module_get() - yields to module removal and bumps refcnt otherwise I find this hard to parse. How about: "Take module refcount unless module is being removed" > + * @module: the module we should check for > + * > + * This can be used to try to bump the reference count of a module, so to > + * prevent module removal. The reference count of a module is not allowed > + * to be incremented if the module is already being removed. This I understand. > + * > + * Care must be taken to ensure the module cannot be removed during the call to > + * try_module_get(). This can be done by having another entity other than the > + * module itself increment the module reference count, or through some other > + * means which guarantees the module could not be removed during an operation. > + * An example of this later case is using try_module_get() in a sysfs file > + * which the module created. The sysfs store / read file operations are > + * gauranteed to exist through the use of kernfs's active reference (see > + * kernfs_active()). If a sysfs file operation is being run, the module which > + * created it must still exist as the module is in charge of removing the same > + * sysfs file being read. Also, a sysfs / kernfs file removal cannot happen > + * unless the same file is not active. I can't understand this paragraph at all. "Care must be taken ..."? Why? Shouldn't callers of try_module_get() be satisfied with the results? I don't follow the example at all. It seems to just say "sysfs store/read functions don't need try_module_get() because whatever opened the sysfs file is already keeping the module referenced." ? > + * > + * One of the real values to try_module_get() is the module_is_live() check > + * which ensures this the caller of try_module_get() can yield to userspace > + * module removal requests and fail whatever it was about to process. Please document the return value explicitly. > + */ > extern bool try_module_get(struct module *module); > > +/** > + * module_put() - release a reference count to a module > + * @module: the module we should release a reference count for > + * > + * If you successfully bump a reference count to a module with try_module_get(), > + * when you are finished you must call module_put() to release that reference > + * count. > + */ > extern void module_put(struct module *module); > > #else /*!CONFIG_MODULE_UNLOAD*/ > -- > 2.30.2 > -- Kees Cook