ia64/linux-2.6.18-xen.hg

annotate kernel/pid.c @ 798:b02a90bf5bbc

ACPI: Backport missing part for T-State MSR support

Part of below kernel commit was missed while packporting T-State
support.

commit f79f06ab9f86d7203006d2ec8992ac80df36a34e
Author: Zhao Yakui <yakui.zhao@intel.com>
Date: Thu Nov 15 17:06:36 2007 +0800

ACPI: Enable MSR (FixedHW) support for T-States

Add throttling control via MSR when T-states uses
the FixHW Control Status registers.

Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Signed-off-by: Li Shaohua <shaohua.li@intel.com>
Signed-off-by: Len Brown <len.brown@intel.com>

Signed-off-by: Wei Gang <gang.wei@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Mar 02 10:53:59 2009 +0000 (2009-03-02)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * Generic pidhash and scalable, time-bounded PID allocator
ian@0 3 *
ian@0 4 * (C) 2002-2003 William Irwin, IBM
ian@0 5 * (C) 2004 William Irwin, Oracle
ian@0 6 * (C) 2002-2004 Ingo Molnar, Red Hat
ian@0 7 *
ian@0 8 * pid-structures are backing objects for tasks sharing a given ID to chain
ian@0 9 * against. There is very little to them aside from hashing them and
ian@0 10 * parking tasks using given ID's on a list.
ian@0 11 *
ian@0 12 * The hash is always changed with the tasklist_lock write-acquired,
ian@0 13 * and the hash is only accessed with the tasklist_lock at least
ian@0 14 * read-acquired, so there's no additional SMP locking needed here.
ian@0 15 *
ian@0 16 * We have a list of bitmap pages, which bitmaps represent the PID space.
ian@0 17 * Allocating and freeing PIDs is completely lockless. The worst-case
ian@0 18 * allocation scenario when all but one out of 1 million PIDs possible are
ian@0 19 * allocated already: the scanning of 32 list entries and at most PAGE_SIZE
ian@0 20 * bytes. The typical fastpath is a single successful setbit. Freeing is O(1).
ian@0 21 */
ian@0 22
ian@0 23 #include <linux/mm.h>
ian@0 24 #include <linux/module.h>
ian@0 25 #include <linux/slab.h>
ian@0 26 #include <linux/init.h>
ian@0 27 #include <linux/bootmem.h>
ian@0 28 #include <linux/hash.h>
ian@0 29
ian@0 30 #define pid_hashfn(nr) hash_long((unsigned long)nr, pidhash_shift)
ian@0 31 static struct hlist_head *pid_hash;
ian@0 32 static int pidhash_shift;
ian@0 33 static kmem_cache_t *pid_cachep;
ian@0 34
ian@0 35 int pid_max = PID_MAX_DEFAULT;
ian@0 36 int last_pid;
ian@0 37
ian@0 38 #define RESERVED_PIDS 300
ian@0 39
ian@0 40 int pid_max_min = RESERVED_PIDS + 1;
ian@0 41 int pid_max_max = PID_MAX_LIMIT;
ian@0 42
ian@0 43 #define PIDMAP_ENTRIES ((PID_MAX_LIMIT + 8*PAGE_SIZE - 1)/PAGE_SIZE/8)
ian@0 44 #define BITS_PER_PAGE (PAGE_SIZE*8)
ian@0 45 #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
ian@0 46 #define mk_pid(map, off) (((map) - pidmap_array)*BITS_PER_PAGE + (off))
ian@0 47 #define find_next_offset(map, off) \
ian@0 48 find_next_zero_bit((map)->page, BITS_PER_PAGE, off)
ian@0 49
ian@0 50 /*
ian@0 51 * PID-map pages start out as NULL, they get allocated upon
ian@0 52 * first use and are never deallocated. This way a low pid_max
ian@0 53 * value does not cause lots of bitmaps to be allocated, but
ian@0 54 * the scheme scales to up to 4 million PIDs, runtime.
ian@0 55 */
ian@0 56 typedef struct pidmap {
ian@0 57 atomic_t nr_free;
ian@0 58 void *page;
ian@0 59 } pidmap_t;
ian@0 60
ian@0 61 static pidmap_t pidmap_array[PIDMAP_ENTRIES] =
ian@0 62 { [ 0 ... PIDMAP_ENTRIES-1 ] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } };
ian@0 63
ian@0 64 /*
ian@0 65 * Note: disable interrupts while the pidmap_lock is held as an
ian@0 66 * interrupt might come in and do read_lock(&tasklist_lock).
ian@0 67 *
ian@0 68 * If we don't disable interrupts there is a nasty deadlock between
ian@0 69 * detach_pid()->free_pid() and another cpu that does
ian@0 70 * spin_lock(&pidmap_lock) followed by an interrupt routine that does
ian@0 71 * read_lock(&tasklist_lock);
ian@0 72 *
ian@0 73 * After we clean up the tasklist_lock and know there are no
ian@0 74 * irq handlers that take it we can leave the interrupts enabled.
ian@0 75 * For now it is easier to be safe than to prove it can't happen.
ian@0 76 */
ian@0 77 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
ian@0 78
ian@0 79 static fastcall void free_pidmap(int pid)
ian@0 80 {
ian@0 81 pidmap_t *map = pidmap_array + pid / BITS_PER_PAGE;
ian@0 82 int offset = pid & BITS_PER_PAGE_MASK;
ian@0 83
ian@0 84 clear_bit(offset, map->page);
ian@0 85 atomic_inc(&map->nr_free);
ian@0 86 }
ian@0 87
ian@0 88 static int alloc_pidmap(void)
ian@0 89 {
ian@0 90 int i, offset, max_scan, pid, last = last_pid;
ian@0 91 pidmap_t *map;
ian@0 92
ian@0 93 pid = last + 1;
ian@0 94 if (pid >= pid_max)
ian@0 95 pid = RESERVED_PIDS;
ian@0 96 offset = pid & BITS_PER_PAGE_MASK;
ian@0 97 map = &pidmap_array[pid/BITS_PER_PAGE];
ian@0 98 max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
ian@0 99 for (i = 0; i <= max_scan; ++i) {
ian@0 100 if (unlikely(!map->page)) {
ian@0 101 unsigned long page = get_zeroed_page(GFP_KERNEL);
ian@0 102 /*
ian@0 103 * Free the page if someone raced with us
ian@0 104 * installing it:
ian@0 105 */
ian@0 106 spin_lock_irq(&pidmap_lock);
ian@0 107 if (map->page)
ian@0 108 free_page(page);
ian@0 109 else
ian@0 110 map->page = (void *)page;
ian@0 111 spin_unlock_irq(&pidmap_lock);
ian@0 112 if (unlikely(!map->page))
ian@0 113 break;
ian@0 114 }
ian@0 115 if (likely(atomic_read(&map->nr_free))) {
ian@0 116 do {
ian@0 117 if (!test_and_set_bit(offset, map->page)) {
ian@0 118 atomic_dec(&map->nr_free);
ian@0 119 last_pid = pid;
ian@0 120 return pid;
ian@0 121 }
ian@0 122 offset = find_next_offset(map, offset);
ian@0 123 pid = mk_pid(map, offset);
ian@0 124 /*
ian@0 125 * find_next_offset() found a bit, the pid from it
ian@0 126 * is in-bounds, and if we fell back to the last
ian@0 127 * bitmap block and the final block was the same
ian@0 128 * as the starting point, pid is before last_pid.
ian@0 129 */
ian@0 130 } while (offset < BITS_PER_PAGE && pid < pid_max &&
ian@0 131 (i != max_scan || pid < last ||
ian@0 132 !((last+1) & BITS_PER_PAGE_MASK)));
ian@0 133 }
ian@0 134 if (map < &pidmap_array[(pid_max-1)/BITS_PER_PAGE]) {
ian@0 135 ++map;
ian@0 136 offset = 0;
ian@0 137 } else {
ian@0 138 map = &pidmap_array[0];
ian@0 139 offset = RESERVED_PIDS;
ian@0 140 if (unlikely(last == offset))
ian@0 141 break;
ian@0 142 }
ian@0 143 pid = mk_pid(map, offset);
ian@0 144 }
ian@0 145 return -1;
ian@0 146 }
ian@0 147
ian@0 148 fastcall void put_pid(struct pid *pid)
ian@0 149 {
ian@0 150 if (!pid)
ian@0 151 return;
ian@0 152 if ((atomic_read(&pid->count) == 1) ||
ian@0 153 atomic_dec_and_test(&pid->count))
ian@0 154 kmem_cache_free(pid_cachep, pid);
ian@0 155 }
ian@0 156
ian@0 157 static void delayed_put_pid(struct rcu_head *rhp)
ian@0 158 {
ian@0 159 struct pid *pid = container_of(rhp, struct pid, rcu);
ian@0 160 put_pid(pid);
ian@0 161 }
ian@0 162
ian@0 163 fastcall void free_pid(struct pid *pid)
ian@0 164 {
ian@0 165 /* We can be called with write_lock_irq(&tasklist_lock) held */
ian@0 166 unsigned long flags;
ian@0 167
ian@0 168 spin_lock_irqsave(&pidmap_lock, flags);
ian@0 169 hlist_del_rcu(&pid->pid_chain);
ian@0 170 spin_unlock_irqrestore(&pidmap_lock, flags);
ian@0 171
ian@0 172 free_pidmap(pid->nr);
ian@0 173 call_rcu(&pid->rcu, delayed_put_pid);
ian@0 174 }
ian@0 175
ian@0 176 struct pid *alloc_pid(void)
ian@0 177 {
ian@0 178 struct pid *pid;
ian@0 179 enum pid_type type;
ian@0 180 int nr = -1;
ian@0 181
ian@0 182 pid = kmem_cache_alloc(pid_cachep, GFP_KERNEL);
ian@0 183 if (!pid)
ian@0 184 goto out;
ian@0 185
ian@0 186 nr = alloc_pidmap();
ian@0 187 if (nr < 0)
ian@0 188 goto out_free;
ian@0 189
ian@0 190 atomic_set(&pid->count, 1);
ian@0 191 pid->nr = nr;
ian@0 192 for (type = 0; type < PIDTYPE_MAX; ++type)
ian@0 193 INIT_HLIST_HEAD(&pid->tasks[type]);
ian@0 194
ian@0 195 spin_lock_irq(&pidmap_lock);
ian@0 196 hlist_add_head_rcu(&pid->pid_chain, &pid_hash[pid_hashfn(pid->nr)]);
ian@0 197 spin_unlock_irq(&pidmap_lock);
ian@0 198
ian@0 199 out:
ian@0 200 return pid;
ian@0 201
ian@0 202 out_free:
ian@0 203 kmem_cache_free(pid_cachep, pid);
ian@0 204 pid = NULL;
ian@0 205 goto out;
ian@0 206 }
ian@0 207
ian@0 208 struct pid * fastcall find_pid(int nr)
ian@0 209 {
ian@0 210 struct hlist_node *elem;
ian@0 211 struct pid *pid;
ian@0 212
ian@0 213 hlist_for_each_entry_rcu(pid, elem,
ian@0 214 &pid_hash[pid_hashfn(nr)], pid_chain) {
ian@0 215 if (pid->nr == nr)
ian@0 216 return pid;
ian@0 217 }
ian@0 218 return NULL;
ian@0 219 }
ian@0 220
ian@0 221 int fastcall attach_pid(struct task_struct *task, enum pid_type type, int nr)
ian@0 222 {
ian@0 223 struct pid_link *link;
ian@0 224 struct pid *pid;
ian@0 225
ian@0 226 WARN_ON(!task->pid); /* to be removed soon */
ian@0 227 WARN_ON(!nr); /* to be removed soon */
ian@0 228
ian@0 229 link = &task->pids[type];
ian@0 230 link->pid = pid = find_pid(nr);
ian@0 231 hlist_add_head_rcu(&link->node, &pid->tasks[type]);
ian@0 232
ian@0 233 return 0;
ian@0 234 }
ian@0 235
ian@0 236 void fastcall detach_pid(struct task_struct *task, enum pid_type type)
ian@0 237 {
ian@0 238 struct pid_link *link;
ian@0 239 struct pid *pid;
ian@0 240 int tmp;
ian@0 241
ian@0 242 link = &task->pids[type];
ian@0 243 pid = link->pid;
ian@0 244
ian@0 245 hlist_del_rcu(&link->node);
ian@0 246 link->pid = NULL;
ian@0 247
ian@0 248 for (tmp = PIDTYPE_MAX; --tmp >= 0; )
ian@0 249 if (!hlist_empty(&pid->tasks[tmp]))
ian@0 250 return;
ian@0 251
ian@0 252 free_pid(pid);
ian@0 253 }
ian@0 254
ian@0 255 struct task_struct * fastcall pid_task(struct pid *pid, enum pid_type type)
ian@0 256 {
ian@0 257 struct task_struct *result = NULL;
ian@0 258 if (pid) {
ian@0 259 struct hlist_node *first;
ian@0 260 first = rcu_dereference(pid->tasks[type].first);
ian@0 261 if (first)
ian@0 262 result = hlist_entry(first, struct task_struct, pids[(type)].node);
ian@0 263 }
ian@0 264 return result;
ian@0 265 }
ian@0 266
ian@0 267 /*
ian@0 268 * Must be called under rcu_read_lock() or with tasklist_lock read-held.
ian@0 269 */
ian@0 270 struct task_struct *find_task_by_pid_type(int type, int nr)
ian@0 271 {
ian@0 272 return pid_task(find_pid(nr), type);
ian@0 273 }
ian@0 274
ian@0 275 EXPORT_SYMBOL(find_task_by_pid_type);
ian@0 276
ian@0 277 struct task_struct *fastcall get_pid_task(struct pid *pid, enum pid_type type)
ian@0 278 {
ian@0 279 struct task_struct *result;
ian@0 280 rcu_read_lock();
ian@0 281 result = pid_task(pid, type);
ian@0 282 if (result)
ian@0 283 get_task_struct(result);
ian@0 284 rcu_read_unlock();
ian@0 285 return result;
ian@0 286 }
ian@0 287
ian@0 288 struct pid *find_get_pid(pid_t nr)
ian@0 289 {
ian@0 290 struct pid *pid;
ian@0 291
ian@0 292 rcu_read_lock();
ian@0 293 pid = get_pid(find_pid(nr));
ian@0 294 rcu_read_unlock();
ian@0 295
ian@0 296 return pid;
ian@0 297 }
ian@0 298
ian@0 299 /*
ian@0 300 * The pid hash table is scaled according to the amount of memory in the
ian@0 301 * machine. From a minimum of 16 slots up to 4096 slots at one gigabyte or
ian@0 302 * more.
ian@0 303 */
ian@0 304 void __init pidhash_init(void)
ian@0 305 {
ian@0 306 int i, pidhash_size;
ian@0 307 unsigned long megabytes = nr_kernel_pages >> (20 - PAGE_SHIFT);
ian@0 308
ian@0 309 pidhash_shift = max(4, fls(megabytes * 4));
ian@0 310 pidhash_shift = min(12, pidhash_shift);
ian@0 311 pidhash_size = 1 << pidhash_shift;
ian@0 312
ian@0 313 printk("PID hash table entries: %d (order: %d, %Zd bytes)\n",
ian@0 314 pidhash_size, pidhash_shift,
ian@0 315 pidhash_size * sizeof(struct hlist_head));
ian@0 316
ian@0 317 pid_hash = alloc_bootmem(pidhash_size * sizeof(*(pid_hash)));
ian@0 318 if (!pid_hash)
ian@0 319 panic("Could not alloc pidhash!\n");
ian@0 320 for (i = 0; i < pidhash_size; i++)
ian@0 321 INIT_HLIST_HEAD(&pid_hash[i]);
ian@0 322 }
ian@0 323
ian@0 324 void __init pidmap_init(void)
ian@0 325 {
ian@0 326 pidmap_array->page = (void *)get_zeroed_page(GFP_KERNEL);
ian@0 327 /* Reserve PID 0. We never call free_pidmap(0) */
ian@0 328 set_bit(0, pidmap_array->page);
ian@0 329 atomic_dec(&pidmap_array->nr_free);
ian@0 330
ian@0 331 pid_cachep = kmem_cache_create("pid", sizeof(struct pid),
ian@0 332 __alignof__(struct pid),
ian@0 333 SLAB_PANIC, NULL, NULL);
ian@0 334 }