Advertisement
Guest User

Untitled

a guest
Jan 20th, 2025
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1.        int i, offset, max_scan, pid, last = last_pid;
  2.         pidmap_t *map;
  3.  
  4.  
  5.         pid = last + 1;
  6.         if (pid >= pid_max)
  7.                 pid = RESERVED_PIDS;
  8.         offset = pid & BITS_PER_PAGE_MASK;
  9.         map = &pidmap_array[pid/BITS_PER_PAGE];
  10.         max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
  11.         for (i = 0; i <= max_scan; ++i) {
  12.                 if (unlikely(!map->page)) {
  13.                         unsigned long page = get_zeroed_page(GFP_KERNEL);
  14.                         /*
  15.                          * Free the page if someone raced with us
  16.                          * installing it:
  17.                          */
  18.                         spin_lock(&pidmap_lock);
  19.                         if (map->page)
  20.                                 free_page(page);
  21.                         else
  22.                                 map->page = (void *)page;
  23.                         spin_unlock(&pidmap_lock);
  24.                         if (unlikely(!map->page))
  25.                                 break;
  26.                 }
  27.                 if (likely(atomic_read(&map->nr_free))) {
  28.                         do {
  29.                                 if (!test_and_set_bit(offset, map->page)) {
  30.                                         atomic_dec(&map->nr_free);
  31.                                         last_pid = pid;
  32.                                         return pid;
  33.                                 }
  34.                                 offset = find_next_offset(map, offset);
  35.                                 pid = mk_pid(map, offset);
  36.                         /*
  37.                          * find_next_offset() found a bit, the pid from it
  38.                          * is in-bounds, and if we fell back to the last
  39.                          * bitmap block and the final block was the same
  40.                          * as the starting point, pid is before last_pid.
  41.                          */
  42.                         } while (offset < BITS_PER_PAGE && pid < pid_max &&
  43.                                         (i != max_scan || pid < last ||
  44.                                             !((last+1) & BITS_PER_PAGE_MASK)));
  45.                 }
  46.                 if (map < &pidmap_array[(pid_max-1)/BITS_PER_PAGE]) {
  47.                         ++map;
  48.                         offset = 0;
  49.                 } else {
  50.                         map = &pidmap_array[0];
  51.                         offset = RESERVED_PIDS;
  52.                         if (unlikely(last == offset))
  53.                                 break;
  54.                 }
  55.                 pid = mk_pid(map, offset);
  56.         }
  57.         return -1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement