Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp1203298rwe; Fri, 14 Apr 2023 16:29:15 -0700 (PDT) X-Google-Smtp-Source: AKy350aRlCkTJGNNSqjBL1vHpVTlVZt9vilMnatoFqQnhTg/Lxc8cnlmeShNorXJqFVzGy6iBgGS X-Received: by 2002:a17:90b:1207:b0:246:b038:2f53 with SMTP id gl7-20020a17090b120700b00246b0382f53mr7011598pjb.24.1681514955528; Fri, 14 Apr 2023 16:29:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681514955; cv=none; d=google.com; s=arc-20160816; b=TGBHhQSjilbW/JHmSq22QUg7Q5N81T/qnZSVf3PuI1EX+SbjFFXfE1g39MezMQWx/j RmrpWZ47DekFJbx34tQVr3iO//ctXkT0fvMEBP3VAqA1acIIYJTiBKnl39xSyMy3h4cv O2KwWzC+v8ASx+oqTfLuXNgy0qfhF+8NtY3C5vMlxtygkJE95uQtyAAqxQD2ZdQ1u/Gf 5zocKJzou9R2ZtKtgbbSOkVyYi60aczck/Hc3d57ZlV6CvDEX33/JLNUaIoQhexerY9y wUUFsNKvmvrbXhLPVo8SdUeiaPKUmhcZUQ7aNzkdD5f4i5s6YroBuEEY/YfZgaNKnKUh shlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=9+CdS/YAmA5S1F/LB6OGsrwDYAK7DxTDkVw+gfhAepA=; b=BAp1aBFsbEGeVFg/lA2zKpRXM4TCpHClzh5dRP5fqwfdpalZDnm7QauT4r2XAI4JtW 91W+Qt39h5ttttyuVVDoAr3dEcG5LVeP1XDJn5/COLk8I2ajY/SNmjIGTzaRVthJXjFv lajPAit8CsApqWEZypSGI6qb6rNUYwj+hVNtUR5vThqBP+Uss4jP3ImHvgJ1YXOSlAC+ CYoqjkGq6bTNEPnWr2N5j5xWGjSVFcrdNBVpL4qD/HUVomMeCR4u/exgMAOF0Aix4cD4 jFYe02aPEvCfDC1edcxPJ662xE0cS9yxDlx2z3OMQfmeWHLVnnQvFEemex0+FoIrNTbs Wg/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=X6xqig1K; 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 n3-20020a6546c3000000b00513ba1faed0si5540976pgr.134.2023.04.14.16.29.04; Fri, 14 Apr 2023 16:29:15 -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=20221208 header.b=X6xqig1K; 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 S229805AbjDNX0w (ORCPT + 99 others); Fri, 14 Apr 2023 19:26:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229568AbjDNX0v (ORCPT ); Fri, 14 Apr 2023 19:26:51 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 977FB4ECB for ; Fri, 14 Apr 2023 16:26:43 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54f9df9ebc5so96402087b3.13 for ; Fri, 14 Apr 2023 16:26:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681514802; x=1684106802; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9+CdS/YAmA5S1F/LB6OGsrwDYAK7DxTDkVw+gfhAepA=; b=X6xqig1KGe44JWfGobl88i68LvH8fuxeySMJifqRsYyD6UTHZ7QOjIguBlvCRg96oh WXZfrnDdhsEwDT29I8rCw6tpWoA2VDlVT/hcuKKgo7Ei+dZTDumyfXXSJIV4h3HvvTtQ M/jF1u45Ld3m/t0bapwufNcZVEpiU7GNEn4IS7RZqLJ4IxYziW/+em7ZbxmEzFPl0h5u ngGOBHl0NKqHdVAxdirGtinyrWG0TxNmTOjSO/BOzHuWHKnZ8WRNCgK0XWrX2vqO8QKz a23RkYq2JbNyZXdSKRuH9aUC5ut9ZsgPw7GVcbkGfAu+Z6WUUklAn4WnntF4rDnomDVB fbkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681514802; x=1684106802; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9+CdS/YAmA5S1F/LB6OGsrwDYAK7DxTDkVw+gfhAepA=; b=dvDXT0TDpQaA8v6B8FVR6pWG6Thq96TtpSEGgCRZWEvAG+io/Dgzi56PujPXI/7/l9 FUH/kaDPkKb2amJKX5lEs0B0WZOt9s4b8p4zv6AKe8DA0803y6uXd023GGgq41x8PQqL vF7+Z0RuUhscslJKk9+igzMh3v4GtmJJ6TQAb3MiSAIgFoNPigPr5FosoCHlulM3DU3P gPYzQ15kDndhmUlH3nT2ZqInt5j8Y/LjGl8CjEoWxq63ozQ61HIJzWFJiUFryAwLYIgy jdEkU4l9KB84gHqNI4O92i8mM8tUOzn5QYthYMJfMeDtMTxxNa9gNlMhzlnqOSG24lXx AvOA== X-Gm-Message-State: AAQBX9eDBvg+zVzczpmy332pMJdI/16mhFRAjeGivhj6SIvDUels60gU ddcPOYnn9WmvW48uaPj6g+fTaO5DJZ0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:ec47:0:b0:54e:e490:d190 with SMTP id r7-20020a0dec47000000b0054ee490d190mr4836875ywn.4.1681514802680; Fri, 14 Apr 2023 16:26:42 -0700 (PDT) Date: Fri, 14 Apr 2023 16:26:41 -0700 In-Reply-To: Mime-Version: 1.0 References: Message-ID: Subject: Re: [PATCH v7 00/14] KVM: mm: fd-based approach for supporting KVM guest private memory From: Sean Christopherson To: Ackerley Tng Cc: brauner@kernel.org, kirill.shutemov@linux.intel.com, chao.p.peng@linux.intel.com, hughd@google.com, 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, linux-kselftest@vger.kernel.org, pbonzini@redhat.com, corbet@lwn.net, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, jlayton@kernel.org, bfields@fieldses.org, akpm@linux-foundation.org, shuah@kernel.org, rppt@kernel.org, steven.price@arm.com, mail@maciej.szmigiero.name, vbabka@suse.cz, vannapurve@google.com, yu.c.zhang@linux.intel.com, 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, qperret@google.com, michael.roth@amd.com, mhocko@suse.com, songmuchun@bytedance.com, pankaj.gupta@amd.com, linux-arch@vger.kernel.org, arnd@arndb.de, linmiaohe@huawei.com, naoya.horiguchi@nec.com, tabba@google.com, wei.w.wang@intel.com Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_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 On Fri, Apr 14, 2023, Ackerley Tng wrote: > Sean Christopherson writes: > > > On Thu, Apr 13, 2023, Christian Brauner wrote: > > > * by a mount option to tmpfs that makes it act > > > in this restricted manner then you don't need an ioctl() and can get > > > away with regular open calls. Such a tmpfs instance would only create > > > regular, restricted memfds. > > > I'd prefer to not go this route, becuase IIUC, it would require relatively > > invasive changes to shmem code, and IIUC would require similar changes to > > other support backings in the future, e.g. hugetlbfs? And as above, I > > don't think any of the potential use cases need restrictedmem to be a > > uniquely identifiable mount. > > FWIW, I'm starting to look at extending restrictedmem to hugetlbfs and > the separation that the current implementation has is very helpful. Also > helps that hugetlbfs and tmpfs are structured similarly, I guess. > > > One of the goals (hopefully not a pipe dream) is to design restrictmem in > > such a way that extending it to support other backing types isn't terribly > > difficult. In case it's not obvious, most of us working on this stuff > > aren't filesystems experts, and many of us aren't mm experts either. The > > more we (KVM folks for the most part) can leverage existing code to do the > > heavy lifting, the better. > > > After giving myself a bit of a crash course in file systems, would > > something like the below have any chance of (a) working, (b) getting > > merged, and (c) being maintainable? > > > The idea is similar to a stacking filesystem, but instead of stacking, > > restrictedmem hijacks a f_ops and a_ops to create a lightweight shim around > > tmpfs. There are undoubtedly issues and edge cases, I'm just looking for a > > quick "yes, this might be doable" or a "no, that's absolutely bonkers, > > don't try it". > > Not an FS expert by any means, but I did think of approaching it this > way as well! > > "Hijacking" perhaps gives this approach a bit of a negative connotation. Heh, commandeer then. > I thought this is pretty close to subclassing (as in Object > Oriented Programming). When some methods (e.g. fallocate) are called, > restrictedmem does some work, and calls the same method in the > superclass. > > The existing restrictedmem code is a more like instantiating an shmem > object and keeping that object as a field within the restrictedmem > object. > > Some (maybe small) issues I can think of now: > > (1) > > One difficulty with this approach is that other functions may make > assumptions about private_data being of a certain type, or functions may > use private_data. > > I checked and IIUC neither shmem nor hugetlbfs use the private_data > field in the inode's i_mapping (also file's f_mapping). > > But there's fs/buffer.c which uses private_data, although those > functions seem to be used by FSes like ext4 and fat, not memory-backed > FSes. > > We can probably fix this if any backing filesystems of restrictedmem, > like tmpfs and future ones use private_data. Ya, if we go the route of poking into f_ops and stuff, I would want to add WARN_ON_ONCE() hardening of everything that restrictemem wants to "commandeer" ;-) > > static int restrictedmem_file_create(struct file *file) > > { > > struct address_space *mapping = file->f_mapping; > > struct restrictedmem *rm; > > > rm = kzalloc(sizeof(*rm), GFP_KERNEL); > > if (!rm) > > return -ENOMEM; > > > rm->backing_f_ops = file->f_op; > > rm->backing_a_ops = mapping->a_ops; > > rm->file = file; > > We don't really need to do this, since rm->file is already the same as > file, we could just pass the file itself when it's needed Aha! I was working on getting rid of it, but forgot to go back and do another pass. > > init_rwsem(&rm->lock); > > xa_init(&rm->bindings); > > > file->f_flags |= O_LARGEFILE; > > > file->f_op = &restrictedmem_fops; > > mapping->a_ops = &restrictedmem_aops; > > I think we probably have to override inode_operations as well, because > otherwise other methods would become available to a restrictedmem file > (like link, unlink, mkdir, tmpfile). Or maybe that's a feature instead > of a bug. I think we want those? What we want to restrict are operations that require read/write/execute access to the file, everything else should be ok. fallocate() is a special case because restrictmem needs to tell KVM to unmap the memory when a hole is punched. I assume ->setattr() needs similar treatment to handle ftruncate()? I'd love to hear Christian's input on this aspect of things. > > if (WARN_ON_ONCE(file->private_data)) { > > err = -EEXIST; > > goto err_fd; > > } > > Did you intend this as a check that the backing filesystem isn't using > the private_data field in the mapping? > > I think you meant file->f_mapping->private_data. Ya, sounds right. I should have added disclaimers that (a) I wrote this quite quickly and (b) it's compile tested only at this point. > On this note, we will probably have to fix things whenever any backing > filesystems need the private_data field. Yep. > > f = fdget_raw(mount_fd); > > if (!f.file) > > return -EBADF; ... > > /* > > * The filesystem must be mounted no-execute, executing from guest > > * private memory in the host is nonsensical and unsafe. > > */ > > if (!(mnt->mnt_sb->s_iflags & SB_I_NOEXEC)) > > goto out; Looking at this more closely, I don't think we need to require NOEXEC, things like like execve() should get squashed by virtue of not providing any read/write implementations. And dropping my misguided NOEXEC requirement means there's no reason to disallow using the kernel internal mount.