• Eric Biggers's avatar
    f2fs: use fscrypt_prepare_new_inode() and fscrypt_set_context() · e075b690
    Eric Biggers authored
    Convert f2fs to use the new functions fscrypt_prepare_new_inode() and
    fscrypt_set_context().  This avoids calling
    fscrypt_get_encryption_info() from under f2fs_lock_op(), which can
    deadlock because fscrypt_get_encryption_info() isn't GFP_NOFS-safe.
    
    For more details about this problem, see the earlier patch
    "fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()".
    
    This also fixes a f2fs-specific deadlock when the filesystem is mounted
    with '-o test_dummy_encryption' and a file is created in an unencrypted
    directory other than the root directory:
    
        INFO: task touch:207 blocked for more than 30 seconds.
              Not tainted 5.9.0-rc4-00099-g729e3d09 #2
        "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
        task:touch           state:D stack:    0 pid:  207 ppid:   167 flags:0x00000000
        Call Trace:
         [...]
         lock_page include/linux/pagemap.h:548 [inline]
         pagecache_get_page+0x25e/0x310 mm/filemap.c:1682
         find_or_create_page include/linux/pagemap.h:348 [inline]
         grab_cache_page include/linux/pagemap.h:424 [inline]
         f2fs_grab_cache_page fs/f2fs/f2fs.h:2395 [inline]
         f2fs_grab_cache_page fs/f2fs/f2fs.h:2373 [inline]
         __get_node_page.part.0+0x39/0x2d0 fs/f2fs/node.c:1350
         __get_node_page fs/f2fs/node.c:35 [inline]
         f2fs_get_node_page+0x2e/0x60 fs/f2fs/node.c:1399
         read_inline_xattr+0x88/0x140 fs/f2fs/xattr.c:288
         lookup_all_xattrs+0x1f9/0x2c0 fs/f2fs/xattr.c:344
         f2fs_getxattr+0x9b/0x160 fs/f2fs/xattr.c:532
         f2fs_get_context+0x1e/0x20 fs/f2fs/super.c:2460
         fscrypt_get_encryption_info+0x9b/0x450 fs/crypto/keysetup.c:472
         fscrypt_inherit_context+0x2f/0xb0 fs/crypto/policy.c:640
         f2fs_init_inode_metadata+0xab/0x340 fs/f2fs/dir.c:540
         f2fs_add_inline_entry+0x145/0x390 fs/f2fs/inline.c:621
         f2fs_add_dentry+0x31/0x80 fs/f2fs/dir.c:757
         f2fs_do_add_link+0xcd/0x130 fs/f2fs/dir.c:798
         f2fs_add_link fs/f2fs/f2fs.h:3234 [inline]
         f2fs_create+0x104/0x290 fs/f2fs/namei.c:344
         lookup_open.isra.0+0x2de/0x500 fs/namei.c:3103
         open_last_lookups+0xa9/0x340 fs/namei.c:3177
         path_openat+0x8f/0x1b0 fs/namei.c:3365
         do_filp_open+0x87/0x130 fs/namei.c:3395
         do_sys_openat2+0x96/0x150 fs/open.c:1168
         [...]
    
    That happened because f2fs_add_inline_entry() locks the directory
    inode's page in order to add the dentry, then f2fs_get_context() tries
    to lock it recursively in order to read the encryption xattr.  This
    problem is specific to "test_dummy_encryption" because normally the
    directory's fscrypt_info would be set up prior to
    f2fs_add_inline_entry() in order to encrypt the new filename.
    
    Regardless, the new design fixes this test_dummy_encryption deadlock as
    well as potential deadlocks with fs reclaim, by setting up any needed
    fscrypt_info structs prior to taking so many locks.
    
    The test_dummy_encryption deadlock was reported by Daniel Rosenberg.
    Reported-by: default avatarDaniel Rosenberg <drosen@google.com>
    Acked-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
    Link: https://lore.kernel.org/r/20200917041136.178600-5-ebiggers@kernel.orgSigned-off-by: default avatarEric Biggers <ebiggers@google.com>
    e075b690
namei.c 30.6 KB