From patchwork Thu Nov 3 20:03:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 15171 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp743190wru; Thu, 3 Nov 2022 13:08:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5TV60si8jXpQm0lzTEid2RBUYtyTHJZi/j1yYpCIALudQQYoT1chxUwIjlbZj+oN1aiy81 X-Received: by 2002:a17:90a:cb96:b0:213:1dc2:b1de with SMTP id a22-20020a17090acb9600b002131dc2b1demr48993635pju.21.1667506093580; Thu, 03 Nov 2022 13:08:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667506093; cv=none; d=google.com; s=arc-20160816; b=GxCuWdZizmGIcKPlVzCksJMqo6RpfFHMEY8tVPps+UX8BpBqEMH0ri4dHN7gPRaW4g fn5ACzfFIa+8j9/WnD8kxKS8SPB0BDURsg/aGgM38U9KDT+fOI41yL1B+WdaJ0csTGM3 AUr28eJB7bF6ahxVHiZhBFbGaox5THMGWiqHzEPJlWFfz68ovo9AmOKmf8ywCgdgXTMJ IFjFGfAEb1P0PfGhfGLV6TJ38Zi7OcmFTjHocRMWa4m5g37WlvQ1qaDeCUapAgLpXTmt D6mVXOCMNZFEUwmzpKSX6drtV0esOj2PHlbvKwbs825wqDN2DqL7lps1/AUImj6eKtCw OA1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=grmA72BhJPeUzzy3zmNwQHR5TQlLcrMaYhBRyvIpIf8=; b=qsT+g7boRZOvjWZEg76vmE93ZNor4qAj2Et0KtNYnK/juZKsrLltjhIJs99OYecPuD +0AiJX3r2orrbk5dpMdT47enmObb3jIFsrrq17n5awpVA0OW5QkIyWfK7hk/UlkUAUjt EPktmHpxymWIgUyJ2Qso3rPLYphjOuxDmc6zYohPgjSsBD70ImOUaWe5Jpaf/MEJeTdM WLhMGzCfasRjnbopIEj3gDaLJ2Q7QD+aNXVdYtOGEQOGnpiy9nxvL3SgvK0BfMikNx6o 17/DtwkRw0N4H9TCw9czb8c2YlscojAMcZ2lcYW+TOa0t5cLxIRrXEIEvx0joTsAldMa ediA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b="kVb/UGc8"; 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=NONE sp=NONE dis=NONE) header.from=efficios.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e29-20020a63501d000000b00439f012ca81si2207231pgb.605.2022.11.03.13.07.58; Thu, 03 Nov 2022 13:08:13 -0700 (PDT) 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=@efficios.com header.s=smtpout1 header.b="kVb/UGc8"; 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=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231566AbiKCUFf (ORCPT + 99 others); Thu, 3 Nov 2022 16:05:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230496AbiKCUFA (ORCPT ); Thu, 3 Nov 2022 16:05:00 -0400 Received: from smtpout.efficios.com (smtpout.efficios.com [IPv6:2607:5300:203:5aae::31e5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 354212035F; Thu, 3 Nov 2022 13:04:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1667505898; bh=m+9Uyu/8dIKQx29uw36+nk66J6rIL4EwiN+rQ3z02SU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kVb/UGc8ovwrhb2uKOvg5SMZa60UJJPtHjxz1GhTLyJ6Kq8Bhj8QyScMpH7C7dZek 4PqILDGfQBA22s45boDaYACw1U/NiKIMzPP378IPXK3sW+KhmnH5phKqwuXQvTJx+E 2Ae5Sacljn3Ee2qhj04jgemGeiFNFhokatsWTuUFxXLDt0+53iBWfdfRNp6sVGwvwe lp11tyhtXvdLwvnnC5aA7/TfmtthoozJDAUKqZB6VPQaYE0W0yh9Ms0jqKfB+7UzCN DCY9FPEZGW8WVGEIK1ZbqPe1cg0WInnPHcAUDzBUCSMYw6NmrOdeaqc8JQ0lK6SWMj Jwllk2W8OOa/w== Received: from localhost.localdomain (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4N3F961sHdzfjV; Thu, 3 Nov 2022 16:04:58 -0400 (EDT) From: Mathieu Desnoyers To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Thomas Gleixner , "Paul E . McKenney" , Boqun Feng , "H . Peter Anvin" , Paul Turner , linux-api@vger.kernel.org, Christian Brauner , Florian Weimer , David.Laight@ACULAB.COM, carlos@redhat.com, Peter Oskolkov , Alexander Mikhalitsyn , Chris Kennelly , Mathieu Desnoyers Subject: [PATCH v5 09/24] rseq: Extend struct rseq with per memory space vcpu id Date: Thu, 3 Nov 2022 16:03:44 -0400 Message-Id: <20221103200359.328736-10-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221103200359.328736-1-mathieu.desnoyers@efficios.com> References: <20221103200359.328736-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS 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?1748506869097631384?= X-GMAIL-MSGID: =?utf-8?q?1748506869097631384?= If a memory space has fewer threads than cores, or is limited to run on few cores concurrently through sched affinity or cgroup cpusets, the virtual cpu ids will be values close to 0, thus allowing efficient use of user-space memory for per-cpu data structures. Signed-off-by: Mathieu Desnoyers --- include/uapi/linux/rseq.h | 9 +++++++++ kernel/rseq.c | 11 ++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/rseq.h b/include/uapi/linux/rseq.h index 1cb90a435c5c..77a136586ac6 100644 --- a/include/uapi/linux/rseq.h +++ b/include/uapi/linux/rseq.h @@ -139,6 +139,15 @@ struct rseq { */ __u32 node_id; + /* + * Restartable sequences vm_vcpu_id field. Updated by the kernel. Read by + * user-space with single-copy atomicity semantics. This field should + * only be read by the thread which registered this data structure. + * Aligned on 32-bit. Contains the current thread's virtual CPU ID + * (allocated uniquely within a memory space). + */ + __u32 vm_vcpu_id; + /* * Flexible array member at end of structure, after last feature field. */ diff --git a/kernel/rseq.c b/kernel/rseq.c index e21ad8929958..604c284a355c 100644 --- a/kernel/rseq.c +++ b/kernel/rseq.c @@ -90,12 +90,15 @@ static int rseq_update_cpu_node_id(struct task_struct *t) struct rseq __user *rseq = t->rseq; u32 cpu_id = raw_smp_processor_id(); u32 node_id = cpu_to_node(cpu_id); + u32 vm_vcpu_id = task_mm_vcpu_id(t); + WARN_ON_ONCE((int) vm_vcpu_id < 0); if (!user_write_access_begin(rseq, t->rseq_len)) goto efault; unsafe_put_user(cpu_id, &rseq->cpu_id_start, efault_end); unsafe_put_user(cpu_id, &rseq->cpu_id, efault_end); unsafe_put_user(node_id, &rseq->node_id, efault_end); + unsafe_put_user(vm_vcpu_id, &rseq->vm_vcpu_id, efault_end); /* * Additional feature fields added after ORIG_RSEQ_SIZE * need to be conditionally updated only if @@ -113,7 +116,8 @@ static int rseq_update_cpu_node_id(struct task_struct *t) static int rseq_reset_rseq_cpu_node_id(struct task_struct *t) { - u32 cpu_id_start = 0, cpu_id = RSEQ_CPU_ID_UNINITIALIZED, node_id = 0; + u32 cpu_id_start = 0, cpu_id = RSEQ_CPU_ID_UNINITIALIZED, node_id = 0, + vm_vcpu_id = 0; /* * Reset cpu_id_start to its initial state (0). @@ -132,6 +136,11 @@ static int rseq_reset_rseq_cpu_node_id(struct task_struct *t) */ if (put_user(node_id, &t->rseq->node_id)) return -EFAULT; + /* + * Reset vm_vcpu_id to its initial state (0). + */ + if (put_user(vm_vcpu_id, &t->rseq->vm_vcpu_id)) + return -EFAULT; /* * Additional feature fields added after ORIG_RSEQ_SIZE * need to be conditionally reset only if