From patchwork Mon Aug 3 21:30:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Eric W. Biederman" X-Patchwork-Id: 6933321 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 934349F38B for ; Mon, 3 Aug 2015 21:37:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 74D2B20601 for ; Mon, 3 Aug 2015 21:37:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 69C7C20634 for ; Mon, 3 Aug 2015 21:37:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755488AbbHCVht (ORCPT ); Mon, 3 Aug 2015 17:37:49 -0400 Received: from out03.mta.xmission.com ([166.70.13.233]:56091 "EHLO out03.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755177AbbHCVhs (ORCPT ); Mon, 3 Aug 2015 17:37:48 -0400 Received: from in01.mta.xmission.com ([166.70.13.51]) by out03.mta.xmission.com with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.82) (envelope-from ) id 1ZMNQZ-0007Yt-MT; Mon, 03 Aug 2015 15:37:47 -0600 Received: from 97-119-22-40.omah.qwest.net ([97.119.22.40] helo=x220.int.ebiederm.org.xmission.com) by in01.mta.xmission.com with esmtpsa (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.82) (envelope-from ) id 1ZMNQM-0000sM-7U; Mon, 03 Aug 2015 15:37:47 -0600 From: ebiederm@xmission.com (Eric W. Biederman) To: Linux Containers Cc: , Al Viro , Andy Lutomirski , "Serge E. Hallyn" , Richard Weinberger , Andrey Vagin , Jann Horn , Willy Tarreau , Omar Sandoval , Miklos Szeredi , Linus Torvalds , "J. Bruce Fields" References: <871tncuaf6.fsf@x220.int.ebiederm.org> <87mw5xq7lt.fsf@x220.int.ebiederm.org> <87a8yqou41.fsf_-_@x220.int.ebiederm.org> <874moq9oyb.fsf_-_@x220.int.ebiederm.org> <871tfkawu9.fsf_-_@x220.int.ebiederm.org> Date: Mon, 03 Aug 2015 16:30:54 -0500 In-Reply-To: <871tfkawu9.fsf_-_@x220.int.ebiederm.org> (Eric W. Biederman's message of "Mon, 03 Aug 2015 16:25:18 -0500") Message-ID: <8738009i0h.fsf_-_@x220.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 X-XM-AID: U2FsdGVkX18cX6EvfoM/yRVUZzOY2kY7waF56kFYXIc= X-SA-Exim-Connect-IP: 97.119.22.40 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-DCC: XMission; sa05 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Linux Containers X-Spam-Relay-Country: X-Spam-Timing: total 906 ms - load_scoreonly_sql: 0.06 (0.0%), signal_user_changed: 4.3 (0.5%), b_tie_ro: 3.2 (0.4%), parse: 3.0 (0.3%), extract_message_metadata: 14 (1.6%), get_uri_detail_list: 2.8 (0.3%), tests_pri_-1000: 7 (0.7%), tests_pri_-950: 1.34 (0.1%), tests_pri_-900: 1.07 (0.1%), tests_pri_-400: 39 (4.3%), check_bayes: 37 (4.1%), b_tokenize: 11 (1.3%), b_tok_get_all: 13 (1.4%), b_comp_prob: 3.8 (0.4%), b_tok_touch_all: 4.6 (0.5%), b_finish: 1.13 (0.1%), tests_pri_0: 828 (91.4%), tests_pri_500: 4.8 (0.5%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH review 6/6] vfs: Cache the results of path_connected X-SA-Exim-Version: 4.2.1 (built Wed, 24 Sep 2014 11:00:52 -0600) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a new field mnt_escape_count in nameidata, initialize it to 0 and cache the value of read_mnt_escape_count in nd->mnt_escape_count. This allows a single check in path_connected in the common case where either the mount has had no escapes (mnt_escape_count == 0) or there has been an escape and it has been validated that the current path does not escape. To keep the cache valid nd->mnt_escape_count must be set to 0 whenever the nd->path.mnt changes or when nd->path.dentry changes such that the connectedness of the previous value of nd->path.dentry does not imply the connected of the new value of nd->path.dentry. Various locations in fs/namei.c are updated to set nd->mnt_escape_count to 0 as necessary. Signed-off-by: "Eric W. Biederman" --- fs/namei.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index bccd3810ff60..79a5dca073f5 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -514,6 +514,7 @@ struct nameidata { struct nameidata *saved; unsigned root_seq; int dfd; + unsigned mnt_escape_count; }; static void set_nameidata(struct nameidata *p, int dfd, struct filename *name) @@ -572,12 +573,13 @@ static bool path_connected(struct nameidata *nd) struct vfsmount *mnt = nd->path.mnt; unsigned escape_count = read_mnt_escape_count(mnt); - if (likely(escape_count == 0)) + if (likely(escape_count == nd->mnt_escape_count)) return true; if (!is_subdir(nd->path.dentry, mnt->mnt_root)) return false; + cache_mnt_escape_count(&nd->mnt_escape_count, escape_count); return true; } @@ -840,6 +842,9 @@ static inline void path_to_nameidata(const struct path *path, if (nd->path.mnt != path->mnt) mntput(nd->path.mnt); } + if (unlikely((nd->path.mnt != path->mnt) || + (nd->path.dentry != path->dentry->d_parent))) + nd->mnt_escape_count = 0; nd->path.mnt = path->mnt; nd->path.dentry = path->dentry; } @@ -856,6 +861,7 @@ void nd_jump_link(struct path *path) nd->path = *path; nd->inode = nd->path.dentry->d_inode; nd->flags |= LOOKUP_JUMPED; + nd->mnt_escape_count = 0; } static inline void put_link(struct nameidata *nd) @@ -1040,6 +1046,7 @@ const char *get_link(struct nameidata *nd) nd->inode = nd->path.dentry->d_inode; } nd->flags |= LOOKUP_JUMPED; + nd->mnt_escape_count = 0; while (unlikely(*++res == '/')) ; } @@ -1335,6 +1342,7 @@ static int follow_dotdot_rcu(struct nameidata *nd) nd->path.mnt = &mparent->mnt; inode = inode2; nd->seq = seq; + nd->mnt_escape_count = 0; } } while (unlikely(d_mountpoint(nd->path.dentry))) { @@ -1348,6 +1356,7 @@ static int follow_dotdot_rcu(struct nameidata *nd) nd->path.dentry = mounted->mnt.mnt_root; inode = nd->path.dentry->d_inode; nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); + nd->mnt_escape_count = 0; } nd->inode = inode; return 0; @@ -1406,8 +1415,9 @@ EXPORT_SYMBOL(follow_down); /* * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() */ -static void follow_mount(struct path *path) +static bool follow_mount(struct path *path) { + bool followed = false; while (d_mountpoint(path->dentry)) { struct vfsmount *mounted = lookup_mnt(path); if (!mounted) @@ -1416,7 +1426,9 @@ static void follow_mount(struct path *path) mntput(path->mnt); path->mnt = mounted; path->dentry = dget(mounted->mnt_root); + followed = true; } + return followed; } static int follow_dotdot(struct nameidata *nd) @@ -1444,8 +1456,10 @@ static int follow_dotdot(struct nameidata *nd) } if (!follow_up(&nd->path)) break; + nd->mnt_escape_count = 0; } - follow_mount(&nd->path); + if (follow_mount(&nd->path)) + nd->mnt_escape_count = 0; nd->inode = nd->path.dentry->d_inode; return 0; } @@ -1997,6 +2011,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags) nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT; nd->depth = 0; nd->total_link_count = 0; + nd->mnt_escape_count = 0; if (flags & LOOKUP_ROOT) { struct dentry *root = nd->root.dentry; struct inode *inode = root->d_inode; @@ -3026,6 +3041,7 @@ static int do_last(struct nameidata *nd, unsigned seq; struct inode *inode; struct path save_parent = { .dentry = NULL, .mnt = NULL }; + unsigned save_parent_escape_count = 0; struct path path; bool retried = false; int error; @@ -3155,6 +3171,9 @@ finish_lookup: } else { save_parent.dentry = nd->path.dentry; save_parent.mnt = mntget(path.mnt); + save_parent_escape_count = nd->mnt_escape_count; + if (nd->path.dentry != path.dentry->d_parent) + nd->mnt_escape_count = 0; nd->path.dentry = path.dentry; } @@ -3227,6 +3246,7 @@ stale_open: BUG_ON(save_parent.dentry != dir); path_put(&nd->path); + nd->mnt_escape_count = save_parent_escape_count; nd->path = save_parent; nd->inode = dir->d_inode; save_parent.mnt = NULL;