From patchwork Thu Sep 4 12:01:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 36699 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ig0-f198.google.com (mail-ig0-f198.google.com [209.85.213.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A359B202E4 for ; Thu, 4 Sep 2014 12:04:15 +0000 (UTC) Received: by mail-ig0-f198.google.com with SMTP id h18sf48334559igc.1 for ; Thu, 04 Sep 2014 05:04:15 -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:in-reply-to:references:in-reply-to:references :sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=cRWP4B6a0B5Aa9jqcZ9o8oHfm+1XXwpLnp67/5UFHNs=; b=cO8X7VULnBDGdkOQnoYwtKP0ktx4THRRdxC9r8+WamUyS3c4lNptFJ/dy20Y21dIAT LlfNs5/IvM3cRCLYoHTtODyAmPApmQlcysK59P9MvO0KdpI+XcoDzYYpv6RfYhR98Pme LJjVPcjeSncGV6BlTJeUbQuqQJMiz9qn4NJSwMplKAKpXL18WOxG2EP45R9vqUw3AmSh xHafW4uamkRBFLR/H4j6iz2Or5mIHg53tDsM9hQHC7RZQyhG9hkoVy+5lV3hSjU2f0Uo MlZYQgr9Nn/gc4GaISONquwekM1IKyHzdkYbGTTYUQwDe4zs5gKQMkEXFR0yK8RYHWIs HQ6w== X-Gm-Message-State: ALoCoQnOL2wTRcfauoXbPvDEQaUWLirKjdj7XVghzE+uQBqTREqDH1n5/xuZPPijBiDIsVYCdIDv X-Received: by 10.42.130.70 with SMTP id u6mr2302687ics.2.1409832255283; Thu, 04 Sep 2014 05:04:15 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.85.240 with SMTP id n103ls248439qgd.13.gmail; Thu, 04 Sep 2014 05:04:15 -0700 (PDT) X-Received: by 10.52.38.67 with SMTP id e3mr411659vdk.56.1409832255154; Thu, 04 Sep 2014 05:04:15 -0700 (PDT) Received: from mail-vc0-f176.google.com (mail-vc0-f176.google.com [209.85.220.176]) by mx.google.com with ESMTPS id cd8si5452226vcb.83.2014.09.04.05.04.15 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 04 Sep 2014 05:04:15 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.176 as permitted sender) client-ip=209.85.220.176; Received: by mail-vc0-f176.google.com with SMTP id ik5so10468763vcb.35 for ; Thu, 04 Sep 2014 05:04:15 -0700 (PDT) X-Received: by 10.52.94.108 with SMTP id db12mr2670497vdb.8.1409832255071; Thu, 04 Sep 2014 05:04:15 -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.221.45.67 with SMTP id uj3csp824535vcb; Thu, 4 Sep 2014 05:04:14 -0700 (PDT) X-Received: by 10.70.128.195 with SMTP id nq3mr8081730pdb.158.1409832254218; Thu, 04 Sep 2014 05:04:14 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id lp3si3076549pab.230.2014.09.04.05.04.07 for ; Thu, 04 Sep 2014 05:04:08 -0700 (PDT) Received-SPF: none (google.com: linux-pm-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753971AbaIDMDF (ORCPT + 15 others); Thu, 4 Sep 2014 08:03:05 -0400 Received: from mail-pa0-f53.google.com ([209.85.220.53]:55791 "EHLO mail-pa0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753926AbaIDMDD (ORCPT ); Thu, 4 Sep 2014 08:03:03 -0400 Received: by mail-pa0-f53.google.com with SMTP id fa1so20047661pad.12 for ; Thu, 04 Sep 2014 05:03:00 -0700 (PDT) X-Received: by 10.66.121.103 with SMTP id lj7mr7986975pab.80.1409832179556; Thu, 04 Sep 2014 05:02:59 -0700 (PDT) Received: from localhost ([122.167.123.172]) by mx.google.com with ESMTPSA id pn13sm1536985pdb.53.2014.09.04.05.02.57 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 04 Sep 2014 05:02:58 -0700 (PDT) From: Viresh Kumar To: sre@kernel.org, dbaryshkov@gmail.com, dwmw2@infradead.org, anton@enomsg.org Cc: linux-pm@vger.kernel.org, linaro-kernel@lists.linaro.org, Viresh Kumar , Zoran Markovic Subject: [PATCH 11/15] power-supply: Mark 'if' blocks in power_supply_changed_work() with 'likely' Date: Thu, 4 Sep 2014 17:31:32 +0530 Message-Id: X-Mailer: git-send-email 2.0.3.693.g996b0fd In-Reply-To: <2eacd908a8094887cc1796f75ae0513be8a3e36d.1409831636.git.viresh.kumar@linaro.org> References: <2eacd908a8094887cc1796f75ae0513be8a3e36d.1409831636.git.viresh.kumar@linaro.org> In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: viresh.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.176 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org 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: , The 'if' statements in power_supply_changed_work() are mostly there for taking care of races and normally they will always evaluate to true. Optimize them for fast execution with 'likely' statements. Also there is need to have better comments in code to mention about the races clearly. Get them in place. Cc: Zoran Markovic Signed-off-by: Viresh Kumar --- drivers/power/power_supply_core.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c index bcff7fd..26518c8 100644 --- a/drivers/power/power_supply_core.c +++ b/drivers/power/power_supply_core.c @@ -78,7 +78,14 @@ static void power_supply_changed_work(struct work_struct *work) dev_dbg(psy->dev, "%s\n", __func__); spin_lock_irqsave(&psy->changed_lock, flags); - if (psy->changed) { + /* + * Check 'changed' here to avoid issues due to race between + * power_supply_changed() and this routine. In worst case + * power_supply_changed() can be called again just before we take above + * lock. During the first call of this routine we will mark 'changed' as + * false and it will stay false for the next call as well. + */ + if (likely(psy->changed)) { psy->changed = false; spin_unlock_irqrestore(&psy->changed_lock, flags); class_for_each_device(power_supply_class, NULL, psy, @@ -89,12 +96,13 @@ static void power_supply_changed_work(struct work_struct *work) kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE); spin_lock_irqsave(&psy->changed_lock, flags); } + /* - * Dependent power supplies (e.g. battery) may have changed state - * as a result of this event, so poll again and hold the - * wakeup_source until all events are processed. + * Hold the wakeup_source until all events are processed. + * power_supply_changed() might have called again and have set 'changed' + * to true. */ - if (!psy->changed) + if (likely(!psy->changed)) pm_relax(psy->dev); spin_unlock_irqrestore(&psy->changed_lock, flags); }