From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) (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 D557C7DA82 for ; Mon, 20 Jan 2025 18:57:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737399437; cv=none; b=SExZ/+gFOINNRejlRZfcKnA99j/c4RhZaHXpNHmsrnEXcSmC6Dl1c/XaIxU/3dTsgRBNVB9vFUTD02SLkgIDNF19jWWDu6uL6iKLPly3r9jP6iyJO05oZUSirUZ4cN8+3sotZsaz2IP1TxEnQ2g862ecdo330CMzclPyoC7+4i8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737399437; c=relaxed/simple; bh=E71MbHwfnt82zmg933nK2TRf3AmNthVl3Ld8OKTOVU8=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=rxqmNDhgjMXlEiQ1HnoeBGAuBYzd56slZvTKmoSrU/u/s0ulQInhrsYilGnoyCqdNllbdwpjHjmzghYqMMpDkKARS1Y9hlpEk6HPWgz7TsGgJLRQmaCYh5vXF0Mmkgrpm7uBHOH060ue/qh/jbSeqGS5O9ChEBt96d2BZS4bTiQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iQEumRFl; arc=none smtp.client-ip=209.85.208.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iQEumRFl" Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-5d932eac638so9406376a12.1 for ; Mon, 20 Jan 2025 10:57:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737399432; x=1738004232; darn=vger.kernel.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fVlS0bSdoUsO3X5t3gNoXRny3XQ7hWlr3RcXjkXsCTc=; b=iQEumRFl4Xoq3U3dxxQ7OTL5ZA0ALpsWM0zLrBv543WL5NpQu4gh70sawIVWjCLo+0 tRSWUAbjjFjjT+AXcNtU/1FSpMDnkHbjB/doAqLhcX7xFFuJGksxMJSVK2XCGxVNAEKK 3WRA7hdqE3OP/Sm2YatfqDV6egXRGfPYIro9ZBFTe5eIhXuABVwY4hos497d4xYqlo/y X3blc7OIUEmk26xrnk6/8FNcr3Lvw9P2+Ik+in4BWv1xUsU7DsAKGBFk1OzLk2duHE13 260R7wtEmeB1s7M/F2XgnJ7D4xzTjhz3XsJBmZWuKt5Hbg9i9AQnLFNgix75QqIPUOpm 2JaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737399432; x=1738004232; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fVlS0bSdoUsO3X5t3gNoXRny3XQ7hWlr3RcXjkXsCTc=; b=MG3ZLjxYjFrHer6ZxszFP1ZdNKVmPZ15fOGZEwhv1SRht3rxdYcFuDbGq/kvZzJiV+ iQBSkAa4J5kpeXjLPSEw+KHG2S/vC1+j29eebm6pkUJgnT4MJF7wHvtCm1W/0FinKp1q PZKUPfix8AUh2WUbEklLx7lNovO/ue7l+N6QJzQB3CVK4wPm0Sndv2d8TKCYH4VwVwm4 lX/33l7jNRcLmutnLJL6bbLjd0YfvIie/KNwkt9yWXOiUAQdZ3aMW0J9UNHVZtROiuR7 DZhf6OPhonhz0SgWzYgNBN3GJcGTekLGgv9mhZACyhAMs4AeKppVflce/YyD+Jy1h0hz NVpw== X-Forwarded-Encrypted: i=1; AJvYcCVMqUGzDgMbCB419KhbWnQiyeAfakAw/yIMJe+cxUD43ioHY6X0psN8eLBH5dFRIoyysUpSZbzzSbWXUsA=@vger.kernel.org X-Gm-Message-State: AOJu0Yy3lOfZFI5m5ZGqdsMUSY69dV6F9LbOM7SmPUtCwW6wKA25uB8T KrnJZSY8DXNNWxcpEh5EFIGm/9LZ3UC/tsxv/29RhTyfwH3xp3qY X-Gm-Gg: ASbGnctOzI3NVYCNVR5dl22ZHiJr1aGCxh2z7e5cOscXXtzTSxRJScgZ9Xbz2527V3G u0Xrup9V8DzLx2R8ZU4Jzu0JquCrDk+JjYPM4dj2X4yWh1XjOlibblhFaJC7m2h6nvtCiOhSPb1 /Jxz7kwV8LXzFMdNfBhqJJJvPQj3PXvAB1rXMhKcCr0VEOABK7N2/aqbKL9WTeX26PMwcuxajJg /ZUKjitBLeIekffZvkRN5H3hkGWAqGehxn3UWeBcSN9FU3oRrDwLpjsRLnQ1z8fWbDaXzn/HnJr hvHmBQycoA== X-Google-Smtp-Source: AGHT+IG6uUmWM5mD1QsNYKOmV++UK2ElHoI3ydKUgGpw8xfMOpbTfz1aUz0ndRST7pgcRx7OA0ke9w== X-Received: by 2002:a05:6402:13cb:b0:5d3:d8bb:3c5c with SMTP id 4fb4d7f45d1cf-5db7d2f0e98mr13603922a12.12.1737399431751; Mon, 20 Jan 2025 10:57:11 -0800 (PST) Received: from smtpclient.apple ([132.68.46.60]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5db73683e1asm5876404a12.39.2025.01.20.10.57.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Jan 2025 10:57:10 -0800 (PST) Content-Type: text/plain; charset=us-ascii Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.300.87.4.3\)) Subject: Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code From: Nadav Amit In-Reply-To: <11fcbbbb545576a1bfa54839cf87a72faf5603ab.camel@surriel.com> Date: Mon, 20 Jan 2025 20:56:57 +0200 Cc: the arch/x86 maintainers , Linux Kernel Mailing List , Borislav Petkov , peterz@infradead.org, Dave Hansen , zhengqi.arch@bytedance.com, thomas.lendacky@amd.com, kernel-team@meta.com, "open list:MEMORY MANAGEMENT" , Andrew Morton , jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com Content-Transfer-Encoding: 7bit Message-Id: <5501C6EC-38C7-472F-B129-F7A5144C3096@gmail.com> References: <20250116023127.1531583-1-riel@surriel.com> <20250116023127.1531583-11-riel@surriel.com> <13bc0c49-09a4-434e-bd35-1ea50be38e25@gmail.com> <4dcf2b4ecaede883e2c7f6af3db58a4f6afaf4ad.camel@surriel.com> <2975f6a4-58ef-4499-b177-4b73ead4c670@gmail.com> <2c93b3f17e658483e3ea03ca3f0a717668fd117d.camel@surriel.com> <11d9f41a-eae3-43a1-90bc-eb5c69020002@gmail.com> <5F51AFA6-EA48-42BF-8E24-10FAAAA7BE4B@gmail.com> <11fcbbbb545576a1bfa54839cf87a72faf5603ab.camel@surriel.com> To: Rik van Riel X-Mailer: Apple Mail (2.3826.300.87.4.3) > On 20 Jan 2025, at 19:56, Rik van Riel wrote: > > How would you keep track of CPUs where the tlbsync > has NOT happened before arch_tlbbatch_flush()? > > That part seems to be missing still. You only keep track if there is a pending tlbsync on *your* CPU. No need to track if other CPUs did not issue tlbsync during arch_tlbbatch_add_pending(). If the process that does the reclamation was migrated, a TLBSYNC is issued during the context switch, before that thread that does the reclamation has any chance of being scheduled. I hope this code changes on top of your would make it clearer: > +void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, > + struct mm_struct *mm, > + unsigned long uaddr) > +{ > + if (static_cpu_has(X86_FEATURE_INVLPGB) && mm_global_asid(mm)) { > + u16 asid = mm_global_asid(mm); > + /* > + * Queue up an asynchronous invalidation. The corresponding > + * TLBSYNC is done in arch_tlbbatch_flush(), and must be done > + * on the same CPU. > + */ #if 0 // remove > + if (!batch->used_invlpgb) { > + batch->used_invlpgb = true; > + migrate_disable(); > + } #endif batch->used_invlpg = true; preempt_disable(); > + invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false); > + /* Do any CPUs supporting INVLPGB need PTI? */ > + if (static_cpu_has(X86_FEATURE_PTI)) > + invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false); this_cpu_write(cpu_tlbstate.pending_tlbsync, true); preempt_enable(); > + > + /* > + * Some CPUs might still be using a local ASID for this > + * process, and require IPIs, while others are using the > + * global ASID. > + * > + * In this corner case we need to do both the broadcast > + * TLB invalidation, and send IPIs. The IPIs will help > + * stragglers transition to the broadcast ASID. > + */ > + if (READ_ONCE(mm->context.asid_transition)) > + goto also_send_ipi; > + } else { > +also_send_ipi: > + inc_mm_tlb_gen(mm); > + cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); > + } > + mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); > +} > + Then in switch_mm_irqs_off(), b if (this_cpu_read(cpu_tlbstate.pending_tlbsync)) tlbsync(); Note that when switch_mm_irqs_off() is called due to context switch from context_switch(), finish_task_switch() has still not took place, so the task cannot be scheduled on other cores.