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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50BCBC433F5 for ; Thu, 7 Apr 2022 16:06:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B32D56B0072; Thu, 7 Apr 2022 12:05:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE0B56B0073; Thu, 7 Apr 2022 12:05:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9824A6B0074; Thu, 7 Apr 2022 12:05:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id 89D836B0072 for ; Thu, 7 Apr 2022 12:05:52 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5C07325CD3 for ; Thu, 7 Apr 2022 16:05:42 +0000 (UTC) X-FDA: 79330558524.20.514CF2B Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by imf08.hostedemail.com (Postfix) with ESMTP id D9A3F160004 for ; Thu, 7 Apr 2022 16:05:41 +0000 (UTC) Received: by mail-pj1-f45.google.com with SMTP id s14-20020a17090a880e00b001caaf6d3dd1so9270329pjn.3 for ; Thu, 07 Apr 2022 09:05:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=oJxIZVkcUncMUZQfQzkdJ+zb+DZalywUloW4568yYn0=; b=kfqHsKdfw89Dwd7QGak9HdKv00acSQig8ZHOqKWrcgwuHdvnnaELMDCR55SPQFtBnS TCxq7MWV9AQn4Hy0EhgxCx0TPpiUCpQgwy7H23Vwal4SDaAw8HvwrqITWyGFNK4sQzhH JFlGRWNfuLUz6qBVdSmZZ4kfxxkJBk87qruGPkjprjbLBVVAK37/Q8fsRcuV33Xw/HVj B8h8cqJw82ZLbYL11sh4DuKcy3QGnH0AAQvp3PE979fT3HkEwRHAW82vgfR+KkL2QJN5 KSj7BU6Ix6FVP1cRvsNGtSRLODq/C8E5+MMo0e8vGjCEj/PqSsvyu1EMlSFPMgRzy1o2 ak7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=oJxIZVkcUncMUZQfQzkdJ+zb+DZalywUloW4568yYn0=; b=bwU+/Tdt7VEXPfYvIhU2QdTHnP9PuQkkdrWg0s2GWDh06F+UH1rYnLCcDPfh0F/8sO 1Bq+UIrykYf7AEasETePIQbtkmzH2lUBU8/OBU7ngH9HcidzCX+S7u+jCcwNIlQyZXRp DwyJOu6eguF2dVn266wi99tVI4GUTmnTGoDWF58RteffCXVYCDSRFD9HPz38O4wwnN3L P/zwHQVsFb0MP05F6frmlu/AXf/h99S9KAd677XFHm1BRLZ8m1tAG2CpzZsKR5YI0zvP cIBa/QgBOgHLd6SEmO5A83r+mMthKtiIDAHJJrFviFjXmTFBmpr9zdbVmPrTO6nzdkuZ vsCg== X-Gm-Message-State: AOAM530wuRq7HwkySWD3imr5A8/DqGXmho8Qrmj9rUy+B7a8DGqKbRBV 6nUNeyEguYccjy21VX0NVePwjQ== X-Google-Smtp-Source: ABdhPJxwrn5jcOCrefa44ifJu+1yvC1mRdMBBIerz3MlZcxhkTZR+e25OlxT/CA5AXmZeG4/pzPIww== X-Received: by 2002:a17:90b:4a84:b0:1cb:29bd:db3e with SMTP id lp4-20020a17090b4a8400b001cb29bddb3emr1947695pjb.112.1649347540416; Thu, 07 Apr 2022 09:05:40 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id q17-20020aa79831000000b0050566040330sm3561920pfl.126.2022.04.07.09.05.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 09:05:39 -0700 (PDT) Date: Thu, 7 Apr 2022 16:05:36 +0000 From: Sean Christopherson To: Chao Peng Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, qemu-devel@nongnu.org, Paolo Bonzini , Jonathan Corbet , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vlastimil Babka , Vishal Annapurve , Yu Zhang , "Kirill A . Shutemov" , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com Subject: Re: [PATCH v5 04/13] mm/shmem: Restrict MFD_INACCESSIBLE memory against RLIMIT_MEMLOCK Message-ID: References: <20220310140911.50924-1-chao.p.peng@linux.intel.com> <20220310140911.50924-5-chao.p.peng@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220310140911.50924-5-chao.p.peng@linux.intel.com> X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: oefjs1cdynm7z446ounr6uttuid13kqs Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kfqHsKdf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of seanjc@google.com designates 209.85.216.45 as permitted sender) smtp.mailfrom=seanjc@google.com X-Rspamd-Queue-Id: D9A3F160004 X-HE-Tag: 1649347541-155792 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: On Thu, Mar 10, 2022, Chao Peng wrote: > Since page migration / swapping is not supported yet, MFD_INACCESSIBLE > memory behave like longterm pinned pages and thus should be accounted to > mm->pinned_vm and be restricted by RLIMIT_MEMLOCK. > > Signed-off-by: Chao Peng > --- > mm/shmem.c | 25 ++++++++++++++++++++++++- > 1 file changed, 24 insertions(+), 1 deletion(-) > > diff --git a/mm/shmem.c b/mm/shmem.c > index 7b43e274c9a2..ae46fb96494b 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -915,14 +915,17 @@ static void notify_fallocate(struct inode *inode, pgoff_t start, pgoff_t end) > static void notify_invalidate_page(struct inode *inode, struct folio *folio, > pgoff_t start, pgoff_t end) > { > -#ifdef CONFIG_MEMFILE_NOTIFIER > struct shmem_inode_info *info = SHMEM_I(inode); > > +#ifdef CONFIG_MEMFILE_NOTIFIER > start = max(start, folio->index); > end = min(end, folio->index + folio_nr_pages(folio)); > > memfile_notifier_invalidate(&info->memfile_notifiers, start, end); > #endif > + > + if (info->xflags & SHM_F_INACCESSIBLE) > + atomic64_sub(end - start, ¤t->mm->pinned_vm); As Vishal's to-be-posted selftest discovered, this is broken as current->mm may be NULL. Or it may be a completely different mm, e.g. AFAICT there's nothing that prevents a different process from punching hole in the shmem backing. I don't see a sane way of tracking this in the backing store unless the inode is associated with a single mm when it's created, and that opens up a giant can of worms, e.g. what happens with the accounting if the creating process goes away? I think the correct approach is to not do the locking automatically for SHM_F_INACCESSIBLE, and instead require userspace to do shmctl(.., SHM_LOCK, ...) if userspace knows the consumers don't support migrate/swap. That'd require wrapping migrate_page() and then wiring up notifier hooks for migrate/swap, but IMO that's a good thing to get sorted out sooner than later. KVM isn't planning on support migrate/swap for TDX or SNP, but supporting at least migrate for a software-only implementation a la pKVM should be relatively straightforward. On the notifiee side, KVM can terminate the VM if it gets an unexpected migrate/swap, e.g. so that TDX/SEV VMs don't die later with exceptions and/or data corruption (pre-SNP SEV guests) in the guest. Hmm, shmem_writepage() already handles SHM_F_INACCESSIBLE by rejecting the swap, so maybe it's just the page migration path that needs to be updated?