fix #394. this cleans up some handles after using them, and doesn't disable ALL process privileges upon success

This commit is contained in:
Jameson Nash 2014-06-27 12:10:04 -04:00
parent d10db52edb
commit f41f03ab83
6 changed files with 39 additions and 35 deletions

View File

@ -388,6 +388,15 @@ please https://github.com/xianyi/OpenBLAS/issues/246
#include "common_arm64.h" #include "common_arm64.h"
#endif #endif
#ifndef ASSEMBLER
#ifdef OS_WINDOWS
typedef char env_var_t[MAX_PATH];
#define readenv(p, n) GetEnvironmentVariable((n), (p), sizeof(p))
#else
typedef char* env_var_t;
#define readenv(p, n) ((p)=getenv(n))
#endif
#endif
#ifdef OS_LINUX #ifdef OS_LINUX
#include "common_linux.h" #include "common_linux.h"
@ -515,13 +524,9 @@ static __inline void blas_unlock(volatile BLASULONG *address){
*address = 0; *address = 0;
} }
static __inline int readenv(char *env) { static __inline int readenv_atoi(char *env) {
env_var_t p;
char *p; return readenv(p,env) ? 0 : atoi(p);
p = getenv(env);
if (p == NULL) return 0; else return atoi(p);
} }
@ -687,8 +692,8 @@ extern int gotoblas_profile;
#define PRINT_DEBUG_CNAME #define PRINT_DEBUG_CNAME
#define PRINT_DEBUG_NAME #define PRINT_DEBUG_NAME
#else #else
#define PRINT_DEBUG_CNAME if (readenv("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME) #define PRINT_DEBUG_CNAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
#define PRINT_DEBUG_NAME if (readenv("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME) #define PRINT_DEBUG_NAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -533,18 +533,15 @@ int blas_thread_init(void){
if (!blas_server_avail){ if (!blas_server_avail){
char *p; env_var_t p;
p = getenv("THREAD_TIMEOUT"); if (readenv(p,"THREAD_TIMEOUT")) {
if (p) {
thread_timeout = atoi(p); thread_timeout = atoi(p);
if (thread_timeout < 4) thread_timeout = 4; if (thread_timeout < 4) thread_timeout = 4;
if (thread_timeout > 30) thread_timeout = 30; if (thread_timeout > 30) thread_timeout = 30;
thread_timeout = (1 << thread_timeout); thread_timeout = (1 << thread_timeout);
}else{ }else{
p = getenv("GOTO_THREAD_TIMEOUT"); if (readenv(p,"GOTO_THREAD_TIMEOUT")) {
if (p) {
thread_timeout = atoi(p); thread_timeout = atoi(p);
if (thread_timeout < 4) thread_timeout = 4; if (thread_timeout < 4) thread_timeout = 4;
if (thread_timeout > 30) thread_timeout = 30; if (thread_timeout > 30) thread_timeout = 30;

View File

@ -698,11 +698,11 @@ void gotoblas_affinity_init(void) {
#ifdef USE_OPENMP #ifdef USE_OPENMP
numprocs = 0; numprocs = 0;
#else #else
numprocs = readenv("OPENBLAS_NUM_THREADS"); numprocs = readenv_atoi("OPENBLAS_NUM_THREADS");
if (numprocs == 0) numprocs = readenv("GOTO_NUM_THREADS"); if (numprocs == 0) numprocs = readenv_atoi("GOTO_NUM_THREADS");
#endif #endif
if (numprocs == 0) numprocs = readenv("OMP_NUM_THREADS"); if (numprocs == 0) numprocs = readenv_atoi("OMP_NUM_THREADS");
numnodes = 1; numnodes = 1;
@ -793,7 +793,7 @@ void gotoblas_affinity_init(void) {
setup_mempolicy(); setup_mempolicy();
if (readenv("OPENBLAS_MAIN_FREE") || readenv("GOTOBLAS_MAIN_FREE")) { if (readenv_atoi("OPENBLAS_MAIN_FREE") || readenv_atoi("GOTOBLAS_MAIN_FREE")) {
sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]); sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
} }

View File

@ -273,7 +273,7 @@ void openblas_fork_handler()
} }
int blas_get_cpu_number(void){ int blas_get_cpu_number(void){
char *p; env_var_t p;
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN)
int max_num; int max_num;
#endif #endif
@ -288,21 +288,18 @@ int blas_get_cpu_number(void){
blas_goto_num = 0; blas_goto_num = 0;
#ifndef USE_OPENMP #ifndef USE_OPENMP
p = getenv("OPENBLAS_NUM_THREADS"); if (readenv(p,"OPENBLAS_NUM_THREADS")) blas_goto_num = atoi(p);
if (p) blas_goto_num = atoi(p);
if (blas_goto_num < 0) blas_goto_num = 0; if (blas_goto_num < 0) blas_goto_num = 0;
if (blas_goto_num == 0) { if (blas_goto_num == 0) {
p = getenv("GOTO_NUM_THREADS"); if (readenv(p,"GOTO_NUM_THREADS")) blas_goto_num = atoi(p);
if (p) blas_goto_num = atoi(p);
if (blas_goto_num < 0) blas_goto_num = 0; if (blas_goto_num < 0) blas_goto_num = 0;
} }
#endif #endif
blas_omp_num = 0; blas_omp_num = 0;
p = getenv("OMP_NUM_THREADS"); if (readenv(p,"OMP_NUM_THREADS")) blas_omp_num = atoi(p);
if (p) blas_omp_num = atoi(p);
if (blas_omp_num < 0) blas_omp_num = 0; if (blas_omp_num < 0) blas_omp_num = 0;
if (blas_goto_num > 0) blas_num_threads = blas_goto_num; if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
@ -769,16 +766,23 @@ static void *alloc_hugetlb(void *address){
tp.PrivilegeCount = 1; tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) return (void *) -1; if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
CloseHandle(hToken);
return -1;
}
if (AdjustTokenPrivileges(hToken, FALSE, (PTOKEN_PRIVILEGES)&tp, 0, NULL, NULL) != TRUE) return (void *) -1; if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
CloseHandle(hToken);
return -1;
}
map_address = (void *)VirtualAlloc(address, map_address = (void *)VirtualAlloc(address,
BUFFER_SIZE, BUFFER_SIZE,
MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT, MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE); PAGE_READWRITE);
AdjustTokenPrivileges(hToken, TRUE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, NULL); tp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
if (map_address == (void *)NULL) map_address = (void *)-1; if (map_address == (void *)NULL) map_address = (void *)-1;

View File

@ -35,9 +35,8 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
int openblas_verbose() { int openblas_verbose() {
int ret=0; int ret=0;
char *p; env_var_t p;
p = getenv("OPENBLAS_VERBOSE"); if (readenv(p,"OPENBLAS_VERBOSE")) ret = atoi(p);
if (p) ret = atoi(p);
if(ret<0) ret=0; if(ret<0) ret=0;
return ret; return ret;
} }

View File

@ -248,7 +248,7 @@ int get_L2_size(void){
void blas_set_parameter(void){ void blas_set_parameter(void){
char *p; env_var_t p;
int factor; int factor;
int size = get_L2_size(); int size = get_L2_size();
@ -463,9 +463,8 @@ void blas_set_parameter(void){
#endif #endif
#endif #endif
p = getenv("GOTO_BLOCK_FACTOR");
if (p) { if (readenv(p,"GOTO_BLOCK_FACTOR")) {
factor = atoi(p); factor = atoi(p);
if (factor < 10) factor = 10; if (factor < 10) factor = 10;
if (factor > 200) factor = 200; if (factor > 200) factor = 200;