Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp611704rdb; Fri, 6 Oct 2023 13:03:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IElGNq6Z+gUJm6dpc+63I5EG3bxOGnnINSREOdJSgxOolbekhQywVtQcXkYzh0oiX8rmATI X-Received: by 2002:a17:90a:b102:b0:26f:d6f4:9646 with SMTP id z2-20020a17090ab10200b0026fd6f49646mr8474999pjq.40.1696622608331; Fri, 06 Oct 2023 13:03:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696622608; cv=none; d=google.com; s=arc-20160816; b=mtjIMg5M6H7B+dI38k/PfT0bLLtCmlnzpjzGLD/jffW2kVFw3fPsRPUzGH9CRWOS9b 2ZbtDP1DCVfdLRx/YMIV73o+rnzrDytQtr6VufZlN5pgQlMIdagGTn6bkud/ugw7/Cqr z6pGahWOw7j3polVv4cIo1pogfrNEOuQHx0kI+SnfrVV9/6xktsZ8JNaWUirl0N8yNcB htpq6fLiEsjwerrYHGr4iFcalRawL+6/+vBoc5NxVJNgvRCCT/R18mAY3k3rfEJSRzmX LOASCH0QeUW2J/pXytvAUbtgv5A+EE9wEEqx4/Nnx6YF5UDNLERMq0Z9XvOhO/GVZYez NPjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=33C4kJ6pFErF+0C7GjHFwR+K3jY3HBFbpoTYV+AMfh4=; fh=8xT39xpT08Zzu9uiqn3JBiPJ6BHR7oKM/GCWHJj86Ls=; b=VV9+NhZSrWamGC8e77lfTPgoQGI2id+DXa/sWPT4WnbSAxvQ363QD73Ct11vdZpBLu 3XxCzPJA/eZjgVympzVQeaBx6g1qNSc99P6cLCXAHmvvapctJDe9QilNv/xpSbRKgEGt fH53zhTk6BoKozrZLdqR+kiUz/2cBjoFhSFvx1zH1bsorSaPD95eSsdd/NCUwzCRMw7c AmDcNAR9osbGJLL8kDUbtkmLtUxU30PzJFdTxuLjw+784/Ja5HeICfu/V7/vYsXBsYLR 2gHuqa9rezVT4PrMZ5SIgeSi1HJeQj7dRft+VPXaBf0ROac4VJpBF7sahDG0tqSq4ePl XpFA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id d20-20020a17090ac25400b0027740943ec2si171796pjx.158.2023.10.06.13.03.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 13:03:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id CFB63801D778; Fri, 6 Oct 2023 13:03:25 -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 S233344AbjJFUDT convert rfc822-to-8bit (ORCPT + 99 others); Fri, 6 Oct 2023 16:03:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231163AbjJFUDR (ORCPT ); Fri, 6 Oct 2023 16:03:17 -0400 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D64ED83; Fri, 6 Oct 2023 13:03:14 -0700 (PDT) Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6bc9353be9bso352413a34.1; Fri, 06 Oct 2023 13:03:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696622594; x=1697227394; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nrnQlg+Ks22EwGBpOoGufahFztzvaGoZvpb10F2egwQ=; b=UTKLJa09Xmgyttk1+eZD4QFPifDz58ws5WB5dGJfQs8c5U5eSSmBtSjdumIT3msDsu 3agrzizzDZk3B9/qiAm9ipjkhvmSW7Q5yUOL/MsPMHQ1n+TWLhleQoB9Hs3o5mL2UWkn m5kRKOo4/EQIbNBxRJVyXIhasE16w0erBeKR/HPHS2gfCS9WgH6UBiZ85QkgDYRQMfc4 lRsw6iboE4YuG/Bx8yF9d3blYobU6OL2O+uv20+ckXAMAc/Mq/uNZM91ENoHF5aVO6C0 N9bGWnWrj4114ntTjSVtujmvnI99DchTqQAm2yigOb3XBeu92UVTbk9U7msD26+Q4CS6 dy6g== X-Gm-Message-State: AOJu0YxwmH6dQMNQy5C+MKYX6yEbEJ3k2nfBSdGFoXxKzo/1/CHzCJCY aNrBcDa6QCAWUA+u1DZsKVWVn4A/FxSdH8Bw9AYfacFt X-Received: by 2002:a4a:b588:0:b0:578:c2af:45b5 with SMTP id t8-20020a4ab588000000b00578c2af45b5mr8791223ooo.0.1696622594035; Fri, 06 Oct 2023 13:03:14 -0700 (PDT) MIME-Version: 1.0 References: <20230912061057.2516963-1-liaochang1@huawei.com> <20231005110255.fk736npzqsrffl2c@vireshk-i7> In-Reply-To: <20231005110255.fk736npzqsrffl2c@vireshk-i7> From: "Rafael J. Wysocki" Date: Fri, 6 Oct 2023 22:03:03 +0200 Message-ID: Subject: Re: [PATCH 1/2] cpufreq: userspace: Use fine-grained mutex in userspace governor To: Viresh Kumar , Liao Chang Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=2.6 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no 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]); Fri, 06 Oct 2023 13:03:26 -0700 (PDT) X-Spam-Level: ** On Thu, Oct 5, 2023 at 1:05 PM Viresh Kumar wrote: > > 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 Applied as 6.7 material along with the [2/2], thanks!