s390/ap: Support new APMLM field with PQAP/TAPQ instruction
Subsystems (2)
Harald Freudenberger
maintainerPATCH v2 1/1 s390/ap: Support new APMLM field with PQAP/TAPQ instruction
There is a new field APMLM (AP message limit multiplier) defined within the GR2 register on successful invocation of the TAPQ subfunction for the PQAP instruction.
So the new formula to calculate the AP max message limit is now:
if ml field <= 3
AP max message limit is 12KB
else
AP max message limit = apml * (apmlm + 1) * 4KB
Also adapt the HWINFO mask to have this new apmlm field shine through this bit filter mask.
--- arch/s390/include/asm/ap.h | 4 ++-- drivers/s390/crypto/ap_bus.h | 2 +- drivers/s390/crypto/ap_card.c | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-)
arch/s390/include/asm/ap.h +2 -2
@@ -123,8 +123,8 @@ struct ap_tapq_hwinfo { unsigned int : 14; unsigned int at : 8; /* ap type */ unsigned int nd : 8; /* nr of domains */ - unsigned int : 4; - unsigned int ml : 4; /* apxl ml */ + unsigned int mlm : 4; /* AP msg limit multiplier */ + unsigned int ml : 4; /* AP msg limit */ unsigned int : 3; unsigned int qd : 5; /* queue depth */ };
drivers/s390/crypto/ap_bus.h +1 -1
@@ -181,7 +181,7 @@ struct ap_card {bool chkstop;/* checkstop state */ }; -#define TAPQ_CARD_HWINFO_MASK 0xFFFF0000FFFF0F1FUL +#define TAPQ_CARD_HWINFO_MASK 0xFFFF0000FFFFFF1FUL#define ASSOC_IDX_INVALID 0x10000#define to_ap_card (x) container_of((x), struct ap_card, ap_dev.device)
drivers/s390/crypto/ap_card.c +2 -1
@@ -242,7 +242,8 @@ struct ap_card *ap_card_create(int id, struct ap_tapq_hwinfo hwinfo, ac->hwinfo = hwinfo; ac->id = id; ac->maxmsgsize = hwinfo.ml >3 ? - hwinfo.ml *AP_TAPQ_ML_FIELD_CHUNK_SIZE :AP_DEFAULT_MAX_MSG_SIZE ; + hwinfo.ml * (hwinfo.mlm +1 ) *AP_TAPQ_ML_FIELD_CHUNK_SIZE : +AP_DEFAULT_MAX_MSG_SIZE ;return ac; }
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] s390/ap: order-8 kmalloc allocation causes DoS for crypto requests --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] s390/ap: order-8 kmalloc allocation causes DoS for crypto requests --
commit c12ee44fe3064032047e06539777b0c95c7b1201
Author: Harald Freudenberger <[email protected]>
s390/ap: Support new APMLM field with PQAP/TAPQ instruction
This commit adds support for the new AP message limit multiplier field in the GR2 register for the PQAP instruction. It updates the HWINFO mask and modifies the formula used to calculate the AP maximum message limit.
drivers/s390/crypto/ap_card.c struct ap_card *ap_card_create(int id, struct ap_tapq_hwinfo hwinfo, · line 246
Quoted 10 lines
drivers/s390/crypto/ap_card.c +2
-1@@ -242,7 +242,8 @@ struct ap_card *ap_card_create(int id, struct ap_tapq_hwinfo hwinfo, ac->hwinfo = hwinfo; ac->id = id; ac->maxmsgsize = hwinfo.ml >3 ? - hwinfo.ml *AP_TAPQ_ML_FIELD_CHUNK_SIZE :AP_DEFAULT_MAX_MSG_SIZE ; + hwinfo.ml * (hwinfo.mlm +1 ) *AP_TAPQ_ML_FIELD_CHUNK_SIZE : +AP_DEFAULT_MAX_MSG_SIZE ;
[Severity: High]
Will this scaling of the maximum message size cause order-8 physically contiguous memory allocation failures?
By introducing the hwinfo.mlm multiplier, the maximum message size can now scale up to around 960KB. This value is used to set the global maximum message size.
However, looking at how messages are allocated indrivers/s390/crypto/ap_bus.c in ap_init_apmsg():
void ap_init_apmsg(...)
{
...
maxmsgsize = atomic_read(&ap_max_msg_size);
ap_msg->msg = kmalloc(maxmsgsize, GFP_KERNEL);
...
}And similarly in drivers/s390/crypto/zcrypt_queue.c in zcrypt_queue_alloc():
zq->reply.msg = kmalloc(reply_buf_size, GFP_KERNEL);
Since these functions unconditionally use kmalloc() for the maximum size, user-space crypto requests will trigger massive order-8 allocations. On a system with fragmented memory, these physically contiguous allocations are highly likely to fail. This could lead to a reliable denial of service for hardware cryptographic services since the underlying AP instruction operates on absolute memory addresses requiring physically contiguous memory.
Is there a plan to adjust the allocation strategy alongside this limit increase, or otherwise cap the unconditional allocation sizes?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Harald Freudenberger
maintainerQuoted 57 lines · On 2026-10-06 17:09, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] s390/ap: order-8 kmalloc allocation causes DoS for crypto requests
--commit
c12ee44fe3064032047e06539777b0c95c7b1201
Author: Harald Freudenberger <[email protected]>s390/ap: Support new APMLM field with PQAP/TAPQ instruction
This commit adds support for the new AP message limit multiplier field in the GR2 register for the PQAP instruction. It updates the HWINFO mask and modifies the formula used to calculate the AP maximum message limit.
drivers/s390/crypto/ap_card.c struct ap_card *ap_card_create(int id, struct · line 247
Quoted 11 lines
drivers/s390/crypto/ap_card.c +2
-1@@ -242,7 +242,8 @@ struct ap_card *ap_card_create(int id, structap_tapq_hwinfo hwinfo, ac->hwinfo = hwinfo; ac->id = id; ac->maxmsgsize = hwinfo.ml >3 ? - hwinfo.ml *AP_TAPQ_ML_FIELD_CHUNK_SIZE :AP_DEFAULT_MAX_MSG_SIZE ; + hwinfo.ml * (hwinfo.mlm +1 ) *AP_TAPQ_ML_FIELD_CHUNK_SIZE : +AP_DEFAULT_MAX_MSG_SIZE ;[Severity: High]
Will this scaling of the maximum message size cause order-8 physically contiguous memory allocation failures?By introducing the hwinfo.mlm multiplier, the maximum message size can now scale up to around 960KB. This value is used to set the global maximum message size.
However, looking at how messages are allocated in
drivers/s390/crypto/ap_bus.cinap_init_apmsg():void
ap_init_apmsg(...){ ... maxmsgsize = atomic_read(&ap_max_msg_size); ap_msg->msg = kmalloc(maxmsgsize, GFP_KERNEL); ... }And similarly in
drivers/s390/crypto/zcrypt_queue.cinzcrypt_queue_alloc():zq->reply.msg = kmalloc(reply_buf_size, GFP_KERNEL);Since these functions unconditionally use
kmalloc()for the maximum size, user-space crypto requests will trigger massive order-8 allocations. On a system with fragmented memory, these physically contiguous allocations are highly likely to fail. This could lead to a reliable denial of service for hardware cryptographic services since the underlying AP instruction operates on absolute memory addresses requiring physically contiguous memory.
Quoted 2 lines
Is there a plan to adjust the allocation strategy alongside this limit increase, or otherwise cap the unconditional allocation sizes?
Hm this leads to the question if there should be an upper limit for this AP max message size. However this is then a protection against malicious firmware as the both fields ml and mlm are provided via TAPQ from firmware.
@Holger, @Finn any suggestions ?
Quoted 10 lines · Harald Freudenberger
There is a new field APMLM (AP message limit multiplier) defined within the GR2 register on successful invocation of the TAPQ subfunction for the PQAP instruction.
So the new formula to calculate the AP max message limit is now:
if ml field <= 3
AP max message limit is 12KB
else
AP max message limit = apml * (apmlm + 1) * 4KB
As per architecture it is more like:
if apml < 3 && apmlm == 0
mapml = 12k else if apmlm == 0 mapml = apml * 4k else if apml > 0 mapml = apml * (apmlm + 1) * 4k else // undefined
But yours is effectively the same.
Quoted 44 lines
Also adapt the HWINFO mask to have this new apmlm field shine through this bit filter mask.
Signed-off-by Harald Freudenberger <[email protected]>--- arch/s390/include/asm/ap.h | 4 ++-- drivers/s390/crypto/ap_bus.h | 2 +- drivers/s390/crypto/ap_card.c | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-)arch/s390/include/asm/ap.h +2
-2@@ -123,8 +123,8 @@ struct ap_tapq_hwinfo { unsigned int : 14; unsigned int at : 8; /* ap type */ unsigned int nd : 8; /* nr of domains */ - unsigned int : 4; - unsigned int ml : 4; /* apxl ml */ + unsigned int mlm : 4; /* AP msg limit multiplier */ + unsigned int ml : 4; /* AP msg limit */ unsigned int : 3; unsigned int qd : 5; /* queue depth */ };drivers/s390/crypto/ap_bus.h +1
-1@@ -181,7 +181,7 @@ struct ap_card {bool chkstop;/* checkstop state */ }; -#define TAPQ_CARD_HWINFO_MASK 0xFFFF0000FFFF0F1FUL +#define TAPQ_CARD_HWINFO_MASK 0xFFFF0000FFFFFF1FUL#define ASSOC_IDX_INVALID 0x10000#define to_ap_card (x) container_of((x), struct ap_card, ap_dev.device)drivers/s390/crypto/ap_card.c +0
-0
drivers/s390/crypto/ap_card.c struct ap_card *ap_card_create(int id, struct ap_tapq_hwinfo hwinfo, · line 249
Quoted 9 lines
drivers/s390/crypto/ap_card.c +2
-1@@ -242,7 +242,8 @@ struct ap_card *ap_card_create(int id, struct ap_tapq_hwinfo hwinfo, ac->hwinfo = hwinfo; ac->id = id; ac->maxmsgsize = hwinfo.ml >3 ? - hwinfo.ml *AP_TAPQ_ML_FIELD_CHUNK_SIZE :AP_DEFAULT_MAX_MSG_SIZE ; + hwinfo.ml * (hwinfo.mlm +1 ) *AP_TAPQ_ML_FIELD_CHUNK_SIZE : +AP_DEFAULT_MAX_MSG_SIZE ;return ac; }
Have you tested it with your qemu support or my mocking?
Quoted 59 lines · Harald Freudenberger
Quoted 58 lines · On 2026-10-06 17:09, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] s390/ap: order-8 kmalloc allocation causes DoS for crypto requestsSignature · 8 lines
commit c12ee44fe3064032047e06539777b0c95c7b1201 Author: Harald Freudenberger <[email protected]> s390/ap: Support new APMLM field with PQAP/TAPQ instruction This commit adds support for the new AP message limit multiplier field in the GR2 register for the PQAP instruction. It updates the HWINFO mask and modifies the formula used to calculate the AP maximum message limit.Quoted 7 lines
drivers/s390/crypto/ap_card.c +0
-0@@ -242,7 +242,8 @@ struct ap_card *ap_card_create(int id, structap_tapq_hwinfo hwinfo, ac->hwinfo = hwinfo; ac->id = id;Quoted 5 lines
ac->maxmsgsize= hwinfo.ml > 3 ?
- hwinfo.ml *AP_TAPQ_ML_FIELD_CHUNK_SIZE:AP_DEFAULT_MAX_MSG_SIZE;
+ hwinfo.ml * (hwinfo.mlm + 1) *AP_TAPQ_ML_FIELD_CHUNK_SIZE:
+AP_DEFAULT_MAX_MSG_SIZE;[Severity: High]
Will this scaling of the maximum message size cause order-8 physically contiguous memory allocation failures?By introducing the hwinfo.mlm multiplier, the maximum message size can now scale up to around 960KB. This value is used to set the global maximum message size.
However, looking at how messages are allocated in
drivers/s390/crypto/ap_bus.cinap_init_apmsg():void
ap_init_apmsg(...)
{
...
maxmsgsize =atomic_read(&ap_max_msg_size);ap_msg->msg=kmalloc(maxmsgsize, GFP_KERNEL);
...
}And similarly in
drivers/s390/crypto/zcrypt_queue.cinzcrypt_queue_alloc():
zq->reply.msg=kmalloc(reply_buf_size, GFP_KERNEL);Since these functions unconditionally use
kmalloc()for the maximum size, user-space crypto requests will trigger massive order-8 allocations. On a system with fragmented memory, these physically contiguous allocations are highly likely to fail. This could lead to a reliable denial of service for hardware cryptographic services since the underlying AP instruction operates on absolute memory addresses requiring physically contiguous memory.
Quoted 8 lines
Quoted 2 lines
Is there a plan to adjust the allocation strategy alongside this limit increase, or otherwise cap the unconditional allocation sizes?
Hm this leads to the question if there should be an upper limit for this AP max message size. However this is then a protection against malicious firmware as the both fields ml and mlm are provided via TAPQ from firmware.
@Holger, @Finn any suggestions ?
I think when firmware is compromised this is not our biggest problem...
We could add a maximum valid apml which is whatever the firmware reports for the most current card generation. (Currently 24k)
Harald Freudenberger
maintainerQuoted 12 lines · Finn Callies
Quoted 10 lines · Harald Freudenberger
There is a new field APMLM (AP message limit multiplier) defined within the GR2 register on successful invocation of the TAPQ subfunction for the PQAP instruction.
So the new formula to calculate the AP max message limit is now:
if ml field <= 3
AP max message limit is 12KB
else
AP max message limit = apml * (apmlm + 1) * 4KB
Quoted 6 lines
As per architecture it is more like:
if apml < 3 && apmlm == 0
mapml = 12kelse if apmlm == 0
mapml = apml * 4k
this
Quoted 2 lines
else if apml > 0
mapml = apml * (apmlm + 1) * 4k
and this come to the same result.
And for ampl == 3 the formula results in 12KB,
Quoted 2 lines
else
// undefined
and this else is unreachable.
Quoted 2 lines
But yours is effectively the same.
Yep
Quoted 56 lines
Quoted 44 lines
Also adapt the HWINFO mask to have this new apmlm field shine through this bit filter mask.
Signed-off-by Harald Freudenberger <[email protected]>--- arch/s390/include/asm/ap.h | 4 ++-- drivers/s390/crypto/ap_bus.h | 2 +- drivers/s390/crypto/ap_card.c | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-)arch/s390/include/asm/ap.h +2
-2@@ -123,8 +123,8 @@ struct ap_tapq_hwinfo { unsigned int : 14; unsigned int at : 8; /* ap type */ unsigned int nd : 8; /* nr of domains */ - unsigned int : 4; - unsigned int ml : 4; /* apxl ml */ + unsigned int mlm : 4; /* AP msg limit multiplier */ + unsigned int ml : 4; /* AP msg limit */ unsigned int : 3; unsigned int qd : 5; /* queue depth */ };drivers/s390/crypto/ap_bus.h +1
-0@@ -181,7 +181,7 @@ struct ap_card {bool chkstop;/* checkstop state */ }; -#define TAPQ_CARD_HWINFO_MASK 0xFFFF0000FFFF0F1FUL +#define TAPQ_CARD_HWINFO_MASK 0xFFFF0000FFFFFF1FUL#define ASSOC_IDX_INVALID 0x10000#define to_ap_card (x) container_of((x), struct ap_card, ap_dev.device)drivers/s390/crypto/ap_card.c +0
-0drivers/s390/crypto/ap_card.c struct ap_card *ap_card_create(int id, struct · line 249
Quoted 9 lines
drivers/s390/crypto/ap_card.c +2
-1@@ -242,7 +242,8 @@ struct ap_card *ap_card_create(int id, structap_tapq_hwinfo hwinfo, ac->hwinfo = hwinfo; ac->id = id; ac->maxmsgsize = hwinfo.ml >3 ? - hwinfo.ml *AP_TAPQ_ML_FIELD_CHUNK_SIZE :AP_DEFAULT_MAX_MSG_SIZE ; + hwinfo.ml * (hwinfo.mlm +1 ) *AP_TAPQ_ML_FIELD_CHUNK_SIZE : +AP_DEFAULT_MAX_MSG_SIZE ;return ac; }Reviewed-by Finn Callies <[email protected]>
Quoted 1 line
Have you tested it with your qemu support or my mocking?
Still untested. I am about to test this in my qemu AP emulation soon.