From patchwork Wed Jan 31 10:24:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Courbot X-Patchwork-Id: 10193859 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9524C603EE for ; Wed, 31 Jan 2018 10:29:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5949E23B32 for ; Wed, 31 Jan 2018 10:29:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4E11F27DCD; Wed, 31 Jan 2018 10:29:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E2C3C23B32 for ; Wed, 31 Jan 2018 10:29:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752391AbeAaK31 (ORCPT ); Wed, 31 Jan 2018 05:29:27 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:37392 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752998AbeAaKZN (ORCPT ); Wed, 31 Jan 2018 05:25:13 -0500 Received: by mail-pf0-f194.google.com with SMTP id p1so12147165pfh.4 for ; Wed, 31 Jan 2018 02:25:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=TJVSrIxe4g3JzsrXXC0V2rI1ns1DHsUTcPY4zfu5WsA=; b=lY1b4DMyXljEaeeNo7yiNRW3ptU0xEPv2Ut2funOAOQjA2YMNNYAGVpWPq7uk0ixly ZCV+ikJTBk1/luitHF5aVED7sFR+DjkH6FTBEJbxCT9gjOs3+Tn9EWPohAzWknDAMwOe Z/s3KtNZ08pdfBWG43AjeaZ6plpF4V7LNLf0A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=TJVSrIxe4g3JzsrXXC0V2rI1ns1DHsUTcPY4zfu5WsA=; b=R7nvYcp36RgQWF9Puc1sDtHG9+6YsfTdDx3u6Py50HOlqC8E7JhF9R3zSqe9Q12Ebo IxPhTHH3oYMle+lUP04wKuzryLuYPeR/4E81hnLQwV9/pXZHpKYKbtBEDOsDFbsS1zwN ZG65y2/JpigxyzxPEpzcylawqB/PSv85IzKlcCDfzOUwjWXOf1SWz3hVCfEkTQusZzXq LAbchQWesg9uvqAxWI3YNNcTbwlTv4bAtc5vCpJVvN2bbkUxzRwB23IxJw4MqIN11USg EKn7nKI2MBljJLQR4phNtffEpqm5KbGzWupjIR3TjAL9N4oUZv67MNKxvWvhZHHeGwlk xS1Q== X-Gm-Message-State: AKwxytdgkav8qoaNp/1zayg8zLIbm4Ta4iMSeOUVIXXiR5GFrEEf2s7I fwXX2G7WFSkGNXeuuDuRGS/KTQ== X-Google-Smtp-Source: AH8x226Us0Mitg3vn0l9zc6qbekJQsDic6rQJJi1WRAZIO5Z0SXxGn1/qL1jYwJM5U4J+VUMXrpLGQ== X-Received: by 10.98.204.144 with SMTP id j16mr33616190pfk.101.1517394312676; Wed, 31 Jan 2018 02:25:12 -0800 (PST) Received: from acourbot.tok.corp.google.com ([2401:fa00:4:1002:a6cd:a898:e07b:a331]) by smtp.gmail.com with ESMTPSA id 63sm45301239pft.11.2018.01.31.02.25.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jan 2018 02:25:12 -0800 (PST) From: Alexandre Courbot To: Mauro Carvalho Chehab , Hans Verkuil , Laurent Pinchart , Pawel Osciak , Marek Szyprowski , Tomasz Figa , Sakari Ailus , Gustavo Padovan Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot Subject: [RFCv2 04/17] media: vb2: add support for requests in QBUF ioctl Date: Wed, 31 Jan 2018 19:24:22 +0900 Message-Id: <20180131102427.207721-5-acourbot@chromium.org> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog In-Reply-To: <20180131102427.207721-1-acourbot@chromium.org> References: <20180131102427.207721-1-acourbot@chromium.org> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Support the request argument of the QBUF ioctl. Signed-off-by: Alexandre Courbot --- drivers/media/v4l2-core/v4l2-ioctl.c | 83 +++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c index fdd2f784c264..3b44f1fe4f23 100644 --- a/drivers/media/v4l2-core/v4l2-ioctl.c +++ b/drivers/media/v4l2-core/v4l2-ioctl.c @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -965,6 +966,71 @@ static int check_fmt(struct file *file, enum v4l2_buf_type type) return -EINVAL; } +/* + * Validate that a given request can be used during an ioctl. + * + * When using the request API, request file descriptors must be matched against + * the actual request object. User-space can pass any file descriptor, so we + * need to make sure the call is valid before going further. + * + * This function looks up the request and associated data and performs the + * following sanity checks: + * + * * Make sure that the entity belongs to the media_device managing the passed + * request, + * * Make sure that the entity data (if any) is associated to the current file + * handler. + * + * This function returns a pointer to the valid request, or and error code in + * case of failure. When successful, a reference to the request is acquired and + * must be properly released by the caller. If _data is not NULL, then it is + * filled with a pointer to the corresponding entity data. + */ +#ifdef CONFIG_MEDIA_CONTROLLER +static struct media_request * +check_request(int request, struct file *file, void *fh, struct media_request_entity_data **_data) +{ + struct media_request *req = NULL; + struct video_device *vfd = video_devdata(file); + struct v4l2_fh *vfh = + test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL; + struct media_entity *entity = &vfd->entity; + struct media_request_entity_data *data; + + if (!entity) + return ERR_PTR(-EINVAL); + + req = media_request_get_from_fd(request); + if (!req) + return ERR_PTR(-EINVAL); + + /* Validate that the entity belongs to the correct media_device */ + if (vfd->v4l2_dev->mdev->req_mgr != req->mgr) { + media_request_put(req); + return ERR_PTR(-EINVAL); + } + + /* Validate that the entity's data belongs to the correct fh */ + data = media_request_get_entity_data(req, entity, vfh); + if (IS_ERR(data)) { + media_request_put(req); + return ERR_PTR(PTR_ERR(data)); + } + + if (_data) + *_data = data; + + return req; +} +#else /* CONFIG_MEDIA_CONTROLLER */ +static struct media_request * +check_request(int request, struct file *file, void *fh) +{ + return ERR_PTR(-ENOSYS); +} + +#endif /* CONFIG_MEDIA_CONTROLLER */ + static void v4l_sanitize_format(struct v4l2_format *fmt) { unsigned int offset; @@ -1902,10 +1968,25 @@ static int v4l_querybuf(const struct v4l2_ioctl_ops *ops, static int v4l_qbuf(const struct v4l2_ioctl_ops *ops, struct file *file, void *fh, void *arg) { + struct media_request *req = NULL; struct v4l2_buffer *p = arg; int ret = check_fmt(file, p->type); - return ret ? ret : ops->vidioc_qbuf(file, fh, p); + if (ret) + return ret; + + if (p->request_fd > 0) { + req = check_request(p->request_fd, file, fh, NULL); + if (IS_ERR(req)) + return PTR_ERR(req); + } + + ret = ops->vidioc_qbuf(file, fh, p); + + if (req) + media_request_put(req); + + return ret; } static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,