Received: by 10.223.185.116 with SMTP id b49csp1597073wrg; Wed, 21 Feb 2018 23:12:16 -0800 (PST) X-Google-Smtp-Source: AH8x2272V3Yez64ao2I0aZJimLF1KHeVJoj32i+lt4Q0jFGGY4BH2GBKDkN7O9Kqg2oe3Ami9uoK X-Received: by 10.98.7.73 with SMTP id b70mr5886078pfd.39.1519283536174; Wed, 21 Feb 2018 23:12:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519283536; cv=none; d=google.com; s=arc-20160816; b=TA8np/sGRT+UtMtqlz7f9zGNmTPHy5NwkDqTvriERx4VnAz2IqvPpH4bcoO+VU2wQT W9vVMYFsJfO9gIwpAi1kXbQ/xi3KrOKMdyvMxQSWs/VIdtYZOU9utn8jHkCcIbTYlY8F KED7GccqqKXngYT7ba1yWKlLNqicj5xbOw9ul4B6PvhKNZ9elwZ01kHXYniSLK5EPqK2 sXZBl+xy9wAvJCK+745vbXEhIfhodyj3e20yhCiGfAM9ChxetCY+3OcgXXt5ZG615nfC yFGMhSjtFKpqknV5Pb4RLe6966GZ4x6xY2gH3HEAMfNtilpjA+86ya09PYK5ASRymuAj qFbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=ok+P2/521oPLhC7jB584XRbBhNy30IIQEuzs5z3fFT8=; b=vxDnQeSsx81pz5QryCU0SbijtQyaoQ20yB+tIhvFvpTv1vZn0ArYl9SSPuLUw5zELp V4JGjhDgKFMtoFh+/5Tc5Pf//syFwJxBxgKZ5YnCxjhM7/sTqetwflIbl8a6SQ8Qmg/q 8lLRHiCN0IF7wo4qsHeBHTzu34h5fJb5+ovys7VPxnpgjUJs0QK2fbSJ/ks8R93MvtFz aGVPYytsSrhfb8qz3i0ktz8PinLm58ZL1xzW9jvVsY1q93SpgFcpz3B4l3HpcyiKEak4 Mb8txoyQkKPUpDn7a+5hRq5U51Mi+vwKakLao6fuY+eAV6CT8HyLWdxAQ6QupwlH/pA9 ZuMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=c6IwBpbY; 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 j61-v6si2297454plb.395.2018.02.21.23.12.01; Wed, 21 Feb 2018 23:12:16 -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=c6IwBpbY; 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 S1752696AbeBVHJ2 (ORCPT + 99 others); Thu, 22 Feb 2018 02:09:28 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:52595 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752584AbeBVHGG (ORCPT ); Thu, 22 Feb 2018 02:06:06 -0500 Received: by mail-wm0-f68.google.com with SMTP id t3so1825179wmc.2 for ; Wed, 21 Feb 2018 23:06:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ok+P2/521oPLhC7jB584XRbBhNy30IIQEuzs5z3fFT8=; b=c6IwBpbYR1mV2Mc922Yi8r8cA0o0XqiG2uyfRLJ7s4Zj4OK27M+WcF78wB9HvPrTYH BD6+4BPvaQJ+VL5eC5iV2oN/qa2pV5lN5P3Nf83HJVOktWxDRPfzKBYvn1s24793cy2F 7vN4kKutzp54JifgxanIKS8S3EC64yNDYiJcnKB0sFPIF1HcSN7hE+fV475uqIVDxQWS dwiq5WmzrnOUW1qvaq+Mg3P1wbTplMwosKlZ5F+qjrkV7Lt6TmvPmTERCjiD9hKqNECF NxVxhjb0E0Ujq9UPWsFr4DNYwAlhXs53ze8WvZ6gDa5AQX+AE5S1MN+uyKNLcoqunEKx xmlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ok+P2/521oPLhC7jB584XRbBhNy30IIQEuzs5z3fFT8=; b=nFbzx3blvJ6stW+z8NsZRTViP9Dqx9MMMO0msPyRqJi2uIf/Snvz7ua0+WMzvtQx5H j81tEHRgZWoV3XLCjj+A4+kpa7DJ2loRbbyz2MPIojfL6jy2rBW8Uw1Gr0FonfTYgVM3 /Bnh49toMW8cypj2mye56O+lWxzM95xxd8cRZXuR23lCjAvFHXCzHCEz+dckxoS/aDXj 0aIvDAhHTBgsAOFLGz7+2z+MnbMFG+D6dEJpduks4ZCIpcfz9RAWdqS3BTmfpHYb5yOq X4Mz4X4mAQkZ/dr+KUJZgQJm+afDQ+11WpHty1dSDxN4GerICDw1EHCQARS+RoGvEY8d G9fQ== X-Gm-Message-State: APf1xPACyAob49+QRNSkytkeneycR9ufH3tfUk+Kauf+hcqHFW715Y1T vwLty8rzIQDLsl2jTgNTuXkbFVaL X-Received: by 10.80.192.68 with SMTP id u4mr8225446edd.109.1519283165563; Wed, 21 Feb 2018 23:06:05 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id m1sm1267548edd.75.2018.02.21.23.06.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Feb 2018 23:06:04 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 2542A20784; Thu, 22 Feb 2018 02:06:03 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute7.internal (MEProxy); Thu, 22 Feb 2018 02:06:03 -0500 X-ME-Sender: Received: from localhost (unknown [45.32.128.109]) by mail.messagingengine.com (Postfix) with ESMTPA id 8B4B624610; Thu, 22 Feb 2018 02:06:02 -0500 (EST) From: Boqun Feng To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , Ingo Molnar , Andrea Parri , Boqun Feng Subject: [RFC tip/locking/lockdep v5 04/17] lockdep: Introduce lock_list::dep Date: Thu, 22 Feb 2018 15:08:51 +0800 Message-Id: <20180222070904.548-5-boqun.feng@gmail.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180222070904.548-1-boqun.feng@gmail.com> References: <20180222070904.548-1-boqun.feng@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To add recursive read locks into the dependency graph, we need to store the types of dependencies for the BFS later. There are four kinds of dependencies: * Non-recursive -> Non-recursive dependencies(NN) e.g. write_lock(prev) -> write_lock(next), we can also write this as "prev --(NN)--> next". * Recursive -> Non-recursive dependencies(RN) e.g. read_lock(prev) -> write_lock(next), we can also write this as "prev --(RN)--> next". * Non-recursive -> recursive dependencies(NR) e.g. write_lock(prev) -> read_lock(next), we can also write this as "prev --(NR)--> next". * Recursive -> recursive dependencies(RR) e.g. read_lock(prev) -> read_lock(next), we can also write this as "prev --(RR)--> next". Given a pair of two locks, four kinds of dependencies could all exist between them, so we use 4 bit for the presence of each kind(stored in lock_list::dep). Helper functions and marcos are also introduced to convert a pair of locks into ::dep bit and maintain the addition of different kinds of dependencies. Signed-off-by: Boqun Feng --- include/linux/lockdep.h | 2 ++ kernel/locking/lockdep.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h index 6fc77d4dbdcd..ab1e5a7d8864 100644 --- a/include/linux/lockdep.h +++ b/include/linux/lockdep.h @@ -187,6 +187,8 @@ struct lock_list { struct lock_class *class; struct stack_trace trace; int distance; + /* bitmap of different dependencies from head to this */ + u16 dep; /* * The parent field is used to implement breadth-first search, and the diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 5e6bf8d6954d..acd25bfc336d 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -859,7 +859,7 @@ static struct lock_list *alloc_list_entry(void) * Add a new dependency to the head of the list: */ static int add_lock_to_list(struct lock_class *this, struct list_head *head, - unsigned long ip, int distance, + unsigned long ip, int distance, unsigned int dep, struct stack_trace *trace) { struct lock_list *entry; @@ -872,6 +872,7 @@ static int add_lock_to_list(struct lock_class *this, struct list_head *head, return 0; entry->class = this; + entry->dep = dep; entry->distance = distance; entry->trace = *trace; /* @@ -1012,6 +1013,33 @@ static inline bool bfs_error(enum bfs_result res) return res < 0; } +#define DEP_NN_BIT 0 +#define DEP_RN_BIT 1 +#define DEP_NR_BIT 2 +#define DEP_RR_BIT 3 + +#define DEP_NN_MASK (1U << (DEP_NN_BIT)) +#define DEP_RN_MASK (1U << (DEP_RN_BIT)) +#define DEP_NR_MASK (1U << (DEP_NR_BIT)) +#define DEP_RR_MASK (1U << (DEP_RR_BIT)) + +static inline unsigned int __calc_dep_bit(int prev, int next) +{ + if (prev == 2 && next != 2) + return DEP_RN_BIT; + if (prev != 2 && next == 2) + return DEP_NR_BIT; + if (prev == 2 && next == 2) + return DEP_RR_BIT; + else + return DEP_NN_BIT; +} + +static inline unsigned int calc_dep(int prev, int next) +{ + return 1U << __calc_dep_bit(prev, next); +} + static enum bfs_result __bfs(struct lock_list *source_entry, void *data, int (*match)(struct lock_list *entry, void *data), @@ -1921,6 +1949,16 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev, if (entry->class == hlock_class(next)) { if (distance == 1) entry->distance = 1; + entry->dep |= calc_dep(prev->read, next->read); + } + } + + /* Also, update the reverse dependency in @next's ->locks_before list */ + list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) { + if (entry->class == hlock_class(prev)) { + if (distance == 1) + entry->distance = 1; + entry->dep |= calc_dep(next->read, prev->read); return 1; } } @@ -1948,14 +1986,18 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev, */ ret = add_lock_to_list(hlock_class(next), &hlock_class(prev)->locks_after, - next->acquire_ip, distance, trace); + next->acquire_ip, distance, + calc_dep(prev->read, next->read), + trace); if (!ret) return 0; ret = add_lock_to_list(hlock_class(prev), &hlock_class(next)->locks_before, - next->acquire_ip, distance, trace); + next->acquire_ip, distance, + calc_dep(next->read, prev->read), + trace); if (!ret) return 0; -- 2.16.1