From patchwork Thu Oct 19 14:01:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13429255 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 76987CDB465 for ; Thu, 19 Oct 2023 14:03:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EF4848D0019; Thu, 19 Oct 2023 10:03:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E9E478D0006; Thu, 19 Oct 2023 10:03:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3FC48D0019; Thu, 19 Oct 2023 10:03:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BF3048D0006 for ; Thu, 19 Oct 2023 10:03:32 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8B2231405A1 for ; Thu, 19 Oct 2023 14:03:32 +0000 (UTC) X-FDA: 81362378664.29.96CAA14 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) by imf15.hostedemail.com (Postfix) with ESMTP id B15A9A002E for ; Thu, 19 Oct 2023 14:03:05 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nZB1Tkis; dmarc=none; spf=pass (imf15.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.167.45 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697724185; 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=hd/FLevx18XhR5UIn9smwlv93JRL3lzcOo0hNfovrwU=; b=qhhMEfYlQQ5HVd4bxzPEX2MhgyWVsR7sWk9Yk1U2/u12haEUDMHmCLNxBlpjh4dmG5++4g zjwA1fNbuSiKecuH/tC0YjV2o0UxVAusGxs+2FeD9Lhm/dOeBIxp4GItJqn5KaSwXTa98z PwpA82NEBR93ug6vMM1P6YawClRssQ8= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nZB1Tkis; dmarc=none; spf=pass (imf15.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.167.45 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697724185; a=rsa-sha256; cv=none; b=u9mXSw1hnL8JCBanwSe1ON+E80laJrI8ENpy6vqNVzV6Xr0Q7hIhQP93OwKmrpKw4Me0wA je8PVuFu6aPHcY3RLGnZjuRa+phlVzD7fXGPHmDVITGTETJwtsSBMJqOYSXbQRsZIKpOEY pTAa2THWq7fKXDjAX+yYYatSGo8JDM4= Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-507a0907896so7843986e87.2 for ; Thu, 19 Oct 2023 07:03:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1697724184; x=1698328984; darn=kvack.org; 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=hd/FLevx18XhR5UIn9smwlv93JRL3lzcOo0hNfovrwU=; b=nZB1TkisA8tHSJUiDK4D4bKSRW9SS/NSRusXrHMWVBeeU32ACGTziRTEuRN7QqCI8E /o+ElW8HseaVQIwjeZSLx2UOlMYMUd+jA6LFHYZnivo72FtIvKyoeweT6cIgrqNbQZMa Wz5GTfgk/ilcdxJvui4hRpbltVwe2qUFeTCn5ENpfHrun0W9oIqHUSVOBJsxcwkTXCZz SjYPorGK5ThlaviUNEsu6m6Fr82vwBtOWcBPi+4M9tuxps2/Oiw/rGASattcrYnGmdvj 1RsT4R3Afg7ppfYy8RTaZKx5/MfkzLiFBmTrJwI4ddDFZgbb+F3zzrbrl7VsGQFzQlqF mesg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697724184; x=1698328984; 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=hd/FLevx18XhR5UIn9smwlv93JRL3lzcOo0hNfovrwU=; b=WGnxLoW+D9Qu6dTZ1b2f3UiAl1Lvr6lmGm3NmsIrQPWia6OqH9QFOL5Y97T4X6PRT7 mS+xlXAYsna4HcDrGR/ZHazXO27BHtnJIq1JMV7FLi6E3+79+YWDFnb6F7wXgFbPN6CY TAn3eUPs6Qz67Wrxdus7LpT6nyIfkNzqLi6dq7/bcKYUpUm0bcVRdtIJi9+I3OQq7cwY +lQknIqstSGvcuEY2mSdwIgc/Wm+Ti2WmM904FyXsfiwdQ2QivrMcbW/A4Ie6FoStXay CFoD+Jf3QGNHtS48XA1acSel9e7dfveROSfV/uIUkS4LONNYGekTj79QWn9v1E2KNpS3 tOZw== X-Gm-Message-State: AOJu0YxPW+iMHHJelB2WmnvVE1w/BzpnwiBlj8L7yBHC+UuQ+PIT+CQQ 70wjp8SYhNZYxi9NaQw/BTc4dQ== X-Google-Smtp-Source: AGHT+IEreDwOMoW56yjrW09GCEE/pI+6cCamWfZVDmTeqTnMJ6ObGDZTCddBTRTbchZ8BncLsOt9fw== X-Received: by 2002:ac2:5a04:0:b0:507:b993:bb86 with SMTP id q4-20020ac25a04000000b00507b993bb86mr1486713lfn.66.1697724183683; Thu, 19 Oct 2023 07:03:03 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id q9-20020a05600000c900b0032d8eecf901sm4567033wrx.3.2023.10.19.07.03.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 07:03:03 -0700 (PDT) From: Alexandre Ghiti To: Will Deacon , "Aneesh Kumar K . V" , Andrew Morton , Nick Piggin , Peter Zijlstra , Mayuresh Chitale , Vincent Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Samuel Holland , Lad Prabhakar Cc: Alexandre Ghiti , Andrew Jones , Samuel Holland , Lad Prabhakar Subject: [PATCH v5 1/4] riscv: Improve tlb_flush() Date: Thu, 19 Oct 2023 16:01:48 +0200 Message-Id: <20231019140151.21629-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231019140151.21629-1-alexghiti@rivosinc.com> References: <20231019140151.21629-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: B15A9A002E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: j55b8mt9zdi9so51x16uo3jneg6eigjy X-HE-Tag: 1697724185-395231 X-HE-Meta: U2FsdGVkX18fl2bPb7cKCffczwjxciK2ddTXhlPE5VxUy1Sh68h+WCtUlw91rt38gEecWezrXHd6DqCWvrXm1Bc9UaE9ABVxiHuNpsrPKoZ57+21zzFgxD+WRfAEDs8XdZ71rg8LA6ugdueQdCiCZ8Hw6QEEl6LmIM4LdnoaVgddubwYdwj5nWYgZ88BZ5QEFSJ6Y8JAH0GMf5nE4IEFJjPY5C+H4aj4aMnfJBGobOGccGd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54c+VSHPkqnLuj1DOTpwuc8uzXqWYdS26bqHG8+zuOyJwh6UfBfB1TPC21Q6m/MZnIHBdNZRD4s9M+HVwn1UILNaMkwTVKhG/4wXs4pNBxxSRYSbPNoeqkDSAH2mtybv2wYxN6Wxdm+b43iJ2+LSP7rtEdt87Zw4i+rDm0Y58vL2iOOPUID/249lGNPqOoMWdiOIdROwoQc8yDh+nhJfDvEsDi4tSvvk/hPvVzSNt7fSLPPIl1gZ3TG2adSRHLd/x+KRgymWV1ISXA5OIMPP3Yax/418Si2hwalcWhSWbaOJtIijEC21eXCyZ0CWgWoWuZqN8s5wqcsrGQuS9u4cMzK0qcK0gdUR2CLLKXr9GtlVZ/SE= 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: For now, tlb_flush() simply calls flush_tlb_mm() which results in a flush of the whole TLB. So let's use mmu_gather fields to provide a more fine-grained flush of the TLB. Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones Reviewed-by: Samuel Holland Tested-by: Lad Prabhakar # On RZ/Five SMARC --- arch/riscv/include/asm/tlb.h | 8 +++++++- arch/riscv/include/asm/tlbflush.h | 3 +++ arch/riscv/mm/tlbflush.c | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/tlb.h b/arch/riscv/include/asm/tlb.h index 120bcf2ed8a8..1eb5682b2af6 100644 --- a/arch/riscv/include/asm/tlb.h +++ b/arch/riscv/include/asm/tlb.h @@ -15,7 +15,13 @@ static void tlb_flush(struct mmu_gather *tlb); static inline void tlb_flush(struct mmu_gather *tlb) { - flush_tlb_mm(tlb->mm); +#ifdef CONFIG_MMU + if (tlb->fullmm || tlb->need_flush_all) + flush_tlb_mm(tlb->mm); + else + flush_tlb_mm_range(tlb->mm, tlb->start, tlb->end, + tlb_get_unmap_size(tlb)); +#endif } #endif /* _ASM_RISCV_TLB_H */ diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h index a09196f8de68..f5c4fb0ae642 100644 --- a/arch/riscv/include/asm/tlbflush.h +++ b/arch/riscv/include/asm/tlbflush.h @@ -32,6 +32,8 @@ static inline void local_flush_tlb_page(unsigned long addr) #if defined(CONFIG_SMP) && defined(CONFIG_MMU) void flush_tlb_all(void); void flush_tlb_mm(struct mm_struct *mm); +void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, + unsigned long end, unsigned int page_size); void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr); void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end); @@ -52,6 +54,7 @@ static inline void flush_tlb_range(struct vm_area_struct *vma, } #define flush_tlb_mm(mm) flush_tlb_all() +#define flush_tlb_mm_range(mm, start, end, page_size) flush_tlb_all() #endif /* !CONFIG_SMP || !CONFIG_MMU */ /* Flush a range of kernel pages */ diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 77be59aadc73..fa03289853d8 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -132,6 +132,13 @@ void flush_tlb_mm(struct mm_struct *mm) __flush_tlb_range(mm, 0, -1, PAGE_SIZE); } +void flush_tlb_mm_range(struct mm_struct *mm, + unsigned long start, unsigned long end, + unsigned int page_size) +{ + __flush_tlb_range(mm, start, end - start, page_size); +} + void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr) { __flush_tlb_range(vma->vm_mm, addr, PAGE_SIZE, PAGE_SIZE); From patchwork Thu Oct 19 14:01:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13429272 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 919A4CDB465 for ; Thu, 19 Oct 2023 14:04:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3558C8D001F; Thu, 19 Oct 2023 10:04:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 306498D0006; Thu, 19 Oct 2023 10:04:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CDFD8D001F; Thu, 19 Oct 2023 10:04:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0EA248D0006 for ; Thu, 19 Oct 2023 10:04:49 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D1BB81CAB85 for ; Thu, 19 Oct 2023 14:04:48 +0000 (UTC) X-FDA: 81362381856.09.AD5571B Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) by imf28.hostedemail.com (Postfix) with ESMTP id 1ECC2C0160 for ; Thu, 19 Oct 2023 14:04:12 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=uUQyVn0w; spf=pass (imf28.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697724253; 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=/ApwA47W5sfKWNkq2IymUPAqINYWJl4bGVtXFqY2oGI=; b=1m0Bh/WxLCGZvAs61GEeAEg9C31HhmZnuDtWSu+bDFOA0hCYTQNb6fC2vQp7TE1E98yA1a RObZHIgm7eVRg+Tp6bsMA+XK4ugXyvovh+3em99VN860HP0jqgDkDlms/iMeu1q8PSUeEo cyNwDlUYQtx2Br7TbdDgnCc7DdThBj0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697724253; a=rsa-sha256; cv=none; b=8VgpiSAiWsE/p8Ti0fX9ElUaVAUgWx4JoLbGQAeg0KMybraehRoTnPiOQADeJxYUMPeNZ+ PluwdFqurRORtQHPABPXB655u94Ecu4QsnGoltINebexYKBS9V6ZPONJQ+sTELV4yNWRjG d+c9+pvbIMc72v7yCTMbGhdRSG0AyNw= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=uUQyVn0w; spf=pass (imf28.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2b9338e4695so109319811fa.2 for ; Thu, 19 Oct 2023 07:04:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1697724251; x=1698329051; darn=kvack.org; 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=/ApwA47W5sfKWNkq2IymUPAqINYWJl4bGVtXFqY2oGI=; b=uUQyVn0wIrZ57tLxJQ9hF4T0XR/9tBmHMXpYR1dR5hug4q5M327mUMv0BW74EUHuCf nNWKyPPFa1fJz+XkANt5fNf/JtB1k2IBD2ko356WI7Irqt+BtsiZQkkm4e12qH2K8V27 NYIv60chBFd0dNyD3SwSkTPG2zNS8miqK5DTUQQJUFYXKvOdAV/YEwUcfPb4/XCj023q 42tu/UQioFELIbvucx0HpUvjBTW2/jzMSWz3yVUUXW/C6RjsYp3exa1nkJCKrCDARoN3 zoiY4ofmoVHkDsBcjkFLtoHrJy6YvCdjuru92lQbZKc4HULdQFARQncRXdJMbDGSGmN1 wo8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697724251; x=1698329051; 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=/ApwA47W5sfKWNkq2IymUPAqINYWJl4bGVtXFqY2oGI=; b=hIpE3thNkbt1XBoLzPQtv55wxet4tv9ihGEwSsTJfWWqWUuhtCXGKhNMcVYKtClAWP zv93WNO6ZoH6mEMkPdiS/6vJIDNCoknin0RGxPuhnokisRHxWzbQHyyDKz56CiMxC28u lr2rKwHI56KxsxRZZcSeqPKGY1J3tr8h02Kf7HKfcxsvaIL8dl6aUGQPAS/g6LyvlyKm 6oTYzwWVYt655YsRDVZYCCo1CpOfUlPh/hNFtY7yv7nl2DhWarUByWyJg5KQMpihBlNu ybS4Fl1csiBVxKQqh0NQl/zjwVLBG0NwyZrZnWOLd2WxfnCRO+2VLi2JIcswwDsda802 WH7g== X-Gm-Message-State: AOJu0YyqeHmJP27XanmaZDBqFqYgfVd4yjuvwNYoAmDMCbKcW+QfL2nz O+Ej5a3TdUsY8HlvQVIiQuYxMA== X-Google-Smtp-Source: AGHT+IEBYCq6FlJQnZX7dLdouPmrLmiH7GFteySmGUDpOnC8cPojpOsH5qTZKNPlCuCg8e75j80KCg== X-Received: by 2002:a05:651c:1502:b0:2c5:1ad0:e2ff with SMTP id e2-20020a05651c150200b002c51ad0e2ffmr2017092ljf.39.1697724250744; Thu, 19 Oct 2023 07:04:10 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id az15-20020a05600c600f00b00406447b798bsm4543769wmb.37.2023.10.19.07.04.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 07:04:05 -0700 (PDT) From: Alexandre Ghiti To: Will Deacon , "Aneesh Kumar K . V" , Andrew Morton , Nick Piggin , Peter Zijlstra , Mayuresh Chitale , Vincent Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Samuel Holland , Lad Prabhakar Cc: Alexandre Ghiti Subject: [PATCH v5 2/4] riscv: Improve flush_tlb_range() for hugetlb pages Date: Thu, 19 Oct 2023 16:01:49 +0200 Message-Id: <20231019140151.21629-3-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231019140151.21629-1-alexghiti@rivosinc.com> References: <20231019140151.21629-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: 1mm63pnksr3cy4148cqqgrn6w85718sn X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 1ECC2C0160 X-Rspam-User: X-HE-Tag: 1697724252-219550 X-HE-Meta: U2FsdGVkX1/PC10ZFceGUBYJ3eWsB/hYsJM6ZSRr4ZRthQJyqQ/3DC2m/d+49otA6HT6U7CpuW0e9o10FzxDJBBkkxMH2YevOWf9kECZRBEyKt4435KQDTIUAQ25jYFCGfPLHYHIHR1MJOnm31zDjsSuGjB1H2aQPZ+myhe1iwZ0u6T87gEVhA4zfnQiTi6softg5DTGUK9g3qsofkhOkhif8B/XiUunO0jEN5vnUxF2VV2wJHfl0QB5Vj94aOqVvEV64Ml2s6ESSbhF7n1R1bT7KcO0J0a95zGtC+cbrZ8PaB1p6a/hv1VnYiBYLpf2hvd87uOKNtBzcVp8JKx+tOtiMeN0kfwx5B+lDHKDBmhPyP8AOA0BMMV4sa1Gmq2GYRxhzBf/LMUbNzVbZdKOv04TBZUeDktSSajUG2W3xoZdeU+zB5pucE0Mv5AOUDi0JpahwaHoR3oiHa6ED/cniNQa6CaYZRtzaFN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wuEkVHasMYd 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: flush_tlb_range() uses a fixed stride of PAGE_SIZE and in its current form, when a hugetlb mapping needs to be flushed, flush_tlb_range() flushes the whole tlb: so set a stride of the size of the hugetlb mapping in order to only flush the hugetlb mapping. However, if the hugepage is a NAPOT region, all PTEs that constitute this mapping must be invalidated, so the stride size must actually be the size of the PTE. Note that THPs are directly handled by flush_pmd_tlb_range(). Signed-off-by: Alexandre Ghiti Reviewed-by: Samuel Holland Tested-by: Lad Prabhakar # --- arch/riscv/mm/tlbflush.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index fa03289853d8..5933744df91a 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -147,7 +148,35 @@ void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr) void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) { - __flush_tlb_range(vma->vm_mm, start, end - start, PAGE_SIZE); + unsigned long stride_size; + + if (!is_vm_hugetlb_page(vma)) { + stride_size = PAGE_SIZE; + } else { + stride_size = huge_page_size(hstate_vma(vma)); + +#ifdef CONFIG_RISCV_ISA_SVNAPOT + /* + * As stated in the privileged specification, every PTE in a + * NAPOT region must be invalidated, so reset the stride in that + * case. + */ + if (has_svnapot()) { + if (stride_size >= PGDIR_SIZE) + stride_size = PGDIR_SIZE; + else if (stride_size >= P4D_SIZE) + stride_size = P4D_SIZE; + else if (stride_size >= PUD_SIZE) + stride_size = PUD_SIZE; + else if (stride_size >= PMD_SIZE) + stride_size = PMD_SIZE; + else + stride_size = PAGE_SIZE; + } +#endif + } + + __flush_tlb_range(vma->vm_mm, start, end - start, stride_size); } #ifdef CONFIG_TRANSPARENT_HUGEPAGE void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, From patchwork Thu Oct 19 14:01:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13429273 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 E8297CDB483 for ; Thu, 19 Oct 2023 14:05:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 838EE8D002B; Thu, 19 Oct 2023 10:05:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E8618D0006; Thu, 19 Oct 2023 10:05:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B12A8D002B; Thu, 19 Oct 2023 10:05:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5A3BB8D0006 for ; Thu, 19 Oct 2023 10:05:21 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B8130A05EA for ; Thu, 19 Oct 2023 14:05:20 +0000 (UTC) X-FDA: 81362383200.30.D451A53 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by imf03.hostedemail.com (Postfix) with ESMTP id 01DAE20028 for ; Thu, 19 Oct 2023 14:05:14 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NtWoNYX2; spf=pass (imf03.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.46 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697724315; 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=UcqaVPeAGwTbj9zEMGR6ijY363bXbBd6qhh4XcO8DLA=; b=Vw3RNdWwq5vaX+TZNV/NkR0vNL7ZWHWkYbAmLy3dhiVGo4hAG3LAE0kseCvNx0cMLFslw7 zngwmRvhNRZ/V4lh2L8oB9F9gRFrIxdOyoAOzeQ0x2hbbqNbecZmVwSKc85ngvkkd33xC9 LUhkOQIa9JZxhoLgYfL9eV7du2dp7sM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697724315; a=rsa-sha256; cv=none; b=7jN3ZiTNMmAGPYaLkBreqm042c/hdyZVDod9ftir3EoQgfCCqQH5K8kyjpjnnEF4JbsL4z iRuSOYIKCOounhoc3+qtKQkKGcOanWgQ6JUcLFjouisESl0b2MIyYmdkEVHwYI5O3KymdZ UrAvwu5egBb0ToOE7O2/aKDlcrg5iI8= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NtWoNYX2; spf=pass (imf03.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.46 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-40790b0a224so25241625e9.0 for ; Thu, 19 Oct 2023 07:05:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1697724313; x=1698329113; darn=kvack.org; 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=UcqaVPeAGwTbj9zEMGR6ijY363bXbBd6qhh4XcO8DLA=; b=NtWoNYX2GnYvDClylHqxIrFCgZohQm4ndD60LeVSF+IPnE1mO5ZKrOwgKXcvT6hkG6 KQL61eGXX+MOzHr+CTYJfEqJjv1pMJNUMwiHTHZ/iZU+yedkBwerxqg1yt1SyR5lzZ96 3TrIz+oBnBi+NRQxoQxioRRMUlblESpNsm5SWRhjoNUuW+HjVOq6PTLFqepZzebo1lAy Nidp+3ew791xjIqM3ZoJtqPl0f0WcxaXXok4JnxR1Zzw53kF4gV31KfGCBV6yd8zUx4P uwl1ADeVSX3m5AK+GdRWn1MF+NPP8LUuzRbf9wyWBU+CWlrsz5tRiQnEqenA+OnKg9Wg ARpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697724313; x=1698329113; 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=UcqaVPeAGwTbj9zEMGR6ijY363bXbBd6qhh4XcO8DLA=; b=Eo/iSYa1Q/JYgsUIAmRmhCmIXUDgrJJbuAOjIOX8MstUm8imEL/4oV2J7jTjsVET7W d8NGqjfMjccexsloMsUqBcKFdFp7eyo80t6deJdrgGFGXVEG8EkcbcId98FkakWOWvZ7 RcS3YFLwRs7QauqTVIhXEV/l8OktWlmU4lo5iLaWidNt0KJ6FxBxrdFOyAGr8vAZhhX+ WF9BDb+0Ax9QU6PH1xutcuKDAThi47SDy1xAGaFjjr4uorJ0xv9adW9WdMsRJrsfNtfx FOeSmHLJOAbAEf/9YXv38W7QcaK+x7dl8mVfHYwIUIYyZ64yzudgkBhmFV8p9HFh4Lh1 GCZQ== X-Gm-Message-State: AOJu0YydWLVMMPDp0bQ6LOTktGxMhNNglNPcPvY7aRB9vD9ExgvDA1t2 /KAtm/Mx6AKcyvJrLt02/36jJQ== X-Google-Smtp-Source: AGHT+IHnKiCvjHaBH1kde0mGFwQY25hXd0xzfatBl3O3oogknqg699PegckRO25Yp/WvPwgt7YDLxw== X-Received: by 2002:a5d:5908:0:b0:32d:bafd:809f with SMTP id v8-20020a5d5908000000b0032dbafd809fmr1389930wrd.70.1697724313195; Thu, 19 Oct 2023 07:05:13 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id q9-20020a05600000c900b0032d8eecf901sm4571012wrx.3.2023.10.19.07.05.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 07:05:12 -0700 (PDT) From: Alexandre Ghiti To: Will Deacon , "Aneesh Kumar K . V" , Andrew Morton , Nick Piggin , Peter Zijlstra , Mayuresh Chitale , Vincent Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Samuel Holland , Lad Prabhakar Cc: Alexandre Ghiti , Andrew Jones , Lad Prabhakar Subject: [PATCH v5 3/4] riscv: Make __flush_tlb_range() loop over pte instead of flushing the whole tlb Date: Thu, 19 Oct 2023 16:01:50 +0200 Message-Id: <20231019140151.21629-4-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231019140151.21629-1-alexghiti@rivosinc.com> References: <20231019140151.21629-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: esuarisawuwg18fmqc9g191gtnqi9k9o X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 01DAE20028 X-Rspam-User: X-HE-Tag: 1697724314-806805 X-HE-Meta: U2FsdGVkX18R55iFORJWmlDe9lXnU0NcQJ+f4iXVVBP/H5rmmGiMd7kmQknzuGPVnxmYfkaciRE0Vb1TqVdBZ5H8ydzpITcnGFMmDJi5X9UlYrtIBt2zZSVCe+50SR1hngDefq4/JPZpZLeRkNpnBKh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ZmpKSZiIgJJzEuN8aSeEqL/nc9XuyFfD3EkaIgQKoCoGgz5RD/TqQ3vZ0qN9/SribCi4+eRndrlS4r4FCe69KTuSK7eRR5g4cxKD0FuJK5jYMfKUfnw9+5d+lQnYP8fKkhW/XoNTtoijMssWsGDtS0eoLRzgctqPSKGT9mP4+a87D8Argi3Sa0Kly/HHJ7YHEUhalnzWZ6XkoTuQlT6T3Ipf0bTa0A5El1HsAOheN7jt0SEAj3xeCfZb4o3rHDxuffnqdzbb8zpDFlq0PCnYJRn66OqhGU4zLEfb5OnQGHEaWO082mXKJPncjSUOxN9tkOeiBIO1DXvpJrjwni+Vy43z7K3deHQgxhPBYS5u9a8tVfJEiz4MRfcsEaAGueLN0Jj/h3486WdA3rZnNdXwTfA= 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: Currently, when the range to flush covers more than one page (a 4K page or a hugepage), __flush_tlb_range() flushes the whole tlb. Flushing the whole tlb comes with a greater cost than flushing a single entry so we should flush single entries up to a certain threshold so that: threshold * cost of flushing a single entry < cost of flushing the whole tlb. Co-developed-by: Mayuresh Chitale Signed-off-by: Mayuresh Chitale Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones Tested-by: Lad Prabhakar # On RZ/Five SMARC Reviewed-by: Samuel Holland Tested-by: Samuel Holland --- arch/riscv/include/asm/sbi.h | 3 - arch/riscv/include/asm/tlbflush.h | 3 + arch/riscv/kernel/sbi.c | 32 +++------ arch/riscv/mm/tlbflush.c | 115 +++++++++++++++--------------- 4 files changed, 72 insertions(+), 81 deletions(-) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 12dfda6bb924..0892f4421bc4 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -280,9 +280,6 @@ void sbi_set_timer(uint64_t stime_value); void sbi_shutdown(void); void sbi_send_ipi(unsigned int cpu); int sbi_remote_fence_i(const struct cpumask *cpu_mask); -int sbi_remote_sfence_vma(const struct cpumask *cpu_mask, - unsigned long start, - unsigned long size); int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask, unsigned long start, diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h index f5c4fb0ae642..170a49c531c6 100644 --- a/arch/riscv/include/asm/tlbflush.h +++ b/arch/riscv/include/asm/tlbflush.h @@ -11,6 +11,9 @@ #include #include +#define FLUSH_TLB_MAX_SIZE ((unsigned long)-1) +#define FLUSH_TLB_NO_ASID ((unsigned long)-1) + #ifdef CONFIG_MMU extern unsigned long asid_mask; diff --git a/arch/riscv/kernel/sbi.c b/arch/riscv/kernel/sbi.c index c672c8ba9a2a..5a62ed1da453 100644 --- a/arch/riscv/kernel/sbi.c +++ b/arch/riscv/kernel/sbi.c @@ -11,6 +11,7 @@ #include #include #include +#include /* default SBI version is 0.1 */ unsigned long sbi_spec_version __ro_after_init = SBI_SPEC_VERSION_DEFAULT; @@ -376,32 +377,15 @@ int sbi_remote_fence_i(const struct cpumask *cpu_mask) } EXPORT_SYMBOL(sbi_remote_fence_i); -/** - * sbi_remote_sfence_vma() - Execute SFENCE.VMA instructions on given remote - * harts for the specified virtual address range. - * @cpu_mask: A cpu mask containing all the target harts. - * @start: Start of the virtual address - * @size: Total size of the virtual address range. - * - * Return: 0 on success, appropriate linux error code otherwise. - */ -int sbi_remote_sfence_vma(const struct cpumask *cpu_mask, - unsigned long start, - unsigned long size) -{ - return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA, - cpu_mask, start, size, 0, 0); -} -EXPORT_SYMBOL(sbi_remote_sfence_vma); - /** * sbi_remote_sfence_vma_asid() - Execute SFENCE.VMA instructions on given - * remote harts for a virtual address range belonging to a specific ASID. + * remote harts for a virtual address range belonging to a specific ASID or not. * * @cpu_mask: A cpu mask containing all the target harts. * @start: Start of the virtual address * @size: Total size of the virtual address range. - * @asid: The value of address space identifier (ASID). + * @asid: The value of address space identifier (ASID), or FLUSH_TLB_NO_ASID + * for flushing all address spaces. * * Return: 0 on success, appropriate linux error code otherwise. */ @@ -410,8 +394,12 @@ int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask, unsigned long size, unsigned long asid) { - return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID, - cpu_mask, start, size, asid, 0); + if (asid == FLUSH_TLB_NO_ASID) + return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA, + cpu_mask, start, size, 0, 0); + else + return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID, + cpu_mask, start, size, asid, 0); } EXPORT_SYMBOL(sbi_remote_sfence_vma_asid); diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 5933744df91a..c27ba720e35f 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -9,28 +9,50 @@ static inline void local_flush_tlb_all_asid(unsigned long asid) { - __asm__ __volatile__ ("sfence.vma x0, %0" - : - : "r" (asid) - : "memory"); + if (asid != FLUSH_TLB_NO_ASID) + __asm__ __volatile__ ("sfence.vma x0, %0" + : + : "r" (asid) + : "memory"); + else + local_flush_tlb_all(); } static inline void local_flush_tlb_page_asid(unsigned long addr, unsigned long asid) { - __asm__ __volatile__ ("sfence.vma %0, %1" - : - : "r" (addr), "r" (asid) - : "memory"); + if (asid != FLUSH_TLB_NO_ASID) + __asm__ __volatile__ ("sfence.vma %0, %1" + : + : "r" (addr), "r" (asid) + : "memory"); + else + local_flush_tlb_page(addr); } -static inline void local_flush_tlb_range(unsigned long start, - unsigned long size, unsigned long stride) +/* + * Flush entire TLB if number of entries to be flushed is greater + * than the threshold below. + */ +static unsigned long tlb_flush_all_threshold __read_mostly = 64; + +static void local_flush_tlb_range_threshold_asid(unsigned long start, + unsigned long size, + unsigned long stride, + unsigned long asid) { - if (size <= stride) - local_flush_tlb_page(start); - else - local_flush_tlb_all(); + u16 nr_ptes_in_range = DIV_ROUND_UP(size, stride); + int i; + + if (nr_ptes_in_range > tlb_flush_all_threshold) { + local_flush_tlb_all_asid(asid); + return; + } + + for (i = 0; i < nr_ptes_in_range; ++i) { + local_flush_tlb_page_asid(start, asid); + start += stride; + } } static inline void local_flush_tlb_range_asid(unsigned long start, @@ -38,8 +60,10 @@ static inline void local_flush_tlb_range_asid(unsigned long start, { if (size <= stride) local_flush_tlb_page_asid(start, asid); - else + else if (size == FLUSH_TLB_MAX_SIZE) local_flush_tlb_all_asid(asid); + else + local_flush_tlb_range_threshold_asid(start, size, stride, asid); } static void __ipi_flush_tlb_all(void *info) @@ -52,7 +76,7 @@ void flush_tlb_all(void) if (riscv_use_ipi_for_rfence()) on_each_cpu(__ipi_flush_tlb_all, NULL, 1); else - sbi_remote_sfence_vma(NULL, 0, -1); + sbi_remote_sfence_vma_asid(NULL, 0, FLUSH_TLB_MAX_SIZE, FLUSH_TLB_NO_ASID); } struct flush_tlb_range_data { @@ -69,18 +93,12 @@ static void __ipi_flush_tlb_range_asid(void *info) local_flush_tlb_range_asid(d->start, d->size, d->stride, d->asid); } -static void __ipi_flush_tlb_range(void *info) -{ - struct flush_tlb_range_data *d = info; - - local_flush_tlb_range(d->start, d->size, d->stride); -} - static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, unsigned long size, unsigned long stride) { struct flush_tlb_range_data ftd; struct cpumask *cmask = mm_cpumask(mm); + unsigned long asid = FLUSH_TLB_NO_ASID; unsigned int cpuid; bool broadcast; @@ -90,39 +108,24 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, cpuid = get_cpu(); /* check if the tlbflush needs to be sent to other CPUs */ broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids; - if (static_branch_unlikely(&use_asid_allocator)) { - unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask; - - if (broadcast) { - if (riscv_use_ipi_for_rfence()) { - ftd.asid = asid; - ftd.start = start; - ftd.size = size; - ftd.stride = stride; - on_each_cpu_mask(cmask, - __ipi_flush_tlb_range_asid, - &ftd, 1); - } else - sbi_remote_sfence_vma_asid(cmask, - start, size, asid); - } else { - local_flush_tlb_range_asid(start, size, stride, asid); - } + + if (static_branch_unlikely(&use_asid_allocator)) + asid = atomic_long_read(&mm->context.id) & asid_mask; + + if (broadcast) { + if (riscv_use_ipi_for_rfence()) { + ftd.asid = asid; + ftd.start = start; + ftd.size = size; + ftd.stride = stride; + on_each_cpu_mask(cmask, + __ipi_flush_tlb_range_asid, + &ftd, 1); + } else + sbi_remote_sfence_vma_asid(cmask, + start, size, asid); } else { - if (broadcast) { - if (riscv_use_ipi_for_rfence()) { - ftd.asid = 0; - ftd.start = start; - ftd.size = size; - ftd.stride = stride; - on_each_cpu_mask(cmask, - __ipi_flush_tlb_range, - &ftd, 1); - } else - sbi_remote_sfence_vma(cmask, start, size); - } else { - local_flush_tlb_range(start, size, stride); - } + local_flush_tlb_range_asid(start, size, stride, asid); } put_cpu(); @@ -130,7 +133,7 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, void flush_tlb_mm(struct mm_struct *mm) { - __flush_tlb_range(mm, 0, -1, PAGE_SIZE); + __flush_tlb_range(mm, 0, FLUSH_TLB_MAX_SIZE, PAGE_SIZE); } void flush_tlb_mm_range(struct mm_struct *mm, From patchwork Thu Oct 19 14:01:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13429274 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 B74F3CDB465 for ; Thu, 19 Oct 2023 14:06:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F46B8D0097; Thu, 19 Oct 2023 10:06:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A3E78D0006; Thu, 19 Oct 2023 10:06:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 26CA58D0097; Thu, 19 Oct 2023 10:06:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 16A578D0006 for ; Thu, 19 Oct 2023 10:06:18 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E84CD1CAD1E for ; Thu, 19 Oct 2023 14:06:17 +0000 (UTC) X-FDA: 81362385594.06.3430C3D Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by imf29.hostedemail.com (Postfix) with ESMTP id 05F8A12000D for ; Thu, 19 Oct 2023 14:06:15 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GAA2WsRh; dmarc=none; spf=pass (imf29.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697724376; 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=kwR/Ke20NAcUS81IhTmctZxyEuc+6gPtGwlWuG8Kglw=; b=5a2eDOk7JdmA7poqRu+0ETRyUxA0bkOMPUbPovCdn3QxuZgA2MRAwjMoH7uaS0wINFP+34 IStygt2GuSYr0W1zLHHKfIXHdUKZaMiNIELsAjV+UbbPkaQeVHLlncDkMcZptya7jQeL5M bTjhtj1ISMUdtN9Kvwv1UiGrQ6d3sTc= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GAA2WsRh; dmarc=none; spf=pass (imf29.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697724376; a=rsa-sha256; cv=none; b=XxJANJS302/bmEPRso7gHM0uAKZcX8bCdf0rwI4Fe/jVrUaYGUONNFzxh4o9EUcKhAkl7R C0uqpT15ay5dDAeupmcTpjRGnpGCWbySn6caUOuyNP20zcxvXAz8jKez0rU+qQxT+e+RC3 xjZ7qPce1OIAtSWUF1zG7UzhqgGTzIs= Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-32d9552d765so6219070f8f.2 for ; Thu, 19 Oct 2023 07:06:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1697724375; x=1698329175; darn=kvack.org; 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=kwR/Ke20NAcUS81IhTmctZxyEuc+6gPtGwlWuG8Kglw=; b=GAA2WsRhRtHNqYAdDDo1b0AYiLKc2PTj4WTfZ4Ji9p5dOi5QLVLr2gexfYve5jNoid bXGjXvXvph5MmQmUJJahFWDZgTQSHduqt7g+CcpqJEeRE43JWII4DmbyxwCYg92Qrav1 OBfOZgumACjtTtObLg0BI2dJWjgMNNy1Xhpb4LubGzuianxleIwo2+43q+wQddY9tiSU FvIY9nND/Hoxf+OiOOqYvkUlxh5en0CYyLF9Y/qDLE+zNmNaJp5vvaFhf7ST2Qr9PM53 zt1Qg2f2I6LAv+AlnRyTmBXobz/c1BFhmXRHm2A6Yr/ZrgSJay2ICfnyY1XbkepgW/5R cmdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697724375; x=1698329175; 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=kwR/Ke20NAcUS81IhTmctZxyEuc+6gPtGwlWuG8Kglw=; b=D6ezSreQKES/Yadux0/NijeiLdKSGymdOgJ9TmwmZtXRvigvUS5FTBGyXOpUZVKNJY WBDucyhGq+wquK/fYNFhbnDUSBMI8ZGT7CPa9ChQECzygj7LLf0PMz51qbU3cG2Q5FFa JRWwEK83HmYWQdHeEmnM8oGidWcJk0Bw5Ka5lXgh3S4Ge9IiO5+yi1RKCe4IS7OYNCOe CUOWrtM7M58FdxDMItqcAdZ4YBuuro+UAql0nO943k3FNziwx3MJ2pDFnSjMudQGSBg6 WymQOV3qrm13RrAtRiEqL8E98Q+z2jpKC5cU4CuS8vypLCKeYBXa70GYGgnou22E9Cnx 1eOA== X-Gm-Message-State: AOJu0YwFgZfe1/04uToEXOLXOSkQkJhXRtMvOpGVifYDLnO3DruQKp70 mGM0yQNqsj5Anhz7FlXaKeniXkV4gPdU5ddLXFc= X-Google-Smtp-Source: AGHT+IEj56EYfD3VGGct58Hoxz0IsXjkWxjSdBpSEK26F7D/MeS2KrVfdBwf1rOZcSIiBfUvUcP6qg== X-Received: by 2002:a5d:6e0b:0:b0:32d:8505:b9d7 with SMTP id h11-20020a5d6e0b000000b0032d8505b9d7mr1610093wrz.43.1697724374647; Thu, 19 Oct 2023 07:06:14 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id u11-20020a5d514b000000b0032db4825495sm4572197wrt.22.2023.10.19.07.06.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 07:06:14 -0700 (PDT) From: Alexandre Ghiti To: Will Deacon , "Aneesh Kumar K . V" , Andrew Morton , Nick Piggin , Peter Zijlstra , Mayuresh Chitale , Vincent Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Samuel Holland , Lad Prabhakar Cc: Alexandre Ghiti , Andrew Jones , Lad Prabhakar Subject: [PATCH v5 4/4] riscv: Improve flush_tlb_kernel_range() Date: Thu, 19 Oct 2023 16:01:51 +0200 Message-Id: <20231019140151.21629-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231019140151.21629-1-alexghiti@rivosinc.com> References: <20231019140151.21629-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 05F8A12000D X-Stat-Signature: io59x68xgcrq5334i3cys6hh8i1orf8w X-HE-Tag: 1697724375-578185 X-HE-Meta: U2FsdGVkX189DFhF4kipS2oc7mS88LwgfzWC01FeQKGLrjjZlivx/XikYpcSr7fMuv1jNzLKrZHkrs2YLlZ5arEGreK57GNYsUIw9mmMeNNUsy1ypggGMtY3M4GAUJKvZu+QdsAW/FzfmlBN/k8AJ1TlDD3fa+XJjyY2Y/oC9agj+B3baj4/GmPAKAwl0OrWpiOyG6MmeFCNtlEZqwAtZqTk+iNBiW/vb86PIQUFzornK78/oBE5C2JjpAVHkq47TsdU/mWFWEsprcs73hRYOZ5+QQZSZPc6QCMu5437Y9QzmH2pygCGXsSnl5f16KE3qzPWE7h27Koa/omzmCk4KfCFM5fagNKRfeX8zmvETKUS/WCes24nPUuUlDayspA1HjzD6Xl+jaH2iKWomglVuGiaxwdlNrS2Gwm+eD1Ld2iL2Fnnhn3NQI03qA36/lTlGDVDNuXV7xbvxNRHTa4JYnRERvs6yL01LCCrGTeHu3dCH0ezQ9oUY0hpiyK504odTVeSDPHOpoih36k08quIw6+Wut3DR5d9AGex4ZgQQQ2IpIpD5O5Y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: 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: This function used to simply flush the whole tlb of all harts, be more subtile and try to only flush the range. The problem is that we can only use PAGE_SIZE as stride since we don't know the size of the underlying mapping and then this function will be improved only if the size of the region to flush is < threshold * PAGE_SIZE. Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones Tested-by: Lad Prabhakar # On RZ/Five SMARC Reviewed-by: Samuel Holland Tested-by: Samuel Holland --- arch/riscv/include/asm/tlbflush.h | 11 ++++++----- arch/riscv/mm/tlbflush.c | 33 ++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h index 170a49c531c6..8f3418c5f172 100644 --- a/arch/riscv/include/asm/tlbflush.h +++ b/arch/riscv/include/asm/tlbflush.h @@ -40,6 +40,7 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr); void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end); +void flush_tlb_kernel_range(unsigned long start, unsigned long end); #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, @@ -56,15 +57,15 @@ static inline void flush_tlb_range(struct vm_area_struct *vma, local_flush_tlb_all(); } -#define flush_tlb_mm(mm) flush_tlb_all() -#define flush_tlb_mm_range(mm, start, end, page_size) flush_tlb_all() -#endif /* !CONFIG_SMP || !CONFIG_MMU */ - /* Flush a range of kernel pages */ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end) { - flush_tlb_all(); + local_flush_tlb_all(); } +#define flush_tlb_mm(mm) flush_tlb_all() +#define flush_tlb_mm_range(mm, start, end, page_size) flush_tlb_all() +#endif /* !CONFIG_SMP || !CONFIG_MMU */ + #endif /* _ASM_RISCV_TLBFLUSH_H */ diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index c27ba720e35f..7e182f2bc0ab 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -97,19 +97,27 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, unsigned long size, unsigned long stride) { struct flush_tlb_range_data ftd; - struct cpumask *cmask = mm_cpumask(mm); + struct cpumask *cmask, full_cmask; unsigned long asid = FLUSH_TLB_NO_ASID; - unsigned int cpuid; bool broadcast; - if (cpumask_empty(cmask)) - return; + if (mm) { + unsigned int cpuid; + + cmask = mm_cpumask(mm); + if (cpumask_empty(cmask)) + return; - cpuid = get_cpu(); - /* check if the tlbflush needs to be sent to other CPUs */ - broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids; + cpuid = get_cpu(); + /* check if the tlbflush needs to be sent to other CPUs */ + broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids; + } else { + cpumask_setall(&full_cmask); + cmask = &full_cmask; + broadcast = true; + } - if (static_branch_unlikely(&use_asid_allocator)) + if (static_branch_unlikely(&use_asid_allocator) && mm) asid = atomic_long_read(&mm->context.id) & asid_mask; if (broadcast) { @@ -128,7 +136,8 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, local_flush_tlb_range_asid(start, size, stride, asid); } - put_cpu(); + if (mm) + put_cpu(); } void flush_tlb_mm(struct mm_struct *mm) @@ -181,6 +190,12 @@ void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, __flush_tlb_range(vma->vm_mm, start, end - start, stride_size); } + +void flush_tlb_kernel_range(unsigned long start, unsigned long end) +{ + __flush_tlb_range(NULL, start, end - start, PAGE_SIZE); +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)