[ 8743.393379] ======================================================
[ 8743.393385] WARNING: possible circular locking dependency detected
[ 8743.393391] 6.4.0-rc1+ #11 Tainted: G           OE
[ 8743.393397] ------------------------------------------------------
[ 8743.393402] kworker/0:2/12921 is trying to acquire lock:
[ 8743.393408] 
ffff888127a14460 (sb_writers#8){.+.+}-{0:0}, at: ksmbd_vfs_setxattr+0x3d/0xd0 [ksmbd]
[ 8743.393510]
               but task is already holding lock:
[ 8743.393515] 
ffff8880360d97f0 (&type->i_mutex_dir_key#6/1){+.+.}-{3:3}, at: ksmbd_vfs_kern_path_locked+0x181/0x670 [ksmbd]
[ 8743.393618]
               which lock already depends on the new lock.
[ 8743.393623]
               the existing dependency chain (in reverse order) is:
[ 8743.393628]
               -> #1 (&type->i_mutex_dir_key#6/1){+.+.}-{3:3}:
[ 8743.393648]        down_write_nested+0x9a/0x1b0
[ 8743.393660]        filename_create+0x128/0x270
[ 8743.393670]        do_mkdirat+0xab/0x1f0
[ 8743.393680]        __x64_sys_mkdir+0x47/0x60
[ 8743.393690]        do_syscall_64+0x5d/0x90
[ 8743.393701]        entry_SYSCALL_64_after_hwframe+0x72/0xdc
[ 8743.393711]
               -> #0 (sb_writers#8){.+.+}-{0:0}:
[ 8743.393728]        __lock_acquire+0x2201/0x3b80
[ 8743.393737]        lock_acquire+0x18f/0x440
[ 8743.393746]        mnt_want_write+0x5f/0x240
[ 8743.393755]        ksmbd_vfs_setxattr+0x3d/0xd0 [ksmbd]
[ 8743.393839]        ksmbd_vfs_set_dos_attrib_xattr+0xcc/0x110 [ksmbd]
[ 8743.393924]        compat_ksmbd_vfs_set_dos_attrib_xattr+0x39/0x50 [ksmbd]
[ 8743.394010]        smb2_open+0x3432/0x3cc0 [ksmbd]
[ 8743.394099]        handle_ksmbd_work+0x2c9/0x7b0 [ksmbd]
[ 8743.394187]        process_one_work+0x65a/0xb30
[ 8743.394198]        worker_thread+0x2cf/0x700
[ 8743.394209]        kthread+0x1ad/0x1f0
[ 8743.394218]        ret_from_fork+0x29/0x50
This patch add mnt_want_write() above parent inode lock and remove
nested mnt_want_write calls in smb2_open().
Fixes: 40b268d384a2 ("ksmbd: add mnt_want_write to ksmbd vfs functions")
Cc: stable@vger.kernel.org
Reported-by: Marios Makassikis <mmakassikis@freebox.fr>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
Signed-off-by: Steve French <stfrench@microsoft.com>
                        rc = 0;
                } else {
                        rc = ksmbd_vfs_setxattr(idmap, path, attr_name, value,
-                                               le16_to_cpu(eabuf->EaValueLength), 0);
+                                               le16_to_cpu(eabuf->EaValueLength),
+                                               0, true);
                        if (rc < 0) {
                                ksmbd_debug(SMB,
                                            "ksmbd_vfs_setxattr is failed(%d)\n",
                return -EBADF;
        }
 
-       rc = ksmbd_vfs_setxattr(idmap, path, xattr_stream_name, NULL, 0, 0);
+       rc = ksmbd_vfs_setxattr(idmap, path, xattr_stream_name, NULL, 0, 0, false);
        if (rc < 0)
                pr_err("Failed to store XATTR stream name :%d\n", rc);
        return 0;
        da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
                XATTR_DOSINFO_ITIME;
 
-       rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_idmap(path->mnt), path, &da);
+       rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_idmap(path->mnt), path, &da, false);
        if (rc)
                ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
 }
            sizeof(struct create_sd_buf_req))
                return -EINVAL;
        return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
-                           le32_to_cpu(sd_buf->ccontext.DataLength), true);
+                           le32_to_cpu(sd_buf->ccontext.DataLength), true, false);
 }
 
 static void ksmbd_acls_fattr(struct smb_fattr *fattr,
                                                                    idmap,
                                                                    &path,
                                                                    pntsd,
-                                                                   pntsd_size);
+                                                                   pntsd_size,
+                                                                   false);
                                        kfree(pntsd);
                                        if (rc)
                                                pr_err("failed to store ntacl in xattr : %d\n",
        if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
                ksmbd_fd_set_delete_on_close(fp, file_info);
 
-       if (need_truncate) {
-               rc = smb2_create_truncate(&path);
-               if (rc)
-                       goto err_out;
-       }
-
        if (req->CreateContextsOffset) {
                struct create_alloc_size_req *az_req;
 
        }
 
 err_out:
-       if (file_present || created) {
-               inode_unlock(d_inode(parent_path.dentry));
-               path_put(&path);
-               path_put(&parent_path);
-       }
+       if (file_present || created)
+               ksmbd_vfs_kern_path_unlock(&parent_path, &path);
+
+       if (fp && need_truncate)
+               rc = smb2_create_truncate(&fp->filp->f_path);
+
        ksmbd_revert_fsids(work);
 err_out1:
        if (!rc) {
                rc = ksmbd_vfs_setxattr(file_mnt_idmap(fp->filp),
                                        &fp->filp->f_path,
                                        xattr_stream_name,
-                                       NULL, 0, 0);
+                                       NULL, 0, 0, true);
                if (rc < 0) {
                        pr_err("failed to store stream name in xattr: %d\n",
                               rc);
        if (rc)
                rc = -EINVAL;
 out:
-       if (file_present) {
-               inode_unlock(d_inode(parent_path.dentry));
-               path_put(&path);
-               path_put(&parent_path);
-       }
+       if (file_present)
+               ksmbd_vfs_kern_path_unlock(&parent_path, &path);
+
        if (!IS_ERR(link_name))
                kfree(link_name);
        kfree(pathname);
                da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
                        XATTR_DOSINFO_ITIME;
 
-               rc = ksmbd_vfs_set_dos_attrib_xattr(idmap, &filp->f_path, &da);
+               rc = ksmbd_vfs_set_dos_attrib_xattr(idmap, &filp->f_path, &da,
+                               true);
                if (rc)
                        ksmbd_debug(SMB,
                                    "failed to restore file attribute in EA\n");
        fp->saccess |= FILE_SHARE_DELETE_LE;
 
        return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
-                       buf_len, false);
+                       buf_len, false, true);
 }
 
 /**
 
                da.attr = le32_to_cpu(fp->f_ci->m_fattr);
                ret = ksmbd_vfs_set_dos_attrib_xattr(idmap,
-                                                    &fp->filp->f_path, &da);
+                                                    &fp->filp->f_path,
+                                                    &da, true);
                if (ret)
                        fp->f_ci->m_fattr = old_fattr;
        }
 
                        pntsd_size += sizeof(struct smb_acl) + nt_size;
                }
 
-               ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, pntsd_size);
+               ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, pntsd_size, false);
                kfree(pntsd);
        }
 
 
 int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
                 const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
-                bool type_check)
+                bool type_check, bool get_write)
 {
        int rc;
        struct smb_fattr fattr = {{0}};
        if (test_share_config_flag(tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) {
                /* Update WinACL in xattr */
                ksmbd_vfs_remove_sd_xattrs(idmap, path);
-               ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, ntsd_len);
+               ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, ntsd_len,
+                               get_write);
        }
 
 out:
 
                        __le32 *pdaccess, int uid);
 int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
                 const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
-                bool type_check);
+                bool type_check, bool get_write);
 void id_to_sid(unsigned int cid, uint sidtype, struct smb_sid *ssid);
 void ksmbd_init_domain(u32 *sub_auth);
 
 
                return -ENOENT;
        }
 
+       err = mnt_want_write(parent_path->mnt);
+       if (err) {
+               path_put(parent_path);
+               putname(filename);
+               return -ENOENT;
+       }
+
        inode_lock_nested(parent_path->dentry->d_inode, I_MUTEX_PARENT);
        d = lookup_one_qstr_excl(&last, parent_path->dentry, 0);
        if (IS_ERR(d))
 
 err_out:
        inode_unlock(d_inode(parent_path->dentry));
+       mnt_drop_write(parent_path->mnt);
        path_put(parent_path);
        putname(filename);
        return -ENOENT;
                                 fp->stream.name,
                                 (void *)stream_buf,
                                 size,
-                                0);
+                                0,
+                                true);
        if (err < 0)
                goto out;
 
                goto out_err;
        }
 
-       err = mnt_want_write(path->mnt);
-       if (err)
-               goto out_err;
-
        idmap = mnt_idmap(path->mnt);
        if (S_ISDIR(d_inode(path->dentry)->i_mode)) {
                err = vfs_rmdir(idmap, d_inode(parent), path->dentry);
                if (err)
                        ksmbd_debug(VFS, "unlink failed, err %d\n", err);
        }
-       mnt_drop_write(path->mnt);
 
 out_err:
        ksmbd_revert_fsids(work);
  * @attr_value:        xattr value to set
  * @attr_size: size of xattr value
  * @flags:     destination buffer length
+ * @get_write: get write access to a mount
  *
  * Return:     0 on success, otherwise error
  */
 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap,
                       const struct path *path, const char *attr_name,
-                      void *attr_value, size_t attr_size, int flags)
+                      void *attr_value, size_t attr_size, int flags,
+                      bool get_write)
 {
        int err;
 
-       err = mnt_want_write(path->mnt);
-       if (err)
-               return err;
+       if (get_write == true) {
+               err = mnt_want_write(path->mnt);
+               if (err)
+                       return err;
+       }
 
        err = vfs_setxattr(idmap,
                           path->dentry,
                           flags);
        if (err)
                ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
-       mnt_drop_write(path->mnt);
+       if (get_write == true)
+               mnt_drop_write(path->mnt);
        return err;
 }
 
        }
 
        if (!err) {
+               err = mnt_want_write(parent_path->mnt);
+               if (err) {
+                       path_put(path);
+                       path_put(parent_path);
+                       return err;
+               }
+
                err = ksmbd_vfs_lock_parent(parent_path->dentry, path->dentry);
                if (err) {
                        path_put(path);
        return err;
 }
 
+void ksmbd_vfs_kern_path_unlock(struct path *parent_path, struct path *path)
+{
+       inode_unlock(d_inode(parent_path->dentry));
+       mnt_drop_write(parent_path->mnt);
+       path_put(path);
+       path_put(parent_path);
+}
+
 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
                                          const char *name,
                                          unsigned int flags,
 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
                           struct mnt_idmap *idmap,
                           const struct path *path,
-                          struct smb_ntsd *pntsd, int len)
+                          struct smb_ntsd *pntsd, int len,
+                          bool get_write)
 {
        int rc;
        struct ndr sd_ndr = {0}, acl_ndr = {0};
 
        rc = ksmbd_vfs_setxattr(idmap, path,
                                XATTR_NAME_SD, sd_ndr.data,
-                               sd_ndr.offset, 0);
+                               sd_ndr.offset, 0, get_write);
        if (rc < 0)
                pr_err("Failed to store XATTR ntacl :%d\n", rc);
 
 
 int ksmbd_vfs_set_dos_attrib_xattr(struct mnt_idmap *idmap,
                                   const struct path *path,
-                                  struct xattr_dos_attrib *da)
+                                  struct xattr_dos_attrib *da,
+                                  bool get_write)
 {
        struct ndr n;
        int err;
                return err;
 
        err = ksmbd_vfs_setxattr(idmap, path, XATTR_NAME_DOS_ATTRIBUTE,
-                                (void *)n.data, n.offset, 0);
+                                (void *)n.data, n.offset, 0, get_write);
        if (err)
                ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
        kfree(n.data);
        }
        posix_state_to_acl(&acl_state, acls->a_entries);
 
-       rc = mnt_want_write(path->mnt);
-       if (rc)
-               goto out_err;
-
        rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls);
        if (rc < 0)
                ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
                        ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
                                    rc);
        }
-       mnt_drop_write(path->mnt);
 
-out_err:
        free_acl_state(&acl_state);
        posix_acl_release(acls);
        return rc;
                }
        }
 
-       rc = mnt_want_write(path->mnt);
-       if (rc)
-               goto out_err;
-
        rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls);
        if (rc < 0)
                ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
                        ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
                                    rc);
        }
-       mnt_drop_write(path->mnt);
 
-out_err:
        posix_acl_release(acls);
        return rc;
 }
 
                                int attr_name_len);
 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap,
                       const struct path *path, const char *attr_name,
-                      void *attr_value, size_t attr_size, int flags);
+                      void *attr_value, size_t attr_size, int flags,
+                      bool get_write);
 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
                                size_t *xattr_stream_name_size, int s_type);
 int ksmbd_vfs_remove_xattr(struct mnt_idmap *idmap,
 int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name,
                               unsigned int flags, struct path *parent_path,
                               struct path *path, bool caseless);
+void ksmbd_vfs_kern_path_unlock(struct path *parent_path, struct path *path);
 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
                                          const char *name,
                                          unsigned int flags,
 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
                           struct mnt_idmap *idmap,
                           const struct path *path,
-                          struct smb_ntsd *pntsd, int len);
+                          struct smb_ntsd *pntsd, int len,
+                          bool get_write);
 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
                           struct mnt_idmap *idmap,
                           struct dentry *dentry,
                           struct smb_ntsd **pntsd);
 int ksmbd_vfs_set_dos_attrib_xattr(struct mnt_idmap *idmap,
                                   const struct path *path,
-                                  struct xattr_dos_attrib *da);
+                                  struct xattr_dos_attrib *da,
+                                  bool get_write);
 int ksmbd_vfs_get_dos_attrib_xattr(struct mnt_idmap *idmap,
                                   struct dentry *dentry,
                                   struct xattr_dos_attrib *da);