]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
mm: convert totalram_pages and totalhigh_pages variables to atomic
authorArun KS <arunks@codeaurora.org>
Fri, 28 Dec 2018 08:34:29 +0000 (00:34 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 28 Dec 2018 20:11:47 +0000 (12:11 -0800)
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.

[akpm@linux-foundation.org: coding style fixes]
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>
Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
53 files changed:
arch/csky/mm/init.c
arch/powerpc/platforms/pseries/cmm.c
arch/s390/mm/init.c
arch/um/kernel/mem.c
arch/x86/kernel/cpu/microcode/core.c
drivers/char/agp/backend.c
drivers/gpu/drm/i915/i915_gem.c
drivers/gpu/drm/i915/selftests/i915_gem_gtt.c
drivers/hv/hv_balloon.c
drivers/md/dm-bufio.c
drivers/md/dm-crypt.c
drivers/md/dm-integrity.c
drivers/md/dm-stats.c
drivers/media/platform/mtk-vpu/mtk_vpu.c
drivers/misc/vmw_balloon.c
drivers/parisc/ccio-dma.c
drivers/parisc/sba_iommu.c
drivers/staging/android/ion/ion_system_heap.c
drivers/xen/xen-selfballoon.c
fs/ceph/super.h
fs/file_table.c
fs/fuse/inode.c
fs/nfs/write.c
fs/nfsd/nfscache.c
fs/ntfs/malloc.h
fs/proc/base.c
include/linux/highmem.h
include/linux/mm.h
include/linux/swap.h
kernel/fork.c
kernel/kexec_core.c
kernel/power/snapshot.c
mm/highmem.c
mm/huge_memory.c
mm/kasan/quarantine.c
mm/memblock.c
mm/mm_init.c
mm/oom_kill.c
mm/page_alloc.c
mm/shmem.c
mm/slab.c
mm/swap.c
mm/util.c
mm/vmalloc.c
mm/workingset.c
mm/zswap.c
net/dccp/proto.c
net/decnet/dn_route.c
net/ipv4/tcp_metrics.c
net/netfilter/nf_conntrack_core.c
net/netfilter/xt_hashlimit.c
net/sctp/protocol.c
security/integrity/ima/ima_kexec.c

index dc07c078f9b8e92ee704bf1ef3664fb1ce9fae6c..66e59705348802374a939717612dd1698ac4afe0 100644 (file)
@@ -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
@@ -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;
        }
 
index 25427a48feae3a2dcdee2182cd3bb6c5765066d9..e8d63a6a90023aee345b9235b4fc3afa2894d3b7 100644 (file)
@@ -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--;
        }
@@ -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);
@@ -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);
@@ -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 = {
@@ -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)
index 76d0708438e9b8d9c4dcabd5b280caceafb2bfb0..50388190b39302470e609d43bed992ad0c3e7fa4 100644 (file)
@@ -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);
index 2da209687a2237398ddaceacbcc9c4d1efdf6a16..8d21a83dd28933721fa479711b112be745d27b7a 100644 (file)
@@ -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;
index 168fa272cc3ec2782abc700364e6ab74335a1a92..97f9ada9cedaf4e7cde47e819f7b4a0d6ac39d77 100644 (file)
@@ -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);
index 38ffb281df97c71eee3dc8980a98e19df3ca24d3..004a3ce8ba72d5df0b7dd8c72f98074d111c26a8 100644 (file)
@@ -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;
 
index d36a9755ad910f29870e252bb7d6a18a800101d3..a9de07bb72c896e8f3714799fd3a9264010bb79e 100644 (file)
@@ -2559,7 +2559,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);
index 69fe86b30fbb79aaea1c8d8ece1edffe03c9d684..a9ed0ecc94e2d77fa23a721fb9ace38655851597 100644 (file)
@@ -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 */
@@ -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);
index f3e7da981610738033f74a303e1d76211157fd6e..5301fef16c31b740fab409ced2f3e7ef6c5bbe23 100644 (file)
@@ -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
index dc385b70e4c336fc2322c9538f86538b000a8556..8b0b628e5d1cd1c1a849d94aa624aaf1c2ad630f 100644 (file)
@@ -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)
index a7195eb5b8d8949b87cfcedc5daf7c944abce5f2..a8c32de29e3fe9eeaf1fa106c52014944c5a5e9f 100644 (file)
@@ -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;
index d4ad0bfee2519aeef4a3f4e7c4baf100fa38bb16..62baa3214cc7bf5a91a723e1c5bba5ad6fb94402 100644 (file)
@@ -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;
index 21de30b4e2a16051490ce5a2548c3711a3f0284e..45b92a3d9d8e17a493c11df5cbd85b0caacacf36 100644 (file)
@@ -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)
index 616f78b24a795ae1f555921b1220f159df692db3..b6602490a247c55429bea653785cca1c397cfec5 100644 (file)
@@ -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) {
index 9b0b3fa4f8367c421abb1e58e3bf7b5e82b16aeb..e6126a4b95d3c92960d59775310bb11646eb1474 100644 (file)
@@ -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)
index 701a7d6a74d5c4cf1ac6e0bc697ce9455c0f3519..358e380eb7fa05ef031fd083b511df98503cb510 100644 (file)
@@ -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 */
 
@@ -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);
 
index c1e599a429af9714fa03a50de9664e7cfb2a3837..e0655949480a37b0aee037b63ea88ad093c22dc1 100644 (file)
@@ -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))) {
@@ -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);
 
index 548bb02c0ca6b049aa0f9949368931b4460b7994..6cb0eebdff894327abd69732fa2da8f6b56ab765 100644 (file)
@@ -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);
index 5165aa82bf7d47322f9abc1ad7e7fb161663b6e4..246f6122c9ee0f3fec46a1bdbc354e5ab10cbeb3 100644 (file)
@@ -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 +
@@ -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
@@ -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);
index 79a265ba92006b309796c525a5a2db3e9522f076..dfb64a5211b61e19f0fd03e26093fa261d6591bf 100644 (file)
@@ -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;
 
index b6e9587f05c7e764a5bbde91a51ced449803ba39..5679e7fcb6b0a47d972b34a58478aeaa1456acf0 100644 (file)
@@ -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);
index 568abed20eb20a097f43c108db7f274947ad604a..76baaa6be3934eef20c1c2a327a4f3b10c9bffed 100644 (file)
@@ -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)
index 586726a590d88149fa0fe769de37fa7aa14033a2..4f15665f0ad1c8fdd72367b8e952aa6cb6d376c6 100644 (file)
@@ -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;
 
index e2fe0e9ce0df08973e400db9754c842005ace49a..da52b594362a4d58299f8dbba503bde35e75fe20 100644 (file)
@@ -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);
index ab172e5f51d91f874f4dfa19f03fb1600c37d01c..5becc8acc8f4d0d3e306e823e2a7269c6b3977f5 100644 (file)
@@ -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;
 }
index ce34654794472d0a7b8c2574340c18cc7d594f7a..d7fd1ca807d2c32ca28c78bd852d50b66e001822 100644 (file)
@@ -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) *
index 0690679832d45fda5c4c945dc5e96f980eae5ca1..ea5cdbd8c2c326cd6f8702bfc2e6aa2e9fbca2c0 100644 (file)
@@ -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);
 
@@ -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)
index b4d01969e70089fd690f00cb5c79d7f6c6fd3298..1d2be4c2d34aa9d0d8b04581e392ad3479dceaef 100644 (file)
@@ -48,7 +48,32 @@ static inline void set_max_mapnr(unsigned long limit)
 static inline void set_max_mapnr(unsigned long limit) { }
 #endif
 
-extern unsigned long totalram_pages;
+extern atomic_long_t _totalram_pages;
+static inline unsigned long totalram_pages(void)
+{
+       return (unsigned long)atomic_long_read(&_totalram_pages);
+}
+
+static inline void totalram_pages_inc(void)
+{
+       atomic_long_inc(&_totalram_pages);
+}
+
+static inline void totalram_pages_dec(void)
+{
+       atomic_long_dec(&_totalram_pages);
+}
+
+static inline void totalram_pages_add(long count)
+{
+       atomic_long_add(count, &_totalram_pages);
+}
+
+static inline void totalram_pages_set(long val)
+{
+       atomic_long_set(&_totalram_pages, val);
+}
+
 extern void * high_memory;
 extern int page_cluster;
 
index a8f6d5d89524d3163a0c8f1dc835259f8d1dea51..77459d69501002293568e481c82f821f102f5479 100644 (file)
@@ -310,7 +310,6 @@ void workingset_update_node(struct xa_node *node);
 } while (0)
 
 /* linux/mm/page_alloc.c */
-extern unsigned long totalram_pages;
 extern unsigned long totalreserve_pages;
 extern unsigned long nr_free_buffer_pages(void);
 extern unsigned long nr_free_pagecache_pages(void);
index 8617a326e9f536e30924c52109ca0e9b2932c9cf..c979605fe806e503e8412d74c921d783601d7691 100644 (file)
@@ -744,7 +744,7 @@ void __init __weak arch_task_cache_init(void) { }
 static void set_max_threads(unsigned int max_threads_suggested)
 {
        u64 threads;
-       unsigned long nr_pages = totalram_pages;
+       unsigned long nr_pages = totalram_pages();
 
        /*
         * The number of threads shall be limited such that the thread
index 7e967ca98d926d53e45a181190a7b3f2bd026082..d7140447be75b809c5f5979d7be280c2f46de1c2 100644 (file)
@@ -152,7 +152,7 @@ int sanity_check_segment_list(struct kimage *image)
        int i;
        unsigned long nr_segments = image->nr_segments;
        unsigned long total_pages = 0;
-       unsigned long nr_pages = totalram_pages;
+       unsigned long nr_pages = totalram_pages();
 
        /*
         * Verify we have good destination addresses.  The caller is
index b0308a2c6000d3bf34b897a5a2d248049cb4c492..640b2034edd660da46372b38d8eca7b37bd482fc 100644 (file)
@@ -105,7 +105,7 @@ unsigned long image_size;
 
 void __init hibernate_image_size_init(void)
 {
-       image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE;
+       image_size = ((totalram_pages() * 2) / 5) * PAGE_SIZE;
 }
 
 /*
index 59db3223a5d62a9ea26f506ed3fd9fe6d54d2cf2..107b10f9878ef73bb936721728d28b39612884bd 100644 (file)
@@ -105,9 +105,8 @@ static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
 }
 #endif
 
-unsigned long totalhigh_pages __read_mostly;
-EXPORT_SYMBOL(totalhigh_pages);
-
+atomic_long_t _totalhigh_pages __read_mostly;
+EXPORT_SYMBOL(_totalhigh_pages);
 
 EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
 
index e84a10b0d310d0be5acebedcd2d422fe5e928f86..da6682bb69aae13df0314298b102fbfe339af2ec 100644 (file)
@@ -420,7 +420,7 @@ static int __init hugepage_init(void)
         * where the extra memory used could hurt more than TLB overhead
         * is likely to save.  The admin can still enable it through /sys.
         */
-       if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
+       if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
                transparent_hugepage_flags = 0;
                return 0;
        }
index 57334ef2d7efbcb9564bca02afd9d58a59589147..978bc4a3eb51a8d2db006e52a97b52de400bc8b1 100644 (file)
@@ -237,7 +237,7 @@ void quarantine_reduce(void)
         * Update quarantine size in case of hotplug. Allocate a fraction of
         * the installed memory to quarantine minus per-cpu queue limits.
         */
-       total_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
+       total_size = (totalram_pages() << PAGE_SHIFT) /
                QUARANTINE_FRACTION;
        percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
        new_quarantine_size = (total_size < percpu_quarantines) ?
index 0068f87af1e8c46e98facfb9131269cc18a5f45c..a53d8697612c5cd7df86bc059e1c01d2a3a77e29 100644 (file)
@@ -1576,7 +1576,7 @@ void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
 
        for (; cursor < end; cursor++) {
                memblock_free_pages(pfn_to_page(cursor), cursor, 0);
-               totalram_pages++;
+               totalram_pages_inc();
        }
 }
 
@@ -1978,7 +1978,7 @@ unsigned long __init memblock_free_all(void)
        reset_all_zones_managed_pages();
 
        pages = free_low_memory_core_early();
-       totalram_pages += pages;
+       totalram_pages_add(pages);
 
        return pages;
 }
index 6838a530789b414bcda3a2fca71747d067760118..33917105a3a28ce4d791fb6d47144d3feef403f2 100644 (file)
@@ -146,7 +146,7 @@ static void __meminit mm_compute_batch(void)
        s32 batch = max_t(s32, nr*2, 32);
 
        /* batch size set to 0.4% of (total memory/#cpus), or max int32 */
-       memsized_batch = min_t(u64, (totalram_pages/nr)/256, 0x7fffffff);
+       memsized_batch = min_t(u64, (totalram_pages()/nr)/256, 0x7fffffff);
 
        vm_committed_as_batch = max_t(s32, memsized_batch, batch);
 }
index 6589f60d50186aca3812be1a9ae3d8ba7369b87b..21d487749e1da86b4c0a34503bf4b2ea68f788f4 100644 (file)
@@ -269,7 +269,7 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc)
        }
 
        /* Default to all available memory */
-       oc->totalpages = totalram_pages + total_swap_pages;
+       oc->totalpages = totalram_pages() + total_swap_pages;
 
        if (!IS_ENABLED(CONFIG_NUMA))
                return CONSTRAINT_NONE;
index 4b5c4ff68f186d8627fc858b7e3a09af4a1eaf64..eb2027892ef9077ffb2643c0b20939810dadd397 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <linux/stddef.h>
 #include <linux/mm.h>
+#include <linux/highmem.h>
 #include <linux/swap.h>
 #include <linux/interrupt.h>
 #include <linux/pagemap.h>
@@ -124,7 +125,8 @@ EXPORT_SYMBOL(node_states);
 /* Protect totalram_pages and zone->managed_pages */
 static DEFINE_SPINLOCK(managed_page_count_lock);
 
-unsigned long totalram_pages __read_mostly;
+atomic_long_t _totalram_pages __read_mostly;
+EXPORT_SYMBOL(_totalram_pages);
 unsigned long totalreserve_pages __read_mostly;
 unsigned long totalcma_pages __read_mostly;
 
@@ -4747,11 +4749,11 @@ EXPORT_SYMBOL_GPL(si_mem_available);
 
 void si_meminfo(struct sysinfo *val)
 {
-       val->totalram = totalram_pages;
+       val->totalram = totalram_pages();
        val->sharedram = global_node_page_state(NR_SHMEM);
        val->freeram = global_zone_page_state(NR_FREE_PAGES);
        val->bufferram = nr_blockdev_pages();
-       val->totalhigh = totalhigh_pages;
+       val->totalhigh = totalhigh_pages();
        val->freehigh = nr_free_highpages();
        val->mem_unit = PAGE_SIZE;
 }
@@ -7077,10 +7079,10 @@ void adjust_managed_page_count(struct page *page, long count)
 {
        spin_lock(&managed_page_count_lock);
        atomic_long_add(count, &page_zone(page)->managed_pages);
-       totalram_pages += count;
+       totalram_pages_add(count);
 #ifdef CONFIG_HIGHMEM
        if (PageHighMem(page))
-               totalhigh_pages += count;
+               totalhigh_pages_add(count);
 #endif
        spin_unlock(&managed_page_count_lock);
 }
@@ -7123,9 +7125,9 @@ EXPORT_SYMBOL(free_reserved_area);
 void free_highmem_page(struct page *page)
 {
        __free_reserved_page(page);
-       totalram_pages++;
+       totalram_pages_inc();
        atomic_long_inc(&page_zone(page)->managed_pages);
-       totalhigh_pages++;
+       totalhigh_pages_inc();
 }
 #endif
 
@@ -7174,10 +7176,10 @@ void __init mem_init_print_info(const char *str)
                physpages << (PAGE_SHIFT - 10),
                codesize >> 10, datasize >> 10, rosize >> 10,
                (init_data_size + init_code_size) >> 10, bss_size >> 10,
-               (physpages - totalram_pages - totalcma_pages) << (PAGE_SHIFT - 10),
+               (physpages - totalram_pages() - totalcma_pages) << (PAGE_SHIFT - 10),
                totalcma_pages << (PAGE_SHIFT - 10),
 #ifdef CONFIG_HIGHMEM
-               totalhigh_pages << (PAGE_SHIFT - 10),
+               totalhigh_pages() << (PAGE_SHIFT - 10),
 #endif
                str ? ", " : "", str ? str : "");
 }
index b1f0f54470fb4d98002f854e4e24f0284cadcca8..6ece1e2fe76eb6b6670fc95fc9cf50d294e821cc 100644 (file)
@@ -109,13 +109,14 @@ struct shmem_falloc {
 #ifdef CONFIG_TMPFS
 static unsigned long shmem_default_max_blocks(void)
 {
-       return totalram_pages / 2;
+       return totalram_pages() / 2;
 }
 
 static unsigned long shmem_default_max_inodes(void)
 {
-       unsigned long nr_pages = totalram_pages;
-       return min(nr_pages - totalhigh_pages, nr_pages / 2);
+       unsigned long nr_pages = totalram_pages();
+
+       return min(nr_pages - totalhigh_pages(), nr_pages / 2);
 }
 #endif
 
@@ -3302,7 +3303,7 @@ static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
                        size = memparse(value,&rest);
                        if (*rest == '%') {
                                size <<= PAGE_SHIFT;
-                               size *= totalram_pages;
+                               size *= totalram_pages();
                                do_div(size, 100);
                                rest++;
                        }
index 01991060714c2216b8aab16243510f2f4a6347ab..73fe23e649c91abb135cd930a48475021b9f54d2 100644 (file)
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -1235,7 +1235,7 @@ void __init kmem_cache_init(void)
         * page orders on machines with more than 32MB of memory if
         * not overridden on the command line.
         */
-       if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
+       if (!slab_max_order_set && totalram_pages() > (32 << 20) >> PAGE_SHIFT)
                slab_max_order = SLAB_MAX_ORDER_HI;
 
        /* Bootstrap is tricky, because several objects are allocated
index 5d786019eab957dc3a0160d0accc541894a9fc3f..4d8a1f1afaab44976ce3f4bb1e9efd690276e819 100644 (file)
--- a/mm/swap.c
+++ b/mm/swap.c
@@ -1022,7 +1022,7 @@ EXPORT_SYMBOL(pagevec_lookup_range_nr_tag);
  */
 void __init swap_setup(void)
 {
-       unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
+       unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
 
        /* Use a smaller cluster for small-memory machines */
        if (megs < 16)
index 8bf08b5b576061bd68d62d7e5eb15c89f3e1c5ad..4df23d64aac7ae0c4846ba72f31ee1f20a21c143 100644 (file)
--- a/mm/util.c
+++ b/mm/util.c
@@ -593,7 +593,7 @@ unsigned long vm_commit_limit(void)
        if (sysctl_overcommit_kbytes)
                allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
        else
-               allowed = ((totalram_pages - hugetlb_total_pages())
+               allowed = ((totalram_pages() - hugetlb_total_pages())
                           * sysctl_overcommit_ratio / 100);
        allowed += total_swap_pages;
 
index 97d4b25d0373102a197ba1de455a944cf7204061..871e41c55e239afb83051d8e73fde088d746ba37 100644 (file)
@@ -1634,7 +1634,7 @@ void *vmap(struct page **pages, unsigned int count,
 
        might_sleep();
 
-       if (count > totalram_pages)
+       if (count > totalram_pages())
                return NULL;
 
        size = (unsigned long)count << PAGE_SHIFT;
@@ -1739,7 +1739,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
        unsigned long real_size = size;
 
        size = PAGE_ALIGN(size);
-       if (!size || (size >> PAGE_SHIFT) > totalram_pages)
+       if (!size || (size >> PAGE_SHIFT) > totalram_pages())
                goto fail;
 
        area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
index d46f8c92aa2fe736a1248ae30e0fb73a17b58a3e..dcb994f2acc2e600f39e2338d16f7bd839aefd2d 100644 (file)
@@ -549,7 +549,7 @@ static int __init workingset_init(void)
         * double the initial memory by using totalram_pages as-is.
         */
        timestamp_bits = BITS_PER_LONG - EVICTION_SHIFT;
-       max_order = fls_long(totalram_pages - 1);
+       max_order = fls_long(totalram_pages() - 1);
        if (max_order > timestamp_bits)
                bucket_order = max_order - timestamp_bits;
        pr_info("workingset: timestamp_bits=%d max_order=%d bucket_order=%u\n",
index cd91fd9d96b814d145e378b573dd289fb501e64e..a4e4d36ec085853740b0fc07e2412333990a9a81 100644 (file)
@@ -219,8 +219,8 @@ static const struct zpool_ops zswap_zpool_ops = {
 
 static bool zswap_is_full(void)
 {
-       return totalram_pages * zswap_max_pool_percent / 100 <
-               DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
+       return totalram_pages() * zswap_max_pool_percent / 100 <
+                       DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
 }
 
 static void zswap_update_total_size(void)
index ff727ff61b5bfc3f6bf9d3fd4c078f24823d2bc4..0e2f71ab8367794ac3b059120713cb7495079ee2 100644 (file)
@@ -1131,7 +1131,7 @@ EXPORT_SYMBOL_GPL(dccp_debug);
 static int __init dccp_init(void)
 {
        unsigned long goal;
-       unsigned long nr_pages = totalram_pages;
+       unsigned long nr_pages = totalram_pages();
        int ehash_order, bhash_order, i;
        int rc;
 
index 1c002c0fb712a45e20b4e3daa72da4b6d19e935a..950613ee7881bc22ee0ebb106dbbacc4c35b7172 100644 (file)
@@ -1866,7 +1866,7 @@ void __init dn_route_init(void)
        dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
        add_timer(&dn_route_timer);
 
-       goal = totalram_pages >> (26 - PAGE_SHIFT);
+       goal = totalram_pages() >> (26 - PAGE_SHIFT);
 
        for(order = 0; (1UL << order) < goal; order++)
                /* NOTHING */;
index 03b51cdcc731f5d85cbe51337f864bc8abc40135..b467a7cabf40e6af2ce69649f3ca48635bf06319 100644 (file)
@@ -1000,7 +1000,7 @@ static int __net_init tcp_net_metrics_init(struct net *net)
 
        slots = tcpmhash_entries;
        if (!slots) {
-               if (totalram_pages >= 128 * 1024)
+               if (totalram_pages() >= 128 * 1024)
                        slots = 16 * 1024;
                else
                        slots = 8 * 1024;
index 5eb99083034842f96750a3fa92733d87600a94bc..741b533148baedd70cd4184ba47bb507b055c142 100644 (file)
@@ -2248,7 +2248,7 @@ static __always_inline unsigned int total_extension_size(void)
 
 int nf_conntrack_init_start(void)
 {
-       unsigned long nr_pages = totalram_pages;
+       unsigned long nr_pages = totalram_pages();
        int max_factor = 8;
        int ret = -ENOMEM;
        int i;
index 88b520ba2abcc2aa48343670e8898f9acde3bf39..8d86e39d6280a6112ca37ecd1732da70819b1f98 100644 (file)
@@ -274,7 +274,7 @@ static int htable_create(struct net *net, struct hashlimit_cfg3 *cfg,
        struct xt_hashlimit_htable *hinfo;
        const struct seq_operations *ops;
        unsigned int size, i;
-       unsigned long nr_pages = totalram_pages;
+       unsigned long nr_pages = totalram_pages();
        int ret;
 
        if (cfg->size) {
index a5b24182b3cc137999543ea6dfb003becd7b3c8a..d5878ae5584090dbfd2841953a87088538593908 100644 (file)
@@ -1368,7 +1368,7 @@ static __init int sctp_init(void)
        int status = -EINVAL;
        unsigned long goal;
        unsigned long limit;
-       unsigned long nr_pages = totalram_pages;
+       unsigned long nr_pages = totalram_pages();
        int max_share;
        int order;
        int num_entries;
index 16bd18747cfa0280c400f647854e6cbbe09e6d1d..d6f32807b3479479f68f212d0117ec3fc85f36e9 100644 (file)
@@ -106,7 +106,7 @@ void ima_add_kexec_buffer(struct kimage *image)
                kexec_segment_size = ALIGN(ima_get_binary_runtime_size() +
                                           PAGE_SIZE / 2, PAGE_SIZE);
        if ((kexec_segment_size == ULONG_MAX) ||
-           ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages / 2)) {
+           ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages() / 2)) {
                pr_err("Binary measurement list too large.\n");
                return;
        }