From 38976141fa3954a938f215772e23a2a60ad7c3a3 Mon Sep 17 00:00:00 2001
From: Kyle Yan <kyan@codeaurora.org>
Date: Mon, 23 Jan 2017 15:53:12 -0800
Subject: [PATCH] ldrp-v2: Add new method of finding number of CPUs as of
 kernel 4.5

As of commit c4c54dd1 (kernel/cpu.c: change type of cpu_possible_bits
and friends), cpu_present_bits no longer exists. Add new method of finding
number of cpus and also change other functions to use the get_num_cpus
API instead of defining it again.

Change-Id: I6d38d505ba7674b94e8481e6679226803eba977e
---
 linux-ramdump-parser-v2/debug_image_v2.py      | 5 ++---
 linux-ramdump-parser-v2/parsers/irqstate.py    | 6 ++----
 linux-ramdump-parser-v2/parsers/slabinfo.py    | 6 ++----
 linux-ramdump-parser-v2/parsers/slabsummary.py | 6 ++----
 linux-ramdump-parser-v2/parsers/workqueue.py   | 6 ++----
 linux-ramdump-parser-v2/ramdump.py             | 8 +++++++-
 6 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/linux-ramdump-parser-v2/debug_image_v2.py b/linux-ramdump-parser-v2/debug_image_v2.py
index 5545f50..8d0e7b1 100755
--- a/linux-ramdump-parser-v2/debug_image_v2.py
+++ b/linux-ramdump-parser-v2/debug_image_v2.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+# Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 and
@@ -452,8 +452,7 @@ class DebugImage_v2():
             'msm_dump_table_ids', MAX_NUM_ENTRIES)
         self.dump_data_id_lookup_table = ram_dump.gdbmi.get_enum_lookup_table(
             'msm_dump_data_ids', MAX_NUM_ENTRIES)
-        cpu_present_bits = ram_dump.read_word('cpu_present_bits')
-        cpus = bin(cpu_present_bits).count('1')
+        cpus = ram_dump.get_num_cpus()
         # per cpu entries
         for i in range(1, cpus):
 
diff --git a/linux-ramdump-parser-v2/parsers/irqstate.py b/linux-ramdump-parser-v2/parsers/irqstate.py
index b9f8d3e..3ef4063 100755
--- a/linux-ramdump-parser-v2/parsers/irqstate.py
+++ b/linux-ramdump-parser-v2/parsers/irqstate.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
+# Copyright (c) 2012-2015, 2017 The Linux Foundation. All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 and
@@ -20,9 +20,7 @@ class IrqParse(RamParser):
         print_out_str(
             '=========================== IRQ STATE ===============================')
         per_cpu_offset_addr = ram_dump.address_of('__per_cpu_offset')
-        cpu_present_bits_addr = ram_dump.address_of('cpu_present_bits')
-        cpu_present_bits = ram_dump.read_word(cpu_present_bits_addr)
-        cpus = bin(cpu_present_bits).count('1')
+        cpus = ram_dump.get_num_cpus()
         irq_desc = ram_dump.address_of('irq_desc')
         foo, irq_desc_size = ram_dump.unwind_lookup(irq_desc, 1)
         h_irq_offset = ram_dump.field_offset('struct irq_desc', 'handle_irq')
diff --git a/linux-ramdump-parser-v2/parsers/slabinfo.py b/linux-ramdump-parser-v2/parsers/slabinfo.py
index 460c08e..e65fb15 100644
--- a/linux-ramdump-parser-v2/parsers/slabinfo.py
+++ b/linux-ramdump-parser-v2/parsers/slabinfo.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+# Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 and
@@ -284,9 +284,7 @@ class Slabinfo(RamParser):
     def validate_slab_cache(self, slab_out, input_slabname,  map_fn):
         slab_name_found = False
         original_slab = self.ramdump.address_of('slab_caches')
-        cpu_present_bits_addr = self.ramdump.address_of('cpu_present_bits')
-        cpu_present_bits = self.ramdump.read_word(cpu_present_bits_addr)
-        cpus = bin(cpu_present_bits).count('1')
+        cpus = self.ramdump.get_num_cpus()
         offsetof = struct_member_offset(self.ramdump)
         self.initializeOffset()
         slab_list_offset = g_offsetof.kmemcache_list
diff --git a/linux-ramdump-parser-v2/parsers/slabsummary.py b/linux-ramdump-parser-v2/parsers/slabsummary.py
index 3ae6313..abe47dc 100644
--- a/linux-ramdump-parser-v2/parsers/slabsummary.py
+++ b/linux-ramdump-parser-v2/parsers/slabsummary.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+# Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 and
@@ -58,9 +58,7 @@ class Slabinfo_summary(RamParser):
         slab_summary = {}
         nCounter = 0
         original_slab = self.ramdump.address_of('slab_caches')
-        cpu_present_bits_addr = self.ramdump.address_of('cpu_present_bits')
-        cpu_present_bits = self.ramdump.read_word(cpu_present_bits_addr)
-        cpus = bin(cpu_present_bits).count('1')
+        cpus = self.ramdump.get_num_cpus()
         slab_list_offset = self.ramdump.field_offset(
             'struct kmem_cache', 'list')
         slab_name_offset = self.ramdump.field_offset(
diff --git a/linux-ramdump-parser-v2/parsers/workqueue.py b/linux-ramdump-parser-v2/parsers/workqueue.py
index 21c7b92..86e2312 100644
--- a/linux-ramdump-parser-v2/parsers/workqueue.py
+++ b/linux-ramdump-parser-v2/parsers/workqueue.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
+# Copyright (c) 2012-2015, 2017 The Linux Foundation. All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 and
@@ -212,9 +212,7 @@ class Workqueues(RamParser):
         worker_pool_size = ram_dump.sizeof('struct worker_pool')
         pending_work_offset = ram_dump.field_offset(
             'struct worker_pool', 'worklist')
-        cpu_present_bits_addr = ram_dump.address_of('cpu_present_bits')
-        cpu_present_bits = ram_dump.read_word(cpu_present_bits_addr)
-        cpus = bin(cpu_present_bits).count('1')
+        cpus = ram_dump.get_num_cpus()
 
         s = '<'
         for a in range(0, 64):
diff --git a/linux-ramdump-parser-v2/ramdump.py b/linux-ramdump-parser-v2/ramdump.py
index 58b332f..289b850 100755
--- a/linux-ramdump-parser-v2/ramdump.py
+++ b/linux-ramdump-parser-v2/ramdump.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+# Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 and
@@ -1399,8 +1399,14 @@ class RamDump():
 
     def get_num_cpus(self):
         """Gets the number of CPUs in the system."""
+        major, minor, patch = self.kernel_version
         cpu_present_bits_addr = self.address_of('cpu_present_bits')
         cpu_present_bits = self.read_word(cpu_present_bits_addr)
+
+        if (major, minor) >= (4, 5):
+            cpu_present_bits_addr = self.address_of('__cpu_present_mask')
+            bits_offset = self.field_offset('struct cpumask', 'bits')
+            cpu_present_bits = self.read_word(cpu_present_bits_addr + bits_offset)
         return bin(cpu_present_bits).count('1')
 
     def iter_cpus(self):
-- 
GitLab