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 AF3C3C433EF for ; Tue, 19 Apr 2022 22:40:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3403F6B0072; Tue, 19 Apr 2022 18:40:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2EE866B0073; Tue, 19 Apr 2022 18:40:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B7326B0074; Tue, 19 Apr 2022 18:40:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 0DF0E6B0072 for ; Tue, 19 Apr 2022 18:40:23 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C59BA625A3 for ; Tue, 19 Apr 2022 22:40:22 +0000 (UTC) X-FDA: 79375098684.21.DD2F663 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf11.hostedemail.com (Postfix) with ESMTP id BBEE940011 for ; Tue, 19 Apr 2022 22:40:20 +0000 (UTC) Received: by mail-pl1-f172.google.com with SMTP id n18so70916plg.5 for ; Tue, 19 Apr 2022 15:40:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jj65B/Q5u+6/PbNUYjs1QdIcGYmBVQVJnII5Wt+lIX4=; b=I1nobehVf5LWIA7uopWD44Vv9PFicIlwJ83srfQrmZBtflj2atQJNxVv37gj4T4l6v ScNi3IZVoLT5qpIizY9su9A+Gg9F3EImVTRjv9aQAAxS2kei4tEtl7sH9RRka1Mc+h0i 3J1w1FYAB9pXAlDmlVX9UJydCY8Hw6IJX2CqVhTFuZprTscj2mU2kcxYbydeeIrRNG+0 /hdiIk2glg2k2TPStCK1q71Rq2aAqP23tYdUpYm5CzdhY0DW9Ssn6m9sVY8WEQyaIIQA ZlAaw8heoEOaMNA8nyRAyYDxmBrb15yBY5YrSLDiOl1MOET5f0HiV/cXzEtZGNQ6Jz+o 1c7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jj65B/Q5u+6/PbNUYjs1QdIcGYmBVQVJnII5Wt+lIX4=; b=Fim9RJiVAI1tRMx4pqWrm/SYm0L40TVy5PBMnl/c92DeEUFAYyONx0M2Ci8dJs3PD9 ZxlQmIPEIaJH1WuXThbWmWVsWX2NIceOkdcUD3nCwGGDJwAumTR6ty/nAcIZ3RLl9zNW f2b2GMijrw16Jbe0bKa9sHQUKj7huTgBUd98d9XPeTAaQD4o9dAN0sVN7DM5AVrTk2Ic w5mV6NOctalYkV3NPlOz4B7X9xLSoLLf9uVj65o6kND10b9l+DYo5yhySOHbEz2+BW/g Hx0JVykKlZbh1yU9tJ3g8vticXG9H9pawdnNOfjpBftSEGYbh7BZXwhwiLREdRvwJh8Q yo9w== X-Gm-Message-State: AOAM533KvrTlP0kizieHckJ4T5cEVkOF79yrNw34HDztimUAIYqVdI/Q KIeb4G1lVYrA/jmI5A4Q8eFryPHPIoo3vAqaOMBjTA== X-Google-Smtp-Source: ABdhPJww/F0+ShnRulehEAsze0oZV1m6PnQGb4q7nZZiU5Dg58u6Z1IHEBgnXvUTReeiVw8AeB78Z4kC+sTgEsLi9v4= X-Received: by 2002:a17:90b:4d86:b0:1d2:cd59:d275 with SMTP id oj6-20020a17090b4d8600b001d2cd59d275mr888317pjb.119.1650408020788; Tue, 19 Apr 2022 15:40:20 -0700 (PDT) MIME-Version: 1.0 References: <20220310140911.50924-1-chao.p.peng@linux.intel.com> <20220310140911.50924-4-chao.p.peng@linux.intel.com> In-Reply-To: <20220310140911.50924-4-chao.p.peng@linux.intel.com> From: Vishal Annapurve Date: Tue, 19 Apr 2022 15:40:09 -0700 Message-ID: Subject: Re: [PATCH v5 03/13] mm/shmem: Support memfile_notifier 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 , Sean Christopherson , 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 , Yu Zhang , "Kirill A . Shutemov" , Andy Lutomirski , Jun Nakajima , dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=I1nobehV; spf=pass (imf11.hostedemail.com: domain of vannapurve@google.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=vannapurve@google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: htj8g3kwp51p5fqxn8op1pobhtei66qa X-Rspamd-Queue-Id: BBEE940011 X-Rspamd-Server: rspam04 X-Rspam-User: X-HE-Tag: 1650408020-360234 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 at 6:10 AM Chao Peng wrote: > > From: "Kirill A. Shutemov" > > It maintains a memfile_notifier list in shmem_inode_info structure and > implements memfile_pfn_ops callbacks defined by memfile_notifier. It > then exposes them to memfile_notifier via > shmem_get_memfile_notifier_info. > > We use SGP_NOALLOC in shmem_get_lock_pfn since the pages should be > allocated by userspace for private memory. If there is no pages > allocated at the offset then error should be returned so KVM knows that > the memory is not private memory. > > Signed-off-by: Kirill A. Shutemov > Signed-off-by: Chao Peng > --- > include/linux/shmem_fs.h | 4 +++ > mm/shmem.c | 76 ++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 80 insertions(+) > > diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h > index 2dde843f28ef..7bb16f2d2825 100644 > --- a/include/linux/shmem_fs.h > +++ b/include/linux/shmem_fs.h > @@ -9,6 +9,7 @@ > #include > #include > #include > +#include > > /* inode in-kernel data */ > > @@ -28,6 +29,9 @@ struct shmem_inode_info { > struct simple_xattrs xattrs; /* list of xattrs */ > atomic_t stop_eviction; /* hold when working on inode */ > unsigned int xflags; /* shmem extended flags */ > +#ifdef CONFIG_MEMFILE_NOTIFIER > + struct memfile_notifier_list memfile_notifiers; > +#endif > struct inode vfs_inode; > }; > > diff --git a/mm/shmem.c b/mm/shmem.c > index 9b31a7056009..7b43e274c9a2 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -903,6 +903,28 @@ static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) > return page ? page_folio(page) : NULL; > } > > +static void notify_fallocate(struct inode *inode, pgoff_t start, pgoff_t end) > +{ > +#ifdef CONFIG_MEMFILE_NOTIFIER > + struct shmem_inode_info *info = SHMEM_I(inode); > + > + memfile_notifier_fallocate(&info->memfile_notifiers, start, end); > +#endif > +} > + > +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); > + > + start = max(start, folio->index); > + end = min(end, folio->index + folio_nr_pages(folio)); > + > + memfile_notifier_invalidate(&info->memfile_notifiers, start, end); > +#endif > +} > + > /* > * Remove range of pages and swap entries from page cache, and free them. > * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. > @@ -946,6 +968,8 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, > } > index += folio_nr_pages(folio) - 1; > > + notify_invalidate_page(inode, folio, start, end); > + > if (!unfalloc || !folio_test_uptodate(folio)) > truncate_inode_folio(mapping, folio); > folio_unlock(folio); > @@ -1019,6 +1043,9 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, > index--; > break; > } > + > + notify_invalidate_page(inode, folio, start, end); > + Should this be done in batches or done once for all of range [start, end)? > VM_BUG_ON_FOLIO(folio_test_writeback(folio), > folio); > truncate_inode_folio(mapping, folio); > @@ -2279,6 +2306,9 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode > info->flags = flags & VM_NORESERVE; > INIT_LIST_HEAD(&info->shrinklist); > INIT_LIST_HEAD(&info->swaplist); > +#ifdef CONFIG_MEMFILE_NOTIFIER > + memfile_notifier_list_init(&info->memfile_notifiers); > +#endif > simple_xattrs_init(&info->xattrs); > cache_no_acl(inode); > mapping_set_large_folios(inode->i_mapping); > @@ -2802,6 +2832,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, > if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) > i_size_write(inode, offset + len); > inode->i_ctime = current_time(inode); > + notify_fallocate(inode, start, end); > undone: > spin_lock(&inode->i_lock); > inode->i_private = NULL; > @@ -3909,6 +3940,47 @@ static struct file_system_type shmem_fs_type = { > .fs_flags = FS_USERNS_MOUNT, > }; > > +#ifdef CONFIG_MEMFILE_NOTIFIER > +static long shmem_get_lock_pfn(struct inode *inode, pgoff_t offset, int *order) > +{ > + struct page *page; > + int ret; > + > + ret = shmem_getpage(inode, offset, &page, SGP_NOALLOC); > + if (ret) > + return ret; > + > + *order = thp_order(compound_head(page)); > + > + return page_to_pfn(page); > +} > + > +static void shmem_put_unlock_pfn(unsigned long pfn) > +{ > + struct page *page = pfn_to_page(pfn); > + > + VM_BUG_ON_PAGE(!PageLocked(page), page); > + > + set_page_dirty(page); > + unlock_page(page); > + put_page(page); > +} > + > +static struct memfile_notifier_list* shmem_get_notifier_list(struct inode *inode) > +{ > + if (!shmem_mapping(inode->i_mapping)) > + return NULL; > + > + return &SHMEM_I(inode)->memfile_notifiers; > +} > + > +static struct memfile_backing_store shmem_backing_store = { > + .pfn_ops.get_lock_pfn = shmem_get_lock_pfn, > + .pfn_ops.put_unlock_pfn = shmem_put_unlock_pfn, > + .get_notifier_list = shmem_get_notifier_list, > +}; > +#endif /* CONFIG_MEMFILE_NOTIFIER */ > + > int __init shmem_init(void) > { > int error; > @@ -3934,6 +4006,10 @@ int __init shmem_init(void) > else > shmem_huge = SHMEM_HUGE_NEVER; /* just in case it was patched */ > #endif > + > +#ifdef CONFIG_MEMFILE_NOTIFIER > + memfile_register_backing_store(&shmem_backing_store); > +#endif > return 0; > > out1: > -- > 2.17.1 >