Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3890796rwb; Fri, 30 Sep 2022 09:40:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6PzmVtiFJdG0AHXwxYd87/krVSYmh/dbBrwlUkj6efZBBbTwh/mwSPoKt6Oei1fWOKgwGt X-Received: by 2002:a17:907:7245:b0:782:331b:60f4 with SMTP id ds5-20020a170907724500b00782331b60f4mr7134859ejc.594.1664556026317; Fri, 30 Sep 2022 09:40:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664556026; cv=none; d=google.com; s=arc-20160816; b=hba7p8GjhEd/I1QwKYJ3y64aQF4O2rbAfRojXZPGV9YY7Oh+JyGqjCHTZdnq3lcVRr XsDCvkEYqosFyiqx1jVfwT6tjxQC6FZoTVA4o5XKEvK7ErmxoPNyvfV5YEhGriFY4+SG foxEwzWPzhMnDE0nskahKXoexOT1zNyfPYDY51eTkspVyhhFKbK2rjA19zYhtFK3JGwx AQogY9D92YFQw6kla5uzCaE6eR6bJK3yr5wVT2ITYaLIuhUT+c5+nWe4O9g8nfRzQtLl joPfbbtnugYaFK/9Lo8LwSxE9oc1dBBTPLS0V8AJ47Ls7Pj6opk5+YLDCOwgHB/J+JIu a1Mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=GtlSJkLXw2SNcPVROAD1SJDnwutfnbZAayHj4onnywk=; b=uInehW7wieS6JKcQ1LJzkJopqplyQ4gpRdt5I11CML4317Z9bkQE+2YxwcknsBALw1 wVgTuG2UygNByeAK0DE8/rWk54bfnEInEi35IpTYa6JxYpqLJ1CN38TwHJEZfBgtq5Er FrRDZKWLWXvKlbM36EUdcVyVuvh06gHT8QuYk7vDS7X1PEwYHxt2ydH2y8M81jxtdPD6 irp73ph8Kv2UbMRtUfe99zpaYTtVDueiw0LhvZphzVRclpwX/8D9dnuezDO/l8vOjlsS 6Bsl/dl/VrKxEhq+GGz27SLDofamifv+8Pz+LJSaE7h9ttyGkLZPFgNwCAL/NaafRVn8 9Epg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DOMyGYFK; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y12-20020a056402270c00b0044e6ce6c84fsi2556006edd.548.2022.09.30.09.40.00; Fri, 30 Sep 2022 09:40:26 -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=@google.com header.s=20210112 header.b=DOMyGYFK; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231985AbiI3QPL (ORCPT + 99 others); Fri, 30 Sep 2022 12:15:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231979AbiI3QPF (ORCPT ); Fri, 30 Sep 2022 12:15:05 -0400 Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69EDB32EC4 for ; Fri, 30 Sep 2022 09:14:59 -0700 (PDT) Received: by mail-lj1-x22c.google.com with SMTP id t16so5313746ljh.3 for ; Fri, 30 Sep 2022 09:14:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=GtlSJkLXw2SNcPVROAD1SJDnwutfnbZAayHj4onnywk=; b=DOMyGYFKf+TZsvY4xp2ON3TIpgoHPlU49dDhE2e+lblBc8uIrSThwS/2igACfNwrxT B80A327TY7zTQhfHZWcTomLg9RP5BFCafC1G5Xq0tQ7fwoJvvStDoxT0G2gxcG663JP2 Fs7G3g4B/VZpF4ch3hxit7Ug+GQ+GOdS/DQOAl43rZESxPq8F6OkxZ3VQDuWTfhZaVv4 SIQHFtGYvuP6RLRGuDR8p9bpU8nbzmLRHnOHWxhEywjmhLPHJS0aPcx7Quodth6mnW/e VlopEFYGcfN96ScGyCOubqRcKtcTue5tCsqbI81iqfPNUwyTk6FXx34dym02k3Ilq/RK sZ/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=GtlSJkLXw2SNcPVROAD1SJDnwutfnbZAayHj4onnywk=; b=mJHAZNUgkFHbbUfGurSFDhT3wtRsF36Oh6dk3HWnhfKX0aeLEP35hDVV7bcTNn1C2Q UWs0W6xHs5Z6iNAyowojRebD4sanjPHre7c4ge1OWZLjrERX9qDpmrhTKpMrNgMI9HFw 9+Rarmwqa9DX0Fnn4jIjegCz6CkFIU/AfkWIZoUnTvpBBqf1f5EnEDc6EtchL3sLXGpG gWkFTjEJR0tKtg+ornJICa57kJ5L5pAU5D7SCNtN1QfWC3vlzZPGpN3Kcr2qAFS7nxo1 LH1wt3qZPZZBztVRXEBYsk1RjFej5fdiGQxeEM9KwESyrLKzMo5cTQ66Am0vjirVoR5Q 5lOw== X-Gm-Message-State: ACrzQf1dDt3UiM06k5QOAAN0M8LZXz5Xy/6iqZvpulUHgxn4Hi0Sc729 bkhzKnY2jM/uIbA53sacg+0mHBpHOBPnOwmPuo7x3A== X-Received: by 2002:a05:651c:1508:b0:26c:622e:abe1 with SMTP id e8-20020a05651c150800b0026c622eabe1mr3044232ljf.228.1664554497777; Fri, 30 Sep 2022 09:14:57 -0700 (PDT) MIME-Version: 1.0 References: <20220915142913.2213336-1-chao.p.peng@linux.intel.com> <20220915142913.2213336-2-chao.p.peng@linux.intel.com> In-Reply-To: <20220915142913.2213336-2-chao.p.peng@linux.intel.com> From: Fuad Tabba Date: Fri, 30 Sep 2022 17:14:00 +0100 Message-ID: Subject: Re: [PATCH v8 1/8] mm/memfd: Introduce userspace inaccessible memfd 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, linux-doc@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 , Shuah Khan , 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, aarcange@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, Quentin Perret , Michael Roth , mhocko@suse.com, Muchun Song , wei.w.wang@intel.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 Hi, <...> > diff --git a/mm/memfd_inaccessible.c b/mm/memfd_inaccessible.c > new file mode 100644 > index 000000000000..2d33cbdd9282 > --- /dev/null > +++ b/mm/memfd_inaccessible.c > @@ -0,0 +1,219 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include "linux/sbitmap.h" > +#include > +#include > +#include > +#include > +#include > +#include > + > +struct inaccessible_data { > + struct mutex lock; > + struct file *memfd; > + struct list_head notifiers; > +}; > + > +static void inaccessible_notifier_invalidate(struct inaccessible_data *data, > + pgoff_t start, pgoff_t end) > +{ > + struct inaccessible_notifier *notifier; > + > + mutex_lock(&data->lock); > + list_for_each_entry(notifier, &data->notifiers, list) { > + notifier->ops->invalidate(notifier, start, end); > + } > + mutex_unlock(&data->lock); > +} > + > +static int inaccessible_release(struct inode *inode, struct file *file) > +{ > + struct inaccessible_data *data = inode->i_mapping->private_data; > + > + fput(data->memfd); > + kfree(data); > + return 0; > +} > + > +static long inaccessible_fallocate(struct file *file, int mode, > + loff_t offset, loff_t len) > +{ > + struct inaccessible_data *data = file->f_mapping->private_data; > + struct file *memfd = data->memfd; > + int ret; > + > + if (mode & FALLOC_FL_PUNCH_HOLE) { > + if (!PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len)) > + return -EINVAL; > + } > + > + ret = memfd->f_op->fallocate(memfd, mode, offset, len); I think that shmem_file_operations.fallocate is only set if CONFIG_TMPFS is enabled (shmem.c). Should there be a check at initialization that fallocate is set, or maybe a config dependency, or can we count on it always being enabled? > + inaccessible_notifier_invalidate(data, offset, offset + len); > + return ret; > +} > + <...> > +void inaccessible_register_notifier(struct file *file, > + struct inaccessible_notifier *notifier) > +{ > + struct inaccessible_data *data = file->f_mapping->private_data; > + > + mutex_lock(&data->lock); > + list_add(¬ifier->list, &data->notifiers); > + mutex_unlock(&data->lock); > +} > +EXPORT_SYMBOL_GPL(inaccessible_register_notifier); If the memfd wasn't marked as inaccessible, or more generally speaking, if the file isn't a memfd_inaccessible file, this ends up accessing an uninitialized pointer for the notifier list. Should there be a check for that here, and have this function return an error if that's not the case? Thanks, /fuad > + > +void inaccessible_unregister_notifier(struct file *file, > + struct inaccessible_notifier *notifier) > +{ > + struct inaccessible_data *data = file->f_mapping->private_data; > + > + mutex_lock(&data->lock); > + list_del(¬ifier->list); > + mutex_unlock(&data->lock); > +} > +EXPORT_SYMBOL_GPL(inaccessible_unregister_notifier); > + > +int inaccessible_get_pfn(struct file *file, pgoff_t offset, pfn_t *pfn, > + int *order) > +{ > + struct inaccessible_data *data = file->f_mapping->private_data; > + struct file *memfd = data->memfd; > + struct page *page; > + int ret; > + > + ret = shmem_getpage(file_inode(memfd), offset, &page, SGP_WRITE); > + if (ret) > + return ret; > + > + *pfn = page_to_pfn_t(page); > + *order = thp_order(compound_head(page)); > + SetPageUptodate(page); > + unlock_page(page); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(inaccessible_get_pfn); > + > +void inaccessible_put_pfn(struct file *file, pfn_t pfn) > +{ > + struct page *page = pfn_t_to_page(pfn); > + > + if (WARN_ON_ONCE(!page)) > + return; > + > + put_page(page); > +} > +EXPORT_SYMBOL_GPL(inaccessible_put_pfn); > -- > 2.25.1 >