Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp945385rdg; Fri, 11 Aug 2023 05:16:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHSMWKPRa1VagH+qNNaVcFVZA8Abj+imSlRIVS1x+7Iaub5Tr7j5Hbo6h4wZ1atvM5+hsFr X-Received: by 2002:a05:6a20:244a:b0:131:c760:2a0b with SMTP id t10-20020a056a20244a00b00131c7602a0bmr2423524pzc.52.1691756162461; Fri, 11 Aug 2023 05:16:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691756162; cv=none; d=google.com; s=arc-20160816; b=zAmLgHfvAglbGgZhSv/uSMmAvHjrWR/uAirTXvgdPe0ZipePdYbuFqpX0QeaxyhXwq r43ytpGKaOUOclmkfKb16HBxehcYZFjU02mFlwvhRcFjBr+x8OB4piK0n/Ffaof8Qu78 9ZpB/n8TtFcKzFXFYTHNb9EZFLU4k34AHogB+voqPLP9NztGPsLE5oqgEZ9JUBSpCMFu 017iHBaR62y2VVAsitJ0AKtLlvcXv73H6uD/eip7sIyCyqc2BnwTRsw8PIaMOwhb/gFv O1CboCcIAKitS7QFo5FR9zX9iAtNKYmcytmuz3QvyEsI2go9pPUrDgnftktvIqnF3R/H WwkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=8xar4nAlCgRmGFupnHDxTUCz35dFKq/ecird6X4Qb3o=; fh=thXO/zs9R4fXVl9m6IfRyVeSYDgYRCGfz/oMQk9f94w=; b=G497LswgKvwmvBlPBHAFYEQAuE+CSdQOsk+6JZv9IZ+jTBocUXzmvzq6HP3VATn9kL OoAsu4vOUVXw9GbX0Kp64Fb/ZK2uydwmo0mhVZbGInsmQHnQvvKP3JhBDC4VFW7rOu6Z 489d/uLEwZM9ogjtECrp1LivbzsFkmQUl+J3ZJTH4J7X3xIFJFY8mYPsetI9R46y9bQ4 7/zRigMN9MKDVbupt2I3af6A7JuzZsZ0MYpp5PNcy95q6CJYjgI9x7gMqqrinNKng3J0 5LVEAYjs6xO/A0CVUtNL81zXCS9sn2uVc/6mB9CIt8LPhCz7Eqwsftvpnk3j9itmHkYp JS7w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e9-20020a656889000000b00564514df654si3413437pgt.895.2023.08.11.05.15.50; Fri, 11 Aug 2023 05:16:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235000AbjHKLjY (ORCPT + 99 others); Fri, 11 Aug 2023 07:39:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229543AbjHKLjX (ORCPT ); Fri, 11 Aug 2023 07:39:23 -0400 Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 372FE26A6 for ; Fri, 11 Aug 2023 04:39:21 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 403C160002; Fri, 11 Aug 2023 11:39:16 +0000 (UTC) Message-ID: Date: Fri, 11 Aug 2023 13:39:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH -fixes] riscv: uaccess: Return the number of bytes effectively copied Content-Language: en-US To: Alexandre Ghiti , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alan Kao , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Bo YU , Aurelien Jarno References: <20230811110304.1613032-1-alexghiti@rivosinc.com> From: Alexandre Ghiti In-Reply-To: <20230811110304.1613032-1-alexghiti@rivosinc.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-GND-Sasl: alex@ghiti.fr X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS 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 On 11/08/2023 13:03, Alexandre Ghiti wrote: > It was reported that the riscv kernel hangs while executing the test > in [1]. > > Indeed, the test hangs when trying to write a buffer to a file. The > problem is that the riscv implementation of raw_copy_from_user() does not > return the number of bytes written when an exception happens and is fixed > up. I'll respin another version as the changelog and the title are incorrect: the uaccess routines should not return the number of bytes copied but actually the number of bytes not copied (this is what this patch implements). I'll wait for feedbacks before doing so! Sorry about that! Alex > > generic_perform_write() pre-faults the user pages and bails out if nothing > can be written, otherwise it will access the userspace buffer: here the > riscv implementation keeps returning it was not able to copy any byte > though the pre-faulting indicates otherwise. So generic_perform_write() > keeps retrying to access the user memory and ends up in an infinite > loop. > > Note that before the commit mentioned in [1] that introduced this > regression, it worked because generic_perform_write() would bail out if > only one byte could not be written. > > So fix this by returning the number of bytes effectively written in > __asm_copy_[to|from]_user() and __clear_user(), as it is expected. > > [1] https://lore.kernel.org/linux-riscv/20230309151841.bomov6hq3ybyp42a@debian/ > > Fixes: ebcbd75e3962 ("riscv: Fix the bug in memory access fixup code") > Reported-by: Bo YU > Closes: https://lore.kernel.org/linux-riscv/20230309151841.bomov6hq3ybyp42a@debian/#t > Reported-by: Aurelien Jarno > Closes: https://lore.kernel.org/linux-riscv/ZNOnCakhwIeue3yr@aurel32.net/ > Signed-off-by: Alexandre Ghiti > --- > arch/riscv/lib/uaccess.S | 11 +++++++---- > 1 file changed, 7 insertions(+), 4 deletions(-) > > diff --git a/arch/riscv/lib/uaccess.S b/arch/riscv/lib/uaccess.S > index ec486e5369d9..09b47ebacf2e 100644 > --- a/arch/riscv/lib/uaccess.S > +++ b/arch/riscv/lib/uaccess.S > @@ -17,8 +17,11 @@ ENTRY(__asm_copy_from_user) > li t6, SR_SUM > csrs CSR_STATUS, t6 > > - /* Save for return value */ > - mv t5, a2 > + /* > + * Save the terminal address which will be used to compute the number > + * of bytes copied in case of a fixup exception. > + */ > + add t5, a0, a2 > > /* > * Register allocation for code below: > @@ -176,7 +179,7 @@ ENTRY(__asm_copy_from_user) > 10: > /* Disable access to user memory */ > csrc CSR_STATUS, t6 > - mv a0, t5 > + sub a0, t5, a0 > ret > ENDPROC(__asm_copy_to_user) > ENDPROC(__asm_copy_from_user) > @@ -228,7 +231,7 @@ ENTRY(__clear_user) > 11: > /* Disable access to user memory */ > csrc CSR_STATUS, t6 > - mv a0, a1 > + sub a0, a3, a0 > ret > ENDPROC(__clear_user) > EXPORT_SYMBOL(__clear_user)