From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FDA43D75AD for ; Thu, 6 Aug 2026 09:11:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786007502; cv=none; b=ajmMlrR5W95/BKvvgvoKKCyMe0JezbwQcEmM+bU/ALFsmTNy/ncGO6F5u24em9woU8sO4Tkq4atm1UNHkmxcqSJkHRg++Abyp5X8rpbe0MUGo0DSP8+ic1w/F+8rQcPFtGLUtyVj4JhITv+PLGgx1UGLDBBgDmZpb6JiYj3B/vw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786007502; c=relaxed/simple; bh=gHYpCiquiPuzfw0VdPmWbBvz8c5AF5pIoFf55eZmMD4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=C4tZouPkIzFVSy6X8iyhZbDWQanIy/ortGowzMuQdpv9Em1UoNl8z4SEl7BD0e40k43NbNsfug0qh0xjdn9EI2zH87jdSC2620/+UolYmFgrL1SyNld6/yg0IwcHrMtNCuM1zaNsaN1dsqyoI4D6d3UcCgCxoOHWUU0xb/acXOU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=NWgxOukE; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="NWgxOukE" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4954afac04bso21285375e9.0 for ; Thu, 06 Aug 2026 02:11:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1786007498; x=1786612298; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=3W5Kt8XCRH1ookmt7hLyt3rKd0QiLjUKVXtnwwecrfM=; b=NWgxOukEZ1Ejp78fDnNzgkuS7IvVyzwY1PSZuVTGS0tAVP/+TXl4xN4cjsV7MXtDJf gnG5fHDYPxtpiu1laAnwUbktOZyb/iJ99cWx+v5cVCBWLGGhopmae6Day+cPTQQmv2Ul 3k5o9YhqzhHcocLbm3o6sJtf6DZ6DCid4z8U20foho0giNe/6VTP0gRdeSGFd+SmcoO9 nWHKfok3aM1rzGInHYh+gNfIR+ATQiDqgLt3TfCXcAJx9osKNgr1U+Ng941SLDOe7Tl9 qVeYb6tmUyWhwKHJitLLsIzta4WVl5o8GrK1f+OFAauPK1qRRLO2BXCMrxGE5QwyWoKk Pv9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786007498; x=1786612298; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=3W5Kt8XCRH1ookmt7hLyt3rKd0QiLjUKVXtnwwecrfM=; b=LOY/W9wTOzn8A01qnuRh+hCmPtvYSN2U9WHRc1iKfUR6HcKFWNHY+SaEeBZKJuZ77V x2DcZjQVJrS4nEn7a7TAoAapdFCLnS02ABTpF8gW5NpBUy4iWGchjR8sVBrj7vwcieyG ZiPsYPfzHtAZSeTIs0444ntIeYkeeFp9uFmTIBsP5aLyOGvWdHbKhQYiR5KSxyNNDgR0 PdsK/O+GkeCWh4hTR8l8d7OiLWOF5TQnR++H7eEM0W1t3513ildWCdNf2SWNBrJqS4kM NXnEuEcTukRR78+MCGmjtVIY7dN6uOLOFv4+LxgYb6YiOxxbhngCK3W+iI21XXXpFey0 Mc1g== X-Forwarded-Encrypted: i=1; AHgh+RqU1EoCTDa6GGfmfTXphraeCVYghhofp3/dHLTq9kcigwL3hI8FY7ARp6cpi3reVUzFUv2msx0nNXFHlMc=@vger.kernel.org X-Gm-Message-State: AOJu0YzM2+IQ1bPJ7H2cnhvH465ojdkAajS9mHXdVn/cbIo8UB+S6ZML WJTyFr5X5o5gqv9UzoLY2AMbtfsVatYYAlLRdMia2RwzJBS/WkCOO+hsyvy4ZAxr3AY= X-Gm-Gg: AR+sD12nHVeZC6rzebAkVBm8Tkr2iq8boJQtHeI4kht+5SnzrVe7MgAuZFft/VCftDs P7z3SFe3JUPdyCMLrQcfCpyIn1LOIWV2Ld6yNno3vK4c/3Ozo6aymOQXQ6rmwSSoxhsGac750yP REmgp8mzP8pB6L1FeTwcygIRWa6EvVr1nBd5zd2JbyfDfvgtai6h3CwO9ZheGxqJ9UZl4StCHvt upV5SvEmuflEcovbZO+LDTTnSbVN2yDFLk5MZYkyNf1rXCBd/daW/tFTyMc/lGsFwgQNnMnR7uQ Bl00MPAv3CeBExQhaf3VVSt4r9OXZd0E9U9NCUc/aZdkn0UnLCVZ7jPfgjgmMn5D06+x5AHb6F2 dwm0XHWJmK7E0eVhH5WqPyg0/P4XSMGskBGf47N+PtavdY2AmViAKkFLyrBdumWoTgCHvLBRfqX meQ/wxKNT/72zGGAoR3Fezo02MDZJvcLhofBMG6rqPG9O2IUJWnIEdwFeY X-Received: by 2002:a05:600c:354c:b0:499:51cc:4e57 with SMTP id 5b1f17b1804b1-49951cc6d0fmr109929275e9.0.1786007497764; Thu, 06 Aug 2026 02:11:37 -0700 (PDT) Received: from zovi.suse.cz ([2001:af0:8000:1409:193:86:92:181]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4994e4c32e0sm93157095e9.0.2026.08.06.02.11.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Aug 2026 02:11:37 -0700 (PDT) From: Petr Pavlu To: Luis Chamberlain , Petr Pavlu , Daniel Gomez , Sami Tolvanen Cc: Aaron Tomlin , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/6] module/dups: Avoid unnecessary kmod_dup_req allocations Date: Thu, 6 Aug 2026 11:10:17 +0200 Message-ID: <20260806-module-stats-rcu-v3-3-2b0331106f00@suse.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260806-module-stats-rcu-v3-0-2b0331106f00@suse.com> References: <20260806-module-stats-rcu-v3-0-2b0331106f00@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit The kmod dups code preallocates kmod_dup_req before taking kmod_dup_mutex to avoid allocating memory while holding the lock. This provides little benefit, since the allocation is fast and can safely be done under the lock. On the other hand, it leads to unnecessary allocations when the request turns out to be a duplicate and slightly complicates the code. Allocate kmod_dup_req only when needed and introduce a helper function alloc_kmod_req() to initialize the structure. Reviewed-by: Aaron Tomlin Signed-off-by: Petr Pavlu --- kernel/module/dups.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/kernel/module/dups.c b/kernel/module/dups.c index db7377229703..276016051ad2 100644 --- a/kernel/module/dups.c +++ b/kernel/module/dups.c @@ -104,28 +104,31 @@ static void kmod_dup_request_delete(struct work_struct *work) put_kmod_req(kmod_req); } -bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) +static struct kmod_dup_req *alloc_kmod_req(const char *module_name) { - struct kmod_dup_req *kmod_req, *new_kmod_req; - int ret; + struct kmod_dup_req *kmod_req = kzalloc_obj(*kmod_req); - /* - * Pre-allocate the entry in case we have to use it later - * to avoid contention with the mutex. - */ - new_kmod_req = kzalloc_obj(*new_kmod_req); - if (!new_kmod_req) - return false; + if (!kmod_req) + return NULL; - refcount_set(&new_kmod_req->refcount, 1); - strscpy(new_kmod_req->name, module_name); - INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete); - init_completion(&new_kmod_req->first_req_done); + refcount_set(&kmod_req->refcount, 1); + strscpy(kmod_req->name, module_name); + INIT_DELAYED_WORK(&kmod_req->delete_work, kmod_dup_request_delete); + init_completion(&kmod_req->first_req_done); + return kmod_req; +} + +bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) +{ + struct kmod_dup_req *kmod_req; + int ret; mutex_lock(&kmod_dup_mutex); kmod_req = kmod_dup_request_lookup(module_name); if (!kmod_req) { + struct kmod_dup_req *new_kmod_req; + /* * If the first request that came through for a module * was with request_module_nowait() we cannot wait for it @@ -138,7 +141,6 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) * would benefit from duplicate detection. */ if (!wait) { - kfree(new_kmod_req); pr_debug("New request_module_nowait() for %s -- cannot track duplicates for this request\n", module_name); mutex_unlock(&kmod_dup_mutex); return false; @@ -149,6 +151,11 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) * keep tab on duplicates later. */ pr_debug("New request_module() for %s\n", module_name); + new_kmod_req = alloc_kmod_req(module_name); + if (!new_kmod_req) { + mutex_unlock(&kmod_dup_mutex); + return false; + } list_add(&new_kmod_req->list, &dup_kmod_reqs); mutex_unlock(&kmod_dup_mutex); return false; @@ -158,7 +165,6 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) mutex_unlock(&kmod_dup_mutex); /* We are dealing with a duplicate request now */ - kfree(new_kmod_req); /* * To fix these try to use try_then_request_module() instead as that -- 2.55.0