Skip to content

Commit

Permalink
mm: convert totalram_pages and totalhigh_pages variables to atomic
Browse files Browse the repository at this point in the history
totalram_pages and totalhigh_pages are made static inline function.

Main motivation was that managed_page_count_lock handling was complicating
things.  It was discussed in length here,
https://lore.kernel.org/patchwork/patch/995739/#1181785 So it seemes
better to remove the lock and convert variables to atomic, with preventing
poteintial store-to-read tearing as a bonus.

Link: http://lkml.kernel.org/r/1542090790-21750-4-git-send-email-arunks@codeaurora.org
Signed-off-by: Arun KS <arunks@codeaurora.org>
Suggested-by: Michal Hocko <mhocko@suse.com>
Suggested-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Acked-by: Michal Hocko <mhocko@suse.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Cc: David Hildenbrand <david@redhat.com>
Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
  • Loading branch information
Arun KS authored and sfrothwell committed Dec 15, 2018
1 parent 52b7502 commit ed57134
Show file tree
Hide file tree
Showing 53 changed files with 130 additions and 81 deletions.
4 changes: 2 additions & 2 deletions arch/csky/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void free_initrd_mem(unsigned long start, unsigned long end)
ClearPageReserved(virt_to_page(start));
init_page_count(virt_to_page(start));
free_page(start);
totalram_pages++;
totalram_pages_inc();
}
}
#endif
Expand All @@ -88,7 +88,7 @@ void free_initmem(void)
ClearPageReserved(virt_to_page(addr));
init_page_count(virt_to_page(addr));
free_page(addr);
totalram_pages++;
totalram_pages_inc();
addr += PAGE_SIZE;
}

Expand Down
10 changes: 5 additions & 5 deletions arch/powerpc/platforms/pseries/cmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ static long cmm_alloc_pages(long nr)

pa->page[pa->index++] = addr;
loaned_pages++;
totalram_pages--;
totalram_pages_dec();
spin_unlock(&cmm_lock);
nr--;
}
Expand Down Expand Up @@ -247,7 +247,7 @@ static long cmm_free_pages(long nr)
free_page(addr);
loaned_pages--;
nr--;
totalram_pages++;
totalram_pages_inc();
}
spin_unlock(&cmm_lock);
cmm_dbg("End request with %ld pages unfulfilled\n", nr);
Expand Down Expand Up @@ -291,7 +291,7 @@ static void cmm_get_mpp(void)
int rc;
struct hvcall_mpp_data mpp_data;
signed long active_pages_target, page_loan_request, target;
signed long total_pages = totalram_pages + loaned_pages;
signed long total_pages = totalram_pages() + loaned_pages;
signed long min_mem_pages = (min_mem_mb * 1024 * 1024) / PAGE_SIZE;

rc = h_get_mpp(&mpp_data);
Expand Down Expand Up @@ -322,7 +322,7 @@ static void cmm_get_mpp(void)

cmm_dbg("delta = %ld, loaned = %lu, target = %lu, oom = %lu, totalram = %lu\n",
page_loan_request, loaned_pages, loaned_pages_target,
oom_freed_pages, totalram_pages);
oom_freed_pages, totalram_pages());
}

static struct notifier_block cmm_oom_nb = {
Expand Down Expand Up @@ -581,7 +581,7 @@ static int cmm_mem_going_offline(void *arg)
free_page(pa_curr->page[idx]);
freed++;
loaned_pages--;
totalram_pages++;
totalram_pages_inc();
pa_curr->page[idx] = pa_last->page[--pa_last->index];
if (pa_last->index == 0) {
if (pa_curr == pa_last)
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static void __init setup_zero_pages(void)
order = 7;

/* Limit number of empty zero pages for small memory sizes */
while (order > 2 && (totalram_pages >> 10) < (1UL << order))
while (order > 2 && (totalram_pages() >> 10) < (1UL << order))
order--;

empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
Expand Down
2 changes: 1 addition & 1 deletion arch/um/kernel/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void __init mem_init(void)

/* this will put all low memory onto the freelists */
memblock_free_all();
max_low_pfn = totalram_pages;
max_low_pfn = totalram_pages();
max_pfn = max_low_pfn;
mem_init_print_info(NULL);
kmalloc_ok = 1;
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kernel/cpu/microcode/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ static ssize_t microcode_write(struct file *file, const char __user *buf,
size_t len, loff_t *ppos)
{
ssize_t ret = -EINVAL;
unsigned long nr_pages = totalram_pages;
unsigned long nr_pages = totalram_pages();

if ((len >> PAGE_SHIFT) > nr_pages) {
pr_err("too much data (max %ld pages)\n", nr_pages);
Expand Down
4 changes: 2 additions & 2 deletions drivers/char/agp/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ static int agp_find_max(void)
long memory, index, result;

#if PAGE_SHIFT < 20
memory = totalram_pages >> (20 - PAGE_SHIFT);
memory = totalram_pages() >> (20 - PAGE_SHIFT);
#else
memory = totalram_pages << (PAGE_SHIFT - 20);
memory = totalram_pages() << (PAGE_SHIFT - 20);
#endif
index = 1;

Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/i915/i915_gem.c
Original file line number Diff line number Diff line change
Expand Up @@ -2539,7 +2539,7 @@ static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
* If there's no chance of allocating enough pages for the whole
* object, bail early.
*/
if (page_count > totalram_pages)
if (page_count > totalram_pages())
return -ENOMEM;

st = kmalloc(sizeof(*st), GFP_KERNEL);
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpu/drm/i915/selftests/i915_gem_gtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static int igt_ppgtt_alloc(void *arg)
* This should ensure that we do not run into the oomkiller during
* the test and take down the machine wilfully.
*/
limit = totalram_pages << PAGE_SHIFT;
limit = totalram_pages() << PAGE_SHIFT;
limit = min(ppgtt->vm.total, limit);

/* Check we can allocate the entire range */
Expand Down Expand Up @@ -1244,7 +1244,7 @@ static int exercise_mock(struct drm_i915_private *i915,
u64 hole_start, u64 hole_end,
unsigned long end_time))
{
const u64 limit = totalram_pages << PAGE_SHIFT;
const u64 limit = totalram_pages() << PAGE_SHIFT;
struct i915_gem_context *ctx;
struct i915_hw_ppgtt *ppgtt;
IGT_TIMEOUT(end_time);
Expand Down
2 changes: 1 addition & 1 deletion drivers/hv/hv_balloon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
static unsigned long compute_balloon_floor(void)
{
unsigned long min_pages;
unsigned long nr_pages = totalram_pages;
unsigned long nr_pages = totalram_pages();
#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
/* Simple continuous piecewiese linear function:
* max MiB -> min MiB gradient
Expand Down
2 changes: 1 addition & 1 deletion drivers/md/dm-bufio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ static int __init dm_bufio_init(void)
dm_bufio_allocated_vmalloc = 0;
dm_bufio_current_allocated = 0;

mem = (__u64)mult_frac(totalram_pages - totalhigh_pages,
mem = (__u64)mult_frac(totalram_pages() - totalhigh_pages(),
DM_BUFIO_MEMORY_PERCENT, 100) << PAGE_SHIFT;

if (mem > ULONG_MAX)
Expand Down
2 changes: 1 addition & 1 deletion drivers/md/dm-crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,7 @@ static int crypt_wipe_key(struct crypt_config *cc)

static void crypt_calculate_pages_per_client(void)
{
unsigned long pages = (totalram_pages - totalhigh_pages) * DM_CRYPT_MEMORY_PERCENT / 100;
unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100;

if (!dm_crypt_clients_n)
return;
Expand Down
2 changes: 1 addition & 1 deletion drivers/md/dm-integrity.c
Original file line number Diff line number Diff line change
Expand Up @@ -2843,7 +2843,7 @@ static int create_journal(struct dm_integrity_c *ic, char **error)
journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
journal_desc_size = journal_pages * sizeof(struct page_list);
if (journal_pages >= totalram_pages - totalhigh_pages || journal_desc_size > ULONG_MAX) {
if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) {
*error = "Journal doesn't fit into memory";
r = -ENOMEM;
goto bad;
Expand Down
2 changes: 1 addition & 1 deletion drivers/md/dm-stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static bool __check_shared_memory(size_t alloc_size)
a = shared_memory_amount + alloc_size;
if (a < shared_memory_amount)
return false;
if (a >> PAGE_SHIFT > totalram_pages / DM_STATS_MEMORY_FACTOR)
if (a >> PAGE_SHIFT > totalram_pages() / DM_STATS_MEMORY_FACTOR)
return false;
#ifdef CONFIG_MMU
if (a > (VMALLOC_END - VMALLOC_START) / DM_STATS_VMALLOC_FACTOR)
Expand Down
2 changes: 1 addition & 1 deletion drivers/media/platform/mtk-vpu/mtk_vpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ static int mtk_vpu_probe(struct platform_device *pdev)
/* Set PTCM to 96K and DTCM to 32K */
vpu_cfg_writel(vpu, 0x2, VPU_TCM_CFG);

vpu->enable_4GB = !!(totalram_pages > (SZ_2G >> PAGE_SHIFT));
vpu->enable_4GB = !!(totalram_pages() > (SZ_2G >> PAGE_SHIFT));
dev_info(dev, "4GB mode %u\n", vpu->enable_4GB);

if (vpu->enable_4GB) {
Expand Down
2 changes: 1 addition & 1 deletion drivers/misc/vmw_balloon.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ static int vmballoon_send_get_target(struct vmballoon *b)
unsigned long status;
unsigned long limit;

limit = totalram_pages;
limit = totalram_pages();

/* Ensure limit fits in 32-bits */
if (limit != (u32)limit)
Expand Down
4 changes: 2 additions & 2 deletions drivers/parisc/ccio-dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ ccio_ioc_init(struct ioc *ioc)
** Hot-Plug/Removal of PCI cards. (aka PCI OLARD).
*/

iova_space_size = (u32) (totalram_pages / count_parisc_driver(&ccio_driver));
iova_space_size = (u32) (totalram_pages() / count_parisc_driver(&ccio_driver));

/* limit IOVA space size to 1MB-1GB */

Expand Down Expand Up @@ -1290,7 +1290,7 @@ ccio_ioc_init(struct ioc *ioc)

DBG_INIT("%s() hpa 0x%p mem %luMB IOV %dMB (%d bits)\n",
__func__, ioc->ioc_regs,
(unsigned long) totalram_pages >> (20 - PAGE_SHIFT),
(unsigned long) totalram_pages() >> (20 - PAGE_SHIFT),
iova_space_size>>20,
iov_order + PAGE_SHIFT);

Expand Down
4 changes: 2 additions & 2 deletions drivers/parisc/sba_iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ sba_ioc_init(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
** for DMA hints - ergo only 30 bits max.
*/

iova_space_size = (u32) (totalram_pages/global_ioc_cnt);
iova_space_size = (u32) (totalram_pages()/global_ioc_cnt);

/* limit IOVA space size to 1MB-1GB */
if (iova_space_size < (1 << (20 - PAGE_SHIFT))) {
Expand All @@ -1439,7 +1439,7 @@ sba_ioc_init(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
DBG_INIT("%s() hpa 0x%lx mem %ldMB IOV %dMB (%d bits)\n",
__func__,
ioc->ioc_hpa,
(unsigned long) totalram_pages >> (20 - PAGE_SHIFT),
(unsigned long) totalram_pages() >> (20 - PAGE_SHIFT),
iova_space_size>>20,
iov_order + PAGE_SHIFT);

Expand Down
2 changes: 1 addition & 1 deletion drivers/staging/android/ion/ion_system_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static int ion_system_heap_allocate(struct ion_heap *heap,
unsigned long size_remaining = PAGE_ALIGN(size);
unsigned int max_order = orders[0];

if (size / PAGE_SIZE > totalram_pages / 2)
if (size / PAGE_SIZE > totalram_pages() / 2)
return -ENOMEM;

INIT_LIST_HEAD(&pages);
Expand Down
6 changes: 3 additions & 3 deletions drivers/xen/xen-selfballoon.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static void selfballoon_process(struct work_struct *work)
bool reset_timer = false;

if (xen_selfballooning_enabled) {
cur_pages = totalram_pages;
cur_pages = totalram_pages();
tgt_pages = cur_pages; /* default is no change */
goal_pages = vm_memory_committed() +
totalreserve_pages +
Expand Down Expand Up @@ -227,7 +227,7 @@ static void selfballoon_process(struct work_struct *work)
if (tgt_pages < floor_pages)
tgt_pages = floor_pages;
balloon_set_new_target(tgt_pages +
balloon_stats.current_pages - totalram_pages);
balloon_stats.current_pages - totalram_pages());
reset_timer = true;
}
#ifdef CONFIG_FRONTSWAP
Expand Down Expand Up @@ -569,7 +569,7 @@ int xen_selfballoon_init(bool use_selfballooning, bool use_frontswap_selfshrink)
* much more reliably and response faster in some cases.
*/
if (!selfballoon_reserved_mb) {
reserve_pages = totalram_pages / 10;
reserve_pages = totalram_pages() / 10;
selfballoon_reserved_mb = PAGES2MB(reserve_pages);
}
schedule_delayed_work(&selfballoon_worker, selfballoon_interval * HZ);
Expand Down
2 changes: 1 addition & 1 deletion fs/ceph/super.h
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ static inline int default_congestion_kb(void)
* This allows larger machines to have larger/more transfers.
* Limit the default to 256M
*/
congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
if (congestion_kb > 256*1024)
congestion_kb = 256*1024;

Expand Down
2 changes: 1 addition & 1 deletion fs/file_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void __init files_init(void)
void __init files_maxfiles_init(void)
{
unsigned long n;
unsigned long nr_pages = totalram_pages;
unsigned long nr_pages = totalram_pages();
unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2;

memreserve = min(memreserve, nr_pages - 1);
Expand Down
2 changes: 1 addition & 1 deletion fs/fuse/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ static const struct super_operations fuse_super_operations = {
static void sanitize_global_limit(unsigned *limit)
{
if (*limit == 0)
*limit = ((totalram_pages << PAGE_SHIFT) >> 13) /
*limit = ((totalram_pages() << PAGE_SHIFT) >> 13) /
sizeof(struct fuse_req);

if (*limit >= 1 << 16)
Expand Down
2 changes: 1 addition & 1 deletion fs/nfs/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -2121,7 +2121,7 @@ int __init nfs_init_writepagecache(void)
* This allows larger machines to have larger/more transfers.
* Limit the default to 256M
*/
nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
if (nfs_congestion_kb > 256*1024)
nfs_congestion_kb = 256*1024;

Expand Down
2 changes: 1 addition & 1 deletion fs/nfsd/nfscache.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static unsigned int
nfsd_cache_size_limit(void)
{
unsigned int limit;
unsigned long low_pages = totalram_pages - totalhigh_pages;
unsigned long low_pages = totalram_pages() - totalhigh_pages();

limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
return min_t(unsigned int, limit, 256*1024);
Expand Down
2 changes: 1 addition & 1 deletion fs/ntfs/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static inline void *__ntfs_malloc(unsigned long size, gfp_t gfp_mask)
return kmalloc(PAGE_SIZE, gfp_mask & ~__GFP_HIGHMEM);
/* return (void *)__get_free_page(gfp_mask); */
}
if (likely((size >> PAGE_SHIFT) < totalram_pages))
if (likely((size >> PAGE_SHIFT) < totalram_pages()))
return __vmalloc(size, gfp_mask, PAGE_KERNEL);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion fs/proc/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ static const struct file_operations proc_lstats_operations = {
static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
struct pid *pid, struct task_struct *task)
{
unsigned long totalpages = totalram_pages + total_swap_pages;
unsigned long totalpages = totalram_pages() + total_swap_pages;
unsigned long points = 0;

points = oom_badness(task, NULL, NULL, totalpages) *
Expand Down
28 changes: 26 additions & 2 deletions include/linux/highmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,31 @@ static inline void invalidate_kernel_vmap_range(void *vaddr, int size)

/* declarations for linux/mm/highmem.c */
unsigned int nr_free_highpages(void);
extern unsigned long totalhigh_pages;
extern atomic_long_t _totalhigh_pages;
static inline unsigned long totalhigh_pages(void)
{
return (unsigned long)atomic_long_read(&_totalhigh_pages);
}

static inline void totalhigh_pages_inc(void)
{
atomic_long_inc(&_totalhigh_pages);
}

static inline void totalhigh_pages_dec(void)
{
atomic_long_dec(&_totalhigh_pages);
}

static inline void totalhigh_pages_add(long count)
{
atomic_long_add(count, &_totalhigh_pages);
}

static inline void totalhigh_pages_set(long val)
{
atomic_long_set(&_totalhigh_pages, val);
}

void kmap_flush_unused(void);

Expand All @@ -51,7 +75,7 @@ static inline struct page *kmap_to_page(void *addr)
return virt_to_page(addr);
}

#define totalhigh_pages 0UL
static inline unsigned long totalhigh_pages(void) { return 0UL; }

#ifndef ARCH_HAS_KMAP
static inline void *kmap(struct page *page)
Expand Down
Loading

0 comments on commit ed57134

Please sign in to comment.