From patchwork Wed Jan 8 15:49:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13931262 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3E231E77199 for ; Wed, 8 Jan 2025 15:49:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=WHeM64FSn96yqAsIkloc1buDAI5KAi2vvuwo+C0d77s=; b=h5zl+9Wxs5AH3P o0rOeU2cRP50rZrEtGW8QNpGtbxMEVt6qwUZpp5WwNFO4jLMy0ALjeYWYeUyqussHntbQ1d6uBQIZ /C26vRwvSN40OSDkeXNIhLK6+lDjhYYpU7VlRWJyD5qvvk344hXsQIY6uvVjGbH99P2XJhALG81wi otPBYoCMvSB8LTS1bk8pat0VYcIzve8G0bO38naQoezJfmVbePQq38/pT93vjSa5zLmnLhJ4AFNRL aN5SR1nLR6WQHO9ZGwVDzUaax2PP88nDSgxocnKtIZTpLsmRP8gvecdsR5yry1LSmlS+05OBePDwq xU7uP+gXUbTzCKFMU4bA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVYJ3-000000092Jc-3MYG; Wed, 08 Jan 2025 15:49:17 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVYJ0-000000092Iq-0tXM for linux-riscv@lists.infradead.org; Wed, 08 Jan 2025 15:49:15 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-4361c705434so122895165e9.3 for ; Wed, 08 Jan 2025 07:49:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736351351; x=1736956151; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=vgsjKbIw/Qtp7jQ5AD1ITXyk1buirQCSu+JWGBt/Rxw=; b=ukeLmJo3UqPchbEeZI5Mi3v4h+g7tnYIvcnOgAoNle5zvxIZcRK6USb3EQZSrsJA4a 6XsaE55NYMnfWUrFZcFIRJduXwCRzE+tRGeITiZyfe5Y0eCxDaRli/mW27runRhcDyrS Z1BCK9w01UWYBg3FTP6v1zFRe4973lfPOn9RR31EHOZ+u2IDzB4AqM/wt8H7dnYV0IK7 5M6Zh5OPrR3i1nBoBI675gEkfW624RJoPNUtGzwvLnUotqjOCBVM+cue504UNZ8z0uDS MUQNVzSMxWMBylJGdy6FZ8Ld2ygwImWSXxXenFGoV8DsB5n+drRsyIZ5BvNAlyTx4sN8 Phkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736351351; x=1736956151; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vgsjKbIw/Qtp7jQ5AD1ITXyk1buirQCSu+JWGBt/Rxw=; b=YXHJawvBLMuPicwhVmqJzvbntX2m/ZMYvjJE20fmRIlSPydvmL7Fw2DuMewJEZVgH+ a8H45vvpVOmJHtqlZnpd0JGkxbyl0Jr3HKjlI8uMMEu2LQm5Db32qFppBwdE+EEjdT2g mr0Pixb3u7HF7qtPc1Dnye8kwKFFzp0H67zp78sq4+J0XO+w9yu8HN4wgmxffot2m7Ke uEi1aRZYaBRMyfisS0kEGh6xJ8ni06sJtNH2pqH5Ldo4lQb4h+m9fJ7a7dssoGdLm4oP HueL5OUbvKdqcAPdky6GKgbnWPYEeyBEY9S/V1XmJeQNy6UBd33ots+12+5glt4t+jAY 8wWQ== X-Forwarded-Encrypted: i=1; AJvYcCWc1jFIY9YJEq2tTJrQ/VRlmQkz2S97dpJ0aroJoWMgsvzjCha5DejQkkqqxP7+QGMqpNcnvWcSnCIBqg==@lists.infradead.org X-Gm-Message-State: AOJu0Yw6KJRAO/UoCXEYfadhlc7/9d9Lv7OccAgAQlHP2AZG0DGSnRxV TxTrz+FgX/cZCmpkgDE9ozBQQiZ17l0ClyHBIqssR6uE0arZCgVNvZsEOxEnyok= X-Gm-Gg: ASbGncsVhGv9AeWmJ7kg+5PNgAn775pkXZcRx3JbcP0bEkmjSJtQXn03eOKeG8OaS8W 7Zaq9M07WAF48cirTvmZ0bc/OrEcsOe2GCVUUnpT6lYQawDKagZTPQ8dYy4AQEWmqMdiztcd5ZN s41pgt/S/k0zxqY8B3G2O08oDiGHQxDk5jn1m6STgpcVBORsOYvv/rOiV2DxZ4a4gAu9lqOtH0F c1rHagp4bvr7GuF0xVx5A+EEb+yh651xZ8AaYc2D+/hPKtdiSwTHhSrfkpR4JL0kdPOVQKVpf66 yomDINP9mSvQktg0OF2/M1wt X-Google-Smtp-Source: AGHT+IGOypGQLh3BUcPGq2oqFr1ZK/Glm85bEtHFzrrEmZJ9NzY1G2pqFasac+LFgi7mTjPivTeA+g== X-Received: by 2002:a05:6000:18ab:b0:385:f349:ffe7 with SMTP id ffacd0b85a97d-38a872f6d5fmr2989417f8f.2.1736351350106; Wed, 08 Jan 2025 07:49:10 -0800 (PST) Received: from alex-rivos.home (lfbn-lyo-1-472-36.w2-7.abo.wanadoo.fr. [2.7.62.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8475cesm52898915f8f.57.2025.01.08.07.49.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2025 07:49:09 -0800 (PST) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Samuel Holland , Andrew Jones , Albert Ou , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti , =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PATCH v3] riscv: Call secondary mmu notifier when flushing the tlb Date: Wed, 8 Jan 2025 16:49:07 +0100 Message-Id: <20250108154907.132863-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250108_074914_517220_57076F22 X-CRM114-Status: GOOD ( 11.81 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This is required to allow the IOMMU driver to correctly flush its own TLB. Signed-off-by: Alexandre Ghiti Reviewed-by: Clément Léger --- Changes in v3: - Rebase on top of 6.13-rc3 Changes in v2: - Rebase on top of 6.9-rc1 arch/riscv/mm/tlbflush.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 9b6e86ce3867..f5f8bc6d1c61 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -78,10 +79,19 @@ static void __ipi_flush_tlb_range_asid(void *info) local_flush_tlb_range_asid(d->start, d->size, d->stride, d->asid); } -static void __flush_tlb_range(const struct cpumask *cmask, unsigned long asid, +static inline unsigned long get_mm_asid(struct mm_struct *mm) +{ + return mm ? cntx2asid(atomic_long_read(&mm->context.id)) : FLUSH_TLB_NO_ASID; +} + +static void __flush_tlb_range(struct mm_struct *mm, + const struct cpumask *cmask, unsigned long start, unsigned long size, unsigned long stride) { + struct flush_tlb_range_data ftd; + unsigned long asid = get_mm_asid(mm); + bool broadcast; unsigned int cpu; if (cpumask_empty(cmask)) @@ -105,30 +115,26 @@ static void __flush_tlb_range(const struct cpumask *cmask, unsigned long asid, } put_cpu(); -} -static inline unsigned long get_mm_asid(struct mm_struct *mm) -{ - return cntx2asid(atomic_long_read(&mm->context.id)); + if (mm) + mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, start + size); } void flush_tlb_mm(struct mm_struct *mm) { - __flush_tlb_range(mm_cpumask(mm), get_mm_asid(mm), - 0, FLUSH_TLB_MAX_SIZE, PAGE_SIZE); + __flush_tlb_range(mm, mm_cpumask(mm), 0, FLUSH_TLB_MAX_SIZE, 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_cpumask(mm), get_mm_asid(mm), - start, end - start, page_size); + __flush_tlb_range(mm, mm_cpumask(mm), start, end - start, page_size); } void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr) { - __flush_tlb_range(mm_cpumask(vma->vm_mm), get_mm_asid(vma->vm_mm), + __flush_tlb_range(vma->vm_mm, mm_cpumask(vma->vm_mm), addr, PAGE_SIZE, PAGE_SIZE); } @@ -161,13 +167,13 @@ void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, } } - __flush_tlb_range(mm_cpumask(vma->vm_mm), get_mm_asid(vma->vm_mm), + __flush_tlb_range(vma->vm_mm, mm_cpumask(vma->vm_mm), start, end - start, stride_size); } void flush_tlb_kernel_range(unsigned long start, unsigned long end) { - __flush_tlb_range(cpu_online_mask, FLUSH_TLB_NO_ASID, + __flush_tlb_range(NULL, cpu_online_mask, start, end - start, PAGE_SIZE); } @@ -175,7 +181,7 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end) void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) { - __flush_tlb_range(mm_cpumask(vma->vm_mm), get_mm_asid(vma->vm_mm), + __flush_tlb_range(vma->vm_mm, mm_cpumask(vma->vm_mm), start, end - start, PMD_SIZE); } #endif @@ -189,7 +195,10 @@ void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, struct mm_struct *mm, unsigned long uaddr) { + unsigned long start = uaddr & PAGE_MASK; + cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, start + PAGE_SIZE); } void arch_flush_tlb_batched_pending(struct mm_struct *mm) @@ -199,7 +208,7 @@ void arch_flush_tlb_batched_pending(struct mm_struct *mm) void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) { - __flush_tlb_range(&batch->cpumask, FLUSH_TLB_NO_ASID, 0, - FLUSH_TLB_MAX_SIZE, PAGE_SIZE); + __flush_tlb_range(NULL, &batch->cpumask, + 0, FLUSH_TLB_MAX_SIZE, PAGE_SIZE); cpumask_clear(&batch->cpumask); }