From patchwork Wed Feb 7 10:06:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pranav Prasad X-Patchwork-Id: 197833 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2124696dyb; Wed, 7 Feb 2024 02:07:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IE9ktp9VpAkQ8HnWGd5aUJnsHkEb79Cxcb1/WY6WRIoESj0SqMl0qOF8LRIkaa+QEDfPFqQ X-Received: by 2002:a17:902:d4c5:b0:1d9:859f:b505 with SMTP id o5-20020a170902d4c500b001d9859fb505mr5322347plg.21.1707300441487; Wed, 07 Feb 2024 02:07:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707300441; cv=pass; d=google.com; s=arc-20160816; b=F/3fL3MhCvlgpbK1gw42E2B7hnpviQGKBD6B9+LtnHdJLR+JkfJoWCfDkADnnhsIl4 jruQ8MmGPYicnShcsVtYcfz31Nh5t4WrrKst+upOEOZF8ayH0itsZqHSTPdCp7a6mXoI p7RPlYlo7qyqV9sXsLPEUgpbeJ/ZKwHcdlVhZGZZf0A97s954MDRNdk+Ky3hYcjm8L9Y tEWk2koFY+3RDkI4J1NdpYzAy4PDMnfP0bXZ0YbyMTB68Qz2tCcYuhhm2UpXxtVNkS+v WSiZU3VKbVk91Asx9LKgLpqqdlSVXXwz3XEVHAz3ZKyebiMx+ckw+yu2qiTbKIkfRl/8 gZuw== 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=hibLsU9VPwJifCjSyfxgS+Y2Xj9FySfn0enbUMWfKnM=; fh=ZZDPakfvTjyXv3LLi24SAxJT5FSqCpmVE3NWKn5MWFg=; b=kG4LjAOsANG2XhRyCYzMQsCZpcM1byhplN2ZXymBNiTLF/4FR+1rRiLDUd4gLg7Oxb 3Q6EFpw7BB3tH60FcWn5u1nf+wkmOR0ZyaVeFlqfiFhFk6LmXMMjQ/mYo/RiWnc7eY8G KgWwiys6lxgaeC4KjF9jPg1bxamI6aDmRVwKfQfmdy/+SYvKqSoebn9m/U7jvvq2udlh CfDeuc1TpPV1oqmgj1E0G9psGX9NOnn2XBeu8RZqfRSRxep1TLN8TgbchiBvTNJ3D52a MVxw+CQ6OaxIMEZ37aS1hPqbYDhLYtXsT3CuHCa6N0wM1FqYB/Q56DHeZNsEEcyS1aPR +0Ig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kezZJ3r3; 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-56281-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56281-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVpRRRqg7Sv4et4y7mcp9Evj5QbKvJjTiaa9eIZNpDB/OH65oBYaleaTJY6nggvuFnkjDwXBikhrfZeieI0MEm/9DDYnw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id p15-20020a170902e74f00b001d95ef2a557si1397824plf.170.2024.02.07.02.07.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 02:07:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56281-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=kezZJ3r3; 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-56281-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56281-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 19943286176 for ; Wed, 7 Feb 2024 10:07:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C5CE428DAB; Wed, 7 Feb 2024 10:06:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kezZJ3r3" Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 A691A25567 for ; Wed, 7 Feb 2024 10:06:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707300400; cv=none; b=RdxmrI8K3LaJZtCr9Y8rAYfQk8BMszQAIK6BzPJVEUBRQNpx1k9REIohNJoVUuV6S23f57NwqUmmwR/+6aEPYSkfLewSqZZe5P77AT2ywTCPTE8+Lsv0NGCDxYSn6JXfY67DgPxJox2vC+xxCYFAmqzs13iZ6fUMHacTQtWsgrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707300400; c=relaxed/simple; bh=9L1TGEU45Ux6IE1/28h+r8bLolg1IHUK0dCIu0KT0ik=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=L82nJYtihlAcMWaeJfMyHmfcG77zGaR7nN8SaKYsJZun5mX6DUAjd+815jx4DPbdtrXlwaUasQ9cdM0iloMjrTpMXBXo2xyNHz7wr092fMeauWavtYB4NCkEiaOdgTF4ZQWhgyTVTKfxPmaReO0L38qnd1aF3lixcKCFEEb2j64= 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=kezZJ3r3; arc=none smtp.client-ip=209.85.210.201 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-pf1-f201.google.com with SMTP id d2e1a72fcca58-6e05118feabso511725b3a.1 for ; Wed, 07 Feb 2024 02:06:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707300398; x=1707905198; 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=hibLsU9VPwJifCjSyfxgS+Y2Xj9FySfn0enbUMWfKnM=; b=kezZJ3r3EhXxvyw9YTGY+R3TpSjpEjguPToaXVL4I6ffe+TeteVPTpaxio1Xku3eUB TR7LhdgNCKIpFnW8CsJUp1BHPTk0V1jwFyXfmAJazDcxJcLDbm2FYUGj76MoZveBWtL8 ZHqsHPtQ5nVaoj8yxlhlRV7S5C8PifpW9Ymt/TraC7lmAKWjCzPlCVoMeMmAS6HBKE1B X9bvX4hWc7T0UGlfe04VZ5/Gqd4o3OJ6GRKQCfpZWocy2vZnXgR8fMyRJuU3H2rqU/OW PMj+M/m2FHCMs0IGJcwMbZ5Clr2WHzpKArRP82wE/V9ASDedWYjvbvJOIGmvkMLYVzvo gUGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707300398; x=1707905198; 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=hibLsU9VPwJifCjSyfxgS+Y2Xj9FySfn0enbUMWfKnM=; b=o7zYLmS1VKWZlyHpo6gfg6eH/UYyU30n+dpoZNTctFkR/k5snYX+91VGeopHAe5FIA /99BpnGpFtWColV3UUXdrmEy/NkzriUJndJJuMk5DF/0jbWfp2Ki5DtrQjkE4WjUW/9l vxEKi6JOOI63OcutIhfApdY48WhcefiE5LjXiI4B10UWm6IGxBFMzHtmnMNFOH37tH02 UHC+p68j8GVtB5wiw3k2RcRUd/y5Yy1fC2oHi2PewenPrbrixLrADaLPQ4bDoNkXqC9m 9FHy7+/WyTcWdyA1/HjgqvWzaFy4xlDYFWqn0P1qp8e5lGU9aavOGSdrks+HdHQ5t4b1 d/rA== X-Gm-Message-State: AOJu0YxP35qQtIrXtC/ye4B0UInRgzwo5opXpLnDo1bNQ19bsRP/kdEZ AJcthlN/S4AvONgI7Hn/WJ1DQGLhinmfIv3piLQsUqbsLVk3OaaPyniDR/E3tNbgDCbLtW3LDKE 5AFtbTSmMgA== X-Received: from pranav-first.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:390b]) (user=pranavpp job=sendgmr) by 2002:a05:6a00:228a:b0:6e0:2d2c:d2ec with SMTP id f10-20020a056a00228a00b006e02d2cd2ecmr82970pfe.0.1707300397931; Wed, 07 Feb 2024 02:06:37 -0800 (PST) Date: Wed, 7 Feb 2024 10:06:19 +0000 In-Reply-To: <20240207100619.3947442-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: <20240207100619.3947442-1-pranavpp@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240207100619.3947442-3-pranavpp@google.com> Subject: [PATCH 2/2] alarmtimer: Modify alarmtimer suspend callback to From: Pranav Prasad To: tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org Cc: linux-kernel@vger.kernel.org, Pranav Prasad , Kelly Rossmoyer X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790234267547513285 X-GMAIL-MSGID: 1790234267547513285 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 Signed-off-by: Kelly Rossmoyer --- kernel/time/alarmtimer.c | 121 ++++++++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 33 deletions(-) diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index e5d2e560b4c1..229de937c266 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_init_soonest - Initializes parameters to find soonest alarm. + * @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 + * + */ +static void alarmtimer_init_soonest(ktime_t *min, ktime_t *expires, int *type) +{ + 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); +} + +/** + * 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 + * + */ +static void alarmtimer_get_soonest(ktime_t *min, ktime_t *expires, int *type) +{ + int i; + unsigned long flags; + + /* 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; + } + } +} + +static int alarmtimer_pm_callback(struct notifier_block *nb, + unsigned long mode, void *_unused) +{ + ktime_t min, expires; + int type; + + switch (mode) { + case PM_SUSPEND_PREPARE: + /* Initialize parameters to find soonest timer */ + alarmtimer_init_soonest(&min, &expires, &type); + + /* Find the soonest timer to expire */ + alarmtimer_get_soonest(&min, &expires, &type); + + if (min == 0) + return NOTIFY_DONE; + + if (ktime_to_ns(min) < suspend_check_duration_ns) { + pr_warn("[%s] Suspend abort due to imminent alarm\n", __func__); + 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,48 +379,20 @@ EXPORT_SYMBOL_GPL(alarm_expires_remaining); static int alarmtimer_suspend(struct device *dev) { ktime_t min, now, expires; - int i, ret, type; + int ret, type; struct rtc_device *rtc; - unsigned long flags; struct rtc_time tm; - 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); + /* Initialize parameters to find soonest timer */ + alarmtimer_init_soonest(&min, &expires, &type); 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; - - if (ktime_to_ns(min) < suspend_check_duration_ns) { - pm_wakeup_event(dev, suspend_check_duration_ns/NSEC_PER_MSEC); - return -EBUSY; - } + /* Find the soonest timer to expire */ + alarmtimer_get_soonest(&min, &expires, &type); trace_alarmtimer_suspend(expires, type);