From patchwork Wed Feb 14 09:29:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pranav Prasad X-Patchwork-Id: 200880 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1097258dyb; Wed, 14 Feb 2024 01:42:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWkbov/Totx8a8ZINfH4bEwMIsqh7CfGmTBsVxht2E462m+6EYlWpVYBMYXu4wun0/hV/LazT8gfCVbPE4hEMRIvd544Q== X-Google-Smtp-Source: AGHT+IHLV2KW+3hYi5WzopgKSBAC+XoM0SJ575uJ6u9TrFZLcdiRuMEBQzWfEP7kk2eUotQCQdGz X-Received: by 2002:a17:906:b252:b0:a3d:623d:9e7e with SMTP id ce18-20020a170906b25200b00a3d623d9e7emr179088ejb.59.1707903727817; Wed, 14 Feb 2024 01:42:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707903727; cv=pass; d=google.com; s=arc-20160816; b=fscqhH01aECrSDNXNp+LcGf6a+2v8fDuJDJMWJuNTSUZDsf7zd90dSsHd6ekTMrEn7 WQxYYiJgcdXYqnOuzXB2+XxBg7r3wE8Vb7Vj97al66XHqlbbYwPMp/Y9vSOw/djW5n05 TT55w9Oviq3AImOmcnf/AGedbLxmNClTSKbxHUMCbFYAGCem+U5sv8g1B3Fdre9d3YEX RclpcBL04i/PEAZ6EZazk3PAPGjoOOoVBZDrBx04FR8JJIBC14x/Sz4hg5931L5Te2u8 4qOEd0nVWgmutKfv8Re9l4tfUezY8U6w446Fnwi8wba6LUQ7Q4d3w3KoSanWn9BSHZRm i1Tg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=gJBAQ5NDq5JSH9VWfJvPqqJU+MxkFsGwwmLKeuSdh/k=; fh=xVAKqcjTZjzHjh8skpAJbGJUeQyRXNX8tWBvulZ8bKQ=; b=NHxpurgSbmWgMrgeBNcdr3pnbA3SyfeMvoe7uYJTHoUZup0E/ybz4x+QKUfXxkjGik jVxbLof/xdsHwfqf9Eq0fSIvV1phbsqdUdqD3uVP5aXxzn1U6ImkPm4jCuH4jJbVcNe7 EBy90r54Ye8Ru2FBmeaAoC0ngmGiQeXqjkwV8pkZ/0h9iQgbzVpHxQZf9kd3rOxCHcRD ATBmbcNfAHIQeq/9Ov5CklzLxwR0jYIhTKuPnYFUT4O2GPRy4anVoNgtB8IDkjOnOTWH 73cj1glw3HLby9tLLJMykzBpHgXQSSDSVU4CXpCtvYVzOEFDLxi31oURxFFHY3fxAyai N9uQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EspVej7R; arc=pass (i=1 spf=pass spfdomain=flex--pranavpp.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64970-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64970-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUVRrSJnrBcXn2WC0kU88ZoMW/zGzsB77NamM/ZDzgiyDgAbrd44YsczLLFEktHaDsEenS6hk1WgDv0o5aH5MSudfw7dw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id qq10-20020a17090720ca00b00a3d216b9108si1006877ejb.32.2024.02.14.01.42.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 01:42:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64970-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EspVej7R; arc=pass (i=1 spf=pass spfdomain=flex--pranavpp.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64970-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64970-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 63DA51F2CD7D for ; Wed, 14 Feb 2024 09:31:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6832618AEE; Wed, 14 Feb 2024 09:30:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EspVej7R" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6348B12B69 for ; Wed, 14 Feb 2024 09:30:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707903018; cv=none; b=n7vOjj9ZMdnm56BKvUXMb81JteEyOu7R2CPRXNeTUAQWByZVYwWiz1I7LmJYDW7oUvf/V7hNgKl/zG+8fgVOi/pVDzAupCTthgo7NX+jBk43BwbKFn+vY93yF+KbXlT8IwEq9eMnBaw1YClpRj45DcWjpebR5IbvSqM5ikv3Dyg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707903018; c=relaxed/simple; bh=0sbOqlYgKuYMYuenntCpahB0mOEu7ATewBc2h8dzDgg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qBCDIl9DKa9Bnv0Gpk589Rh7nC5fZgM4T0BNZRIPmsk2VzSwVwW3OYUyH8M91GCntUv7pykopXBCAGV7jek51ZPlWu/UG9dW0LiDv4A9dm4KNr5CkSVbcjFBxlzJK9acHjWvjdXZAiuhaAkuWX2ShQ9Ow3z1s5M1vVUNWiCkQRM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--pranavpp.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=EspVej7R; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--pranavpp.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbf216080f5so8377250276.1 for ; Wed, 14 Feb 2024 01:30:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707903015; x=1708507815; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gJBAQ5NDq5JSH9VWfJvPqqJU+MxkFsGwwmLKeuSdh/k=; b=EspVej7RJH1F+MlajvtpRMhK9DI1TKsrVxBtepsf6AvCL7yqxqtu9XCbNqY5t9/Cf/ sJ9ZW6CpyXXefhLIrEF+GXEQ7U7zuD8wcls3W8EqAIclbUTyin4x19Z5PkqcQDvUhGdr o9HsdUQImHXV9vYnUlWWl9k8xu8DIoM0iaiL0Eft0/XWwsRYZdDSNAvZ0v5nwutzVfxg TBnj+OCvXUK5lXULY6/H2dJCgICcGPgiu2ZoDZsTIrRnIj6mjDf/DORInOZaXTLUbqZs TaNPXrv+6kueAznQJWQAVin0BS9Iz+wSpcAw1srLQCuUC6Lfg/y4mHmBq0Y8IsYdYlhA at8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707903015; x=1708507815; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gJBAQ5NDq5JSH9VWfJvPqqJU+MxkFsGwwmLKeuSdh/k=; b=TYTeffhlBKmA3frCs91wM9345zdDmIboHgHdKMO6mP9txbwmd2CPFBg9UvUv9YE9oZ zeTtJnW67MAuIzsUAMzLX2ARzHNP7N8rmwOnLEfm2xnKCWDBRKltq8uG1aPQlopS44o2 6xZKV8DSPMeqnhGKJp8iAjpTwCe5qlkdD4tGIGYFQXiJuYbGm04NZdGaPrZyLZsEhU0H OxjeNuYSB+1lR+KBtIwQot4SR0xULaJMXpdLqgcXgFSmcii/w90WzZcQfPYDfza9efBt A/DzAxk6kd1dFWKyB5DBbMOA3o5pew+d+ZUgVZt/FnHRXC5+zcUhtZDU2QA4La9We0Ig YlXg== X-Gm-Message-State: AOJu0YyiwGdJ5gKM68JtSMDqXrP8gGzqdSr5X69DhbTbKxdLy/rgtPdG uB+dqDV1hWqyesJ9SxvkgiT3WnP3MQH/FiWW6UNo6JanwLnXOy8L0vKtmEhTVdp7mmUjPu5Pc+J uSb+f761s9A== X-Received: from pranav-first.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:390b]) (user=pranavpp job=sendgmr) by 2002:a25:3619:0:b0:dc6:c2e4:5126 with SMTP id d25-20020a253619000000b00dc6c2e45126mr406705yba.12.1707903015399; Wed, 14 Feb 2024 01:30:15 -0800 (PST) Date: Wed, 14 Feb 2024 09:29:01 +0000 In-Reply-To: <20240214092902.1908443-1-pranavpp@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240214092902.1908443-1-pranavpp@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240214092902.1908443-2-pranavpp@google.com> Subject: [PATCH v3 1/2] alarmtimer: Add PM notifier to check early for imminent alarm From: Pranav Prasad To: tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org Cc: linux-kernel@vger.kernel.org, krossmo@google.com, Pranav Prasad X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790866859226760347 X-GMAIL-MSGID: 1790866859226760347 The alarmtimer driver currently fails suspend attempts when there is an alarm pending within the next 2 seconds, since the system is expected to wake up soon anyway. The entire suspend process is initiated even though the system will immediately awaken. This process includes substantial work before the suspend fails and additional work afterwards to undo the failed suspend that was attempted. Therefore on battery-powered devices that initiate suspend attempts from userspace, it may be advantageous to be able to fail the suspend earlier in the suspend flow to avoid power consumption instead of unnecessarily doing extra work. As one data point, an analysis of a subset of Android devices showed that imminent alarms account for roughly 40% of all suspend failures on average leading to unnecessary power wastage. To facilitate this, register a PM notifier in the alarmtimer subsystem that checks if an alarm is imminent during the prepare stage of kernel suspend denoted by the event PM_SUSPEND_PREPARE. If an alarm is imminent, it returns the errno code ETIME instead of EBUSY to userspace in order to make it easily diagnosable. Signed-off-by: Pranav Prasad --- kernel/time/alarmtimer.c | 100 +++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 21 deletions(-) diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index 4657cb8e8b1f..366ca3568f87 100644 --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "posix-timers.h" @@ -63,6 +64,78 @@ static struct rtc_timer rtctimer; static struct rtc_device *rtcdev; static DEFINE_SPINLOCK(rtcdev_lock); +/** + * alarmtimer_get_soonest - Finds the soonest alarm to expire among the + * alarm bases. + * @min: ptr to relative time to the soonest alarm to expire + * @expires: ptr to absolute time of the soonest alarm to expire + * @type: ptr to alarm type + * + * Returns true if soonest alarm was found, returns false if don't care. + */ +static bool alarmtimer_get_soonest(ktime_t *min, + ktime_t *expires, int *type) +{ + unsigned long flags; + int i; + + /* Find the soonest timer to expire */ + for (i = 0; i < ALARM_NUMTYPE; i++) { + struct alarm_base *base = &alarm_bases[i]; + struct timerqueue_node *next; + ktime_t delta; + + spin_lock_irqsave(&base->lock, flags); + next = timerqueue_getnext(&base->timerqueue); + spin_unlock_irqrestore(&base->lock, flags); + if (!next) + continue; + delta = ktime_sub(next->expires, base->get_ktime()); + if (*min == 0 || delta < *min) { + *expires = next->expires; + *min = delta; + *type = i; + } + } + + if (*min == 0) + return false; + + return true; +} + +static int alarmtimer_pm_callback(struct notifier_block *nb, + unsigned long mode, void *_unused) +{ + struct rtc_device *rtc; + ktime_t min, expires; + int type; + + switch (mode) { + case PM_SUSPEND_PREPARE: + rtc = alarmtimer_get_rtcdev(); + /* If we have no rtcdev, just return */ + if (!rtc) + return NOTIFY_DONE; + + /* Find the soonest timer to expire */ + if (!alarmtimer_get_soonest(&min, &expires, &type)) + return NOTIFY_DONE; + + if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { + pr_debug("Suspend abort due to imminent alarm\n"); + pm_wakeup_event(&rtc->dev, 2 * MSEC_PER_SEC); + return notifier_from_errno(-ETIME); + } + } + + return NOTIFY_DONE; +} + +static struct notifier_block alarmtimer_pm_notifier = { + .notifier_call = alarmtimer_pm_callback, +}; + /** * alarmtimer_get_rtcdev - Return selected rtcdevice * @@ -126,6 +199,7 @@ static int alarmtimer_rtc_add_device(struct device *dev) static inline void alarmtimer_rtc_timer_init(void) { rtc_timer_init(&rtctimer, NULL, NULL); + register_pm_notifier(&alarmtimer_pm_notifier); } static struct class_interface alarmtimer_rtc_interface = { @@ -241,10 +315,10 @@ EXPORT_SYMBOL_GPL(alarm_expires_remaining); static int alarmtimer_suspend(struct device *dev) { ktime_t min, now, expires; - int i, ret, type; struct rtc_device *rtc; unsigned long flags; struct rtc_time tm; + int ret, type; spin_lock_irqsave(&freezer_delta_lock, flags); min = freezer_delta; @@ -258,30 +332,14 @@ static int alarmtimer_suspend(struct device *dev) if (!rtc) return 0; - /* Find the soonest timer to expire*/ - for (i = 0; i < ALARM_NUMTYPE; i++) { - struct alarm_base *base = &alarm_bases[i]; - struct timerqueue_node *next; - ktime_t delta; - - spin_lock_irqsave(&base->lock, flags); - next = timerqueue_getnext(&base->timerqueue); - spin_unlock_irqrestore(&base->lock, flags); - if (!next) - continue; - delta = ktime_sub(next->expires, base->get_ktime()); - if (!min || (delta < min)) { - expires = next->expires; - min = delta; - type = i; - } - } - if (min == 0) + /* Find the soonest timer to expire */ + if (!alarmtimer_get_soonest(&min, &expires, &type)) return 0; if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { + pr_debug("Suspend abort due to imminent alarm\n"); pm_wakeup_event(dev, 2 * MSEC_PER_SEC); - return -EBUSY; + return -ETIME; } trace_alarmtimer_suspend(expires, type); From patchwork Wed Feb 14 09:29:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pranav Prasad X-Patchwork-Id: 200881 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1097474dyb; Wed, 14 Feb 2024 01:42:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU+X+fZOdz0QcQsNTuKXm1/xf3c5k4aBW3b/CY4Gk0iC+tnUYAuOlgp6KbBZ10AQtKiddDA2chxkrMKGhcpjMhfB12q2Q== X-Google-Smtp-Source: AGHT+IH10uokiwbHFy25sGpEhCd6VWCdmjLXM8kggH999KXPx2EHGMC+yJRzK3NvQGFAWtbGdvIP X-Received: by 2002:a05:6402:547:b0:561:5a24:65fc with SMTP id i7-20020a056402054700b005615a2465fcmr1462708edx.5.1707903759787; Wed, 14 Feb 2024 01:42:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707903759; cv=pass; d=google.com; s=arc-20160816; b=RcA+0y60+SIuk/400gN13JMIpp8w7P8XFC89F8o+3rpr37nFWE6X/Ng2y0jlRNpaUc KD0fON3FT3yaeOa+JvmgMRzAagDgKgnFRxvxAvB0GNpE/I7oqTs2R2XUblUrQdH4bhpa iSi4JBzsdtu9qL2xqlXxG2wOSZyn+bVfXbJfxJG+hXw3QaIN3bWaGiZgCCquYqmMZK+z zCpW2Fkc6HjACfVFMuOEi1uqblNYPMXwgei1VadwSrZB3u9CX26RabWNPhd1B2jow9UT nI/iVWXhquA0OEqfBmOB8jcFsws+NAGhTcUG4mi41BLd9aNUTS3Fp4HqEDy73FexQ7zs OWQA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=mKxGRkg+AXU4O8sjeZUwM8p4zzY+fxcXGuSAzHn2RVs=; fh=Om2exbWNYyw3/3Adxnz9PmSrV8SgbC+ePsmSjyTpsZY=; b=Xk5goWJyPLIdZ6635Ve/OaOnNR09fgsqlQlCwGKu1CsFugpFUe7Aioy+eT0TzVlr0i X9rwE3iNiat+mb2SajQNhfBirNvVf9Pt/Nr2iHBNbnxD0U4Z3cjMJ+HCkiqyc5UJ85Tl 1g2zbMYzLqfI5nQ1fazIe460MuloxWLTYsuOXTa9cn58qDKTH6lOv3k7i0m6wL+/cZ4I YpBd1bLW+wreliOmYB2tzBIeAX9CGLdAQq3OaqI7srCti3su1JG6eoTMuXaLDNaTrUXO 46mUkW5wTVhggq0vUz9GavMAczQEN+3MmeWAe5LEdbSqV+A04G+Ljus5dmcURh2FYJj9 bFfQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xp7RErj1; arc=pass (i=1 spf=pass spfdomain=flex--pranavpp.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64971-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64971-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVpgvCfRb07dNtcvnytNgENC39EPX2yINSZw5kLFitXv+SPI7fsz0+9CZ7oTM9aK7W4ihNsZCsR0HSdLdreeWvj14ZUHA== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id j16-20020aa7c350000000b0055fcfb658bbsi4682561edr.435.2024.02.14.01.42.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 01:42:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64971-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xp7RErj1; arc=pass (i=1 spf=pass spfdomain=flex--pranavpp.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64971-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64971-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 023EB1F2D45C for ; Wed, 14 Feb 2024 09:31:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D3E6418E14; Wed, 14 Feb 2024 09:30:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xp7RErj1" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D6EB1643E for ; Wed, 14 Feb 2024 09:30:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707903019; cv=none; b=o63Orr1HRfC0LRS1EPUCfhyydzJu3Vjyh4uwrB47KpBgFASyf39iDAUyH0Pdh7JPDKw19EKrQz8v+2g1rQKq8ElLlzlSJqorH523pWzlEBqpQI6qTgfKrBQoCqqp2lS3dswmXWA0tltxahflUkOKruDvRZoEMQXwDWUagH1tI90= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707903019; c=relaxed/simple; bh=jNtQz+owYOLm3MkN2mpaPOkCW0/vAPSlEGfcoArUtWE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kAIrGLYUAZmKwT3elAkXmAVMtHQbcQmFCKA61nW7SktLMAY/kfmfHhR98tAP5AQusBwtkMl01EC1V7Ug2Yh0FvtvfG4NepXxjgqn6pCoItvFtM9DjX3D/2oK1FV4hQNIUapQEmgVFIZdldj7i2g3K7lp1EDA1z18dY160Oh1S7c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--pranavpp.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xp7RErj1; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--pranavpp.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6077ec913f0so39993577b3.0 for ; Wed, 14 Feb 2024 01:30:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707903017; x=1708507817; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mKxGRkg+AXU4O8sjeZUwM8p4zzY+fxcXGuSAzHn2RVs=; b=xp7RErj1vC03IjNj4N4B8YwY8nU9Zonl2e/ZxSmXBjC1BMSuR7rbswNvnJ8MBE0kb+ SFR7pOiGa018wkMAP02yR6LEfI9jyym5RtcWBGd2ZQ1c3bnl/1kEgGR4Z9KhQCrAvDYN ZNnON0biCUOtBoA0PmuCB5IVQh/ZH+WlKHTollYvDySeYM9yPkzdWAZAsibVWAv0m7ZK rDD8dXNVlYkLS8gr4Exa1c2JAJ7iIn4xTVNGT6nhZ1/NxFs8E/vE/cdPMBQLnRrmpYyu QlDQW2VTqjfFQHkzLSum5RaKw8NActCsdHGfWBaZZ1YgMqkvD1HGR1WHFZh9fUPXY847 UAWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707903017; x=1708507817; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mKxGRkg+AXU4O8sjeZUwM8p4zzY+fxcXGuSAzHn2RVs=; b=tYuc7cW+8OPz+txXHjy+nJ2DCTvQ53QaBjvCK6OZ2kIjYloM95i/zRm8TmkPPAZqL0 PhnV5GQsR3xucQIuzOYtZcdMM0kbqfK45zPCMZclyRAkEAia11ik75qP8T8SaOn0jLS9 AJX0MNYFG1+CodYDYteDXJiGctBi/KdPE0+eHVgWHBUL+SqgWBwdk/XKhaTuW0PzNLz2 YbhwRZ1s1InFKBQppB/0YuHnO6VQIIKCQkhGv4CaugWGR+2KcaZcAlXy4xnm4Vpe+hBw uah0Ppz0FH5acM/5wTm3xbjSfJwbSvOlUUJIZ31Wykw9Cd4wlDTTxReOqQ3jcaXe8V55 QqBQ== X-Gm-Message-State: AOJu0YygWMjo/AxeDiFPtDY3p7KZvWmcIQKQaTRljJBxVJZ57IPC/X+M YELvZCK0EhjBmbAkhSR8HR2M8qWTQWZGzjiBhmr8NJVL1d/N3VOuPIQ7Ekzq+QkalzWaF8LtZQf NbsXUz1JuUg== X-Received: from pranav-first.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:390b]) (user=pranavpp job=sendgmr) by 2002:a05:6902:1811:b0:dc6:dfc6:4207 with SMTP id cf17-20020a056902181100b00dc6dfc64207mr455834ybb.10.1707903017135; Wed, 14 Feb 2024 01:30:17 -0800 (PST) Date: Wed, 14 Feb 2024 09:29:02 +0000 In-Reply-To: <20240214092902.1908443-1-pranavpp@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240214092902.1908443-1-pranavpp@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240214092902.1908443-3-pranavpp@google.com> Subject: [PATCH v3 2/2] alarmtimer: Create sysfs to make alarm check window configurable From: Pranav Prasad To: tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org Cc: linux-kernel@vger.kernel.org, krossmo@google.com, Pranav Prasad X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790866892489313762 X-GMAIL-MSGID: 1790866892489313762 Currently, the alarmtimer_suspend does not allow the kernel to suspend if the next alarm is within 2 seconds. Instead, a configurable value for the window to check the next pending alarm allows developers to change the value according to their specific device and use case. As an example, on Android devices if the value is changed to the short suspend threshold used in Android's SuspendControlService, it is expected to lower power consumption during kernel suspend. The value to be configured needs to be found experimentally based on the device and use case. The current value of 2 seconds will continue to be used as the default if no changes are made. If the value is configured to be very small, there may be a pending alarm that would wake the system up right after the successful kernel suspend, which would devalue the benefits of having the check. If the value is configured to be very large, the kernel suspend would be aborted regularly which would cause to lose the benefits of kernel suspension in general. Therefore, the value should not lean too much in either direction. To facilitate this, create alarmtimer sysfs to make the value of 2 seconds configurable. Signed-off-by: Pranav Prasad --- kernel/time/alarmtimer.c | 70 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index 366ca3568f87..d11f8cb945c2 100644 --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -34,6 +34,8 @@ #define CREATE_TRACE_POINTS #include +static const char alarmtimer_group_name[] = "alarmtimer"; + /** * struct alarm_base - Alarm timer bases * @lock: Lock for syncrhonized access to the base @@ -64,6 +66,56 @@ static struct rtc_timer rtctimer; static struct rtc_device *rtcdev; static DEFINE_SPINLOCK(rtcdev_lock); +/* Duration to check for soonest alarm during kernel suspend */ +static unsigned long suspend_alarm_pending_window_ms = 2 * MSEC_PER_SEC; + +static ssize_t suspend_alarm_pending_window_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", suspend_alarm_pending_window_ms); +} + +static ssize_t suspend_alarm_pending_window_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t n) +{ + unsigned long val; + + if (kstrtoul(buf, 10, &val)) + return -EINVAL; + + suspend_alarm_pending_window_ms = val; + + return n; +} + +static struct kobj_attribute suspend_alarm_pending_window = + __ATTR_RW(suspend_alarm_pending_window); + +static struct attribute *alarmtimer_attrs[] = { + &suspend_alarm_pending_window.attr, + NULL, +}; + +static const struct attribute_group alarmtimer_attr_group = { + .name = alarmtimer_group_name, + .attrs = alarmtimer_attrs, +}; + +/** + * alarmtimer_sysfs_add - Adds sysfs attributes for alarmtimer + * + * Returns 0 if successful, non-zero value for error. + */ +static int alarmtimer_sysfs_add(void) +{ + int ret = sysfs_create_group(kernel_kobj, &alarmtimer_attr_group); + + if (ret) + pr_warn("[%s] failed to create a sysfs group\n", __func__); + + return ret; +} + /** * alarmtimer_get_soonest - Finds the soonest alarm to expire among the * alarm bases. @@ -105,7 +157,7 @@ static bool alarmtimer_get_soonest(ktime_t *min, } static int alarmtimer_pm_callback(struct notifier_block *nb, - unsigned long mode, void *_unused) + unsigned long mode, void *_unused) { struct rtc_device *rtc; ktime_t min, expires; @@ -122,9 +174,11 @@ static int alarmtimer_pm_callback(struct notifier_block *nb, if (!alarmtimer_get_soonest(&min, &expires, &type)) return NOTIFY_DONE; - if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { + if (ktime_to_ns(min) < + suspend_alarm_pending_window_ms * NSEC_PER_MSEC) { pr_debug("Suspend abort due to imminent alarm\n"); - pm_wakeup_event(&rtc->dev, 2 * MSEC_PER_SEC); + pm_wakeup_event(&rtc->dev, + suspend_alarm_pending_window_ms); return notifier_from_errno(-ETIME); } } @@ -171,8 +225,11 @@ static int alarmtimer_rtc_add_device(struct device *dev) pdev = platform_device_register_data(dev, "alarmtimer", PLATFORM_DEVID_AUTO, NULL, 0); - if (!IS_ERR(pdev)) + if (!IS_ERR(pdev)) { device_init_wakeup(&pdev->dev, true); + if (alarmtimer_sysfs_add()) + pr_warn("[%s] Failed to add alarmtimer sysfs attributes\n", __func__); + } spin_lock_irqsave(&rtcdev_lock, flags); if (!IS_ERR(pdev) && !rtcdev) { @@ -336,9 +393,10 @@ static int alarmtimer_suspend(struct device *dev) if (!alarmtimer_get_soonest(&min, &expires, &type)) return 0; - if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { + if (ktime_to_ns(min) < + suspend_alarm_pending_window_ms * NSEC_PER_MSEC) { pr_debug("Suspend abort due to imminent alarm\n"); - pm_wakeup_event(dev, 2 * MSEC_PER_SEC); + pm_wakeup_event(dev, suspend_alarm_pending_window_ms); return -ETIME; }