Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1794336yba; Fri, 10 May 2019 00:53:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqzXUfUD+iMLpsvlAaGjpS6my0ktv/+dmXLmTfzKz3iT+mer6pe2pEIvvGmocqsAWkomOtvH X-Received: by 2002:a17:902:704c:: with SMTP id h12mr11179367plt.270.1557474806035; Fri, 10 May 2019 00:53:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557474806; cv=none; d=google.com; s=arc-20160816; b=RIQYc3aTmYX7/ne8SKtmHvLqnAcYcNArJl/KtPtFriDn/oSx3xETRV6UQEJOLh9FGC S2VVZw2krOVXV5UN7SkkXp/c2jJJQCCY4EmfKng0DeRVLFFQqMXUyf0OE63a5KJaZU95 122Vw532CMXhMFx6gufRT1vfKmNaIVBgsDUxeFwDjPtN47NLs9JrZd2ReelbtUzVnuZg toXWrrh/2ggO7b/PErF6cP8mseJtpWVd4/qip9RqoVUawjIAHbt9qUppVsrCu5ss7now wqQE5nk/+ULq3caOl61P1f3fP26rrWADQiH2IOsE8bXVKzqmPjU5/fdd2ZJ21xkdMJC6 GNHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=bb+vyJ2l2JyKWoFlPq1nHRAbP2mdN5kXUD5ewhMbnn8=; b=hF2ksMvvi+71DiFpbJJr3NZxGl4I7449R7x8sTGypRB3JoqnipORfzhhbGEl8JwhN4 NM4A+0hQ5SHBgjWuMsf1XRS2AL5hSKluDmGV93a27290g2lDWkFSJXTMoty5IzUkLj+I GZQJKku97afOl+cyaYlSPlw+TZ9rDF9+zy6YRRim/ANSU6YR//wKIo9BDCx0veWCqvne t7z6FtAaEYoWOkWnxTn6es7HcIf/NhfREjDCr/C4dg63g9XL6qZGYiqR/rAOtVFLLYwp h4ViZuQMzK3xSrAqiBg39jTHWnF0vMD+r9uRJ4z9gtw9Wjv5eQSwfe2dQUP0grEsDgIR an1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=ZbMxX3MK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d16si6486304pfr.229.2019.05.10.00.53.09; Fri, 10 May 2019 00:53:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=ZbMxX3MK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727052AbfEJHwA (ORCPT + 99 others); Fri, 10 May 2019 03:52:00 -0400 Received: from mail-it1-f194.google.com ([209.85.166.194]:39725 "EHLO mail-it1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726899AbfEJHwA (ORCPT ); Fri, 10 May 2019 03:52:00 -0400 Received: by mail-it1-f194.google.com with SMTP id 9so489915itf.4 for ; Fri, 10 May 2019 00:51:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=bb+vyJ2l2JyKWoFlPq1nHRAbP2mdN5kXUD5ewhMbnn8=; b=ZbMxX3MKqmdfjEtLjPb6AIYZUKmckX5fhBu1Np3jWVfzdBwLml/8MtAA98LAuk5eVa CO+jAczip8dABiK05qRCe/BFFNNLq4gqQmZOelR55RYGn2zHNl/v2THzyk/G585xF1E5 uGHwq5WIOtP7mmksLzUClSUXAAFQ9FYNn7dCI= 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; bh=bb+vyJ2l2JyKWoFlPq1nHRAbP2mdN5kXUD5ewhMbnn8=; b=hM68atDUYyxBx3IVJZ+Bvbq3U8hNCcdXJQFPljdgefRDefSGqSI6pn3ztjEpUmAWEQ nmn76NYKcucZ32ZIdDqyapzyAvdyi5GEExy6icjIlc5fWzVmxPBHoVePegtN653C8e4Z 0aD7mzQm7RLVfpPjjN6m3HqPBH+fYYoo1EidI7NQk33IELyoKPm7GHBZnw45711jZG9+ F6cIMXZ+u3x0z52NtBjgjyc42i6ZNi1uwRfzAkkUSRLI2mw8L/8wjMkjcH+5nvjbYeM8 MyQLP0BMC9lh9nXbz34HR7y9vzXPyu/rG8kRND3kV366871jIlZAqmYImfMsVDEtHqcN t0sQ== X-Gm-Message-State: APjAAAWK4NdmJ03T9iIZmpYgiNNf/W8ckQGgzqmvhvNhchFXu73XiNai bSAHoOqSxA43mFazTihmzdbXDJe0rp1sCrYVpBqvkg== X-Received: by 2002:a24:7897:: with SMTP id p145mr6524450itc.117.1557474719408; Fri, 10 May 2019 00:51:59 -0700 (PDT) MIME-Version: 1.0 References: <20190509120903.28939-1-daniel.vetter@ffwll.ch> <20190509200633.19678-1-daniel.vetter@ffwll.ch> <20190510055053.GA9864@jagdpanzerIV> In-Reply-To: <20190510055053.GA9864@jagdpanzerIV> From: Daniel Vetter Date: Fri, 10 May 2019 09:51:48 +0200 Message-ID: Subject: Re: [PATCH] kernel/locking/semaphore: use wake_q in up() To: Sergey Senozhatsky Cc: Intel Graphics Development , DRI Development , Daniel Vetter , Peter Zijlstra , Ingo Molnar , Will Deacon , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , John Ogness , Chris Wilson , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 10, 2019 at 7:50 AM Sergey Senozhatsky wrote: > > On (05/09/19 22:06), Daniel Vetter wrote: > [..] > > +/* Functions for the contended case */ > > + > > +struct semaphore_waiter { > > + struct list_head list; > > + struct task_struct *task; > > + bool up; > > +}; > > + > > /** > > * up - release the semaphore > > * @sem: the semaphore to release > > @@ -179,24 +187,25 @@ EXPORT_SYMBOL(down_timeout); > > void up(struct semaphore *sem) > > { > > unsigned long flags; > > + struct semaphore_waiter *waiter; > > + DEFINE_WAKE_Q(wake_q); > > > > raw_spin_lock_irqsave(&sem->lock, flags); > > - if (likely(list_empty(&sem->wait_list))) > > + if (likely(list_empty(&sem->wait_list))) { > > sem->count++; > > - else > > - __up(sem); > > + } else { > > + waiter = list_first_entry(&sem->wait_list, > > + struct semaphore_waiter, list); > > + list_del(&waiter->list); > > + waiter->up = true; > > + wake_q_add(&wake_q, waiter->task); > > + } > > raw_spin_unlock_irqrestore(&sem->lock, flags); > > So the new code still can printk/WARN under sem->lock in some buggy > cases. > > E.g. > wake_q_add() > get_task_struct() > refcount_inc_checked() > WARN_ONCE() > > Are we fine with that? Hm not great. It's not as bad as the one I'm trying to fix (or not the same at least), because with the wake up chain we have a few locks in there. Which allows lockdep to connect the loop and complain, even when we never actually hit that specific recursion. I.e. once hitting a WARN_ON from try_to_wake_up is enough, plus a totally separate callchain can then close the semaphore.lock->scheduler locks part. Your chain only goes boom if it happens from the console_lock's up. wake_q_add_safe would be an option, but then we somehow need to arrange for down to call get_task_struct(current) and releasing that, but only if there's no waker who needs that task ref. Sounds tricky ... Also not sure we want to stuff that trickery into the generic semaphore code. -Daniel -- Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch