Received: by 2002:a05:7412:e79e:b0:f3:1519:9f41 with SMTP id o30csp141754rdd; Wed, 22 Nov 2023 11:31:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IHyWeD9g5E0MC6mikvHBEqwuXLxQGEd9W0yXSnCm0hAtjm7I2Ha2tiq+ISaLV/mlUZeZTxU X-Received: by 2002:a05:6a20:da98:b0:187:71f7:bb26 with SMTP id iy24-20020a056a20da9800b0018771f7bb26mr3623864pzb.54.1700681469662; Wed, 22 Nov 2023 11:31:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700681469; cv=none; d=google.com; s=arc-20160816; b=VBurA8nm/NIjgKHcEoC5xZGmDCBu1fheu2cj11oPZ+VKazs0vqYbBSGeW5oolx2bq1 8LUtHWAfiBCknk4fcXf16m7XfuX2Gt0eveAnOKSmpKKfBF7dcZ1ix7LaeJsnVdC7wozc Xbry2r7895x13uREQ5wrnxH70nzuasT4E/60RTVlF3r8Qwf2kLVwnnhYRmhLabEv6QyK K0vXhcKu9Mxj316l4D2Qe/sFVairve+aAKvx2Ci12tAAIY1XOzFKs5pnMv5Waxuy0Y6T /TZGpFPTlbu0RukEyO1J/JM3AJOWIJjlvoJi76p4mCKH1nAF+N5KCjYIGl+anw2ZKJ7n YzoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature; bh=p1irU5/sMV7m/y1pOMYTaih3tLiq5FQfX+AGXmsO5cU=; fh=W6BEtqt7iV8phMzKg0C2MCZmXBORzGPTeuIhW3f8iqY=; b=Zsc5zPP0L6W63mIlfDXeJ474d+UU4ZMpQbRb39rSWx3Jim9amuGOs5aHJU8x74dCh5 e96PPKqQpUkasDJ3Lp0+HtSPpZ+A5O7uuZngl/WYHN5Qk0yeLZQhiyDEbKwpD8Fz5OzG +tTXShK/LglxwmE0nz5ntE0AEJuXtU2pDbOBSANehXurwMdKXYtwCFGIWNMtlbmBbVLK /mQ3quvVsRrNTLOqYmjrLFoclzYjsNjMZb9732NU2ElQ+D0fEMJct5pX8RAwfXDv/Mc9 dmDNBE0mH3RaxgZmFW5jcMcyeA99W9qw8gw6i30Nprz/y/9eSDw9DkIyYOhM/fY2xT1Q byRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=VRmy3WrW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id a1-20020a170902ee8100b001c7347e993esi88732pld.17.2023.11.22.11.31.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 11:31:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=VRmy3WrW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 2D05B803D00F; Wed, 22 Nov 2023 11:30:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232009AbjKVTah (ORCPT + 99 others); Wed, 22 Nov 2023 14:30:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231430AbjKVTag (ORCPT ); Wed, 22 Nov 2023 14:30:36 -0500 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [IPv6:2a03:a000:7:0:5054:ff:fe1c:15ff]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E742D18E; Wed, 22 Nov 2023 11:30:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:Content-Type:MIME-Version: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To:References; bh=p1irU5/sMV7m/y1pOMYTaih3tLiq5FQfX+AGXmsO5cU=; b=VRmy3WrW8+pNBsWOfDxL2s3pUK 4ILG+s+rBsn3aSTA4U1blwa1fJXQ+ZpkggvOqaLfnADIYbA87Qo4cFraQgYStoZ/lRSxvQ56/ViFA tCJ0VsCWD8U6HO/zCG6BoGOHVXXg7UfUZFLw1KHk1lOY6q38c1bk0bhWCJJCVxCocMYr/ZeVL9CvP /hblKokHWEGibFcKO3nYnnYX4HTr3SYdR2YSu+lx46CO/muf7KYlc+2lEjIC+Vz0uTEWod/bUGBhm g5JXG1aR/Z+kMTRenMZ29fJzTDZvhrnci4xFcCfnc+DvsmjoE74Ts5m8XPsCiiTBDtIgYaqbHw6CM i1ZsnxGA==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.96 #2 (Red Hat Linux)) id 1r5svc-001ksS-1G; Wed, 22 Nov 2023 19:30:28 +0000 Date: Wed, 22 Nov 2023 19:30:28 +0000 From: Al Viro To: linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Mo Zou , Jan Kara , linux-kernel@vger.kernel.org Subject: [PATCHES][CFT] rename deadlock fixes Message-ID: <20231122193028.GE38156@ZenIV> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: Al Viro X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 22 Nov 2023 11:30:50 -0800 (PST) Directory locking used to provide the following warranties: 1. Any read operations (lookup, readdir) are done with directory locked at least shared. 2. Any link creation or removal is done with directory locked exclusive. 3. Any link count changes are done with the object locked exclusive. 4. Any emptiness checks (for rmdir() or overwriting rename()) are done with the victim locked exclusive. 5. Any rename of a non-directory is done with the object locked exclusive (the last part is needed by nfsd). As far as directory contents is concerned, it very nearly amounted to "all reads are done with directory locked shared, all modifications - exclusive". There had been one gap in that, though - rename() can change the parent of subdirectory and strictly speaking that does modify the contents - ".." entry might need to be altered to match the new parent. For almost all filesystems it posed no problem - location and representation of ".." entry is fs-dependent, but it tends to be unaffected by any other directory modifications. However, in some cases it's not true - for example, a filesystem might have the contents of small directories kept directly in the inode, switched to separate allocation when enough entries are added. For such beasts we need an exclusion between modifying ".." and (at least) switchover from small to large directory format. One solution would be an fs-private locking inside the method, another - having cross-directory ->rename() take the normal lock on directory being moved. Or one could make vfs_rename() itself lock that directory instead, sparing the ->rename() instances all that headache. That had been done in 6.5; unfortunately, locking the moved subdirectory had been done in *all* cases, cross-directory or not. And that turns out to be more than a bit of harmless overlocking - deadlock prevention relies upon the fact that we never lock two directories that are not descendents of each other without holding ->s_vfs_rename_mutex. Kudos to Mo Zou for pointing to the holes in proof of correctness - that's what uncovered the problem... We could revert to pre-6.5 locking scheme, but there's a less painful solution; the cause of problem is same-directory case and in those there's no reason for ->rename() to touch the ".." entry at all - the parent does not change, so the modification of ".." would be tautological. Let's keep locking moved subdirectory in cross-directory move; that spares ->rename() instances the need to do home-grown exclusion. They need to be careful in one respect - if they do rely upon the exclusion between the change of ".." and other directory modifications, they should only touch ".." if the parent does get changed. Exclusion is still provided by the caller for such (cross-directory) renames. The series lives in git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.git #work.rename; individual patches in followups. It does surivive local beating, but it needs more - additional review and testing would be very welcome. It starts with making sure that ->rename() instances are careful. Then the locking rules for rename get changed, so that we don't lock moved subdirectory in same-directory case. The proof of correctness gets updated^Wfixed - the current one had several holes. 1/9..6/9) (me and Jan) don't do tautological ".." changes in instances. reiserfs: Avoid touching renamed directory if parent does not change ocfs2: Avoid touching renamed directory if parent does not change udf_rename(): only access the child content on cross-directory rename ext2: Avoid reading renamed directory if parent does not change ext4: don't access the source subdirectory content on same-directory rename f2fs: Avoid reading renamed directory if parent does not change 7/9) rename(): fix the locking of subdirectories We should never lock two subdirectories without having taken ->s_vfs_rename_mutex; inode pointer order or not, the "order" proposed in 28eceeda130f "fs: Lock moved directories" is not transitive, with the usual consequences. The rationale for locking renamed subdirectory in all cases was the possibility of race between rename modifying .. in a subdirectory to reflect the new parent and another thread modifying the same subdirectory. For a lot of filesystems that's not a problem, but for some it can lead to trouble (e.g. the case when short directory contents is kept in the inode, but creating a file in it might push it across the size limit and copy its contents into separate data block(s)). However, we need that only in case when the parent does change - otherwise ->rename() doesn't need to do anything with .. entry in the first place. Some instances are lazy and do a tautological update anyway, but it's really not hard to avoid. Amended locking rules for rename(): find the parent(s) of source and target if source and target have the same parent lock the common parent else lock ->s_vfs_rename_mutex lock both parents, in ancestor-first order; if neither is an ancestor of another, lock the parent of source first. find the source and target. if source and target have the same parent if operation is an overwriting rename of a subdirectory lock the target subdirectory else if source is a subdirectory lock the source if target is a subdirectory lock the target lock non-directories involved, in inode pointer order if both source and target are such. That way we are guaranteed that parents are locked (for obvious reasons), that any renamed non-directory is locked (nfsd relies upon that), that any victim is locked (emptiness check needs that, among other things) and subdirectory that changes parent is locked (needed to protect the update of .. entries). We are also guaranteed that any operation locking more than one directory either takes ->s_vfs_rename_mutex or locks a parent followed by its child. 8/9) kill lock_two_inodes() Folded into the sole caller and simplified - it doesn't need to deal with the mix of directories and non-directories anymore. 9/9) rename(): avoid a deadlock in the case of parents having no common ancestor ... and fix the directory locking documentation and proof of correctness. Holding ->s_vfs_rename_mutex *almost* prevents ->d_parent changes; the case where we really don't want it is splicing the root of disconnected tree to somewhere. In other words, ->s_vfs_rename_mutex is sufficient to stabilize "X is an ancestor of Y" only if X and Y are already in the same tree. Otherwise it can go from false to true, and one can construct a deadlock on that. Make lock_two_directories() report an error in such case and update the callers of lock_rename()/lock_rename_child() to handle such errors. The ones that could get an error, that is - e.g. debugfs_rename() is never asked to change the parent and shouldn't be using lock_rename() in the first place; that's a separate series, though.