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=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL 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 42787C4338F for ; Fri, 30 Jul 2021 08:10:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DCF64603E9 for ; Fri, 30 Jul 2021 08:10:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DCF64603E9 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 5BA7D8D0002; Fri, 30 Jul 2021 04:10:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 542DC8D0001; Fri, 30 Jul 2021 04:10:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 40A6F8D0002; Fri, 30 Jul 2021 04:10:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0123.hostedemail.com [216.40.44.123]) by kanga.kvack.org (Postfix) with ESMTP id 23BCA8D0001 for ; Fri, 30 Jul 2021 04:10:01 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id C5F8018149036 for ; Fri, 30 Jul 2021 08:10:00 +0000 (UTC) X-FDA: 78418530960.19.A6293D6 Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) by imf20.hostedemail.com (Postfix) with ESMTP id 8AA33D00584C for ; Fri, 30 Jul 2021 08:10:00 +0000 (UTC) Received: by mail-qk1-f175.google.com with SMTP id 129so8658431qkg.4 for ; Fri, 30 Jul 2021 01:10:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=iO85ZMC6Pth+31xPvVbOO8VKXEmUR+jk3X7hFax1vLE=; b=gL63kdkFN7iyXAPotPB2doPP4eoWZKnl2sfroPBPAmL0gzVw5Upydd4gfixGG7s4WC giXC1QKX0/mEwYnfv2DX5x83kim9pV5K+Gm8NMUXleRvZrKLiKonmPtcs7J3ioWqHe6F Ckfr9iXkkJcyYmswDgsxzVxSxBIysXhiyCXSKrZy8vVuiXmWrD2T+1NhJYm15q+9DkgU XwXZEF2H4svRElS8LTbSdUa4RDEDk42LvlEa7OxioJzfqS/+4IOmq0XGsfWVqB2JgtX/ zTuwoWGUPFo1/OuUj+lNosYVHJAFcNXFHYHIbWeDozDiEk7Ux5ckytcnsj1H/ae+IAiB bTCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=iO85ZMC6Pth+31xPvVbOO8VKXEmUR+jk3X7hFax1vLE=; b=EFl0iOo2dVNtYpbpLnWg/tDlHOGvdtpsTyY/bZfhQUvOUNGGUW4UMBFdYxWst7PvAq kP9UfdIjPzyhHQzJMBe8r30YmqtWF+evizErQ4F47RTxKuDjWqFwa/wA7BOyegXxdopA O977XuUYT+lnTARnOlmtgWcO43WpL4MBgP2N+dW3W5wrR0CfI0o8hMIAdYAh5ESzEphE cppUUJsEmy9uDIdsSqpFvh7peWrdDFEiNVvVqI3JFo5JxhX6BO7f3PQYqPDT//AvYdLa 3ie36u/+LrRmry2zEJSBYJ9MKT1PA8Sc+xmKF74E5PTf8qREV0kwqC07BtQ18Xx658dk VPOg== X-Gm-Message-State: AOAM532oj6DBOJ6Lb2eNSWDyKHEJI1HEcR3KmCigJuDTKBZ1LHdBFcgx JxthGUn2nXx52s8YptCkoqXwEA== X-Google-Smtp-Source: ABdhPJzxLMkX99MbqShrEgAY7r7lr2pFJKeO21G4hsIJ29P9sxgcQLfSXm9uBiPHU0BoiYUpJA2ihQ== X-Received: by 2002:ae9:e90e:: with SMTP id x14mr1119707qkf.118.1627632599619; Fri, 30 Jul 2021 01:09:59 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id w71sm571098qkb.67.2021.07.30.01.09.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jul 2021 01:09:58 -0700 (PDT) Date: Fri, 30 Jul 2021 01:09:56 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.anvils To: Andrew Morton cc: Hugh Dickins , Shakeel Butt , "Kirill A. Shutemov" , Yang Shi , Miaohe Lin , Mike Kravetz , Michal Hocko , Rik van Riel , Christoph Hellwig , Matthew Wilcox , "Eric W. Biederman" , Alexey Gladkov , Chris Wilson , Matthew Auld , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 15/16] tmpfs: permit changing size of memlocked file In-Reply-To: <2862852d-badd-7486-3a8e-c5ea9666d6fb@google.com> Message-ID: References: <2862852d-badd-7486-3a8e-c5ea9666d6fb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=gL63kdkF; spf=pass (imf20.hostedemail.com: domain of hughd@google.com designates 209.85.222.175 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam02 X-Stat-Signature: coiiej539jythd3wepdrahwq6m9rfdmi X-Rspamd-Queue-Id: 8AA33D00584C X-HE-Tag: 1627632600-518618 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: We have users who change the size of their memlocked file by F_MEM_UNLOCK, ftruncate, F_MEM_LOCK. That risks swapout in between, and is distasteful: particularly if the file is very large (when shmem_unlock_mapping() has a lot of work to move pages off the Unevictable list, only for them to be moved back there later on). Modify shmem_setattr() to grow or shrink, and shmem_fallocate() to grow, the locked extent. But forbid (EPERM) both if current_ucounts() differs from the locker's mlock_ucounts (without even a CAP_IPC_LOCK override). They could be permitted (the caller already has unsealed write access), but it's probably less confusing to restrict size change to the locker. But leave shmem_write_begin() as is, preventing the memlocked file from being extended implicitly by writes beyond EOF: I think that it's best to demand an explicit size change, by truncate or fallocate, when memlocked. (But notice in testing "echo x >memlockedfile" how the O_TRUNC succeeds but the write fails: would F_MEM_UNLOCK on truncation to 0 be better?) Signed-off-by: Hugh Dickins --- mm/shmem.c | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 1ddb910e976c..fa4a264453bf 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1123,15 +1123,30 @@ static int shmem_setattr(struct user_namespace *mnt_userns, /* protected by i_mutex */ if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || - (newsize > oldsize && (info->seals & F_SEAL_GROW)) || - (newsize != oldsize && info->mlock_ucounts)) + (newsize > oldsize && (info->seals & F_SEAL_GROW))) return -EPERM; if (newsize != oldsize) { - error = shmem_reacct_size(SHMEM_I(inode)->flags, - oldsize, newsize); + struct ucounts *ucounts = info->mlock_ucounts; + + if (ucounts && ucounts != current_ucounts()) + return -EPERM; + error = shmem_reacct_size(info->flags, + oldsize, newsize); if (error) return error; + if (ucounts) { + loff_t mlock = round_up(newsize, PAGE_SIZE) - + round_up(oldsize, PAGE_SIZE); + if (mlock < 0) { + user_shm_unlock(-mlock, ucounts, false); + } else if (mlock > 0 && + !user_shm_lock(mlock, ucounts, false)) { + shmem_reacct_size(info->flags, + newsize, oldsize); + return -EPERM; + } + } i_size_write(inode, newsize); inode->i_ctime = inode->i_mtime = current_time(inode); } @@ -2784,6 +2799,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, struct shmem_inode_info *info = SHMEM_I(inode); struct shmem_falloc shmem_falloc; pgoff_t start, index, end, undo_fallocend; + loff_t mlock = 0; int error; if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) @@ -2830,13 +2846,23 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, if (error) goto out; - if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { - error = -EPERM; - goto out; - } - if (info->mlock_ucounts && offset + len > inode->i_size) { + if (offset + len > inode->i_size) { error = -EPERM; - goto out; + if (info->seals & F_SEAL_GROW) + goto out; + if (info->mlock_ucounts) { + if (info->mlock_ucounts != current_ucounts() || + (mode & FALLOC_FL_KEEP_SIZE)) + goto out; + mlock = round_up(offset + len, PAGE_SIZE) - + round_up(inode->i_size, PAGE_SIZE); + if (mlock > 0 && + !user_shm_lock(mlock, info->mlock_ucounts, false)) { + mlock = 0; + goto out; + } + } + error = 0; } start = offset >> PAGE_SHIFT; @@ -2932,6 +2958,8 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, inode->i_private = NULL; spin_unlock(&inode->i_lock); out: + if (error && mlock > 0) + user_shm_unlock(mlock, info->mlock_ucounts, false); inode_unlock(inode); return error; } -- 2.26.2