Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp5984111ybx; Mon, 11 Nov 2019 01:53:16 -0800 (PST) X-Google-Smtp-Source: APXvYqyFuGFfxbaYRf/4kszJZJya6og8v5KaHNEmY2jofjM1skL7H7ZcDmQUNd7hPYzy4RG6OS6F X-Received: by 2002:a50:f987:: with SMTP id q7mr25209783edn.205.1573465996030; Mon, 11 Nov 2019 01:53:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573465996; cv=none; d=google.com; s=arc-20160816; b=ErnuXVpB5nui0Ydqz1Rp2rySU9DI3hrG43JONoDNJmdU3rFKPk+0vhOAW4QwoOZ71N rfFZQNJKIYIvVqzucEWImBbJTWEJtQA84bhH84esQ2Z9B3PpVzN9fkOuhaSBod3XuZZj 9Ga6GANfMblMgxTzkoxHhxlW2l4CvFDuYw4gcb8oR8qAZZRPzM2NH9AL1qZImzgnuHjg 3HAOnADqZoFfasbkrOp2gFfA5Z9uoPY7nLnB/WESgEgXy1A2eBXBwIeSTuJV3EiIVDr5 1e/1Bx6e+jXwMqErJAUdYCsI9uPCDERWfFudgDtOet35g+W1B/JYIpWkEMz0mCiXTnZU DHyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:content-transfer-encoding :mime-version:user-agent:date:cc:to:from:subject; bh=pXoqM3ZJwgRNer+QDcdOvt/bFfPNS6UM1JdOt8m7aMY=; b=LI54mIO7PTx/fkatGrIPgSg+CLqnYHhHJJaAD/3RqK0K9bxC/VxGORe9NiBoj+br2A f5dpOwdBJ3ZofHCc5bbnivwWr2JW0811ag2JCycBx+/H8RPGp/29tkGAC4vH8mcvtZ/c OVhSGxtaetQV2QDDqgTKNkuYvvJ8c1IdJQ75tu4qVdl3mfMtU+yAz4OhzdxYriPKZ6PO EOaLlpdPVqPoD/j6HShHvbsZhOEh5Jy5nZaBV4DEqG/KpcVaQDugoLQPVFMXdpf9zX7f mPBy+KuOUTS1rTuVUBtFcpAKt8QVNORKxL48JPX03SnafWFvacpT6XC9k88tnAU2tbP8 I8XA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a3si2185839ejy.247.2019.11.11.01.52.52; Mon, 11 Nov 2019 01:53:16 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726978AbfKKJtg (ORCPT + 99 others); Mon, 11 Nov 2019 04:49:36 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:23694 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726960AbfKKJtf (ORCPT ); Mon, 11 Nov 2019 04:49:35 -0500 Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id xAB9kpxC013958 for ; Mon, 11 Nov 2019 04:49:34 -0500 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0a-001b2d01.pphosted.com with ESMTP id 2w741ub496-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 11 Nov 2019 04:49:33 -0500 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 11 Nov 2019 09:49:32 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Mon, 11 Nov 2019 09:49:28 -0000 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id xAB9nRvh35520728 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 11 Nov 2019 09:49:27 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 777474C059; Mon, 11 Nov 2019 09:49:27 +0000 (GMT) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 181CF4C044; Mon, 11 Nov 2019 09:49:27 +0000 (GMT) Received: from bahia.lan (unknown [9.145.184.11]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTP; Mon, 11 Nov 2019 09:49:27 +0000 (GMT) Subject: [PATCH] KVM: PPC: Book3S HV: XIVE: Free previous EQ page when setting up a new one From: Greg Kurz To: Paul Mackerras Cc: Michael Ellerman , Benjamin Herrenschmidt , =?utf-8?q?C=C3=A9dric?= Le Goater , David Gibson , Lijun Pan , Satheesh Rajendran , kvm-ppc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org Date: Mon, 11 Nov 2019 10:49:26 +0100 User-Agent: StGit/unknown-version MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 x-cbid: 19111109-4275-0000-0000-0000037CAF98 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19111109-4276-0000-0000-00003890081E Message-Id: <157346576671.818016.10401178701091199969.stgit@bahia.lan> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-11-11_02:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1034 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1910280000 definitions=main-1911110097 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The EQ page is allocated by the guest and then passed to the hypervisor with the H_INT_SET_QUEUE_CONFIG hcall. A reference is taken on the page before handing it over to the HW. This reference is dropped either when the guest issues the H_INT_RESET hcall or when the KVM device is released. But, the guest can legitimately call H_INT_SET_QUEUE_CONFIG several times to reset the EQ (vCPU hot unplug) or set a new EQ (guest reboot). In both cases the EQ page reference is leaked. This is especially visible when the guest memory is backed with huge pages: start a VM up to the guest userspace, either reboot it or unplug a vCPU, quit QEMU. The leak is observed by comparing the value of HugePages_Free in /proc/meminfo before and after the VM is run. Note that the EQ reset path seems to be calling put_page() but this is done after xive_native_configure_queue() which clears the qpage field in the XIVE queue structure, ie. the put_page() block is a nop and the previous page pointer was just overwritten anyway. In the other case of configuring a new EQ page, nothing seems to be done to release the old one. Fix both cases by always calling put_page() on the existing EQ page in kvmppc_xive_native_set_queue_config(). This is a seemless change for the EQ reset case. However this causes xive_native_configure_queue() to be called twice for the new EQ page case: one time to reset the EQ and another time to configure the new page. This is needed because we cannot release the EQ page before calling xive_native_configure_queue() since it may still be used by the HW. We cannot modify xive_native_configure_queue() to drop the reference either because this function is also used by the XICS-on-XIVE device which requires free_pages() instead of put_page(). This isn't a big deal anyway since H_INT_SET_QUEUE_CONFIG isn't a hot path. Reported-by: Satheesh Rajendran Cc: stable@vger.kernel.org # v5.2 Fixes: 13ce3297c576 ("KVM: PPC: Book3S HV: XIVE: Add controls for the EQ configuration") Signed-off-by: Greg Kurz --- arch/powerpc/kvm/book3s_xive_native.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/arch/powerpc/kvm/book3s_xive_native.c b/arch/powerpc/kvm/book3s_xive_native.c index 34bd123fa024..8ab908d23dc2 100644 --- a/arch/powerpc/kvm/book3s_xive_native.c +++ b/arch/powerpc/kvm/book3s_xive_native.c @@ -570,10 +570,12 @@ static int kvmppc_xive_native_set_queue_config(struct kvmppc_xive *xive, __func__, server, priority, kvm_eq.flags, kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex); - /* reset queue and disable queueing */ - if (!kvm_eq.qshift) { - q->guest_qaddr = 0; - q->guest_qshift = 0; + /* + * Reset queue and disable queueing. It will be re-enabled + * later on if the guest is configuring a new EQ page. + */ + if (q->guest_qshift) { + page = virt_to_page(q->qpage); rc = xive_native_configure_queue(xc->vp_id, q, priority, NULL, 0, true); @@ -583,12 +585,13 @@ static int kvmppc_xive_native_set_queue_config(struct kvmppc_xive *xive, return rc; } - if (q->qpage) { - put_page(virt_to_page(q->qpage)); - q->qpage = NULL; - } + put_page(page); - return 0; + if (!kvm_eq.qshift) { + q->guest_qaddr = 0; + q->guest_qshift = 0; + return 0; + } } /*