From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 C463820E00F for ; Tue, 14 Jan 2025 21:13:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736889242; cv=none; b=cfazgtFMDy2XahMph3YGRjA9OErU9ioSE/+Pt8rK4mP18DEkuATjWUhP62sELEdYHUb2AjaYYt1f/A5iGS0VKO17kAQxY5kPr//0TbjhryZJdxQZcKL/ryn9EVNK5pj7YX8kS6AI1svSOt17RSqNSWRScogQinUxax2RC26rfhI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736889242; c=relaxed/simple; bh=HPeOyPTCGGTDHOMXLb+UXWG1X7FxbwpONqdOtUySSrA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aWmxX4B2w+lJf8jyuFc7mbWunT28I4WHCb2t6x5KtWXoJsOJhDcHh0vnaDtaN03JFTkytPqJb3117XpSCHjG70mFN7BFi8kNRRQNiwv+inw0p0lO/XP400yr+pJU9/O7TLrsNm962UYM4sqnIsihrTbO7kMebaJRSlLXXafNN+U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=09aTSmhX; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="09aTSmhX" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ee5668e09bso10664022a91.3 for ; Tue, 14 Jan 2025 13:13:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736889239; x=1737494039; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=d9cRGGic7Te5o12Iwy1M3OB31drPOXRp+PHli2u04Fc=; b=09aTSmhXemgOHNzoANHiHB34UUgsK/+cgu15WCBhn9FBQ1svW5rzJc/7bPDFDo6T8J MrhM0u2PeJLyljjSpV8dr/KZlrgruH5ycBGShfNfBqO/kPxaVemkUIfkPMP1n3Q1djMy xiGDUd9L8Tr53064h3B9AUkSwKjh0d71S4OWptLiLvgX/wV/t4RnMvpuKYLneHdkOsxe wWC2ho4L13zmMxYdlsY/NGCWfvP1av05MPnb0NER5+HiJjekP+vN4Untj8/InQOY6iZE wrQIZJPYT+2nb/ICNiniHZZMUa7l/zWz+UPvg1eMQJmli4XyCCKHeMp5pUpW5dh5hnOn uw1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736889239; x=1737494039; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=d9cRGGic7Te5o12Iwy1M3OB31drPOXRp+PHli2u04Fc=; b=HrxawEh/Mw4ROzg+cmgc69h+2i6IfkgYgn8WGqQKtti3uFgW0o25VxSfAyP+LaHEGE iBzEObutjEr+UodcVGNI2ZK8Kgyzwg1OXV0CA42wQHziTDzDlDocw8Dh2CtMjTFB2E5t KZq7lnQ3iCBkfJsOO+RSnNHIULCNfzV3ZtZ+jv2kqjTBqLWtJJKt5KSLHWwmsb8ioNEf p2pj+CpOfWuM2xiX+MUyCMHWsvS60fkKVp5MnkLh+RnmZkO+biASXBOUWvr7mDFWrvv1 m7+CejYCJbi9Zdf5Zv9avIGc832NEeo4qyCPQiQtOSNjUeA7AHgH7IH6SwWh8mwutfwN M3aQ== X-Forwarded-Encrypted: i=1; AJvYcCWAu5QdTQg96GcMjM9kHyR7lapiaxV9Ig0H8bvuFXmio5wyViaJceRMRn7AJGgcXF4RZo8DAMbj0RbLI4HjPg==@lists.linux.dev X-Gm-Message-State: AOJu0Ywzafa8bkinbiMObO+b8NkuQp1hgSXklzo4ORtoIKtwn6JkLGOh GgzzKxdIl0LHZ/LcQvQEre7Sd9CUUO6xMu6ZPx2Fr2oSRRHCzm1dG6u2tBvswO8dTiMKNNAHcXT aIA== X-Google-Smtp-Source: AGHT+IFnD8IBYtaFLZfmn+yp8faKF4j5W1D2QQ93HaiFkyR3KITtNCKyHKA+6Vtw5k/ROipPB4Gea/5UJbQ= X-Received: from pjbsn8.prod.google.com ([2002:a17:90b:2e88:b0:2f4:465d:5c61]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1f8b:b0:2ee:bf84:4fe8 with SMTP id 98e67ed59e1d1-2f548f1d44cmr36656404a91.30.1736889238866; Tue, 14 Jan 2025 13:13:58 -0800 (PST) Date: Tue, 14 Jan 2025 13:13:57 -0800 In-Reply-To: <20250114175143.81438-26-vschneid@redhat.com> Precedence: bulk X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250114175143.81438-1-vschneid@redhat.com> <20250114175143.81438-26-vschneid@redhat.com> Message-ID: Subject: Re: [PATCH v4 25/30] context_tracking,x86: Defer kernel text patching IPIs From: Sean Christopherson To: Valentin Schneider Cc: linux-kernel@vger.kernel.org, x86@kernel.org, virtualization@lists.linux.dev, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org, linux-perf-users@vger.kernel.org, xen-devel@lists.xenproject.org, kvm@vger.kernel.org, linux-arch@vger.kernel.org, rcu@vger.kernel.org, linux-hardening@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, bpf@vger.kernel.org, bcm-kernel-feedback-list@broadcom.com, Peter Zijlstra , Nicolas Saenz Julienne , Juergen Gross , Ajay Kaher , Alexey Makhalov , Russell King , Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Paul Walmsley , Palmer Dabbelt , Albert Ou , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Boris Ostrovsky , Josh Poimboeuf , Pawan Gupta , Paolo Bonzini , Andy Lutomirski , Arnd Bergmann , Frederic Weisbecker , "Paul E. McKenney" , Jason Baron , Steven Rostedt , Ard Biesheuvel , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Uladzislau Rezki , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Juri Lelli , Clark Williams , Yair Podemsky , Tomas Glozar , Vincent Guittot , Dietmar Eggemann , Ben Segall , Mel Gorman , Kees Cook , Andrew Morton , Christoph Hellwig , Shuah Khan , Sami Tolvanen , Miguel Ojeda , Alice Ryhl , "Mike Rapoport (Microsoft)" , Samuel Holland , Rong Xu , Geert Uytterhoeven , Yosry Ahmed , "Kirill A. Shutemov" , "Masami Hiramatsu (Google)" , Jinghao Jia , Luis Chamberlain , Randy Dunlap , Tiezhu Yang Content-Type: text/plain; charset="us-ascii" On Tue, Jan 14, 2025, Valentin Schneider wrote: > text_poke_bp_batch() sends IPIs to all online CPUs to synchronize > them vs the newly patched instruction. CPUs that are executing in userspace > do not need this synchronization to happen immediately, and this is > actually harmful interference for NOHZ_FULL CPUs. ... > This leaves us with static keys and static calls. ... > @@ -2317,11 +2334,20 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries > * First step: add a int3 trap to the address that will be patched. > */ > for (i = 0; i < nr_entries; i++) { > - tp[i].old = *(u8 *)text_poke_addr(&tp[i]); > - text_poke(text_poke_addr(&tp[i]), &int3, INT3_INSN_SIZE); > + void *addr = text_poke_addr(&tp[i]); > + > + /* > + * There's no safe way to defer IPIs for patching text in > + * .noinstr, record whether there is at least one such poke. > + */ > + if (is_kernel_noinstr_text((unsigned long)addr)) > + cond = NULL; Maybe pre-check "cond", especially if multiple ranges need to be checked? I.e. if (cond && is_kernel_noinstr_text(...)) > + > + tp[i].old = *((u8 *)addr); > + text_poke(addr, &int3, INT3_INSN_SIZE); > } > > - text_poke_sync(); > + __text_poke_sync(cond); > > /* > * Second step: update all but the first byte of the patched range. ... > +/** > + * is_kernel_noinstr_text - checks if the pointer address is located in the > + * .noinstr section > + * > + * @addr: address to check > + * > + * Returns: true if the address is located in .noinstr, false otherwise. > + */ > +static inline bool is_kernel_noinstr_text(unsigned long addr) > +{ > + return addr >= (unsigned long)__noinstr_text_start && > + addr < (unsigned long)__noinstr_text_end; > +} This doesn't do the right thing for modules, which matters because KVM can be built as a module on x86, and because context tracking understands transitions to GUEST mode, i.e. CPUs that are running in a KVM guest will be treated as not being in the kernel, and thus will have IPIs deferred. If KVM uses a static key or branch between guest_state_enter_irqoff() and guest_state_exit_irqoff(), the patching code won't wait for CPUs to exit guest mode, i.e. KVM could theoretically use the wrong static path. I don't expect this to ever cause problems in practice, because patching code in KVM's VM-Enter/VM-Exit path that has *functional* implications, while CPUs are actively running guest code, would be all kinds of crazy. But I do think we should plug the hole. If this issue is unique to KVM, i.e. is not a generic problem for all modules (I assume module code generally isn't allowed in the entry path, even via NMI?), one idea would be to let KVM register its noinstr section for text poking.