Received: by 2002:a05:7412:518d:b0:e2:908c:2ebd with SMTP id fn13csp315644rdb; Thu, 5 Oct 2023 06:56:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGbB1Tm2fcoq5maunYdEHJNkx8wp8xI7Mwu7n+qro1XEGuc1T4VwX8swGf8VfxGVG5dObdS X-Received: by 2002:a05:6e02:12e3:b0:34d:ff6c:3e22 with SMTP id l3-20020a056e0212e300b0034dff6c3e22mr6497481iln.20.1696514176125; Thu, 05 Oct 2023 06:56:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696514176; cv=none; d=google.com; s=arc-20160816; b=TiAom0gfGo9lE2Ib3vi0h4Uu8bEfD0WSZfoRa8afwB7U05Nox8BBbbZX9H6L9sJc0n KEWn5w/fqYXHnz4SEMa1B8udny+a/KkIOzcFhdsGytit6GBqC7X4OrLGmbMHfXbtGCd2 D/CC89tpwY6twyR6SbxbFAfHOYqcMB5wt2pHhnD9IHUqics56hap4VP6eKbJJkRXUXod jQa5Ic2BPrLJE9qFrHvl7OnlNJWJS99ueCRd8ggmtjHXhQnNea8hmv62jFNbZkwwdhEZ 8MiSD1txZK50S2eccR4Ogn3xj5GD5CM5NysXogfVF+fzXj66F0cdf/bUXH45j9GoVKjo reNw== 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=yLNoHDbJ10mcu0IVX/StKsMp6qzKf98pbXCXR9Rqaug=; fh=LMimJJHAGJQAxa8ES6PZTGBEDzrZfy9j1/0wCeHmW1k=; b=ove3QdZvIss/VZTb4U+LB48Yfhijs1k0VKXX1PZkKx+QMMse90gGpjUm25PYd2uRzj R18P4XPw/PNwia02XZ3GFMF4gGfIHSmuUpgqeVlf2yKNpJUemubojZOjZXJ35IBe5QLk lTUzWPzo+Cd829+82EQyBdu6hOJpXwcVnCbvdz6MAXEm9JP+7vaLOFv4dtVnS9Q2BXUG lnOrYnOPJfsCMw9DlrrJMuZ5cZTuJKWSYbVWOg+lcl2CXBab3+z6gqg5KguMER/uWEk3 DBO4UMxmARmR4UF4I/WiQSrXZWbvtIPPBJaQq/YAIDraD7ziHXgrNjrwPkw+U9DOI4yn s44A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=s9Z4fSo8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id q136-20020a632a8e000000b00573f76b1f75si1461348pgq.592.2023.10.05.06.56.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 06:56:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=s9Z4fSo8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id C0B5080907BB; Thu, 5 Oct 2023 06:56:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233830AbjJENyT (ORCPT + 99 others); Thu, 5 Oct 2023 09:54:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244090AbjJENvP (ORCPT ); Thu, 5 Oct 2023 09:51:15 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0920023D11 for ; Thu, 5 Oct 2023 04:02:59 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1c5bf7871dcso6357435ad.1 for ; Thu, 05 Oct 2023 04:02:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696503778; x=1697108578; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=yLNoHDbJ10mcu0IVX/StKsMp6qzKf98pbXCXR9Rqaug=; b=s9Z4fSo8xs76+r26MIk/ML/9RwWrGchAxKPlmDE7cv0XEN7zEMY5w0dkWhlDFcNBwZ MggkFfDjiey4bHaYN2VxFlVBS9H4/KMMe9whilPutPmkkRI+YM2qDhsHd8uAoHkeozcm z5uKTtBXD34w9273pzT6E8244YafH8G9nd+8iyqbTqGNNUWQce8VTKjmq9eW/eNfIgRG NRrOFYAEDac+nLOA+hxLiGPQK/azF8xfdbVErqFqKAsl6J0++BkSdtzFjhwviA+SGv8B NAdbK1wXdgpEB9XJd0NjJnH3cUsH61tRpypJ3OttkItv2OaKsj305fv8JQtvPSyClLU1 4hsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696503778; x=1697108578; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yLNoHDbJ10mcu0IVX/StKsMp6qzKf98pbXCXR9Rqaug=; b=aQdCqVlQYuTIvY90FRqFNk0I8P5KGWtm0BnWQxe/kAAby7Amn4BpqM4shyFA93Fywj GDnhy30fRJkWKfHHKwwT4ZaL/ovFiAuD4Qu/TOXiqA+2K+4GiwhXeEBcLa5DjhnIlXdS TxKvDBhU+NS4TYVJBnlW6Fs4WKBFURSG2U0uM99tnqDzq0Yz4avoEx03PpTUJPs5QSpl KYeUaT8cOg+gB7YIjDFSA1fQPEVsHxHgO/u0tu0JHYTGmppRBrjyntAFvvAugfGdrPmA zLJUJSxcJXvQ9A8mrU00bUMtHg9mIDjfwokZMuxzYKNqfkqRnWN0+4+3hJHeMMtB6w/h tLIg== X-Gm-Message-State: AOJu0YyHFCUWs2M3EwRPNGCWX13312pBcTr225XR8hTQewFhSduyRG7L mxuTW9dHHp8u32GrtFFy7+4iMQ== X-Received: by 2002:a17:902:f80a:b0:1c0:cb4d:df7c with SMTP id ix10-20020a170902f80a00b001c0cb4ddf7cmr4476950plb.1.1696503778489; Thu, 05 Oct 2023 04:02:58 -0700 (PDT) Received: from localhost ([122.172.81.92]) by smtp.gmail.com with ESMTPSA id e18-20020a17090301d200b001b89b1b99fasm1345702plh.243.2023.10.05.04.02.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 04:02:57 -0700 (PDT) Date: Thu, 5 Oct 2023 16:32:55 +0530 From: Viresh Kumar To: Liao Chang Cc: rafael@kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] cpufreq: userspace: Use fine-grained mutex in userspace governor Message-ID: <20231005110255.fk736npzqsrffl2c@vireshk-i7> References: <20230912061057.2516963-1-liaochang1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230912061057.2516963-1-liaochang1@huawei.com> X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 05 Oct 2023 06:56:12 -0700 (PDT) On 12-09-23, 06:10, Liao Chang wrote: > The userspace governor currently uses a big global mutex to avoid the > race condition on the governor_data field of cpufreq_policy structure. > This leads to a low concurrency if multiple userspace applications are > trying to set the speed of different policies at the same time. This > patch introduces a per-policy mutex to allow the updating of different > policies to be performed concurrently, improving overall concurrency. > > Signed-off-by: Liao Chang > --- > drivers/cpufreq/cpufreq_userspace.c | 69 +++++++++++++++++------------ > 1 file changed, 40 insertions(+), 29 deletions(-) > > diff --git a/drivers/cpufreq/cpufreq_userspace.c b/drivers/cpufreq/cpufreq_userspace.c > index 50a4d7846580..442e31060d62 100644 > --- a/drivers/cpufreq/cpufreq_userspace.c > +++ b/drivers/cpufreq/cpufreq_userspace.c > @@ -16,7 +16,11 @@ > #include > > static DEFINE_PER_CPU(unsigned int, cpu_is_managed); > -static DEFINE_MUTEX(userspace_mutex); > + > +struct userspace_policy { > + unsigned int setspeed; > + struct mutex mutex; > +}; > > /** > * cpufreq_set - set the CPU frequency > @@ -28,19 +32,19 @@ static DEFINE_MUTEX(userspace_mutex); > static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) > { > int ret = -EINVAL; > - unsigned int *setspeed = policy->governor_data; > + struct userspace_policy *userspace = policy->governor_data; > > pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq); > > - mutex_lock(&userspace_mutex); > + mutex_lock(&userspace->mutex); > if (!per_cpu(cpu_is_managed, policy->cpu)) > goto err; > > - *setspeed = freq; > + userspace->setspeed = freq; > > ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); > err: > - mutex_unlock(&userspace_mutex); > + mutex_unlock(&userspace->mutex); > return ret; > } > > @@ -51,67 +55,74 @@ static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) > > static int cpufreq_userspace_policy_init(struct cpufreq_policy *policy) > { > - unsigned int *setspeed; > + struct userspace_policy *userspace; > > - setspeed = kzalloc(sizeof(*setspeed), GFP_KERNEL); > - if (!setspeed) > + userspace = kzalloc(sizeof(*userspace), GFP_KERNEL); > + if (!userspace) > return -ENOMEM; > > - policy->governor_data = setspeed; > + mutex_init(&userspace->mutex); > + > + policy->governor_data = userspace; > return 0; > } > > +/* > + * Any routine that writes to the policy struct will hold the "rwsem" of > + * policy struct that means it is free to free "governor_data" here. > + */ > static void cpufreq_userspace_policy_exit(struct cpufreq_policy *policy) > { > - mutex_lock(&userspace_mutex); > kfree(policy->governor_data); > policy->governor_data = NULL; > - mutex_unlock(&userspace_mutex); > } > > static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy) > { > - unsigned int *setspeed = policy->governor_data; > + struct userspace_policy *userspace = policy->governor_data; > > BUG_ON(!policy->cur); > pr_debug("started managing cpu %u\n", policy->cpu); > > - mutex_lock(&userspace_mutex); > + mutex_lock(&userspace->mutex); > per_cpu(cpu_is_managed, policy->cpu) = 1; > - *setspeed = policy->cur; > - mutex_unlock(&userspace_mutex); > + userspace->setspeed = policy->cur; > + mutex_unlock(&userspace->mutex); > return 0; > } > > static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy) > { > - unsigned int *setspeed = policy->governor_data; > + struct userspace_policy *userspace = policy->governor_data; > > pr_debug("managing cpu %u stopped\n", policy->cpu); > > - mutex_lock(&userspace_mutex); > + mutex_lock(&userspace->mutex); > per_cpu(cpu_is_managed, policy->cpu) = 0; > - *setspeed = 0; > - mutex_unlock(&userspace_mutex); > + userspace->setspeed = 0; > + mutex_unlock(&userspace->mutex); > } > > static void cpufreq_userspace_policy_limits(struct cpufreq_policy *policy) > { > - unsigned int *setspeed = policy->governor_data; > + struct userspace_policy *userspace = policy->governor_data; > > - mutex_lock(&userspace_mutex); > + mutex_lock(&userspace->mutex); > > pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n", > - policy->cpu, policy->min, policy->max, policy->cur, *setspeed); > - > - if (policy->max < *setspeed) > - __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H); > - else if (policy->min > *setspeed) > - __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L); > + policy->cpu, policy->min, policy->max, policy->cur, userspace->setspeed); > + > + if (policy->max < userspace->setspeed) > + __cpufreq_driver_target(policy, policy->max, > + CPUFREQ_RELATION_H); > + else if (policy->min > userspace->setspeed) > + __cpufreq_driver_target(policy, policy->min, > + CPUFREQ_RELATION_L); > else > - __cpufreq_driver_target(policy, *setspeed, CPUFREQ_RELATION_L); > + __cpufreq_driver_target(policy, userspace->setspeed, > + CPUFREQ_RELATION_L); > > - mutex_unlock(&userspace_mutex); > + mutex_unlock(&userspace->mutex); > } > > static struct cpufreq_governor cpufreq_gov_userspace = { Acked-by: Viresh Kumar -- viresh