From patchwork Thu Jan 9 13:38:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13932582 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87D73220686 for ; Thu, 9 Jan 2025 13:38:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429911; cv=none; b=qprPigaPZEh7h2ZuYDePh0EDJK8kaoSiVnmoeBHFUOu0mH2mXrae97RSRMTbvlb1Of2akxSAWhnsAaGtT3aw8+XAm/kUxbsqP8aPYTh/vMMs6OeCK6ixF02DEnDirBOIUwxtLmd3In95DMqzONt78cFCspWhuSbhen9u5Zat+K8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429911; c=relaxed/simple; bh=gHVqbeeLDW26xdNiUZY5UzwMkP98DCAKakRVGGlY3Dg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GD3/AJ97BU6oqDXWj7fakRDTtsH7mwno2YBUbl99cvbIq2nhUI7dAC4aNiaL89PTmzyFzQKxRl8MESkUwUT81urh6yLyEXZTKCtcTlx2Q67YklTmqE7Q0Q01snceMrvYuKQ9r7WJqwjRZMQ3NFtnzj9jV7DcpXJM89YajH6wxSY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YqGkmfWy; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YqGkmfWy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736429905; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+qIG/0LvnjXMgyU8eqBX8hgYV7UmJPw6Xa2UEwpxHJA=; b=YqGkmfWyEjjY77HA0vyTth1nUFN7C+3p0TP6pni2mbjmzQzWkCOEOtuS6ch3rFRJUnmvJZ TP2s+6j68+qVWWDwZQQnViMHPtRUIvXqNfINzq8KF0LIsblGIUT5jmNeRNajkGc97+16bw pF+kcTICC5E/UQHUWdPcxFr51mKz8KY= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-621-6K0RCVH7Nd-N0zYGN-2gHw-1; Thu, 09 Jan 2025 08:38:24 -0500 X-MC-Unique: 6K0RCVH7Nd-N0zYGN-2gHw-1 X-Mimecast-MFC-AGG-ID: 6K0RCVH7Nd-N0zYGN-2gHw Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-5d3eea3b9aaso677685a12.1 for ; Thu, 09 Jan 2025 05:38:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736429903; x=1737034703; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+qIG/0LvnjXMgyU8eqBX8hgYV7UmJPw6Xa2UEwpxHJA=; b=OVxiy6yOXtzkyNv4xPx1TAcwpPD5jB7O+OF9ad8VqhoQjVqLjBq/Ta96e4w6J1WOgy UVHKPrKf3VkJR8+wNlMx9aIDSGr2BhhBx2yb0x10t/FHnNdKMOvqp5rvkU+XjVm+jgGj jRP8POYp4fDcYRCv0v+FkjleEsM+gVcVN8Lu4E8/p/xp/RfYjol/VV83XzJAnJitzV+H p/vceTOjVRV5VfP+GmCurBKM0m2B79eKshXBfTB+AAHVH302LlmtSgBiHfSKsvWkjaV9 tVPgVCsJeJ3EoCUiRwP5UN+NGuPMQLtm2VLPU7dUu56aTJyRgRT/JOjoP3sCWqlC7kEM zB5A== X-Forwarded-Encrypted: i=1; AJvYcCW+bgnflyT+Ut7AcLCUDnO6dWESkvfQv8oq74qrmZgfzN6hIxAjBW2lPMO0H6Pb0YSQIHA=@vger.kernel.org X-Gm-Message-State: AOJu0YwKD6/f7V6GKZGNKgopbmr8RTOlHcbuKVg/XtG+dehSzd3n9CwQ mf/i5iNZ6Pn7oDJ+faGnljU1zbU6QsUKStAJxrXJRzprSexySQgSON8QO7q6MKGMpUWiaBCWm13 zMqoYFQy2djwUPYsDcThobZhygZWazNXKTIDpRAg5Datc2sg/zA== X-Gm-Gg: ASbGnctvS3CRW7m8XTWFY530dMnG1i7IhcamCEegUGXb9Ym50uPqpdh53qxtOF0xE+e DwSmS39r3t9eEyJWYqfPRT0vaDzQvx9D5a0vQtxEfHZuc/hTE++54z1ZKZ1Pf6ojlTMugsTuxKh QeKF7hN0gygV659K8nY3xm7La9tkx3JLGmWkbAk9NGgyPtpACATGMeAVyCrAbGeofsusrcfHIux 6afPqCiFy6tu1tiPN/8axY7oicYtTQ3u106GJwZaWSqOkjq/j91E8simi0C X-Received: by 2002:a05:6402:3549:b0:5d0:e73c:b7f0 with SMTP id 4fb4d7f45d1cf-5d972e70945mr15040460a12.28.1736429902493; Thu, 09 Jan 2025 05:38:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHsGUykWSOpesU6tHxPGZnO3JxPfRRnO6klBd/fYEFfPEHAwQWWT0oVTu7t2r5Sxoacsy+c3Q== X-Received: by 2002:a05:6402:3549:b0:5d0:e73c:b7f0 with SMTP id 4fb4d7f45d1cf-5d972e70945mr15040371a12.28.1736429901823; Thu, 09 Jan 2025 05:38:21 -0800 (PST) Received: from [192.168.10.47] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5d99008c366sm624477a12.17.2025.01.09.05.38.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jan 2025 05:38:21 -0800 (PST) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: oliver.upton@linux.dev, Will Deacon , Anup Patel , Andrew Jones , seanjc@google.com, linuxppc-dev@lists.ozlabs.org, regressions@lists.linux.dev, stable@vger.kernel.org Subject: [PATCH 1/5] KVM: e500: retry if no memslot is found Date: Thu, 9 Jan 2025 14:38:13 +0100 Message-ID: <20250109133817.314401-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250109133817.314401-1-pbonzini@redhat.com> References: <20250109133817.314401-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Avoid a NULL pointer dereference if the memslot table changes between the exit and the call to kvmppc_e500_shadow_map(). Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini --- arch/powerpc/kvm/e500_mmu_host.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index e5a145b578a4..732335444d68 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -349,6 +349,11 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, * pointer through from the first lookup. */ slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn); + if (!slot) { + ret = -EAGAIN; + goto out; + } + hva = gfn_to_hva_memslot(slot, gfn); if (tlbsel == 1) { From patchwork Thu Jan 9 13:38:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13932583 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0CBF2206BB for ; Thu, 9 Jan 2025 13:38:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429913; cv=none; b=ma2GcGwHvldPguz6sNCAPTNCfHUTLfj9WMJzCvshmQPX6HjNaLeLsYdEry/QIFwk7I/oEI89n08CKxYFy+kEwnEp2dcxUUD4FItdO5oqiv/F9WSqp+OSndVGBMoV+uLBT/yV/giKDF/toMXqPoRR65ay35BJWofHg7uRj2o3x1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429913; c=relaxed/simple; bh=CLPtAWyQrS2entG2WH31BYmUD0uC+KITDZ/IYJGu0jo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=faWX1jPyODeygWIY5NQK1CTApFQbAzNqnG6Bi+R6cLId0qxrmT0mOzsikUGBHA51bkvl8x3vsHp34fruelHKZEtDnlkxPrpvmfWGjTX8nS0FLg3StSdE/3/YxjxVLEisqV9vzPPhiTEEs739ayqW6aQ/8FTetL3g3lv3/rLzzY8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YH3g+COC; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YH3g+COC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736429910; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d/K9YNVS7ltWEgnQj5oYbH5hWmhcl5BqPq+/x8nLjmU=; b=YH3g+COCDCLjHJUN4O4SS+yupzd4iXUTfYQZPijMLdzGBBYY0cJsNoxP0krWC0bp+Vn6is JF2+d8+eWfM3XXU1bpzum6HE7NLLw1xLv9kcXqixVB3OypRKJHDQ020PEUfMYwmjDn1tG3 +JUtqTweqFFRMN6PVoxB+LSEr5T0leM= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-299-b7oVtHaONCWp2Axzhso61w-1; Thu, 09 Jan 2025 08:38:27 -0500 X-MC-Unique: b7oVtHaONCWp2Axzhso61w-1 X-Mimecast-MFC-AGG-ID: b7oVtHaONCWp2Axzhso61w Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-aa6732a1af5so97122566b.3 for ; Thu, 09 Jan 2025 05:38:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736429906; x=1737034706; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d/K9YNVS7ltWEgnQj5oYbH5hWmhcl5BqPq+/x8nLjmU=; b=ox7QOY8y2Tg8lq5rfh4qOGQwdQi9Pz+sS6Ck9yWg7OJs7Xyy/Zm8hrYrGl4BLFoQkL wRSlX8gwHav2WDsd5dw4w/PhIO5AdmXPNi9qFD7dFh/gfCAenbb9Icu6+Zxi4ApYUifs t5UUZB8LFjwzC0J82FLHXWkTHbN4itTckGyGvdTvLoOkuXjJrG4E+8/DeyK8qmGBhZoZ ufX7e95Xv5F+8zfnpi9d6KFKyLTKcbXTePdHhVHK/SbZT7/V0xzYgsGaZbdISmmPVO0d BKFQYz13JQYGKpbvpyTBQ8ilFmZLZ5ARGQ7Chb8e8MGVl2XF7Zy26BT4dAqOHgEHa25I C4Ww== X-Forwarded-Encrypted: i=1; AJvYcCVy7pua7dyusOS3itqM5sVS+dNMtG6oA3veby0+A+ybNaGPYp85FsL1e/7+sFk2UiJxbak=@vger.kernel.org X-Gm-Message-State: AOJu0YxgE5wNBb9tbpLfhHBub1fqndPIos4ZGSsqUU8iz4hdvegrrwD9 J0H5iIBH317/EZbDKujMhiqvIQ0JCo9CCNkvBXkBg7QIcs5lepjIbwdLjxyc2kiGHRNzTgczBP2 14jyCzd7A3cT6Gkmu09tq+YsquKt/HZqnJM1qEfc88DChl8B42A== X-Gm-Gg: ASbGncv+XBSN4RqrUe3UH4tzY4y3bwDWeHV1ZPK/N0pDVKkdVtfOaYNBpnxxtdGwcQx dYQE9vsRATPUFOOOPwtfNEBbaYADcy/XPpsdAHN6glq64zdHO6FBbZ2xCfVdijY2bO0itMkRak6 fPfcVKPpLr3ce7BWJ3PjLT1pBISgKJKQSTMiyp7w3HYuITmyt9ofljQEXstN91fdPK1e6fCUpRg 01oCrPz3tfkWeZEuVa8CHw5miUvKLhUFJ5knGoImpBp5XtEKB5LpvCevKTN X-Received: by 2002:a17:907:94cc:b0:aa6:7cae:dba7 with SMTP id a640c23a62f3a-ab2ab6a851amr624677166b.4.1736429906033; Thu, 09 Jan 2025 05:38:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IGTbBeYDIBQ3z954w6dfAEk3XY9Iu9HEUTdIMdZmv3AtPH2uw4HEV5T8fdkL1pcscV4BnFyug== X-Received: by 2002:a17:907:94cc:b0:aa6:7cae:dba7 with SMTP id a640c23a62f3a-ab2ab6a851amr624675766b.4.1736429905640; Thu, 09 Jan 2025 05:38:25 -0800 (PST) Received: from [192.168.10.47] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab2c95648e7sm72802566b.95.2025.01.09.05.38.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jan 2025 05:38:23 -0800 (PST) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: oliver.upton@linux.dev, Will Deacon , Anup Patel , Andrew Jones , seanjc@google.com, linuxppc-dev@lists.ozlabs.org, regressions@lists.linux.dev Subject: [PATCH 2/5] KVM: e500: use shadow TLB entry as witness for writability Date: Thu, 9 Jan 2025 14:38:14 +0100 Message-ID: <20250109133817.314401-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250109133817.314401-1-pbonzini@redhat.com> References: <20250109133817.314401-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 kvmppc_e500_ref_setup is returning whether the guest TLB entry is writable, which is than passed to kvm_release_faultin_page. This makes little sense for two reasons: first, because the function sets up the private data for the page and the return value feels like it has been bolted on the side; second, because what really matters is whether the _shadow_ TLB entry is writable. If it is not writable, the page can be released as non-dirty. Shift from using tlbe_is_writable(gtlbe) to doing the same check on the shadow TLB entry. Signed-off-by: Paolo Bonzini --- arch/powerpc/kvm/e500_mmu_host.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index 732335444d68..06e23c625be0 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -242,7 +242,7 @@ static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe) return tlbe->mas7_3 & (MAS3_SW|MAS3_UW); } -static inline bool kvmppc_e500_ref_setup(struct tlbe_ref *ref, +static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref, struct kvm_book3e_206_tlb_entry *gtlbe, kvm_pfn_t pfn, unsigned int wimg) { @@ -251,8 +251,6 @@ static inline bool kvmppc_e500_ref_setup(struct tlbe_ref *ref, /* Use guest supplied MAS2_G and MAS2_E */ ref->flags |= (gtlbe->mas2 & MAS2_ATTRIB_MASK) | wimg; - - return tlbe_is_writable(gtlbe); } static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref) @@ -493,10 +491,11 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, goto out; } } - writable = kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg); + kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg); kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize, ref, gvaddr, stlbe); + writable = tlbe_is_writable(stlbe); /* Clear i-cache for new pages */ kvmppc_mmu_flush_icache(pfn); From patchwork Thu Jan 9 13:38:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13932584 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A2BC2206B9 for ; Thu, 9 Jan 2025 13:38:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429913; cv=none; b=bGqcqOFMO1wKFv6EIQwtMh+cRzzqQl/vkeXMN/QZ+/mkkR67eIV3qEBnN0KlFdaczQHL8BYriDfsh5k1OSUrMzz+HMenFv91gBmmtR1ww+8WIzHUD+YN8mGADebHu7/XYybrIbG4/yb9bxyMHc5RT8vYYjqkMXIpwxIoTtmsgKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429913; c=relaxed/simple; bh=sE3kc+9sHBVpfMHODBVRzxlEUWaM8pbPMewDEM8p2rI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NNtzbl58t7TxqDH5IvIhpn6WjyZ8ooTVfa4VQWXQTAZei3n/xAhtJhVHK6sxgs8hXlcbHzO7yXBQrY6dL3f1TjccBlBX9upphl2EDZWHxAiq9HH4W/vNR9QAXmbHRBCGkSEE+xWwfmMVx/B78Xy6vz3JuWyxXcLEXU+EKhRtweE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=aPL/VTle; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="aPL/VTle" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736429911; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zguszej+6js0jv121He/RU1+H3xrJy3UeZApbWTSTbc=; b=aPL/VTleUzRwTxINAaBioSqm4ksH0Z23ChPE5HHZI3uVtfNmMJ7vHEuJPrdFJcBHU/0YmU eEaTq0vaAL+GT8DK38ZLVTEnCCE5sLfsiAjzADDi4/xeevxaU387I0IjIKaA1ZJ5wdg9WV 85iTjgZp8cw9Xi+SCWfAK1TDF0Gmfp8= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-508-QByXxKtnN7qmIIzgcX5PLQ-1; Thu, 09 Jan 2025 08:38:30 -0500 X-MC-Unique: QByXxKtnN7qmIIzgcX5PLQ-1 X-Mimecast-MFC-AGG-ID: QByXxKtnN7qmIIzgcX5PLQ Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-aafc90962ffso95561966b.0 for ; Thu, 09 Jan 2025 05:38:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736429909; x=1737034709; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zguszej+6js0jv121He/RU1+H3xrJy3UeZApbWTSTbc=; b=JilzMcyyhRjfm0O8A1vvy2upMx6VwUleRqSiPS2sjRSznAID7hTQJdtBb+APm/oO+H qKkyQbZ49xN+5+xAn1eV5pmDhkyuh4Nhhn2NJUEZHDBq+fKmSf1vM2a+o7R0HpGBVJim gX6rUBnMuaSPbKmdcpfqgqzqS8/0uTo6isl9b8ezF+6531R54PC7AxRaItATs/Vrq3kV O14eCdQZJLfxUbOLY3dDALWkNu/mCnWmiOWe096FBLJhhvxpDV8D1KXfa9xVSKM9RBnc LWW+llomaoCUq9v5ou3wKw1UUmNkdJxR0b4mvClaYn3RDBxrTt++0xa+RWZoO2U+AzBh ksbA== X-Forwarded-Encrypted: i=1; AJvYcCVNmU9dfVv98GOQJctgE4Mo22ctOt9m+Fd+6nThGkNOhsfqe0h8j79MCv2T8LGZo+sKqis=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+uutdv9Ll96xSaGU1Kc4mmcXMB7wshVQz3MwuXudjvO9OaHoe LJ3ktkeKOrxB/mUdIg/pWsRxChmqyWJzv7lhyzJH8yL6npFzaVr6MGqHBwoNDl3Zn/tCSDkugLd VoPDvB0wfDU9mpWe9Xkd7FlPssKwgyLJmlKoniOCPSDFtcjIi9A== X-Gm-Gg: ASbGnctoFbwvva2IvI+sWR3bhGU3LTRwp7DzsjN2JsL2MA0F/vAtvoB/ojjOxcfpfeC rYatOi/v63P2jHcc8YflybvD86OCFOtu4QjRxO73Bf1x7/+BaZilkxhxiWnSLzSBhM/drRYC+v9 BWMNmgm4atG5aVLUiA59R/4YbzSFLoQYjdO1e+98d8Vf1v0mKkOn/CbENxtLe43VZAeyUJl1o5p SMGA0H4+KWrRSWH8yNf4u+Vyo0ptLU42V3hx6zwvZuCcCxCNDPBM39JI794 X-Received: by 2002:a17:907:724b:b0:aa6:abb2:be12 with SMTP id a640c23a62f3a-ab2abc91b53mr573667666b.37.1736429908683; Thu, 09 Jan 2025 05:38:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IGo9tyQhMRCjTpqjQKY5ONk4yRTr2KDDRMNsirKe/0lHnxJJpvXbaOhKUZHyIFh2cUX8sdNHg== X-Received: by 2002:a17:907:724b:b0:aa6:abb2:be12 with SMTP id a640c23a62f3a-ab2abc91b53mr573664766b.37.1736429908325; Thu, 09 Jan 2025 05:38:28 -0800 (PST) Received: from [192.168.10.47] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab2c95af187sm73137566b.142.2025.01.09.05.38.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jan 2025 05:38:27 -0800 (PST) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: oliver.upton@linux.dev, Will Deacon , Anup Patel , Andrew Jones , seanjc@google.com, linuxppc-dev@lists.ozlabs.org, regressions@lists.linux.dev Subject: [PATCH 3/5] KVM: e500: track host-writability of pages Date: Thu, 9 Jan 2025 14:38:15 +0100 Message-ID: <20250109133817.314401-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250109133817.314401-1-pbonzini@redhat.com> References: <20250109133817.314401-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add the possibility of marking a page so that the UW and SW bits are force-cleared. This is stored in the private info so that it persists across multiple calls to kvmppc_e500_setup_stlbe. Signed-off-by: Paolo Bonzini --- arch/powerpc/kvm/e500.h | 2 ++ arch/powerpc/kvm/e500_mmu_host.c | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/arch/powerpc/kvm/e500.h b/arch/powerpc/kvm/e500.h index 6d0d329cbb35..f9acf866c709 100644 --- a/arch/powerpc/kvm/e500.h +++ b/arch/powerpc/kvm/e500.h @@ -34,6 +34,8 @@ enum vcpu_ftr { #define E500_TLB_BITMAP (1 << 30) /* TLB1 entry is mapped by host TLB0 */ #define E500_TLB_TLB0 (1 << 29) +/* entry is writable on the host */ +#define E500_TLB_WRITABLE (1 << 28) /* bits [6-5] MAS2_X1 and MAS2_X0 and [4-0] bits for WIMGE */ #define E500_TLB_MAS2_ATTR (0x7f) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index 06e23c625be0..e332a10fff00 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -45,11 +45,14 @@ static inline unsigned int tlb1_max_shadow_size(void) return host_tlb_params[1].entries - tlbcam_index - 1; } -static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode) +static inline u32 e500_shadow_mas3_attrib(u32 mas3, bool writable, int usermode) { /* Mask off reserved bits. */ mas3 &= MAS3_ATTRIB_MASK; + if (!writable) + mas3 &= ~(MAS3_UW|MAS3_SW); + #ifndef CONFIG_KVM_BOOKE_HV if (!usermode) { /* Guest is in supervisor mode, @@ -244,10 +247,13 @@ static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe) static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref, struct kvm_book3e_206_tlb_entry *gtlbe, - kvm_pfn_t pfn, unsigned int wimg) + kvm_pfn_t pfn, unsigned int wimg, + bool writable) { ref->pfn = pfn; ref->flags = E500_TLB_VALID; + if (writable) + ref->flags |= E500_TLB_WRITABLE; /* Use guest supplied MAS2_G and MAS2_E */ ref->flags |= (gtlbe->mas2 & MAS2_ATTRIB_MASK) | wimg; @@ -303,6 +309,7 @@ static void kvmppc_e500_setup_stlbe( { kvm_pfn_t pfn = ref->pfn; u32 pr = vcpu->arch.shared->msr & MSR_PR; + bool writable = !!(ref->flags & E500_TLB_WRITABLE); BUG_ON(!(ref->flags & E500_TLB_VALID)); @@ -310,7 +317,7 @@ static void kvmppc_e500_setup_stlbe( stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID; stlbe->mas2 = (gvaddr & MAS2_EPN) | (ref->flags & E500_TLB_MAS2_ATTR); stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) | - e500_shadow_mas3_attrib(gtlbe->mas7_3, pr); + e500_shadow_mas3_attrib(gtlbe->mas7_3, writable, pr); } static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, @@ -492,7 +499,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, } } - kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg); + kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg, true); kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize, ref, gvaddr, stlbe); writable = tlbe_is_writable(stlbe); From patchwork Thu Jan 9 13:38:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13932585 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5D6B221DAB for ; Thu, 9 Jan 2025 13:38:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429916; cv=none; b=b4SVhut74IZjqMVVxMbSVVmAiWEPGfun7HBJggqrcf7nm/XFf8vJjx6iJnGViugNGgL/5B9z32/8VUM//l6id6M+jdiq6p6Z/oouId1PIR8D+Wfgjyw2tGp/Aa2q735clWaZnLOHuEOl466CV73AwGWmA7B1d0FjyYfJ9/+rd2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429916; c=relaxed/simple; bh=iNT9KPtrkMdcv/JnCsg0+6j7kTEB+dZG7xYjFTS5aX8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=riCaE2fBmhZs8y/KENnqhmrHAVK+tV7PzyFOYRpkZLl4jvZ0MlrzjPWOx4bpbxCiB0dFgT1Tut99GkvDPBPZKB5NoZm2okZ/6MPhcbrT/yMSkIUtlGd0eIOmjhjVfksdfopKtX+AD00jVknjQxUZ5et7IN3jKwLUyXEpHyEuFMk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=NHs1/KEK; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NHs1/KEK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736429913; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y4qkNvDxtryDJuaOhZabXXDqOZ28SJd16eOJBotltio=; b=NHs1/KEKNATP/8dD/qoXG7gyKO4Ce4uoKyekzHtmBKXl6vSyapSAIrcj5g3hyDBo3UoaTa 58LhJFMtIbstWoyRLW5cu2m4KKDSgj4qpgt6wiCiBV9vY286JotGCGuPRgQL3Ck09igZGa /bLoVjKSIJwzqNFMQf65yL14DEaIDSw= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-67-0oofM-aOPQSOjlmqGHSEEQ-1; Thu, 09 Jan 2025 08:38:32 -0500 X-MC-Unique: 0oofM-aOPQSOjlmqGHSEEQ-1 X-Mimecast-MFC-AGG-ID: 0oofM-aOPQSOjlmqGHSEEQ Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-aa689b88293so95486766b.3 for ; Thu, 09 Jan 2025 05:38:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736429911; x=1737034711; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y4qkNvDxtryDJuaOhZabXXDqOZ28SJd16eOJBotltio=; b=GDf8Be4z4mB/VNXzbYP6Yi+GX5bW5k3xiqq9hYouLfyJFZiqbdXWXSuuhe2gDw8GVh ZntmMCiKNMhK07XmQrgZAaDZebuiQ+fJATNI2pSTMadEvtzpu3y3k3k2U0R5ImKmt+qE 6Yhx5NfZp8eXMybY3pXMIMPHtHosYbPaqNFKR2av7I+ffRKdkhOj64Is2I1lCU3ZFv7O cnhC83G8BHDbkEeLskFhdvWzpz9isOIbGkQOOg5hsFHdXejuopGPJeOFoSye03Q5SDTK EQZ5p05iFx9Xv57EAfFTyDoHhtkLxTwHPH+fdLFXbNxELiARfRSzKEEzx7kAvuD8u1AY 9I2w== X-Forwarded-Encrypted: i=1; AJvYcCWuhwaURHsIX+KTf9KjkJWUTgIwGhfwRh6YgNgUrrXdaHSRr24syL8UzMZXYu29TWrqVro=@vger.kernel.org X-Gm-Message-State: AOJu0YzRUnrWNCOHKxU1H55UZ2SehlkCdN42dVnSomKPdO30K3DpnJSz 365G/2u8vvViCZpPyJeg+F+jRm7gL3fDlyvP5LadPfrWKaxd3noG/GHHI85/uxg4MKWp1/O8EQh VSmB+522/YQE8022JaPslg5NXP2kmVMC+AJkfQptKa3rWNhWi0A== X-Gm-Gg: ASbGncsdCY3WgKKE/I3aGfn/5OLTW9OMwy/nxsOeXsGTKoj9CaO/0WIDl/hI1bMzp8/ A63YKc8MVg/TamO3djnr/PdwTpCW1lFR4uLInHEUKIyyyGKdDdXe79vSa+/NOhPTdVpjBYdk8Z/ fkM1VTqk79S6/TmA/YKYblGaRA1jJIxo2n+6C5saYpubfmA2wpz4imxNu71LdzRIIe+iwZ5BQIG s88KkXIXAqxuzA6e2o5tOPPUAvvm0UY+YzxAUR7/XdhIJtp9PcWlg9ezyGf X-Received: by 2002:a17:907:97c6:b0:aaf:208:fd3f with SMTP id a640c23a62f3a-ab2ab558911mr610031766b.13.1736429911049; Thu, 09 Jan 2025 05:38:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IEcNnAC+O5NQsf3e6ZbNH5VgDVjtZpeWPQB3s6uy9MQKvp52Beiu2u+bl2BWL3zXg+RcIfN2Q== X-Received: by 2002:a17:907:97c6:b0:aaf:208:fd3f with SMTP id a640c23a62f3a-ab2ab558911mr610029466b.13.1736429910681; Thu, 09 Jan 2025 05:38:30 -0800 (PST) Received: from [192.168.10.47] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab2c90d6838sm74386766b.55.2025.01.09.05.38.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jan 2025 05:38:29 -0800 (PST) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: oliver.upton@linux.dev, Will Deacon , Anup Patel , Andrew Jones , seanjc@google.com, linuxppc-dev@lists.ozlabs.org, regressions@lists.linux.dev, Christian Zigotzky Subject: [PATCH 4/5] KVM: e500: map readonly host pages for read Date: Thu, 9 Jan 2025 14:38:16 +0100 Message-ID: <20250109133817.314401-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250109133817.314401-1-pbonzini@redhat.com> References: <20250109133817.314401-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The new __kvm_faultin_pfn() function is upset by the fact that e500 KVM ignores host page permissions - __kvm_faultin requires a "writable" outgoing argument, but e500 KVM is nonchalantly passing NULL. If the host page permissions do not include writability, the shadow TLB entry is forcibly mapped read-only. Reported-by: Christian Zigotzky Signed-off-by: Paolo Bonzini --- arch/powerpc/kvm/e500_mmu_host.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index e332a10fff00..7752b7f24c51 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -379,6 +379,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, unsigned long slot_start, slot_end; pfnmap = 1; + writable = vma->vm_flags & VM_WRITE; start = vma->vm_pgoff; end = start + @@ -454,7 +455,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, if (likely(!pfnmap)) { tsize_pages = 1UL << (tsize + 10 - PAGE_SHIFT); - pfn = __kvm_faultin_pfn(slot, gfn, FOLL_WRITE, NULL, &page); + pfn = __kvm_faultin_pfn(slot, gfn, FOLL_WRITE, &writable, &page); if (is_error_noslot_pfn(pfn)) { if (printk_ratelimit()) pr_err("%s: real page not found for gfn %lx\n", @@ -499,7 +500,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, } } - kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg, true); + kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg, writable); kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize, ref, gvaddr, stlbe); writable = tlbe_is_writable(stlbe); From patchwork Thu Jan 9 13:38:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 13932586 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F86C2206B9 for ; Thu, 9 Jan 2025 13:38:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429918; cv=none; b=A1tVShsUg1xExGADyhVJypeq2an1MS6giXwCR4LxfpjELlAgNqxiEX7Y/zZRV9FgJS6+LOtkq0dVbtV+YfmwOjI7wy00vVo1V4LVaJO7MmBmAWXq9DL6W1DkdALzp4gWH3OCB3wOvelySDoE/7uwWe7eySYjtyJXPkCinn8Q+44= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736429918; c=relaxed/simple; bh=P7sSfp0wHvngJJFC7alhWn4VcAvboR/GnnVkh2D7CyU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jp3Z23QpgfP/4f0MeasjiWG68Z/S1iy2lhVEOsbcyLDmgAqIQFNOkLp/+O/iL1uYcSibq5q8rpMabPlEVRbEyqaVKuoAXoHUKPKiGBKlxq5XRPNcYnbRpT0Z+Gx+ULLQbuyrM2+YAobtXuqyNTPQGFCjF30IpImviw/bnfQ3HHo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YVJLmmvt; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YVJLmmvt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736429915; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6U5GaYmzSeHIwPHM2TLhTcGPV4HaT+bdvS3rFAmfbSs=; b=YVJLmmvtLiaG90URK05B/4L6UHc5Xhop3vVYGy4ncntqkbaJN46HJtSzBIxvBYZIqMJuzX m+QmU5M3Wf1dWfTfCPQm8flekRfO7N8Y42DP7w/bZ1MFqVyLweVaJDJkYUudlnnOGPvWyV scTAMmlQWpQdGf8DEIKKWZrRwhTDqjk= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-524-yKKacJ_DM7W4uYrrEVR3HQ-1; Thu, 09 Jan 2025 08:38:34 -0500 X-MC-Unique: yKKacJ_DM7W4uYrrEVR3HQ-1 X-Mimecast-MFC-AGG-ID: yKKacJ_DM7W4uYrrEVR3HQ Received: by mail-ed1-f69.google.com with SMTP id 4fb4d7f45d1cf-5d40c0c728aso685728a12.2 for ; Thu, 09 Jan 2025 05:38:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736429913; x=1737034713; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6U5GaYmzSeHIwPHM2TLhTcGPV4HaT+bdvS3rFAmfbSs=; b=Yji7FOiwJqB+nj9W+o35t3N4bgG1cNY3UH67P88SckerAvwHPqULZjoEwQKQuNeqLj ZtxDY2QFz9bPP99yGwWMk9UnIVH7GLVbeGthHftpNrR/yH93uPg0pzdbVY6/pKX8R45k 2gtLqmPEllQAnJTRcgfD8PvkPfRxYxLh38BGDmjTwcTtV4rHmab77YI3Y/ICK4kFC7ZY gPDnLKoCqgMbSJVoekTlvaemkM6ni93ZzVRjcj7QAEmjUgd3E0UYlbGfpz5MvNMLb0AH ARPpbvRA1boTSAxDoRoSFsGA4t+HFKYaTAiNuGEL3lWZSw/EShdrWYOayJUbn34YZrt8 mcUg== X-Forwarded-Encrypted: i=1; AJvYcCVKk00fD4ayHKbITvYTviCF4YKiOeGvDODwVO5aTvvRrSGwapl+3fXFezQC5ude5lb+/SQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxWvjqDczKffg32hzq1V3SBVlxijIU3C60QTPb7PErR7kB3S/gj nYYAoxLj1J+d5eoqBybQC4ljRRupI78NiQcGQHM9iushdEuDkaDbWWYzienhr4mQS2puwvPINhv RZT5wXYRFR6cohrKpQumJCb6wTFk9zSfZGVnWghnmY8V3TediJg== X-Gm-Gg: ASbGncvs2iO7o72NZN9+bkfrt9Uwds70+LpQ1b1A7+A3h3XoNJJM8tWF3yPE/PKFaoM RYmQv5QzJA/rw1xP7ivXh3kBpcIkz2W7NLF5KYMg9ue8X7tfb8+wr77ae6Jyz9alQ9AFB/wV4EW 7MoEmaKA411zYdT4sVlafMLRIgXj9AYLA+nHJpT6m95mYvq1RpHGyFKM7XaJkpt3V0miBZAyHgV jR+rtHSRV6Y1ydN6p6Tvz9UpYx1oPslRxO9vJ/M6lAiaFB+BJoIA6pgsIHD X-Received: by 2002:a05:6402:5251:b0:5d0:bf5e:eb8 with SMTP id 4fb4d7f45d1cf-5d972e63ddfmr13854959a12.23.1736429913102; Thu, 09 Jan 2025 05:38:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IEvrvZyCjR9P8H1XfDQSbpZ1ii019x3nOmgFJfPU4BSjpH27IgNbkbMhVv45gD16B6Iwt+Skw== X-Received: by 2002:a05:6402:5251:b0:5d0:bf5e:eb8 with SMTP id 4fb4d7f45d1cf-5d972e63ddfmr13854906a12.23.1736429912705; Thu, 09 Jan 2025 05:38:32 -0800 (PST) Received: from [192.168.10.47] ([151.62.105.73]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab2c90d6a4csm73856966b.71.2025.01.09.05.38.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jan 2025 05:38:32 -0800 (PST) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: oliver.upton@linux.dev, Will Deacon , Anup Patel , Andrew Jones , seanjc@google.com, linuxppc-dev@lists.ozlabs.org, regressions@lists.linux.dev Subject: [PATCH 5/5] KVM: e500: perform hugepage check after looking up the PFN Date: Thu, 9 Jan 2025 14:38:17 +0100 Message-ID: <20250109133817.314401-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250109133817.314401-1-pbonzini@redhat.com> References: <20250109133817.314401-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 e500 KVM tries to bypass __kvm_faultin_pfn() in order to map VM_PFNMAP VMAs as huge pages. This is a Bad Idea because VM_PFNMAP VMAs could become noncontiguous as a result of callsto remap_pfn_range(). Instead, use the already existing host PTE lookup to retrieve a valid host-side mapping level after __kvm_faultin_pfn() has returned. Then find the largest size that will satisfy the guest's request while staying within a single host PTE. Signed-off-by: Paolo Bonzini --- arch/powerpc/kvm/e500_mmu_host.c | 180 ++++++++++++------------------- 1 file changed, 70 insertions(+), 110 deletions(-) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index 7752b7f24c51..0457bbc2526f 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -326,15 +326,14 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, struct tlbe_ref *ref) { struct kvm_memory_slot *slot; - unsigned long pfn = 0; /* silence GCC warning */ + unsigned int psize; + unsigned long pfn; struct page *page = NULL; unsigned long hva; - int pfnmap = 0; int tsize = BOOK3E_PAGESZ_4K; int ret = 0; unsigned long mmu_seq; struct kvm *kvm = vcpu_e500->vcpu.kvm; - unsigned long tsize_pages = 0; pte_t *ptep; unsigned int wimg = 0; pgd_t *pgdir; @@ -361,111 +360,12 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, hva = gfn_to_hva_memslot(slot, gfn); - if (tlbsel == 1) { - struct vm_area_struct *vma; - mmap_read_lock(kvm->mm); - - vma = find_vma(kvm->mm, hva); - if (vma && hva >= vma->vm_start && - (vma->vm_flags & VM_PFNMAP)) { - /* - * This VMA is a physically contiguous region (e.g. - * /dev/mem) that bypasses normal Linux page - * management. Find the overlap between the - * vma and the memslot. - */ - - unsigned long start, end; - unsigned long slot_start, slot_end; - - pfnmap = 1; - writable = vma->vm_flags & VM_WRITE; - - start = vma->vm_pgoff; - end = start + - vma_pages(vma); - - pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT); - - slot_start = pfn - (gfn - slot->base_gfn); - slot_end = slot_start + slot->npages; - - if (start < slot_start) - start = slot_start; - if (end > slot_end) - end = slot_end; - - tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >> - MAS1_TSIZE_SHIFT; - - /* - * e500 doesn't implement the lowest tsize bit, - * or 1K pages. - */ - tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1); - - /* - * Now find the largest tsize (up to what the guest - * requested) that will cover gfn, stay within the - * range, and for which gfn and pfn are mutually - * aligned. - */ - - for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) { - unsigned long gfn_start, gfn_end; - tsize_pages = 1UL << (tsize - 2); - - gfn_start = gfn & ~(tsize_pages - 1); - gfn_end = gfn_start + tsize_pages; - - if (gfn_start + pfn - gfn < start) - continue; - if (gfn_end + pfn - gfn > end) - continue; - if ((gfn & (tsize_pages - 1)) != - (pfn & (tsize_pages - 1))) - continue; - - gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1); - pfn &= ~(tsize_pages - 1); - break; - } - } else if (vma && hva >= vma->vm_start && - is_vm_hugetlb_page(vma)) { - unsigned long psize = vma_kernel_pagesize(vma); - - tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >> - MAS1_TSIZE_SHIFT; - - /* - * Take the largest page size that satisfies both host - * and guest mapping - */ - tsize = min(__ilog2(psize) - 10, tsize); - - /* - * e500 doesn't implement the lowest tsize bit, - * or 1K pages. - */ - tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1); - } - - mmap_read_unlock(kvm->mm); - } - - if (likely(!pfnmap)) { - tsize_pages = 1UL << (tsize + 10 - PAGE_SHIFT); - pfn = __kvm_faultin_pfn(slot, gfn, FOLL_WRITE, &writable, &page); - if (is_error_noslot_pfn(pfn)) { - if (printk_ratelimit()) - pr_err("%s: real page not found for gfn %lx\n", - __func__, (long)gfn); - return -EINVAL; - } - - /* Align guest and physical address to page map boundaries */ - pfn &= ~(tsize_pages - 1); - gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1); + pfn = __kvm_faultin_pfn(slot, gfn, FOLL_WRITE, &writable, &page); + if (is_error_noslot_pfn(pfn)) { + if (printk_ratelimit()) + pr_err("%s: real page not found for gfn %lx\n", + __func__, (long)gfn); + return -EINVAL; } spin_lock(&kvm->mmu_lock); @@ -483,7 +383,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, * can't run hence pfn won't change. */ local_irq_save(flags); - ptep = find_linux_pte(pgdir, hva, NULL, NULL); + ptep = find_linux_pte(pgdir, hva, NULL, &psize); if (ptep) { pte_t pte = READ_ONCE(*ptep); @@ -500,6 +400,66 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, } } + if (psize && tlbsel == 1) { + unsigned long psize_pages, tsize_pages; + unsigned long start, end; + unsigned long slot_start, slot_end; + + psize_pages = 1UL << (psize - PAGE_SHIFT); + start = pfn & ~(psize_pages - 1); + end = start + psize_pages; + + slot_start = pfn - (gfn - slot->base_gfn); + slot_end = slot_start + slot->npages; + + if (start < slot_start) + start = slot_start; + if (end > slot_end) + end = slot_end; + + tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >> + MAS1_TSIZE_SHIFT; + + /* + * Any page size that doesn't satisfy the host mapping + * will fail the start and end tests. + */ + tsize = min(psize - PAGE_SHIFT + BOOK3E_PAGESZ_4K, tsize); + + /* + * e500 doesn't implement the lowest tsize bit, + * or 1K pages. + */ + tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1); + + /* + * Now find the largest tsize (up to what the guest + * requested) that will cover gfn, stay within the + * range, and for which gfn and pfn are mutually + * aligned. + */ + + for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) { + unsigned long gfn_start, gfn_end; + tsize_pages = 1UL << (tsize - 2); + + gfn_start = gfn & ~(tsize_pages - 1); + gfn_end = gfn_start + tsize_pages; + + if (gfn_start + pfn - gfn < start) + continue; + if (gfn_end + pfn - gfn > end) + continue; + if ((gfn & (tsize_pages - 1)) != + (pfn & (tsize_pages - 1))) + continue; + + gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1); + pfn &= ~(tsize_pages - 1); + break; + } + } + kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg, writable); kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize, ref, gvaddr, stlbe);