Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp894028pxv; Thu, 22 Jul 2021 15:21:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzz+6wpmraO6t92748kJghaCKJ80J45xWPdm2i8Chu589fLDHtpcK6kyLcgV6e3+LluA3xD X-Received: by 2002:a05:6e02:1b03:: with SMTP id i3mr1390411ilv.160.1626992468854; Thu, 22 Jul 2021 15:21:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626992468; cv=none; d=google.com; s=arc-20160816; b=conJj7h3T7aRK/jKfuItGeSwlu7/gI4fXeI4usEYfLF2oa0rIRoz4LfRi7QVFK9KQ7 x/bZoR1qAlDFbF2tLyHiJDslhZaHnN/8hL6uOfdWuY0jYQ2nX7Xns2/dmz4hzMvRSwVt 2Yy2ImBfSHSauIAzgKQA3taRej9Q8wvTDp31xnqTuk30gxPHobp5KWsTjoBdZEfBQMaz ojyIQjGDaL61ugYeUfKQMGWLbpffV+PSReVWUGcgxwFYnJhFY0F7Atc3it34Tv4pHK6G 0GegYBJo/luPWcS6WUeiwL0AX5a20Uye1aZlD7bbCoTnLDy288g7ZNR3MrevYev/waeF +OhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=lWYwLLr1F+wEohZcNxTrxT9x/1X4krqlfhkjTKwn5LU=; b=MB8rsj+MSaaNCJ89YX+TWw/xTT18S6TqZZafdGfoZAdoAkmhtVGfADgaakiPkzYn05 dNPfNy+5EFyerUnrocB3ocd1d0dVXjDK/8poPyoNEIP7GcOLpdhPvQocb7AFKL75Baz4 HkXxPW6x23DugttJEyFuhF2fZ2bkOkMnQtbLx3CFMDjzNs5doAe6JDiyZzgVI6zO7GqQ 3EhLhT1e+9IyWpjAQ5qcEKNRmt2qm0Lt/6RcxBfnXAm35l0tBCUj1sG4XryTxjCYLTnq Cez4rWaQihGzQ4eBgXnKCRk2IWN1vQ1WmGPm5Yhg3aBFxFzKOHdPxXoL9txaWy9doVCP UbjA== 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 x12si14328998iom.57.2021.07.22.15.20.57; Thu, 22 Jul 2021 15:21:08 -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 S232529AbhGVVig (ORCPT + 99 others); Thu, 22 Jul 2021 17:38:36 -0400 Received: from mail-pl1-f171.google.com ([209.85.214.171]:39742 "EHLO mail-pl1-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232375AbhGVVif (ORCPT ); Thu, 22 Jul 2021 17:38:35 -0400 Received: by mail-pl1-f171.google.com with SMTP id e5so996870pla.6; Thu, 22 Jul 2021 15:19:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lWYwLLr1F+wEohZcNxTrxT9x/1X4krqlfhkjTKwn5LU=; b=Zw5P5x4B2pcmiuvW4R7oJVCjTwx72gcEgkCCMUOy5gKQ80kuskDFmKGvfeUGP/6Ufx P+odj9q3IHgbF16n45gtEGoLT7M6u0EibME0h7ty7yyUL533IY3mkojcsNjNyvM/suKQ VIr3AxPM0YMMMNLDZR4R76d30xvukAiArdGkS3p1HO5ZcOMYQ4MMvHtAUx2oLhfx6P4k b2Nl/XnTW/jlgmryoZ1NctDWkRNTuAXy/ehpZzc3BSX04PxFzIJ0pfAxyR8b/oaUuU6l /wr8GC5wvRQqcH06ZdoDCIIPvIvSiG3hH61mKzVpbwNudIlmerO0eVLrLOFN4m6FmeUY xkoQ== X-Gm-Message-State: AOAM530Sv1GhZQIjLRz8FKGpKUZkRUA856e6HXyucw/tLsX9gRK8p47t F3FiVPZEKXiNdP2oC2ORq3A= X-Received: by 2002:a63:449:: with SMTP id 70mr2067429pge.174.1626992349448; Thu, 22 Jul 2021 15:19:09 -0700 (PDT) Received: from localhost ([191.96.121.239]) by smtp.gmail.com with ESMTPSA id q31sm8997884pjh.13.2021.07.22.15.19.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 22 Jul 2021 15:19:08 -0700 (PDT) From: Luis Chamberlain To: gregkh@linuxfoundation.org, 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 Cc: jeyu@kernel.org, ngupta@vflare.org, sergey.senozhatsky.work@gmail.com, minchan@kernel.org, mcgrof@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: [PATCH] kernel/module: add documentation for try_module_get() Date: Thu, 22 Jul 2021 15:19:05 -0700 Message-Id: <20210722221905.1718213-1-mcgrof@kernel.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Luis Chamberlain --- kernel/module.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/kernel/module.c b/kernel/module.c index ed13917ea5f3..0d609647a54d 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -1066,6 +1066,28 @@ void __module_get(struct module *module) } EXPORT_SYMBOL(__module_get); +/** + * try_module_get - yields to module removal and bumps reference count otherwise + * @module: the module we should check for + * + * This can be used to check if userspace has requested to remove a module, + * and if so let the caller give up. Otherwise it takes a reference count to + * ensure a request from userspace to remove the module cannot happen. + * + * Care must be taken to ensure the module cannot be removed during + * 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 gaurantees the module could not be removed during an operation. + * An example of this later case is using this call in a sysfs file which the + * module created. The sysfs store / read file operation is ensured to exist + * and still be present by kernfs's active reference. If a sysfs file operation + * is being run, the module which created it must still exist as the module is + * in charge of removal of the sysfs file. + * + * The real value to try_module_get() is the module_is_live() check which + * ensures this the caller of try_module_get() can yields to userspace module + * removal requests and fail whatever it was about to process. + */ bool try_module_get(struct module *module) { bool ret = true; -- 2.30.2