Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2577553rwi; Fri, 21 Oct 2022 05:38:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6w2y+czm6oR+oty307Xrzxz8+yToEPC5Nwov4lfL64FAf6KWhSVFFNQrl99x2D6VVGY8Sm X-Received: by 2002:a17:90b:3141:b0:20d:1690:d3d2 with SMTP id ip1-20020a17090b314100b0020d1690d3d2mr58459651pjb.230.1666355918867; Fri, 21 Oct 2022 05:38:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666355918; cv=none; d=google.com; s=arc-20160816; b=hcXlIIspqU/6OO5XotwIB4mMkCqPOsaJ932FP1RmlfPnkg8oiyu46WxtO6Hidc4FGf QJdmQ6kyP0WZtLrJppkRZm6oCJP4PDJ8UFZv0BWDEh8FPcO18mguRigiyYWrVj+nq/zL FbQHhyDL6fUCLEv1YLccK7gv+rMKvKjlixJV8bUxku04Q9JSl21kUKdLQNVmQYcBq3xO yrVJ33YKNrCvE7pfph8a0DjHzNRs+/osDrjDCmbvVg6FyriNuZ/moEgxA/CK8VzSOZKy fu/iOAqrk33U8UZExADlWzLMxFbWmMlWjqY52aTdx7At37jfP+c/QPxwDMFyGymiNmPs 7f/w== 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; bh=Imp7J1fjxWz8LXo4QoQPySBwx+lxw9aJ8LJdlpN9KoA=; b=bH2ELw/BF7v+gN0bFEKjFaET/lLMp02c2297bKhO6cekJ0EEOtKH91yoXF4cBH3o7M 2DN1LR/f/2ZBjj5EOc0G6dG7MKB5VnCFr4K8ldcUuSAcjrDELm7TWhyqWOI2aoa7bCYp dDgJX1Ayo/7iA3qjPVpZNeUoW+/xhZtTDavS5WAp+Ht1SrmkZgpSSkhi9g9aXoSOpixD oCn9zxfZCwcKYXD8ZDqESiESqoKgm6zE/gLm+kmLWMnmgftZdPqbG6oh0EIcK9GzH4Sz W50S96+wI18+CvGq9Ce8PCMJlWDbmgE1CWfBAKTptYJk/VQwcekzN4qYQkK+9Qe1tPam 4mRw== 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i27-20020a63541b000000b0044601bb2f96si27144947pgb.221.2022.10.21.05.38.25; Fri, 21 Oct 2022 05:38:38 -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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229966AbiJULyY (ORCPT + 99 others); Fri, 21 Oct 2022 07:54:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbiJULyV (ORCPT ); Fri, 21 Oct 2022 07:54:21 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB912264E4A for ; Fri, 21 Oct 2022 04:54:15 -0700 (PDT) Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Mv2tg64RCzHv7g; Fri, 21 Oct 2022 19:54:03 +0800 (CST) Received: from kwepemm600017.china.huawei.com (7.193.23.234) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 19:53:45 +0800 Received: from localhost.localdomain (10.175.112.125) by kwepemm600017.china.huawei.com (7.193.23.234) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 21 Oct 2022 19:53:45 +0800 From: Tong Tiangen To: Paul Walmsley , Palmer Dabbelt , Albert Ou CC: , , "Tong Tiangen" , , Guohanjun Subject: [PATCH -next] riscv/mm/fault: simplify code for do_page_fault() Date: Fri, 21 Oct 2022 12:19:32 +0000 Message-ID: <20221021121932.40967-1-tongtiangen@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600017.china.huawei.com (7.193.23.234) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,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 To make the code more hierarchical and readable, we fold vma related judgments into __do_page_fault(). And to simplify the code, move the tsk->thread.bad_cause's setting into bad_area(). No functional change intended. Signed-off-by: Tong Tiangen --- arch/riscv/mm/fault.c | 77 +++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c index d86f7cebd4a7..7194326ae96c 100644 --- a/arch/riscv/mm/fault.c +++ b/arch/riscv/mm/fault.c @@ -85,6 +85,8 @@ static inline void mm_fault_error(struct pt_regs *regs, unsigned long addr, vm_f static inline void bad_area(struct pt_regs *regs, struct mm_struct *mm, int code, unsigned long addr) { + current->thread.bad_cause = regs->cause; + /* * Something tried to access memory that isn't in our memory map. * Fix it, but check if it's kernel or user first. @@ -200,6 +202,38 @@ static inline bool access_error(unsigned long cause, struct vm_area_struct *vma) return false; } +#define VM_FAULT_BADMAP ((__force vm_fault_t)0x010000) +#define VM_FAULT_BADACCESS ((__force vm_fault_t)0x020000) + +static vm_fault_t __do_page_fault(struct mm_struct *mm, unsigned long addr, + unsigned int mm_flags, struct pt_regs *regs) +{ + struct vm_area_struct *vma = find_vma(mm, addr); + + if (unlikely(!vma)) + return VM_FAULT_BADMAP; + + if (unlikely(vma->vm_start > addr)) { + if (unlikely(!(vma->vm_flags & VM_GROWSDOWN) || + expand_stack(vma, addr))) + return VM_FAULT_BADMAP; + } + + /* + * Ok, we have a good vm_area for this memory access, so + * we can handle it. + */ + if (unlikely(access_error(regs->cause, vma))) + return VM_FAULT_BADACCESS; + + /* + * If for any reason at all we could not handle the fault, + * make sure we exit gracefully rather than endlessly redo + * the fault. + */ + return handle_mm_fault(vma, addr, mm_flags, regs); +} + /* * This routine handles page faults. It determines the address and the * problem, and then passes it off to one of the appropriate routines. @@ -207,7 +241,6 @@ static inline bool access_error(unsigned long cause, struct vm_area_struct *vma) asmlinkage void do_page_fault(struct pt_regs *regs) { struct task_struct *tsk; - struct vm_area_struct *vma; struct mm_struct *mm; unsigned long addr, cause; unsigned int flags = FAULT_FLAG_DEFAULT; @@ -280,44 +313,16 @@ asmlinkage void do_page_fault(struct pt_regs *regs) flags |= FAULT_FLAG_INSTRUCTION; retry: mmap_read_lock(mm); - vma = find_vma(mm, addr); - if (unlikely(!vma)) { - tsk->thread.bad_cause = cause; - bad_area(regs, mm, code, addr); - return; - } - if (likely(vma->vm_start <= addr)) - goto good_area; - if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { - tsk->thread.bad_cause = cause; - bad_area(regs, mm, code, addr); - return; - } - if (unlikely(expand_stack(vma, addr))) { - tsk->thread.bad_cause = cause; - bad_area(regs, mm, code, addr); - return; - } - /* - * Ok, we have a good vm_area for this memory access, so - * we can handle it. - */ -good_area: - code = SEGV_ACCERR; + fault = __do_page_fault(mm, addr, flags, regs); - if (unlikely(access_error(cause, vma))) { - tsk->thread.bad_cause = cause; - bad_area(regs, mm, code, addr); - return; - } + if (unlikely(fault & VM_FAULT_BADMAP)) + return bad_area(regs, mm, code, addr); - /* - * If for any reason at all we could not handle the fault, - * make sure we exit gracefully rather than endlessly redo - * the fault. - */ - fault = handle_mm_fault(vma, addr, flags, regs); + if (unlikely(fault & VM_FAULT_BADMAP)) { + code = SEGV_ACCERR; + return bad_area(regs, mm, code, addr); + } /* * If we need to retry but a fatal signal is pending, handle the -- 2.25.1