Skip to content

Commit 5640014

Browse files
committed
fix: uniqMcs use all cpu
Signed-off-by: ningmingxiao <[email protected]>
1 parent a8faa24 commit 5640014

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

go-selinux/selinux_linux.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ const (
3939

4040
type selinuxState struct {
4141
mcsList map[string]bool
42+
cond *sync.Cond
4243
selinuxfs string
44+
maxMCSCount int
4345
selinuxfsOnce sync.Once
44-
enabledSet bool
45-
enabled bool
46+
mu sync.Mutex
4647
sync.Mutex
48+
enabledSet bool
49+
enabled bool
4750
}
48-
4951
type level struct {
5052
cats *big.Int
5153
sens int
@@ -936,6 +938,18 @@ func mcsAdd(mcs string) error {
936938
if mcs == "" {
937939
return nil
938940
}
941+
state.mu.Lock()
942+
if state.maxMCSCount == 0 {
943+
state.maxMCSCount = int(CategoryRange * (CategoryRange - 1) / 2)
944+
}
945+
if state.cond == nil {
946+
state.cond = sync.NewCond(&state.mu)
947+
}
948+
if len(state.mcsList) >= getMcsListSize() {
949+
state.cond.Wait()
950+
}
951+
state.mu.Unlock()
952+
939953
state.Lock()
940954
defer state.Unlock()
941955
if state.mcsList[mcs] {
@@ -945,13 +959,22 @@ func mcsAdd(mcs string) error {
945959
return nil
946960
}
947961

962+
func getMcsListSize() int {
963+
state.Lock()
964+
defer state.Unlock()
965+
return len(state.mcsList)
966+
}
967+
948968
func mcsDelete(mcs string) {
949969
if mcs == "" {
950970
return
951971
}
952972
state.Lock()
953973
defer state.Unlock()
954974
state.mcsList[mcs] = false
975+
state.mu.Lock()
976+
state.cond.Signal()
977+
state.mu.Unlock()
955978
}
956979

957980
func intToMcs(id int, catRange uint32) string {

0 commit comments

Comments
 (0)