From patchwork Mon Aug 31 03:34:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xunlei Pang X-Patchwork-Id: 52884 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f199.google.com (mail-lb0-f199.google.com [209.85.217.199]) by patches.linaro.org (Postfix) with ESMTPS id 1708E2127E for ; Mon, 31 Aug 2015 03:35:48 +0000 (UTC) Received: by lbck9 with SMTP id k9sf33707251lbc.0 for ; Sun, 30 Aug 2015 20:35:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=6x6Cl+KArMU+iphhGqar4/RkhvG54B0sUewcE83OhGE=; b=j79lYtless8wPuRep/kB1w4s7eYsksYn+4zNOF/FonmLjOoBjImKxRkeoJm9ODHgBE lOIEbPIDHS3GqhYKEjdY3MsKUIWYY6Eh1rNs/gPF4L1Bp8TkQtkgthF30hJdAeu8nGdZ uzPH3f3dJrCvNMQxOkL2I8bFvOjqFMTC+FnThcmF5QaAfJMVeAA/M9+WFbF66zEFymKH Q2bgLPea2b+cFdRRL2OcXdhdzWSs5aDNx0tGLhH/9kEOAOFevUIjnnfyajagM1SxYFSL m0/0F/3MgdesNO9twYpCV/l1iQTmb9Hspjsgv25meuj61ieETQcDAt0xqR6T1qegRK0Q fQzg== X-Gm-Message-State: ALoCoQl0ce5q0kMTeUL+Mv87U9KhcwfuXd6M02MrhuyBvsntvLDkT86vBlsLoXSSnCUnKZgjMAft X-Received: by 10.112.54.166 with SMTP id k6mr5765086lbp.0.1440992146662; Sun, 30 Aug 2015 20:35:46 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.21.100 with SMTP id u4ls412224lae.51.gmail; Sun, 30 Aug 2015 20:35:46 -0700 (PDT) X-Received: by 10.152.20.98 with SMTP id m2mr9201256lae.119.1440992146526; Sun, 30 Aug 2015 20:35:46 -0700 (PDT) Received: from mail-lb0-x22e.google.com (mail-lb0-x22e.google.com. [2a00:1450:4010:c04::22e]) by mx.google.com with ESMTPS id 6si12138152lam.37.2015.08.30.20.35.46 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Aug 2015 20:35:46 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::22e as permitted sender) client-ip=2a00:1450:4010:c04::22e; Received: by lbvd4 with SMTP id d4so17340237lbv.3 for ; Sun, 30 Aug 2015 20:35:46 -0700 (PDT) X-Received: by 10.112.235.130 with SMTP id um2mr9320186lbc.72.1440992146270; Sun, 30 Aug 2015 20:35:46 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.151.194 with SMTP id us2csp1361972lbb; Sun, 30 Aug 2015 20:35:45 -0700 (PDT) X-Received: by 10.68.223.229 with SMTP id qx5mr34543378pbc.69.1440992145056; Sun, 30 Aug 2015 20:35:45 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id kh9si21831073pad.30.2015.08.30.20.35.44; Sun, 30 Aug 2015 20:35:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751943AbbHaDfn (ORCPT + 13 others); Sun, 30 Aug 2015 23:35:43 -0400 Received: from m50-111.126.com ([123.125.50.111]:47046 "EHLO m50-111.126.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751919AbbHaDfm (ORCPT ); Sun, 30 Aug 2015 23:35:42 -0400 Received: from localhost.localdomain (unknown [210.21.223.3]) by smtp5 (Coremail) with SMTP id jtKowAAXu6g9y+NV8kKTAg--.21709S3; Mon, 31 Aug 2015 11:34:31 +0800 (CST) From: Xunlei Pang To: "Rafael J. Wysocki" , linux-kernel@vger.kernel.org Cc: Daniel Lezcano , linux-pm@vger.kernel.org, Xunlei Pang Subject: [PATCH v3] cpuidle/coupled: Add sanity check for safe_state_index Date: Mon, 31 Aug 2015 11:34:05 +0800 Message-Id: <1440992045-17464-1-git-send-email-xlpang@126.com> X-Mailer: git-send-email 1.9.1 X-CM-TRANSID: jtKowAAXu6g9y+NV8kKTAg--.21709S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxCr47Jw4xXw48Xw45JrWkZwb_yoW5CF43pF WDW34akr4DJ3yxW3yIy34j9as8ua4vqrWrGrWYka43XFn7tF1Fyan8JFy5Zr4kJry8CF12 qw1DtrnxC3W5KFUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jk8n5UUUUU= X-Originating-IP: [210.21.223.3] X-CM-SenderInfo: p0ost0bj6rjloofrz/1tbi6AVmv1UJSFwI6gABsf Sender: linux-pm-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Original-Sender: xlpang@126.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::22e as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=neutral (body hash did not verify) header.i=@126.com; dmarc=fail (p=NONE dis=NONE) header.from=126.com Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Xunlei Pang Since we are using cpuidle_driver::safe_state_index directly as the target state index, it is better to add the sanity check at the point of registering the driver. Signed-off-by: Xunlei Pang --- v2->v3: Move the code to a new cpuidle_coupled_state_verify() depending on CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED, to avoid using #ifdef in function bodies. Thanks Rafael's comments on this. drivers/cpuidle/coupled.c | 22 ++++++++++++++++++++++ drivers/cpuidle/cpuidle.h | 6 ++++++ drivers/cpuidle/driver.c | 4 ++++ 3 files changed, 32 insertions(+) diff --git a/drivers/cpuidle/coupled.c b/drivers/cpuidle/coupled.c index 1523e2d..344058f 100644 --- a/drivers/cpuidle/coupled.c +++ b/drivers/cpuidle/coupled.c @@ -187,6 +187,28 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state) } /** + * cpuidle_coupled_state_verify - check if the coupled states are correctly set. + * @drv: struct cpuidle_driver for the platform + * + * Returns 0 for valid state values, a negative error code otherwise: + * * -EINVAL if any coupled state(safe_state_index) is wrongly set. + */ +int cpuidle_coupled_state_verify(struct cpuidle_driver *drv) +{ + int i; + + for (i = drv->state_count - 1; i >= 0; i--) { + if (cpuidle_state_is_coupled(drv, i) && + (drv->safe_state_index == i || + drv->safe_state_index < 0 || + drv->safe_state_index >= drv->state_count)) + return -EINVAL; + } + + return 0; +} + +/** * cpuidle_coupled_set_ready - mark a cpu as ready * @coupled: the struct coupled that contains the current cpu */ diff --git a/drivers/cpuidle/cpuidle.h b/drivers/cpuidle/cpuidle.h index 178c5ad..f87f399 100644 --- a/drivers/cpuidle/cpuidle.h +++ b/drivers/cpuidle/cpuidle.h @@ -35,6 +35,7 @@ extern void cpuidle_remove_sysfs(struct cpuidle_device *dev); #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state); +int cpuidle_coupled_state_verify(struct cpuidle_driver *drv); int cpuidle_enter_state_coupled(struct cpuidle_device *dev, struct cpuidle_driver *drv, int next_state); int cpuidle_coupled_register_device(struct cpuidle_device *dev); @@ -46,6 +47,11 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state) return false; } +static inline int cpuidle_coupled_state_verify(struct cpuidle_driver *drv) +{ + return 0; +} + static inline int cpuidle_enter_state_coupled(struct cpuidle_device *dev, struct cpuidle_driver *drv, int next_state) { diff --git a/drivers/cpuidle/driver.c b/drivers/cpuidle/driver.c index 5db1478..389ade4 100644 --- a/drivers/cpuidle/driver.c +++ b/drivers/cpuidle/driver.c @@ -227,6 +227,10 @@ static int __cpuidle_register_driver(struct cpuidle_driver *drv) if (!drv || !drv->state_count) return -EINVAL; + ret = cpuidle_coupled_state_verify(drv); + if (ret) + return ret; + if (cpuidle_disabled()) return -ENODEV;