From patchwork Thu Feb 8 19:56:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pranav Prasad X-Patchwork-Id: 198548 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp417905dyd; Thu, 8 Feb 2024 12:00:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPG7lGXimd5gbF3nJ9wkF9eLAcNf+JKseIJ9KPPPsJ99cLFyM/awDdpRmaP8+bA51p+Ocx X-Received: by 2002:a17:902:7488:b0:1d9:e189:f444 with SMTP id h8-20020a170902748800b001d9e189f444mr210169pll.45.1707422401782; Thu, 08 Feb 2024 12:00:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707422401; cv=pass; d=google.com; s=arc-20160816; b=d5R0MVVY5B58hXRP1Sf2v5vnVRW9l8e63ROHk1lQ+tMVY0NDQ7WkuTMrL48EcUOrOd osVNVNVgkCCyK7I4fbq4sxfWEMOeddc6vqL38AJUGG2991mQBXFMEtpUtfHDR0YGngFC djCSjlkrpOgWO74Ty5ArhLCO6xYkyGrfdN5QDDoMoUsErJ5IrR6Il4J56c3uNNQmZcFP FkBjUf5sywRfQKsV5H60RCB8VlxpXF7lQO5TeuzuLMYyF3O7fDqsnrjmhbPHhN2HCV/x bBeEsNrTyRF12NIE2yTOIg9XhOb8LyDGzwNAqlWoqefRihxQwU14K6m1jPuj/+P2ND2u SqXw== 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=TnQJCTWx6Sm6QL8V28Ci6hAsHnK1z9M5vvlKvn8fEaM=; fh=YIZ6ezpco15oZDCIyniKnMgt4+/8r4YhBnx7DRmP5nE=; b=0InCx3A+Pq3EaePNOAy5KGjolmzjHao7+D/RytUicboMSt23a36bGIVyLcmePLgil9 Yi+84oEM+uhwqGW/JbmVtmS1lFdYaBzdIrCcHaN198oF+gvFFZoWVHKsolx5S4e+E/op Ck7L5BiOU5ZvBbUwJ6s54t64qXrT0+lhohTJp7mxYga5flIiOwgE+r2aO16vKiGI3XSj nBtcl1lmXzF6e1k0NcbnAs9yEu/S9/R4OjNoRfeZYv6OSwvHMQlC9/JNMTV3QY44ddJ2 GyPDw3O3lO7z6I5k+nyQUqQKlhKqA6I0hVKwDD4GXosh70erg8UxdO2Cu6yU11HeKrbn 1HrQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=3tlsG2bE; 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-58640-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58640-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXiZua+FLsBP5Imk58SBvdFiYIIw40bap3hLGvHMSVtBnk1fNMYlqnNi9ksttyMVSpQLu5KTSE3u2ndg70jqwYkdsanRw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id w19-20020a170902d71300b001d453cffb92si223707ply.620.2024.02.08.12.00.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 12:00:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58640-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=3tlsG2bE; 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-58640-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58640-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 857A728E5C6 for ; Thu, 8 Feb 2024 19:57:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 790923C46B; Thu, 8 Feb 2024 19:56:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3tlsG2bE" 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 6236F3C06A for ; Thu, 8 Feb 2024 19:56:49 +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=1707422211; cv=none; b=UcrOOjbiAa5ouFBe0yu2l854HyL3fTjqf7dGo5VkOgzcnpJa5W7AX89as+HIjGAutWlAPoxPRz3IGbHMWq7tQTuTOLZH2UdLH6hLaKyoxycBIVIi/aFAcXhz4hZvYhfyV7H+9Kt56DRutEW7kyk0zvk492I87pC1vnjfLyYKmiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707422211; c=relaxed/simple; bh=hR3uFrKpWs9ZL5wZZwmaEE6pKEDdze82pyxCfRxki4c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GZQd7wvS2jVZgzKZXem+sypMQX1sy+NIHKHS0xFjqykouLkOSEjfQHXDAtt/N7CN9q1gXHBMW+KwZDchc4T2P9j7zMHOP8hd8lwPj5/l0r8yQMF2IQokSTTXvGaskV4Uo+JJARcrDpxm2i24oQhjs+KxrznFyHQggJ8C+n9VxiE= 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=3tlsG2bE; 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-dc746178515so327537276.2 for ; Thu, 08 Feb 2024 11:56:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707422208; x=1708027008; 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=TnQJCTWx6Sm6QL8V28Ci6hAsHnK1z9M5vvlKvn8fEaM=; b=3tlsG2bEDzZe6ws47zQk83wbJyBz86sgPT3z1VFIGeOj2TRs4bYRqXAdiTeMCL71LQ UnulDlDtkqE/ZKhhOGQqWGI4AI1yXpmcmQ1lHtm0SK6j8Z3Cr6gqjgXcr0HeMfx1wVam 4iA6SrQBx1rcSEt8lTPz7Gmi+dt5ee8zcUCLDyiVawKOGdyVIaLQO1EjunanOeTx++kh DTI8mVkDWAXkI/i19gYDE9o9udODxPPK5ahQSSBj6spnrPcgw+CCY0PM7IxIhDn98dSJ AJvLuJ3wPrMR+KU7WqWm5lsYwbtRj1j6hva/s5KZhx1jTpDlVuETqgrsRbcaJ5esoGdy mqaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707422208; x=1708027008; 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=TnQJCTWx6Sm6QL8V28Ci6hAsHnK1z9M5vvlKvn8fEaM=; b=exwDKjFoSy9uI7jeZZZ22X+VPWqfGZmMx2liPxoNZqZQG0KP6V8PfLt5T7nvL4rQ4g sZaJu9OOFKA4qBenM+oRxY/As2jRxLV3Hvar+Aw5Pa+9DPXsknDw0Kh2CwQ+BMPqITnQ pPsaHP3X6lnSHfag11x+cJt+IpMYlR6RdDy3g4frg8ImuhyOYBWME/dqzBFaw3GZpvpK jRdUhqn94a1pVwWGgyR1O8adZHBaKhe7yjDy28fwmW44xQkvkTp27UbzTuVNpl+NCajT k+REnR/ht17Vy++8hpoi6VttKJm5Mbw5PdeoQAbqVQovHKPPMV5y9fui5t2dofapzgcS 50JA== X-Gm-Message-State: AOJu0YzMB41S6IjqRAla5+UdzA/idG2xbnQAQkYiKBXCoI9giJqOuiMZ o80gpwxIZbB9GeM7/JudDQT8ofhnNtopzwrLEbKTXi/eOaUefWYYeMdZHdNB9WOvgM9jDp9wkXW oZ1dOJH1BTA== X-Received: from pranav-first.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:390b]) (user=pranavpp job=sendgmr) by 2002:a05:6902:1108:b0:dc7:49a9:6666 with SMTP id o8-20020a056902110800b00dc749a96666mr120837ybu.3.1707422208265; Thu, 08 Feb 2024 11:56:48 -0800 (PST) Date: Thu, 8 Feb 2024 19:56:22 +0000 In-Reply-To: <20240208195622.758765-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: <20240208195622.758765-1-pranavpp@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240208195622.758765-3-pranavpp@google.com> Subject: [PATCH v2 2/2] alarmtimer: Modify alarmtimer suspend callback to check for imminent alarm using PM notifier 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: 1790362152494289913 X-GMAIL-MSGID: 1790362152494289913 The alarmtimer driver currently fails suspend attempts when there is an alarm pending within the next suspend_check_duration_ns nanoseconds, 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 | 126 +++++++++++++++++++++++++++------------ 1 file changed, 87 insertions(+), 39 deletions(-) diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index e4b88c8dc0e1..809cfd97328d 100644 --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "posix-timers.h" @@ -115,6 +116,87 @@ static int alarmtimer_sysfs_add(void) return ret; } +/** + * alarmtimer_get_soonest - Finds the soonest alarm to expire among the + * alarm bases. + * @rtc: ptr to rtc_device struct + * @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 1 if soonest alarm was found, returns 0 if don't care. + */ +static int alarmtimer_get_soonest(struct rtc_device *rtc, ktime_t *min, + ktime_t *expires, int *type) +{ + int i; + unsigned long flags; + + spin_lock_irqsave(&freezer_delta_lock, flags); + *min = freezer_delta; + *expires = freezer_expires; + *type = freezer_alarmtype; + freezer_delta = 0; + spin_unlock_irqrestore(&freezer_delta_lock, flags); + + rtc = alarmtimer_get_rtcdev(); + /* If we have no rtcdev, just return */ + 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) + return 0; + + return 1; +} + +static int alarmtimer_pm_callback(struct notifier_block *nb, + unsigned long mode, void *_unused) +{ + ktime_t min, expires; + struct rtc_device *rtc = NULL; + int type; + + switch (mode) { + case PM_SUSPEND_PREPARE: + /* Find the soonest timer to expire */ + if (!alarmtimer_get_soonest(rtc, &min, &expires, &type)) + return NOTIFY_DONE; + + if (ktime_to_ns(min) < + suspend_check_duration_ms * NSEC_PER_MSEC) { + pr_warn("[%s] Suspend abort due to imminent alarm\n", __func__); + pm_wakeup_event(&rtc->dev, suspend_check_duration_ms); + 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 * @@ -181,6 +263,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 = { @@ -296,49 +379,14 @@ 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_device *rtc = NULL; struct rtc_time tm; + int ret, type; - spin_lock_irqsave(&freezer_delta_lock, flags); - min = freezer_delta; - expires = freezer_expires; - type = freezer_alarmtype; - freezer_delta = 0; - spin_unlock_irqrestore(&freezer_delta_lock, flags); - - rtc = alarmtimer_get_rtcdev(); - /* If we have no rtcdev, just return */ - 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(rtc, &min, &expires, &type)) return 0; - if (ktime_to_ns(min) < suspend_check_duration_ms * NSEC_PER_MSEC) { - pm_wakeup_event(dev, suspend_check_duration_ms); - return -EBUSY; - } - trace_alarmtimer_suspend(expires, type); /* Setup an rtc timer to fire that far in the future */