Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2435489pxa; Mon, 24 Aug 2020 14:15:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzm5MzGGDQQ+VvFVveTttn4DVqkRMNQylZh6sdY4Dim9AwCNXZ26673De0EfIbH1Q3y9Xy+ X-Received: by 2002:a50:af83:: with SMTP id h3mr7308461edd.139.1598303733180; Mon, 24 Aug 2020 14:15:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598303733; cv=none; d=google.com; s=arc-20160816; b=VipbtCJ0zXAcDHzt4WB+hp4EDJER+2rVL6Q6tIUZ4uw/PT5JjzDcqS4/dRZB1zYjV9 ruEbO1JPHSDxgbEjD9Q1uc9ODo2HRXMFJpoDREWDgfwTzSnPAikYFyReVDMvhk892h1Q Fz0IfM2+bsh53m3MgxRrvTWxElEfH6Jxsmi73sX/R159q19Ai75pTGuznJ6Gi+ca9XBj xZY4DBo0bMCZXIMxRPYZ0ctgVowk2Cgx0cu7JpG/GzzEsa6GwMKHmaJFOEzGlZlwxfW5 W4++a9k1O3HhfAWo5JyfSMLjgB4BV2Ny7RqaD69e+TC9mhKXpVUNqaxyA2wdl9aPBDlG VkSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:dkim-signature:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=sPKT2vVWDEL5uCOVvAzzh/qnGwDPnupB1+gNHfgo2wA=; b=WbaHB+UJ7tnb0wZ91VaZwffT+Us7I3hndtS0u6Rb3+DkkX4j2UjOEPSdcNvMwl9QHS P2oHPEt8oI219jYC7QQSFX6GC0WTjJNBqln4I9kxMpPvMhQ22fWqekYnb1oRV9615mtY FWJV8XeupTK/tVswq/nMfIIDr3+cvI1oWZ3cOlnED6le3X8Tmwh6gzozC776SvrW8j4o DVgqCPMZJHKML13HvqaucPpShOLiQlkv/HLRx324nei0ET8EftSpFpnkTxvc621TlbwE idqbXcn1TUb8Ixye+i/cehUblhPAJ+GCU0gGip6j6/9L3AmqIEEx9tPbE9SnSWTzbN9o E5Ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=DMCiLDcu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v17si7237956ejd.324.2020.08.24.14.15.09; Mon, 24 Aug 2020 14:15:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=DMCiLDcu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727964AbgHXVL3 (ORCPT + 99 others); Mon, 24 Aug 2020 17:11:29 -0400 Received: from hqnvemgate25.nvidia.com ([216.228.121.64]:12810 "EHLO hqnvemgate25.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726138AbgHXVL2 (ORCPT ); Mon, 24 Aug 2020 17:11:28 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate25.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Mon, 24 Aug 2020 14:10:26 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Mon, 24 Aug 2020 14:11:27 -0700 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Mon, 24 Aug 2020 14:11:27 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Mon, 24 Aug 2020 21:11:27 +0000 Received: from hqnvemgw03.nvidia.com (10.124.88.68) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Mon, 24 Aug 2020 21:11:27 +0000 Received: from sandstorm.nvidia.com (Not Verified[10.2.58.8]) by hqnvemgw03.nvidia.com with Trustwave SEG (v7,5,8,10121) id ; Mon, 24 Aug 2020 14:11:27 -0700 From: John Hubbard To: CC: , , , , , , , Sumit Semwal , , , Subject: [PATCH v3] tee: convert convert get_user_pages() --> pin_user_pages() Date: Mon, 24 Aug 2020 14:11:25 -0700 Message-ID: <20200824211125.1867329-1-jhubbard@nvidia.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: References: MIME-Version: 1.0 X-NVConfidentiality: public Content-Transfer-Encoding: quoted-printable Content-Type: text/plain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1598303426; bh=sPKT2vVWDEL5uCOVvAzzh/qnGwDPnupB1+gNHfgo2wA=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:X-NVConfidentiality: Content-Transfer-Encoding:Content-Type; b=DMCiLDcu/bbZ0HfTovzGNWhZ32KGT2RI2dt39vFl1huAYVropJDqFzm95VE0bDl17 2iKl7y4XYSQN7sP9FAZMlRVcoc/USP4iaa04eQ7aXDmCVYNO767uZnlAmtC6qoBwSL fPhaLcdA2bU0nPQSEaYElAz9fEBZOrQj2TOCj8kKL5eZ7LC2xqisYmnZ6lgpBZP2GL sc1CODEDr1LwWbDI2cyEsedGFLX8WmgiHSiUuLZ80FM/PcQqRoj1db3hi3k8oT0RVD QHtHVMRDNGMP/tpWp7jgNH8DS27MP6YoKI7d8OAKpTnFrTMezxD/WDZj4q1zJ0GVZZ 8N4VPH4CgRURg== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This code was using get_user_pages*(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages*() + put_page() calls to pin_user_pages*() + unpin_user_pages() calls. Factor out a new, small release_registered_pages() function, in order to consolidate the logic for discerning between TEE_SHM_USER_MAPPED and TEE_SHM_KERNEL_MAPPED pages. This also absorbs the kfree() call that is also required there. There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages. [1] Documentation/core-api/pin_user_pages.rst [2] "Explicit pinning of user-space pages": https://lwn.net/Articles/807108/ Cc: Jens Wiklander Cc: Sumit Semwal Cc: tee-dev@lists.linaro.org Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Cc: linaro-mm-sig@lists.linaro.org Signed-off-by: John Hubbard --- OK, one more try, this time actually handling the _USER_MAPPED vs. _KERNEL_MAPPED pages! thanks, John Hubbard NVIDIA drivers/tee/tee_shm.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 827ac3d0fea9..00472f5ce22e 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -12,6 +12,22 @@ #include #include "tee_private.h" =20 +static void release_registered_pages(struct tee_shm *shm) +{ + if (shm->pages) { + if (shm->flags & TEE_SHM_USER_MAPPED) { + unpin_user_pages(shm->pages, shm->num_pages); + } else { + size_t n; + + for (n =3D 0; n < shm->num_pages; n++) + put_page(shm->pages[n]); + } + + kfree(shm->pages); + } +} + static void tee_shm_release(struct tee_shm *shm) { struct tee_device *teedev =3D shm->ctx->teedev; @@ -32,17 +48,13 @@ static void tee_shm_release(struct tee_shm *shm) =20 poolm->ops->free(poolm, shm); } else if (shm->flags & TEE_SHM_REGISTER) { - size_t n; int rc =3D teedev->desc->ops->shm_unregister(shm->ctx, shm); =20 if (rc) dev_err(teedev->dev.parent, "unregister shm %p failed: %d", shm, rc); =20 - for (n =3D 0; n < shm->num_pages; n++) - put_page(shm->pages[n]); - - kfree(shm->pages); + release_registered_pages(shm); } =20 teedev_ctx_put(shm->ctx); @@ -228,7 +240,7 @@ struct tee_shm *tee_shm_register(struct tee_context *ct= x, unsigned long addr, } =20 if (flags & TEE_SHM_USER_MAPPED) { - rc =3D get_user_pages_fast(start, num_pages, FOLL_WRITE, + rc =3D pin_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages); } else { struct kvec *kiov; @@ -292,18 +304,12 @@ struct tee_shm *tee_shm_register(struct tee_context *= ctx, unsigned long addr, return shm; err: if (shm) { - size_t n; - if (shm->id >=3D 0) { mutex_lock(&teedev->mutex); idr_remove(&teedev->idr, shm->id); mutex_unlock(&teedev->mutex); } - if (shm->pages) { - for (n =3D 0; n < shm->num_pages; n++) - put_page(shm->pages[n]); - kfree(shm->pages); - } + release_registered_pages(shm); } kfree(shm); teedev_ctx_put(ctx); --=20 2.28.0