From patchwork Mon Jan 13 14:24:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13937444 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 C7A21C02180 for ; Mon, 13 Jan 2025 14:25:59 +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=qWNntZSzv9bYUrHnatU64Ms09H/WnRoIoDo/o3ad/Gc=; b=Eeqw7Yx2lWomV5 DHOzbnzDf89Gm/4NP76Dmu+2SjTAJuOjkM9g/ujW3fsMyrKyTCCoOJWoGyixjVF99DzZdiejxyW2Y HebAkYtJv8VPXFP1jvjL5Vgaczkj4TozMaGVZMbH/nlwAKzjoJvcAJrMWbFbSE41cgt+pz7qbKgce fFDJcw6Kqc0I0UxWcriBS0ROam6gYC3x6Qk7kTdZ16AAWm506Az+BsHYX8Q+syffSsjwVqr30z12R 98+HSiv+lfdvrS5CmI/4l0SyhIsWs91fvfxdvUzfHmWGP8VEV7dHU4y5ZZKh6+7cBopOnJ2fIpg/2 V20bHXs5y39e2U/x1LYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tXLO5-00000005Oi0-3W9V; Mon, 13 Jan 2025 14:25:53 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tXLMm-00000005OXz-2a9f for linux-riscv@lists.infradead.org; Mon, 13 Jan 2025 14:24:34 +0000 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-43622267b2eso46397205e9.0 for ; Mon, 13 Jan 2025 06:24:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736778270; x=1737383070; 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=U6Zgp09z8lzuttgM51PWASr/v2Wid6qJyqxiEWy6j0I=; b=lTuzZOAKLCnWEDqz46zkTswm6pR0Axn8jQTzfjfeZ5IN5ocorYMnDYyYf3xSnPeINy KC2LVbvc7JfecQkcMELWcPQhtmQ9Xs8WACtEbVR8uJh4TeIOS2BtY2/LAgmc7jp3RvWA tNWj4hT2mmI7s2N32Xz1YJs2JMOXR/CiGeTZSS+7DXd3sRRvMJL+9DYIH2+IQQ6cnj7I wE9FY09Rw2ODJcl/wAomk/Y3G09QBeGeVhUoRC+NdqO6Ygu01wD7W6MexWMm1+8lCAYp 5+xAty66eKb7GqZNT6JAbwbdLxqOMmc7ZYmGg1g8ubqHaagwp0ZSFIUZ98CAsOtK5+lR ZJPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736778270; x=1737383070; 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=U6Zgp09z8lzuttgM51PWASr/v2Wid6qJyqxiEWy6j0I=; b=Tbc+WjdADAODwYZvBlBBBHamQ3nnstOKdGjiEQToQRMWwZ0SWWJwJGLuWpTI4Tx4Dg XArSZ+0wmIeyzWeFmgAbqahWNQERyYS2sXY6RnChuoklkDzu/cBTH/GaUcIh8P+SSKkl SwolrTjw03Rtv6e+cxAINH2YbIgJkq1bMFgSwl/I4Yoq97L406yTm78ZDa1HFrkyeX9W xmfleqlqo7kNcrJUafIHooTW/iRQqgryBnNWy+UrCxS6eag/ihodzKfC2X+PpDZn/q1F h8yin8j9ABuKJdruOaUkKGPeedodFOpTvEswhkvd3ZJXrmPXl3Sut2913u4qz4txP3Y/ mRBg== X-Forwarded-Encrypted: i=1; AJvYcCW+Ep/5wNn+7KBAHhPYxnCAp+sFJtsgXZul6YywM4IZsI5mLdDpJ6Ey+h2vv96mXVhcolkKqW1D65YpfQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yzh3BsTvXhp1dyqd1bB8T+3dg+yfL5OL5NlZiuv26KusXqtwjPk Jl0vO0cUdnKHr5O9Ic1/vFKmdqF104pxw0eg/c6k918pejOUGME0TuynnVZPCXw= X-Gm-Gg: ASbGncs00zZa7rNXxpYRsEJ1o0pIQf5mc/rkK1GkcEJT59uALa28Lbk0pTOtqDEEDlF twYcegwXpBfmafvXCFfkrClFr0RlxuJaWVnLDxrTuSaaEG8LZoAHnb37cU6iB8Q3nunb6K9WYHP Y8JnuVUty2f8JX2jTMG/56oyfDblyFi8kS5ccenn1E3K9uI1+4agQb7FVOeVwgJzAvuA3v4UMXr U5yDlUvGGEh8pAT4+T7XBQYLqds1LoezZ/EgyGPUybxJSemHBjsRFhLvdjvXYZUe7aP9bHKXRKj X-Google-Smtp-Source: AGHT+IGMa2ldUEkMiuq4eErSnbpALm4uDp90FJrdLywqcG2hGCMQ9lmvAIoLp9JhfgfEIFI4LBCwjA== X-Received: by 2002:a05:600c:4586:b0:434:edcf:7461 with SMTP id 5b1f17b1804b1-436e26f135bmr207312645e9.30.1736778270319; Mon, 13 Jan 2025 06:24:30 -0800 (PST) Received: from localhost.localdomain ([2001:861:3382:ef90:1d6c:a0ef:bd2f:4ccd]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-436dd11ddfdsm133376335e9.1.2025.01.13.06.24.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 06:24:29 -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 v4] riscv: Call secondary mmu notifier when flushing the tlb Date: Mon, 13 Jan 2025 15:24:24 +0100 Message-Id: <20250113142424.30487-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-20250113_062432_919043_D1D5DEA4 X-CRM114-Status: GOOD ( 12.49 ) 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 Reviewed-by: Samuel Holland --- Changes in v4: - Remove unused variables as reported by kernel test robot 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 | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 9b6e86ce3867..bb77607c87aa 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,17 @@ 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) { + unsigned long asid = get_mm_asid(mm); unsigned int cpu; if (cpumask_empty(cmask)) @@ -105,30 +113,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 +165,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 +179,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 +193,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 +206,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); }