Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp217961pxb; Thu, 21 Jan 2021 05:38:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJy02S8opR2pN3Gtinc5HoBRraeOzZMNngLz5FzkDiS99Jo4Bk2JOrAk3Mcr7o8WuUBEWXVu X-Received: by 2002:a05:6402:3510:: with SMTP id b16mr10757357edd.242.1611236285536; Thu, 21 Jan 2021 05:38:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611236285; cv=none; d=google.com; s=arc-20160816; b=wkcdwCSxEdDhzTamwfpREkhagIsbVVryiVl9JEsKNb3fugLXKFOYefiyA7n6dcez5L LhlW4Lrx9H2iesFfMeTgD4nC7zy/ahwjbxd+30z26uTwEqK7cg0H73fsEbaqnvPTdQE/ jHfJmwk6mERg2lZn8nrh6KiFhnXIJrUSaiAO0IIeE6CNJmVoLuyaLgnq1GDGK/dBzduo OHaWGQyt5nNjofJepnkQ/THFSaif2Sw+ioxw/c+54p3leHhugE5FjkIsXPa1Q6mZrY3I nUe4nkTBRafs6vZrcRmA8O298zivmka357iBvoCKeNpy/b+57RlbMc3xKihoAt2QLeWb Nsxw== 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=iPHhV48B0vripVq8vmdppWaJm77hEJOUMNbqOpK29yo=; b=QRQezLI8r6kYaAD+fscmf2ViMEHZYa9mX0s1a2J/eoI7dG0mdHz2FZjL34iT2DeBoa wPAzadS58SmXTDgLF+J5ZqaOMwuR54JZ2KX4UAf+YhCds7Yccc3eUoco6i6sG6wWVibi MK8ric9cSbmZVomct2sYhdhZxBunU2r9F82gfeAycJOB5OvP2qkW1bGXsJfi2psCHiL/ eysSKuinQB55+kGXeUOKUU1GEfhnS5sItcMES/kSUfiysNWgi1Ax4njpdI3kq+VnyY+f JVwqjPCvG2awwtvd9nc39oIfKSEMOFlnu3WraYQG1bBmrPcDxO1WEqyK+92ujKbDywFq mwyw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-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 d2si2089421edd.145.2021.01.21.05.37.41; Thu, 21 Jan 2021 05:38:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-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-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732137AbhAUNec (ORCPT + 99 others); Thu, 21 Jan 2021 08:34:32 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:55222 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732124AbhAUNcZ (ORCPT ); Thu, 21 Jan 2021 08:32:25 -0500 Received: from ip5f5af0a0.dynamic.kabel-deutschland.de ([95.90.240.160] helo=wittgenstein.fritz.box) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l2Zup-0005g7-G5; Thu, 21 Jan 2021 13:22:23 +0000 From: Christian Brauner To: Alexander Viro , Christoph Hellwig , linux-fsdevel@vger.kernel.org Cc: John Johansen , James Morris , Mimi Zohar , Dmitry Kasatkin , Stephen Smalley , Casey Schaufler , Arnd Bergmann , Andreas Dilger , OGAWA Hirofumi , Geoffrey Thomas , Mrunal Patel , Josh Triplett , Andy Lutomirski , Theodore Tso , Alban Crequy , Tycho Andersen , David Howells , James Bottomley , Seth Forshee , =?UTF-8?q?St=C3=A9phane=20Graber?= , Linus Torvalds , Aleksa Sarai , Lennart Poettering , "Eric W. Biederman" , smbarber@chromium.org, Phil Estes , Serge Hallyn , Kees Cook , Todd Kjos , Paul Moore , Jonathan Corbet , containers@lists.linux-foundation.org, linux-security-module@vger.kernel.org, linux-api@vger.kernel.org, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, linux-integrity@vger.kernel.org, selinux@vger.kernel.org, Christian Brauner Subject: [PATCH v6 29/40] namespace: take lock_mount_hash() directly when changing flags Date: Thu, 21 Jan 2021 14:19:48 +0100 Message-Id: <20210121131959.646623-30-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210121131959.646623-1-christian.brauner@ubuntu.com> References: <20210121131959.646623-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; i=+EOqvkSOxu9JCGW0Rc3vZBIUJjJSA6JSNVX4DwVrmRY=; m=jQ3B4I/0GzMv1ZZe0jmdtmfmOHRzatt37dzxqk6Qdsc=; p=Mp4MtJhhaaTxXITxZKDXxRqUdiEtcBdAeOB5TynuHHo=; g=8e89644d361e75b82524365516392cf4a074a9c9 X-Patch-Sig: m=pgp; i=christian.brauner@ubuntu.com; s=0x0x91C61BC06578DCA2; b=iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCYAl9pgAKCRCRxhvAZXjcog36AP9/Sa5 9EdObRqhxptULowV09x7R5qICtoXkclbKqjLwLwD+MBZ/eqJRlC/5CmIQTO+0gCtFyTfPUEmH584k 0VtBpAE= Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Changing mount options always ends up taking lock_mount_hash() but when MNT_READONLY is requested and neither the mount nor the superblock are MNT_READONLY we end up taking the lock, dropping it, and retaking it to change the other mount attributes. Instead, let's acquire the lock once when changing the mount attributes. This simplifies the locking in these codepath, makes them easier to reason about and avoids having to reacquire the lock right after dropping it. Link: https://lore.kernel.org/r/20210112220124.837960-2-christian.brauner@ubuntu.com Cc: David Howells Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Reviewed-by: Christoph Hellwig Signed-off-by: Christian Brauner --- /* v2 */ - Christoph Hellwig : - Remove pointless __mnt_unmake_readonly() helper. - Even though Christoph suggested to lockdep_assert_held() into places that require {lock,unlock}_mount_hash() it seems that seqlock's don't support it. /* v3 */ unchanged /* v4 */ unchanged /* v5 */ unchanged base-commit: 7c53f6b671f4aba70ff15e1b05148b10d58c2837 /* v6 */ unchanged base-commit: 19c329f6808995b142b3966301f217c831e7cf31 --- fs/namespace.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index ecdc63ef881c..bdfb130f2c3c 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -464,7 +464,6 @@ static int mnt_make_readonly(struct mount *mnt) { int ret = 0; - lock_mount_hash(); mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; /* * After storing MNT_WRITE_HOLD, we'll read the counters. This store @@ -498,18 +497,9 @@ static int mnt_make_readonly(struct mount *mnt) */ smp_wmb(); mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; - unlock_mount_hash(); return ret; } -static int __mnt_unmake_readonly(struct mount *mnt) -{ - lock_mount_hash(); - mnt->mnt.mnt_flags &= ~MNT_READONLY; - unlock_mount_hash(); - return 0; -} - int sb_prepare_remount_readonly(struct super_block *sb) { struct mount *mnt; @@ -2523,7 +2513,8 @@ static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags) if (readonly_request) return mnt_make_readonly(mnt); - return __mnt_unmake_readonly(mnt); + mnt->mnt.mnt_flags &= ~MNT_READONLY; + return 0; } /* @@ -2532,11 +2523,9 @@ static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags) */ static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags) { - lock_mount_hash(); mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; mnt->mnt.mnt_flags = mnt_flags; touch_mnt_namespace(mnt->mnt_ns); - unlock_mount_hash(); } static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt) @@ -2582,9 +2571,11 @@ static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags) return -EPERM; down_write(&sb->s_umount); + lock_mount_hash(); ret = change_mount_ro_state(mnt, mnt_flags); if (ret == 0) set_mount_attributes(mnt, mnt_flags); + unlock_mount_hash(); up_write(&sb->s_umount); mnt_warn_timestamp_expiry(path, &mnt->mnt); @@ -2625,8 +2616,11 @@ static int do_remount(struct path *path, int ms_flags, int sb_flags, err = -EPERM; if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) { err = reconfigure_super(fc); - if (!err) + if (!err) { + lock_mount_hash(); set_mount_attributes(mnt, mnt_flags); + unlock_mount_hash(); + } } up_write(&sb->s_umount); } -- 2.30.0