FAILED: patch "[PATCH] crypto: hisilicon/hpre - fix GFP flags for dma_alloc_coherent" failed to apply to 5.10-stable tree
Sasha Levin
maintainerThe patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x 40ec9605cb195e065470f8f23ef94b1b541411a3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From 40ec9605cb195e065470f8f23ef94b1b541411a3 Mon Sep 17 00:00:00 2001
From: Weili Qian <[email protected]>
Date: Fri, 18 Sep 2026 20:27:24 +0800
Subject: [PATCH] crypto: hisilicon/hpre - fix GFP flags for dma_alloc_coherent
The HPRE driver hard-codes GFP_ATOMIC for RSA/DH and GFP_KERNEL for ECDH. This may cause "scheduling while atomic" in atomic context or unnecessary allocation failures under memory pressure.
Select GFP flags dynamically based on CRYPTO_TFM_REQ_MAY_SLEEP and store them in hpre_asym_request::flags. The areq field is a union of rsa/dh/ecdh request pointers; reading base.flags through any union member other than the one that was written would be type-punning, so a dedicated flags field avoids that.
--- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/crypto/hisilicon/hpre/hpre_crypto.c +8 -2
@@ -138,6 +138,7 @@ struct hpre_asym_request {int err;hpre_cb cb;struct timespec64 req_time; +u32 flags; };static inline unsigned int hpre_align_sz (void )
@@ -186,6 +187,7 @@ static int hpre_prepare_dma_buf(struct hpre_asym_request *hpre_req, struct scatterlist *data, unsigned int len, int is_src, dma_addr_t *tmp) { + gfp_t gfp = hpre_req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : GFP_ATOMIC;struct hpre_ctx *ctx = hpre_req->ctx;struct device *dev = ctx->dev;void *ptr;
@@ -195,7 +197,7 @@ static int hpre_prepare_dma_buf(struct hpre_asym_request *hpre_req,if (unlikely (shift <0 ))return -EINVAL ; - ptr =dma_alloc_coherent (dev, ctx->key_sz, tmp,GFP_ATOMIC ); + ptr =dma_alloc_coherent (dev, ctx->key_sz, tmp, gfp);if (unlikely (!ptr))return -ENOMEM ;
@@ -419,6 +421,7 @@ static int hpre_msg_request_set(struct hpre_ctx *ctx, void *req, bool is_rsa) h_req =PTR_ALIGN (tmp,hpre_align_sz ()); h_req->cb = hpre_rsa_cb; h_req->areq.rsa = akreq; + h_req->flags = akreq->base.flags; msg = &h_req->req;memset (msg,0 ,sizeof (*msg)); }else {
@@ -433,6 +436,7 @@ static int hpre_msg_request_set(struct hpre_ctx *ctx, void *req, bool is_rsa) h_req =PTR_ALIGN (tmp,hpre_align_sz ()); h_req->cb = hpre_dh_cb; h_req->areq.dh = kreq; + h_req->flags = kreq->base.flags; msg = &h_req->req;memset (msg,0 ,sizeof (*msg)); msg->key =cpu_to_le64 (ctx->dh.dma_xa_p);
@@ -1457,6 +1461,7 @@ static int hpre_ecdh_msg_request_set(struct hpre_ctx *ctx, h_req =PTR_ALIGN (tmp,hpre_align_sz ()); h_req->cb = hpre_ecdh_cb; h_req->areq.ecdh = req; + h_req->flags = req->base.flags; msg = &h_req->req;memset (msg,0 ,sizeof (*msg)); msg->in =cpu_to_le64 (DMA_MAPPING_ERROR );
@@ -1475,6 +1480,7 @@ static int hpre_ecdh_msg_request_set(struct hpre_ctx *ctx,static int hpre_ecdh_src_data_init (struct hpre_asym_request *hpre_req,struct scatterlist *data,unsigned int len) { +gfp_t gfp = hpre_req->flags &CRYPTO_TFM_REQ_MAY_SLEEP ?GFP_KERNEL :GFP_ATOMIC ;struct hpre_sqe *msg = &hpre_req->req;struct hpre_ctx *ctx = hpre_req->ctx;struct device *dev = ctx->dev;
@@ -1488,7 +1494,7 @@ static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req,if (unlikely (shift <0 ))return -EINVAL ; - ptr =dma_alloc_coherent (dev, ctx->key_sz <<2 , &dma,GFP_KERNEL ); + ptr =dma_alloc_coherent (dev, ctx->key_sz <<2 , &dma, gfp);if (unlikely (!ptr))return -ENOMEM ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/4 crypto: hisilicon/hpre - add version adapt to new algorithms
From: Meng Yu <[email protected]>
[ Upstream commit 8123455a648581ed08b1740e4fc33863eb353687 ]
A new generation of accelerator Kunpeng930 has appeared, and the corresponding driver needs to be updated to support some new algorithms of Kunpeng930. To be compatible with Kunpeng920, we add parameter 'struct hisi_qm *qm' to sec_algs_(un)register to identify the chip's version.
Stable-dep-of: 40ec9605cb19 ("crypto: hisilicon/hpre - fix GFP flags for dma_alloc_coherent")
--- drivers/crypto/hisilicon/hpre/hpre.h | 5 +++-- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 4 ++-- drivers/crypto/hisilicon/qm.c | 4 ++-- drivers/crypto/hisilicon/qm.h | 4 ++-- drivers/crypto/hisilicon/sec2/sec.h | 4 ++-- drivers/crypto/hisilicon/sec2/sec_crypto.c | 4 ++-- drivers/crypto/hisilicon/sec2/sec_crypto.h | 4 ++-- drivers/crypto/hisilicon/zip/zip.h | 4 ++-- drivers/crypto/hisilicon/zip/zip_crypto.c | 4 ++-- 9 files changed, 19 insertions(+), 18 deletions(-)
drivers/crypto/hisilicon/hpre/hpre.h +3 -2
@@ -91,7 +91,8 @@ struct hpre_sqe { };struct hisi_qp *hpre_create_qp (void ); -int hpre_algs_register (void ); -void hpre_algs_unregister (void ); +int hpre_algs_register (struct hisi_qm *qm); +void hpre_algs_unregister (struct hisi_qm *qm); +#endif
drivers/crypto/hisilicon/hpre/hpre_crypto.c +2 -2
@@ -1154,7 +1154,7 @@ static struct kpp_alg dh = { };#endif -int hpre_algs_register (void ) +int hpre_algs_register (struct hisi_qm *qm) {int ret;
@@ -1171,7 +1171,7 @@ int hpre_algs_register(void)return ret; } -void hpre_algs_unregister (void ) +void hpre_algs_unregister (struct hisi_qm *qm) {crypto_unregister_akcipher (&rsa);#ifdef CONFIG_CRYPTO_DH
drivers/crypto/hisilicon/qm.c +2 -2
@@ -3960,7 +3960,7 @@ int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list)mutex_unlock (&qm_list->lock);if (flag) { - ret = qm_list->register_to_crypto (); + ret = qm_list->register_to_crypto (qm);if (ret) {mutex_lock (&qm_list->lock);list_del (&qm->list);
@@ -3988,7 +3988,7 @@ void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list)mutex_unlock (&qm_list->lock);if (list_empty (&qm_list->list)) - qm_list->unregister_from_crypto (); + qm_list->unregister_from_crypto (qm); }EXPORT_SYMBOL_GPL (hisi_qm_alg_unregister);
drivers/crypto/hisilicon/qm.h +2 -2
@@ -193,8 +193,8 @@ struct hisi_qm_err_ini {struct hisi_qm_list {struct mutex lock;struct list_head list; -int (*register_to_crypto)(void ); -void (*unregister_from_crypto)(void ); +int (*register_to_crypto)(struct hisi_qm *qm); +void (*unregister_from_crypto)(struct hisi_qm *qm); };struct hisi_qm {
drivers/crypto/hisilicon/sec2/sec.h +2 -2
@@ -184,6 +184,6 @@ struct sec_dev {void sec_destroy_qps (struct hisi_qp **qps,int qp_num);struct hisi_qp **sec_create_qps (void ); -int sec_register_to_crypto (void ); -void sec_unregister_from_crypto (void ); +int sec_register_to_crypto (struct hisi_qm *qm); +void sec_unregister_from_crypto (struct hisi_qm *qm);#endif
drivers/crypto/hisilicon/sec2/sec_crypto.c +2 -2
@@ -1632,7 +1632,7 @@ static struct aead_alg sec_aeads[] = { AES_BLOCK_SIZE, AES_BLOCK_SIZE, SHA512_DIGEST_SIZE), }; -int sec_register_to_crypto (void ) +int sec_register_to_crypto (struct hisi_qm *qm) {int ret;
@@ -1649,7 +1649,7 @@ int sec_register_to_crypto(void)return ret; } -void sec_unregister_from_crypto (void ) +void sec_unregister_from_crypto (struct hisi_qm *qm) {crypto_unregister_skciphers (sec_skciphers,ARRAY_SIZE (sec_skciphers));
drivers/crypto/hisilicon/sec2/sec_crypto.h +2 -2
@@ -210,6 +210,6 @@ struct sec_sqe { struct sec_sqe_type2 type2; }; -int sec_register_to_crypto (void ); -void sec_unregister_from_crypto (void ); +int sec_register_to_crypto (struct hisi_qm *qm); +void sec_unregister_from_crypto (struct hisi_qm *qm);#endif
drivers/crypto/hisilicon/zip/zip.h +2 -2
@@ -62,6 +62,6 @@ struct hisi_zip_sqe { };int zip_create_qps (struct hisi_qp **qps,int ctx_num,int node); -int hisi_zip_register_to_crypto (void ); -void hisi_zip_unregister_from_crypto (void ); +int hisi_zip_register_to_crypto (struct hisi_qm *qm); +void hisi_zip_unregister_from_crypto (struct hisi_qm *qm);#endif
drivers/crypto/hisilicon/zip/zip_crypto.c +2 -2
@@ -665,7 +665,7 @@ static struct acomp_alg hisi_zip_acomp_gzip = { } }; -int hisi_zip_register_to_crypto (void ) +int hisi_zip_register_to_crypto (struct hisi_qm *qm) {int ret;
@@ -684,7 +684,7 @@ int hisi_zip_register_to_crypto(void)return ret; } -void hisi_zip_unregister_from_crypto (void ) +void hisi_zip_unregister_from_crypto (struct hisi_qm *qm) {crypto_unregister_acomp (&hisi_zip_acomp_gzip);crypto_unregister_acomp (&hisi_zip_acomp_zlib);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 crypto: hisilicon/hpre - add algorithm type
From: Meng Yu <[email protected]>
[ Upstream commit 9b94ae729068f6608ec7364dea891ee86dd3dad2 ]
Algorithm type is brought in to get hardware HPRE queue to support different algorithms.
Stable-dep-of: 40ec9605cb19 ("crypto: hisilicon/hpre - fix GFP flags for dma_alloc_coherent")
--- drivers/crypto/hisilicon/hpre/hpre.h | 10 +++++++++- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 12 ++++++------ drivers/crypto/hisilicon/hpre/hpre_main.c | 11 +++++++++-- 3 files changed, 24 insertions(+), 9 deletions(-)
drivers/crypto/hisilicon/hpre/hpre.h +9 -1
@@ -10,6 +10,14 @@#define HPRE_PF_DEF_Q_NUM 64#define HPRE_PF_DEF_Q_BASE 0 +/* +* type used in qm sqc DW6. +* 0 - Algorithm which has been supported in V2, like RSA, DH and so on; +* 1 - ECC algorithm in V3. +*/ +#define HPRE_V2_ALG_TYPE 0 +#define HPRE_V3_ECC_ALG_TYPE 1 +enum {HPRE_CLUSTER0 ,HPRE_CLUSTER1 ,
@@ -90,7 +98,7 @@ struct hpre_sqe {__le32 rsvd1[_HPRE_SQE_ALIGN_EXT]; }; -struct hisi_qp *hpre_create_qp (void ); +struct hisi_qp *hpre_create_qp (u8 type);int hpre_algs_register (struct hisi_qm *qm);void hpre_algs_unregister (struct hisi_qm *qm);
drivers/crypto/hisilicon/hpre/hpre_crypto.c +6 -6
@@ -152,12 +152,12 @@ static void hpre_rm_req_from_ctx(struct hpre_asym_request *hpre_req) } } -static struct hisi_qp *hpre_get_qp_and_start (void ) +static struct hisi_qp *hpre_get_qp_and_start (u8 type) {struct hisi_qp *qp;int ret; - qp =hpre_create_qp (); + qp =hpre_create_qp (type);if (!qp) {pr_err ("Can not create hpre qp!\n" );return ERR_PTR (-ENODEV );
@@ -422,11 +422,11 @@ static void hpre_alg_cb(struct hisi_qp *qp, void *resp) req->cb (ctx, resp); } -static int hpre_ctx_init (struct hpre_ctx *ctx) +static int hpre_ctx_init (struct hpre_ctx *ctx,u8 type) {struct hisi_qp *qp; - qp =hpre_get_qp_and_start (); + qp =hpre_get_qp_and_start (type);if (IS_ERR (qp))return PTR_ERR (qp);
@@ -674,7 +674,7 @@ static int hpre_dh_init_tfm(struct crypto_kpp *tfm) {struct hpre_ctx *ctx =kpp_tfm_ctx (tfm); -return hpre_ctx_init (ctx); +return hpre_ctx_init (ctx,HPRE_V2_ALG_TYPE ); }static void hpre_dh_exit_tfm (struct crypto_kpp *tfm)
@@ -1100,7 +1100,7 @@ static int hpre_rsa_init_tfm(struct crypto_akcipher *tfm)return PTR_ERR (ctx->rsa.soft_tfm); } - ret =hpre_ctx_init (ctx); + ret =hpre_ctx_init (ctx,HPRE_V2_ALG_TYPE );if (ret)crypto_free_akcipher (ctx->rsa.soft_tfm);
drivers/crypto/hisilicon/hpre/hpre_main.c +9 -2
@@ -201,13 +201,20 @@ static u32 vfs_num;module_param_cb (vfs_num, &vfs_num_ops, &vfs_num,0444 );MODULE_PARM_DESC (vfs_num,"Number of VFs to enable(1-63), 0(default)" ); -struct hisi_qp *hpre_create_qp (void ) +struct hisi_qp *hpre_create_qp (u8 type) {int node =cpu_to_node (smp_processor_id ());struct hisi_qp *qp =NULL ;int ret; - ret =hisi_qm_alloc_qps_node (&hpre_devices,1 ,0 , node, &qp); +if (type !=HPRE_V2_ALG_TYPE && type !=HPRE_V3_ECC_ALG_TYPE ) +return NULL ; + +/* +* type: 0 - RSA/DH. algorithm supported in V2, +* 1 - ECC algorithm in V3. +*/ + ret =hisi_qm_alloc_qps_node (&hpre_devices,1 , type, node, &qp);if (!ret)return qp;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 crypto: ecc - expose ecc curves
In reply to Sasha LevinFrom: Meng Yu <[email protected]>
[ Upstream commit 14bb76768275a056a91d249525a717019fd46663 ]
Move 'ecc_get_curve' to 'include/crypto/ecc_curve.h', so everyone in kernel tree can easily get ecc curve params;
Stable-dep-of: 40ec9605cb19 ("crypto: hisilicon/hpre - fix GFP flags for dma_alloc_coherent")
--- crypto/ecc.c | 5 +++- crypto/ecc.h | 37 ++------------------------ include/crypto/ecc_curve.h | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 include/crypto/ecc_curve.h
crypto/ecc.c +4 -1
@@ -24,6 +24,7 @@* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <crypto/ecc_curve.h> #include <linux/module.h> #include <linux/random.h> #include <linux/slab.h>
@@ -42,7 +43,8 @@ typedef struct {u64 m_high; } uint128_t; -static inline const struct ecc_curve *ecc_get_curve (unsigned int curve_id) + +const struct ecc_curve *ecc_get_curve (unsigned int curve_id) {switch (curve_id) {/* In FIPS mode only allow P256 and higher */
@@ -54,6 +56,7 @@ static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)return NULL ; } } +EXPORT_SYMBOL (ecc_get_curve);static u64 *ecc_alloc_digits_space (unsigned int ndigits) {
crypto/ecc.h +2 -35
@@ -26,6 +26,8 @@#ifndef _CRYPTO_ECC_H#define _CRYPTO_ECC_H +#include <crypto/ecc_curve.h> +/* One digit is u64 qword. */ #define ECC_CURVE_NIST_P192_DIGITS 3#define ECC_CURVE_NIST_P256_DIGITS 4
@@ -33,43 +35,8 @@#define ECC_DIGITS_TO_BYTES_SHIFT 3 -/** -* struct ecc_point - elliptic curve point in affine coordinates -* -* @x: X coordinate in vli form. -* @y: Y coordinate in vli form. -* @ndigits: Length of vlis in u64 qwords. -*/ -struct ecc_point { -u64 *x; -u64 *y; -u8 ndigits; -}; -#define ECC_POINT_INIT (x, y, ndigits) (struct ecc_point) { x, y, ndigits } -/** -* struct ecc_curve - definition of elliptic curve -* -* @name: Short name of the curve. -* @g: Generator point of the curve. -* @p: Prime number, if Barrett's reduction is used for this curve -* pre-calculated value 'mu' is appended to the @p after ndigits. -* Use of Barrett's reduction is heuristically determined in -* vli_mmod_fast(). -* @n: Order of the curve group. -* @a: Curve parameter a. -* @b: Curve parameter b. -*/ -struct ecc_curve { -char *name; -struct ecc_point g; -u64 *p; -u64 *n; -u64 *a; -u64 *b; -}; -/** * ecc_is_key_valid() - Validate a given ECDH private key *
include/crypto/ecc_curve.h new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 crypto: hisilicon/hpre - fix GFP flags for dma_alloc_coherent
In reply to Sasha LevinFrom: Weili Qian <[email protected]>
[ Upstream commit 40ec9605cb195e065470f8f23ef94b1b541411a3 ]
The HPRE driver hard-codes GFP_ATOMIC for RSA/DH and GFP_KERNEL for ECDH. This may cause "scheduling while atomic" in atomic context or unnecessary allocation failures under memory pressure.
Select GFP flags dynamically based on CRYPTO_TFM_REQ_MAY_SLEEP and store them in hpre_asym_request::flags. The areq field is a union of rsa/dh/ecdh request pointers; reading base.flags through any union member other than the one that was written would be type-punning, so a dedicated flags field avoids that.
--- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/crypto/hisilicon/hpre/hpre_crypto.c +5 -1
@@ -96,6 +96,7 @@ struct hpre_asym_request {int req_id;hpre_cb cb;struct timespec64 req_time; +u32 flags; };static int hpre_alloc_req_id (struct hpre_ctx *ctx)
@@ -201,6 +202,7 @@ static int hpre_prepare_dma_buf(struct hpre_asym_request *hpre_req, struct scatterlist *data, unsigned int len, int is_src, dma_addr_t *tmp) { + gfp_t gfp = hpre_req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : GFP_ATOMIC;struct hpre_ctx *ctx = hpre_req->ctx;struct device *dev =HPRE_DEV (ctx);void *ptr;
@@ -210,7 +212,7 @@ static int hpre_prepare_dma_buf(struct hpre_asym_request *hpre_req,if (unlikely (shift <0 ))return -EINVAL ; - ptr =dma_alloc_coherent (dev, ctx->key_sz, tmp,GFP_ATOMIC ); + ptr =dma_alloc_coherent (dev, ctx->key_sz, tmp, gfp);if (unlikely (!ptr))return -ENOMEM ;
@@ -455,6 +457,7 @@ static int hpre_msg_request_set(struct hpre_ctx *ctx, void *req, bool is_rsa) h_req =PTR_ALIGN (tmp,HPRE_ALIGN_SZ ); h_req->cb = hpre_rsa_cb; h_req->areq.rsa = akreq; + h_req->flags = akreq->base.flags; msg = &h_req->req;memset (msg,0 ,sizeof (*msg)); }else {
@@ -469,6 +472,7 @@ static int hpre_msg_request_set(struct hpre_ctx *ctx, void *req, bool is_rsa) h_req =PTR_ALIGN (tmp,HPRE_ALIGN_SZ ); h_req->cb = hpre_dh_cb; h_req->areq.dh = kreq; + h_req->flags = kreq->base.flags; msg = &h_req->req;memset (msg,0 ,sizeof (*msg)); msg->key =cpu_to_le64 (ctx->dh.dma_xa_p);
Signature · 1 line
2.53.0