Received: by 2002:a05:7412:d024:b0:f9:90c9:de9f with SMTP id bd36csp148984rdb; Wed, 20 Dec 2023 08:28:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IEglYcNkEVApZGP4IjILPneYn3XD2rHnHa6wgzlHSp2B8hTTWpHMj6gUdjMN4k0OyK08ib6 X-Received: by 2002:a05:6e02:18cb:b0:35f:c234:3b6 with SMTP id s11-20020a056e0218cb00b0035fc23403b6mr3286023ilu.115.1703089727474; Wed, 20 Dec 2023 08:28:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703089727; cv=none; d=google.com; s=arc-20160816; b=oUP1OJicto88mCpRfzqYAIICT8zEcqrp+CRnU47nXMDVOIODsnEENFdzgI7Ja7hZbI Isf4kP+mS7/lYIkC8q1fODSY1Xx24EcD9tk7E+Dqa1buaQPIAI3ya9PC2LfQ5m9h3Jw3 n7YCSZbxujx2YPHzuTtSLK8oHjbHHtsofQgeUjUVTgJKvuXASBDszAAowMaG6JAqXCyG Cx32ksBvy6+w0SYT0qsQANNrJ6CuA6jZs3IJGthHmBVdkAIMrPliIUP3Np03JF2dtRKG XYnvXpVgX78P/G6fazgtCnzyluyaN/dCdaF/k+hH5/+bOGrcNTuM0lMrHznK0COy07TX GGfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=G1S3Jnc/gQwDmqKnp3oQ0DUkVseZ3SgCqNavboQWXeY=; fh=s+3ccwKL/Xc2i8uH9tAeSOb9IGpvLhPrVmnBNs/vSzQ=; b=VnVUJMsQjNHHwE044rRGxem+Z/MulxY31t8lu4lRQ1ZoU/U0jHikXQNXmXr590v1lZ C10gWkvfQaeGxaazcwm1udNsmLlBDjz//zSYNrvFef41xKbbyfKwlyfY8ySIcZQmEwPL B3fDfulcrnJ1pejMWIIJRTxt2Q+EHPg9tTt+Dv8Fp6TtaFteNvZwIyFAIQfSq+f6LjPX aZfQc3LoSL5QwwThfUF3VwdWCPi2qG4T/LZU1L7Y2LMg8MQnm2sSEBW+q5ZpAl8Mr0jz oo90q+3L/kEEpcs/MiL4iDsC/lC2DNW3qIC6WHArGvIemdUDFrtu8r+o6RgEVB1fWooq o3Vw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-7288-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7288-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c31-20020a631c5f000000b0058556a636dbsi20942777pgm.434.2023.12.20.08.28.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 08:28:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7288-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-7288-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7288-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 70994280F38 for ; Wed, 20 Dec 2023 16:26:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0494647A7D; Wed, 20 Dec 2023 16:22:55 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F85946432; Wed, 20 Dec 2023 16:22:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 898C31FB; Wed, 20 Dec 2023 08:23:37 -0800 (PST) Received: from [10.57.82.217] (unknown [10.57.82.217]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C19F93F64C; Wed, 20 Dec 2023 08:22:51 -0800 (PST) Message-ID: <65b879ad-aff2-4f7e-bad1-ac85dc5fd146@arm.com> Date: Wed, 20 Dec 2023 16:23:59 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 6/8] thermal/sysfs: Update instance->weight under tz lock Content-Language: en-US To: "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, daniel.lezcano@linaro.org, linux-pm@vger.kernel.org, rui.zhang@intel.com References: <20231212134844.1213381-1-lukasz.luba@arm.com> <20231212134844.1213381-7-lukasz.luba@arm.com> From: Lukasz Luba In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/20/23 14:53, Rafael J. Wysocki wrote: > On Tue, Dec 12, 2023 at 2:48 PM Lukasz Luba wrote: >> >> The user-space can change thermal instance weight value while the >> throtte() callback is running for a governor. > > This needs to be more precise IMV. Something like > > "User space can change the weight of a thermal instance via sysfs > while the .throttle() callback is running for a governor, because > weight_store() does not use the zone lock." Agree, I'll use it. > >> The IPA governor uses the >> 'weight' value for power calculation and also keeps it in 'total_weight'. >> Therefore, the 'weight' value must not change during the throttle() >> callback. Use 'tz->lock' mutex which also guards the throttle() to make >> the update value safe. > > "The IPA governor uses instance weight values for power calculations > and caches the sum of them as total_weight, so it gets confused when > one of them changes while its .throttle() callback is running. > > To prevent that from happening, use thermal zone locking in weight_store()." Agree, thanks. > >> >> Signed-off-by: Lukasz Luba >> --- >> drivers/thermal/thermal_sysfs.c | 6 ++++++ >> 1 file changed, 6 insertions(+) >> >> diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c >> index eef40d4f3063..df85df7d4a88 100644 >> --- a/drivers/thermal/thermal_sysfs.c >> +++ b/drivers/thermal/thermal_sysfs.c >> @@ -961,6 +961,7 @@ ssize_t weight_store(struct device *dev, struct device_attribute *attr, >> const char *buf, size_t count) >> { >> struct thermal_instance *instance; >> + struct thermal_zone_device *tz; > > IMO the local var doesn't help much. You may as well use > instance->tz->lock directly below. OK, I'll change that. > >> int ret, weight; >> >> ret = kstrtoint(buf, 0, &weight); >> @@ -968,7 +969,12 @@ ssize_t weight_store(struct device *dev, struct device_attribute *attr, >> return ret; >> >> instance = container_of(attr, struct thermal_instance, weight_attr); >> + tz = instance->tz; >> + >> + /* Don't race with governors using the 'weight' value */ >> + mutex_lock(&tz->lock); >> instance->weight = weight; >> + mutex_unlock(&tz->lock); >> >> return count; >> } >> --