1 /* ckrmutils.c - Utility functions for CKRM
3 * Copyright (C) Chandra Seetharaman, IBM Corp. 2003
4 * (C) Hubertus Franke , IBM Corp. 2004
6 * Provides simple utility functions for the core module, CE and resource
9 * Latest version, more details at http://ckrm.sf.net
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
25 #include <linux/err.h>
26 #include <linux/mount.h>
27 #include <linux/module.h>
28 #include <linux/ckrm_rc.h>
30 int get_exe_path_name(struct task_struct *tsk, char *buf, int buflen)
32 struct vm_area_struct *vma;
34 struct mm_struct *mm = get_task_mm(tsk);
35 struct dentry *dentry;
44 down_read(&mm->mmap_sem);
47 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
48 dentry = dget(vma->vm_file->f_dentry);
49 mnt = mntget(vma->vm_file->f_vfsmnt);
50 lname = d_path(dentry, mnt, buf, buflen);
52 strncpy(buf, lname, strlen(lname) + 1);
54 rc = (int)PTR_ERR(lname);
62 up_read(&mm->mmap_sem);
68 * must be called with cnt_lock of parres held
69 * Caller is responsible for making sure that the new guarantee doesn't
70 * overflow parent's total guarantee.
72 void child_guarantee_changed(struct ckrm_shares *parent, int cur, int new)
74 if (new == cur || !parent) {
77 if (new != CKRM_SHARE_DONTCARE) {
78 parent->unused_guarantee -= new;
80 if (cur != CKRM_SHARE_DONTCARE) {
81 parent->unused_guarantee += cur;
87 * must be called with cnt_lock of parres held
88 * Caller is responsible for making sure that the new limit is not more
89 * than parent's max_limit
91 void child_maxlimit_changed(struct ckrm_shares *parent, int new_limit)
93 if (parent && parent->cur_max_limit < new_limit) {
94 parent->cur_max_limit = new_limit;
101 * Caller is responsible for holding any lock to protect the data
102 * structures passed to this function
105 set_shares(struct ckrm_shares *new, struct ckrm_shares *cur,
106 struct ckrm_shares *par)
109 int cur_usage_guar = cur->total_guarantee - cur->unused_guarantee;
110 int increase_by = new->my_guarantee - cur->my_guarantee;
112 // Check total_guarantee for correctness
113 if (new->total_guarantee <= CKRM_SHARE_DONTCARE) {
114 printk(KERN_ERR "new->total_guarantee %d <= CKRM_SHARE_DONTCARE\n",
115 new->total_guarantee);
117 } else if (new->total_guarantee == CKRM_SHARE_UNCHANGED) {
119 } else if (cur_usage_guar > new->total_guarantee) {
120 printk(KERN_ERR "cur_usage_guar %d > new->total_guarantee %d\n",
121 cur_usage_guar,new->total_guarantee);
124 // Check max_limit for correctness
125 if (new->max_limit <= CKRM_SHARE_DONTCARE) {
126 printk(KERN_ERR "new->max_limit %d <= CKRM_SHARE_DONTCARE\n",
129 } else if (new->max_limit == CKRM_SHARE_UNCHANGED) {
131 } else if (cur->cur_max_limit > new->max_limit) {
132 printk(KERN_ERR "cur->cur_max_limit %d > new->max_limit %d\n",
133 cur->cur_max_limit, new->max_limit);
136 // Check my_guarantee for correctness
137 if (new->my_guarantee == CKRM_SHARE_UNCHANGED) {
139 } else if (new->my_guarantee == CKRM_SHARE_DONTCARE) {
141 } else if (par && increase_by > par->unused_guarantee) {
142 printk(KERN_ERR "increase_by %d > par->unused_guarantee %d\n",
143 increase_by, par->unused_guarantee);
146 // Check my_limit for correctness
147 if (new->my_limit == CKRM_SHARE_UNCHANGED) {
149 } else if (new->my_limit == CKRM_SHARE_DONTCARE) {
151 } else if (par && new->my_limit > par->max_limit) {
152 // I can't get more limit than my parent's limit
153 printk(KERN_ERR "new->my_limit %d > par->max_limit %d\n",
154 new->my_limit,par->max_limit);
158 // make sure guarantee is lesser than limit
159 if (new->my_limit == CKRM_SHARE_DONTCARE) {
161 } else if (new->my_limit == CKRM_SHARE_UNCHANGED) {
162 if (new->my_guarantee == CKRM_SHARE_DONTCARE) {
164 } else if (new->my_guarantee == CKRM_SHARE_UNCHANGED) {
165 ; // do nothing earlier setting would've
167 } else if (new->my_guarantee > cur->my_limit) {
168 printk(KERN_ERR "new->my_guarantee %d > cur->my_limit %d\n",
169 new->my_guarantee,par->max_limit);
172 } else { // new->my_limit has a valid value
173 if (new->my_guarantee == CKRM_SHARE_DONTCARE) {
175 } else if (new->my_guarantee == CKRM_SHARE_UNCHANGED) {
176 if (cur->my_guarantee > new->my_limit) {
177 printk(KERN_ERR "cur->my_guarantee %d > new->my_limit %d\n",
178 cur->my_guarantee,new->my_limit);
181 } else if (new->my_guarantee > new->my_limit) {
182 printk(KERN_ERR "new->my_guarantee %d > new->my_limit %d\n",
183 new->my_guarantee,new->my_limit);
188 if (new->my_guarantee != CKRM_SHARE_UNCHANGED) {
189 child_guarantee_changed(par, cur->my_guarantee,
191 cur->my_guarantee = new->my_guarantee;
194 if (new->my_limit != CKRM_SHARE_UNCHANGED) {
195 child_maxlimit_changed(par, new->my_limit);
196 cur->my_limit = new->my_limit;
199 if (new->total_guarantee != CKRM_SHARE_UNCHANGED) {
200 cur->unused_guarantee = new->total_guarantee - cur_usage_guar;
201 cur->total_guarantee = new->total_guarantee;
204 if (new->max_limit != CKRM_SHARE_UNCHANGED) {
205 cur->max_limit = new->max_limit;
213 EXPORT_SYMBOL(get_exe_path_name);
214 EXPORT_SYMBOL(child_guarantee_changed);
215 EXPORT_SYMBOL(child_maxlimit_changed);
216 EXPORT_SYMBOL(set_shares);