Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2069641rdf; Mon, 6 Nov 2023 04:05:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IFr6dqXIFTkrHTS6ICvlFQ0kJPTZHfO4J5UiGXeoGyESd0aj8xpA5shJAy3Iln7LhWX+Tot X-Received: by 2002:aa7:800f:0:b0:6c2:cb9a:885a with SMTP id j15-20020aa7800f000000b006c2cb9a885amr13060294pfi.27.1699272310810; Mon, 06 Nov 2023 04:05:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699272310; cv=none; d=google.com; s=arc-20160816; b=XjEDRRuUC3VSiIVOzVLNKtSzJWSo1dgujXdHzO+c00RPIviTFOZcF2g2iv43+qteJb SJZVnlDFmqEvvBRxwRoqGJ1pLtYVsrYjZzuEXGTwUf9uijWW86FW4LSGubd6ZGvVHkko sI40PSqyztJLaL8EiqTt+VifKxKHkBUnoLYJOAfYTzkeKHnnOakBQr7d/DbDTECPsD3A RbIAiMvzKmIK0zBlvTOozn06RbYaRgjA+82xhV8b8+BmoAP1V8jtPY9sFWKCGWkaqn/I wl7EJzfDQDHnEgf6LpiL8csjsz7SPCUgesCAAqCqhVZjYCqCWoHp14CeWX1FtDncl03/ xorQ== 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=H1hzDIVX/uYFlZAzRgT3yZuD5V71PO6DoLhIHHVMUSc=; fh=8U7QuxU7cADb+Zu8vDAVVKQqqLyxXSbl7CN71zWgSeE=; b=VuOO/JfFnJzpm5W/omEO7KnL5JIEQGPGOZvd5m9oblLGngoVTFttHCsH9cyiusgXYL FyscisXW0IAEIxVei9atp4aeu3grN/a+49gtjyi8cdv230Y4Ax02SAmvwGbQMds4JJ2e 4F7MaZMbWoL/6DTI+pMxhZurq1ps6/Q0U2Vzj0uUYfDDEd1XvU+0yXiK8LkhWccYpXGL 6xpTWA8Kg3v7iM3ZImKqRLjMaacl6Q5fcPyNZLFUf4AIykOK0AgriEOW7c9UnWUXQ/3/ fLpMBMcueisM+xJEIZr+GaVbQYqh42KiOfCsALaQLEGnIS5sbsATH42RZeGS34o2rm1k 5t9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=ijohy3ni; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id cl15-20020a056a0032cf00b0068a557febfcsi8266484pfb.111.2023.11.06.04.05.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 04:05:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=ijohy3ni; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 8C400802AD25; Mon, 6 Nov 2023 04:05:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231638AbjKFME4 (ORCPT + 99 others); Mon, 6 Nov 2023 07:04:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231552AbjKFMEi (ORCPT ); Mon, 6 Nov 2023 07:04:38 -0500 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 038F5FA; Mon, 6 Nov 2023 04:04:34 -0800 (PST) X-UUID: a32277287c9c11eea33bb35ae8d461a2-20231106 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=H1hzDIVX/uYFlZAzRgT3yZuD5V71PO6DoLhIHHVMUSc=; b=ijohy3niSJqp0SCjVCC2M92MXFrSltXjf/HllXvAxHRT849cCnMT59bcE4+U6Kn2jqXZLwPXUfq4zDB7rahxmGjRGqqqbC9tnpQZqTnzyrk3trbSS+HDt0Vnhq07zaFAaEkGzK0BP5oC+v5gOMwtJC0OxRF9FpdfsbuMzFXJvOg=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.33,REQID:3b9cefca-cf2f-4757-aae4-b6d1afb0e7c8,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:364b77b,CLOUDID:87b7bc5f-c89d-4129-91cb-8ebfae4653fc,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO, DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: a32277287c9c11eea33bb35ae8d461a2-20231106 Received: from mtkmbs13n1.mediatek.inc [(172.21.101.193)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 313314417; Mon, 06 Nov 2023 20:04:28 +0800 Received: from mtkmbs11n1.mediatek.inc (172.21.101.185) by mtkmbs11n1.mediatek.inc (172.21.101.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 6 Nov 2023 20:04:27 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs11n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 6 Nov 2023 20:04:26 +0800 From: Yunfei Dong To: Jeffrey Kardatzke , "T . J . Mercier" , John Stultz , Yong Wu , =?UTF-8?q?N=C3=ADcolas=20F=20=2E=20R=20=2E=20A=20=2E=20Prado?= , Nicolas Dufresne , Hans Verkuil , AngeloGioacchino Del Regno , Benjamin Gaignard , Nathan Hebert CC: Chen-Yu Tsai , Hsin-Yi Wang , Fritz Koenig , Daniel Vetter , Steve Cho , Yunfei Dong , , , , , , Subject: [PATCH v2,02/21] v4l2: handle secure memory flags in queue setup Date: Mon, 6 Nov 2023 20:04:04 +0800 Message-ID: <20231106120423.23364-3-yunfei.dong@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231106120423.23364-1-yunfei.dong@mediatek.com> References: <20231106120423.23364-1-yunfei.dong@mediatek.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-MTK: N X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 04:05:06 -0800 (PST) From: Jeffrey Kardatzke Validates the secure memory flags when setting up a queue and ensures the queue has the proper capability. Signed-off-by: Jeffrey Kardatzke Signed-off-by: Yunfei Dong --- .../media/common/videobuf2/videobuf2-core.c | 25 ++++++++++++++ .../media/common/videobuf2/videobuf2-v4l2.c | 33 ++++++++++++------- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 27aee92f3eea..7ee68f5533ba 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -744,12 +744,22 @@ static bool verify_coherency_flags(struct vb2_queue *q, bool non_coherent_mem) return true; } +static bool verify_secure_mem_flags(struct vb2_queue *q, bool secure_mem) +{ + if (secure_mem != q->secure_mem) { + dprintk(q, 1, "secure memory model mismatch\n"); + return false; + } + return true; +} + int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, unsigned int flags, unsigned int *count) { unsigned int num_buffers, allocated_buffers, num_planes = 0; unsigned plane_sizes[VB2_MAX_PLANES] = { }; bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; + bool secure_mem = flags & V4L2_MEMORY_FLAG_SECURE; unsigned int i; int ret; @@ -766,6 +776,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, if (*count == 0 || q->num_buffers != 0 || (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory) || !verify_coherency_flags(q, non_coherent_mem)) { + bool no_previous_buffers = !q->num_buffers; + /* * We already have buffers allocated, so first check if they * are not in use and can be freed. @@ -784,6 +796,14 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, __vb2_queue_free(q, q->num_buffers); mutex_unlock(&q->mmap_lock); + /* + * Do not allow switching secure buffer mode. + */ + if (!no_previous_buffers && + !verify_secure_mem_flags(q, secure_mem)) { + return -EINVAL; + } + /* * In case of REQBUFS(0) return immediately without calling * driver's queue_setup() callback and allocating resources. @@ -807,6 +827,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, q->memory = memory; mutex_unlock(&q->mmap_lock); set_queue_coherency(q, non_coherent_mem); + q->secure_mem = secure_mem; /* * Ask the driver how many buffers and planes per buffer it requires. @@ -910,6 +931,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, unsigned int num_planes = 0, num_buffers, allocated_buffers; unsigned plane_sizes[VB2_MAX_PLANES] = { }; bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; + bool secure_mem = flags & V4L2_MEMORY_FLAG_SECURE; bool no_previous_buffers = !q->num_buffers; int ret; @@ -933,6 +955,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, mutex_unlock(&q->mmap_lock); q->waiting_for_buffers = !q->is_output; set_queue_coherency(q, non_coherent_mem); + q->secure_mem = secure_mem; } else { if (q->memory != memory) { dprintk(q, 1, "memory model mismatch\n"); @@ -940,6 +963,8 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, } if (!verify_coherency_flags(q, non_coherent_mem)) return -EINVAL; + if (!verify_secure_mem_flags(q, secure_mem)) + return -EINVAL; } num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers); diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/media/common/videobuf2/videobuf2-v4l2.c index c7a54d82a55e..966ce74e26b3 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -685,26 +685,33 @@ static void fill_buf_caps(struct vb2_queue *q, u32 *caps) *caps |= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF; if (q->allow_cache_hints && q->io_modes & VB2_MMAP) *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS; + if (q->allow_secure_mem && q->io_modes & VB2_DMABUF) + *caps |= V4L2_BUF_CAP_SUPPORTS_SECURE_MEM; #ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API if (q->supports_requests) *caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS; #endif } -static void validate_memory_flags(struct vb2_queue *q, +static bool validate_memory_flags(struct vb2_queue *q, int memory, u32 *flags) { + if (*flags & V4L2_MEMORY_FLAG_SECURE && + (!q->allow_secure_mem || memory != V4L2_MEMORY_DMABUF)) { + return false; + } if (!q->allow_cache_hints || memory != V4L2_MEMORY_MMAP) { /* - * This needs to clear V4L2_MEMORY_FLAG_NON_COHERENT only, - * but in order to avoid bugs we zero out all bits. + * This needs to clear V4L2_MEMORY_FLAG_NON_COHERENT only. */ - *flags = 0; - } else { - /* Clear all unknown flags. */ - *flags &= V4L2_MEMORY_FLAG_NON_COHERENT; + *flags &= ~V4L2_MEMORY_FLAG_NON_COHERENT; } + + /* Clear all unknown flags. */ + *flags &= V4L2_MEMORY_FLAG_NON_COHERENT | V4L2_MEMORY_FLAG_SECURE; + + return true; } int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) @@ -713,7 +720,8 @@ int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) u32 flags = req->flags; fill_buf_caps(q, &req->capabilities); - validate_memory_flags(q, req->memory, &flags); + if (!validate_memory_flags(q, req->memory, &flags)) + return -EINVAL; req->flags = flags; return ret ? ret : vb2_core_reqbufs(q, req->memory, req->flags, &req->count); @@ -748,7 +756,8 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) unsigned i; fill_buf_caps(q, &create->capabilities); - validate_memory_flags(q, create->memory, &create->flags); + if (!validate_memory_flags(q, create->memory, &create->flags)) + return -EINVAL; create->index = q->num_buffers; if (create->count == 0) return ret != -EBUSY ? ret : 0; @@ -986,7 +995,8 @@ int vb2_ioctl_reqbufs(struct file *file, void *priv, u32 flags = p->flags; fill_buf_caps(vdev->queue, &p->capabilities); - validate_memory_flags(vdev->queue, p->memory, &flags); + if (!validate_memory_flags(vdev->queue, p->memory, &flags)) + return -EINVAL; p->flags = flags; if (res) return res; @@ -1010,7 +1020,8 @@ int vb2_ioctl_create_bufs(struct file *file, void *priv, p->index = vdev->queue->num_buffers; fill_buf_caps(vdev->queue, &p->capabilities); - validate_memory_flags(vdev->queue, p->memory, &p->flags); + if (!validate_memory_flags(vdev->queue, p->memory, &p->flags)) + return -EINVAL; /* * If count == 0, then just check if memory and type are valid. * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0. -- 2.18.0