From patchwork Wed Mar 15 03:03:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Palmer Dabbelt X-Patchwork-Id: 13175240 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE1F3C7618D for ; Wed, 15 Mar 2023 03:05:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 62BE98E0003; Tue, 14 Mar 2023 23:05:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5D76E8E0001; Tue, 14 Mar 2023 23:05:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 450DF8E0003; Tue, 14 Mar 2023 23:05:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 379018E0001 for ; Tue, 14 Mar 2023 23:05:30 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 04883AAA14 for ; Wed, 15 Mar 2023 03:05:29 +0000 (UTC) X-FDA: 80569642020.28.B013C98 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf02.hostedemail.com (Postfix) with ESMTP id 2B90D80009 for ; Wed, 15 Mar 2023 03:05:27 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=hZOWobSL; spf=pass (imf02.hostedemail.com: domain of palmer@rivosinc.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=palmer@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678849528; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hPLiCKdxpc2r5PKNkHmqRCNmdb24wQijBhgYpLkC43U=; b=sGY37W0F9FkjuyMeA2gEpliJIfWphpK94S11Kzfwa0GdzN2paNtd+CTZeB5TFoNyq8T8ia o8e1iWmD5CXGt0O2XPPQ8lHopl1zmxNYHy83i4H3OEl5kFR8PNjLw9xQVfeBFfK63RWDgI XQNw2SKrvrV/KFeHpVBKUlNjhVNY77E= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=hZOWobSL; spf=pass (imf02.hostedemail.com: domain of palmer@rivosinc.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=palmer@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678849528; a=rsa-sha256; cv=none; b=YoWyMQDRv1DF4WQzrgn52tYsb/DHSftui/e+M14yIy1Gyzm1ZO2RfUIsR2M60aFesTBHFe HOwqbr/fhETGuDCj8TNz3paXpTAwvmgoGeZEpebHohJTQ7oyXxz41fj5go5zuWdzJAnL+j g3ZMNR0Z93CMckYP/XOGteRtdSW7yTg= Received: by mail-pj1-f51.google.com with SMTP id y15-20020a17090aa40f00b00237ad8ee3a0so466344pjp.2 for ; Tue, 14 Mar 2023 20:05:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1678849526; h=to:from:cc:content-transfer-encoding:mime-version:references :in-reply-to:message-id:date:subject:from:to:cc:subject:date :message-id:reply-to; bh=hPLiCKdxpc2r5PKNkHmqRCNmdb24wQijBhgYpLkC43U=; b=hZOWobSL1PKWIUT7UWS9tniLiB9mdfLQVx/Z/9zPgZaqvjnOteqzCtEB9HjY6uDm5Z iih9EdmkZVUeQo0xOfl1VcnfVacdcLCd8AJJAPxhyA2sm6Xke46e1e2rJ7Rh0BSVXkF8 LPvQJyzonpQFWDXt2SUjjq9ID4+tvuPP8BALY8prYRlpppPYUlsITVn79fdnaiUXQEF1 deb+t9btxFOPW+9acEXIhNbZp2doaKT5YgC3FtBNm79vfC3LW+6mWJ6+9PBqPIyH0Kic Coc17KHtjWazt5MqaHhgFOTwgQhPQ0N0ANZIoSAUsfoul0ONsbg8PSj+Y8JcyFTwqcEL HRCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678849526; h=to:from:cc:content-transfer-encoding:mime-version:references :in-reply-to:message-id:date:subject:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hPLiCKdxpc2r5PKNkHmqRCNmdb24wQijBhgYpLkC43U=; b=KM8lday4dOUgBPo2a0hkYQtPMknaWuHvIn93/FcRDEiiCYMjs4z/A4At+/vCSLwy/E SJoxrTAU6X7iVT7AsPd5K3ZSkmBu+GOZTd0BajFBW2FeMWQNhKiPTgtzpcH505naYUSU Tfvt6W7gSkSOD9gjmOOcSnBZgOy/6GoFks46BC9sOOkRqRt3oPAMio57/kYncNmybMx5 Mstz4ZgbzNOfVIHGrqPqHzGXpDwZEZLREKTGEiTlmivpEUlz+hDuySxXLmYTklwNteC1 Z4ZZfZ+iZSIsfHv/ETkqu/OaeAtE5EeVHLuMyiY8uulY0lIs4z6Ppxa6DZ94duRvcQZK tsag== X-Gm-Message-State: AO0yUKV3nHhJf1rvtRjLgqrmHbj/gD0ObRQ0MW+jtbNGuw2s5bZ0VLgU Db/+lUjmYFgU1uNA233AOV14LrmZY9KMJnhnMe0= X-Google-Smtp-Source: AK7set9vypcudDyWo/h8khv9E95mQjVI9aZbPGtfY3t0wFWv20PLrTeh2GiFiqI+9gk8Y0mpUagQ+Q== X-Received: by 2002:a17:902:ec8a:b0:19e:416e:abf5 with SMTP id x10-20020a170902ec8a00b0019e416eabf5mr1078987plg.34.1678849526457; Tue, 14 Mar 2023 20:05:26 -0700 (PDT) Received: from localhost ([50.221.140.188]) by smtp.gmail.com with ESMTPSA id s30-20020a63451e000000b0050300a7c8c2sm2194674pga.89.2023.03.14.20.05.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Mar 2023 20:05:26 -0700 (PDT) Subject: [PATCH v2 1/6] riscv/mm/fault: simplify code for do_page_fault() Date: Tue, 14 Mar 2023 20:03:54 -0700 Message-Id: <20230315030359.14162-2-palmer@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230315030359.14162-1-palmer@rivosinc.com> References: <20230315030359.14162-1-palmer@rivosinc.com> MIME-Version: 1.0 Cc: linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Tong Tiangen , Palmer Dabbelt From: Palmer Dabbelt To: akpm@linux-foundation.org X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2B90D80009 X-Stat-Signature: dkgu5xpno714cd6fxzbcxdij83h8gpfj X-Rspam-User: X-HE-Tag: 1678849527-957091 X-HE-Meta: U2FsdGVkX1/E8alQJcg2RHW+BmefYb5sjnBXfGUJQ6/cpYevoI7/TCxRK5Gy8PqSIoxXbe+OZ8h0ZRadANnb/lyqFBgHuDpHWe4Sxip+HuKC4vyeZiCggczTr3LJ+byW7eyYLvMj4oHjDhRPChHn6HQ/6FM06uk+j2Vsns3qevNjq0cucLOEh1rG/nqsflzeXbwXR2gSUjCh/nHkUj4WmcFGivCZb1x2f89qIZ5fXHSEHA4MPcezCRJ4TCnghr5sSa0skuHQX+FbV/3019uZ41Vy1O6N46WL7rDKQMcFY9OjJroLmc2sYVnFMsQKQKcaiAq9QEZN+HumtKFB/Ef7EJUd07CcTR5Bg+GU2AbD1KfwkevGoGsM5z9tSxV3CwXTLhcXwjnkNE4OnB+HdJ4gvuj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Ckf4KOcbNxPhtg4eGXpM4kw/u0ruLs1G8/IpZjAlC3YNoQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Tong Tiangen 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 Signed-off-by: Palmer Dabbelt --- 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 460f785f6e09..0a8c9afeee22 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; @@ -282,44 +315,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_BADACCESS)) { + code = SEGV_ACCERR; + return bad_area(regs, mm, code, addr); + } /* * If we need to retry but a fatal signal is pending, handle the