[v2,0/4] Add uprobes support for LoongArch

Message ID 1681464781-4428-1-git-send-email-yangtiezhu@loongson.cn
Headers
Series Add uprobes support for LoongArch |

Message

Tiezhu Yang April 14, 2023, 9:32 a.m. UTC
  v2:
  -- Move the functions to inst.c in patch #1
  -- Pass around union for insns_not_supported(),
     insns_need_simulation() and arch_simulate_insn()

v1:
  -- Split the RFC patch #2 into two patches
  -- Use larch_insn_gen_break() to generate break insns
     for kprobes and uprobes
  -- Pass around instruction word instead of union for
     insns_not_supported(), insns_need_simulation() and
     arch_simulate_insn() to avoid type conversion for callers
  -- Add a simple test case for uprobes in the commit message

Tiezhu Yang (4):
  LoongArch: Move three functions from kprobes.c to inst.c
  LoongArch: Add larch_insn_gen_break() to generate break insns
  LoongArch: Use larch_insn_gen_break() for kprobes
  LoongArch: Add uprobes support

 arch/loongarch/Kconfig               |   3 +
 arch/loongarch/include/asm/inst.h    |  16 ++++
 arch/loongarch/include/asm/kprobes.h |   2 +-
 arch/loongarch/include/asm/uprobes.h |  36 +++++++++
 arch/loongarch/kernel/Makefile       |   1 +
 arch/loongarch/kernel/inst.c         |  48 ++++++++++++
 arch/loongarch/kernel/kprobes.c      |  75 ++++--------------
 arch/loongarch/kernel/traps.c        |   9 +--
 arch/loongarch/kernel/uprobes.c      | 142 +++++++++++++++++++++++++++++++++++
 9 files changed, 265 insertions(+), 67 deletions(-)
 create mode 100644 arch/loongarch/include/asm/uprobes.h
 create mode 100644 arch/loongarch/kernel/uprobes.c
  

Comments

Hengqi Chen April 17, 2023, 3:17 a.m. UTC | #1
Hi Tiezhu:

On 2023/4/14 17:32, Tiezhu Yang wrote:
> v2:
>   -- Move the functions to inst.c in patch #1
>   -- Pass around union for insns_not_supported(),
>      insns_need_simulation() and arch_simulate_insn()
> 
> v1:
>   -- Split the RFC patch #2 into two patches
>   -- Use larch_insn_gen_break() to generate break insns
>      for kprobes and uprobes
>   -- Pass around instruction word instead of union for
>      insns_not_supported(), insns_need_simulation() and
>      arch_simulate_insn() to avoid type conversion for callers
>   -- Add a simple test case for uprobes in the commit message
> 
> Tiezhu Yang (4):
>   LoongArch: Move three functions from kprobes.c to inst.c
>   LoongArch: Add larch_insn_gen_break() to generate break insns
>   LoongArch: Use larch_insn_gen_break() for kprobes
>   LoongArch: Add uprobes support
> 
>  arch/loongarch/Kconfig               |   3 +
>  arch/loongarch/include/asm/inst.h    |  16 ++++
>  arch/loongarch/include/asm/kprobes.h |   2 +-
>  arch/loongarch/include/asm/uprobes.h |  36 +++++++++
>  arch/loongarch/kernel/Makefile       |   1 +
>  arch/loongarch/kernel/inst.c         |  48 ++++++++++++
>  arch/loongarch/kernel/kprobes.c      |  75 ++++--------------
>  arch/loongarch/kernel/traps.c        |   9 +--
>  arch/loongarch/kernel/uprobes.c      | 142 +++++++++++++++++++++++++++++++++++
>  9 files changed, 265 insertions(+), 67 deletions(-)
>  create mode 100644 arch/loongarch/include/asm/uprobes.h
>  create mode 100644 arch/loongarch/kernel/uprobes.c
> 

I've test this series and found one corner case:

#include <pthread.h>
#include <stdio.h>
#include <thread>

static pthread_spinlock_t lock;
static int count = 0;
static const int n = 10000;

int main()
{
	int ret;

	ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
	if (ret)
		return -1;

	auto t1 = std::thread([&](){
		for (auto i = 0; i < n; i++) {
			pthread_spin_lock(&lock);
			count++;
			pthread_spin_unlock(&lock);
		}
	});

	auto t2 = std::thread([&](){
		for (auto i = 0; i < n; i++) {
			pthread_spin_lock(&lock);
			count++;
			pthread_spin_unlock(&lock);
		}
	});

	auto t3 = std::thread([&](){
		for (auto i = 0; i < n; i++) {
			pthread_spin_lock(&lock);
			count++;
			pthread_spin_unlock(&lock);
		}
	});

	t1.join();
	t2.join();
	t3.join();

	pthread_spin_destroy(&lock);

	printf("%d\n", count);
	return 0;
}

When I try to uprobe the pthread_spin_lock calls, the application core dumped.
On X86, this will return -EINVAL instead of crashing the userspace application.
Probably, we should add more opcode to insns_not_supported().

Cheers,
---
Hengqi
  
Tiezhu Yang April 17, 2023, 9:22 a.m. UTC | #2
On 04/17/2023 11:17 AM, Hengqi Chen wrote:
> Hi Tiezhu:
>

...

>
> When I try to uprobe the pthread_spin_lock calls, the application core dumped.
> On X86, this will return -EINVAL instead of crashing the userspace application.
> Probably, we should add more opcode to insns_not_supported().
>

Yes, the atomic memory access instructions such as amswap_db.w
should not be supported, like llsc instructions, let me add them
in insns_not_supported(), thank you.

Thanks,
Tiezhu