From patchwork Thu Feb 8 19:56:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pranav Prasad X-Patchwork-Id: 198559 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp435845dyd; Thu, 8 Feb 2024 12:32:28 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVtHyR0rcL1l2MExzpWrDmBvgrmvVbuUkH4WDigMGd7zbjd3Y2uvjCyPvsnYt/OVtidRIybqX+wUdrac2Bxx57TTVG9UQ== X-Google-Smtp-Source: AGHT+IHiu7UMKHbC0NrmrvOHimUiRxCwIMGlWg1TEQqJKoUIW063Cm3xbzteQp61gDo3UfpHnB55 X-Received: by 2002:a17:90b:4a44:b0:296:3ca5:df7b with SMTP id lb4-20020a17090b4a4400b002963ca5df7bmr362131pjb.40.1707424348300; Thu, 08 Feb 2024 12:32:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707424348; cv=pass; d=google.com; s=arc-20160816; b=f2++3xLTv0lSj+BFcnZvUEv/PTB6Kbuxi0LOynuyt/Da7bi1OIOvqSs81OpAghGE5r lMn7ZeHB21YASVB1qBSfKmJCDRrDs9irNTk7Ighwq0O/Wy1sI7r8cURIDkkNvExhQEOt w+pyuz0bgAKrXl6r0cGiJYiLmsWBDyAXivZAbvuaMAiKkgErax7qpTPHnRqNDDm+BFIq q6j+m9cWA4IGEv4JEDa5t6vgenLzzyTnMrFe98II9l+0Jv5UAxR/5CIocf/tnsPRniYo g0ZMC2rwWBWtQJ+a/ZpibJxl/QbBuXXrJM+YmumTQ3y/eVKagoxZFMldxksuld2Jpl1T CPiQ== 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=zaRZ3wf2NZjQHPcjuUdcp3FA76JczLwqBxReMfEbU8g=; fh=5cYNNo499O77dSh93b4+RWSWu1vFF1aNQWUYiJxqhz0=; b=WeGyH9FqZVMZc74RF62KTEiGTawtfMRPoE+irpSwZA19jkPXNjNvLGNWh7HeBnii7Z wmNPm8LhfEUp10YnJZg7YBke9/l8d4kxtkCdb/E8BFj2Aq04CR4weoLX4KOkk6AxaDlj 44oh2BCwQrN/BhE9uLtfJHlq9AGXz8/La/nOWc25zYAYDuLxSEVkHWrn7Zjbu/ol3OGD I5HLS1nTaiUjJsPDfTsfE1zs3Hv5nnaksw45EqL+h3MAd6yiP3RhrMWQ5RVgrGps97hX XO4TLKy1koQBuKGIrSObQkmlJr8loBWG4ucdn6id0E7UP5AjVH0DYYyRVGkDiknZGqrJ PlMQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lybLQk+Y; 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-58638-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58638-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCV6IylBrpGoH09jgLhqweD1nZviVjZ5lp5nf1GMLYWcYYkOjkE2OMKEUSZC+2gCM3+exX9tEI2Cq4+S7Q/sTGWkIJ1Rog== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id e24-20020a17090a9a9800b00296c8a3a5c7si289362pjp.39.2024.02.08.12.32.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 12:32:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58638-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lybLQk+Y; 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-58638-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58638-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 72CC6B28808 for ; Thu, 8 Feb 2024 19:57:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3BCE538F88; Thu, 8 Feb 2024 19:56:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lybLQk+Y" 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 C31CA383B0 for ; Thu, 8 Feb 2024 19:56:39 +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=1707422201; cv=none; b=EwRXhgUPUEnWN0ljpVFde87GWsodVTbTl592iURP/TV1dvsBsOFIL+VKXM5bjT13KQ2ko60a+an3aWrizQe4GhQVMV0B/5W878GDuzrpL743mnydLA7rH0C+LjrNvXy9VpV/7I0j/ghG5e8nkymUC9l7SmdDXVsz7Y5YpfsWFLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707422201; c=relaxed/simple; bh=YuOGVyhQ0G/NAULkmFSuiReHREPJ7BzhTsSx1dgiFmI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=J9Fw+dcbGruiBsSjrXFVXHdAa693hIzh+p8h9XamE3vGzKbfHhwRK8f6v5DGwZTS4BY2wPH/2u8AUSWcgZkzsH1QIcAIyNMBcdvIgf+FL7FY48h3v4WZJVrQaM1ZiZpqycNEvA0Fz3LmgsNTaSGQOL/W0jOTImM8KUxn4A4mhmE= 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=lybLQk+Y; 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-6047a047f58so4738187b3.3 for ; Thu, 08 Feb 2024 11:56:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707422198; x=1708026998; 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=zaRZ3wf2NZjQHPcjuUdcp3FA76JczLwqBxReMfEbU8g=; b=lybLQk+YXOmdWEd308D+V3i9jp2QP3QsNjkjSX9FJrbU69f63Zk2Kiu7/FdJYTX+2B DgvCR0CfiiMaYM0fu5e3/pMP+NUSMnGKQGCKcCKMcyqyQjNynXuZ2gm4Hd+ZOn3IAiEW KZkjAV4/SwFsW2IUZgbnX2uzDmHWDMvSJOa8430EGPUgAkc7/liTzgV66bAOsKfV9k6l Fw57rhYXtDXvswcK/f3ENzK4Uk5UrG78/iJn7tGVFUf4l+siZ449WuEXwxZBaSWzquMc L6PRj9xj+EMMjEN/l9kEYYX9o7MmsHCWK3gNtjIumCy7vGPtAosVboQccwklYJ3iMro0 rAiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707422198; x=1708026998; 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=zaRZ3wf2NZjQHPcjuUdcp3FA76JczLwqBxReMfEbU8g=; b=pwF+m0SATDSl3CDzIvNMC8ZQs+ar2xxcoNHMXHxwOiOGaSgF2lLaTtENrVAWdhAlUW J+OMg42vTxnOnsqj4rfxQelaU/dwL7Mdkp7egNMolZOsNJIWR/Nt2yb3ic1p0GRNdty7 Zb7CxEDSc6VOlLovjrvqZIIRtjjpWaYn2Jhz5LKFCuF0iHeBij4gGh/3B0fdPfeGr5Pl +G9n3D1LbapmFGoxbofU8WuBaeFTNcMs7L7eqjZrVgu5QXAJSBAN3kN44gYmuIgJI911 vo9wMROl3mFSrTY1FeXeJ3VUOdwDDzOsUYbCraW2xHg+PULjOePIstrraus1e0ijJin/ 1KHA== X-Gm-Message-State: AOJu0YxN8jTPXhok6hz203IYWYFddVYvItDRy7LHrf2GTO39uusgyh8S Yt7XAfYdK2eLkiJbVXhqJumfQ+FbgRBtrLFueVXGxI7IlJpauuQueK+k429xbbXeVDnIxC0be40 5kAU3au0YEQ== X-Received: from pranav-first.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:390b]) (user=pranavpp job=sendgmr) by 2002:a0d:d583:0:b0:604:5ca:5ab2 with SMTP id x125-20020a0dd583000000b0060405ca5ab2mr75992ywd.9.1707422198703; Thu, 08 Feb 2024 11:56:38 -0800 (PST) Date: Thu, 8 Feb 2024 19:56:21 +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-2-pranavpp@google.com> Subject: [PATCH v2 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, krossmo@google.com, Pranav Prasad X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790364193444309724 X-GMAIL-MSGID: 1790364193444309724 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 --- 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..e4b88c8dc0e1 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 unsigned long suspend_check_duration_ms = 2 * MSEC_PER_SEC; + +static ssize_t suspend_check_duration_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", suspend_check_duration_ms); +} + +static ssize_t suspend_check_duration_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_check_duration_ms = 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_ms * NSEC_PER_MSEC) { + pm_wakeup_event(dev, suspend_check_duration_ms); return -EBUSY; } 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 */