Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp746877img; Fri, 22 Mar 2019 07:45:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqzgwu4vq3CJgT6Sic/RFvryfWSBdc65DAStSonlaikItmSF44cY3an2j6DSEWLsDN+SINmX X-Received: by 2002:a65:6203:: with SMTP id d3mr9309481pgv.109.1553265927477; Fri, 22 Mar 2019 07:45:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553265927; cv=none; d=google.com; s=arc-20160816; b=mQxSu0SLES3XUWikJzGRXVH0OXMSuW/R1+hWJqcyWqqLsMj9bNKFEcRymnvV0Mo1qV aa2S3Znbt8YBs27p7OlFhO3BfJglsMGPz1Zjl2bJcePoFNFD1BuSTLemKyQs35TeAjo2 LZhLsuJPtumgfb+zHFIhyHfOZ31+nlghSOk5y+lrq8hRDZVtTL+jKI4wUOMyUu3LncD/ IHgKVxJdRGNcIm2CoYkLY39v1OLq59Xa03Qne7IiBlS4e8ZUroWPRmZ9usBs6RXBHRvT ub2AwBISiX4t9fdiWb5HvhEw9XfEepzbc0017+zFiv3dZUxve/lnPUT64ht5YTrNDM+r L/Mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=8O4GsUDxjl2WRK5o3FC2kvsG0FtBt2WhZOcwM1pJYlQ=; b=I3e5jme1CSCy5KKchuZXZFRNz4O6OcC44rY0tRDwIzwGY9FKFVMn/mTzlTtQLnwYcl 9i3PSfDzSKXR/c0IQf0yxkQAIB4UYfvOCYHWaTM7979ygRp4EbHLFntX+vosw67VoUQG wXLPsDEhT7viaKPLpQ4K1aKaroqb2ImeBfLrV3FggHnJjvimb/cpjZcF27kxgM3u1SX4 9NZGRpTIC3yI7YW2wC9CpbBze+ctt0ecQ8LaPqOivoEfwuJsJTIcYL+aY8urKA9YTWVL Tmer+gWQ1BNAv9ebKQUYSkP7uM/GLTJyT/tjAKaQM56qiL3RL8P2veLCUX/TkzRI3+y0 Cvlw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o14si6775821pgv.310.2019.03.22.07.45.12; Fri, 22 Mar 2019 07:45:27 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729260AbfCVOn6 (ORCPT + 99 others); Fri, 22 Mar 2019 10:43:58 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:47514 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728834AbfCVOn6 (ORCPT ); Fri, 22 Mar 2019 10:43:58 -0400 Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x2MEeZDI077169 for ; Fri, 22 Mar 2019 10:43:56 -0400 Received: from e06smtp02.uk.ibm.com (e06smtp02.uk.ibm.com [195.75.94.98]) by mx0a-001b2d01.pphosted.com with ESMTP id 2rd0sajs0y-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 22 Mar 2019 10:43:55 -0400 Received: from localhost by e06smtp02.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 22 Mar 2019 14:43:47 -0000 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp02.uk.ibm.com (192.168.101.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 22 Mar 2019 14:43:44 -0000 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x2MEhnIk61276288 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 22 Mar 2019 14:43:49 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 445454C04A; Fri, 22 Mar 2019 14:43:49 +0000 (GMT) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A733F4C040; Fri, 22 Mar 2019 14:43:48 +0000 (GMT) Received: from morel-ThinkPad-W530.boeblingen.de.ibm.com (unknown [9.145.173.187]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 22 Mar 2019 14:43:48 +0000 (GMT) From: Pierre Morel To: borntraeger@de.ibm.com Cc: alex.williamson@redhat.com, cohuck@redhat.com, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, kvm@vger.kernel.org, frankja@linux.ibm.com, akrowiak@linux.ibm.com, pasic@linux.ibm.com, david@redhat.com, schwidefsky@de.ibm.com, heiko.carstens@de.ibm.com, freude@linux.ibm.com, mimu@linux.ibm.com Subject: [PATCH v6 0/7] vfio: ap: AP Queue Interrupt Control Date: Fri, 22 Mar 2019 15:43:41 +0100 X-Mailer: git-send-email 2.7.4 X-TM-AS-GCONF: 00 x-cbid: 19032214-0008-0000-0000-000002D063A6 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19032214-0009-0000-0000-0000223C8488 Message-Id: <1553265828-27823-1-git-send-email-pmorel@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-03-22_08:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=945 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1903220108 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch implement PQAP/AQIC interception in KVM. To implement this we need to add a new structure, vfio_ap_queue,to be able to retrieve the mediated device associated with a queue and specific values needed to register/unregister the interrupt structures: - APQN: to be able to issue the commands and search for queue structures - NIB : to unpin the NIB on clear IRQ - ISC : to unregister with the GIB interface - MATRIX: a pointer to the matrix mediated device - LIST: the list_head to handle the vfio_queue life cycle Having this structure and the list management greatly ease the handling of the AP queues and diminues the LOCs needed in the vfio_ap driver by more than 150 lines in comparison with the previous version. 0) Queues life cycle vfio_ap_queues are created on probe We define one bucket on the matrix device to store the free vfio_ap_queues, the queues not assign to any matrix mediated device. We define one bucket on each matrix mediated device to hold the vfio_ap_queues belonging to it. vfio_ap_queues are deleted on remove This makes the search for a queue easy and the detection of assignent incoherency obvious (the queue is not avilable) and simplifies assignment. 1) Phase 1, probe and remove from vfio_ap_queue The vfio_ap_queue structures are dynamically allocated and setup when a queue is probed by the ap_vfio_driver. The vfio_ap_queue is linked to the ap_queue device as the driver data. The new The vfio_ap_queue is put on a free_list belonging to the matrix device. The vfio_ap_queue are free during remove. 2) Phase 2, assignment of vfio_ap_queue to a mediated device When a APID is assigned we look for APQI already assigned to the matrix mediated device and associate all the queue with the APQN = (APID,APQI) to the mediated device by adding them to the mediated device queue list. We do the same when a APQI is assigned. If any queue with a matching APQN can not be found on the matrix device free list it means it is already associated to another matrix mediated device and no queue is added to the matrix mediated device. 3) Phase 3, starting the guest When the VFIO device is opened the PQAP callback and a pointer to the matrix mediated device are set inside KVM during the open callback. When the device is closed or if a queue is removed, the vfio_ap_queue is dissociated from the mediated device. 4) Phase 3 intercepting the PQAP/AQIC instruction On interception of the PQAP/AQIC instruction, the interception code verifies that AP instructions are available on hardware and in the guest and retrun the usual -EOPNOTSUPP return code to let QEMU handle the fault if it is not the case. If instructions are allowed but intercepted it can only be due to specifications errors in instruction usage or to the valid interception of PQAP/AQIC. In this case, we make sure the pqap_hook is initialized and call it. Otherwise, if the hook is not initialize, we assume that there is no VFIO AP driver to handle the CRYCB which is consequently empty and setup a response as AP queue unavailable. the pqap callback search for the queue asociated with the APQN stored in the register 0, setting the code to "illegal APQN" if the vfio_ap_queue can not be found. Depending on the "i" bit of the register 1, the pqap callback setup or clear the interruption by calling the host format PQAP/AQIC instruction. When seting up the interruption it uses the NIB and the guest ISC provided by the guest and the host ISC provided by the registration to the GIB code, pin the NIB and also stores ISC and NIB inside the vfio_ap_queue structure. When clearing the interrupt it retrieves the host ISC to unregister with the GIB code and unpin the NIB. We take care when enabling GISA that the guest may have issued a reset and will not need to disable the interuptions before re-enabling interruptions. To make sure that the module holding the callback does not disapear we use a module reference counting in the structure containing the callback. 5) Phase 4 clean dissociation from the mediated device on remove On removing of the AP device the remove callback is called. To be sure that the guest will not access the queue anymore we clear the APID CRYCB bit. Cleaning the APID, over the APQI, is chosen because the architecture specifies that only the APID can be dynamically changed outside IPL. To be sure that the IRQ is cleared before the GISA is free we use the KVM reference counting, raise it in open, lower it on release. 6) Associated QEMU patch There is a QEMU patch which is needed to enable the PQAP/AQIC facility in the guest. Posted in qemu-devel@nongnu.org as: Message-Id: <1550146494-21085-1-git-send-email-pmorel@linux.ibm.com> Pierre Morel (7): s390: ap: kvm: add PQAP interception for AQIC s390: ap: new vfio_ap_queue structure s390: ap: setup relation betwen KVM and mediated device vfio: ap: register IOMMU VFIO notifier s390: ap: implement PAPQ AQIC interception in kernel s390: ap: Cleanup on removing the AP device s390: ap: kvm: Enable PQAP/AQIC facility for the guest arch/s390/include/asm/kvm_host.h | 8 + arch/s390/kvm/priv.c | 90 ++++ arch/s390/tools/gen_facilities.c | 1 + drivers/s390/crypto/ap_bus.h | 1 + drivers/s390/crypto/vfio_ap_drv.c | 69 ++- drivers/s390/crypto/vfio_ap_ops.c | 784 +++++++++++++++++++++++----------- drivers/s390/crypto/vfio_ap_private.h | 20 + 7 files changed, 728 insertions(+), 245 deletions(-) -- 2.7.4 Changelog from v5: - Refactoring of the PQAP interception after all discussions (Conny, Halil (offline)) - take a big lock around open to avoid parallel changes through assignment - verify that at least one queue has a APID or APQI when first assignment is done to not accept unavailable APID/APQI (myself) - Adding comment for locks on free_list (Conny) - Modified comment for "s390: ap: setup relation betwen KVM and mediated device" (Halil) Changelog from v4: - Add forgotten locking for vfio_get_queue() in pqap callback (Conny / Halil) - Add KVM reference counting to make sure GISA is free after IRQ (Christian / Halil) - Take care that ISC = 0 is a valid ISC (Halil) - Integrate the PQAP call back in a structure with module owner reference counting to make sure the callback does not disappear. - Restrict functionality to always open KVM before opening the VFIO device. - Search all devices in the vfio_ap driver list when associating a queue to a mediated device (Halil / Tony) - Get vfio_ap_free_irq() out of vfio_ap_mdev_reset_queue() to call it always, whatever the result of the reset. (Tony) Changelog from v3: - Associating the vfio_queues during APID/APQI assign (Tony) - Dissociating the vfio_queues during APID/APQI unassign (Tony) - Taking care that the guest can directly disable the interrupt by using a RESET (Halil) - Remove the patch creating the matrix bus to accelerate its integration in Linux stable (Christian)