From patchwork Wed Feb 7 10:06:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pranav Prasad X-Patchwork-Id: 197832 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2124528dyb; Wed, 7 Feb 2024 02:07:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGJnlK171fL4O2s6bJIK2wRkyoOBF3RlSoE6Zq3c6cAyT3eNtLEYkuBOSstaF7kgGbqZeMS X-Received: by 2002:a05:620a:389d:b0:785:60bb:984 with SMTP id qp29-20020a05620a389d00b0078560bb0984mr4984171qkn.41.1707300420165; Wed, 07 Feb 2024 02:07:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707300420; cv=pass; d=google.com; s=arc-20160816; b=doxoX5gDwFOrCfU+lkShpa7mZ6mw+VGW2wdo3pixUbmFSMoat9Pu4mBh97dMzIf/h3 HG0Q7rn4C//N6YFBatrpNxwTar4pzHECXB0iIXA5i5NZ78RopyJxtnX4uSDCxBihu88L aHajgrNd7pTthWwBNGvprk5tk8SLAgHns9hnx4D30V3010ufdpzUU6QKXAJU5T1henFT SXXpf29RL/IPX+fD3fOhbhBfiqyaDDt3h5PFAhF9ofjPCJnRmYBai0DRmyJ5+gZn6OeI XvMh7ETsEUGnI7MyKl7Jh4f+K3fNYUDEqshwz99Cz8Wl/crUQpFccnGLDwXwCPWa8Vnx g3eA== 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=JHyxNaPCKju0suiGcb7YkRX+LpmSfcg8/gQ3kq3ldf8=; fh=PFG1/B0E6j2tiN5RUycm0jrKgimJci05ejE9ihIIUWk=; b=CVGjrjrwv9aUPXIwIC3JbUrBtx4l42c11vY6bZ12coLhHTAJQuplICPWJZxq43RbIs 8/Z1uYT0SIMKl10SIcoTf570aTv2s6rH2sX8AAwC8l86SShQuJk0Lj0Nf+jnxQPy9dOY /l4/6Y9X5R+o9810bJFsYREEEh2/brWr93fKBlgA54DD51PAs14nxVEGe7WU2kcx8QYe zFyYSxu3klKU7LPly48NF8xthr8s+3bz2p54sN9O0FYa4VXVaYhUmr5RNgIpsG3K3wBl IIXuJaAPoqWIMXmmUyE2X+M7QH/4z2/6g/VNrlEjlxN+jSUC7i3KMk4ty8LrIyS0zOtI fjGA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=IeMtiH1Z; 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-56279-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56279-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCWTlYz4TnwEsgSGY5GNnXs1oaYBLKsuWCoMR/LK+BMWBEixzIO0QfJyNGJ9WOBVc8rEd4Aih1QyTg0/b0Cj5aN1EqPnHQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id l21-20020a05620a211500b007853f72dd4fsi783485qkl.286.2024.02.07.02.07.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 02:07:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56279-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=IeMtiH1Z; 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-56279-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56279-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id D4DF01C21A46 for ; Wed, 7 Feb 2024 10:06:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8C0DE25566; Wed, 7 Feb 2024 10:06:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IeMtiH1Z" Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 E6F5D25602 for ; Wed, 7 Feb 2024 10:06:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707300395; cv=none; b=YK5U2I5NciLs8OX/DW0H88QL/bDnpL4cKRBW4qNGUVOiMnidisXBOWmUlfp8M1MvzWg8Y8uyaW/cyMgB2g15FZ2bdQAwIpVzJeqq/AD3yQEK4DISx+/buBU8uUPkLW9o4UBS60K1kOEw2jYBxS3ipErkN+68Y1L/8g806c/T1ck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707300395; c=relaxed/simple; bh=e+QYfe3EBAnFeI6C06KSlz6hgCpRzI0/cX5GEtPsNfg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fgf40/LemyrFGbzrv7xXxRSUoFDw//E7UIUu9HdiAnZnEUyoKwLJBrtb0FQnjPN9w2rE5nUQ5FzRY9M6VO3RjE/HlpyPEsEuFnUtbpR/QNi4GUHPf0DuV2vaZAwvN67PR7OnhzSSXzZN1nVWJmV/MtKdWOVGJvZIa0TWF0i0unQ= 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=IeMtiH1Z; arc=none smtp.client-ip=209.85.216.73 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-pj1-f73.google.com with SMTP id 98e67ed59e1d1-296e09c305eso488152a91.1 for ; Wed, 07 Feb 2024 02:06:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707300393; x=1707905193; 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=JHyxNaPCKju0suiGcb7YkRX+LpmSfcg8/gQ3kq3ldf8=; b=IeMtiH1ZN91C6+yvDyPuFLmRl+pabFaac/Lhm0oSRLDv3V3AbDbTIYzIDZAcgRIXo/ rJVDYtBKLShXpVPFWMI51ur24FWRSvXZ2DMDJhgr3KxvJVJAloASj6NW8wl9hrYS+rUK ZzUglr3SExuyb+/5Fc9hAvfY+08Gxf4i3JpwAuGLUoBRz5F0caabMxP6AVkxM88MNSz/ SmbZAvjklHC/juUS/iCXrlxmc2X48GXupNMYVB5mxPPZUbIZR4LQDw/9k5q4hoGZe9SC WpBYWMBMRq/EZTBwCQgw1MlEGfIBSF2WnZjTZHj0xc2uI0OOEI12CpO0T5NFVLzhOkYQ qLHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707300393; x=1707905193; 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=JHyxNaPCKju0suiGcb7YkRX+LpmSfcg8/gQ3kq3ldf8=; b=jeilbY+gisMopPPsShT59sNWbHjBs+4mvi3WPExDrNhnP//nBzgOhAcbEUD6zgY8g/ ui0fQue0qwu4SfPO25H3g93/+7yssWuwdkssBl/4HHkLVX0a5/KEmevJArNiC37UnIH5 SIFdShHn9AbuGhtaYxPr0ZEIKhcYSllQlq9u3nyyYqx5FmW/FTDpw1/t99Y4rpj5sZg+ owHkM+zAO6ii9yR1Pz2EATkmUfcBltTvpM+vPlk5v0CCBpbITSL2XZLvzGtg0MOmyOSm jxCN7YYP8YUwKBdymk4DRB+BZIXMfzMcY1VXZbB+UMQjejghWNDhgsPgaYrGoS0vKvCI 0QfA== X-Gm-Message-State: AOJu0YypjgjNLZkzXK2K6UUoM5Rl8kOlv3MdpbGKAp3JQXZy2pQ00zY/ k/2ZWHGizKSLEe1ocd5x6uNEaRzMsEv4rMjLRlt6Kto1pfV+pmaVBP2ClwkVIpa4zyAomp+zhqU JK+gL99yxvw== X-Received: from pranav-first.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:390b]) (user=pranavpp job=sendgmr) by 2002:a17:90a:cc3:b0:296:dd68:e4d4 with SMTP id 3-20020a17090a0cc300b00296dd68e4d4mr36447pjt.3.1707300393162; Wed, 07 Feb 2024 02:06:33 -0800 (PST) Date: Wed, 7 Feb 2024 10:06:18 +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-2-pranavpp@google.com> Subject: [PATCH 1/2] alarmtimer: Create alarmtimer sysfs to make duration of kernel suspend check configurable 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: 1790234245589448087 X-GMAIL-MSGID: 1790234245589448087 Currently, the alarmtimer_suspend does not allow the kernel to suspend if the next alarm is within 2 seconds. Create alarmtimer sysfs to make the value of 2 seconds configurable. This allows flexibility to provide a different value based on the type of device running the Linux kernel. As a data point, about 40% of kernel suspend failures in a subset of Android devices were due to this check. A differently configured value can avoid these suspend failures which performs a lot of additional work affecting the power consumption of these Android devices. Signed-off-by: Pranav Prasad Signed-off-by: Kelly Rossmoyer --- kernel/time/alarmtimer.c | 61 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index 4657cb8e8b1f..e5d2e560b4c1 100644 --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -33,6 +33,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 @@ -63,6 +65,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 u64 suspend_check_duration_ns = 2 * NSEC_PER_SEC; + +static ssize_t suspend_check_duration_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%llu\n", suspend_check_duration_ns); +} + +static ssize_t suspend_check_duration_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t n) +{ + u64 val; + + if (kstrtou64(buf, 10, &val)) + return -EINVAL; + + suspend_check_duration_ns = val; + + return n; +} + +static struct kobj_attribute suspend_check_duration = + __ATTR_RW(suspend_check_duration); + +static struct attribute *alarmtimer_attrs[] = { + &suspend_check_duration.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_rtcdev - Return selected rtcdevice * @@ -98,8 +150,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) { @@ -279,8 +334,8 @@ static int alarmtimer_suspend(struct device *dev) if (min == 0) return 0; - if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { - pm_wakeup_event(dev, 2 * MSEC_PER_SEC); + if (ktime_to_ns(min) < suspend_check_duration_ns) { + pm_wakeup_event(dev, suspend_check_duration_ns/NSEC_PER_MSEC); return -EBUSY; } 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);