From nobody Sun Dec 24 08:23:24 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SyYvd2slKz54HdZ; Sun, 24 Dec 2023 08:23:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SyYvd1d7Nz4YKG; Sun, 24 Dec 2023 08:23:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703406205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oudxeeXtLqU+pgKplx4hih+uOQbT7DfwwEtZu/yzQ9U=; b=PLPeAUJh/oFIk8LNc6HK1JPboUG2NIY6GHCWh2/bk9Lut5ucqbE/C8UY8fOj4u85Z3VFvg Upn51YEIsQ2VIHb2J2BKdfiMffZypxYQqxkvh1SZrbR/xT1Qww4eHTXSJBc5WZt+kXP6J6 tcZ0abEUXPVMvcskU8okHI7FEHNnYd2GRx9/jmXuzNFBqfyhYnF7amc1jvv5e9YZzknxy7 EB0bZ8oPQ/ZNn3ffpxJuPwoCH+XT1wtSEvFY3MsjEtfdZ+MgNDLhD6oaHf/ECRpDvw9ecx f4sjXXkby2vNs+ZXTHlBnHzhM2QulF7Y+ZgvRU/eiWNmpI2LVXHyTPkA0A7KFw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703406205; a=rsa-sha256; cv=none; b=jFe4EmhwwpYoRQN8+fLGd3Y9CvooKga47T1AtsBV1l5cgVl2onRG6ofhz1KE9dyTvDMyyN McsJWtsIlg250vhfpWm93z8unt+x9e93AWSWte/608D6k0PNf+A9CoAu62i7006B+AGL2t z2Nhc/Xzt3hjcwf1dSQQXmnE3P87j3PZoKO2qn3BVA+ufzNwkeFFG4SOQ8o0637pknknY2 o+pREN4/qOhh7JBgxpQcsdJjxfOgaJ1A4x3ydXr+MR3XUcegg0FaG0yqmxFtYZRq5+59/w oQQQ9SwWA/iws9XZz9mlLTure1GM2IwTEQo7L1E+/inp+xe6INQiU+beCdWh8A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703406205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oudxeeXtLqU+pgKplx4hih+uOQbT7DfwwEtZu/yzQ9U=; b=ojcRsPvbC85RkIP4jSBMtbR/qV4hFF3xbSTQtyBlhZ5Wqgx7MruT/0DDeRwdQ0/RriYLhS 8FHkxN/gQf2sorAoSY5vBOMUVGZdvxuhjhlRCDpwgGj9meyFUMr6tHX3CM1rPv2VvrcJDC TpJvJHBWuCexkY5o5kvikJvw5By2J2qK5c2bENj3w3IMgYO3Pg5hdiSH0erBKu9z8Ztpgt HyeqUdskY87WWzulTc+jFsUqTU3PTcDzrDKVTHVR5kchfRpg6ki//CtcCZjsY/ve6M5vcM /Ka/1pnNbKR2+a4OXgoLiYl7evfvqK9wwF3zDMGRXVY7opHxDX+0LGZdjML5mw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4SyYvc6lBpz5Tn; Sun, 24 Dec 2023 08:23:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BO8NOfT087784; Sun, 24 Dec 2023 08:23:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BO8NODF087781; Sun, 24 Dec 2023 08:23:24 GMT (envelope-from git) Date: Sun, 24 Dec 2023 08:23:24 GMT Message-Id: <202312240823.3BO8NODF087781@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Vladimir Kondratyev Subject: git: 5a3bd281672b - main - LinuxKPI: Add explicit software context to FPU sections List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: wulf X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 5a3bd281672bd6bd8e18081c3928dfe76d330a5f Auto-Submitted: auto-generated The branch main has been updated by wulf: URL: https://cgit.FreeBSD.org/src/commit/?id=5a3bd281672bd6bd8e18081c3928dfe76d330a5f commit 5a3bd281672bd6bd8e18081c3928dfe76d330a5f Author: Vladimir Kondratyev AuthorDate: 2023-12-24 08:20:01 +0000 Commit: Vladimir Kondratyev CommitDate: 2023-12-24 08:20:01 +0000 LinuxKPI: Add explicit software context to FPU sections Amdgpu driver does a lot of memory allocations in FPU-protected sections of code for certain display cores, e.g. for DCN30. This does not work currently on FreeBSD as its malloc function can not be run within a critical section. Allocate memory for FPU context to overcome such restriction. Sponsored by: Serenity Cyber Security, LLC Reviewed by: manu (previous version), markj MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D42822 --- sys/compat/linuxkpi/common/include/linux/compat.h | 6 ++++++ sys/compat/linuxkpi/common/include/linux/sched.h | 2 ++ sys/compat/linuxkpi/common/src/linux_current.c | 18 ++++++++++++++++++ sys/compat/linuxkpi/common/src/linux_fpu.c | 14 ++++++++++---- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/compat.h b/sys/compat/linuxkpi/common/include/linux/compat.h index d1a02f612f42..7922e884beb1 100644 --- a/sys/compat/linuxkpi/common/include/linux/compat.h +++ b/sys/compat/linuxkpi/common/include/linux/compat.h @@ -33,6 +33,11 @@ #include #include +#if defined(__aarch64__) || defined(__arm__) || defined(__amd64__) || \ + defined(__i386__) || defined(__powerpc64__) +#define LKPI_HAVE_FPU_CTX +#endif + struct domainset; struct thread; struct task_struct; @@ -40,6 +45,7 @@ struct task_struct; extern int linux_alloc_current(struct thread *, int flags); extern void linux_free_current(struct task_struct *); extern struct domainset *linux_get_vm_domain_set(int node); +extern int linux_set_fpu_ctx(struct task_struct *); static inline void linux_set_current(struct thread *td) diff --git a/sys/compat/linuxkpi/common/include/linux/sched.h b/sys/compat/linuxkpi/common/include/linux/sched.h index 8cb6b12100d5..77c4f6d3f80f 100644 --- a/sys/compat/linuxkpi/common/include/linux/sched.h +++ b/sys/compat/linuxkpi/common/include/linux/sched.h @@ -64,6 +64,7 @@ #define TASK_COMM_LEN (MAXCOMLEN + 1) struct seq_file; +struct fpu_kern_ctx; struct work_struct; struct task_struct { @@ -89,6 +90,7 @@ struct task_struct { struct task_struct *group_leader; unsigned rcu_section[TS_RCU_TYPE_MAX]; unsigned int fpu_ctx_level; + struct fpu_kern_ctx *fpu_ctx; }; #define current ({ \ diff --git a/sys/compat/linuxkpi/common/src/linux_current.c b/sys/compat/linuxkpi/common/src/linux_current.c index bec53384361f..668185094cbe 100644 --- a/sys/compat/linuxkpi/common/src/linux_current.c +++ b/sys/compat/linuxkpi/common/src/linux_current.c @@ -43,6 +43,10 @@ #include #include +#ifdef LKPI_HAVE_FPU_CTX +#include +#endif + #ifdef DEV_APIC extern u_int first_msi_irq, num_msi_irqs; #endif @@ -153,6 +157,16 @@ linux_alloc_current(struct thread *td, int flags) return (0); } +int +linux_set_fpu_ctx(struct task_struct *task) +{ +#ifdef LKPI_HAVE_FPU_CTX + if (task->fpu_ctx == NULL && curthread->td_critnest == 0) + task->fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NOWAIT); +#endif + return (task->fpu_ctx != NULL ? 0 : ENOMEM); +} + struct mm_struct * linux_get_task_mm(struct task_struct *task) { @@ -176,6 +190,10 @@ void linux_free_current(struct task_struct *ts) { mmput(ts->mm); +#ifdef LKPI_HAVE_FPU_CTX + if (ts->fpu_ctx != NULL) + fpu_kern_free_ctx(ts->fpu_ctx); +#endif uma_zfree(linux_current_zone, ts); } diff --git a/sys/compat/linuxkpi/common/src/linux_fpu.c b/sys/compat/linuxkpi/common/src/linux_fpu.c index b26dce98774b..d510bc208b88 100644 --- a/sys/compat/linuxkpi/common/src/linux_fpu.c +++ b/sys/compat/linuxkpi/common/src/linux_fpu.c @@ -30,11 +30,12 @@ #include #include +#include #include #include -#if defined(__aarch64__) || defined(__amd64__) || defined(__i386__) +#ifdef LKPI_HAVE_FPU_CTX #include @@ -47,15 +48,20 @@ void lkpi_kernel_fpu_begin(void) { - if ((current->fpu_ctx_level)++ == 0) - fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX); + int err; + + if ((current->fpu_ctx_level)++ == 0) { + err = linux_set_fpu_ctx(current); + fpu_kern_enter(curthread, current->fpu_ctx, + err == 0 ? FPU_KERN_KTHR : FPU_KERN_NOCTX); + } } void lkpi_kernel_fpu_end(void) { if (--(current->fpu_ctx_level) == 0) - fpu_kern_leave(curthread, NULL); + fpu_kern_leave(curthread, current->fpu_ctx); } #else