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

cpuset: add helpers for cpus read and cpuset_mutex locks

cpuset: add helpers for cpus_read_lock and cpuset_mutex locks.

Replace repetitive locking patterns with new helpers:
- cpuset_full_lock()
- cpuset_full_unlock()

This makes the code cleaner and ensures consistent lock ordering.

Signed-off-by: Chen Ridong <chenridong@huawei.com>
Reviewed-by: Waiman Long <longman@redhat.com>
Signed-off-by: Tejun Heo <tj@kernel.org>

authored by

Chen Ridong and committed by
Tejun Heo
2c98144f ada00d51

+40 -34
+2
kernel/cgroup/cpuset-internal.h
··· 276 276 ssize_t cpuset_write_resmask(struct kernfs_open_file *of, 277 277 char *buf, size_t nbytes, loff_t off); 278 278 int cpuset_common_seq_show(struct seq_file *sf, void *v); 279 + void cpuset_full_lock(void); 280 + void cpuset_full_unlock(void); 279 281 280 282 /* 281 283 * cpuset-v1.c
+4 -8
kernel/cgroup/cpuset-v1.c
··· 169 169 cpuset_filetype_t type = cft->private; 170 170 int retval = -ENODEV; 171 171 172 - cpus_read_lock(); 173 - cpuset_lock(); 172 + cpuset_full_lock(); 174 173 if (!is_cpuset_online(cs)) 175 174 goto out_unlock; 176 175 ··· 183 184 break; 184 185 } 185 186 out_unlock: 186 - cpuset_unlock(); 187 - cpus_read_unlock(); 187 + cpuset_full_unlock(); 188 188 return retval; 189 189 } 190 190 ··· 452 454 cpuset_filetype_t type = cft->private; 453 455 int retval = 0; 454 456 455 - cpus_read_lock(); 456 - cpuset_lock(); 457 + cpuset_full_lock(); 457 458 if (!is_cpuset_online(cs)) { 458 459 retval = -ENODEV; 459 460 goto out_unlock; ··· 495 498 break; 496 499 } 497 500 out_unlock: 498 - cpuset_unlock(); 499 - cpus_read_unlock(); 501 + cpuset_full_unlock(); 500 502 return retval; 501 503 } 502 504
+34 -26
kernel/cgroup/cpuset.c
··· 250 250 251 251 static DEFINE_MUTEX(cpuset_mutex); 252 252 253 + /** 254 + * cpuset_lock - Acquire the global cpuset mutex 255 + * 256 + * This locks the global cpuset mutex to prevent modifications to cpuset 257 + * hierarchy and configurations. This helper is not enough to make modification. 258 + */ 253 259 void cpuset_lock(void) 254 260 { 255 261 mutex_lock(&cpuset_mutex); ··· 264 258 void cpuset_unlock(void) 265 259 { 266 260 mutex_unlock(&cpuset_mutex); 261 + } 262 + 263 + /** 264 + * cpuset_full_lock - Acquire full protection for cpuset modification 265 + * 266 + * Takes both CPU hotplug read lock (cpus_read_lock()) and cpuset mutex 267 + * to safely modify cpuset data. 268 + */ 269 + void cpuset_full_lock(void) 270 + { 271 + cpus_read_lock(); 272 + mutex_lock(&cpuset_mutex); 273 + } 274 + 275 + void cpuset_full_unlock(void) 276 + { 277 + mutex_unlock(&cpuset_mutex); 278 + cpus_read_unlock(); 267 279 } 268 280 269 281 static DEFINE_SPINLOCK(callback_lock); ··· 3258 3234 int retval = -ENODEV; 3259 3235 3260 3236 buf = strstrip(buf); 3261 - cpus_read_lock(); 3262 - mutex_lock(&cpuset_mutex); 3237 + cpuset_full_lock(); 3263 3238 if (!is_cpuset_online(cs)) 3264 3239 goto out_unlock; 3265 3240 ··· 3287 3264 if (force_sd_rebuild) 3288 3265 rebuild_sched_domains_locked(); 3289 3266 out_unlock: 3290 - mutex_unlock(&cpuset_mutex); 3291 - cpus_read_unlock(); 3267 + cpuset_full_unlock(); 3292 3268 flush_workqueue(cpuset_migrate_mm_wq); 3293 3269 return retval ?: nbytes; 3294 3270 } ··· 3390 3368 else 3391 3369 return -EINVAL; 3392 3370 3393 - cpus_read_lock(); 3394 - mutex_lock(&cpuset_mutex); 3371 + cpuset_full_lock(); 3395 3372 if (is_cpuset_online(cs)) 3396 3373 retval = update_prstate(cs, val); 3397 - mutex_unlock(&cpuset_mutex); 3398 - cpus_read_unlock(); 3374 + cpuset_full_unlock(); 3399 3375 return retval ?: nbytes; 3400 3376 } 3401 3377 ··· 3518 3498 if (!parent) 3519 3499 return 0; 3520 3500 3521 - cpus_read_lock(); 3522 - mutex_lock(&cpuset_mutex); 3523 - 3501 + cpuset_full_lock(); 3524 3502 if (is_spread_page(parent)) 3525 3503 set_bit(CS_SPREAD_PAGE, &cs->flags); 3526 3504 if (is_spread_slab(parent)) ··· 3570 3552 cpumask_copy(cs->effective_cpus, parent->cpus_allowed); 3571 3553 spin_unlock_irq(&callback_lock); 3572 3554 out_unlock: 3573 - mutex_unlock(&cpuset_mutex); 3574 - cpus_read_unlock(); 3555 + cpuset_full_unlock(); 3575 3556 return 0; 3576 3557 } 3577 3558 ··· 3585 3568 { 3586 3569 struct cpuset *cs = css_cs(css); 3587 3570 3588 - cpus_read_lock(); 3589 - mutex_lock(&cpuset_mutex); 3590 - 3571 + cpuset_full_lock(); 3591 3572 if (!cpuset_v2() && is_sched_load_balance(cs)) 3592 3573 cpuset_update_flag(CS_SCHED_LOAD_BALANCE, cs, 0); 3593 3574 3594 3575 cpuset_dec(); 3595 - 3596 - mutex_unlock(&cpuset_mutex); 3597 - cpus_read_unlock(); 3576 + cpuset_full_unlock(); 3598 3577 } 3599 3578 3600 3579 /* ··· 3602 3589 { 3603 3590 struct cpuset *cs = css_cs(css); 3604 3591 3605 - cpus_read_lock(); 3606 - mutex_lock(&cpuset_mutex); 3607 - 3592 + cpuset_full_lock(); 3608 3593 /* Reset valid partition back to member */ 3609 3594 if (is_partition_valid(cs)) 3610 3595 update_prstate(cs, PRS_MEMBER); 3611 - 3612 - mutex_unlock(&cpuset_mutex); 3613 - cpus_read_unlock(); 3614 - 3596 + cpuset_full_unlock(); 3615 3597 } 3616 3598 3617 3599 static void cpuset_css_free(struct cgroup_subsys_state *css)