Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3803060pxj; Mon, 7 Jun 2021 22:00:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxfFq6BpkpUhAal+HGrBSrING4Kg9YeyFcmHC5cvo0Lrg3RIMtygijCWcKsKD1wEzRvdvIB X-Received: by 2002:a17:906:3644:: with SMTP id r4mr21478762ejb.140.1623128405778; Mon, 07 Jun 2021 22:00:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623128405; cv=none; d=google.com; s=arc-20160816; b=Q2K6pSq6rYkqVyRz5E21umQfknB7tPDVzhYrbZCVC8XpsTd+MgIHUH9mq01dFGH1zV 3X2Gt8q6JIQZn6NbpZnEEmxYmCCjNahq2WxdW9gbQ5va48LSUv5SdONvUKgR6ZgcFjtE 6iNjey1KuNHtcyvKfpNoS31pFw/KWITHpGdgg1iWwQVRSQb1EeV4CF0f0sQjxO0HMuGc feTT+zHxcPZy1rxgvgIrUE0KEL0tn+LMPGaGrIc6/YwqJnrCbW7+7/opDZtdBFvTA+IT OAgt7EWGnIc1TNALT0JenEmU0jR45XQKHlnDdBOnrH8S4Keug8Bjk6BboyNYR8Gt01EL bfTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=wMCiGh7n+l/6a1YWf8fjCZW+MxxtluLrA1icIJKpcak=; b=UPhxWLBUE5iimfONBhrYvVQE5JeENA/T7EtXeW+M0WS8LK8ppuEfZxG8CC70FJWXc9 8jYr/5XAgSRQQvmfrB86hCnUGdzj4uMtP4Wla7NwDxl1hjxVVj/8FivI0WfTQKO9TKF6 3qnfBzhOj5rKOKBy+w5HZ4GJ5q0VBMw5lQJsGMyqZbj3oD/xmlSt57rgVxlesr6tYsVl xUlphUHfoyOdPekxvf+VrI1yX52wpNeFyTdFPFqFncz7wCLCipNaDxxQPfjGklX3EbHV YtwyfoBNd46El4+fN/Rw4uyxpvWHAv10W2dpmXUYt5+bmrtS2PV1TYPB70PwYEaD9V+3 8Vgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lvjM+bXY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i18si13126945edr.251.2021.06.07.21.59.42; Mon, 07 Jun 2021 22:00:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lvjM+bXY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230345AbhFHE6R (ORCPT + 99 others); Tue, 8 Jun 2021 00:58:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:50370 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229797AbhFHE6Q (ORCPT ); Tue, 8 Jun 2021 00:58:16 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A7E8E6124C; Tue, 8 Jun 2021 04:56:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1623128184; bh=6XVSYO53rouXTNC0JGmYmDWKE20TC2eAhh4ZbpoGbcI=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=lvjM+bXY7YAf2FxeomNjPYezK+TKb3jxP8S9yKihYjVF1GotNDtPAUMTqw5joM/PF Ohxhk6l/T63uitoZLn8VRSrG89I5vdJ9o9yamHBIElm9fIOTRHFZoJudw0Kf+gz5rd lh6wB+Ki+5RuVwE/luzZ9qCgP0forEtYAKfCQzxUJwE+Y7nxM/LGSo+MqhfkgijpyQ ESsSSEIJWakxFl5WXdV+neyIDh8yFg+cCmPPIIUc4RwyuZkhVQGOxZLR2tzHLpin3w DCoZVa9LPpduvuwf5CZp2I4HvqSam2JRUbPaMPy79RRBQ3CIZg7zbUJnxUEbh84y1L YiN0FOR+kdj1A== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 7D88F5C08B9; Mon, 7 Jun 2021 21:56:24 -0700 (PDT) Date: Mon, 7 Jun 2021 21:56:24 -0700 From: "Paul E. McKenney" To: "Zhang, Qiang" Cc: "Rafael J. Wysocki" , Rafael Wysocki , Len Brown , Pavel Machek , Linux PM , Linux Kernel Mailing List Subject: Re: [PATCH] PM: sleep: Replace read_lock/unlock(tasklist_lock) with rcu_read_lock/unlock() Message-ID: <20210608045624.GY4397@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20210607065743.1596-1-qiang.zhang@windriver.com> <20210607154138.GU4397@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 08, 2021 at 02:50:26AM +0000, Zhang, Qiang wrote: > > > ________________________________________ > From: Paul E. McKenney > Sent: Monday, 7 June 2021 23:41 > To: Zhang, Qiang > Cc: Rafael J. Wysocki; Rafael Wysocki; Len Brown; Pavel Machek; Linux PM; Linux Kernel Mailing List > Subject: Re: [PATCH] PM: sleep: Replace read_lock/unlock(tasklist_lock) with rcu_read_lock/unlock() > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > On Mon, Jun 07, 2021 at 02:11:37PM +0000, Zhang, Qiang wrote: > > ________________________________________ > > From: Rafael J. Wysocki > > Sent: Monday, 7 June 2021 19:45 > > To: Zhang, Qiang > > Cc: Rafael Wysocki; Len Brown; Pavel Machek; Paul E. McKenney; Linux PM; Linux Kernel Mailing List > > Subject: Re: [PATCH] PM: sleep: Replace read_lock/unlock(tasklist_lock) with rcu_read_lock/unlock() > > > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > > > On Mon, Jun 7, 2021 at 8:57 AM wrote: > > > > > > From: Zqiang > > > > > > Using rcu_read_lock/unlock() instead of read_lock/unlock(tasklist_lock), > > > the task list can be traversed in parallel to any list additions or > > > removals, improve concurrency. > > > > > > Signed-off-by: Zqiang > > > > >This changes the reader side only AFAICS, but what about the >writer side? > > > > Hello rafael > > > > In the case of holding read_lock(tasklist_lock) > > if there are a lot of tasks in the system that need to be frozen, > > the read_lock(tasklist_lock) has not been released for a short time, > > when clone task, we will acquire write_lock(tasklist_lock) and insert task to list, if Ifcan't get the write lock all the time. the hung task > > may be trigger and report warning. > > > > for example: > > > > copy_process() > > {........ > > write_lock_irq(&tasklist_lock); > > ........ > > list_add_tail_rcu(&p->tasks, &init_task.tasks); > > } > > > > > > > >What exactly is there to ensure that the updates of the list will > > >remain safe after this change? > > > > The RCU can guarantee that the list can be traversed and added at the same time, and in the read critical area, it is guaranteed that the task structure will not be released. > > > > In ./Documentation/RCU/listRCU.rst , it is also explained. > > > > Maybe we can ask Paul E, Paul E what's your opinion ? > > >Although RCU does guarantee that the task structures are not freed while > >an RCU reader references them, that is only one of the questions that > >must be answered. > > > >Changing from read_lock(&tasklist_lock) to rcu_read_lock() also allows > >all of the code currently under write_lock(&tasklist_lock) to execute > >concurrently with these newly created RCU readers. Is that safe? > >If it is safe, why? > > > >For example, the first pair of hunks in the diff -might- be safe because > >"todo" is updated while read-holding tasklist_lock, and then that value > >is used after releasing that lock. But I do not know this code well, > >and it is quite possible that something else is going on. > > Thanks Pual E > > you mean is the rcu ensures that the task_struct is not released but it does not guarantee that some members of the task_struct object are valid. > There may be the following scenarios: > In rcu read critical area, when accessing some members of task_struct object , it may have been released, when the task exiting. > > Is my understanding correct? Yes, that is a concern. The freezer code would need to be checked carefully to see whether or not the transformation from read_lock() to rcu_read_lock() was safe. Thanx, Paul > Qiang > > > > > Thanx, Paul > > > Thanks > > Qiang > > > > > --- > > > kernel/power/process.c | 16 ++++++++-------- > > > 1 file changed, 8 insertions(+), 8 deletions(-) > > > > > > diff --git a/kernel/power/process.c b/kernel/power/process.c > > > index 50cc63534486..0f8dee9ee097 100644 > > > --- a/kernel/power/process.c > > > +++ b/kernel/power/process.c > > > @@ -48,7 +48,7 @@ static int try_to_freeze_tasks(bool user_only) > > > > > > while (true) { > > > todo = 0; > > > - read_lock(&tasklist_lock); > > > + rcu_read_lock(); > > > for_each_process_thread(g, p) { > > > if (p == current || !freeze_task(p)) > > > continue; > > > @@ -56,7 +56,7 @@ static int try_to_freeze_tasks(bool user_only) > > > if (!freezer_should_skip(p)) > > > todo++; > > > } > > > - read_unlock(&tasklist_lock); > > > + rcu_read_unlock(); > > > > > > if (!user_only) { > > > wq_busy = freeze_workqueues_busy(); > > > @@ -97,13 +97,13 @@ static int try_to_freeze_tasks(bool user_only) > > > show_workqueue_state(); > > > > > > if (!wakeup || pm_debug_messages_on) { > > > - read_lock(&tasklist_lock); > > > + rcu_read_lock(); > > > for_each_process_thread(g, p) { > > > if (p != current && !freezer_should_skip(p) > > > && freezing(p) && !frozen(p)) > > > sched_show_task(p); > > > } > > > - read_unlock(&tasklist_lock); > > > + rcu_read_unlock(); > > > } > > > } else { > > > pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000, > > > @@ -206,13 +206,13 @@ void thaw_processes(void) > > > > > > cpuset_wait_for_hotplug(); > > > > > > - read_lock(&tasklist_lock); > > > + rcu_read_lock(); > > > for_each_process_thread(g, p) { > > > /* No other threads should have PF_SUSPEND_TASK set */ > > > WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK)); > > > __thaw_task(p); > > > } > > > - read_unlock(&tasklist_lock); > > > + rcu_read_unlock(); > > > > > > WARN_ON(!(curr->flags & PF_SUSPEND_TASK)); > > > curr->flags &= ~PF_SUSPEND_TASK; > > > @@ -233,12 +233,12 @@ void thaw_kernel_threads(void) > > > > > > thaw_workqueues(); > > > > > > - read_lock(&tasklist_lock); > > > + rcu_read_lock(); > > > for_each_process_thread(g, p) { > > > if (p->flags & PF_KTHREAD) > > > __thaw_task(p); > > > } > > > - read_unlock(&tasklist_lock); > > > + rcu_read_unlock(); > > > > > > schedule(); > > > pr_cont("done.\n"); > > > -- > > > 2.17.1 > > >