Skip to content

Commit

Permalink
Add synchronization logic around sysctl interface
Browse files Browse the repository at this point in the history
This commit fixes the race problem between 2+ concurrent sysctl updates.
It is part of the work related to #204
  • Loading branch information
Adam-pi3 authored and solardiz committed Nov 19, 2022
1 parent 2cd2c2b commit 1fc5312
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/modules/wrap/p_struct_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef P_LKRG_WRAPPER_H
#define P_LKRG_WRAPPER_H

extern struct mutex p_ro_page_mutex;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)

static inline void p_set_uid(kuid_t *p_arg, unsigned int p_val) {
Expand Down Expand Up @@ -455,6 +457,8 @@ static inline void p_lkrg_open_rw(void) {

unsigned long p_flags;

mutex_lock(&p_ro_page_mutex);

// preempt_disable();
barrier();
p_set_memory_rw((unsigned long)P_CTRL_ADDR,1);
Expand All @@ -472,6 +476,8 @@ static inline void p_lkrg_close_rw(void) {
p_set_memory_ro((unsigned long)P_CTRL_ADDR,1);
barrier();
// preempt_enable(); //_no_resched();

mutex_unlock(&p_ro_page_mutex);
}

/* ARM */
Expand Down Expand Up @@ -587,6 +593,8 @@ static inline void p_lkrg_open_rw(void) {

unsigned long p_flags;

mutex_lock(&p_ro_page_mutex);

preempt_disable();
barrier();
p_set_memory_rw((unsigned long)P_CTRL_ADDR,1);
Expand All @@ -604,6 +612,8 @@ static inline void p_lkrg_close_rw(void) {
p_set_memory_ro((unsigned long)P_CTRL_ADDR,1);
barrier();
preempt_enable(); //_no_resched();

mutex_unlock(&p_ro_page_mutex);
}

/* ARM64 */
Expand Down Expand Up @@ -741,6 +751,8 @@ static inline void p_lkrg_open_rw(void) {

unsigned long p_flags;

mutex_lock(&p_ro_page_mutex);

preempt_disable();
barrier();
p_set_memory_rw((unsigned long)P_CTRL_ADDR,1);
Expand All @@ -758,6 +770,8 @@ static inline void p_lkrg_close_rw(void) {
p_set_memory_ro((unsigned long)P_CTRL_ADDR,1);
barrier();
preempt_enable(); //_no_resched();

mutex_unlock(&p_ro_page_mutex);
}

#endif
Expand Down
2 changes: 2 additions & 0 deletions src/p_lkrg_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ static enum cpuhp_state p_hot_cpus;
#endif
unsigned int p_attr_init = 0;

DEFINE_MUTEX(p_ro_page_mutex);

p_ro_page p_ro __p_lkrg_read_only = {

#if !defined(CONFIG_ARM) && (!defined(P_KERNEL_AGGRESSIVE_INLINING) && defined(CONFIG_X86))
Expand Down

0 comments on commit 1fc5312

Please sign in to comment.