Skip to content

Commit 6689a04

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

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

go-selinux/selinux_linux.go

Lines changed: 24 additions & 5 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
@@ -937,11 +939,18 @@ func mcsAdd(mcs string) error {
937939
return nil
938940
}
939941
state.Lock()
940-
defer state.Unlock()
942+
state.mu.Lock()
943+
if len(state.mcsList) >= state.maxMCSCount {
944+
state.Unlock()
945+
state.cond.Wait()
946+
}
947+
state.mu.Unlock()
941948
if state.mcsList[mcs] {
949+
state.Unlock()
942950
return ErrMCSAlreadyExists
943951
}
944952
state.mcsList[mcs] = true
953+
state.Unlock()
945954
return nil
946955
}
947956

@@ -952,6 +961,9 @@ func mcsDelete(mcs string) {
952961
state.Lock()
953962
defer state.Unlock()
954963
state.mcsList[mcs] = false
964+
state.mu.Lock()
965+
state.cond.Signal()
966+
state.mu.Unlock()
955967
}
956968

957969
func intToMcs(id int, catRange uint32) string {
@@ -980,7 +992,6 @@ func uniqMcs(catRange uint32) string {
980992
c1, c2 uint32
981993
mcs string
982994
)
983-
984995
for {
985996
_ = binary.Read(rand.Reader, binary.LittleEndian, &n)
986997
c1 = n % catRange
@@ -991,6 +1002,14 @@ func uniqMcs(catRange uint32) string {
9911002
} else if c1 > c2 {
9921003
c1, c2 = c2, c1
9931004
}
1005+
state.Lock()
1006+
if state.maxMCSCount == 0 {
1007+
state.maxMCSCount = int(catRange * (catRange - 1) / 2)
1008+
}
1009+
if state.cond == nil {
1010+
state.cond = sync.NewCond(&state.mu)
1011+
}
1012+
state.Unlock()
9941013
mcs = fmt.Sprintf("s0:c%d,c%d", c1, c2)
9951014
if err := mcsAdd(mcs); err != nil {
9961015
continue

0 commit comments

Comments
 (0)