From nobody Mon Dec 1 22:36:54 2025 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40744332EA5 for ; Thu, 27 Nov 2025 14:11:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764252718; cv=none; b=q9Xwsdcv6G+3wlJhkzQTk+J1ywG/eYei67P/Yt0KGWFsbRctpmg4QtoyrEkLA8XORSBkqLws/RWFSnpQTg/cLtYEk0040KY9b9HdS6LuMLsEX+tU841WG5fdhb3e27j8MeKULWcidHmD+XD1aR8whgOcabMMaylpgcw2HirY3zc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764252718; c=relaxed/simple; bh=W4Vm9ClsWQRE6Rr2tnB5g8ZJh9ErQlE4jvYs/iPj8kA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xgh2ckn8+p0SI6rf0qZJPOQSg56d3IxDRrJhluv1yTk3bc4eWSPjiHSQAx/+OXt0iH3FpbEQuxNx4rILCpFDNVG1Gi/Nd2N5+5vsB1OEalUKtwbW9CJet3mWzmAggGvdxbTNe4LncdsGS4soL6BtctMFh2CLfqC+PW64M+Qi8bI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=dftJo1pa; arc=none smtp.client-ip=209.85.216.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="dftJo1pa" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-34372216275so998363a91.2 for ; Thu, 27 Nov 2025 06:11:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1764252716; x=1764857516; darn=vger.kernel.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=bT1DoFK44/mxXPszG1k06ulWEZ0r0zvum0ccrRfuy/8=; b=dftJo1pa6BiywSxJA6+l3AoDntq2MAS+UbhQUQvxV7rJmbde1SWPRpy4QFWCWQApR9 TM/K+tZh9TAID6sNlETz/4w4ZoSUH+F0t28tqnDnQpxFqiZBqcafODa6QoT1T2WhDklZ HID6Tt4MOClMe67MGez/BLd3xPbVlPLS23jV6KVHV00WBsrNtA/3ZtVHjmZZ3W3ItWA4 rQmXr0OiH1hzhYDK9b0mdrd7NfnVvQi8/y6KX6GrDLQ8WKWa4Sj+1eYzJ1nZXCoEbd+k WyGSaUoaKAT5goOW+nyNvDfKGmS3qjRG4M2X1XRqwOW3tZq42H6LffJhHA6UOGYZAeb0 4WWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764252716; x=1764857516; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=bT1DoFK44/mxXPszG1k06ulWEZ0r0zvum0ccrRfuy/8=; b=EIxUTenUePo/PmcRijHoWlPa706nkRBlsBEYxooNv2ZsnNPxbnA6NsSc/74ixTg2qm FoyBTiscYbRxeZD7NuGVadLfofthkaJEhx8whddunPCoi6pDfv4U5NormlQkjIefeRby YR0IBesgiAOYII52GXg5nLw1X3JC0N5erZ196hxtbxaa0/dzxhOY0kvfBFbh0MNLirGG ClJZV3qZcHIRKY3GTgaNjgu2D5GQiB+br/ObBTHH2iREvNjh8u5nID8NYhg4yWlqcYRW j4zRsm6lQ3e52KYc1PVYd3fK/++9+jnvmASNZA9/u+Lo+TmqL5iWfn38oIpKlqOEGAah TsDQ== X-Forwarded-Encrypted: i=1; AJvYcCU1gSa9F8JrUnTRpUDW0qlbPGwDxVXVUoMhvVDaKjgzGag++VOsCtVuarAP2RO+MZCLXKSkSTT+vlMrTko=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7BKM59rdT9XHSs9wytr07YwJCnMoE/3qF6RR7/2UfNlLLwQ4a CoiczL4fgHy4HsiKq7pRVwJQ5n8FPlseNwh0u1+4FkrflcA0uixLIk1Grwb3LvPn29o= X-Gm-Gg: ASbGnct/mZpOg2nfOMGJeLfnfwYLAePZANkk5SSD27Xj35A9i4k+AcQXw27lYS0B3iD kTwcPmsfI2gXX7/8JS9ewc3F8Bbce1MY5K4lwg6t7ZirmT3sTFf+ooayiO1Mb8fS3kyNPEd96WG i/ZVnfOxJ2JrVRn51dk98l+JAN8KrxcnGBhtkdarcgJMsgrgIJIvf8oEh0+ppn220xEyTrruDPJ NX2cds/RD9hGFma/UpKhiAamIerBdptSz/Zn/RpA7EWuQYGZdft6qvi5wIBxkCEmSC/nrRgQ3Mz 90J515/QoHrTWiWtfUwhR/DGB56eOFV7qkpqI9JaIeAacp8U2AbOiFZw7XjDWiWcEc0OJcjFwb3 Chd8mgN2fP7k4j8B2SPy41DjrrEV7FHxDC97LfejSXfnymbiWHBSX9egQpw6xFZMKfZi0jhIsRc d9WkbcEXET2CMa0aQGHTfmE+iywanjqMQ1fP5HSSG6A2KD+jP9rSDHdnKjFOprhIKth343j0qWh AWWbxKSVj41 X-Google-Smtp-Source: AGHT+IGHWS9mtIo18ykGEbjce9y2+tGQtCuaDp2x5Akdhl4bvcW7U6KnSKpb0r7Pzb23BFyTlkH5Aw== X-Received: by 2002:a17:90b:2f4b:b0:341:8bda:d0ae with SMTP id 98e67ed59e1d1-34733f2de45mr22829266a91.20.1764252716195; Thu, 27 Nov 2025 06:11:56 -0800 (PST) Received: from J9GPGXL7NT.bytedance.net ([61.213.176.58]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3477b7341d2sm2030249a91.11.2025.11.27.06.11.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 27 Nov 2025 06:11:55 -0800 (PST) From: Xu Lu To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, kees@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, akpm@linux-foundation.org, david@redhat.com, apatel@ventanamicro.com, guoren@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Xu Lu Subject: [RFC PATCH v2 3/9] riscv: mm: Grab mm_count to avoid mm getting released Date: Thu, 27 Nov 2025 22:11:11 +0800 Message-ID: <20251127141117.87420-4-luxu.kernel@bytedance.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251127141117.87420-1-luxu.kernel@bytedance.com> References: <20251127141117.87420-1-luxu.kernel@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We maintain an array of mm_structs whose ASIDs are active on the current CPU. To avoid these mm_structs getting released, we grab their mm_count before loaded them into the array. And drop their mm_count via tasklet when they are evicted out of the array. Signed-off-by: Xu Lu --- arch/riscv/include/asm/mmu.h | 4 +++ arch/riscv/mm/tlbflush.c | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/arch/riscv/include/asm/mmu.h b/arch/riscv/include/asm/mmu.h index cf8e6eac77d52..913fa535b3d19 100644 --- a/arch/riscv/include/asm/mmu.h +++ b/arch/riscv/include/asm/mmu.h @@ -30,6 +30,10 @@ typedef struct { #ifdef CONFIG_RISCV_ISA_SUPM u8 pmlen; #endif +#ifdef CONFIG_RISCV_LAZY_TLB_FLUSH + atomic_t lazy_tlb_cnt; + void *next; +#endif } mm_context_t; =20 /* Lock the pointer masking mode because this mm is multithreaded */ diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 0b1c21c7aafb8..4b2ce06cbe6bd 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -104,12 +104,57 @@ struct flush_tlb_range_data { }; =20 #ifdef CONFIG_RISCV_LAZY_TLB_FLUSH + DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_info, tlbinfo) =3D { .rwlock =3D __RW_LOCK_UNLOCKED(tlbinfo.rwlock), .active_mm =3D NULL, .next_gen =3D 1, .contexts =3D { { NULL, 0, }, }, }; + +static DEFINE_PER_CPU(mm_context_t *, mmdrop_victims); + +static void mmdrop_lazy_mms(struct tasklet_struct *tasklet) +{ + mm_context_t *victim =3D xchg_relaxed(this_cpu_ptr(&mmdrop_victims), NULL= ); + struct mm_struct *mm =3D NULL; + + while (victim) { + mm =3D container_of(victim, struct mm_struct, context); + while (atomic_dec_return_relaxed(&victim->lazy_tlb_cnt) !=3D 0) + mmdrop_lazy_tlb(mm); + victim =3D victim->next; + } +} + +static DEFINE_PER_CPU(struct tasklet_struct, mmdrop_tasklets) =3D { + .count =3D ATOMIC_INIT(0), + .callback =3D mmdrop_lazy_mms, + .use_callback =3D true, +}; + +static inline void mmgrab_lazy_mm(struct mm_struct *mm) +{ + mmgrab_lazy_tlb(mm); + atomic_inc(&mm->context.lazy_tlb_cnt); +} + +static inline void mmdrop_lazy_mm(struct mm_struct *mm) +{ + mm_context_t **head, *list, *context =3D &mm->context; + + if (atomic_inc_return_relaxed(&context->lazy_tlb_cnt) =3D=3D 1) { + head =3D this_cpu_ptr(&mmdrop_victims); + + do { + list =3D *head; + context->next =3D list; + } while (cmpxchg_relaxed(head, list, context) !=3D list); + + tasklet_schedule(this_cpu_ptr(&mmdrop_tasklets)); + } +} + #endif /* CONFIG_RISCV_LAZY_TLB_FLUSH */ =20 static void __ipi_flush_tlb_range_asid(void *info) @@ -292,6 +337,7 @@ void local_load_tlb_mm(struct mm_struct *mm) info->active_mm =3D mm; =20 if (contexts[pos].mm !=3D mm) { + mmgrab_lazy_mm(mm); victim =3D contexts[pos].mm; contexts[pos].mm =3D mm; } @@ -302,6 +348,7 @@ void local_load_tlb_mm(struct mm_struct *mm) if (victim) { cpumask_clear_cpu(raw_smp_processor_id(), mm_cpumask(victim)); local_flush_tlb_all_asid(get_mm_asid(victim)); + mmdrop_lazy_mm(victim); } } =20 --=20 2.20.1