Skip to content

Commit 9fb4b7c

Browse files
aet00torvalds
authored andcommitted
page_cgroup: add helper function to get swap_cgroup
There are multiple places which need to get the swap_cgroup address, so add a helper function: static struct swap_cgroup *swap_cgroup_getsc(swp_entry_t ent, struct swap_cgroup_ctrl **ctrl); to simplify the code. Signed-off-by: Bob Liu <[email protected]> Acked-by: Michal Hocko <[email protected]> Acked-by: KAMEZAWA Hiroyuki <[email protected]> Cc: Johannes Weiner <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 40f23a2 commit 9fb4b7c

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

include/linux/page_cgroup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static inline void __init page_cgroup_init_flatmem(void)
149149
extern unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
150150
unsigned short old, unsigned short new);
151151
extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id);
152-
extern unsigned short lookup_swap_cgroup(swp_entry_t ent);
152+
extern unsigned short lookup_swap_cgroup_id(swp_entry_t ent);
153153
extern int swap_cgroup_swapon(int type, unsigned long max_pages);
154154
extern void swap_cgroup_swapoff(int type);
155155
#else
@@ -161,7 +161,7 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
161161
}
162162

163163
static inline
164-
unsigned short lookup_swap_cgroup(swp_entry_t ent)
164+
unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
165165
{
166166
return 0;
167167
}

mm/memcontrol.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,7 @@ struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
24742474
memcg = NULL;
24752475
} else if (PageSwapCache(page)) {
24762476
ent.val = page_private(page);
2477-
id = lookup_swap_cgroup(ent);
2477+
id = lookup_swap_cgroup_id(ent);
24782478
rcu_read_lock();
24792479
memcg = mem_cgroup_lookup(id);
24802480
if (memcg && !css_tryget(&memcg->css))
@@ -5264,7 +5264,7 @@ static int is_target_pte_for_mc(struct vm_area_struct *vma,
52645264
}
52655265
/* There is a swap entry and a page doesn't exist or isn't charged */
52665266
if (ent.val && !ret &&
5267-
css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5267+
css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
52685268
ret = MC_TARGET_SWAP;
52695269
if (target)
52705270
target->ent = ent;

mm/page_cgroup.c

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ struct swap_cgroup {
334334
unsigned short id;
335335
};
336336
#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
337-
#define SC_POS_MASK (SC_PER_PAGE - 1)
338337

339338
/*
340339
* SwapCgroup implements "lookup" and "exchange" operations.
@@ -376,6 +375,21 @@ static int swap_cgroup_prepare(int type)
376375
return -ENOMEM;
377376
}
378377

378+
static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
379+
struct swap_cgroup_ctrl **ctrlp)
380+
{
381+
pgoff_t offset = swp_offset(ent);
382+
struct swap_cgroup_ctrl *ctrl;
383+
struct page *mappage;
384+
385+
ctrl = &swap_cgroup_ctrl[swp_type(ent)];
386+
if (ctrlp)
387+
*ctrlp = ctrl;
388+
389+
mappage = ctrl->map[offset / SC_PER_PAGE];
390+
return page_address(mappage) + offset % SC_PER_PAGE;
391+
}
392+
379393
/**
380394
* swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
381395
* @end: swap entry to be cmpxchged
@@ -388,21 +402,13 @@ static int swap_cgroup_prepare(int type)
388402
unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
389403
unsigned short old, unsigned short new)
390404
{
391-
int type = swp_type(ent);
392-
unsigned long offset = swp_offset(ent);
393-
unsigned long idx = offset / SC_PER_PAGE;
394-
unsigned long pos = offset & SC_POS_MASK;
395405
struct swap_cgroup_ctrl *ctrl;
396-
struct page *mappage;
397406
struct swap_cgroup *sc;
398407
unsigned long flags;
399408
unsigned short retval;
400409

401-
ctrl = &swap_cgroup_ctrl[type];
410+
sc = lookup_swap_cgroup(ent, &ctrl);
402411

403-
mappage = ctrl->map[idx];
404-
sc = page_address(mappage);
405-
sc += pos;
406412
spin_lock_irqsave(&ctrl->lock, flags);
407413
retval = sc->id;
408414
if (retval == old)
@@ -423,21 +429,13 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
423429
*/
424430
unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
425431
{
426-
int type = swp_type(ent);
427-
unsigned long offset = swp_offset(ent);
428-
unsigned long idx = offset / SC_PER_PAGE;
429-
unsigned long pos = offset & SC_POS_MASK;
430432
struct swap_cgroup_ctrl *ctrl;
431-
struct page *mappage;
432433
struct swap_cgroup *sc;
433434
unsigned short old;
434435
unsigned long flags;
435436

436-
ctrl = &swap_cgroup_ctrl[type];
437+
sc = lookup_swap_cgroup(ent, &ctrl);
437438

438-
mappage = ctrl->map[idx];
439-
sc = page_address(mappage);
440-
sc += pos;
441439
spin_lock_irqsave(&ctrl->lock, flags);
442440
old = sc->id;
443441
sc->id = id;
@@ -447,28 +445,14 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
447445
}
448446

449447
/**
450-
* lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
448+
* lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
451449
* @ent: swap entry to be looked up.
452450
*
453451
* Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
454452
*/
455-
unsigned short lookup_swap_cgroup(swp_entry_t ent)
453+
unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
456454
{
457-
int type = swp_type(ent);
458-
unsigned long offset = swp_offset(ent);
459-
unsigned long idx = offset / SC_PER_PAGE;
460-
unsigned long pos = offset & SC_POS_MASK;
461-
struct swap_cgroup_ctrl *ctrl;
462-
struct page *mappage;
463-
struct swap_cgroup *sc;
464-
unsigned short ret;
465-
466-
ctrl = &swap_cgroup_ctrl[type];
467-
mappage = ctrl->map[idx];
468-
sc = page_address(mappage);
469-
sc += pos;
470-
ret = sc->id;
471-
return ret;
455+
return lookup_swap_cgroup(ent, NULL)->id;
472456
}
473457

474458
int swap_cgroup_swapon(int type, unsigned long max_pages)

0 commit comments

Comments
 (0)