diff options
author | Paul Menage <menage@google.com> | 2009-01-07 21:08:38 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-01-08 11:31:10 -0500 |
commit | e7c5ec9193d32b9559a3bb8893ceedbda85201ff (patch) | |
tree | fd7505c03e2c7525a110a702907c0604c233e2d9 /include/linux | |
parent | 2cb378c862777d050c20db903b119a029845fdcb (diff) |
cgroups: add css_tryget()
Add css_tryget(), that obtains a counted reference on a CSS. It is used
in situations where the caller has a "weak" reference to the CSS, i.e.
one that does not protect the cgroup from removal via a reference count,
but would instead be cleaned up by a destroy() callback.
css_tryget() will return true on success, or false if the cgroup is being
removed.
This is similar to Kamezawa Hiroyuki's patch from a week or two ago, but
with the difference that in the event of css_tryget() racing with a
cgroup_rmdir(), css_tryget() will only return false if the cgroup really
does get removed.
This implementation is done by biasing css->refcnt, so that a refcnt of 1
means "releasable" and 0 means "released or releasing". In the event of a
race, css_tryget() distinguishes between "released" and "releasing" by
checking for the CSS_REMOVED flag in css->flags.
Signed-off-by: Paul Menage <menage@google.com>
Tested-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Li Zefan <lizf@cn.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'include/linux')
-rw-r--r-- | include/linux/cgroup.h | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index ce1c1f34c30c..e267e62827bb 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h | |||
@@ -52,9 +52,9 @@ struct cgroup_subsys_state { | |||
52 | * hierarchy structure */ | 52 | * hierarchy structure */ |
53 | struct cgroup *cgroup; | 53 | struct cgroup *cgroup; |
54 | 54 | ||
55 | /* State maintained by the cgroup system to allow | 55 | /* State maintained by the cgroup system to allow subsystems |
56 | * subsystems to be "busy". Should be accessed via css_get() | 56 | * to be "busy". Should be accessed via css_get(), |
57 | * and css_put() */ | 57 | * css_tryget() and and css_put(). */ |
58 | 58 | ||
59 | atomic_t refcnt; | 59 | atomic_t refcnt; |
60 | 60 | ||
@@ -64,11 +64,14 @@ struct cgroup_subsys_state { | |||
64 | /* bits in struct cgroup_subsys_state flags field */ | 64 | /* bits in struct cgroup_subsys_state flags field */ |
65 | enum { | 65 | enum { |
66 | CSS_ROOT, /* This CSS is the root of the subsystem */ | 66 | CSS_ROOT, /* This CSS is the root of the subsystem */ |
67 | CSS_REMOVED, /* This CSS is dead */ | ||
67 | }; | 68 | }; |
68 | 69 | ||
69 | /* | 70 | /* |
70 | * Call css_get() to hold a reference on the cgroup; | 71 | * Call css_get() to hold a reference on the css; it can be used |
71 | * | 72 | * for a reference obtained via: |
73 | * - an existing ref-counted reference to the css | ||
74 | * - task->cgroups for a locked task | ||
72 | */ | 75 | */ |
73 | 76 | ||
74 | static inline void css_get(struct cgroup_subsys_state *css) | 77 | static inline void css_get(struct cgroup_subsys_state *css) |
@@ -77,9 +80,32 @@ static inline void css_get(struct cgroup_subsys_state *css) | |||
77 | if (!test_bit(CSS_ROOT, &css->flags)) | 80 | if (!test_bit(CSS_ROOT, &css->flags)) |
78 | atomic_inc(&css->refcnt); | 81 | atomic_inc(&css->refcnt); |
79 | } | 82 | } |
83 | |||
84 | static inline bool css_is_removed(struct cgroup_subsys_state *css) | ||
85 | { | ||
86 | return test_bit(CSS_REMOVED, &css->flags); | ||
87 | } | ||
88 | |||
89 | /* | ||
90 | * Call css_tryget() to take a reference on a css if your existing | ||
91 | * (known-valid) reference isn't already ref-counted. Returns false if | ||
92 | * the css has been destroyed. | ||
93 | */ | ||
94 | |||
95 | static inline bool css_tryget(struct cgroup_subsys_state *css) | ||
96 | { | ||
97 | if (test_bit(CSS_ROOT, &css->flags)) | ||
98 | return true; | ||
99 | while (!atomic_inc_not_zero(&css->refcnt)) { | ||
100 | if (test_bit(CSS_REMOVED, &css->flags)) | ||
101 | return false; | ||
102 | } | ||
103 | return true; | ||
104 | } | ||
105 | |||
80 | /* | 106 | /* |
81 | * css_put() should be called to release a reference taken by | 107 | * css_put() should be called to release a reference taken by |
82 | * css_get() | 108 | * css_get() or css_tryget() |
83 | */ | 109 | */ |
84 | 110 | ||
85 | extern void __css_put(struct cgroup_subsys_state *css); | 111 | extern void __css_put(struct cgroup_subsys_state *css); |