From patchwork Mon Jan 30 22:27:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Acayan X-Patchwork-Id: 13121967 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F521C636D3 for ; Mon, 30 Jan 2023 22:27:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229880AbjA3W1f (ORCPT ); Mon, 30 Jan 2023 17:27:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229476AbjA3W1e (ORCPT ); Mon, 30 Jan 2023 17:27:34 -0500 Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C31961F4B4; Mon, 30 Jan 2023 14:27:33 -0800 (PST) Received: by mail-io1-xd2d.google.com with SMTP id b4so3400166ioj.0; Mon, 30 Jan 2023 14:27:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fBDPnxYSUfrgiKj2KMKkACqLWIz+4lF8zwzbQUTMXqY=; b=NviNuGt3il7xmstfEMv2zmdDte/GIs933d6FuffEQ6x+qBpvNU2cFze32sFvYnLVOy 128Z367Dg/BTLaPh3A7VAwQigFjoLmpkXtikBvcEokAiWnRKA8ZO3tte12VYEKOlGf3f ZiUOOhgerUEhOJ7RoGQzgRwuJZ0C0iqX7rzgelFODRoaVVvUwKMHl1RBHOw+RmJphBdL 08L2bhbe0fvXg4CIywRzn5BUPffR6mCysDv8/z4UQwgDmeYa4Hpn5f3Q0IQyLV141+yd m+mVZas2xy3mGZUXn9DIdH6Uaa/LEMsnuVyTbxqYsDHzJTuyN8DLORYGdNzKELNOZ5cT a9EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fBDPnxYSUfrgiKj2KMKkACqLWIz+4lF8zwzbQUTMXqY=; b=zhYp9ArZWsFrmXwmVTkY5ESaf3wB6rf5Adqlc/5BNqAlvTuTSF1SdIKnHUUt7tk8dG HRxDbdYDVzwK6vX2qgCYQeglKwaYUWkN75bo+EFwGedehYb39cLRaW3I9HTqqTbZq8V6 1EojslD0PJaQgFTQ4VfOa/SQ6Yc7iCt+FpIL8BLdN55kDSlNJS4LLx8DAgmkoWhqm+P+ iZQLuSlZFmjlfvs7pfShWZmHnb1Ey49GhvpYExAXj8A2E3tT8HR5nbxCGTxGCm8x4s79 pbAra4hI8NuM3oZic08hQzqugMLzbhlpTKRB0MfvGH8c+40sP1gSE6v3/CvMOgl3vXDo d0eg== X-Gm-Message-State: AO0yUKV9eXuUttvb7ww8sbJpij1nRcwdXQayHWaOmOUYOFhuVlxB6+iG 8VW9dX8EnuorVypotTpH9V8Ws0maNFCmvg== X-Google-Smtp-Source: AK7set+oCdscayHGv0+JxNg85OIUfKxsr+c1taK3ZxmdpYN8KHa+xfEyrGEK/5txXwX6X4LM+Wkugw== X-Received: by 2002:a6b:e402:0:b0:707:dcd8:2aea with SMTP id u2-20020a6be402000000b00707dcd82aeamr7151526iog.19.1675117653238; Mon, 30 Jan 2023 14:27:33 -0800 (PST) Received: from localhost ([2607:fea8:a2df:3d00::11ab]) by smtp.gmail.com with ESMTPSA id c62-20020a029644000000b003a96cc2bbdesm4535426jai.85.2023.01.30.14.27.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Jan 2023 14:27:32 -0800 (PST) From: Richard Acayan To: Srinivas Kandagatla , Amol Maheshwari , Arnd Bergmann , Greg Kroah-Hartman , Thierry Escande , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Richard Acayan Subject: [PATCH 1/2] misc: fastrpc: return -EPIPE to invocations on device removal Date: Mon, 30 Jan 2023 17:27:15 -0500 Message-Id: <20230130222716.7016-2-mailingradian@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230130222716.7016-1-mailingradian@gmail.com> References: <20230130222716.7016-1-mailingradian@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org The return value is initialized as -1, or -EPERM. The completion of an invocation implies that the return value is set appropriately, but "Permission denied" does not accurately describe the outcome of the invocation. Set the invocation's return value to a more appropriate "Broken pipe", as the cleanup breaks the driver's connection with rpmsg. Fixes: c68cfb718c8f ("misc: fastrpc: Add support for context Invoke method") Signed-off-by: Richard Acayan Reviewed-by: Srinivas Kandagatla --- drivers/misc/fastrpc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index 7ccaca1b7cb8..2334a4fd5869 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -2337,8 +2337,10 @@ static void fastrpc_notify_users(struct fastrpc_user *user) struct fastrpc_invoke_ctx *ctx; spin_lock(&user->lock); - list_for_each_entry(ctx, &user->pending, node) + list_for_each_entry(ctx, &user->pending, node) { + ctx->retval = -EPIPE; complete(&ctx->work); + } spin_unlock(&user->lock); } From patchwork Mon Jan 30 22:27:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Acayan X-Patchwork-Id: 13121968 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40F35C54EAA for ; Mon, 30 Jan 2023 22:27:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229476AbjA3W1i (ORCPT ); Mon, 30 Jan 2023 17:27:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231224AbjA3W1h (ORCPT ); Mon, 30 Jan 2023 17:27:37 -0500 Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A62AA2FCFD; Mon, 30 Jan 2023 14:27:35 -0800 (PST) Received: by mail-il1-x12d.google.com with SMTP id i6so1924519ilq.8; Mon, 30 Jan 2023 14:27:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=II+Jl8eZoU1E3b0fDfbf4ubv2PCTHVICkLREHENOVHM=; b=L8S9ZUkHW0AKjRS0uBpYEH1JIfpxI9WOsSlK6JW82xJ2PE7r/9A3gHfQrllYXfn/VJ v/dtyPsjpdBZ5LgiPBLOzE25DAPUua+eDirp2k4LOjg85YEn6J5grEOdbNh99+SpsttW k2UM4U/guCTR8Kyb+WWDmnwUF3lWe1TTblbTGylHgXtr+3M2eV44v68+aT5DHqaExC0/ vJicUQGRBZxpMlCuSmDrX4AIbzyRwsROnuX1LracKT1MvFMDf26dx+GoT8ojnwm51N0D HyP/H87pNsa8nWYKMjWLx9zF4vpy2uHdWYJbUH+fiztWtCrhbm8hQYYn2Eyr2qL8wacM 0cVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=II+Jl8eZoU1E3b0fDfbf4ubv2PCTHVICkLREHENOVHM=; b=r9qazdVA5J+pocLrI8rZIIzFH/YTcptM03E5giU2AM9DOjGd70v1s3IMMGUviGp5jS v6KyK2UvMYpbV4G0PgTADbokG1816cphpFMn8Ex7VFJsQOxQ9j2VNwXmVmS3xQO3F5sS kjXThqDXDhU+xmdxcAKA0xHU0/X9AM+RYhyKkULEhyil7prbQqRUVi2MjuRNKgXD0DPH TvI8uZOPEupBMGxKWFuvbV3wkSbSFyxXH13H3APjM37L1vjpx/vN23X0jghaUKLjlywL ZHxAIt9XrUKVshr9/7h+KZ7zQ361rr6qXNUamwVXGmoumAteEbF9srpGG8YJKIB9gkVI vSIQ== X-Gm-Message-State: AO0yUKXdyvE8Fmuo3w+t8KHVfypXIBBE4a0/NUdfeH6vwyHp4BDNGPIG bqNzYrjuh75YAZ4gL/gV3NsM/xF5FIeofw== X-Google-Smtp-Source: AK7set9tKAZvnRI98dKHGEtfJdxen3hCtr5hKEvUmSWCADlgdaO5PpW9+jOnbCupLocQCVh7jooajQ== X-Received: by 2002:a05:6e02:1d8d:b0:310:e4e7:a9ab with SMTP id h13-20020a056e021d8d00b00310e4e7a9abmr7051790ila.31.1675117655055; Mon, 30 Jan 2023 14:27:35 -0800 (PST) Received: from localhost ([2607:fea8:a2df:3d00::11ab]) by smtp.gmail.com with ESMTPSA id h4-20020a92c084000000b00302632f0d20sm4332075ile.67.2023.01.30.14.27.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Jan 2023 14:27:34 -0800 (PST) From: Richard Acayan To: Srinivas Kandagatla , Amol Maheshwari , Arnd Bergmann , Greg Kroah-Hartman , Thierry Escande , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Richard Acayan Subject: [PATCH 2/2] misc: fastrpc: reject new invocations during device removal Date: Mon, 30 Jan 2023 17:27:16 -0500 Message-Id: <20230130222716.7016-3-mailingradian@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230130222716.7016-1-mailingradian@gmail.com> References: <20230130222716.7016-1-mailingradian@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org The channel's rpmsg object allows new invocations to be made. After old invocations are already interrupted, the driver shouldn't try to invoke anymore. Invalidating the rpmsg at the end of the driver removal function makes it easy to cause a race condition in userspace. Even closing a file descriptor before the driver finishes its cleanup can cause an invocation via fastrpc_release_current_dsp_process() and subsequent timeout. Invalidate the channel before the invocations are interrupted to make sure that no invocations can be created to hang after the device closes. Demonstration of the bug as performed on a Google Pixel 3a with devicetree patches: #include #include #include #include #include #include #include static int remotectl_open(int fd, const char *name, uint32_t *handle) { struct fastrpc_invoke invoke; struct fastrpc_invoke_args args[4]; struct { uint32_t namelen; uint32_t errlen; } in; struct { uint32_t handle; uint32_t err; } out; char errstr[256]; int ret; // Remoteproc expects to receive a null terminator in.namelen = strlen(name) + 1; in.errlen = 256; args[0].ptr = (__u64) ∈ args[0].length = sizeof(in); args[0].fd = -1; args[1].ptr = (__u64) name; args[1].length = in.namelen; args[1].fd = -1; args[2].ptr = (__u64) &out; args[2].length = sizeof(out); args[2].fd = -1; args[3].ptr = (__u64) errstr; args[3].length = 256; args[3].fd = -1; invoke.handle = 0; invoke.sc = 0x00020200; invoke.args = (__u64) args; ret = ioctl(fd, FASTRPC_IOCTL_INVOKE, (__u64) &invoke); if (!ret) *handle = out.handle; return ret; } int main() { struct fastrpc_init_create_static create; uint32_t handle; int fd, ret; fd = open("/dev/fastrpc-adsp", O_RDWR); if (fd == -1) { perror("Could not open /dev/fastrpc-adsp"); return 1; } ret = ioctl(fd, FASTRPC_IOCTL_INIT_ATTACH_SNS, NULL); if (ret) { perror("Could not attach to sensorspd"); goto close_dev; } /* * Under normal circumstances, the remote processor * would request a file from a different client, and * quickly find out that there is no such file. When * this other client is not running, this procedure call * conveniently waits for the ADSP to crash. */ ret = remotectl_open(fd, "a", &handle); if (ret == -1) perror("Could not open CHRE interface"); close_dev: // This takes 10 seconds printf("Closing file descriptor\n"); close(fd); printf("Closed file descriptor\n"); return 0; } Fixes: c68cfb718c8f ("misc: fastrpc: Add support for context Invoke method") Signed-off-by: Richard Acayan Reviewed-by: Srinivas Kandagatla --- drivers/misc/fastrpc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index 2334a4fd5869..c8a36b9cf4fe 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -2351,7 +2351,9 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) struct fastrpc_user *user; unsigned long flags; + // No invocations past this point spin_lock_irqsave(&cctx->lock, flags); + cctx->rpdev = NULL; list_for_each_entry(user, &cctx->users, user) fastrpc_notify_users(user); spin_unlock_irqrestore(&cctx->lock, flags); @@ -2370,7 +2372,6 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) of_platform_depopulate(&rpdev->dev); - cctx->rpdev = NULL; fastrpc_channel_ctx_put(cctx); }