Received: by 10.223.185.116 with SMTP id b49csp1593160wrg; Wed, 21 Feb 2018 23:06:59 -0800 (PST) X-Google-Smtp-Source: AH8x226ugn65O/haQvMgOzQrRAUk+o3ahb0c3X5wSLse5CibFqMje0b2uTRUrws+fQQmlohv90Sc X-Received: by 10.99.124.2 with SMTP id x2mr4933984pgc.184.1519283219254; Wed, 21 Feb 2018 23:06:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519283219; cv=none; d=google.com; s=arc-20160816; b=apq5kimpppD5tGMBaZ1kF575XSBHgur2S+Axeat7AwMiecDhNNTnK3Pb6v1ZVL+pi5 4olpQ+bXP1PU8jzkNoNnS9TWXjemsWuAGPf6UvYph1y4k2O9e2grZ98rcv21EOY/2a4L 2i5iZ8kxebdZoy1SFV5/VXfPKKMJXgoogKrd4E9EvBctvWrawX2WR5rwyTJA0U7YlPPJ GIFZYUVw5I+7ZHGo5M/JvLLR3ktiPY/Hda+ToWHEtiXqD40jRSmpKfvK/nTGE9sTzNET LH8I8GP+yEyrtfVAlB0wa7gdh/ThU4JfyGySUNKVkVrlbTwGxTulQTIk8ulR80YYw9LW NJ2g== 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=C/uVfjuaSfz5+Mq/VzsoccpkwkmYgbYNbl6huvx5v/I=; b=h8sTb+/AR7WPeAS02ZuX2TI7Fy9lqw4Hv8KWOmxQIeA8iDzt1PvJM5Ueo8/wWbe967 Kh2IrArc9hrkSFXsJuyMm0sFGaT+V8GRg7/c4hTEtyj7nhHShlvX/A+MKYEQxHHbHoZI VvURT15JY/p9Wjol4XXJlPC8u1N6imOhgTuuOLHkotmBVP/bCIwCv+NO+D/ohyXZFHPT ngsLUPQusiljSNft+09shisL7HKXQp/7Sn1FUqsb/+HaZ24Sii43Fk68csMehnIBEVRr t0CtvQ0htdEgltvSQgxJVVShFrH3aidIxU26rBFMnOUZe7wYzsswqcYSIcVEw0eo/7lB 18Hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=nwHj1mBZ; 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 y14si3424210pge.265.2018.02.21.23.06.45; Wed, 21 Feb 2018 23:06:59 -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=nwHj1mBZ; 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 S1752559AbeBVHF7 (ORCPT + 99 others); Thu, 22 Feb 2018 02:05:59 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:54015 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752534AbeBVHFy (ORCPT ); Thu, 22 Feb 2018 02:05:54 -0500 Received: by mail-wm0-f68.google.com with SMTP id t74so1829976wme.3 for ; Wed, 21 Feb 2018 23:05:53 -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=C/uVfjuaSfz5+Mq/VzsoccpkwkmYgbYNbl6huvx5v/I=; b=nwHj1mBZsmZbXbXjLkgxIRd9ZgkStaK6cMT9cWRmIiCTZhLsEm53JBLs5+EigAl+PE MCHf1kb8IoJiigN8nmbbdeZg0FVvdmwl2CXWgC526F+QqhrJl0S8bqC1w3DsMGIaZIo2 fDn0nG4PSKwrA+4QdC5wZR4HXlePH1IU3g2SSOGGCnDUuVyW5Q0/SE6f5FcVKgrbbRGU kAgK+VNKTCEyK6Z5RaLrBVThA4DGKaq0i0Uxe9rCP8QXRMEe0BQn4yEqrXBuifKp1S/Q hkLaF6rs7kO3wRqqKClK6HDEdmK9RKxO78M7T3FiAhPX2Iqje77m3ExDWJbNJN/gCEnl gzGA== 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=C/uVfjuaSfz5+Mq/VzsoccpkwkmYgbYNbl6huvx5v/I=; b=rM42uajT3zx2k/ofk7ogy+DxosQ9clA1kszjCiYkZM4s61bAmu1omgrcsqvCKSq9Gh MbLNOpqzGu8en4A1Bq4S151dAU5B8+Bz0WqxFc/h4jfEZbi7qclZY2vNTJhZgr1wp52e fwVgdFTF/frrFkuOktRymPiMlmgyWNsW0CCWShTaEy71wPISMprVG37mj7FfpGHp4FwC vYwXd3xeSKGM2UVipqhZu6XHYlPDDiLzgZ9sT5blBEIrSunjj5r8U9dtJXANiWkD/z4m 96eUllpP3fpHnw7OdtwPAsWNBGpk5obkrGkWcHQC8eozOhcrD35oMybdh4BoNC6YAX4J 43vA== X-Gm-Message-State: APf1xPACm8ldJ3TlUajLxgPO861Li7pANvPOmsOKTXH0BFmqLohnXuQi 1ZLUW52Hmj55gcS2MfRWbvAP26TN X-Received: by 10.80.147.21 with SMTP id m21mr8175486eda.175.1519283152488; Wed, 21 Feb 2018 23:05:52 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id f53sm5491730ede.49.2018.02.21.23.05.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Feb 2018 23:05:51 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 2C1DF20BBA; Thu, 22 Feb 2018 02:05:50 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute7.internal (MEProxy); Thu, 22 Feb 2018 02:05:50 -0500 X-ME-Sender: Received: from localhost (unknown [45.32.128.109]) by mail.messagingengine.com (Postfix) with ESMTPA id 5F78D2460B; Thu, 22 Feb 2018 02:05:49 -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 01/17] lockdep: Demagic the return value of BFS Date: Thu, 22 Feb 2018 15:08:48 +0800 Message-Id: <20180222070904.548-2-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 __bfs() could return four magic numbers: 1: search succeeds, but none match. 0: search succeeds, find one match. -1: search fails because of the cq is full. -2: search fails because a invalid node is found. This patch cleans things up by using a enum type for the return value of __bfs() and its friends, this improves the code readability of the code, and further, could help if we want to extend the BFS. Signed-off-by: Boqun Feng --- kernel/locking/lockdep.c | 136 ++++++++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 56 deletions(-) diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 89b5f83f1969..9b2e318bcc81 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -984,21 +984,52 @@ static inline int get_lock_depth(struct lock_list *child) } return depth; } +/* + * Return values of a bfs search: + * + * BFS_E* indicates an error + * BFS_R* indicates a result(match or not) + * + * BFS_EINVALIDNODE: Find a invalid node in the graph. + * + * BFS_EQUEUEFULL: The queue is full while doing the bfs. + * + * BFS_RMATCH: Find the matched node in the graph, and put that node * into + * *@target_entry. + * + * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry + * _unchanged_. + */ +enum bfs_result { + BFS_EINVALIDNODE = -2, + BFS_EQUEUEFULL = -1, + BFS_RMATCH = 0, + BFS_RNOMATCH = 1, +}; -static int __bfs(struct lock_list *source_entry, - void *data, - int (*match)(struct lock_list *entry, void *data), - struct lock_list **target_entry, - int forward) +/* + * bfs_result < 0 means error + */ + +static inline bool bfs_error(enum bfs_result res) +{ + return res < 0; +} + +static enum bfs_result __bfs(struct lock_list *source_entry, + void *data, + int (*match)(struct lock_list *entry, void *data), + struct lock_list **target_entry, + int forward) { struct lock_list *entry; struct list_head *head; struct circular_queue *cq = &lock_cq; - int ret = 1; + enum bfs_result ret = BFS_RNOMATCH; if (match(source_entry, data)) { *target_entry = source_entry; - ret = 0; + ret = BFS_RMATCH; goto exit; } @@ -1019,7 +1050,7 @@ static int __bfs(struct lock_list *source_entry, __cq_dequeue(cq, (unsigned long *)&lock); if (!lock->class) { - ret = -2; + ret = BFS_EINVALIDNODE; goto exit; } @@ -1036,12 +1067,12 @@ static int __bfs(struct lock_list *source_entry, mark_lock_accessed(entry, lock); if (match(entry, data)) { *target_entry = entry; - ret = 0; + ret = BFS_RMATCH; goto exit; } if (__cq_enqueue(cq, (unsigned long)entry)) { - ret = -1; + ret = BFS_EQUEUEFULL; goto exit; } cq_depth = __cq_get_elem_count(cq); @@ -1054,19 +1085,21 @@ static int __bfs(struct lock_list *source_entry, return ret; } -static inline int __bfs_forwards(struct lock_list *src_entry, - void *data, - int (*match)(struct lock_list *entry, void *data), - struct lock_list **target_entry) +static inline enum bfs_result +__bfs_forwards(struct lock_list *src_entry, + void *data, + int (*match)(struct lock_list *entry, void *data), + struct lock_list **target_entry) { return __bfs(src_entry, data, match, target_entry, 1); } -static inline int __bfs_backwards(struct lock_list *src_entry, - void *data, - int (*match)(struct lock_list *entry, void *data), - struct lock_list **target_entry) +static inline enum bfs_result +__bfs_backwards(struct lock_list *src_entry, + void *data, + int (*match)(struct lock_list *entry, void *data), + struct lock_list **target_entry) { return __bfs(src_entry, data, match, target_entry, 0); @@ -1299,13 +1332,13 @@ unsigned long lockdep_count_backward_deps(struct lock_class *class) /* * Prove that the dependency graph starting at can not - * lead to . Print an error and return 0 if it does. + * lead to . Print an error and return BFS_RMATCH if it does. */ -static noinline int +static noinline enum bfs_result check_noncircular(struct lock_list *root, struct lock_class *target, - struct lock_list **target_entry) + struct lock_list **target_entry) { - int result; + enum bfs_result result; debug_atomic_inc(nr_cyclic_checks); @@ -1314,11 +1347,11 @@ check_noncircular(struct lock_list *root, struct lock_class *target, return result; } -static noinline int +static noinline enum bfs_result check_redundant(struct lock_list *root, struct lock_class *target, struct lock_list **target_entry) { - int result; + enum bfs_result result; debug_atomic_inc(nr_redundant_checks); @@ -1347,15 +1380,12 @@ static inline int usage_match(struct lock_list *entry, void *bit) * * Return 0 if such a node exists in the subgraph, and put that node * into *@target_entry. - * - * Return 1 otherwise and keep *@target_entry unchanged. - * Return <0 on error. */ -static int +static enum bfs_result find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit, struct lock_list **target_entry) { - int result; + enum bfs_result result; debug_atomic_inc(nr_find_usage_forwards_checks); @@ -1367,18 +1397,12 @@ find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit, /* * Find a node in the backwards-direction dependency sub-graph starting * at @root->class that matches @bit. - * - * Return 0 if such a node exists in the subgraph, and put that node - * into *@target_entry. - * - * Return 1 otherwise and keep *@target_entry unchanged. - * Return <0 on error. */ -static int +static enum bfs_result find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit, struct lock_list **target_entry) { - int result; + enum bfs_result result; debug_atomic_inc(nr_find_usage_backwards_checks); @@ -1586,18 +1610,18 @@ check_usage(struct task_struct *curr, struct held_lock *prev, this.class = hlock_class(prev); ret = find_usage_backwards(&this, bit_backwards, &target_entry); - if (ret < 0) + if (bfs_error(ret)) return print_bfs_bug(ret); - if (ret == 1) - return ret; + if (ret == BFS_RNOMATCH) + return 1; that.parent = NULL; that.class = hlock_class(next); ret = find_usage_forwards(&that, bit_forwards, &target_entry1); - if (ret < 0) + if (bfs_error(ret)) return print_bfs_bug(ret); - if (ret == 1) - return ret; + if (ret == BFS_RNOMATCH) + return 1; return print_bad_irq_dependency(curr, &this, &that, target_entry, target_entry1, @@ -1834,10 +1858,10 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev, struct held_lock *next, int distance, struct stack_trace *trace, int (*save)(struct stack_trace *trace)) { - struct lock_list *uninitialized_var(target_entry); struct lock_list *entry; + enum bfs_result ret; struct lock_list this; - int ret; + struct lock_list *uninitialized_var(target_entry); /* * Prove that the new -> dependency would not @@ -1851,7 +1875,7 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev, this.class = hlock_class(next); this.parent = NULL; ret = check_noncircular(&this, hlock_class(prev), &target_entry); - if (unlikely(!ret)) { + if (unlikely(ret == BFS_RMATCH)) { if (!trace->entries) { /* * If @save fails here, the printing might trigger @@ -1862,7 +1886,7 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev, } return print_circular_bug(&this, target_entry, next, prev, trace); } - else if (unlikely(ret < 0)) + else if (unlikely(bfs_error(ret))) return print_bfs_bug(ret); if (!check_prev_add_irq(curr, prev, next)) @@ -1900,11 +1924,11 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev, this.class = hlock_class(prev); this.parent = NULL; ret = check_redundant(&this, hlock_class(next), &target_entry); - if (!ret) { + if (ret == BFS_RMATCH) { debug_atomic_inc(nr_redundant); return 2; } - if (ret < 0) + if (bfs_error(ret)) return print_bfs_bug(ret); @@ -2633,16 +2657,16 @@ static int check_usage_forwards(struct task_struct *curr, struct held_lock *this, enum lock_usage_bit bit, const char *irqclass) { - int ret; + enum bfs_result ret; struct lock_list root; struct lock_list *uninitialized_var(target_entry); root.parent = NULL; root.class = hlock_class(this); ret = find_usage_forwards(&root, bit, &target_entry); - if (ret < 0) + if (bfs_error(ret)) return print_bfs_bug(ret); - if (ret == 1) + if (ret == BFS_RNOMATCH) return ret; return print_irq_inversion_bug(curr, &root, target_entry, @@ -2657,17 +2681,17 @@ static int check_usage_backwards(struct task_struct *curr, struct held_lock *this, enum lock_usage_bit bit, const char *irqclass) { - int ret; + enum bfs_result ret; struct lock_list root; struct lock_list *uninitialized_var(target_entry); root.parent = NULL; root.class = hlock_class(this); ret = find_usage_backwards(&root, bit, &target_entry); - if (ret < 0) + if (bfs_error(ret)) return print_bfs_bug(ret); - if (ret == 1) - return ret; + if (ret == BFS_RNOMATCH) + return 1; return print_irq_inversion_bug(curr, &root, target_entry, this, 0, irqclass); -- 2.16.1