Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2356740ybz; Sun, 26 Apr 2020 17:06:36 -0700 (PDT) X-Google-Smtp-Source: APiQypI0xliFgv3SO3JvqcjRLXfyG+VB4YN5Sg+Hc0S2iI3/1Nj3jC3dEq/rYAnuUay0tBaWBxlu X-Received: by 2002:a05:6402:3046:: with SMTP id bu6mr16466567edb.106.1587945995932; Sun, 26 Apr 2020 17:06:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587945995; cv=none; d=google.com; s=arc-20160816; b=DVyOmQlhjAgyMornnTXZ4u+4f/nNnaxC08RV1MjQRx81X0GBQ9/pTEkh5q/21LiBZd 9eOWfHc0feuRJqCHo9DHGCregTTGv8nnMcFZapv2mR8G8d8zEq0FYxVqWjFaellu2aze 8UMIw7mdD0L1y13Kh+XFEXMNsPKBC4GSmG2FCDPSS9y2/btMKcrOUgnpWvcfbXGaV7Qe pKrboaeZbWFbBPTxojfI3Jm6s20dMxhUqvV8KoPBdCmiMoZc4jM9TKbYqtdeATX9cze/ ASLTpyP6TKdIBcyV0bUcMV0Ct98JxU3uSyNAyGXH5Xr11iYyHsaKfP1YyVO2fs51C79X DKeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=7KGz+0+Cll4o0mawl4NXbvvTUOj3HZ99fsd0YD47xD0=; b=B0ftIbkdo4YlW9RkidysuIIBvzZ3Tn1649khAkmTLf9w/bDlfGIGPy9k+2QKA97f6H DTUJRls8/5QnTEaVIPKjDcem7PE2smnKMT869+nhmWFt6LNqJ5MWtS2TBAmzNoVSko4k RnpYgONvOGM0SYeSxvcQEty2GD44IpsATJ9HEwEZreg9PBGBDdO5RVwllKojddEMY8VA /kQ6OrcLIoWqBsL5QX+EpddHaFGZM1HYfnfynJ2vNk9StM63R1Shug0drhjhwmjtpt7L 2weBKvPWS8xJKEa6BPYfqdrdwCeTeMKhhnMevJurqXrCyAydLpIg/wa5905eFAWmdeic bTBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=lJSlPJ4N; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p6si4326815eja.77.2020.04.26.17.05.47; Sun, 26 Apr 2020 17:06:35 -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=@google.com header.s=20161025 header.b=lJSlPJ4N; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726340AbgD0AAm (ORCPT + 99 others); Sun, 26 Apr 2020 20:00:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726196AbgD0AAl (ORCPT ); Sun, 26 Apr 2020 20:00:41 -0400 Received: from mail-ua1-x942.google.com (mail-ua1-x942.google.com [IPv6:2607:f8b0:4864:20::942]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4034FC061A0F for ; Sun, 26 Apr 2020 17:00:40 -0700 (PDT) Received: by mail-ua1-x942.google.com with SMTP id s5so15633926uad.4 for ; Sun, 26 Apr 2020 17:00:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=7KGz+0+Cll4o0mawl4NXbvvTUOj3HZ99fsd0YD47xD0=; b=lJSlPJ4NUdh+uNKk7ghHIVffAyjwZ66Ca0V0L65xgLVtvtoZ2hlHsTwRpzZalEnm1l AXC2pidLYotiYSjTQABfnQsVp3q90k3+3UXnmMGXPDPd1i/YGwmxxVOcQNKcjsfqYC6b wFL6AQDkLLEX/xPtT7skDvUmF/evmHIloTZRUjs0hommD/iIuP/O/TRH8+zDqgPG8A0f z2E2hId8poEEtxY0WrK0IzuPhu3h95e/TbjCzcO1QHHAuHX4eYKaHkL7tGGrmhb0vVtl 2XhtrTJHDhoj7dOt921T2VfsFWaVWwAfteGntU8tpyORe1+v5an8GamStfsCuTMzyaSB 4nkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=7KGz+0+Cll4o0mawl4NXbvvTUOj3HZ99fsd0YD47xD0=; b=Y2yrNOaKAQx5k0ONMcl/5K9jwLWE3mM3C0QY3FywPTPJaFiEMMI93OWh9SCE0DB1ZA 7EKgvUI2A6hJfRVP1x7nkc652BAZKggk9x50OZvf/SKzEd4p9GTGmKTLrMmOCP7GnLh7 WfsrSeef7UqdlGhQ4OpKUxCDoc1+CsLlmDWXFupw9eK+5xn4whaB8v6aRvWCNpivmy6Q +Nw8lmeYph1NmvUJlGD350uT5ExKShvsSAUSbGqEUclVhNOIErRMWZy91SLF+NwXh3py Ef3k6A9ZUsH2fe9pyMf4C8EXHuUJ2lgcpDL1hI0CshB3q/VXgkcgbA0/yAWRtz/GQkE1 Ln+A== X-Gm-Message-State: AGi0Pub+wBjcR67XFvsok+w9IcdFLRj+E5PIko53be5FpFqRY9aSXzSZ vt0It1rcu+dOsVolsQgG+9IsCpYtiiDPeM3vtb0G/w== X-Received: by 2002:ab0:485:: with SMTP id 5mr14227287uaw.86.1587945638973; Sun, 26 Apr 2020 17:00:38 -0700 (PDT) MIME-Version: 1.0 References: <20200424174025.GA13592@hirez.programming.kicks-ass.net> In-Reply-To: From: Suren Baghdasaryan Date: Sun, 26 Apr 2020 17:00:27 -0700 Message-ID: Subject: Re: lockdep warning about possible circular dependency in PSI To: Shakeel Butt Cc: Peter Zijlstra , Ingo Molnar , Andrew Morton , will@kernel.org, Johannes Weiner , LKML , kernel-team Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 24, 2020 at 1:54 PM Shakeel Butt wrote: > > On Fri, Apr 24, 2020 at 10:52 AM Suren Baghdasaryan w= rote: > > > > On Fri, Apr 24, 2020 at 10:40 AM Peter Zijlstra = wrote: > > > > > > On Fri, Apr 24, 2020 at 09:34:42AM -0700, Suren Baghdasaryan wrote: > > > > Sorry to bother you again folks. Any suggestions on how to silence > > > > this lockdep warning which I believe to be a false positive? > > > > > > > > On Wed, Apr 15, 2020 at 4:01 PM Suren Baghdasaryan wrote: > > > > > > > > > > I received a report about possible circular locking dependency wa= rning > > > > > generated from PSI polling code. I think we are protected from th= is > > > > > scenario by poll_scheduled atomic but wanted to double-check and = I=E2=80=99m > > > > > looking for an advice on how to annotate this case to fix the loc= kdep > > > > > warning. I copied the detailed information at the end of this ema= il > > > > > but the short story is this: > > > > > > > > > > "WARNING: possible circular locking dependency detected" is gener= ated > > > > > with CONFIG_PSI and CONFIG_LOCKDEP enabled. The dependency chain = it > > > > > describes is: > > > > > > > > > > #0 > > > > > kthread_delayed_work_timer_fn() > > > > > | > > > > > worker->lock > > > > > | > > > > > try_to_wake_up() > > > > > | > > > > > p->pi_lock > > > > > > > > > > #1 > > > > > sched_fork() > > > > > | > > > > > p->pi_lock > > > > > | > > > > > task_fork_fair() > > > > > | > > > > > rq->lock > > > > > > > > > > #2 > > > > > psi_memstall_enter > > > > > | > > > > > rq->lock > > > > > | > > > > > kthread_queue_delayed_work() > > > > > | > > > > > worker->lock > > > > > > Irrespective of it actually being a deadlock or not, it is fairly > > > fragile. Ideally we'd fix #2, we really should minimize the number of > > > locks nested under rq->lock. > > > > > > That said, here's the easy fix, which breaks #0. > > > > > > > Thanks for the suggestion, Peter. Let me digest this and will post a > > patch with your Suggested-by. > > Cheers! > > > > I tested on my simple repro and the patch fixes the lockdep splat. > > You can add > Tested-by: Shakeel Butt > Thanks Shakeel! Will do. > > > --- > > > diff --git a/kernel/kthread.c b/kernel/kthread.c > > > index bfbfa481be3a..b443bba7dd21 100644 > > > --- a/kernel/kthread.c > > > +++ b/kernel/kthread.c > > > @@ -806,14 +806,15 @@ static void kthread_insert_work_sanity_check(st= ruct kthread_worker *worker, > > > /* insert @work before @pos in @worker */ > > > static void kthread_insert_work(struct kthread_worker *worker, > > > struct kthread_work *work, > > > - struct list_head *pos) > > > + struct list_head *pos, > > > + struct wake_q_head *wake_q) > > > { > > > kthread_insert_work_sanity_check(worker, work); > > > > > > list_add_tail(&work->node, pos); > > > work->worker =3D worker; > > > if (!worker->current_work && likely(worker->task)) > > > - wake_up_process(worker->task); > > > + wake_q_add(wake_q, worker->task); > > > } > > > > > > /** > > > @@ -831,15 +832,19 @@ static void kthread_insert_work(struct kthread_= worker *worker, > > > bool kthread_queue_work(struct kthread_worker *worker, > > > struct kthread_work *work) > > > { > > > - bool ret =3D false; > > > + DEFINE_WAKE_Q(wake_q); > > > unsigned long flags; > > > + bool ret =3D false; > > > > > > raw_spin_lock_irqsave(&worker->lock, flags); > > > if (!queuing_blocked(worker, work)) { > > > - kthread_insert_work(worker, work, &worker->work_list)= ; > > > + kthread_insert_work(worker, work, &worker->work_list,= &wake_q); > > > ret =3D true; > > > } > > > raw_spin_unlock_irqrestore(&worker->lock, flags); > > > + > > > + wake_up_q(&wake_q); > > > + > > > return ret; > > > } > > > EXPORT_SYMBOL_GPL(kthread_queue_work); > > > @@ -857,6 +862,7 @@ void kthread_delayed_work_timer_fn(struct timer_l= ist *t) > > > struct kthread_delayed_work *dwork =3D from_timer(dwork, t, t= imer); > > > struct kthread_work *work =3D &dwork->work; > > > struct kthread_worker *worker =3D work->worker; > > > + DEFINE_WAKE_Q(wake_q); > > > unsigned long flags; > > > > > > /* > > > @@ -873,15 +879,18 @@ void kthread_delayed_work_timer_fn(struct timer= _list *t) > > > /* Move the work from worker->delayed_work_list. */ > > > WARN_ON_ONCE(list_empty(&work->node)); > > > list_del_init(&work->node); > > > - kthread_insert_work(worker, work, &worker->work_list); > > > + kthread_insert_work(worker, work, &worker->work_list, &wake_q= ); > > > > > > raw_spin_unlock_irqrestore(&worker->lock, flags); > > > + > > > + wake_up_q(&wake_q); > > > } > > > EXPORT_SYMBOL(kthread_delayed_work_timer_fn); > > > > > > static void __kthread_queue_delayed_work(struct kthread_worker *work= er, > > > struct kthread_delayed_work = *dwork, > > > - unsigned long delay) > > > + unsigned long delay, > > > + struct wake_q_head *wake_q) > > > { > > > struct timer_list *timer =3D &dwork->timer; > > > struct kthread_work *work =3D &dwork->work; > > > @@ -895,7 +904,7 @@ static void __kthread_queue_delayed_work(struct k= thread_worker *worker, > > > * on that there's no such delay when @delay is 0. > > > */ > > > if (!delay) { > > > - kthread_insert_work(worker, work, &worker->work_list)= ; > > > + kthread_insert_work(worker, work, &worker->work_list,= wake_q); > > > return; > > > } > > > > > > @@ -928,17 +937,21 @@ bool kthread_queue_delayed_work(struct kthread_= worker *worker, > > > unsigned long delay) > > > { > > > struct kthread_work *work =3D &dwork->work; > > > + DEFINE_WAKE_Q(wake_q); > > > unsigned long flags; > > > bool ret =3D false; > > > > > > raw_spin_lock_irqsave(&worker->lock, flags); > > > > > > if (!queuing_blocked(worker, work)) { > > > - __kthread_queue_delayed_work(worker, dwork, delay); > > > + __kthread_queue_delayed_work(worker, dwork, delay, &w= ake_q); > > > ret =3D true; > > > } > > > > > > raw_spin_unlock_irqrestore(&worker->lock, flags); > > > + > > > + wake_up_q(&wake_q); > > > + > > > return ret; > > > } > > > EXPORT_SYMBOL_GPL(kthread_queue_delayed_work); > > > @@ -967,6 +980,7 @@ void kthread_flush_work(struct kthread_work *work= ) > > > KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn), > > > COMPLETION_INITIALIZER_ONSTACK(fwork.done), > > > }; > > > + DEFINE_WAKE_Q(wake_q); > > > struct kthread_worker *worker; > > > bool noop =3D false; > > > > > > @@ -979,15 +993,17 @@ void kthread_flush_work(struct kthread_work *wo= rk) > > > WARN_ON_ONCE(work->worker !=3D worker); > > > > > > if (!list_empty(&work->node)) > > > - kthread_insert_work(worker, &fwork.work, work->node.n= ext); > > > + kthread_insert_work(worker, &fwork.work, work->node.n= ext, &wake_q); > > > else if (worker->current_work =3D=3D work) > > > kthread_insert_work(worker, &fwork.work, > > > - worker->work_list.next); > > > + worker->work_list.next, &wake_q); > > > else > > > noop =3D true; > > > > > > raw_spin_unlock_irq(&worker->lock); > > > > > > + wake_up_q(&wake_q); > > > + > > > if (!noop) > > > wait_for_completion(&fwork.done); > > > } > > > @@ -1065,6 +1081,7 @@ bool kthread_mod_delayed_work(struct kthread_wo= rker *worker, > > > unsigned long delay) > > > { > > > struct kthread_work *work =3D &dwork->work; > > > + DEFINE_WAKE_Q(wake_q); > > > unsigned long flags; > > > int ret =3D false; > > > > > > @@ -1083,9 +1100,12 @@ bool kthread_mod_delayed_work(struct kthread_w= orker *worker, > > > > > > ret =3D __kthread_cancel_work(work, true, &flags); > > > fast_queue: > > > - __kthread_queue_delayed_work(worker, dwork, delay); > > > + __kthread_queue_delayed_work(worker, dwork, delay, &wake_q); > > > out: > > > raw_spin_unlock_irqrestore(&worker->lock, flags); > > > + > > > + wake_up_q(&wake_q); > > > + > > > return ret; > > > } > > > EXPORT_SYMBOL_GPL(kthread_mod_delayed_work); > > > > > > -- > > > To unsubscribe from this group and stop receiving emails from it, sen= d an email to kernel-team+unsubscribe@android.com. > > >