Skip to contentSkip to conversation

page_cgroup: add helper function to get swap_cgroup

PATCH In review Bob Liu 4 participants 4 messages Dec 2, 2011 – Dec 12, 2011 Memory management

lore
Share & tools
lore

Bob Liu

author
#0 lore raw

PATCH v2 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,

                                struct swap_cgroup_ctrl **ctrl);

to simple the code.

v1 -> v2:
- add parameter struct swap_cgroup_ctrl **ctrl suggested by Michal

---
 mm/page_cgroup.c |   57 ++++++++++++++++++++++-------------------------------
 1 files changed, 24 insertions(+), 33 deletions(-)
mm/page_cgroup.c +24 -33
index f0559e0..1970e8a 100644
@@ -362,6 +362,27 @@ not_enough_page:
 	return -ENOMEM;
 }
 
+static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent,
+					struct swap_cgroup_ctrl **ctrl)
+{
+	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 *temp_ctrl;
+	struct page *mappage;
+	struct swap_cgroup *sc;
+
+	temp_ctrl = &swap_cgroup_ctrl[type];
+	if (ctrl)
+		*ctrl = temp_ctrl;
+
+	mappage = temp_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
@@ -374,21 +395,13 @@ not_enough_page:
 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, &ctrl);
 
-	mappage = ctrl->map[idx];
-	sc = page_address(mappage);
-	sc += pos;
 	spin_lock_irqsave(&ctrl->lock, flags);
 	retval = sc->id;
 	if (retval == old)
@@ -409,21 +422,13 @@ 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, &ctrl);
 
-	mappage = ctrl->map[idx];
-	sc = page_address(mappage);
-	sc += pos;
 	spin_lock_irqsave(&ctrl->lock, flags);
 	old = sc->id;
 	sc->id = id;
@@ -440,21 +445,7 @@ 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;
+	return swap_cgroup_getsc(ent, NULL)->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>
Acked-by on v2 1/1
#1 lore raw
Quoted 10 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,

                                struct swap_cgroup_ctrl **ctrl);

to simple the code.

v1 -> v2:
- add parameter struct swap_cgroup_ctrl **ctrl suggested by Michal

Thanks.

Quoted 115 lines
---
 mm/page_cgroup.c |   57 ++++++++++++++++++++++-------------------------------
 1 files changed, 24 insertions(+), 33 deletions(-)
mm/page_cgroup.c +24 -33
index f0559e0..1970e8a 100644
@@ -362,6 +362,27 @@ not_enough_page:
 	return -ENOMEM;
 }
 
+static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent,
+					struct swap_cgroup_ctrl **ctrl)
+{
+	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 *temp_ctrl;
+	struct page *mappage;
+	struct swap_cgroup *sc;
+
+	temp_ctrl = &swap_cgroup_ctrl[type];
+	if (ctrl)
+		*ctrl = temp_ctrl;
+
+	mappage = temp_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
@@ -374,21 +395,13 @@ not_enough_page:
 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, &ctrl);
 
-	mappage = ctrl->map[idx];
-	sc = page_address(mappage);
-	sc += pos;
 	spin_lock_irqsave(&ctrl->lock, flags);
 	retval = sc->id;
 	if (retval == old)
@@ -409,21 +422,13 @@ 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, &ctrl);
 
-	mappage = ctrl->map[idx];
-	sc = page_address(mappage);
-	sc += pos;
 	spin_lock_irqsave(&ctrl->lock, flags);
 	old = sc->id;
 	sc->id = id;
@@ -440,21 +445,7 @@ 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;
+	return swap_cgroup_getsc(ent, NULL)->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>
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>
Acked-by on v2 1/1
#2 lore raw
In reply to Bob Liu
Quoted 10 lines · On Fri, 2 Dec 2011 18:40:27 +0800 Bob Liu <[email protected]> wrote:

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,

                                struct swap_cgroup_ctrl **ctrl);

to simple the code.

v1 -> v2:
- add parameter struct swap_cgroup_ctrl **ctrl suggested by Michal

--
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>

#3 lore raw
In reply to Bob Liu
Quoted 19 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,

                                struct swap_cgroup_ctrl **ctrl);

to simple the code.

v1 -> v2:
- add parameter struct swap_cgroup_ctrl **ctrl suggested by Michal

---
 mm/page_cgroup.c |   57 ++++++++++++++++++++++-------------------------------
 1 files changed, 24 insertions(+), 33 deletions(-)
mm/page_cgroup.c +0 -0
index f0559e0..1970e8a 100644

mm/page_cgroup.c not_enough_page: · line 363

Quoted 2 lines
mm/page_cgroup.c +0 -0
@@ -362,6 +362,27 @@ not_enough_page:
 	return -ENOMEM;
 }

I realize that you mostly moved what was already there, but there are a couple more things to clean up. Would you like to send a patch for them as well?

mm/page_cgroup.c not_enough_page: · line 366

Quoted 2 lines
mm/page_cgroup.c +2 -0
+static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent,
+					struct swap_cgroup_ctrl **ctrl)

__lookup_swap_cgroup()? Or even more matching names would be to have that public interface called lookup_swap_cgroup_id() and let this one be lookup_swap_cgroup().

mm/page_cgroup.c not_enough_page: · line 368

Quoted 2 lines
mm/page_cgroup.c +2 -0
+{
+	int type = swp_type(ent);

swp_type() returns unsigned int

mm/page_cgroup.c not_enough_page: · line 369

Quoted 1 line
mm/page_cgroup.c +1 -0
+	unsigned long offset = swp_offset(ent);

swp_offset() returns pgoff_t

mm/page_cgroup.c not_enough_page: · line 371

Quoted 2 lines
mm/page_cgroup.c +2 -0
+	unsigned long idx = offset / SC_PER_PAGE;
+	unsigned long pos = offset & SC_POS_MASK;

This is actually quite crappy, the definition looks like this:

#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
#define SC_POS_MASK (SC_PER_PAGE - 1)

which relies on the fact that the division named SC_PER_PAGE yields a power of two, which only is true by accident.

Better would be to delete SC_POS_MASK and use offset % SC_PER_PAGE instead.

mm/page_cgroup.c not_enough_page: · line 378

Quoted 7 lines
mm/page_cgroup.c +7 -0
+	struct swap_cgroup_ctrl *temp_ctrl;
+	struct page *mappage;
+	struct swap_cgroup *sc;
+
+	temp_ctrl = &swap_cgroup_ctrl[type];
+	if (ctrl)
+		*ctrl = temp_ctrl;

Name the output parameter ctrlp instead? Then you can call the local one ctrl.

Also, type is only used once, better to just inline it:

&swap_cgroup_ctrl[swp_type(ent)]

mm/page_cgroup.c not_enough_page: · line 380

Quoted 1 line
mm/page_cgroup.c +1 -0
+	mappage = temp_ctrl->map[idx];

Same for idx, just use ctrl->map[offset / SC_PER_PAGE] directly.

mm/page_cgroup.c not_enough_page: · line 384

Quoted 4 lines
mm/page_cgroup.c +4 -0
+	sc = page_address(mappage);
+	sc += pos;
+	return sc;
+}

That seems elaborate.

return page_address(mappage) + offset % SC_PER_PAGE

--
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>