History

res_counter: Merge res_counter_charge and res_counter_charge_nofail

These two functions do almost the same thing and duplicate some code. Merge their implementation into a single common function. res_counter_charge_locked() takes one more parameter but it doesn't seem to be used outside res_counter.c yet anyway. There is no (intended) change in the behaviour.

Frederic Weisbecker committed 14 years ago

Commit 4d8438f1 parent c4c27fb3 files · +37 −42Browse files

Unified · Split

3 files
  1. Documentation/cgroups/resource_counter.txt +2 −2
  2. include/linux/res_counter.h +1 −1
  3. kernel/res_counter.c +34 −39
Documentation/cgroups/resource_counter.txt+2−2
@@ -77,11 +77,11 @@ to work with it. · 76 unmodified lines
7777 where the charging failed.
7878
7979 d. int res_counter_charge_locked
80− (struct res_counter *rc, unsigned long val)
80+ (struct res_counter *rc, unsigned long val, bool force)
8181
8282 The same as res_counter_charge(), but it must not acquire/release the
8383 res_counter->lock internally (it must be called with res_counter->lock
84− held).
84+ held). The force parameter indicates whether we can bypass the limit.
8585
8686 e. void res_counter_uncharge[_locked]
8787 (struct res_counter *rc, unsigned long val)
include/linux/res_counter.h+1−1
@@ -116,7 +116,7 @@ void res_counter_init(struct res_counter *counter, struct res_counter *parent); · 115 unmodified lines
116116 */
117117
118118 int __must_check res_counter_charge_locked(struct res_counter *counter,
119− unsigned long val);
119+ unsigned long val, bool force);
120120 int __must_check res_counter_charge(struct res_counter *counter,
121121 unsigned long val, struct res_counter **limit_fail_at);
122122 int __must_check res_counter_charge_nofail(struct res_counter *counter,
kernel/res_counter.c+34−39
@@ -22,75 +22,70 @@ void res_counter_init(struct res_counter *counter, struct res_counter *parent) · 21 unmodified lines
2222 counter->parent = parent;
2323 }
2424
25−int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
25+int res_counter_charge_locked(struct res_counter *counter, unsigned long val,
26+ bool force)
2627 {
28+ int ret = 0;
29+
2730 if (counter->usage + val > counter->limit) {
2831 counter->failcnt++;
29− return -ENOMEM;
32+ ret = -ENOMEM;
33+ if (!force)
34+ return ret;
3035 }
3136
3237 counter->usage += val;
33− if (counter->usage > counter->max_usage)
38+ if (!force && counter->usage > counter->max_usage)
3439 counter->max_usage = counter->usage;
35− return 0;
40+ return ret;
3641 }
3742
38−int res_counter_charge(struct res_counter *counter, unsigned long val,
39− struct res_counter **limit_fail_at)
43+static int __res_counter_charge(struct res_counter *counter, unsigned long val,
44+ struct res_counter **limit_fail_at, bool force)
4045 {
41− int ret;
46+ int ret, r;
4247 unsigned long flags;
4348 struct res_counter *c, *u;
4449
50+ r = ret = 0;
4551 *limit_fail_at = NULL;
4652 local_irq_save(flags);
4753 for (c = counter; c != NULL; c = c->parent) {
4854 spin_lock(&c->lock);
49− ret = res_counter_charge_locked(c, val);
55+ r = res_counter_charge_locked(c, val, force);
5056 spin_unlock(&c->lock);
51− if (ret < 0) {
57+ if (r < 0 && !ret) {
58+ ret = r;
5259 *limit_fail_at = c;
53− goto undo;
60+ if (!force)
61+ break;
5462 }
5563 }
56− ret = 0;
57− goto done;
58−undo:
59− for (u = counter; u != c; u = u->parent) {
60− spin_lock(&u->lock);
61− res_counter_uncharge_locked(u, val);
62− spin_unlock(&u->lock);
64+
65+ if (ret < 0 && !force) {
66+ for (u = counter; u != c; u = u->parent) {
67+ spin_lock(&u->lock);
68+ res_counter_uncharge_locked(u, val);
69+ spin_unlock(&u->lock);
70+ }
6371 }
64−done:
6572 local_irq_restore(flags);
73+
6674 return ret;
6775 }
6876
77+int res_counter_charge(struct res_counter *counter, unsigned long val,
78+ struct res_counter **limit_fail_at)
79+{
80+ return __res_counter_charge(counter, val, limit_fail_at, false);
81+}
82+
6983 int res_counter_charge_nofail(struct res_counter *counter, unsigned long val,
7084 struct res_counter **limit_fail_at)
7185 {
72− int ret, r;
73− unsigned long flags;
74− struct res_counter *c;
75−
76− r = ret = 0;
77− *limit_fail_at = NULL;
78− local_irq_save(flags);
79− for (c = counter; c != NULL; c = c->parent) {
80− spin_lock(&c->lock);
81− r = res_counter_charge_locked(c, val);
82− if (r)
83− c->usage += val;
84− spin_unlock(&c->lock);
85− if (r < 0 && ret == 0) {
86− *limit_fail_at = c;
87− ret = r;
88− }
89− }
90− local_irq_restore(flags);
91−
92− return ret;
86+ return __res_counter_charge(counter, val, limit_fail_at, true);
9387 }
88+
9489 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
9590 {
9691 if (WARN_ON(counter->usage < val))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t