8215962: Support ThreadPriorityPolicy mode 1 for non-root users on linux/bsd

Reviewed-by: dcubed, dholmes
This commit is contained in:
Matthias Baesken 2019-01-04 17:46:56 +01:00
parent 839d71f4ca
commit db1518dae2
3 changed files with 15 additions and 15 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -2256,7 +2256,8 @@ void os::naked_yield() {
// not the entire user process, and user level threads are 1:1 mapped to kernel // not the entire user process, and user level threads are 1:1 mapped to kernel
// threads. It has always been the case, but could change in the future. For // threads. It has always been the case, but could change in the future. For
// this reason, the code should not be used as default (ThreadPriorityPolicy=0). // this reason, the code should not be used as default (ThreadPriorityPolicy=0).
// It is only used when ThreadPriorityPolicy=1 and requires root privilege. // It is only used when ThreadPriorityPolicy=1 and may require system level permission
// (e.g., root privilege or CAP_SYS_NICE capability).
#if !defined(__APPLE__) #if !defined(__APPLE__)
int os::java_to_os_priority[CriticalPriority + 1] = { int os::java_to_os_priority[CriticalPriority + 1] = {
@ -2303,14 +2304,12 @@ int os::java_to_os_priority[CriticalPriority + 1] = {
static int prio_init() { static int prio_init() {
if (ThreadPriorityPolicy == 1) { if (ThreadPriorityPolicy == 1) {
// Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1
// if effective uid is not root. Perhaps, a more elegant way of doing
// this is to test CAP_SYS_NICE capability, but that will require libcap.so
if (geteuid() != 0) { if (geteuid() != 0) {
if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) { if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {
warning("-XX:ThreadPriorityPolicy requires root privilege on Bsd"); warning("-XX:ThreadPriorityPolicy=1 may require system level permission, " \
"e.g., being the root user. If the necessary permission is not " \
"possessed, changes to priority will be silently ignored.");
} }
ThreadPriorityPolicy = 0;
} }
} }
if (UseCriticalJavaThreadPriority) { if (UseCriticalJavaThreadPriority) {
@ -2327,6 +2326,7 @@ OSReturn os::set_native_priority(Thread* thread, int newpri) {
return OS_OK; return OS_OK;
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
int ret = pthread_setprio(thread->osthread()->pthread_id(), newpri); int ret = pthread_setprio(thread->osthread()->pthread_id(), newpri);
return (ret == 0) ? OS_OK : OS_ERR;
#elif defined(__APPLE__) || defined(__NetBSD__) #elif defined(__APPLE__) || defined(__NetBSD__)
struct sched_param sp; struct sched_param sp;
int policy; int policy;

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -4076,7 +4076,8 @@ void os::naked_yield() {
// not the entire user process, and user level threads are 1:1 mapped to kernel // not the entire user process, and user level threads are 1:1 mapped to kernel
// threads. It has always been the case, but could change in the future. For // threads. It has always been the case, but could change in the future. For
// this reason, the code should not be used as default (ThreadPriorityPolicy=0). // this reason, the code should not be used as default (ThreadPriorityPolicy=0).
// It is only used when ThreadPriorityPolicy=1 and requires root privilege. // It is only used when ThreadPriorityPolicy=1 and may require system level permission
// (e.g., root privilege or CAP_SYS_NICE capability).
int os::java_to_os_priority[CriticalPriority + 1] = { int os::java_to_os_priority[CriticalPriority + 1] = {
19, // 0 Entry should never be used 19, // 0 Entry should never be used
@ -4100,14 +4101,12 @@ int os::java_to_os_priority[CriticalPriority + 1] = {
static int prio_init() { static int prio_init() {
if (ThreadPriorityPolicy == 1) { if (ThreadPriorityPolicy == 1) {
// Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1
// if effective uid is not root. Perhaps, a more elegant way of doing
// this is to test CAP_SYS_NICE capability, but that will require libcap.so
if (geteuid() != 0) { if (geteuid() != 0) {
if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) { if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {
warning("-XX:ThreadPriorityPolicy requires root privilege on Linux"); warning("-XX:ThreadPriorityPolicy=1 may require system level permission, " \
"e.g., being the root user. If the necessary permission is not " \
"possessed, changes to priority will be silently ignored.");
} }
ThreadPriorityPolicy = 0;
} }
} }
if (UseCriticalJavaThreadPriority) { if (UseCriticalJavaThreadPriority) {

View file

@ -2049,7 +2049,8 @@ define_pd_global(uint64_t,MaxRAM, 1ULL*G);
" to higher native thread priorities. This policy should be "\ " to higher native thread priorities. This policy should be "\
" used with care, as sometimes it can cause performance "\ " used with care, as sometimes it can cause performance "\
" degradation in the application and/or the entire system. On "\ " degradation in the application and/or the entire system. On "\
" Linux this policy requires root privilege.") \ " Linux/BSD/macOS this policy requires root privilege or an "\
" extended capability.") \
range(0, 1) \ range(0, 1) \
\ \
product(bool, ThreadPriorityVerbose, false, \ product(bool, ThreadPriorityVerbose, false, \