From patchwork Wed Apr 26 13:48:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudio Imbrenda X-Patchwork-Id: 87879 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp289180vqo; Wed, 26 Apr 2023 07:34:14 -0700 (PDT) X-Google-Smtp-Source: AKy350alZdJcqHCmo/eLfsRVlGjUAlyYuzxYi/O5i2u84QcQTqNTg4eSQvsSSdcgNTJNpJIeo5M1 X-Received: by 2002:a17:902:ebc6:b0:1a5:22a6:4e6a with SMTP id p6-20020a170902ebc600b001a522a64e6amr21868009plg.51.1682519654306; Wed, 26 Apr 2023 07:34:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682519654; cv=none; d=google.com; s=arc-20160816; b=InBH0vgKA7cW+leawTtiVZN/5t8WNzbMpQrxNHaA44A7WqB7TJXG6ihM1lx9QukI8q FifL4g9hVCDvEX5LEN70OLmlDMx9pDsa1bH5ke60QVUJDAQPfPitcOo1H3xUD9tChUwM x9sAOnc2KArVy1kp4A4qDMdVAo+VYllZjiQJKYeN0t6Dv+N22NZOyh8Dbst4Q44HJxxc vwqRJPSkR9/Jfxxd0QuGzWHjz3FmmucQBCmN+9QShN85NmEP3F2LczNtWT7BJngJHqjj 1OV5jv0YevCWHbvuwOlMUoFr2F1Ccata+/hlnw12jhHB8HgIoFLH3Am+u2eAp7sk2EVq YUwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=haMxrfNCcZsNWCFNARsj7f0YXA0WezkWwKNzmYY7JKA=; b=FpY+OdXjD4b4OYFQfmOmtfBsG7dMsCsfZ0WrphL7n/VjqiNuD4s92CzIPjQTIAjhIF zO5S6fwDzq68JI3+vy9yO390VSx1GBMY3M5YoXHE0HVIO9ptHVvC4Tv9LKUsmqMC8QvO bQSNES7T4Rw2M/EXeY7YVRBXbpzg21Dbdtx3wXtcWVsMDA/XWCpYgy/AzEMftZSLelxL pzvtN5MmPR3qMbwPsjU+O0jV4xk+TpuWRxyVdUW0Vh4QjnSCCvdPVKr2nZmys3YO0bzG /RH5Kz8zCRdqygE8DWzEk8Wgk+BssPRLVaOWvot0nu/3qVXj4gbl88q23xju/2bE3/XQ mZmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=ocEB6I71; 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=NONE dis=NONE) header.from=ibm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j6-20020a170902c08600b001a6a46d70ccsi15490931pld.1.2023.04.26.07.34.00; Wed, 26 Apr 2023 07:34:14 -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=@ibm.com header.s=pp1 header.b=ocEB6I71; 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=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241017AbjDZNsz (ORCPT + 99 others); Wed, 26 Apr 2023 09:48:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241084AbjDZNst (ORCPT ); Wed, 26 Apr 2023 09:48:49 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7A765586; Wed, 26 Apr 2023 06:48:45 -0700 (PDT) Received: from pps.filterd (m0353727.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33QDb5aS006964; Wed, 26 Apr 2023 13:48:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=haMxrfNCcZsNWCFNARsj7f0YXA0WezkWwKNzmYY7JKA=; b=ocEB6I71njLSFw4H8v4riWhXayMCnmBjL+kHG2cu3Y+m+UvT0Ok5gjyR6O5PioTWB6i9 JO2L6X+/yVyh00bmFXjVNFGRrCJRr3ZYLVo3vYTkebeDtIkupi1uk3vFrtiG/UQ9AFR1 cK6NTWQCsxWT0lKUE6WxX8pJeqGsp9Z00tw6MnZPpND6lFN0iE118s26a/1OSVGmS7AX 9kCy/F9x73P8YnQCO6hBgpYkM8qmIkRXxsUdd/QEkZ/NxWBrYgopvOH+pI2OullRMjMh RzRrz/L60XoKXsSX4ZR+Nyht6CK615YAuw8s5xzdPxxyjniDkkcXGuX1ra0OSBpv8hfZ Rg== Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3q74usgkf6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 26 Apr 2023 13:48:43 +0000 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 33QCTjnn017020; Wed, 26 Apr 2023 13:48:40 GMT Received: from smtprelay04.fra02v.mail.ibm.com ([9.218.2.228]) by ppma01fra.de.ibm.com (PPS) with ESMTPS id 3q47771yum-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 26 Apr 2023 13:48:40 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay04.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 33QDmYoY46072202 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 26 Apr 2023 13:48:35 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DC2E920040; Wed, 26 Apr 2023 13:48:34 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 97A452004B; Wed, 26 Apr 2023 13:48:34 +0000 (GMT) Received: from p-imbrenda.boeblingen.de.ibm.com (unknown [9.152.224.56]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 26 Apr 2023 13:48:34 +0000 (GMT) From: Claudio Imbrenda To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, frankja@linux.ibm.com, borntraeger@de.ibm.com, nrb@linux.ibm.com, nsg@linux.ibm.com, seiden@linux.ibm.com, jgg@nvidia.com Subject: [PATCH v2 1/1] KVM: s390: fix race in gmap_make_secure Date: Wed, 26 Apr 2023 15:48:34 +0200 Message-Id: <20230426134834.35199-2-imbrenda@linux.ibm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230426134834.35199-1-imbrenda@linux.ibm.com> References: <20230426134834.35199-1-imbrenda@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: H5ChbVY5Mlg6xUB7uQRnAt0mWqOJEbU7 X-Proofpoint-ORIG-GUID: H5ChbVY5Mlg6xUB7uQRnAt0mWqOJEbU7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-26_06,2023-04-26_03,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 phishscore=0 impostorscore=0 malwarescore=0 suspectscore=0 adultscore=0 spamscore=0 bulkscore=0 lowpriorityscore=0 mlxlogscore=787 clxscore=1015 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304260121 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764249728713887990?= X-GMAIL-MSGID: =?utf-8?q?1764249728713887990?= This patch fixes a potential race in gmap_make_secure and removes the last user of follow_page without FOLL_GET. Signed-off-by: Claudio Imbrenda Suggested-by: Jason Gunthorpe Fixes: 214d9bbcd3a6 ("s390/mm: provide memory management functions for protected KVM guests") --- arch/s390/kernel/uv.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/arch/s390/kernel/uv.c b/arch/s390/kernel/uv.c index 9f18a4af9c13..cb2ee06df286 100644 --- a/arch/s390/kernel/uv.c +++ b/arch/s390/kernel/uv.c @@ -192,21 +192,10 @@ static int expected_page_refs(struct page *page) return res; } -static int make_secure_pte(pte_t *ptep, unsigned long addr, - struct page *exp_page, struct uv_cb_header *uvcb) +static int make_page_secure(struct page *page, struct uv_cb_header *uvcb) { - pte_t entry = READ_ONCE(*ptep); - struct page *page; int expected, cc = 0; - if (!pte_present(entry)) - return -ENXIO; - if (pte_val(entry) & _PAGE_INVALID) - return -ENXIO; - - page = pte_page(entry); - if (page != exp_page) - return -ENXIO; if (PageWriteback(page)) return -EAGAIN; expected = expected_page_refs(page); @@ -304,17 +293,18 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb) goto out; rc = -ENXIO; - page = follow_page(vma, uaddr, FOLL_WRITE); - if (IS_ERR_OR_NULL(page)) - goto out; - - lock_page(page); ptep = get_locked_pte(gmap->mm, uaddr, &ptelock); - if (should_export_before_import(uvcb, gmap->mm)) - uv_convert_from_secure(page_to_phys(page)); - rc = make_secure_pte(ptep, uaddr, page, uvcb); + if (pte_present(*ptep) && !(pte_val(*ptep) & _PAGE_INVALID) && pte_write(*ptep)) { + page = pte_page(*ptep); + rc = -EAGAIN; + if (trylock_page(page)) { + if (should_export_before_import(uvcb, gmap->mm)) + uv_convert_from_secure(page_to_phys(page)); + rc = make_page_secure(page, uvcb); + unlock_page(page); + } + } pte_unmap_unlock(ptep, ptelock); - unlock_page(page); out: mmap_read_unlock(gmap->mm);