From patchwork Fri Nov 24 17:34:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud POULIQUEN X-Patchwork-Id: 169533 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp1399151vqx; Fri, 24 Nov 2023 09:35:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IEvTx6U19Lc7sWLAos4snE6bD7FWx7bZpik1MTbaYlHIitFDZ8BoSuYuE6rjIhy2jVFwUAm X-Received: by 2002:a05:6808:1311:b0:3b6:d9fc:f2fb with SMTP id y17-20020a056808131100b003b6d9fcf2fbmr4801085oiv.8.1700847305660; Fri, 24 Nov 2023 09:35:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700847305; cv=none; d=google.com; s=arc-20160816; b=sJY7dANmv877v+Io8nKJXD0wQ2jrjnGtAkZJoXC3Cb8ooWm9UZdMY/4YVH3ZhVNIb/ oWvDI66FDCNpdwGZYJsSu7CrOVnBNgqBj27c39Ft9qX9rjNY5LqE2Xe+QffGgFtM5pM0 Q9ewVvcUWL5Cm4txQ/Vz9mUd/6VjyN2QHg6xaLojgVaf+ONFvk30SX++xydLhAFaDWle AjPx+mqgcmJuCDc2V+ifIVlX0A6DW8QpapQ4WS16sLzX/eFKXBmJrclBMTUnAvcuUiFo VI06M0N3wE38WMec8Epi8R6CUgYhFFcleZtaUVhHos/7NUgxaUIxocT3MKGB4Lmmb1Dr yiUw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=UuRvH6ncOo3hoOBvG71T9rPB4RLcYKiAFEshLkh06H8=; fh=8HpnQdZRE65gzCgIYozqm8Z1lHlHmJJzPtdufnlfrAY=; b=NqWzeJC8VXQpo7Sycp7ALhDVlx4Y8spZ3PRAL1u3uEZnirz3raDcEOHM4WAHy65tT9 UqAgLdxcJw5jY+cthRd7tkqwR+9binszsh+/J0w1gHsOVJXWG1Hiyi9HQZ6eZvEt+txz thh7hvI27DUSGo2V37MElgJnSGRK8fg7glyazkg+waOGy1FEzsJoIaRLdgLWmM0NrtKH jieyCNphJvqjUu4Q775UEWComClWsqZiBe34fqcO1m6wZ/oBtD5RW/pP2V2/jzouJtj5 I6DjrrNP9QJSje2aN+VXshT8p0rV0rBy5IPw/zWw+/gtXHHL+dPzQnNrd2OCuqXEpuoL FFZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b=IWxVAl0m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id i30-20020a63585e000000b005bdfd3a0d3esi3983586pgm.185.2023.11.24.09.35.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 09:35:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b=IWxVAl0m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 485A88057E76; Fri, 24 Nov 2023 09:35:02 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231276AbjKXRel (ORCPT + 99 others); Fri, 24 Nov 2023 12:34:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231397AbjKXReg (ORCPT ); Fri, 24 Nov 2023 12:34:36 -0500 Received: from mx07-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F36BA1BD1 for ; Fri, 24 Nov 2023 09:34:39 -0800 (PST) Received: from pps.filterd (m0046660.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3AOF3wlT003878; Fri, 24 Nov 2023 18:34:24 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:content-type; s=selector1; bh=UuRvH6n cOo3hoOBvG71T9rPB4RLcYKiAFEshLkh06H8=; b=IWxVAl0mPvInArFPrMrFfbB 2aoHZWhBkWiemkrDkQO4/VmN2lSvfVgoBNdQe/hm6cpaET/VtG6wVasjxW9zCJlC gpgQwIlAlkQUnUVvWV6XIrPxKiKNvcALpvQl/gQAuRUSXoZDN4tqhbmi7MM+ha6S CFZ3EsYgrYy6tzZyGT9P3onHzTHTebRnv2D/erRd8L6Iz8PRqaHHj3Trq+XvHOgL rrlcqH8iE/0ozxgNJzliwv9L3HKMa5b3BkF8tK6QxxQV8LxD9NY5NUWJq1fFTGjB 9Xgbsjt49YILVuNwXax8KvgCYkSh2BFMCGLzY5p52u/kC8F7gWHNwleTyIEzfyA= = Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3uj46nu3ec-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 24 Nov 2023 18:34:24 +0100 (CET) Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id D609910002A; Fri, 24 Nov 2023 18:34:22 +0100 (CET) Received: from Webmail-eu.st.com (shfdag1node2.st.com [10.75.129.70]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 82FA9273DA7; Fri, 24 Nov 2023 18:34:22 +0100 (CET) Received: from localhost (10.252.0.204) by SHFDAG1NODE2.st.com (10.75.129.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Fri, 24 Nov 2023 18:34:21 +0100 From: Arnaud Pouliquen To: Jens Wiklander , Sumit Garg , Christoph Hellwig CC: Arnaud Pouliquen , , Subject: [PATCH] tee: Use iov_iter to better support shared buffer registration Date: Fri, 24 Nov 2023 18:34:11 +0100 Message-ID: <20231124173411.111713-1-arnaud.pouliquen@foss.st.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.252.0.204] X-ClientProxiedBy: EQNCAS1NODE4.st.com (10.75.129.82) To SHFDAG1NODE2.st.com (10.75.129.70) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-24_04,2023-11-22_01,2023-05-22_02 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 24 Nov 2023 09:35:02 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783467663914478199 X-GMAIL-MSGID: 1783467663914478199 It is not possible to register buffers allocated with vmalloc. Use iov_iter and associated helper functions to manage the page registration for all type of memories. Suggested-by: Christoph Hellwig Signed-off-by: Arnaud Pouliquen --- The support of buffer registration allocated with vmalloc is no more available since c83900393aa1 ("tee: Remove vmalloc page support"). This patch is an alternative to a revert and resulted from a discussion with Christopher Hellwig [1]. This patch has been tested using xtest tool in optee qemu environment [2] and using the series related to the remoteproc tee that should be proposed soon [3]. References: [1] https://lore.kernel.org/linux-arm-kernel/18a8528d-7d9d-6ed0-0045-5ee47dd39fb2@foss.st.com/T/#m8ec683c44fcd9b69c2aee42eaed0793afac9dd18in [2] https://optee.readthedocs.io/en/latest/building/devices/qemu.html#build-instructions [3] https://lore.kernel.org/linux-arm-kernel/18a8528d-7d9d-6ed0-0045-5ee47dd39fb2@foss.st.com/T/#maca0a1fc897aadd54c7deac432e11473fe970d1d --- drivers/tee/tee_shm.c | 72 +++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 673cf0359494..0bb47ffd049a 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -22,23 +22,13 @@ static void shm_put_kernel_pages(struct page **pages, size_t page_count) put_page(pages[n]); } -static int shm_get_kernel_pages(unsigned long start, size_t page_count, - struct page **pages) +static void shm_get_kernel_pages(struct page **pages, size_t page_count) { - struct page *page; size_t n; - if (WARN_ON_ONCE(is_vmalloc_addr((void *)start) || - is_kmap_addr((void *)start))) - return -EINVAL; - - page = virt_to_page((void *)start); - for (n = 0; n < page_count; n++) { - pages[n] = page + n; + /* iov_iter_extract_kvec_pages does not get reference on the pages, get a pin on them. */ + for (n = 0; n < page_count; n++) get_page(pages[n]); - } - - return page_count; } static void release_registered_pages(struct tee_shm *shm) @@ -214,13 +204,12 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size) EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf); static struct tee_shm * -register_shm_helper(struct tee_context *ctx, unsigned long addr, - size_t length, u32 flags, int id) +register_shm_helper(struct tee_context *ctx, struct iov_iter *iter, u32 flags, int id) { struct tee_device *teedev = ctx->teedev; struct tee_shm *shm; - unsigned long start; - size_t num_pages; + unsigned long start, addr; + size_t num_pages, length, len, off; void *ret; int rc; @@ -245,31 +234,31 @@ register_shm_helper(struct tee_context *ctx, unsigned long addr, shm->flags = flags; shm->ctx = ctx; shm->id = id; - addr = untagged_addr(addr); + addr = (unsigned long)iter_iov_addr(iter); + length = iter_iov_len(iter); start = rounddown(addr, PAGE_SIZE); - shm->offset = addr - start; - shm->size = length; - num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE; + num_pages = iov_iter_npages(iter, INT_MAX); + if (!num_pages) { + ret = ERR_PTR(-ENOMEM); + goto err_ctx_put; + } + shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL); if (!shm->pages) { ret = ERR_PTR(-ENOMEM); goto err_free_shm; } - if (flags & TEE_SHM_USER_MAPPED) - rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE, - shm->pages); - else - rc = shm_get_kernel_pages(start, num_pages, shm->pages); - if (rc > 0) - shm->num_pages = rc; - if (rc != num_pages) { - if (rc >= 0) - rc = -ENOMEM; - ret = ERR_PTR(rc); + len = iov_iter_extract_pages(iter, &shm->pages, LONG_MAX, num_pages, 0, &off); + if (len != length) { + ret = len ? ERR_PTR(len) : ERR_PTR(-ENOMEM); goto err_put_shm_pages; } + shm->offset = off; + shm->size = len; + shm->num_pages = num_pages; + rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages, shm->num_pages, start); if (rc) { @@ -307,6 +296,8 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, u32 flags = TEE_SHM_USER_MAPPED | TEE_SHM_DYNAMIC; struct tee_device *teedev = ctx->teedev; struct tee_shm *shm; + struct iovec iov; + struct iov_iter iter; void *ret; int id; @@ -319,7 +310,10 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, if (id < 0) return ERR_PTR(id); - shm = register_shm_helper(ctx, addr, length, flags, id); + iov.iov_base = (void __user *)addr; + iov.iov_len = length; + iov_iter_init(&iter, ITER_DEST, &iov, 1, length); + shm = register_shm_helper(ctx, &iter, flags, id); if (IS_ERR(shm)) { mutex_lock(&teedev->mutex); idr_remove(&teedev->idr, id); @@ -351,9 +345,19 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, void *addr, size_t length) { + struct tee_shm *shm; u32 flags = TEE_SHM_DYNAMIC; + struct kvec kvec; + struct iov_iter iter; - return register_shm_helper(ctx, (unsigned long)addr, length, flags, -1); + kvec.iov_base = addr; + kvec.iov_len = length; + iov_iter_kvec(&iter, ITER_SOURCE, &kvec, 1, length); + shm = register_shm_helper(ctx, &iter, flags, -1); + if (!IS_ERR_OR_NULL(shm)) + shm_get_kernel_pages(shm->pages, shm->num_pages); + + return shm; } EXPORT_SYMBOL_GPL(tee_shm_register_kernel_buf);