From owner-svn-src-all@freebsd.org Mon Aug 24 12:43:55 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 49FFA3BDF4E; Mon, 24 Aug 2020 12:43:55 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BZsHv1Fb9z4jYm; Mon, 24 Aug 2020 12:43:55 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 0F10A18AB4; Mon, 24 Aug 2020 12:43:55 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 07OChsaR034336; Mon, 24 Aug 2020 12:43:54 GMT (envelope-from trasz@FreeBSD.org) Received: (from trasz@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 07OChsIO034334; Mon, 24 Aug 2020 12:43:54 GMT (envelope-from trasz@FreeBSD.org) Message-Id: <202008241243.07OChsIO034334@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: trasz set sender to trasz@FreeBSD.org using -f From: Edward Tomasz Napierala Date: Mon, 24 Aug 2020 12:43:54 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r364664 - stable/12/sys/compat/linux X-SVN-Group: stable-12 X-SVN-Commit-Author: trasz X-SVN-Commit-Paths: stable/12/sys/compat/linux X-SVN-Commit-Revision: 364664 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Aug 2020 12:43:55 -0000 Author: trasz Date: Mon Aug 24 12:43:54 2020 New Revision: 364664 URL: https://svnweb.freebsd.org/changeset/base/364664 Log: MFC r358483 by tijl: linuxulator: Map scheduler priorities to Linux priorities. On Linux the valid range of priorities for the SCHED_FIFO and SCHED_RR scheduling policies is [1,99]. For SCHED_OTHER the single valid priority is 0. On FreeBSD it is [0,31] for all policies. Programs are supposed to query the valid range using sched_get_priority_(min|max), but of course some programs assume the Linux values are valid. This commit adds a tunable compat.linux.map_sched_prio. When enabled sched_get_priority_(min|max) return the Linux values and sched_setscheduler and sched_(get|set)param translate between FreeBSD and Linux values. Because there are more Linux levels than FreeBSD levels, multiple Linux levels map to a single FreeBSD level, which means pre-emption might not happen as it does on Linux, so the tunable allows to disable this behaviour. It is enabled by default because I think it is unlikely that anyone runs real-time software under Linux emulation on FreeBSD that critically relies on correct pre-emption. This fixes FMOD, a commercial sound library used by several games. PR: 240043 Tested by: Alex S Modified: stable/12/sys/compat/linux/linux_misc.c stable/12/sys/compat/linux/linux_misc.h Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/compat/linux/linux_misc.c ============================================================================== --- stable/12/sys/compat/linux/linux_misc.c Mon Aug 24 12:35:02 2020 (r364663) +++ stable/12/sys/compat/linux/linux_misc.c Mon Aug 24 12:43:54 2020 (r364664) @@ -143,6 +143,11 @@ struct l_pselect6arg { l_size_t ss_len; }; +static bool map_sched_prio = true; +SYSCTL_BOOL(_compat_linux, OID_AUTO, map_sched_prio, CTLFLAG_RDTUN, + &map_sched_prio, 0, "Map scheduler priorities to Linux priorities " + "(not POSIX compliant)"); + static int linux_utimensat_nsec_valid(l_long); @@ -1586,6 +1591,33 @@ linux_sched_setscheduler(struct thread *td, if (error) return (error); + if (map_sched_prio) { + switch (policy) { + case SCHED_OTHER: + if (sched_param.sched_priority != 0) + return (EINVAL); + + sched_param.sched_priority = + PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE; + break; + case SCHED_FIFO: + case SCHED_RR: + if (sched_param.sched_priority < 1 || + sched_param.sched_priority >= LINUX_MAX_RT_PRIO) + return (EINVAL); + + /* + * Map [1, LINUX_MAX_RT_PRIO - 1] to + * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down). + */ + sched_param.sched_priority = + (sched_param.sched_priority - 1) * + (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) / + (LINUX_MAX_RT_PRIO - 1); + break; + } + } + tdt = linux_tdfind(td, args->pid, -1); if (tdt == NULL) return (ESRCH); @@ -1639,6 +1671,20 @@ linux_sched_get_priority_max(struct thread *td, printf(ARGS(sched_get_priority_max, "%d"), args->policy); #endif + if (map_sched_prio) { + switch (args->policy) { + case LINUX_SCHED_OTHER: + td->td_retval[0] = 0; + return (0); + case LINUX_SCHED_FIFO: + case LINUX_SCHED_RR: + td->td_retval[0] = LINUX_MAX_RT_PRIO - 1; + return (0); + default: + return (EINVAL); + } + } + switch (args->policy) { case LINUX_SCHED_OTHER: bsd.policy = SCHED_OTHER; @@ -1666,6 +1712,20 @@ linux_sched_get_priority_min(struct thread *td, printf(ARGS(sched_get_priority_min, "%d"), args->policy); #endif + if (map_sched_prio) { + switch (args->policy) { + case LINUX_SCHED_OTHER: + td->td_retval[0] = 0; + return (0); + case LINUX_SCHED_FIFO: + case LINUX_SCHED_RR: + td->td_retval[0] = 1; + return (0); + default: + return (EINVAL); + } + } + switch (args->policy) { case LINUX_SCHED_OTHER: bsd.policy = SCHED_OTHER; @@ -2123,7 +2183,7 @@ linux_sched_setparam(struct thread *td, { struct sched_param sched_param; struct thread *tdt; - int error; + int error, policy; #ifdef DEBUG if (ldebug(sched_setparam)) @@ -2138,8 +2198,41 @@ linux_sched_setparam(struct thread *td, if (tdt == NULL) return (ESRCH); + if( map_sched_prio ) { + error = kern_sched_getscheduler(td, tdt, &policy); + if (error) + goto out; + + switch (policy) { + case SCHED_OTHER: + if (sched_param.sched_priority != 0) { + error = EINVAL; + goto out; + } + sched_param.sched_priority = + PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE; + break; + case SCHED_FIFO: + case SCHED_RR: + if (sched_param.sched_priority < 1 || + sched_param.sched_priority >= LINUX_MAX_RT_PRIO) { + error = EINVAL; + goto out; + } + /* + * Map [1, LINUX_MAX_RT_PRIO - 1] to + * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down). + */ + sched_param.sched_priority = + (sched_param.sched_priority - 1) * + (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) / + (LINUX_MAX_RT_PRIO - 1); + break; + } + } + error = kern_sched_setparam(td, tdt, &sched_param); - PROC_UNLOCK(tdt->td_proc); +out: PROC_UNLOCK(tdt->td_proc); return (error); } @@ -2149,7 +2242,7 @@ linux_sched_getparam(struct thread *td, { struct sched_param sched_param; struct thread *tdt; - int error; + int error, policy; #ifdef DEBUG if (ldebug(sched_getparam)) @@ -2161,10 +2254,38 @@ linux_sched_getparam(struct thread *td, return (ESRCH); error = kern_sched_getparam(td, tdt, &sched_param); - PROC_UNLOCK(tdt->td_proc); - if (error == 0) - error = copyout(&sched_param, uap->param, - sizeof(sched_param)); + if (error) { + PROC_UNLOCK(tdt->td_proc); + return (error); + } + + if (map_sched_prio) { + error = kern_sched_getscheduler(td, tdt, &policy); + PROC_UNLOCK(tdt->td_proc); + if (error) + return (error); + + switch (policy) { + case SCHED_OTHER: + sched_param.sched_priority = 0; + break; + case SCHED_FIFO: + case SCHED_RR: + /* + * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to + * [1, LINUX_MAX_RT_PRIO - 1] (rounding up). + */ + sched_param.sched_priority = + (sched_param.sched_priority * + (LINUX_MAX_RT_PRIO - 1) + + (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) / + (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1; + break; + } + } else + PROC_UNLOCK(tdt->td_proc); + + error = copyout(&sched_param, uap->param, sizeof(sched_param)); return (error); } Modified: stable/12/sys/compat/linux/linux_misc.h ============================================================================== --- stable/12/sys/compat/linux/linux_misc.h Mon Aug 24 12:35:02 2020 (r364663) +++ stable/12/sys/compat/linux/linux_misc.h Mon Aug 24 12:43:54 2020 (r364664) @@ -105,6 +105,8 @@ extern const char *linux_kplatform; #define LINUX_SCHED_FIFO 1 #define LINUX_SCHED_RR 2 +#define LINUX_MAX_RT_PRIO 100 + struct l_new_utsname { char sysname[LINUX_MAX_UTSNAME]; char nodename[LINUX_MAX_UTSNAME];