Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp992598pxb; Thu, 25 Feb 2021 22:53:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJwLTXw3q/l7FSfDBVZji9FMZEX3Hc1yZPKuyIJUcBDGeVYecRVbHazXpwrPW4gSfSCYT+0k X-Received: by 2002:a05:6402:2690:: with SMTP id w16mr1714029edd.304.1614322388494; Thu, 25 Feb 2021 22:53:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614322388; cv=none; d=google.com; s=arc-20160816; b=GI7/uTMD82F3A2tEqSjSJt69dqgKqKGQHVbrt6Lhfj33H2ZxwotSV6fP9EqOUC8OrM TXvHlaEFHt3RdXsmwSloS1xupMu9Lx2RxAtfHUd8PpoDa+4oEbD76Z9ci/zIhNadjENR MAu4zwSbg+90ADqYrkyKQEmqZj2xmdUps+btl/kcUpuSV92fE3mRal3YAndilleaq7Px XNJYZqs8Guf7YduuMQCRraVIOTZMC4wi2h0QgjAW6PDnCx8yU6/ExJEc/mpSPRF3Hgll QWkuHpbUzpkvh6LYQnPDNAhJ0e+I84rKguq4aERIIkLPEcLqoanl6nVF2T/N96NM0ael P5RA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ZclSCnDVWuNBbEMmwNgS3BfnWGAPHSCqg/pHQOtG8FI=; b=Koz9ckEy/gN73d0tRk3m0KQkUc+Y9vA+nK1a6wEkXMIBq/q9q0rUuNNjsaqK6kUHpT AdFWu55FgJ4krNoLM2y1suC5vYH8LIHpG7j/BLot0NqWRtOPV2V6yS+odH4Kn/Ire8L2 H9dSKsptCLcWZ+l4Wk/e0I6MNfesftVkKSYGpYfeuFtFTRyiNdlC+WOl9oube66aptNr HoRUkeA4eoEvSJM8jgq3IF/fLSTSKyFrIds7KdiCq+1OsFp1cofOweJpoocaS8UxI5Or Us579mKDC+LsbMo82gcZjjtGsWA+n7oqk8rHbgK4WlyFJDsrr1XMPoZeQkZE9MqwtkQI qpEA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dp8si4947764ejc.367.2021.02.25.22.52.45; Thu, 25 Feb 2021 22:53:08 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229618AbhBZGvS (ORCPT + 99 others); Fri, 26 Feb 2021 01:51:18 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:12583 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229537AbhBZGvR (ORCPT ); Fri, 26 Feb 2021 01:51:17 -0500 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Dn0by6MRJzMfMs; Fri, 26 Feb 2021 14:48:30 +0800 (CST) Received: from huawei.com (10.151.151.241) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.498.0; Fri, 26 Feb 2021 14:50:25 +0800 From: Luo Longjun To: , , CC: , , , , Subject: [PATCH v3] fs/locks: print full locks information Date: Thu, 25 Feb 2021 22:58:29 -0500 Message-ID: <685386c2840b76c49b060bf7dcea1fefacf18176.1614322182.git.luolongjun@huawei.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210221201024.GB15975@fieldses.org> References: <20210221201024.GB15975@fieldses.org> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.151.151.241] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit fd7732e033e3 ("fs/locks: create a tree of dependent requests.") has put blocked locks into a tree. So, with a for loop, we can't check all locks information. To solve this problem, we should traverse the tree. Signed-off-by: Luo Longjun --- fs/locks.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/fs/locks.c b/fs/locks.c index 99ca97e81b7a..ecaecd1f1b58 100644 --- a/fs/locks.c +++ b/fs/locks.c @@ -2828,7 +2828,7 @@ struct locks_iterator { }; static void lock_get_status(struct seq_file *f, struct file_lock *fl, - loff_t id, char *pfx) + loff_t id, char *pfx, int repeat) { struct inode *inode = NULL; unsigned int fl_pid; @@ -2844,7 +2844,11 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl, if (fl->fl_file != NULL) inode = locks_inode(fl->fl_file); - seq_printf(f, "%lld:%s ", id, pfx); + seq_printf(f, "%lld: ", id); + + if (repeat) + seq_printf(f, "%*s", repeat - 1 + (int)strlen(pfx), pfx); + if (IS_POSIX(fl)) { if (fl->fl_flags & FL_ACCESS) seq_puts(f, "ACCESS"); @@ -2906,21 +2910,64 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl, } } +static struct file_lock *get_next_blocked_member(struct file_lock *node) +{ + struct file_lock *tmp; + + /* NULL node or root node */ + if (node == NULL || node->fl_blocker == NULL) + return NULL; + + /* Next member in the linked list could be itself */ + tmp = list_next_entry(node, fl_blocked_member); + if (list_entry_is_head(tmp, &node->fl_blocker->fl_blocked_requests, fl_blocked_member) + || tmp == node) { + return NULL; + } + + return tmp; +} + static int locks_show(struct seq_file *f, void *v) { struct locks_iterator *iter = f->private; - struct file_lock *fl, *bfl; + struct file_lock *cur, *tmp; struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); + int level = 0; - fl = hlist_entry(v, struct file_lock, fl_link); + cur = hlist_entry(v, struct file_lock, fl_link); - if (locks_translate_pid(fl, proc_pidns) == 0) + if (locks_translate_pid(cur, proc_pidns) == 0) return 0; - lock_get_status(f, fl, iter->li_pos, ""); + /* View this crossed linked list as a binary tree, the first member of fl_blocked_requests + * is the left child of current node, the next silibing in fl_blocked_member is the + * right child, we can alse get the parent of current node from fl_blocker, so this + * question becomes traversal of a binary tree + */ + while (cur != NULL) { + if (level) + lock_get_status(f, cur, iter->li_pos, "-> ", level); + else + lock_get_status(f, cur, iter->li_pos, "", level); - list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member) - lock_get_status(f, bfl, iter->li_pos, " ->"); + if (!list_empty(&cur->fl_blocked_requests)) { + /* Turn left */ + cur = list_first_entry_or_null(&cur->fl_blocked_requests, + struct file_lock, fl_blocked_member); + level++; + } else { + /* Turn right */ + tmp = get_next_blocked_member(cur); + /* Fall back to parent node */ + while (tmp == NULL && cur->fl_blocker != NULL) { + cur = cur->fl_blocker; + level--; + tmp = get_next_blocked_member(cur); + } + cur = tmp; + } + } return 0; } @@ -2941,7 +2988,7 @@ static void __show_fd_locks(struct seq_file *f, (*id)++; seq_puts(f, "lock:\t"); - lock_get_status(f, fl, *id, ""); + lock_get_status(f, fl, *id, "", 0); } } -- 2.17.1