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 47901C677F1 for ; Fri, 13 Jan 2023 22:02:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0FAA8E0002; Fri, 13 Jan 2023 17:02:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B98DC8E0001; Fri, 13 Jan 2023 17:02:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A12178E0002; Fri, 13 Jan 2023 17:02:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8B9328E0001 for ; Fri, 13 Jan 2023 17:02:18 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3AB5F407AB for ; Fri, 13 Jan 2023 22:02:18 +0000 (UTC) X-FDA: 80351149956.23.7B8DE16 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf05.hostedemail.com (Postfix) with ESMTP id 96BA810000C for ; Fri, 13 Jan 2023 22:02:16 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BR3tUqUT; spf=pass (imf05.hostedemail.com: domain of seanjc@google.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=seanjc@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673647336; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=z7l9vd6TJMxdcmrmIlHi+1aamG6yNr9jBZqHMxmbVBg=; b=Q8ej6t30CVeYMvc6F8DEauO2dx2/vhx3+YrpDYcXrWB02kzkg3eDAzZxEN+tcsX3Myejs0 EhZgqO/7Tkk3FUeM/e4FANTAqA0ultt3mCpxhrmXkKDE50uEQnKy0iT0psep9cP6E2ykfK c1lAZuzdz4Q3ogPD1TpnbvvPC0NoLXA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BR3tUqUT; spf=pass (imf05.hostedemail.com: domain of seanjc@google.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=seanjc@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673647336; a=rsa-sha256; cv=none; b=sXf1JlqhvuVgadfq/GMniZ3p8Tmy1+mP7s/V9lAzvEialqGV+xIv0GCcOOmA2y34/g5u+m 62rJu7dzhO7gSPr/WbvuGIZOWbgtd6tvhWXbhDHbRUQQv+XDAdjIYMSSDe320GpHXupm1q r72rJ+gWvvbqggEVWuMTBfV9vPPJ14Y= Received: by mail-pl1-f173.google.com with SMTP id y1so24788248plb.2 for ; Fri, 13 Jan 2023 14:02:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=z7l9vd6TJMxdcmrmIlHi+1aamG6yNr9jBZqHMxmbVBg=; b=BR3tUqUT1wdTPEhuyI88J1yG4yZUf5PuICve+KLW/6jG7Wb1v6McPHOp0JH41FPvHo cA2xNTdp4kP3XHFHgUTLdE2qj40XKo3fIu/xNDgFentclcLH8D4+aqHtGytwQRH7EFT0 SvW3dkQzsILPvPKa4Is4mjjYyZmLU2ZjXaSMTILUI2Ql4PmYTWFYg91PuBLsgKKGoru5 2kNrKIxXaIvBTzLY81Ye3wChF7mPYngBYjfHrPWew9lZluOUDNoM3QdVM4dxlS9vgJCj 1PCCo9ULYh3HxSCxalWt2dPVhw7cT4s2LG6uTB7ArEkfMypRVuMINSetLuB5egod0Oa5 0CYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=z7l9vd6TJMxdcmrmIlHi+1aamG6yNr9jBZqHMxmbVBg=; b=xCUvOVGwpaxzcm8jAGWmR10+JxCpy7Ct3h7SSqv2/aMC1iLfHl5YSdc3GJ62JcjTmM bKwP1t6Lxo9RSM26lWYGv2CSO5WfjH3o3V3YNO/H3ErGu6mKuk4rtbiG2pPGFBY/APe/ tTcDoWGS6OsgHWb7XXiyZ3tCCmYBtI7lSJlJuXfOl+0XvNTGpFpVB2aNMOjqJSgIcoqh noME9VYnoEYfskXe4MT2G3hg+QPDgT11O+01fQnZ7J9CLcFkSHlZ2l3qhOE1HsryN6cX G4JAsOM8tFkeeqPe6z8SejNavj2pBsSEe77dO3iCoYP64hzGKdvJgu9d8+xBwz1Z0FLl K3XQ== X-Gm-Message-State: AFqh2kppc99M0xXwED0Bpsa3rfLW6rWfhbXsEbhT9++e7ZCEzwERcqA8 RIGqpl4s5E26BeVYUTWnUFEC8Q== X-Google-Smtp-Source: AMrXdXttju4or509XUsZuP2ElabXRLq+UGu6WNhuAKj6QmwsUs3kftnSPzMWDd6fhBAAvxes/xEs1A== X-Received: by 2002:a05:6a20:47de:b0:b3:66b7:24ff with SMTP id ey30-20020a056a2047de00b000b366b724ffmr1359880pzb.1.1673647335304; Fri, 13 Jan 2023 14:02:15 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id k36-20020a635a64000000b0048988ed9e4bsm11687157pgm.19.2023.01.13.14.02.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 14:02:14 -0800 (PST) Date: Fri, 13 Jan 2023 22:02:11 +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-arch@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@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 , Arnd Bergmann , Naoya Horiguchi , Miaohe Lin , 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 , tabba@google.com, Michael Roth , mhocko@suse.com, wei.w.wang@intel.com Subject: Re: [PATCH v10 2/9] KVM: Introduce per-page memory attributes Message-ID: References: <20221202061347.1070246-1-chao.p.peng@linux.intel.com> <20221202061347.1070246-3-chao.p.peng@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221202061347.1070246-3-chao.p.peng@linux.intel.com> X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 96BA810000C X-Stat-Signature: nf94eskwmpxqo7gfzds4ta5r6zpauadn X-HE-Tag: 1673647336-527248 X-HE-Meta: U2FsdGVkX1+TTp/9HVifGpL+1G6nfixLJnpzqjyK/YD66R2N5aZP3PFeBHun7Z45id/fAnK1QXiNqhi5w47FHNOjV/QEFgabumcTWFH/n2V+JAhtCQr2cfR7q6DG+GE2Nrcm8Pk6lFGR01OvcHNveIX2Q7FnwJ704l14wkGb534thoYAsWp/pS1QW2fpnoleC3dmdRV0Ex9nG+wK/4lRIJJ04i/8IWQDwiSGAUE9QdwJEr4vaplwR5AXfLEKzzxaXPiFyO9ueobGZal3WY46rnUryoJVgMY/4XrrFZsClqwzlrNsVHqMhn2WRNUgLqI+t3DYzALLBbwYEr0cZ8xbGp/X7KpawYkLwi3WEBmITwpB/XDLhKfs8KNCFMItr9LmGi/QzYYme54M8VF+jTmOoltFPrMcJ8UKqrL0DRfbFMznNOhkNUxR8syVzhvLYGb+OzUR5ujGShVZvs6wrr+3OMIPBGg7Wv17uUz7VCjN1jEv9oERi/D2paP3DUQp/3O0A6WhSZeOjniI3VRVioqjk9Rzm5AjNPskMfWsNxap5kJc9l7makspgR59PvJpq3JHPXKzV36sFmGOcZR9DugYmuZwpn2Vt8Zoxd8iMn0uu7+3KSF5poPLM4lDbFivs1LW8rgDagTq/Zq+IhmKRHdP7iIW3kq5p7BmvFlgIRK9CciNMeTaj+OY5QrYl26jQI2pBUNlwo5Jw+K/IbMb9sDpmeSgakovNuZto73jcbucmqg33iVd3ZKuG/sfmgYGKRouAepD7Y33ClkBafjMveONAWDyrSryXbUgOYrfq+ZeffmqV1f9KCLy0L4M0VcoC3YOuoWqgQE5FT4CbpBdmEFWp7xFuGIc7bCpnbSXAUeJmnD5ZOsqWJmtgQGdN9tZ4Dd8c5jU7sH2cwvVUKON3YpKRGfdhgEZYrD/dQZUeck3OPEFMgnep76XB+XKSF28LuFNdh4AvrZwTCe8pIeJfLN a4D9ma45 BIUvgbO3UDzAL/e7Y4/L3is24AaFTlnbvd/xzrRaDup+R4qk/aVvRDT3thBRUk97wE+a1SLP9EkS5Nxy1nKQ8f1w8VVodvN9zg6US 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 Fri, Dec 02, 2022, Chao Peng wrote: > diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig > index fbeaa9ddef59..a8e379a3afee 100644 > --- a/arch/x86/kvm/Kconfig > +++ b/arch/x86/kvm/Kconfig > @@ -49,6 +49,7 @@ config KVM > select SRCU > select INTERVAL_TREE > select HAVE_KVM_PM_NOTIFIER if PM > + select HAVE_KVM_MEMORY_ATTRIBUTES I would prefer to call this KVM_GENERIC_MEMORY_ATTRIBUTES. Similar to KVM_GENERIC_HARDWARE_ENABLING, ARM does need/have hardware enabling, it just doesn't want KVM's generic implementation. In this case, pKVM does support memory attributes, but uses stage-2 tables to track ownership and doesn't need/want the overhead of the generic implementation. > help ... > +#define KVM_MEMORY_ATTRIBUTE_READ (1ULL << 0) > +#define KVM_MEMORY_ATTRIBUTE_WRITE (1ULL << 1) > +#define KVM_MEMORY_ATTRIBUTE_EXECUTE (1ULL << 2) > +#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) I think we should carve out bits 0-2 for RWX, but I don't think we should actually define them until they're actually accepted by KVM. > +static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, > + struct kvm_memory_attributes *attrs) > +{ > + gfn_t start, end; > + unsigned long i; > + void *entry; > + u64 supported_attrs = kvm_supported_mem_attributes(kvm); > + > + /* flags is currently not used. */ > + if (attrs->flags) > + return -EINVAL; > + if (attrs->attributes & ~supported_attrs) Nit, no need for "supported_attrs", just consume kvm_supported_mem_attributes() directly. > + return -EINVAL; > + if (attrs->size == 0 || attrs->address + attrs->size < attrs->address) > + return -EINVAL; > + if (!PAGE_ALIGNED(attrs->address) || !PAGE_ALIGNED(attrs->size)) > + return -EINVAL; > + > + start = attrs->address >> PAGE_SHIFT; > + end = (attrs->address + attrs->size - 1 + PAGE_SIZE) >> PAGE_SHIFT; > + > + entry = attrs->attributes ? xa_mk_value(attrs->attributes) : NULL; > + > + mutex_lock(&kvm->lock); Peeking forward multiple patches, this needs to take kvm->slots_lock, not kvm->lock. There's a bug in the lpage_disallowed patch that I believe can most easily be solved by making this mutually exclusive with memslot changes. When a memslot is created, KVM needs to walk through the attributes to detect whether or not the attributes are identical for the entire slot. To avoid races, that means taking slots_lock. The alternative would be to query the attributes when adjusting the hugepage level and avoid lpage_disallowed entirely, but in the (very brief) time I've thought about this I haven't come up with a way to do that in a performant manner. > + for (i = start; i < end; i++) Curly braces needed on the for-loop.