From patchwork Thu Jul 25 00:41:33 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Cross X-Patchwork-Id: 2833126 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 3EC2FC0319 for ; Thu, 25 Jul 2013 00:42:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 28C7F2026F for ; Thu, 25 Jul 2013 00:42:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 46ADE20264 for ; Thu, 25 Jul 2013 00:42:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753048Ab3GYAmT (ORCPT ); Wed, 24 Jul 2013 20:42:19 -0400 Received: from mail-qc0-f201.google.com ([209.85.216.201]:59484 "EHLO mail-qc0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752685Ab3GYAmS (ORCPT ); Wed, 24 Jul 2013 20:42:18 -0400 Received: by mail-qc0-f201.google.com with SMTP id u11so104345qcx.4 for ; Wed, 24 Jul 2013 17:42:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:x-gm-message-state; bh=FMzrc5RSBI7vpquURlr5jBsjFTAtPU9AuuuY2e5xVlc=; b=Zkn3cP/fViGLwn0wf6fAcr+72ia0J2GRSni1mw8DXZ2a2GYW+Mv2Ow7n66ZlrFZPET QZofhlHFH/0ES06kKrv4sGvggqbygCV07sKdTlg1RbRiuzl4K0L+XwTixl1NUP/a8sWc LoLNBYjoZlk+BJemW5MeQTlU7NbNR+IUFmD9CWUl3+iAJTzK9pKyKz/02bf6oZf+hN/T dF2GAaXSfXylfk3DHB1H+yQMV8Kty5cg5kXOTWEtauCMd0evQULGOEaw30DZCMkbLTfY PzimyEcWST3mMFmQiCz406Yo8s4QhLCCgZe5E9jh2QO6mm2alaOcMmAw8YnAh5LYvg+Z PPdw== X-Received: by 10.236.120.232 with SMTP id p68mr22448184yhh.43.1374712938110; Wed, 24 Jul 2013 17:42:18 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id t25si508103yhg.6.2013.07.24.17.42.18 for (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Wed, 24 Jul 2013 17:42:18 -0700 (PDT) Received: from walnut.mtv.corp.google.com (walnut.mtv.corp.google.com [172.18.120.100]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id D581D31C1FC; Wed, 24 Jul 2013 17:42:17 -0700 (PDT) Received: by walnut.mtv.corp.google.com (Postfix, from userid 99897) id 6B38916085C; Wed, 24 Jul 2013 17:42:17 -0700 (PDT) From: Colin Cross To: lkml Cc: Michael Leun , "Rafael J. Wysocki" , Pavel Machek , Linus Torvalds , Linux PM list , Darren Hart , Colin Cross Subject: [PATCH] power: set PF_SUSPEND_TASK flag on tasks that call freeze_processes Date: Wed, 24 Jul 2013 17:41:33 -0700 Message-Id: <1374712893-14487-1-git-send-email-ccross@android.com> X-Mailer: git-send-email 1.8.3 X-Gm-Message-State: ALoCoQmrQnnSGZqVf8Fk3NPjIqHyy+N8SUPh5A6mroDz29xQmf7XQCECAzKfyFGk9ISuodMee19TRjTdbtwmOvQOcVozue1qkStDgtT9s719OF3YYjtaSb1mYreXmfaw8hKddfQhlDZnWgpFsY2efvldCvBWoIsgCrJMmi/OxhQTCpcXF5cptPsZ30eMXoD4UKsQ3jV9aglcs+4FPghseIv/Q77qPCEKJg== Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Calling freeze_processes sets a global flag that will cause any process that calls try_to_freeze to enter the refrigerator. It skips sending a signal to the current task, but if the current task ever hits try_to_freeze all threads will be frozen and the system will deadlock. Set a new flag, PF_SUSPEND_TASK, on the task that calls freeze_processes. The flag notifies the freezer that the thread is involved in suspend and should not be frozen. Also add a WARN_ON in thaw_processes if the caller does not have the PF_SUSPEND_TASK flag set to catch if a different task calls thaw_processes than the one that called freeze_processes, leaving a task with PF_SUSPEND_TASK permanently set on it. Threads that spawn off a task with PF_SUSPEND_TASK set (which swsusp does) will also have PF_SUSPEND_TASK set, preventing them from freezing while they are helping with suspend, but they need to be dead by the time suspend is triggered, otherwise they may run when userspace is expected to be frozen. Add a WARN_ON in thaw_processes if more than one thread has the PF_SUSPEND_TASK flag set. Reported-by: Michael Leun Tested-by: Michael Leun Signed-off-by: Colin Cross --- Resending not as an attachment for review. If the extra process flag is considered too precious for this (there are only 2 left after this patch) I could get the same functionality by having freeze_processes() reject calls from a PF_KTHREAD|PF_NOFREEZE thread, and use PF_KTHREAD to determine if PF_NOFREEZE should be cleared in thaw_processes(). include/linux/sched.h | 1 + kernel/freezer.c | 2 +- kernel/power/process.c | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 50d04b9..d722490 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1628,6 +1628,7 @@ extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, #define PF_MEMPOLICY 0x10000000 /* Non-default NUMA mempolicy */ #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */ #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */ +#define PF_SUSPEND_TASK 0x80000000 /* this thread called freeze_processes and should not be frozen */ /* * Only the _current_ task can read/write to tsk->flags, but other diff --git a/kernel/freezer.c b/kernel/freezer.c index 8b2afc1..b462fa1 100644 --- a/kernel/freezer.c +++ b/kernel/freezer.c @@ -33,7 +33,7 @@ static DEFINE_SPINLOCK(freezer_lock); */ bool freezing_slow_path(struct task_struct *p) { - if (p->flags & PF_NOFREEZE) + if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK)) return false; if (pm_nosig_freezing || cgroup_freezing(p)) diff --git a/kernel/power/process.c b/kernel/power/process.c index fc0df84..06ec886 100644 --- a/kernel/power/process.c +++ b/kernel/power/process.c @@ -109,6 +109,8 @@ static int try_to_freeze_tasks(bool user_only) /** * freeze_processes - Signal user space processes to enter the refrigerator. + * The current thread will not be frozen. The same process that calls + * freeze_processes must later call thaw_processes. * * On success, returns 0. On failure, -errno and system is fully thawed. */ @@ -120,6 +122,9 @@ int freeze_processes(void) if (error) return error; + /* Make sure this task doesn't get frozen */ + current->flags |= PF_SUSPEND_TASK; + if (!pm_freezing) atomic_inc(&system_freezing_cnt); @@ -168,6 +173,7 @@ int freeze_kernel_threads(void) void thaw_processes(void) { struct task_struct *g, *p; + struct task_struct *curr = current; if (pm_freezing) atomic_dec(&system_freezing_cnt); @@ -182,10 +188,15 @@ void thaw_processes(void) read_lock(&tasklist_lock); do_each_thread(g, p) { + /* No other threads should have PF_SUSPEND_TASK set */ + WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK)); __thaw_task(p); } while_each_thread(g, p); read_unlock(&tasklist_lock); + WARN_ON(!(curr->flags & PF_SUSPEND_TASK)); + curr->flags &= ~PF_SUSPEND_TASK; + usermodehelper_enable(); schedule();