From patchwork Tue Nov 15 14:19:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Newman X-Patchwork-Id: 20398 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2759202wru; Tue, 15 Nov 2022 06:26:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf50LWDgHazRRLUtw4I/OZHf4JVsUqVruZ/BVh7x2bw6AB5cRIY13NKAI0QtTpAL6pCoKd5X X-Received: by 2002:a17:907:674b:b0:7a2:b352:a0d3 with SMTP id qm11-20020a170907674b00b007a2b352a0d3mr13810368ejc.399.1668522404770; Tue, 15 Nov 2022 06:26:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668522404; cv=none; d=google.com; s=arc-20160816; b=b6Nnpg25laMBHthBvejDzzUSpGc5PHcTxjRHP3/PdH7yzLP8SaZuZ/iZ0ua0UarNSH 0VfzAqYQnr092vv/u8yhFD23ho5Bh59GV4Z0cv73qKk4xZdRsumq9I9+kHqgQBH7qt0G Ez+rYJorKVaIFThCB/EZYQn5+REvk6SubobjHW1yDU0N3G+/o0MapqizM6Dw9ATNEym8 s+zAx/3m+nuF6RmJDb2xgJK4xTKaH/TXbjo0hxkkDo5vZGnwkZkfiUtfWfYIS9sxkYwX nbw0i0RmJu/Cx4jT5ObxSCxV2VGA/85t066S3bsuZ/t+/45ALSEStN6tNT6THCXro29k F8nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=b9Rh/h+6ZlGogt/ZH/3bghOOublc3eLBTZ62pXvqBB0=; b=k8JpIyIn+UcKRR6JQXBkjkjS2C2fPokngRS/99mGfH3dXgwyDiVKJBHjjckVC5/p3D tOEznyYjjeO8vi7IoHwH0tsVPf3oC6cr/WUB85/j1lNQUwplL5ShdcBlzXMIJ2fl1h1G rcQnP+/YqbF1gyngNfpmysz4NVwX+AaV6M1Pivc03CVdmxQ6mDo9ZM5pUAi4V6w7E1OU npO5FbiCLhWN+iD7B2ztQkpq8g29WaLc96eilBf3TNO4vKYhzz6ykbyE3T5nLPzcaQHk 9kowXgyEvu0icyYuKdEmzjXfVIgWFNqNFh0zpaaEkTcQrxGqSoHnQEGpzOmwK4jMdu4r hjUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Le4fVp49; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cr18-20020a170906d55200b0078d62ae01c1si12043243ejc.536.2022.11.15.06.26.20; Tue, 15 Nov 2022 06:26:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Le4fVp49; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229469AbiKOOUm (ORCPT + 99 others); Tue, 15 Nov 2022 09:20:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230302AbiKOOUk (ORCPT ); Tue, 15 Nov 2022 09:20:40 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC8076302 for ; Tue, 15 Nov 2022 06:20:38 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 204-20020a2510d5000000b006be7970889cso13510271ybq.21 for ; Tue, 15 Nov 2022 06:20:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=b9Rh/h+6ZlGogt/ZH/3bghOOublc3eLBTZ62pXvqBB0=; b=Le4fVp49o0yfNlNz3lamiy4HVGXzuJy2v+4u3zckXkIED6ol/l6y1jHC4u8URf3S4J A9Q+huxLdU2lF0Jx5w1wIeYtPBpptsjYEWA+4ppCbLVP/AavZDXVK+3dF6CK/T8pmqIL mT9tD6fqOpkYSk0/B7EJ+ELjTwnNyNOZnpDSQNOOhuxyCM7Erk74AQRZzMuAPc9TNaA/ u8FO9nAQC/gdhUb7I9vZTCQb/I+YKWwbydiz/d2HelSxGm8Z8YYdNg3oLjPj+JThKQNv QtTIEi2GjkY2z4I6Blrw+OjeDohQkP9+JyMZEu8U1C+4CU96q/npcMzSbo8UF3Ms8OOd mwHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=b9Rh/h+6ZlGogt/ZH/3bghOOublc3eLBTZ62pXvqBB0=; b=qqRhFHBq84M5qMSBb6x04a5LILllm42lFlbJLf2qiy8xaftsR6ltbcK5DHDfVKFYeT 14hymGxggaq8FbahXb2vNAGr3p+mAYYb0kb9hwEAZ6oRCAfoMYVG5hrQsvWzEQ/+5fCe QxmJeolJfDrUoYWQfpyqGlYvcT13g07eOjWdEInG93tWxZ9UQqpjpFeYTak3tEz6bF/b zKrWK16I0c2XdwXcdYibMQL/9NtsAQptG2B/Iqjwfse3io2LdDDIxPlZj3l8uxKQZQBg zzK/jSSl0H0EbSP0V/+omgG13ZdEJLXH6FwK2FMH3KEDJD0rP3gQ+6n9UsZ2nAU+deGi 6VAw== X-Gm-Message-State: ANoB5pmjLZxEjZ6ygaO5dAPOU54maJKQF94jN7kotUgHCtEhNWidf/AB R5Eq41UOieMKmGnAYKkI/jl2kzWooktFY+/sYw== X-Received: from peternewman10.zrh.corp.google.com ([2a00:79e0:9d:6:fe62:9ede:ae13:6fda]) (user=peternewman job=sendgmr) by 2002:a25:d6:0:b0:6ca:1be0:9e4c with SMTP id 205-20020a2500d6000000b006ca1be09e4cmr15819340yba.567.1668522038137; Tue, 15 Nov 2022 06:20:38 -0800 (PST) Date: Tue, 15 Nov 2022 15:19:52 +0100 In-Reply-To: <20221115141953.816851-1-peternewman@google.com> Mime-Version: 1.0 References: <20221115141953.816851-1-peternewman@google.com> X-Mailer: git-send-email 2.38.1.493.g58b659f92b-goog Message-ID: <20221115141953.816851-2-peternewman@google.com> Subject: [PATCH v3 1/2] x86/resctrl: IPI all CPUs for group updates From: Peter Newman To: reinette.chatre@intel.com, fenghua.yu@intel.com Cc: bp@alien8.de, derkling@google.com, eranian@google.com, hpa@zytor.com, james.morse@arm.com, jannh@google.com, kpsingh@google.com, linux-kernel@vger.kernel.org, mingo@redhat.com, tglx@linutronix.de, x86@kernel.org, Peter Newman X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749572548698325680?= X-GMAIL-MSGID: =?utf-8?q?1749572548698325680?= To rule out needing to update a CPU when deleting an rdtgroup, we must search the entire tasklist for group members which could be running on that CPU. This needs to be done while blocking updates to the tasklist to avoid leaving newly-created child tasks assigned to the old CLOSID/RMID. The cost of reliably propagating a CLOSID or RMID update to a single task is higher than originally thought. The present understanding is that we must obtain the task_rq_lock() on each task to ensure that it observes CLOSID/RMID updates in the case that it migrates away from its current CPU before the update IPI reaches it. For now, just notify all the CPUs after updating the closid/rmid fields in impacted tasks task_structs rather than paying the cost of obtaining a more precise cpu mask. Signed-off-by: Peter Newman Reviewed-by: James Morse --- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 52 +++++++------------------- 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c index e5a48f05e787..049971efea2f 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -2385,12 +2385,10 @@ static int reset_all_ctrls(struct rdt_resource *r) * Move tasks from one to the other group. If @from is NULL, then all tasks * in the systems are moved unconditionally (used for teardown). * - * If @mask is not NULL the cpus on which moved tasks are running are set - * in that mask so the update smp function call is restricted to affected - * cpus. + * Following this operation, the caller is required to update the MSRs on all + * CPUs. */ -static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, - struct cpumask *mask) +static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to) { struct task_struct *p, *t; @@ -2400,16 +2398,6 @@ static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, is_rmid_match(t, from)) { WRITE_ONCE(t->closid, to->closid); WRITE_ONCE(t->rmid, to->mon.rmid); - - /* - * If the task is on a CPU, set the CPU in the mask. - * The detection is inaccurate as tasks might move or - * schedule before the smp function call takes place. - * In such a case the function call is pointless, but - * there is no other side effect. - */ - if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t)) - cpumask_set_cpu(task_cpu(t), mask); } } read_unlock(&tasklist_lock); @@ -2440,7 +2428,7 @@ static void rmdir_all_sub(void) struct rdtgroup *rdtgrp, *tmp; /* Move all tasks to the default resource group */ - rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); + rdt_move_group_tasks(NULL, &rdtgroup_default); list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { /* Free any child rmids */ @@ -3099,23 +3087,19 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, return -EPERM; } -static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) +static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp) { struct rdtgroup *prdtgrp = rdtgrp->mon.parent; int cpu; /* Give any tasks back to the parent group */ - rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask); + rdt_move_group_tasks(rdtgrp, prdtgrp); /* Update per cpu rmid of the moved CPUs first */ for_each_cpu(cpu, &rdtgrp->cpu_mask) per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid; - /* - * Update the MSR on moved CPUs and CPUs which have moved - * task running on them. - */ - cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); - update_closid_rmid(tmpmask, NULL); + + update_closid_rmid(cpu_online_mask, NULL); rdtgrp->flags = RDT_DELETED; free_rmid(rdtgrp->mon.rmid); @@ -3140,12 +3124,12 @@ static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp) return 0; } -static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) +static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp) { int cpu; /* Give any tasks back to the default group */ - rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); + rdt_move_group_tasks(rdtgrp, &rdtgroup_default); /* Give any CPUs back to the default group */ cpumask_or(&rdtgroup_default.cpu_mask, @@ -3157,12 +3141,7 @@ static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid; } - /* - * Update the MSR on moved CPUs and CPUs which have moved - * task running on them. - */ - cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); - update_closid_rmid(tmpmask, NULL); + update_closid_rmid(cpu_online_mask, NULL); closid_free(rdtgrp->closid); free_rmid(rdtgrp->mon.rmid); @@ -3181,12 +3160,8 @@ static int rdtgroup_rmdir(struct kernfs_node *kn) { struct kernfs_node *parent_kn = kn->parent; struct rdtgroup *rdtgrp; - cpumask_var_t tmpmask; int ret = 0; - if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) - return -ENOMEM; - rdtgrp = rdtgroup_kn_lock_live(kn); if (!rdtgrp) { ret = -EPERM; @@ -3206,18 +3181,17 @@ static int rdtgroup_rmdir(struct kernfs_node *kn) rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { ret = rdtgroup_ctrl_remove(rdtgrp); } else { - ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask); + ret = rdtgroup_rmdir_ctrl(rdtgrp); } } else if (rdtgrp->type == RDTMON_GROUP && is_mon_groups(parent_kn, kn->name)) { - ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask); + ret = rdtgroup_rmdir_mon(rdtgrp); } else { ret = -EPERM; } out: rdtgroup_kn_unlock(kn); - free_cpumask_var(tmpmask); return ret; } From patchwork Tue Nov 15 14:19:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Newman X-Patchwork-Id: 20399 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2759320wru; Tue, 15 Nov 2022 06:26:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf5BxCyXScd4VQJJ7oACQ6dqhp/i11guWfBoCrYX11KF3PISYo5LdgYfJC93P7oMn5pDGNLO X-Received: by 2002:a17:906:3ace:b0:7ad:9028:4b17 with SMTP id z14-20020a1709063ace00b007ad90284b17mr14247009ejd.366.1668522416184; Tue, 15 Nov 2022 06:26:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668522416; cv=none; d=google.com; s=arc-20160816; b=SqAxWHgjSH4o+PA+mnm1ay6UKEkVm1tlTSHZtmriD0ftN9Fr568zbWHcUBpXe9gNYQ uQUTy1cYPkkT3wlw6jFYmqPd9BZVXPviLdPW8rdvrkjk6ndY15CmBaBJp4juUd7JROi7 b0ZMabis2TYvKT2PO38+uo0TWeKPcGKqS7Mzr3x+CRpCkBosMrs/c4dexoku2c3pZkWk 2qutFBTEeQvMU/uVKxkSIxPd9opWFRPZBKV1o4tpCpz2BAHRVuGXU8hdpionVGQYGOQN tOeQhiKlovJGpjPmMUTHLZ7pg8MLcT0uAYsN1iOC5a01OR6SwIIIn/74Q7Vjn/wblBJC +f+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=yC9Ke3YGhX736WSPCk63GWbYIsXz888Bf6IAXPawHLU=; b=cnecSzjSFm6sHZAvh8BU+wJ7SD4Yjm4xIQIDMCoXm0/LF6EocntwavSbgn6q1EnUTT NwfenEqIcK6D971wmhgBRXqpaPBjmj9u+2Ra/6GOQIsi7hTwCCZ2l6m03SFXnggQAjj+ vwBAz1eVzTmdrh0suXOZLLeWtRePItCFYk3RHJxUBBY2LHpjfnr/yU/mwVKSuAXo4sPZ BXIe98NT097efkie1NAAceT6RbO3CJkJBW1sII8nORJOfPLWaiXppkozv8efcepBxM8h Scfoyf1buHKuEIF/CKTTu4Nc2XJ7a4UtOMaBFKrTaGfPXsKGV5Tftg4z4S5j4bu9Rk2z X2/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XOywDPnE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hp10-20020a1709073e0a00b0078dcddc1b8csi11709622ejc.788.2022.11.15.06.26.31; Tue, 15 Nov 2022 06:26:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XOywDPnE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229832AbiKOOVC (ORCPT + 99 others); Tue, 15 Nov 2022 09:21:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237926AbiKOOUv (ORCPT ); Tue, 15 Nov 2022 09:20:51 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3701A2C113 for ; Tue, 15 Nov 2022 06:20:47 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 204-20020a250fd5000000b006ccc0e91098so13409047ybp.13 for ; Tue, 15 Nov 2022 06:20:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yC9Ke3YGhX736WSPCk63GWbYIsXz888Bf6IAXPawHLU=; b=XOywDPnEa+UXWJS5Ikk9CGHcb2va06XNmGpLjcPQJUxyn7Qz4yzMBoxicvWD9EK2ED 9JcOlqJa1G7bfDnXen68Z1HOoFP1qSUi15nth8oQHNj+hhJ9b+dXJUltgvVMDxPZ2ArL FTBqhvQy+guB6FCMif/p32wWexaCpW+8z1J9CWHGM9fM41KVoINCPTGY878UogXFGPfM hffM5LNUpDi+m79/rCBz4JbepV6pNw7gsiXSxCmzn04fvPhYAmsKQY7AQukIu2UlMpca L8i18ex6Xb4U0XOvN7pMNORw7D5En24aHo8QFgD8wO4sGylA1RIqu1im2zt6+8rtISua SqLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=yC9Ke3YGhX736WSPCk63GWbYIsXz888Bf6IAXPawHLU=; b=qQyRk+kDpJWiN59UtqBctewhDfjEEHboDtU8JSRWd7eLcMsk4TLoRBem1K9TOEygUG dikws5VJPTVgStPPVE/0453MP6lgbFhfM3wucrjgNYbbDPriSTD+yJ4+VR08y/XUl0UM O9C3ziFTwP0shnpFMdrOZjhtgA049qCOjYgCLuuWQtE8Ywvd6VA98vFmr1Oq8zW1M12q FFw8JmK/a6SvTlb4WssSuxjjxNntUywWHqegL4Uy2H1ilBN6WWVDLGq2RBdXitRW5Ao2 ikkNOrsMsJDzDxTfaCxKQ0BrDcsJfKdCwJKo7dXwQQo3fhx1kIXuyJSbbjmUKASUL2hH /uxQ== X-Gm-Message-State: ANoB5pm442M3NxMqkgZHMyLyoxvK2mopyGmpTy6aip69xCfjo59ZFI4Z ypZg7ZoqlkpDzKzIfEu05g/p7AgTDxSbYnBP7g== X-Received: from peternewman10.zrh.corp.google.com ([2a00:79e0:9d:6:fe62:9ede:ae13:6fda]) (user=peternewman job=sendgmr) by 2002:a0d:e5c5:0:b0:36e:ea2:8b56 with SMTP id o188-20020a0de5c5000000b0036e0ea28b56mr17469609ywe.54.1668522046390; Tue, 15 Nov 2022 06:20:46 -0800 (PST) Date: Tue, 15 Nov 2022 15:19:53 +0100 In-Reply-To: <20221115141953.816851-1-peternewman@google.com> Mime-Version: 1.0 References: <20221115141953.816851-1-peternewman@google.com> X-Mailer: git-send-email 2.38.1.493.g58b659f92b-goog Message-ID: <20221115141953.816851-3-peternewman@google.com> Subject: [PATCH v3 2/2] x86/resctrl: update task closid/rmid with task_call_func() From: Peter Newman To: reinette.chatre@intel.com, fenghua.yu@intel.com Cc: bp@alien8.de, derkling@google.com, eranian@google.com, hpa@zytor.com, james.morse@arm.com, jannh@google.com, kpsingh@google.com, linux-kernel@vger.kernel.org, mingo@redhat.com, tglx@linutronix.de, x86@kernel.org, Peter Newman X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749572561524314494?= X-GMAIL-MSGID: =?utf-8?q?1749572561524314494?= When determining whether running tasks need to be interrupted due to a closid/rmid change, it was possible for the task in question to migrate or wake up concurrently without observing the updated values. In particular, __rdtgroup_move_task() assumed that a CPU migrating implied that it observed the updated closid/rmid. This assumption is broken by the following reorderings, both of which are allowed on x86: 1. In __rdtgroup_move_task(), stores updating the closid and rmid in the task structure could reorder with the loads in task_curr() and task_cpu(). 2. In resctrl_sched_in(), the earlier stores to the fields read by task_curr() could be delayed until after the loads from t->{closid,rmid}. Preventing this reordering with barriers would have required an smp_mb() in all context switches whenever resctrlfs is mounted. Instead, when moving a single task, use task_call_func() to serialize updates to the closid and rmid fields in the task_struct with context switch. Signed-off-by: Peter Newman Reviewed-by: James Morse --- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 76 +++++++++++++++----------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c index 049971efea2f..511b7cea143f 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -538,10 +538,47 @@ static void _update_task_closid_rmid(void *task) resctrl_sched_in(); } -static void update_task_closid_rmid(struct task_struct *t) +static int update_locked_task_closid_rmid(struct task_struct *t, void *arg) { - if (IS_ENABLED(CONFIG_SMP) && task_curr(t)) - smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1); + struct rdtgroup *rdtgrp = arg; + + /* + * We assume task_call_func() has provided the necessary serialization + * with resctrl_sched_in(). + */ + if (rdtgrp->type == RDTCTRL_GROUP) { + t->closid = rdtgrp->closid; + t->rmid = rdtgrp->mon.rmid; + } else if (rdtgrp->type == RDTMON_GROUP) { + t->rmid = rdtgrp->mon.rmid; + } + + /* + * If the task is current on a CPU, the PQR_ASSOC MSR needs to be + * updated to make the resource group go into effect. If the task is not + * current, the MSR will be updated when the task is scheduled in. + */ + return task_curr(t); +} + +static void update_task_closid_rmid(struct task_struct *t, + struct rdtgroup *rdtgrp) +{ + /* + * Serialize the closid and rmid update with context switch. If this + * function indicates that the task was running, then it needs to be + * interrupted to install the new closid and rmid. + */ + if (task_call_func(t, update_locked_task_closid_rmid, rdtgrp) && + IS_ENABLED(CONFIG_SMP)) + /* + * If the task has migrated away from the CPU indicated by + * task_cpu() below, then it has already switched in on the + * new CPU using the updated closid and rmid and the call below + * unnecessary, but harmless. + */ + smp_call_function_single(task_cpu(t), + _update_task_closid_rmid, t, 1); else _update_task_closid_rmid(t); } @@ -557,39 +594,16 @@ static int __rdtgroup_move_task(struct task_struct *tsk, return 0; /* - * Set the task's closid/rmid before the PQR_ASSOC MSR can be - * updated by them. - * - * For ctrl_mon groups, move both closid and rmid. * For monitor groups, can move the tasks only from * their parent CTRL group. */ - - if (rdtgrp->type == RDTCTRL_GROUP) { - WRITE_ONCE(tsk->closid, rdtgrp->closid); - WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid); - } else if (rdtgrp->type == RDTMON_GROUP) { - if (rdtgrp->mon.parent->closid == tsk->closid) { - WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid); - } else { - rdt_last_cmd_puts("Can't move task to different control group\n"); - return -EINVAL; - } + if (rdtgrp->type == RDTMON_GROUP && + rdtgrp->mon.parent->closid != tsk->closid) { + rdt_last_cmd_puts("Can't move task to different control group\n"); + return -EINVAL; } - /* - * Ensure the task's closid and rmid are written before determining if - * the task is current that will decide if it will be interrupted. - */ - barrier(); - - /* - * By now, the task's closid and rmid are set. If the task is current - * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource - * group go into effect. If the task is not current, the MSR will be - * updated when the task is scheduled in. - */ - update_task_closid_rmid(tsk); + update_task_closid_rmid(tsk, rdtgrp); return 0; }