From patchwork Fri Apr 14 17:16:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13211855 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3CE4C77B79 for ; Fri, 14 Apr 2023 17:17:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D62386B0075; Fri, 14 Apr 2023 13:17:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D380F6B0078; Fri, 14 Apr 2023 13:17:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4E08900002; Fri, 14 Apr 2023 13:17:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B80686B0075 for ; Fri, 14 Apr 2023 13:17:10 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8A583C0217 for ; Fri, 14 Apr 2023 17:17:10 +0000 (UTC) X-FDA: 80680652220.10.ECE74A6 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf23.hostedemail.com (Postfix) with ESMTP id CE32114000D for ; Fri, 14 Apr 2023 17:17:08 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=Nn3dXunC; dmarc=fail reason="No valid SPF, DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=none (imf23.hostedemail.com: domain of mcgrof@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=mcgrof@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681492628; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IkMdZ0kdtkZIOyES16KmIOxLSIavc8A1X3TaLcRG7x0=; b=gVT/tBH1oj5y8wLteivS7y2cjSj+9aw4/pYZOsqrRW5RozK/phIBE2qqDApVd3Vaqq92ST TG7+xwf/cjH6oc8B7Ry1evdHqrtbzFa//IuLxuy0PKUGV5ePs0UyVd3nx1jYK3PkfDuEQM qvJPsIYssqiwCl1FS7GWo+zcHZZ2IvI= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=Nn3dXunC; dmarc=fail reason="No valid SPF, DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=none (imf23.hostedemail.com: domain of mcgrof@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=mcgrof@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681492628; a=rsa-sha256; cv=none; b=D5dIz0yDDlAD8rU83brloTYk8v2/gguYq3vaYoQAryBHmnM6JmOPmTNqhwhw7bqhXTM2hT H9DeDgfFDeB+JJz9P8koMyaVv9v0A18dWMmDuXHTr/R5cqAYy/80XpAobwVtBZnIoZMe/j Fw0uhzFDWdM7AYuqLb+iSsXe7Yu03QU= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=IkMdZ0kdtkZIOyES16KmIOxLSIavc8A1X3TaLcRG7x0=; b=Nn3dXunCruznm9rKwZ+cpfkvoP FhdNnmTCPqGi6a2xr0yyhArQui+wPgx5qJNA2gXyzJn1Bhn+JGarU0Tmz+WWN3pm7hpw7ed04mhwM ysh2LmCgfjqBSczPV7mjQtxJAVR/Gg7D9XSYkOKoaSH21UeYmjdtku6XHTA89TE7cO781bCBw5QIu arImrYrJHrn6I2psg7QSnMsHgjQIJqdc7g1ikBePhJ7mRdI3jOcJERavtmCYsNKtuXrFVRKEdz3Pl X6Y/g03mRkhRUBLiCZAfnr2MK/XPp+WCHQ7Prvx6RFuHVS3ZxL41tVc1GP1UcoHtzYX8WWiECcv0E E5xuSgoA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1pnN2k-00ADLb-2p; Fri, 14 Apr 2023 17:17:02 +0000 From: Luis Chamberlain To: david@redhat.com, patches@lists.linux.dev, linux-modules@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, pmladek@suse.com, petr.pavlu@suse.com, prarit@redhat.com, torvalds@linux-foundation.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: christophe.leroy@csgroup.eu, tglx@linutronix.de, peterz@infradead.org, song@kernel.org, rppt@kernel.org, dave@stgolabs.net, willy@infradead.org, vbabka@suse.cz, mhocko@suse.com, dave.hansen@linux.intel.com, colin.i.king@gmail.com, jim.cromie@gmail.com, catalin.marinas@arm.com, jbaron@akamai.com, rick.p.edgecombe@intel.com, mcgrof@kernel.org, Miroslav Benes Subject: [PATCH v4 2/2] modules/kmod: replace implementation with a semaphore Date: Fri, 14 Apr 2023 10:16:44 -0700 Message-Id: <20230414171644.2434448-3-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230414171644.2434448-1-mcgrof@kernel.org> References: <20230414171644.2434448-1-mcgrof@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: CE32114000D X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: c3f6fmxh158qfixpcaoq5bdynyp1xcty X-HE-Tag: 1681492628-495170 X-HE-Meta: U2FsdGVkX1/NnFoTAbI7e/kRW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/coKfN3cgprW7iTszyd8v0l10QA9hMoNK+pPBTLVszAH94riEkoUGqRVCQ5FLYxpjs6WTPDPAkWtOE3oit75G5P+M2PwGGIEc9QSdevmFxVkO37PaX3OS2tsayMYxUboBS4D0e2TcYT4XpTVt7yFkG6L+JrytjG9P3BdH/7g/LShLDUCdqSVXbT/31MyV/PX/bBv6jQfG7NdWSih80RpATC8zShKUGmb6hVjdH7St9Dr/CsX6I+GRFcFgez32aaxctQUylFkOpJ8hDKt7g2RW8MzB9z8jae524c+GJwCeb3ep0+e8cQwFcbL4GULxKqaJ5kFAzzZ1bHOrPbeTw94QOMD6ybxaHzi8v6EdPo7E5gTzkPPNDpiC0Ss4CKrczyQXYCxFjYKKO2WInzkdPqXvGF4oMhIvV8nqDljjmtognDtaG7mRQLqITD+P4+wxE+d/jIV7svJEH X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Simplify the concurrency delimiter we use for kmod with the semaphore. I had used the kmod strategy to try to implement a similar concurrency delimiter for the kernel_read*() calls from the finit_module() path so to reduce vmalloc() memory pressure. That effort didn't provide yet conclusive results, but one thing that became clear is we can use the suggested alternative solution with semaphores which Linus hinted at instead of using the atomic / wait strategy. I've stress tested this with kmod test 0008: time /data/linux-next/tools/testing/selftests/kmod/kmod.sh -t 0008 And I get only a *slight* delay. That delay however is small, a few seconds for a full test loop run that runs 150 times, for about ~30-40 seconds. The small delay is worth the simplfication IMHO. Reviewed-by: Davidlohr Bueso Reviewed-by: Miroslav Benes Reviewed-by: David Hildenbrand Signed-off-by: Luis Chamberlain --- kernel/module/kmod.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/kernel/module/kmod.c b/kernel/module/kmod.c index b717134ebe17..5899083436a3 100644 --- a/kernel/module/kmod.c +++ b/kernel/module/kmod.c @@ -40,8 +40,7 @@ * effect. Systems like these are very unlikely if modules are enabled. */ #define MAX_KMOD_CONCURRENT 50 -static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT); -static DECLARE_WAIT_QUEUE_HEAD(kmod_wq); +static DEFINE_SEMAPHORE(kmod_concurrent_max, MAX_KMOD_CONCURRENT); /* * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads @@ -148,29 +147,18 @@ int __request_module(bool wait, const char *fmt, ...) if (ret) return ret; - if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) { - pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...", - atomic_read(&kmod_concurrent_max), - MAX_KMOD_CONCURRENT, module_name); - ret = wait_event_killable_timeout(kmod_wq, - atomic_dec_if_positive(&kmod_concurrent_max) >= 0, - MAX_KMOD_ALL_BUSY_TIMEOUT * HZ); - if (!ret) { - pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now", - module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT); - return -ETIME; - } else if (ret == -ERESTARTSYS) { - pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name); - return ret; - } + ret = down_timeout(&kmod_concurrent_max, MAX_KMOD_ALL_BUSY_TIMEOUT * HZ); + if (ret) { + pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now", + module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT); + return ret; } trace_module_request(module_name, wait, _RET_IP_); ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC); - atomic_inc(&kmod_concurrent_max); - wake_up(&kmod_wq); + up(&kmod_concurrent_max); return ret; }