Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1292597lql; Tue, 12 Mar 2024 12:39:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW2TPZHIocvQKHwWzRyEvtv9CXxBRDCZROC6dkEpKzXpCbi0Fj7/RzRlM81Z+QcPEXbTnr0ERo7YXljuhOg4nx2OdbX0XUxbiJcflfGHg== X-Google-Smtp-Source: AGHT+IH9rUGECl630CNBRRonESTTklH1xqCHMwYBIGQD7Yce7QFXryW4IYmRWI2to+NF7qnqgKK0 X-Received: by 2002:a0c:f582:0:b0:690:b44d:b9b with SMTP id k2-20020a0cf582000000b00690b44d0b9bmr868092qvm.17.1710272393109; Tue, 12 Mar 2024 12:39:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710272393; cv=pass; d=google.com; s=arc-20160816; b=Mdp08teef4bLoA9zvTyxuoG/2An08QjKDLD6T8p8WXlfviBT2dEOLCCrhdraCJYJFP KSBR4q0W/S4xEOXbw/P9id3WJWJ8HyJUk8EjgdQUrM5HavYjRkOTnnmCYuMzilDtZFkm Lb5CYPm4iNvSuf6a8UB0kZKhrklkvS69ZdQ5jeV+Se8ihwKVVVIlrMO3DLDsXvnPoAyE b/hjJpqldTWn3LEFXzn6o/ChaFSWPhsqiv2vK3U39R686fwspNQI8tL4qfFr6lhtfpLK 3imMwacRRNSsCSMxnhZR0TWfosL2rxXOL/+DDojvhb6rbmJKPqD8zYFk+aK4b3ga7wPp VK0A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=y37QTRRCaOyhTss07/2XbpFXeVVO9uDT6HFjFeJL8cA=; fh=mzDj0K9Gp9ixi14K2wnpYo1QPeLnqAzyqrLp41wvwdU=; b=raj0A6PkjkepNNQxnXmEQjcVnD/dMnJ9Et8xowrxamAhSnXahkeBNDaSLwPYH7GkUv UHaAeZiFZgVZwMTvvvOOdtyKGY2LlECuIfwlKZN/cIwdO6N+fziwhRFJDTCJn4i3fpjw 0dJ3SAZEw6NCySJgGJnzV/uCjUCnmxjQ4Zgf1cXoM33bbaDFr47S4EyVydVj3j+hFqr3 QFD3EhraGiMMeWIi9DJIB/PX6VYcYN+/FSUnYq29QsbUBcqJ1dNvBA5FZLh3G4Htzr9Q tplgjGPTL9k8ycLnwQhsE6ujB36fcth5lVf/mhuvDrHn3VPBRz/58+0nxflw0HQdWGQJ 732A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Np2mpj5m; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-100717-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100717-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id z15-20020a056214040f00b00690d9ed71dbsi3895895qvx.496.2024.03.12.12.39.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 12:39:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-100717-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Np2mpj5m; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-100717-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100717-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C04C51C21B46 for ; Tue, 12 Mar 2024 19:39:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B66E37D406; Tue, 12 Mar 2024 19:39:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Np2mpj5m" Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 4F88A1428F7 for ; Tue, 12 Mar 2024 19:39:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710272368; cv=none; b=mrk5ak+ZEsvO3HB3PVu0WzVJnX27v3WQ2fFBeUf7hEbg2iFL1cmLODK8LMch7nnyR/enlE9ArK/lg83rVTXUDHOOW4nPB7KGN5V0Vn8GgAB0JbPUs//tUoanaOAVP14Ywnb9oKXT4aEzNaiAtA7A+mZ+dFm+PC02ngQNo47i1eU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710272368; c=relaxed/simple; bh=7F4aenK3C8j4SGFBioKeFeh7bXOjOVxZfo8KHxdPG24=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KUNu0xlku65AKMA9RwdxoQp8oT2kYsKawz2yasBegZAlH9LhxTUt4sx2C3PKiZeefGPePomuxHrWjX5+AjmXXQcT0PNpJUcU57ydvAnFbCmSuY0Kvnup3LqEvvELJ6SClqj1vFpqWjcAWNq4Mu07qA4YwZ+AIPbbPP6PbYwzsdE= 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=Np2mpj5m; arc=none smtp.client-ip=209.85.215.201 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 Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5e4f312a995so182109a12.2 for ; Tue, 12 Mar 2024 12:39:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710272366; x=1710877166; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=y37QTRRCaOyhTss07/2XbpFXeVVO9uDT6HFjFeJL8cA=; b=Np2mpj5mDgAsX3zwczEbvWJa1bO9R+/asoE3YdhmN7PWnVNjMe1p23BLatbw5YTc+V 4x314HooLgaNYgtEDo1Qb2ees9lxeW7q6V043OyNv/Nsaz0pAGNjtgqlPRdM+MqEfg7L zN55OXSszluXqVGQXNFDbkOzB0PR/BlzoXfIYPrWmPOFwWRmdiS7Lpv4O6kr38RSXXJy Y9LCye8ek3vA1GEgfjOfd7vfZy+6eRPmQ6wSrPFIcfueH2UAhol/QDUr631IJIMfqAa7 nxJvuwkIIWQ0Q75Es05fpd5I3vcOqF6lstMNl/RzGI95IyLcXZj/1SVG5zxRhQFGdpRt pFYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710272366; x=1710877166; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=y37QTRRCaOyhTss07/2XbpFXeVVO9uDT6HFjFeJL8cA=; b=SgJZI2MY6WUPFR+vAPgzA2lQtdm0MjIVx3dQRkEFuTh/jiwca9+zQPPkC9PoijE22E P37oXZhCt/7cQtU2x2k0RNV8Lfh11cFLo4HKXOAxyUHutOXmYHatZGUY3OrsUJJwxrmy caoP8RAU8kuMv/YHspwdTcBM5s1ATpAnZa5JoZvpC9EvzV40klJCS1UM+7GG16NRQn2/ 5aFoNUffSe6eVbnLBjsV5QLTr0PkJZ/n1MR19Umh72BgHuCFwg007D5vp2xrkxasVGHg OCqYftQ0XiKEcybIUHIny7fPr4+SeP4xTLTDOrKsp1ZvNIFbd2NZVKRPwRGx/7pDbLtb IH2g== X-Forwarded-Encrypted: i=1; AJvYcCV7NNU9a6ZteR3IfaSP90IqwngQjOjGQWEspgV5vK6fAQjiDliNmKVvDYCVt8zVRTR74NlsO8sp2GeH+bz8ofhb1MoJx9axnlaCQKHJ X-Gm-Message-State: AOJu0YzgdnRSC+MM1thUloIFLNY28U+bduAOyaJ77K/EI6m9YJY4h639 ouPXqB3aQ9RAV/pie/i8+XPzZRiyIZFey48F2vg0OhAxWecUytgH/hoO2rdjFPLGNsfu6S1+w7q 2bQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:714:b0:5dc:8f95:3d with SMTP id cb20-20020a056a02071400b005dc8f95003dmr2886pgb.2.1710272366615; Tue, 12 Mar 2024 12:39:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 12 Mar 2024 12:39:11 -0700 In-Reply-To: <20240312193911.1796717-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240312193911.1796717-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240312193911.1796717-3-seanjc@google.com> Subject: [PATCH v2 2/2] sched/core: Drop spinlocks on contention iff kernel is preemptible From: Sean Christopherson To: Jonathan Corbet , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Will Deacon Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Christopherson , Valentin Schneider , Marco Elver , Frederic Weisbecker , David Matlack , Friedrich Weber , Ankur Arora , Thomas Gleixner Content-Type: text/plain; charset="UTF-8" Use preempt_model_preemptible() to detect a preemptible kernel when deciding whether or not to reschedule in order to drop a contended spinlock or rwlock. Because PREEMPT_DYNAMIC selects PREEMPTION, kernels built with PREEMPT_DYNAMIC=y will yield contended locks even if the live preemption model is "none" or "voluntary". In short, make kernels with dynamically selected models behave the same as kernels with statically selected models. Somewhat counter-intuitively, NOT yielding a lock can provide better latency for the relevant tasks/processes. E.g. KVM x86's mmu_lock, a rwlock, is often contended between an invalidation event (takes mmu_lock for write) and a vCPU servicing a guest page fault (takes mmu_lock for read). For _some_ setups, letting the invalidation task complete even if there is mmu_lock contention provides lower latency for *all* tasks, i.e. the invalidation completes sooner *and* the vCPU services the guest page fault sooner. But even KVM's mmu_lock behavior isn't uniform, e.g. the "best" behavior can vary depending on the host VMM, the guest workload, the number of vCPUs, the number of pCPUs in the host, why there is lock contention, etc. In other words, simply deleting the CONFIG_PREEMPTION guard (or doing the opposite and removing contention yielding entirely) needs to come with a big pile of data proving that changing the status quo is a net positive. Opportunistically document this side effect of preempt=full, as yielding contended spinlocks can have significant, user-visible impact. Fixes: c597bfddc9e9 ("sched: Provide Kconfig support for default dynamic preempt mode") Link: https://lore.kernel.org/kvm/ef81ff36-64bb-4cfe-ae9b-e3acf47bff24@proxmox.com Cc: Valentin Schneider Cc: Peter Zijlstra (Intel) Cc: Marco Elver Cc: Frederic Weisbecker Cc: David Matlack Cc: Friedrich Weber Cc: Ankur Arora Cc: Thomas Gleixner Signed-off-by: Sean Christopherson --- Documentation/admin-guide/kernel-parameters.txt | 4 +++- include/linux/spinlock.h | 14 ++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 825398d66c69..fdeddb066439 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -4689,7 +4689,9 @@ none - Limited to cond_resched() calls voluntary - Limited to cond_resched() and might_sleep() calls full - Any section that isn't explicitly preempt disabled - can be preempted anytime. + can be preempted anytime. Tasks will also yield + contended spinlocks (if the critical section isn't + explicitly preempt disabled beyond the lock itself). print-fatal-signals= [KNL] debug: print fatal signals diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h index 3fcd20de6ca8..63dd8cf3c3c2 100644 --- a/include/linux/spinlock.h +++ b/include/linux/spinlock.h @@ -462,11 +462,10 @@ static __always_inline int spin_is_contended(spinlock_t *lock) */ static inline int spin_needbreak(spinlock_t *lock) { -#ifdef CONFIG_PREEMPTION + if (!preempt_model_preemptible()) + return 0; + return spin_is_contended(lock); -#else - return 0; -#endif } /* @@ -479,11 +478,10 @@ static inline int spin_needbreak(spinlock_t *lock) */ static inline int rwlock_needbreak(rwlock_t *lock) { -#ifdef CONFIG_PREEMPTION + if (!preempt_model_preemptible()) + return 0; + return rwlock_is_contended(lock); -#else - return 0; -#endif } /* -- 2.44.0.278.ge034bb2e1d-goog