From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6BF2C433FE for ; Mon, 7 Dec 2020 11:33:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 301E423340 for ; Mon, 7 Dec 2020 11:33:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 301E423340 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B5A208D0009; Mon, 7 Dec 2020 06:33:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B36E68D0001; Mon, 7 Dec 2020 06:33:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D3858D0009; Mon, 7 Dec 2020 06:33:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0091.hostedemail.com [216.40.44.91]) by kanga.kvack.org (Postfix) with ESMTP id 87C9C8D0001 for ; Mon, 7 Dec 2020 06:33:47 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 48A428249980 for ; Mon, 7 Dec 2020 11:33:47 +0000 (UTC) X-FDA: 77566276494.28.debt40_05002e3273de Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin28.hostedemail.com (Postfix) with ESMTP id 1C65F6D64 for ; Mon, 7 Dec 2020 11:33:47 +0000 (UTC) X-HE-Tag: debt40_05002e3273de X-Filterd-Recvd-Size: 6221 Received: from mail-pj1-f67.google.com (mail-pj1-f67.google.com [209.85.216.67]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Mon, 7 Dec 2020 11:33:46 +0000 (UTC) Received: by mail-pj1-f67.google.com with SMTP id o7so7271957pjj.2 for ; Mon, 07 Dec 2020 03:33:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eX1IR50aPFiEwdJQV5xGne8H/FATcvY6dG1G5D19yRg=; b=eXbX382qovy7COikmy0KYqgAQma7SSrycd5mwQ9J3QDSquh0VXtGUzblKKkJuAAuVI PHSVAom05MaCyxQFaVh4X0uD1ku5P8agDSw+IP8A/Cg85jWOn4rl+hXVuErT69iZK05Z AcBFV5C+p5lDBPuGmBkyAuL5TR4/MK5NGKvZAxWYLkqPklUmSTDRcAnTp3+KTbgnYNva U4uPsEDUiXFj3Bp/DPp8dqvmHNJAQNKovFWzqT1K4nBuHDOyV8p/E0E60YvnLz8urUmn JGkze+2OWmWXsKpBGQx87ndhmBmG+X4sOrdozDV9Gp1n+V+72T9SRvDWV3Jt7aMnyj1a mpgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eX1IR50aPFiEwdJQV5xGne8H/FATcvY6dG1G5D19yRg=; b=cjftd+pVVCsMpG/3PLZpjMBGVvOIfKZTS3CH74DC33CmLxS2J9YWxRMHYLxHb54LeA CkKyu9SAIwm/rFtQv6BTvnxC5TfeuVsPkVvFl9f0YKBfIaohrdg7MT2RBsOTzrJvCBKM NUchwm4auta1+azNl+HwtZDHBLS94wXRwrdg0JOiMIWPzyDsu26Dny7D1N980ga69FSF XTxyzrh8OuVE0Qk2bL42hiJXuXnPHc2oUd94ui2Bufd3Di4LZYsLaP4cxHQEoRO2Bsm8 ZIdbyAX/6gr9dJeCilzWjM6Vkqya8fq5FFM0NQKLMm2QlI1Q19JDphwZYX1vqeFdPG1O K7Eg== X-Gm-Message-State: AOAM531t24TstezgHk/EMkc0AESr7FrR+w3LaKF/JLzUjrwHO7jW/0Rb Kiquct6C9VqkaeVyyfrQUxjYiRuYlX0= X-Google-Smtp-Source: ABdhPJwWiUXd//0TI6YPXtELG5n3boi+qEdZaWxYAMBrmQrPfGNew3+nfNYMRr95hadFZhb2SRQXRw== X-Received: by 2002:a17:90a:b38d:: with SMTP id e13mr16560118pjr.214.1607340825515; Mon, 07 Dec 2020 03:33:45 -0800 (PST) Received: from localhost.localdomain ([203.205.141.39]) by smtp.gmail.com with ESMTPSA id d4sm14219822pfo.127.2020.12.07.03.33.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Dec 2020 03:33:45 -0800 (PST) From: yulei.kernel@gmail.com X-Google-Original-From: yuleixzhang@tencent.com To: linux-mm@kvack.org, akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, naoya.horiguchi@nec.com, viro@zeniv.linux.org.uk, pbonzini@redhat.com Cc: joao.m.martins@oracle.com, rdunlap@infradead.org, sean.j.christopherson@intel.com, xiaoguangrong.eric@gmail.com, kernellwp@gmail.com, lihaiwei.kernel@gmail.com, Yulei Zhang , Xiao Guangrong Subject: [RFC V2 06/37] dmemfs: support truncating inode down Date: Mon, 7 Dec 2020 19:30:59 +0800 Message-Id: X-Mailer: git-send-email 2.28.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Yulei Zhang To support cut inode down, it will introduce the race between page fault handler and truncating handler as the entry to be deleted is being mapped into process's VMA in order to make page fault faster (as it's the hot path), we use RCU to sync these two handlers. When inode's size is updated, the handler makes sure the new size is visible to page fault handler who will not use truncated entry anymore and will not create new entry in that region Signed-off-by: Xiao Guangrong Signed-off-by: Yulei Zhang --- fs/dmemfs/inode.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++= +++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/fs/dmemfs/inode.c b/fs/dmemfs/inode.c index 7b6e51d..9ec62dc 100644 --- a/fs/dmemfs/inode.c +++ b/fs/dmemfs/inode.c @@ -98,8 +98,73 @@ static int dmemfs_mkdir(struct inode *dir, struct dent= ry *dentry, .rename =3D simple_rename, }; =20 +static void inode_drop_dpages(struct inode *inode, loff_t start, loff_t = end); + +static int dmemfs_truncate(struct inode *inode, loff_t newsize) +{ + struct super_block *sb =3D inode->i_sb; + loff_t current_size; + + if (newsize & ((1 << sb->s_blocksize_bits) - 1)) + return -EINVAL; + + current_size =3D i_size_read(inode); + i_size_write(inode, newsize); + + if (newsize >=3D current_size) + return 0; + + /* it cuts the inode down */ + + /* + * we should make sure inode->i_size has been updated before + * unmapping and dropping radix entries, so that other sides + * can not create new i_mapping entry beyond inode->i_size + * and the radix entry in the truncated region is not being + * used + * + * see the comments in dmemfs_fault() + */ + synchronize_rcu(); + + /* + * should unmap all mapping first as dmem pages are freed in + * inode_drop_dpages() + * + * after that, dmem page in the truncated region is not used + * by any process + */ + unmap_mapping_range(inode->i_mapping, newsize, 0, 1); + + inode_drop_dpages(inode, newsize, LLONG_MAX); + return 0; +} + +/* + * same logic as simple_setattr but we need to handle ftruncate + * carefully as we inserted self-defined entry into radix tree + */ +static int dmemfs_setattr(struct dentry *dentry, struct iattr *iattr) +{ + struct inode *inode =3D dentry->d_inode; + int error; + + error =3D setattr_prepare(dentry, iattr); + if (error) + return error; + + if (iattr->ia_valid & ATTR_SIZE) { + error =3D dmemfs_truncate(inode, iattr->ia_size); + if (error) + return error; + } + setattr_copy(inode, iattr); + mark_inode_dirty(inode); + return 0; +} + static const struct inode_operations dmemfs_file_inode_operations =3D { - .setattr =3D simple_setattr, + .setattr =3D dmemfs_setattr, .getattr =3D simple_getattr, }; =20 --=20 1.8.3.1