From patchwork Wed Jun 7 19:46:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1791889 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=Gm2M6M22; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QbyXv3Qc1z20WP for ; Thu, 8 Jun 2023 05:47:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A21D3858C1F for ; Wed, 7 Jun 2023 19:47:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A21D3858C1F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686167229; bh=N4Lhd8bPw1qWghbhPOcCyxlTEO1u5bHRX+0h+A0nLSQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Gm2M6M22c/UrJ2Ouf7SbOzGn8TSxY9Qbyt3Ky/W6ztaMVZ+4Rl26GQL0uILu+jkWD RtzhYBKoxZIld6+GitT67KugbCEaVoJFkzWiYUmqaFGah6IwmmCrotpDGpU9uk3k1P c8PXgqDn4uMpm9eqbega0sB8NP/gRblmNW/GNqq8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id AB9263858C1F for ; Wed, 7 Jun 2023 19:46:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB9263858C1F Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-977d55ac17bso716052066b.3 for ; Wed, 07 Jun 2023 12:46:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686167213; x=1688759213; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=N4Lhd8bPw1qWghbhPOcCyxlTEO1u5bHRX+0h+A0nLSQ=; b=HFLwCmi8ry0cxTuGF1LxK8BowtlkKCioAFb2KeH8CSuPoXkDNcpmFc1iXRCQ1SIirf dFbPgP7OcIhKMvK94dv/PCYG3hxrVqoyOQsxWrtga5/Lo8lk5hAj/jq6BRMTkEZjvaNJ FSBV+1DuN3xuRxGM0DtNwPn5iubNahPvoPg9jmkFrE6CrR1/CkMM/tphTmeokxddxuWJ otFWslp9GsJxztf+IxniHqvvP/9om1CV2TsyDFR+uu9WoclrcLwnpVbnsdxwRFVTfVe0 UXTI8j6KGH1UkMaX5JXFXADsNBxfOL+GDJcRip8wUE7SfwvEHkEAqQq2sAIwcLkLvAW7 OGPw== X-Gm-Message-State: AC+VfDxUjErznfGKXUrZLHCb5kXRhGWGntN4+8wJg6mXkhqEktsD1PvH IFjdMUykn/3iRmviFd3YT4IgXY/TBzU= X-Google-Smtp-Source: ACHHUZ5aB0pkKldp6tNJYkQ6VMKtcBPgJBL90mHePwzDgERqlBCs32KtqYf4z90wxkrhjYGCD8Zp/A== X-Received: by 2002:a17:907:3189:b0:974:c32c:b484 with SMTP id xe9-20020a170907318900b00974c32cb484mr6507415ejb.72.1686167212666; Wed, 07 Jun 2023 12:46:52 -0700 (PDT) Received: from noahgold-desk.intel.com (2603-8080-1301-76c6-bbb0-ef3c-a689-4ab7.res6.spectrum.com. [2603:8080:1301:76c6:bbb0:ef3c:a689:4ab7]) by smtp.gmail.com with ESMTPSA id y1-20020a1709064b0100b00978874e833bsm919130eju.168.2023.06.07.12.46.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jun 2023 12:46:52 -0700 (PDT) To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com, hjl.tools@gmail.com, carlos@systemhalted.org Subject: [PATCH v1 1/2] linux: Implement sched_yield in C Date: Wed, 7 Jun 2023 14:46:42 -0500 Message-Id: <20230607194643.2081329-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" This is in preperation for a minor optimization before expected context switches. --- sysdeps/unix/sysv/linux/sched_yield.c | 27 +++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/syscalls.list | 1 - 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 sysdeps/unix/sysv/linux/sched_yield.c diff --git a/sysdeps/unix/sysv/linux/sched_yield.c b/sysdeps/unix/sysv/linux/sched_yield.c new file mode 100644 index 0000000000..154bf725b0 --- /dev/null +++ b/sysdeps/unix/sysv/linux/sched_yield.c @@ -0,0 +1,27 @@ +/* Yield current process. Linux specific syscall. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +int +__sched_yield (void) +{ + return INLINE_SYSCALL_CALL (sched_yield); +} +libc_hidden_def (__sched_yield); +weak_alias (__sched_yield, sched_yield) diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list index 73e941ef89..5f571df937 100644 --- a/sysdeps/unix/sysv/linux/syscalls.list +++ b/sysdeps/unix/sysv/linux/syscalls.list @@ -59,7 +59,6 @@ sched_primax - sched_get_priority_max i:i __sched_get_priority_max sched_get_pri sched_primin - sched_get_priority_min i:i __sched_get_priority_min sched_get_priority_min sched_setp - sched_setparam i:ip __sched_setparam sched_setparam sched_sets - sched_setscheduler i:iip __sched_setscheduler sched_setscheduler -sched_yield - sched_yield i: __sched_yield sched_yield setfsgid EXTRA setfsgid i:i setfsgid setfsuid EXTRA setfsuid i:i setfsuid setpgid - setpgid i:ii __setpgid setpgid From patchwork Wed Jun 7 19:46:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1791890 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=FydoXfvx; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QbyXz05byz20WP for ; Thu, 8 Jun 2023 05:47:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0A8893857739 for ; Wed, 7 Jun 2023 19:47:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A8893857739 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686167233; bh=ejWsQxctLYjhhr4C+P8TxdlKk6mBAs07N/yUGHz9pc0=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=FydoXfvxQMj0R8zT+mxJSoBqInO7YwGkLBQsSgp04ttT4z/AhoxU+PkMJ09rgRFTH IPEzSs98NwPL8cRzhiIzjHembx4T2RCUwqIQCsOAgSaO4XQctWAnnDNpdBvzfpRegv QIzs+S/LG49rgFSLdVtbIt5geeZnHfOihahXJ96U= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id BACBF3857727 for ; Wed, 7 Jun 2023 19:46:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BACBF3857727 Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-9786fc23505so357534366b.2 for ; Wed, 07 Jun 2023 12:46:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686167216; x=1688759216; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ejWsQxctLYjhhr4C+P8TxdlKk6mBAs07N/yUGHz9pc0=; b=kTlkepiyvlsOoXPZeZO07V1842SWMSuGiVM8xp+YhynzsJwnYJtnAS88uA77AG12qS X+RJ7YWDmEVv4uvBUgiaskmszR1HY+88vXMy/xB/14hjD5m+4xVTOt5oFNMLBLQcVAM5 yVyYKw/ykWxyCO2+w7OJNMRhYo9uWKfFeNbKOdml13XoD0XAcrGcBkUyvTI7Fi+ApVA4 LIUXyIa9x1AgtlKFHyDHlL4x88eOxnc1Cnk/YjcBcNuICa0VOwp9vUloKYLhn4TwIs5b JOIa0Wb9cZy23pxhCZeyuB9FZSV+WHRor3hJHUYAw1q2bcphf+NJQsPrBol8JYHPo+c6 Gz8Q== X-Gm-Message-State: AC+VfDzGRBK9+y8ZvMTJI+yItRuQ8ByGV4IwOehsLi67D5N+okmgwetr cgnrmz8ww3yjk/Q0VZ5w4z36C+XLD2A= X-Google-Smtp-Source: ACHHUZ6TD/BCuzuQc2d8onMbECVP8KhIQbAYNu/0QYPCt/K08QhgFDeEHIAydivLcI903gnlGM3opg== X-Received: by 2002:a17:907:1c0b:b0:971:c931:3677 with SMTP id nc11-20020a1709071c0b00b00971c9313677mr8755862ejc.68.1686167215864; Wed, 07 Jun 2023 12:46:55 -0700 (PDT) Received: from noahgold-desk.intel.com (2603-8080-1301-76c6-bbb0-ef3c-a689-4ab7.res6.spectrum.com. [2603:8080:1301:76c6:bbb0:ef3c:a689:4ab7]) by smtp.gmail.com with ESMTPSA id y1-20020a1709064b0100b00978874e833bsm919130eju.168.2023.06.07.12.46.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jun 2023 12:46:55 -0700 (PDT) To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com, hjl.tools@gmail.com, carlos@systemhalted.org Subject: [PATCH v1 2/2] x86: Add `prepare_context_switch` to initialize register inuse states Date: Wed, 7 Jun 2023 14:46:43 -0500 Message-Id: <20230607194643.2081329-2-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230607194643.2081329-1-goldstein.w.n@gmail.com> References: <20230607194643.2081329-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" xsave/xrstor have optimization to skip saving/restoring register classes if those register classes are in the init state (inuse[bit]==0). We can get: SSE state AVX state ZMM_HI256 state to init state using `vzeroall`. Doing this before syscalls that will cause a proper context switch can be beneficial in terms of the amount of state the kernel needs to save/restore. This can save time and memory. --- sysdeps/generic/prepare-context-switch.h | 28 +++++++++++++ sysdeps/unix/sysv/linux/clock_nanosleep.c | 2 + sysdeps/unix/sysv/linux/sched_yield.c | 2 + sysdeps/x86/prepare-context-switch.h | 50 +++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 sysdeps/generic/prepare-context-switch.h create mode 100644 sysdeps/x86/prepare-context-switch.h diff --git a/sysdeps/generic/prepare-context-switch.h b/sysdeps/generic/prepare-context-switch.h new file mode 100644 index 0000000000..6153847905 --- /dev/null +++ b/sysdeps/generic/prepare-context-switch.h @@ -0,0 +1,28 @@ +/* Prepare process for context switch. generic version + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _PREPARE_CONTEXT_SWITCH_H +#define _PREPARE_CONTEXT_SWITCH_H + +static void +prepare_context_switch (void) +{ + /* Empty. */ +} + +#endif diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c index ac2d810632..e674f0ac54 100644 --- a/sysdeps/unix/sysv/linux/clock_nanosleep.c +++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c @@ -23,6 +23,7 @@ #include "kernel-posix-cpu-timers.h" #include +#include /* We can simply use the syscall. The CPU clocks are not supported with this function. */ @@ -44,6 +45,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags, #endif int r; + prepare_context_switch(); #ifdef __ASSUME_TIME64_SYSCALLS r = INTERNAL_SYSCALL_CANCEL (clock_nanosleep_time64, clock_id, flags, req, rem); diff --git a/sysdeps/unix/sysv/linux/sched_yield.c b/sysdeps/unix/sysv/linux/sched_yield.c index 154bf725b0..d26c0f8a9f 100644 --- a/sysdeps/unix/sysv/linux/sched_yield.c +++ b/sysdeps/unix/sysv/linux/sched_yield.c @@ -17,10 +17,12 @@ . */ #include +#include int __sched_yield (void) { + prepare_context_switch(); return INLINE_SYSCALL_CALL (sched_yield); } libc_hidden_def (__sched_yield); diff --git a/sysdeps/x86/prepare-context-switch.h b/sysdeps/x86/prepare-context-switch.h new file mode 100644 index 0000000000..bf33a7a1b3 --- /dev/null +++ b/sysdeps/x86/prepare-context-switch.h @@ -0,0 +1,50 @@ +/* Prepare process for context switch. x86 version + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _PREPARE_CONTEXT_SWITCH_H +#define _PREPARE_CONTEXT_SWITCH_H + +#ifdef __AVX__ +static void +prepare_context_switch (void) +{ + /* vzeroall before context switch will restore xsave/xrstor state of the + following to init state: + - SSE state + - AVX state + - ZMM_HI256 state + This saves a touch of overhead and memory in context switches. + This function can/should be used before an operation that will + cause a context switch in the current process (sched_yield, + *sleep, etc...). + */ + __asm__ volatile ("vzeroall" + : + : + : "zmm0", "zmm1", "zmm2", "zmm3", "zmm4", "zmm5", "zmm6", + "zmm7", "zmm8", "zmm9", "zmm10", "zmm11", "zmm12", + "zmm13", "zmm14", "zmm15"); + /* TODO: Add xtilerelease for amx state. */ +} + +#else +# undef _PREPARE_CONTEXT_SWITCH_H +# include +#endif + +#endif