Skip to contentSkip to conversation

page_cgroup: add helper function to get swap_cgroup

PATCH Superseded Bob Liu 2 participants 3 messages Dec 2, 2011 Memory management

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Bob Liu

author
#0 lore raw

PATCH 1/1 page_cgroup: add helper function to get swap_cgroup

There are multi places need to get swap_cgroup, so add a helper function:
static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent); to simple the code.

---
 mm/page_cgroup.c |   49 ++++++++++++++++++++++---------------------------
 1 files changed, 22 insertions(+), 27 deletions(-)
mm/page_cgroup.c +22 -27
index f0559e0..ee1766a 100644
@@ -362,6 +362,24 @@ not_enough_page:
 	return -ENOMEM;
 }
 
+static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent)
+{
+	int type = swp_type(ent);
+	unsigned long offset = swp_offset(ent);
+	unsigned long idx = offset / SC_PER_PAGE;
+	unsigned long pos = offset & SC_POS_MASK;
+	struct swap_cgroup_ctrl *ctrl;
+	struct page *mappage;
+	struct swap_cgroup *sc;
+
+	ctrl = &swap_cgroup_ctrl[type];
+
+	mappage = ctrl->map[idx];
+	sc = page_address(mappage);
+	sc += pos;
+	return sc;
+}
+
 /**
  * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
  * @end: swap entry to be cmpxchged
@@ -375,20 +393,14 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
 					unsigned short old, unsigned short new)
 {
 	int type = swp_type(ent);
-	unsigned long offset = swp_offset(ent);
-	unsigned long idx = offset / SC_PER_PAGE;
-	unsigned long pos = offset & SC_POS_MASK;
 	struct swap_cgroup_ctrl *ctrl;
-	struct page *mappage;
 	struct swap_cgroup *sc;
 	unsigned long flags;
 	unsigned short retval;
 
 	ctrl = &swap_cgroup_ctrl[type];
+	sc = swap_cgroup_getsc(ent);
 
-	mappage = ctrl->map[idx];
-	sc = page_address(mappage);
-	sc += pos;
 	spin_lock_irqsave(&ctrl->lock, flags);
 	retval = sc->id;
 	if (retval == old)
@@ -410,20 +422,14 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
 {
 	int type = swp_type(ent);
-	unsigned long offset = swp_offset(ent);
-	unsigned long idx = offset / SC_PER_PAGE;
-	unsigned long pos = offset & SC_POS_MASK;
 	struct swap_cgroup_ctrl *ctrl;
-	struct page *mappage;
 	struct swap_cgroup *sc;
 	unsigned short old;
 	unsigned long flags;
 
 	ctrl = &swap_cgroup_ctrl[type];
+	sc = swap_cgroup_getsc(ent);
 
-	mappage = ctrl->map[idx];
-	sc = page_address(mappage);
-	sc += pos;
 	spin_lock_irqsave(&ctrl->lock, flags);
 	old = sc->id;
 	sc->id = id;
@@ -440,21 +446,10 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
  */
 unsigned short lookup_swap_cgroup(swp_entry_t ent)
 {
-	int type = swp_type(ent);
-	unsigned long offset = swp_offset(ent);
-	unsigned long idx = offset / SC_PER_PAGE;
-	unsigned long pos = offset & SC_POS_MASK;
-	struct swap_cgroup_ctrl *ctrl;
-	struct page *mappage;
 	struct swap_cgroup *sc;
-	unsigned short ret;
 
-	ctrl = &swap_cgroup_ctrl[type];
-	mappage = ctrl->map[idx];
-	sc = page_address(mappage);
-	sc += pos;
-	ret = sc->id;
-	return ret;
+	sc = swap_cgroup_getsc(ent);
+	return sc->id;
 }
 
 int swap_cgroup_swapon(int type, unsigned long max_pages)
Signature · 9 lines
1.7.0.4


--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 4 lines · Bob Liu

There are multi places need to get swap_cgroup, so add a helper function:
static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent); to simple the code.

I like the cleanup but I guess we can do a little bit better ;)

[...]

Quoted 1 line

+static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent)

Add struct swap_cgroup_ctrl ** ctrl parameter

mm/page_cgroup.c not_enough_page: · line 375

Quoted 10 lines
mm/page_cgroup.c +10 -0
+{
+	int type = swp_type(ent);
+	unsigned long offset = swp_offset(ent);
+	unsigned long idx = offset / SC_PER_PAGE;
+	unsigned long pos = offset & SC_POS_MASK;
+	struct swap_cgroup_ctrl *ctrl;
+	struct page *mappage;
+	struct swap_cgroup *sc;
+
+	ctrl = &swap_cgroup_ctrl[type];

if (ctrl)
*ctrl = &swap_cgroup_ctrl[type]

[...]

Quoted 5 lines
mm/page_cgroup.c +0 -1
@@ -375,20 +393,14 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
 					unsigned short old, unsigned short new)
 {
 	int type = swp_type(ent);
-	unsigned long offset = swp_offset(ent);

mm/page_cgroup.c unsigned short swap_cgroup_cmpxchg(swp_entry_t ent, · line 402

Quoted 10 lines
mm/page_cgroup.c +1 -3
-	unsigned long idx = offset / SC_PER_PAGE;
-	unsigned long pos = offset & SC_POS_MASK;
 	struct swap_cgroup_ctrl *ctrl;
-	struct page *mappage;
 	struct swap_cgroup *sc;
 	unsigned long flags;
 	unsigned short retval;
 
 	ctrl = &swap_cgroup_ctrl[type];
+	sc = swap_cgroup_getsc(ent);
	sc = swap_cgroup_getsc(ent, &ctrl);

[...]

Quoted 5 lines
mm/page_cgroup.c +0 -1
@@ -410,20 +422,14 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
 {
 	int type = swp_type(ent);
-	unsigned long offset = swp_offset(ent);

mm/page_cgroup.c unsigned short swap_cgroup_cmpxchg(swp_entry_t ent, · line 431

Quoted 10 lines
mm/page_cgroup.c +1 -3
-	unsigned long idx = offset / SC_PER_PAGE;
-	unsigned long pos = offset & SC_POS_MASK;
 	struct swap_cgroup_ctrl *ctrl;
-	struct page *mappage;
 	struct swap_cgroup *sc;
 	unsigned short old;
 	unsigned long flags;
 
 	ctrl = &swap_cgroup_ctrl[type];
+	sc = swap_cgroup_getsc(ent);

Same here

[...]

Quoted 11 lines
mm/page_cgroup.c +0 -6
@@ -440,21 +446,10 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
  */
 unsigned short lookup_swap_cgroup(swp_entry_t ent)
 {
-	int type = swp_type(ent);
-	unsigned long offset = swp_offset(ent);
-	unsigned long idx = offset / SC_PER_PAGE;
-	unsigned long pos = offset & SC_POS_MASK;
-	struct swap_cgroup_ctrl *ctrl;
-	struct page *mappage;
 	struct swap_cgroup *sc;

mm/page_cgroup.c unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id) · line 452

Quoted 10 lines
mm/page_cgroup.c +2 -7
-	unsigned short ret;
 
-	ctrl = &swap_cgroup_ctrl[type];
-	mappage = ctrl->map[idx];
-	sc = page_address(mappage);
-	sc += pos;
-	ret = sc->id;
-	return ret;
+	sc = swap_cgroup_getsc(ent);
+	return sc->id;
	return swap_cgroup_getsc(ent, NULL)->id;

What do you think?

Signature · 13 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#2 lore raw
Quoted 89 lines · Michal Hocko
Quoted 4 lines · Bob Liu

There are multi places need to get swap_cgroup, so add a helper function:
static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent); to simple the code.

I like the cleanup but I guess we can do a little bit better ;)

[...]

Quoted 1 line

+static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent)

Add struct swap_cgroup_ctrl ** ctrl parameter

mm/page_cgroup.c not_enough_page: · line 375

Quoted 10 lines
mm/page_cgroup.c +10 -0
+{
+     int type = swp_type(ent);
+     unsigned long offset = swp_offset(ent);
+     unsigned long idx = offset / SC_PER_PAGE;
+     unsigned long pos = offset & SC_POS_MASK;
+     struct swap_cgroup_ctrl *ctrl;
+     struct page *mappage;
+     struct swap_cgroup *sc;
+
+     ctrl = &swap_cgroup_ctrl[type];

       if (ctrl)
               *ctrl = &swap_cgroup_ctrl[type]

[...]

Quoted 5 lines
mm/page_cgroup.c +0 -1
@@ -375,20 +393,14 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
                                       unsigned short old, unsigned short new)
  {
       int type = swp_type(ent);
-     unsigned long offset = swp_offset(ent);

mm/page_cgroup.c unsigned short swap_cgroup_cmpxchg(swp_entry_t ent, · line 402

Quoted 10 lines
mm/page_cgroup.c +1 -3
-     unsigned long idx = offset / SC_PER_PAGE;
-     unsigned long pos = offset & SC_POS_MASK;
       struct swap_cgroup_ctrl *ctrl;
-     struct page *mappage;
       struct swap_cgroup *sc;
       unsigned long flags;
       unsigned short retval;
 
       ctrl = &swap_cgroup_ctrl[type];
+     sc = swap_cgroup_getsc(ent);

       sc = swap_cgroup_getsc(ent, &ctrl);
[...]

Quoted 5 lines
mm/page_cgroup.c +0 -1
@@ -410,20 +422,14 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
  unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
  {
       int type = swp_type(ent);
-     unsigned long offset = swp_offset(ent);

mm/page_cgroup.c unsigned short swap_cgroup_cmpxchg(swp_entry_t ent, · line 431

Quoted 10 lines
mm/page_cgroup.c +1 -3
-     unsigned long idx = offset / SC_PER_PAGE;
-     unsigned long pos = offset & SC_POS_MASK;
       struct swap_cgroup_ctrl *ctrl;
-     struct page *mappage;
       struct swap_cgroup *sc;
       unsigned short old;
       unsigned long flags;
 
       ctrl = &swap_cgroup_ctrl[type];
+     sc = swap_cgroup_getsc(ent);

Same here

[...]

Quoted 11 lines
mm/page_cgroup.c +0 -6
@@ -440,21 +446,10 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
   */
  unsigned short lookup_swap_cgroup(swp_entry_t ent)
  {
-     int type = swp_type(ent);
-     unsigned long offset = swp_offset(ent);
-     unsigned long idx = offset / SC_PER_PAGE;
-     unsigned long pos = offset & SC_POS_MASK;
-     struct swap_cgroup_ctrl *ctrl;
-     struct page *mappage;
       struct swap_cgroup *sc;

mm/page_cgroup.c unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id) · line 452

Quoted 10 lines
mm/page_cgroup.c +2 -7
-     unsigned short ret;
 
-     ctrl = &swap_cgroup_ctrl[type];
-     mappage = ctrl->map[idx];
-     sc = page_address(mappage);
-     sc += pos;
-     ret = sc->id;
-     return ret;
+     sc = swap_cgroup_getsc(ent);
+     return sc->id;

       return swap_cgroup_getsc(ent, NULL)->id;

Quoted 1 line

What do you think?

Alright, i'll send out v2.
Thanks for your review.

Signature · 9 lines
Regards,
--Bob

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>