From patchwork Mon May 29 10:27:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Yu X-Patchwork-Id: 100203 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1413311vqr; Mon, 29 May 2023 03:36:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Z20MtrmJybzwLLkvaVQq/R4aAht953yTXJS5qy5o83aoXhN2VETgjl8jJXLsHwUGprFDG X-Received: by 2002:a17:90a:51c2:b0:256:33ba:8f5f with SMTP id u60-20020a17090a51c200b0025633ba8f5fmr9282146pjh.36.1685356563251; Mon, 29 May 2023 03:36:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685356563; cv=none; d=google.com; s=arc-20160816; b=o5ODt8y34r52kQdrSLxzcM8rjKRgb9FF6pRew6MrK3TZknZYwjebQNYYUQBAIba0w1 MFz8FwR/ve2SFt76/k7mCLC+BxbBoawQNaQIT1PpnzrEpMWoaMRU6swu6qs5f8Xrfpn5 QzVUCzk5pJpkSZEcyNKawcgsYm/OU8LmjqA/HFIRNa8O3Jjc6XFp1In03AQcXBM5RkFY jNx6aJKATMcLdA0ZIDAh0ytlpcD7V/IKNw6arwmD73WWZghlP8Bxr+AihgqokFrqKV0T hwzLcRsi/ORrw7bBUXLRLYaOFTT9Eiw/Ru27p4JiC1znMJ0s9vNLnY+dbSfe0peZgY/l irng== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=8p3IxKKvyciyXqNDAZWoatytAWdJ2r5FxZ6C28uwR4A=; b=Y+ZDpZK/LO8kCEwOtKa60hcqLGAZIU4sb57jv/QKAAxKBQjV5BmS7Cu15+LF5CIz0Q vriAHsP+8kxdTletGK2q6Ghs2MgxoIpeyLvbRBtGeeWPfYihY8QGQoJzf3uIRribJpxj 2RZUSZzCDfBxDVk0pFoBuMLSItq0OhMjlEF9b43N2qeyJ2Qul7+Bxe62mqKrJuTZVdpB O65PZk5j281jCVhM3BHYZw0mLqjQTxulx0pJD5laX5ZFgx+sTnskHbnbZb+PHkqXahUD hNI3g4n9NUwjDNGDtvYRVInW9iOHlf+71wSjR6CGxyHp0w8T/5AUY8QKHOwQhmtzq2y8 4e1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZzFpUyN1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id in1-20020a17090b438100b00256551043fdsi3720162pjb.72.2023.05.29.03.35.51; Mon, 29 May 2023 03:36:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZzFpUyN1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231726AbjE2K1T (ORCPT + 99 others); Mon, 29 May 2023 06:27:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231466AbjE2K1P (ORCPT ); Mon, 29 May 2023 06:27:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76F47C2 for ; Mon, 29 May 2023 03:27:14 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 156DC62340 for ; Mon, 29 May 2023 10:27:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3FBF9C433D2; Mon, 29 May 2023 10:27:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685356033; bh=2J0FBu1GLBGRIwQwggebk9BRVoGLrhgLuRFIuckJHHI=; h=From:To:Cc:Subject:Date:From; b=ZzFpUyN1Y0Elgj3/GK6QytCMNyzm0lSghJfxOy4B+QZ/2pqiGQ83xBbFFnHlXHiYA SMT8UDDbDer7ud8vybF0cqIpRgm2pXLgreUXkf0pH5Y9RlgQmUUmskM2cS82BuD/2w HOaUpc9Q7V0vhnlXtXpgNrxcs6moh/qGjifY6oWcerz6hcdnRwliMhH9r7DR0Hn9UW uOJ3rSuWyfktLvl1l9solrbXJBjU0brN3YP3nWKWZAvmWMTBC8d821h/aanUNUDpX8 J+woCbqvJ7iYeXfpg+B9JrlpktwQGGJIKPTq2r8jDzPxo1AhdszwXdFsno+IAj4AWz d/aXcc1S5PzGA== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu Subject: [PATCH] f2fs: fix to avoid mmap vs set_compress_option case Date: Mon, 29 May 2023 18:27:05 +0800 Message-Id: <20230529102705.2546993-1-chao@kernel.org> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767224443784209323?= X-GMAIL-MSGID: =?utf-8?q?1767224443784209323?= Compression option in inode should not be changed after they have been used, however, it may happen in below race case: Thread A Thread B - f2fs_ioc_set_compress_option - check f2fs_is_mmap_file() - check get_dirty_pages() - check F2FS_HAS_BLOCKS() - f2fs_file_mmap - set_inode_flag(FI_MMAP_FILE) - fault - do_page_mkwrite - f2fs_vm_page_mkwrite - f2fs_get_block_locked - fault_dirty_shared_page - set_page_dirty - update i_compress_algorithm - update i_log_cluster_size - update i_cluster_size Avoid such race condition by covering f2fs_file_mmap() w/ inode lock. Fixes: e1e8debec656 ("f2fs: add F2FS_IOC_SET_COMPRESS_OPTION ioctl") Signed-off-by: Chao Yu --- fs/f2fs/file.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 7b097ab2f5e4..685ded62fc28 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -519,17 +519,25 @@ static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) { struct inode *inode = file_inode(file); + int ret = 0; if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) return -EIO; - if (!f2fs_is_compress_backend_ready(inode)) - return -EOPNOTSUPP; + inode_lock(inode); + + if (!f2fs_is_compress_backend_ready(inode)) { + ret = -EOPNOTSUPP; + goto out_unlock; + } file_accessed(file); vma->vm_ops = &f2fs_file_vm_ops; set_inode_flag(inode, FI_MMAP_FILE); - return 0; + +out_unlock: + inode_unlock(inode); + return ret; } static int f2fs_file_open(struct inode *inode, struct file *filp)