Received: by 10.223.185.116 with SMTP id b49csp2032158wrg; Thu, 22 Feb 2018 07:09:42 -0800 (PST) X-Google-Smtp-Source: AH8x2263SHKbc/PYI5sku7/TJQp3gejHorUv9jUiH9xbjMzVCR16/q3KapM18Opr/t2slLs4mYDy X-Received: by 2002:a17:902:7485:: with SMTP id h5-v6mr7039801pll.236.1519312182421; Thu, 22 Feb 2018 07:09:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519312182; cv=none; d=google.com; s=arc-20160816; b=0zghJTICsAESedkNHLqUb51Yk8EBLK0LmWk3q5DMpYN+KjGezcLMZJNRlPwGhjAVRr H51GGjNXIRnGlx4W4kZTU7/yAr/QnHnGRc0SZ4TSAJtOWUoPKe5rIe0eAkaivyHm1Ou2 /GXaQ6BlagVlrjsihiR3u7mpdyy4HDyaZpeQd2aVmsKLAgsoXVSostgEKPFdAbPcf/+I LF6ePKnXJvVcddk03Gke29AW1iiesxcPAKpHZ9oMvRPawvtSB0KTKaHuYatq46j6/AMu ynYL749Se5uWqaO6GJEPkXOTvCE0YUtshRSblNrArYUMbIFBpLHeTluKz6oHlR2d1sG9 GWWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=uk8MWs8YwPrp9ujmoOOW3865noFoKWHqK+HvZqz61Pc=; b=BbK1AYcqTeWjuP86U+YDM5RNf1wM69/Z/vpR+ZvBT0lkpkaxmgiVWB25N7W8xOHp6g 3ZrMJtIh2/7Aoipvm9sEoYH2QDe1ETk0KqZaG4KdEbYsLPHEqyOZlJiFHh2Hy6a2TGpu pMybwrBd29XW4podQkVfjLDafr9bQtWLAXotFnS+BTwM9KaKB9hm0tY4CwAiNxIucbIG 5tmEx4rvDSjF/T9WZPyPeoHbQT//UF+JFiN+PsQfIpWSDxpA483ATEBtS/wOmPY7BDi1 gUoDu/OdkvgIWSXkH5xamkYW4gN/rr1wwRbo2A9x1avOfRTQ9mjOtXZ+Ehn3uGtgjeUE ZogQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=sRUoA3su; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l80si149912pfb.178.2018.02.22.07.09.27; Thu, 22 Feb 2018 07:09:42 -0800 (PST) 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=@gmail.com header.s=20161025 header.b=sRUoA3su; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932737AbeBVPIt (ORCPT + 99 others); Thu, 22 Feb 2018 10:08:49 -0500 Received: from mail-qk0-f169.google.com ([209.85.220.169]:43523 "EHLO mail-qk0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753805AbeBVPIs (ORCPT ); Thu, 22 Feb 2018 10:08:48 -0500 Received: by mail-qk0-f169.google.com with SMTP id j4so987294qke.10 for ; Thu, 22 Feb 2018 07:08:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=uk8MWs8YwPrp9ujmoOOW3865noFoKWHqK+HvZqz61Pc=; b=sRUoA3suD9JgIpNmZyWyWraoGCLPLLZ1UE08ZyUZUYOnG4DYjqVnWe9AD7vBlSMj1F 5zKUnmgpn+LLjjQl+CpEvtUbNf8alehENkZmgN7CVA4sUMFoQEqz5hGypTUhpmPWdRXu t74t6SmH3Rm4Yw6dQ7rP7oZMAfpHHgLGWJrR/1uKfwZk9P6G0QzMPKR0atph7ridDBZn WDovq6xHY8zbqu9KTs/OtpExxrTYHYxUZBqGYhG3Lomc3UmKd/gsvklD952Z8QQg1o8U Qux7XgFPSRfNvN8LmnTLqhbJWd2QywAz52zP8NBHhEjzsL+4XVfRipRRdJTARgwnnIM6 LyMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=uk8MWs8YwPrp9ujmoOOW3865noFoKWHqK+HvZqz61Pc=; b=TTT8fvEuWh7M0dp8crLxqNSxow2eQaMSDiw8cyZCu6gx3f3C2TqLFwMD839jFIFGby bsCSV87v8bY8rWyLaTZLKH4qCWR1kdNiGWG6Nf4YPH+h4dc0huCCUsRE5/t1oHfcw/Zh O0ZMsbohrRbJ1Nta9ReX6joARMSQm+48GmEZeDf65mxkk6pStQn//6mM+SEWnjuQX6Tb YNAwmYD4rGY4WYa9TDD2qrT5+8Kzz6TI5842NMzpGJPNhWDr0NZMzhQ336QbJsFzVNSa kcz1rGV/2n3iAZiN40vyZ4kCXlOX9xFBZYXVGMlIRVJvkjdKRAx9S9nr0e24eHn6n4ve w6/A== X-Gm-Message-State: APf1xPACxjuPl5e+X23qlqzXJlYMXZuh1PxG/UruSi1CCiJbSLJhXlTz /DZWfIZSBoQHQE7hl20kdz+vZcMA X-Received: by 10.55.80.6 with SMTP id e6mr10710615qkb.187.1519312127601; Thu, 22 Feb 2018 07:08:47 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id o59sm203375qte.36.2018.02.22.07.08.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 07:08:46 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id 872E020E2B; Thu, 22 Feb 2018 10:08:45 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Thu, 22 Feb 2018 10:08:45 -0500 X-ME-Sender: Received: from localhost (unknown [45.32.128.109]) by mail.messagingengine.com (Postfix) with ESMTPA id A88417E070; Thu, 22 Feb 2018 10:08:44 -0500 (EST) Date: Thu, 22 Feb 2018 23:12:10 +0800 From: Boqun Feng To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Ingo Molnar , Andrea Parri Subject: Re: [RFC tip/locking/lockdep v5 05/17] lockdep: Extend __bfs() to work with multiple kinds of dependencies Message-ID: <20180222151210.jwxjchywk4jfecyf@tardis> References: <20180222070904.548-1-boqun.feng@gmail.com> <20180222070904.548-6-boqun.feng@gmail.com> <20180222142614.GR25201@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ipaldxvkbhgugx34" Content-Disposition: inline In-Reply-To: <20180222142614.GR25201@hirez.programming.kicks-ass.net> User-Agent: NeoMutt/20171215 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --ipaldxvkbhgugx34 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Feb 22, 2018 at 03:26:14PM +0100, Peter Zijlstra wrote: > On Thu, Feb 22, 2018 at 03:08:52PM +0800, Boqun Feng wrote: > > Now we have four kinds of dependencies in the dependency graph, and not > > all the pathes carry strong dependencies, for example: > >=20 > > Given lock A, B, C, if we have: > >=20 > > CPU1 CPU2 > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D > > write_lock(A); read_lock(B); > > read_lock(B); write_lock(C); > >=20 > > then we have dependencies A--(NR)-->B, and B--(RN)-->C, (NR and > > RN are to indicate the dependency kind), A actually doesn't have > > strong dependency to C(IOW, C doesn't depend on A), to see this, > ^ missing space >=20 > You're fairly consistent with not putting spaces before opening braces > in text, please don't do this, this is not a C function call. Also Got it. > double check all your braces are matched, IIRC there's at least one that > isn't closed in the previous patches. >=20 Sure, will do. > > let's say we have a third CPU3 doing: > >=20 > > CPU3: > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > write_lock(C); > > write_lock(A); > >=20 > > , this is not a deadlock. However if we change the read_lock() > > on CPU2 to a write_lock(), it's a deadlock then. > >=20 > > So A --(NR)--> B --(RN)--> C is not a strong dependency path but > > A --(NR)--> B --(NN)-->C is a strong dependency path. >=20 > I'm not really satisfied with the above reasoning. I don't disagree, but > if possible it would be nice to have something a little more solid. >=20 What do you mean by "solid"? You mean "A --(NR)--> B --(NN)-->C" is too abstract, and want something like the below instead: CPU 1 CPU 2 CPU 3 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D write_lock(A); write_lock(B); read_lock(B); // blocked write_lock(C); write_lock(C); // blocked write_lock(A); // blocked ? >=20 > > We can generalize this as: If a path of dependencies doesn't have two > > adjacent dependencies as (*R)--L-->(R*), where L is some lock, it is a > > strong dependency path, otherwise it's not. > >=20 > > Now our mission is to make __bfs() traverse only the strong dependency > > paths, which is simple: we record whether we have -(*R)-> at the current > > tail of the path in lock_list::is_rr, and whenever we pick a dependency > > in the traverse, we 1) make sure we don't pick a -(R*)-> dependency if > > our current tail is -(*R)-> and 2) greedily pick a -(*N)-> as hard as > > possible. > >=20 > > With this extension for __bfs(), we now need to initialize the root of > > __bfs() properly(with a correct ->is_rr), to do so, we introduce some > ^ another missing space >=20 > Also, I don't like is_rr as a name, have_xr might be better. >=20 Maybe "pick_xr", which means we pick a *R in the BFS search path for the previous entry, then we can not pick R* for the next entry. > Only if we combine *R with R* do we have RR. >=20 > > +/* > > + * return -1 if no proper dependency could be picked > > + * return 0 if a -(*N)-> dependency could be picked > > + * return 1 if only a -(*R)-> dependency could be picked > > + * > > + * N: non-recursive lock > > + * R: recursive read lock > > + */ > > +static inline int pick_dep(u16 is_rr, u16 cap_dep) > > +{ > > + if (is_rr) { /* could only pick -(N*)-> */ > > + if (cap_dep & DEP_NN_MASK) > > + return 0; > > + else if (cap_dep & DEP_NR_MASK) > > + return 1; > > + else > > + return -1; > > + } else { > > + if (cap_dep & DEP_NN_MASK || cap_dep & DEP_RN_MASK) >=20 > if (cap_dep & (DEP_NN_MASK | DEP_RN_MASK)) Good point! >=20 > > + return 0; > > + else > > + return 1; > > + } > > +} >=20 > However, I would suggest: >=20 > static inline bool is_xr(u16 dep) > { > return !!(dep & (DEP_NR_MASK | DEP_RR_MASK)); > } >=20 > static inline bool is_rx(u16 dep) > { > return !!(dep & (DEP_RN_MASK | DEP_RR_MASK)); > } >=20 >=20 > > @@ -1095,11 +1179,18 @@ static enum bfs_result __bfs(struct lock_list *= source_entry, > > else > > head =3D &lock->class->locks_before; > > =20 > > + is_rr =3D lock->is_rr; > > + > > DEBUG_LOCKS_WARN_ON(!irqs_disabled()); > > =20 > > list_for_each_entry_rcu(entry, head, entry) { > > unsigned int cq_depth; > > =20 > > + next_is_rr =3D pick_dep(is_rr, entry->dep); > > + if (next_is_rr < 0) > > + continue; > > + entry->is_rr =3D next_is_rr; >=20 > /* Skip *R -> R* relations */ > if (have_xr && is_rx(entry->dep)) > continue; I don't think this works, if we pick a *R for previous entry, and for current entry, we have RR, NN and NR, your approach will skip the current entry, but actually we can pick NN or NR (of course, in __bfs(), we can greedily pick NN, because if NR causes a deadlock, so does NN). Note that lock_list::dep can be treated as a set/bitmap for different kinds of dependencies between the same pair of locks. Some related explanation of pick_dep() is put in the comment of __bfs(), which is my bad ;-( I will reorder the code to have an overall explanation for the algorithm, and put all related functions after that. Regards, Boqun >=20 > entry->have_xr =3D is_xr(entry->dep); >=20 > Which to me is a much simpler construct, hmm? >=20 > > + > > visit_lock_entry(entry, lock); > > if (match(entry, data)) { > > *target_entry =3D entry; >=20 >=20 >=20 --ipaldxvkbhgugx34 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEEj5IosQTPz8XU1wRHSXnow7UH+rgFAlqO3ccACgkQSXnow7UH +rjjzggArnklZHe7A7LKNeljg6JDsq/5yqIal3s5KH8JiZ8ZwzYE5dWzOgz2opTU Mt2Yh7vW3u2pMTcnflWw7AVgf0ETH524RedGcHcGDbPnMAVzLBF32+pM8J0CeC/W 79yTCxWKdFzNaOnUZBb5rZixnBbh9njZ+jfGMD2ErrdUd8x/WsD+YDeNljjXsIwM AVujqtxkr54lw2gpuEwlJlsxSexQZYeHij4HD6+vd9U+tRkB+jj5TCtWuD6W1TyS YcfAu+tSVz56wJoqV26RafOGLzmwXd/9c2eBcrVW6EnGaNRrGRzudO3HMvv3ieE5 6fHpfygh91uguyhpZIlCNe9lfG4oQw== =ITZT -----END PGP SIGNATURE----- --ipaldxvkbhgugx34--