Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pids: alloc_pidmap: remove the unnecessary boundary checks

alloc_pidmap() calculates max_scan so that if the initial offset != 0 we
inspect the first map->page twice. This is correct, we want to find the
unused bits < offset in this bitmap block. Add the comment.

But it doesn't make any sense to stop the find_next_offset() loop when we
are looking into this map->page for the second time. We have already
already checked the bits >= offset during the first attempt, it is fine to
do this again, no matter if we succeed this time or not.

Remove this hard-to-understand code. It optimizes the very unlikely case
when we are going to fail, but slows down the more likely case.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Cc: Salman Qazi <sqazi@google.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Sukadev Bhattiprolu <sukadev@us.ibm.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Oleg Nesterov and committed by
Linus Torvalds
c52b0b91 5fdee8c4

+7 -10
+7 -10
kernel/pid.c
··· 169 169 pid = RESERVED_PIDS; 170 170 offset = pid & BITS_PER_PAGE_MASK; 171 171 map = &pid_ns->pidmap[pid/BITS_PER_PAGE]; 172 - max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset; 172 + /* 173 + * If last_pid points into the middle of the map->page we 174 + * want to scan this bitmap block twice, the second time 175 + * we start with offset == 0 (or RESERVED_PIDS). 176 + */ 177 + max_scan = DIV_ROUND_UP(pid_max, BITS_PER_PAGE) - !offset; 173 178 for (i = 0; i <= max_scan; ++i) { 174 179 if (unlikely(!map->page)) { 175 180 void *page = kzalloc(PAGE_SIZE, GFP_KERNEL); ··· 201 196 } 202 197 offset = find_next_offset(map, offset); 203 198 pid = mk_pid(pid_ns, map, offset); 204 - /* 205 - * find_next_offset() found a bit, the pid from it 206 - * is in-bounds, and if we fell back to the last 207 - * bitmap block and the final block was the same 208 - * as the starting point, pid is before last_pid. 209 - */ 210 - } while (offset < BITS_PER_PAGE && pid < pid_max && 211 - (i != max_scan || pid < last || 212 - !((last+1) & BITS_PER_PAGE_MASK))); 199 + } while (offset < BITS_PER_PAGE && pid < pid_max); 213 200 } 214 201 if (map < &pid_ns->pidmap[(pid_max-1)/BITS_PER_PAGE]) { 215 202 ++map;