diff mbox series

[2/3] powerpc/pseries: Export hardware trace macro dump via debugfs

Message ID 20240620174614.53751-2-maddy@linux.ibm.com (mailing list archive)
State Changes Requested
Headers show
Series [1/3] powerpc/pseries: Macros and wrapper functions for H_HTM call | expand

Commit Message

Madhavan Srinivasan June 20, 2024, 5:46 p.m. UTC
This patch adds debugfs interface to export Hardware Trace Macro (HTM)
function data in a LPAR. New hypervisor call "H_HTM" has been
defined to setup, configure, control and dump the HTM data.
This patch supports only dumping of HTM data in a LPAR.
New debugfs folder called "htmdump" has been added under
/sys/kernel/debug/arch path which contains files need to
pass required parameters for the H_HTM dump function. New Kconfig
option called "CONFIG_HTMDUMP" has been in platform/pseries for the same.

With patch series applied and booted, list of files in debugfs path

# pwd
/sys/kernel/debug/powerpc/htmdump
# ls
coreindexonchip  htmtype  nodalchipindex  nodeindex  trace

Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
---
 arch/powerpc/platforms/pseries/Kconfig   |   8 ++
 arch/powerpc/platforms/pseries/Makefile  |   1 +
 arch/powerpc/platforms/pseries/htmdump.c | 130 +++++++++++++++++++++++
 3 files changed, 139 insertions(+)
 create mode 100644 arch/powerpc/platforms/pseries/htmdump.c

Comments

Ritesh Harjani (IBM) June 22, 2024, 7:40 a.m. UTC | #1
This is a generic review and I haven't looked into the PAPR spec for
htmdump hcall and it's interface.

Madhavan Srinivasan <maddy@linux.ibm.com> writes:

> This patch adds debugfs interface to export Hardware Trace Macro (HTM)
> function data in a LPAR. New hypervisor call "H_HTM" has been
> defined to setup, configure, control and dump the HTM data.
> This patch supports only dumping of HTM data in a LPAR.
> New debugfs folder called "htmdump" has been added under
> /sys/kernel/debug/arch path which contains files need to
> pass required parameters for the H_HTM dump function. New Kconfig
> option called "CONFIG_HTMDUMP" has been in platform/pseries for the same.
>
> With patch series applied and booted, list of files in debugfs path
>
> # pwd
> /sys/kernel/debug/powerpc/htmdump
> # ls
> coreindexonchip  htmtype  nodalchipindex  nodeindex  trace
>
> Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
> ---
>  arch/powerpc/platforms/pseries/Kconfig   |   8 ++
>  arch/powerpc/platforms/pseries/Makefile  |   1 +
>  arch/powerpc/platforms/pseries/htmdump.c | 130 +++++++++++++++++++++++
>  3 files changed, 139 insertions(+)
>  create mode 100644 arch/powerpc/platforms/pseries/htmdump.c
>
> diff --git a/arch/powerpc/platforms/pseries/Kconfig b/arch/powerpc/platforms/pseries/Kconfig
> index afc0f6a61337..46c0ea605e33 100644
> --- a/arch/powerpc/platforms/pseries/Kconfig
> +++ b/arch/powerpc/platforms/pseries/Kconfig
> @@ -128,6 +128,14 @@ config CMM
>  	  will be reused for other LPARs. The interface allows firmware to
>  	  balance memory across many LPARs.
>
> +config HTMDUMP
> +	tristate "PHYP HTM data dumper"

Not sure if we can make machine_device_initcall() as a tristate?
Did we try compiling it as a module?

It we would like to keep this as a module - then why not use module_init
call and then make it depend upon...

depends on PPC_PSERIES && DEBUG_FS (??)

> +	default y

and then since this is mostly a debug trace facility, then we need not enable
it by default right?

> +	help
> +	  Select this option, if you want to enable the kernel debugfs
> +	  interface to dump the Hardware Trace Macro (HTM) function data
> +	  in the LPAR.
> +
>  config HV_PERF_CTRS
>  	bool "Hypervisor supplied PMU events (24x7 & GPCI)"
>  	default y
> diff --git a/arch/powerpc/platforms/pseries/Makefile b/arch/powerpc/platforms/pseries/Makefile
> index 7bf506f6b8c8..3f3e3492e436 100644
> --- a/arch/powerpc/platforms/pseries/Makefile
> +++ b/arch/powerpc/platforms/pseries/Makefile
> @@ -19,6 +19,7 @@ obj-$(CONFIG_HVC_CONSOLE)	+= hvconsole.o
>  obj-$(CONFIG_HVCS)		+= hvcserver.o
>  obj-$(CONFIG_HCALL_STATS)	+= hvCall_inst.o
>  obj-$(CONFIG_CMM)		+= cmm.o
> +obj-$(CONFIG_HTMDUMP)		+= htmdump.o
>  obj-$(CONFIG_IO_EVENT_IRQ)	+= io_event_irq.o
>  obj-$(CONFIG_LPARCFG)		+= lparcfg.o
>  obj-$(CONFIG_IBMVIO)		+= vio.o
> diff --git a/arch/powerpc/platforms/pseries/htmdump.c b/arch/powerpc/platforms/pseries/htmdump.c
> new file mode 100644
> index 000000000000..540cdb7e069c
> --- /dev/null
> +++ b/arch/powerpc/platforms/pseries/htmdump.c
> @@ -0,0 +1,130 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) IBM Corporation, 2024
> + */
> +
> +#define pr_fmt(fmt) "htmdump: " fmt
> +
> +#include <linux/bitops.h>
> +#include <linux/string.h>
> +#include <linux/init.h>
> +#include <linux/moduleparam.h>
> +#include <linux/fs.h>
> +#include <linux/debugfs.h>
> +#include <linux/slab.h>
> +#include <linux/memory.h>
> +#include <linux/memory_hotplug.h>
> +#include <linux/numa.h>
> +#include <linux/memblock.h>
> +#include <asm/machdep.h>
> +#include <asm/plpar_wrappers.h>

Do we need all of the above?
e.g. slab, memory_hotplug etc are not needed IMO.

Maybe only?

#include <asm/hvcall.h>
#include <asm/io.h>
#include <asm/machdep.h>
#include <asm/plpar_wrappers.h>

#include <linux/debugfs.h>
#include <linux/module.h>

(module.h depending upon if we make it module_init())


> +
> +/* This enables us to keep track of the memory removed from each node. */
> +struct htmdump_entry {
> +	void *buf;
> +	struct dentry *dir;
> +	char name[16];
> +};
> +
> +static u32 nodeindex = 0;
> +static u32 nodalchipindex = 0;
> +static u32 coreindexonchip = 0;
> +static u32 htmtype = 0;
> +
> +#define BUFFER_SIZE PAGE_SIZE
> +
> +static ssize_t htmdump_read(struct file *filp, char __user *ubuf,
> +			     size_t count, loff_t *ppos)
> +{
> +	struct htmdump_entry *ent = filp->private_data;
> +	unsigned long page, read_size, available;
> +	loff_t offset;
> +	long rc;
> +
> +	page = ALIGN_DOWN(*ppos, BUFFER_SIZE);
> +	offset = (*ppos) % BUFFER_SIZE;
> +
> +	rc = htm_get_dump_hardware(nodeindex, nodalchipindex, coreindexonchip,
> +				   htmtype, virt_to_phys(ent->buf), BUFFER_SIZE, page);
> +
> +	switch(rc) {
> +	case H_SUCCESS:
> +	case H_PARTIAL:
> +		break;
> +	case H_NOT_AVAILABLE:
> +		return 0;
> +	case H_BUSY:
> +	case H_LONG_BUSY_ORDER_1_MSEC:
> +	case H_LONG_BUSY_ORDER_10_MSEC:
> +	case H_LONG_BUSY_ORDER_100_MSEC:
> +	case H_LONG_BUSY_ORDER_1_SEC:
> +	case H_LONG_BUSY_ORDER_10_SEC:
> +	case H_LONG_BUSY_ORDER_100_SEC:
> +	case H_PARAMETER:
> +	case H_P2:
> +	case H_P3:
> +	case H_P4:
> +	case H_P5:
> +	case H_P6:
> +	case H_STATE:
> +	case H_AUTHORITY:
> +		return -EINVAL;
> +	}
> +
> +	available = BUFFER_SIZE - offset;
> +	read_size = min(count, available);
> +	*ppos += read_size;
> +	return simple_read_from_buffer(ubuf, count, &offset, ent->buf, available);
> +}
> +
> +static const struct file_operations htmdump_fops = {
> +	.llseek = default_llseek,
> +	.read	= htmdump_read,
> +	.open	= simple_open,
> +};
> +
> +static struct dentry *htmdump_debugfs_dir;
> +
> +static int htmdump_init_debugfs(void)
> +{
> +	struct htmdump_entry *ent;
> +
> +	ent = kcalloc(1, sizeof(struct htmdump_entry), GFP_KERNEL);
> +	if (!ent) {
> +		pr_err("Failed to allocate ent\n");
> +		return -EINVAL;
> +	}
> +
> +	ent->buf = kmalloc(BUFFER_SIZE, GFP_KERNEL);
> +	if (!ent->buf) {
> +		pr_err("Failed to allocate htmdump buf\n");
> +		return -ENOMEM;
> +	}
> +
> +	pr_debug("%s: ent:%lx buf:%lx\n",
> +			__func__, (long unsigned int)ent, (long unsigned int)ent->buf);
> +
> +	htmdump_debugfs_dir = debugfs_create_dir("htmdump",
> +						  arch_debugfs_dir);
> +
> +	debugfs_create_u32("nodeindex", 0600,
> +			htmdump_debugfs_dir, &nodeindex);
> +	debugfs_create_u32("nodalchipindex", 0600,
> +			htmdump_debugfs_dir, &nodalchipindex);
> +	debugfs_create_u32("coreindexonchip", 0600,
> +			htmdump_debugfs_dir, &coreindexonchip);
> +	debugfs_create_u32("htmtype", 0600,
> +			htmdump_debugfs_dir, &htmtype);

minor nit: For all of the above. S_IRUSR | S_IWUSR instead of 0600.

> +	debugfs_create_file("trace", 0400, htmdump_debugfs_dir, ent, &htmdump_fops);

maybe S_IRUSR instead of 0400.

(makes it more readable).

> +
> +	return 0;
> +}
> +
> +static int htmdump_init(void)

maybe put it into __init section?

> +{
> +	if (htmdump_init_debugfs())
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +machine_device_initcall(pseries, htmdump_init);
> --
> 2.45.2
kernel test robot June 26, 2024, 12:58 a.m. UTC | #2
Hi Madhavan,

kernel test robot noticed the following build errors:

[auto build test ERROR on powerpc/next]
[also build test ERROR on powerpc/fixes linus/master v6.10-rc5 next-20240625]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Madhavan-Srinivasan/powerpc-pseries-Export-hardware-trace-macro-dump-via-debugfs/20240625-144003
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
patch link:    https://lore.kernel.org/r/20240620174614.53751-2-maddy%40linux.ibm.com
patch subject: [PATCH 2/3] powerpc/pseries: Export hardware trace macro dump via debugfs
config: powerpc-allmodconfig (https://download.01.org/0day-ci/archive/20240626/202406260849.z8VoytFS-lkp@intel.com/config)
compiler: powerpc64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240626/202406260849.z8VoytFS-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202406260849.z8VoytFS-lkp@intel.com/

All error/warnings (new ones prefixed by >>):

   In file included from arch/powerpc/platforms/pseries/htmdump.c:19:
>> arch/powerpc/include/asm/machdep.h:262:85: error: expected ')' before numeric constant
     262 | #define machine_device_initcall(mach, fn)       __define_machine_initcall(mach, fn, 6)
         |                                                                                     ^
   arch/powerpc/include/asm/machdep.h:248:61: note: in definition of macro '__define_machine_initcall'
     248 |         __define_initcall(__machine_initcall_##mach##_##fn, id);
         |                                                             ^~
   arch/powerpc/platforms/pseries/htmdump.c:130:1: note: in expansion of macro 'machine_device_initcall'
     130 | machine_device_initcall(pseries, htmdump_init);
         | ^~~~~~~~~~~~~~~~~~~~~~~
>> arch/powerpc/include/asm/machdep.h:244:27: warning: '__machine_initcall_pseries_htmdump_init' defined but not used [-Wunused-function]
     244 |         static int __init __machine_initcall_##mach##_##fn(void) { \
         |                           ^~~~~~~~~~~~~~~~~~~
   arch/powerpc/include/asm/machdep.h:262:49: note: in expansion of macro '__define_machine_initcall'
     262 | #define machine_device_initcall(mach, fn)       __define_machine_initcall(mach, fn, 6)
         |                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~
   arch/powerpc/platforms/pseries/htmdump.c:130:1: note: in expansion of macro 'machine_device_initcall'
     130 | machine_device_initcall(pseries, htmdump_init);
         | ^~~~~~~~~~~~~~~~~~~~~~~


vim +262 arch/powerpc/include/asm/machdep.h

^1da177e4c3f41 include/asm-ppc64/machdep.h        Linus Torvalds   2005-04-16  242  
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  243  #define __define_machine_initcall(mach, fn, id) \
800d68c3aa0dc3 include/asm-powerpc/machdep.h      Grant Likely     2007-12-02 @244  	static int __init __machine_initcall_##mach##_##fn(void) { \
800d68c3aa0dc3 include/asm-powerpc/machdep.h      Grant Likely     2007-12-02  245  		if (machine_is(mach)) return fn(); \
800d68c3aa0dc3 include/asm-powerpc/machdep.h      Grant Likely     2007-12-02  246  		return 0; \
800d68c3aa0dc3 include/asm-powerpc/machdep.h      Grant Likely     2007-12-02  247  	} \
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  248  	__define_initcall(__machine_initcall_##mach##_##fn, id);
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  249  
8d3c941e240ba2 arch/powerpc/include/asm/machdep.h Michael Ellerman 2014-07-15  250  #define machine_early_initcall(mach, fn)	__define_machine_initcall(mach, fn, early)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  251  #define machine_core_initcall(mach, fn)		__define_machine_initcall(mach, fn, 1)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  252  #define machine_core_initcall_sync(mach, fn)	__define_machine_initcall(mach, fn, 1s)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  253  #define machine_postcore_initcall(mach, fn)	__define_machine_initcall(mach, fn, 2)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  254  #define machine_postcore_initcall_sync(mach, fn)	__define_machine_initcall(mach, fn, 2s)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  255  #define machine_arch_initcall(mach, fn)		__define_machine_initcall(mach, fn, 3)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  256  #define machine_arch_initcall_sync(mach, fn)	__define_machine_initcall(mach, fn, 3s)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  257  #define machine_subsys_initcall(mach, fn)	__define_machine_initcall(mach, fn, 4)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  258  #define machine_subsys_initcall_sync(mach, fn)	__define_machine_initcall(mach, fn, 4s)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  259  #define machine_fs_initcall(mach, fn)		__define_machine_initcall(mach, fn, 5)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  260  #define machine_fs_initcall_sync(mach, fn)	__define_machine_initcall(mach, fn, 5s)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  261  #define machine_rootfs_initcall(mach, fn)	__define_machine_initcall(mach, fn, rootfs)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17 @262  #define machine_device_initcall(mach, fn)	__define_machine_initcall(mach, fn, 6)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  263  #define machine_device_initcall_sync(mach, fn)	__define_machine_initcall(mach, fn, 6s)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  264  #define machine_late_initcall(mach, fn)		__define_machine_initcall(mach, fn, 7)
7929d407e47fbf arch/powerpc/include/asm/machdep.h Matthew Leach    2012-12-17  265  #define machine_late_initcall_sync(mach, fn)	__define_machine_initcall(mach, fn, 7s)
800d68c3aa0dc3 include/asm-powerpc/machdep.h      Grant Likely     2007-12-02  266
Madhavan Srinivasan June 26, 2024, 3:51 a.m. UTC | #3
On 6/22/24 1:10 PM, Ritesh Harjani (IBM) wrote:
> This is a generic review and I haven't looked into the PAPR spec for
> htmdump hcall and it's interface.

Sure

> Madhavan Srinivasan <maddy@linux.ibm.com> writes:
>
>> This patch adds debugfs interface to export Hardware Trace Macro (HTM)
>> function data in a LPAR. New hypervisor call "H_HTM" has been
>> defined to setup, configure, control and dump the HTM data.
>> This patch supports only dumping of HTM data in a LPAR.
>> New debugfs folder called "htmdump" has been added under
>> /sys/kernel/debug/arch path which contains files need to
>> pass required parameters for the H_HTM dump function. New Kconfig
>> option called "CONFIG_HTMDUMP" has been in platform/pseries for the same.
>>
>> With patch series applied and booted, list of files in debugfs path
>>
>> # pwd
>> /sys/kernel/debug/powerpc/htmdump
>> # ls
>> coreindexonchip  htmtype  nodalchipindex  nodeindex  trace
>>
>> Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
>> ---
>>   arch/powerpc/platforms/pseries/Kconfig   |   8 ++
>>   arch/powerpc/platforms/pseries/Makefile  |   1 +
>>   arch/powerpc/platforms/pseries/htmdump.c | 130 +++++++++++++++++++++++
>>   3 files changed, 139 insertions(+)
>>   create mode 100644 arch/powerpc/platforms/pseries/htmdump.c
>>
>> diff --git a/arch/powerpc/platforms/pseries/Kconfig b/arch/powerpc/platforms/pseries/Kconfig
>> index afc0f6a61337..46c0ea605e33 100644
>> --- a/arch/powerpc/platforms/pseries/Kconfig
>> +++ b/arch/powerpc/platforms/pseries/Kconfig
>> @@ -128,6 +128,14 @@ config CMM
>>   	  will be reused for other LPARs. The interface allows firmware to
>>   	  balance memory across many LPARs.
>>
>> +config HTMDUMP
>> +	tristate "PHYP HTM data dumper"
> Not sure if we can make machine_device_initcall() as a tristate?
> Did we try compiling it as a module?
>
> It we would like to keep this as a module - then why not use module_init
> call and then make it depend upon...

I will make it as bool and add depends as suggested.

>
> depends on PPC_PSERIES && DEBUG_FS (??)
>
>> +	default y
> and then since this is mostly a debug trace facility, then we need not enable
> it by default right?

Yes, we want this to be there, it is up to hypervisor whether to permit 
the hcalls.

>
>> +	help
>> +	  Select this option, if you want to enable the kernel debugfs
>> +	  interface to dump the Hardware Trace Macro (HTM) function data
>> +	  in the LPAR.
>> +
>>   config HV_PERF_CTRS
>>   	bool "Hypervisor supplied PMU events (24x7 & GPCI)"
>>   	default y
>> diff --git a/arch/powerpc/platforms/pseries/Makefile b/arch/powerpc/platforms/pseries/Makefile
>> index 7bf506f6b8c8..3f3e3492e436 100644
>> --- a/arch/powerpc/platforms/pseries/Makefile
>> +++ b/arch/powerpc/platforms/pseries/Makefile
>> @@ -19,6 +19,7 @@ obj-$(CONFIG_HVC_CONSOLE)	+= hvconsole.o
>>   obj-$(CONFIG_HVCS)		+= hvcserver.o
>>   obj-$(CONFIG_HCALL_STATS)	+= hvCall_inst.o
>>   obj-$(CONFIG_CMM)		+= cmm.o
>> +obj-$(CONFIG_HTMDUMP)		+= htmdump.o
>>   obj-$(CONFIG_IO_EVENT_IRQ)	+= io_event_irq.o
>>   obj-$(CONFIG_LPARCFG)		+= lparcfg.o
>>   obj-$(CONFIG_IBMVIO)		+= vio.o
>> diff --git a/arch/powerpc/platforms/pseries/htmdump.c b/arch/powerpc/platforms/pseries/htmdump.c
>> new file mode 100644
>> index 000000000000..540cdb7e069c
>> --- /dev/null
>> +++ b/arch/powerpc/platforms/pseries/htmdump.c
>> @@ -0,0 +1,130 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (C) IBM Corporation, 2024
>> + */
>> +
>> +#define pr_fmt(fmt) "htmdump: " fmt
>> +
>> +#include <linux/bitops.h>
>> +#include <linux/string.h>
>> +#include <linux/init.h>
>> +#include <linux/moduleparam.h>
>> +#include <linux/fs.h>
>> +#include <linux/debugfs.h>
>> +#include <linux/slab.h>
>> +#include <linux/memory.h>
>> +#include <linux/memory_hotplug.h>
>> +#include <linux/numa.h>
>> +#include <linux/memblock.h>
>> +#include <asm/machdep.h>
>> +#include <asm/plpar_wrappers.h>
> Do we need all of the above?
> e.g. slab, memory_hotplug etc are not needed IMO.
>
> Maybe only?
>
> #include <asm/hvcall.h>
> #include <asm/io.h>
> #include <asm/machdep.h>
> #include <asm/plpar_wrappers.h>
>
> #include <linux/debugfs.h>
> #include <linux/module.h>
>
> (module.h depending upon if we make it module_init())
>
Yeah, my bad, Should have handled this. will fix it in v2

>> +
>> +/* This enables us to keep track of the memory removed from each node. */
>> +struct htmdump_entry {
>> +	void *buf;
>> +	struct dentry *dir;
>> +	char name[16];
>> +};
>> +
>> +static u32 nodeindex = 0;
>> +static u32 nodalchipindex = 0;
>> +static u32 coreindexonchip = 0;
>> +static u32 htmtype = 0;
>> +
>> +#define BUFFER_SIZE PAGE_SIZE
>> +
>> +static ssize_t htmdump_read(struct file *filp, char __user *ubuf,
>> +			     size_t count, loff_t *ppos)
>> +{
>> +	struct htmdump_entry *ent = filp->private_data;
>> +	unsigned long page, read_size, available;
>> +	loff_t offset;
>> +	long rc;
>> +
>> +	page = ALIGN_DOWN(*ppos, BUFFER_SIZE);
>> +	offset = (*ppos) % BUFFER_SIZE;
>> +
>> +	rc = htm_get_dump_hardware(nodeindex, nodalchipindex, coreindexonchip,
>> +				   htmtype, virt_to_phys(ent->buf), BUFFER_SIZE, page);
>> +
>> +	switch(rc) {
>> +	case H_SUCCESS:
>> +	case H_PARTIAL:
>> +		break;
>> +	case H_NOT_AVAILABLE:
>> +		return 0;
>> +	case H_BUSY:
>> +	case H_LONG_BUSY_ORDER_1_MSEC:
>> +	case H_LONG_BUSY_ORDER_10_MSEC:
>> +	case H_LONG_BUSY_ORDER_100_MSEC:
>> +	case H_LONG_BUSY_ORDER_1_SEC:
>> +	case H_LONG_BUSY_ORDER_10_SEC:
>> +	case H_LONG_BUSY_ORDER_100_SEC:
>> +	case H_PARAMETER:
>> +	case H_P2:
>> +	case H_P3:
>> +	case H_P4:
>> +	case H_P5:
>> +	case H_P6:
>> +	case H_STATE:
>> +	case H_AUTHORITY:
>> +		return -EINVAL;
>> +	}
>> +
>> +	available = BUFFER_SIZE - offset;
>> +	read_size = min(count, available);
>> +	*ppos += read_size;
>> +	return simple_read_from_buffer(ubuf, count, &offset, ent->buf, available);
>> +}
>> +
>> +static const struct file_operations htmdump_fops = {
>> +	.llseek = default_llseek,
>> +	.read	= htmdump_read,
>> +	.open	= simple_open,
>> +};
>> +
>> +static struct dentry *htmdump_debugfs_dir;
>> +
>> +static int htmdump_init_debugfs(void)
>> +{
>> +	struct htmdump_entry *ent;
>> +
>> +	ent = kcalloc(1, sizeof(struct htmdump_entry), GFP_KERNEL);
>> +	if (!ent) {
>> +		pr_err("Failed to allocate ent\n");
>> +		return -EINVAL;
>> +	}
>> +
>> +	ent->buf = kmalloc(BUFFER_SIZE, GFP_KERNEL);
>> +	if (!ent->buf) {
>> +		pr_err("Failed to allocate htmdump buf\n");
>> +		return -ENOMEM;
>> +	}
>> +
>> +	pr_debug("%s: ent:%lx buf:%lx\n",
>> +			__func__, (long unsigned int)ent, (long unsigned int)ent->buf);
>> +
>> +	htmdump_debugfs_dir = debugfs_create_dir("htmdump",
>> +						  arch_debugfs_dir);
>> +
>> +	debugfs_create_u32("nodeindex", 0600,
>> +			htmdump_debugfs_dir, &nodeindex);
>> +	debugfs_create_u32("nodalchipindex", 0600,
>> +			htmdump_debugfs_dir, &nodalchipindex);
>> +	debugfs_create_u32("coreindexonchip", 0600,
>> +			htmdump_debugfs_dir, &coreindexonchip);
>> +	debugfs_create_u32("htmtype", 0600,
>> +			htmdump_debugfs_dir, &htmtype);
> minor nit: For all of the above. S_IRUSR | S_IWUSR instead of 0600.
>
>> +	debugfs_create_file("trace", 0400, htmdump_debugfs_dir, ent, &htmdump_fops);
> maybe S_IRUSR instead of 0400.
>
> (makes it more readable).

ok will check and changes.

Thanks for the review comments.

Maddy


>
>> +
>> +	return 0;
>> +}
>> +
>> +static int htmdump_init(void)
> maybe put it into __init section?
>
>> +{
>> +	if (htmdump_init_debugfs())
>> +		return -EINVAL;
>> +
>> +	return 0;
>> +}
>> +machine_device_initcall(pseries, htmdump_init);
>> --
>> 2.45.2
Michael Ellerman June 26, 2024, 8:12 a.m. UTC | #4
Ritesh Harjani (IBM) <ritesh.list@gmail.com> writes:
> This is a generic review and I haven't looked into the PAPR spec for
> htmdump hcall and it's interface.
>
> Madhavan Srinivasan <maddy@linux.ibm.com> writes:
...
>> +
>> +	debugfs_create_u32("nodeindex", 0600,
>> +			htmdump_debugfs_dir, &nodeindex);
>> +	debugfs_create_u32("nodalchipindex", 0600,
>> +			htmdump_debugfs_dir, &nodalchipindex);
>> +	debugfs_create_u32("coreindexonchip", 0600,
>> +			htmdump_debugfs_dir, &coreindexonchip);
>> +	debugfs_create_u32("htmtype", 0600,
>> +			htmdump_debugfs_dir, &htmtype);
>
> minor nit: For all of the above. S_IRUSR | S_IWUSR instead of 0600.
>
>> +	debugfs_create_file("trace", 0400, htmdump_debugfs_dir, ent, &htmdump_fops);
>
> maybe S_IRUSR instead of 0400.

Actually we prefer the octal values, see:
  https://git.kernel.org/torvalds/c/57ad583f2086d55ada284c54bfc440123cf73964

cheers
diff mbox series

Patch

diff --git a/arch/powerpc/platforms/pseries/Kconfig b/arch/powerpc/platforms/pseries/Kconfig
index afc0f6a61337..46c0ea605e33 100644
--- a/arch/powerpc/platforms/pseries/Kconfig
+++ b/arch/powerpc/platforms/pseries/Kconfig
@@ -128,6 +128,14 @@  config CMM
 	  will be reused for other LPARs. The interface allows firmware to
 	  balance memory across many LPARs.
 
+config HTMDUMP
+	tristate "PHYP HTM data dumper"
+	default y
+	help
+	  Select this option, if you want to enable the kernel debugfs
+	  interface to dump the Hardware Trace Macro (HTM) function data
+	  in the LPAR.
+
 config HV_PERF_CTRS
 	bool "Hypervisor supplied PMU events (24x7 & GPCI)"
 	default y
diff --git a/arch/powerpc/platforms/pseries/Makefile b/arch/powerpc/platforms/pseries/Makefile
index 7bf506f6b8c8..3f3e3492e436 100644
--- a/arch/powerpc/platforms/pseries/Makefile
+++ b/arch/powerpc/platforms/pseries/Makefile
@@ -19,6 +19,7 @@  obj-$(CONFIG_HVC_CONSOLE)	+= hvconsole.o
 obj-$(CONFIG_HVCS)		+= hvcserver.o
 obj-$(CONFIG_HCALL_STATS)	+= hvCall_inst.o
 obj-$(CONFIG_CMM)		+= cmm.o
+obj-$(CONFIG_HTMDUMP)		+= htmdump.o
 obj-$(CONFIG_IO_EVENT_IRQ)	+= io_event_irq.o
 obj-$(CONFIG_LPARCFG)		+= lparcfg.o
 obj-$(CONFIG_IBMVIO)		+= vio.o
diff --git a/arch/powerpc/platforms/pseries/htmdump.c b/arch/powerpc/platforms/pseries/htmdump.c
new file mode 100644
index 000000000000..540cdb7e069c
--- /dev/null
+++ b/arch/powerpc/platforms/pseries/htmdump.c
@@ -0,0 +1,130 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) IBM Corporation, 2024
+ */
+
+#define pr_fmt(fmt) "htmdump: " fmt
+
+#include <linux/bitops.h>
+#include <linux/string.h>
+#include <linux/init.h>
+#include <linux/moduleparam.h>
+#include <linux/fs.h>
+#include <linux/debugfs.h>
+#include <linux/slab.h>
+#include <linux/memory.h>
+#include <linux/memory_hotplug.h>
+#include <linux/numa.h>
+#include <linux/memblock.h>
+#include <asm/machdep.h>
+#include <asm/plpar_wrappers.h>
+
+/* This enables us to keep track of the memory removed from each node. */
+struct htmdump_entry {
+	void *buf;
+	struct dentry *dir;
+	char name[16];
+};
+
+static u32 nodeindex = 0;
+static u32 nodalchipindex = 0;
+static u32 coreindexonchip = 0;
+static u32 htmtype = 0;
+
+#define BUFFER_SIZE PAGE_SIZE
+
+static ssize_t htmdump_read(struct file *filp, char __user *ubuf,
+			     size_t count, loff_t *ppos)
+{
+	struct htmdump_entry *ent = filp->private_data;
+	unsigned long page, read_size, available;
+	loff_t offset;
+	long rc;
+
+	page = ALIGN_DOWN(*ppos, BUFFER_SIZE);
+	offset = (*ppos) % BUFFER_SIZE;
+
+	rc = htm_get_dump_hardware(nodeindex, nodalchipindex, coreindexonchip,
+				   htmtype, virt_to_phys(ent->buf), BUFFER_SIZE, page);
+
+	switch(rc) {
+	case H_SUCCESS:
+	case H_PARTIAL:
+		break;
+	case H_NOT_AVAILABLE:
+		return 0;
+	case H_BUSY:
+	case H_LONG_BUSY_ORDER_1_MSEC:
+	case H_LONG_BUSY_ORDER_10_MSEC:
+	case H_LONG_BUSY_ORDER_100_MSEC:
+	case H_LONG_BUSY_ORDER_1_SEC:
+	case H_LONG_BUSY_ORDER_10_SEC:
+	case H_LONG_BUSY_ORDER_100_SEC:
+	case H_PARAMETER:
+	case H_P2:
+	case H_P3:
+	case H_P4:
+	case H_P5:
+	case H_P6:
+	case H_STATE:
+	case H_AUTHORITY:
+		return -EINVAL;
+	}
+
+	available = BUFFER_SIZE - offset;
+	read_size = min(count, available);
+	*ppos += read_size;
+	return simple_read_from_buffer(ubuf, count, &offset, ent->buf, available);
+}
+
+static const struct file_operations htmdump_fops = {
+	.llseek = default_llseek,
+	.read	= htmdump_read,
+	.open	= simple_open,
+};
+
+static struct dentry *htmdump_debugfs_dir;
+
+static int htmdump_init_debugfs(void)
+{
+	struct htmdump_entry *ent;
+
+	ent = kcalloc(1, sizeof(struct htmdump_entry), GFP_KERNEL);
+	if (!ent) {
+		pr_err("Failed to allocate ent\n");
+		return -EINVAL;
+	}
+
+	ent->buf = kmalloc(BUFFER_SIZE, GFP_KERNEL);
+	if (!ent->buf) {
+		pr_err("Failed to allocate htmdump buf\n");
+		return -ENOMEM;
+	}
+
+	pr_debug("%s: ent:%lx buf:%lx\n",
+			__func__, (long unsigned int)ent, (long unsigned int)ent->buf);
+
+	htmdump_debugfs_dir = debugfs_create_dir("htmdump",
+						  arch_debugfs_dir);
+
+	debugfs_create_u32("nodeindex", 0600,
+			htmdump_debugfs_dir, &nodeindex);
+	debugfs_create_u32("nodalchipindex", 0600,
+			htmdump_debugfs_dir, &nodalchipindex);
+	debugfs_create_u32("coreindexonchip", 0600,
+			htmdump_debugfs_dir, &coreindexonchip);
+	debugfs_create_u32("htmtype", 0600,
+			htmdump_debugfs_dir, &htmtype);
+	debugfs_create_file("trace", 0400, htmdump_debugfs_dir, ent, &htmdump_fops);
+
+	return 0;
+}
+
+static int htmdump_init(void)
+{
+	if (htmdump_init_debugfs())
+		return -EINVAL;
+
+	return 0;
+}
+machine_device_initcall(pseries, htmdump_init);