• Yisheng Xie's avatar
    mlock: fix mlock count can not decrease in race condition · 1163e785
    Yisheng Xie authored
    commit 70feee0e upstream.
    
    Kefeng reported that when running the follow test, the mlock count in
    meminfo will increase permanently:
    
     [1] testcase
     linux:~ # cat test_mlockal
     grep Mlocked /proc/meminfo
      for j in `seq 0 10`
      do
     	for i in `seq 4 15`
     	do
     		./p_mlockall >> log &
     	done
     	sleep 0.2
     done
     # wait some time to let mlock counter decrease and 5s may not enough
     sleep 5
     grep Mlocked /proc/meminfo
    
     linux:~ # cat p_mlockall.c
     #include <sys/mman.h>
     #include <stdlib.h>
     #include <stdio.h>
    
     #define SPACE_LEN	4096
    
     int main(int argc, char ** argv)
     {
    	 	int ret;
    	 	void *adr = malloc(SPACE_LEN);
    	 	if (!adr)
    	 		return -1;
    
    	 	ret = mlockall(MCL_CURRENT | MCL_FUTURE);
    	 	printf("mlcokall ret = %d\n", ret);
    
    	 	ret = munlockall();
    	 	printf("munlcokall ret = %d\n", ret);
    
    	 	free(adr);
    	 	return 0;
    	 }
    
    In __munlock_pagevec() we should decrement NR_MLOCK for each page where
    we clear the PageMlocked flag. ...
    1163e785
mlock.c 22.5 KB