All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ilya Leoshkevich <iii@linux.ibm.com>
To: Andrew Holden <andy0x48@linux.ibm.com>,
	qemu-devel@nongnu.org, freude@linux.ibm.com
Cc: qemu-s390x@nongnu.org, richard.henderson@linaro.org,
	david@kernel.org, cohuck@redhat.com, farman@linux.ibm.com,
	mjrosato@linux.ibm.com
Subject: Re: [PATCH v2 2/3] target/s390x/tcg: update regs and IV/tweak per block in CPACF AES/PAES
Date: Wed, 25 Nov 2026 17:32:08 +0100	[thread overview]
Message-ID: <23756053-86fb-4732-bef1-397be6233c38@linux.ibm.com> (raw)
In-Reply-To: <20261120131443.61606-3-andy0x48@linux.ibm.com>



On 11/20/26 14:14, Andrew Holden wrote:
> The CPACF AES and PAES helpers (ECB, CBC, CTR, and XTS) updated the guest
> registers, and the CBC IV and XTS tweak, only after the whole block
> loop. A guest fault unwinds out of the helper, so the registers kept
> their start-of-instruction values, with the instruction having
> restarted at the first block instead of resuming after the last
> completed block, as expected regarding PoP. A block crossing a page
> boundary could also be partly written before the fault.
> 
> Probe the source, destination, and IV/tweak of each block with
> access_prepare() before any data is written, then update the registers
> last, a fault leaves the block untouched and a re-invocation would
> redo this. aes_read_block() and aes_write_block() do all the copying
> for each mode. Addresses are wrapped per block.
> 
> The IV/tweak write is probed first but it is not atomic with the data
> write. It's unlikely a fault happens between the data store and
> IV/tweak, but it could happen due to a specific thread unmapping, page
> change, etc., and only matters when source and destination overlap.
> 
> Signed-off-by: Andrew Holden <andy0x48@linux.ibm.com>
> ---
>   target/s390x/tcg/cpacf_aes.c | 380 ++++++++++++++++++++++++-----------
>   1 file changed, 259 insertions(+), 121 deletions(-)

[...]

>   /*
> - * write exactly one AES block from local buffer to guest memory
> + * read exactly one AES block from a prepared access into a local buffer.
>    */
> -static void aes_write_block(CPUS390XState *env, const int mmu_idx,
> -                            const uintptr_t ra, uint64_t guest_addr,
> -                            uint8_t *buf)
> +static inline void aes_read_block(CPUS390XState *env, const uintptr_t ra,
> +                                  S390Access *acc, uint8_t *buf)
>   {
> -    write_guest_wrap_u8(env, mmu_idx, ra, guest_addr, buf, AES_BLOCK_SIZE);
> +    /* gather block */
> +    for (int i = 0; i < AES_BLOCK_SIZE; i++) {
> +        buf[i] = access_get_byte(env, acc, i, ra);
> +    }
> +}

Can we introduce something like access_memmove(), but for guest-to-host
and host-to-guest transfers in order to avoid byte-by-byte accesses?

> +
> +/*
> + * write exactly one AES block from a local buffer to a prepared access.
> + */
> +static inline void aes_write_block(CPUS390XState *env, const uintptr_t ra,
> +                                   S390Access *acc, uint8_t *buf)
> +{
> +    /* scatter block */
> +    for (int i = 0; i < AES_BLOCK_SIZE; i++) {
> +        access_set_byte(env, acc, i, buf[i], ra);
> +    }
>   }
>   
>   /*
> @@ -62,8 +80,9 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
>   {
>       enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
>       uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
> -    uint64_t len = *src_len_reg, done = 0;
>       int i, keysize, addr_reg_size;
> +    uint64_t len = *src_len_reg;
> +    S390Access srca, dsta;
>       uint8_t key[32];
>       AES_KEY exkey;
>   
> @@ -116,23 +135,37 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
>   
>       /* process up to MAX_BLOCKS_PER_RUN aes blocks */
>       for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
> -        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
> +        /* probe source and destination access */
> +        aes_prepare_block(env, mmu_idx, ra, &srca, *src_ptr_reg,
> +                          MMU_DATA_LOAD);
> +        aes_prepare_block(env, mmu_idx, ra, &dsta, *dst_ptr_reg,
> +                          MMU_DATA_STORE);
> +
> +        /*
> +         * linux-user: the accessors below use host pointers. Record ra so a
> +         * fault from a racing unmap of the probed page is unwound correctly.
> +         */

I'm not sure we need this comment everywhere; the existing usages in
target/s390x/tcg/mem_helper.c don't have it, and it also repeats what
the function's documentation already says.


The rest looks reasonable, thanks!


  reply	other threads:[~2026-11-25 16:33 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 13:14 [PATCH v2 0/3] Fix AES/PAES CPACF register update on faults Andrew Holden
2026-11-20 13:14 ` [PATCH v2 1/3] target/s390x: export access_prepare() and byte accessors Andrew Holden
2026-11-20 13:14 ` [PATCH v2 2/3] target/s390x/tcg: update regs and IV/tweak per block in CPACF AES/PAES Andrew Holden
2026-11-25 16:32   ` Ilya Leoshkevich [this message]
2026-11-26 16:03     ` Andrew Holden
2026-11-20 13:14 ` [PATCH v2 3/3] tests/s390x/tcg: Add cpacf clear key register update tests Andrew Holden
2026-11-25 16:41   ` Ilya Leoshkevich
2026-12-01  8:34     ` Harald Freudenberger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=23756053-86fb-4732-bef1-397be6233c38@linux.ibm.com \
    --to=iii@linux.ibm.com \
    --cc=andy0x48@linux.ibm.com \
    --cc=cohuck@redhat.com \
    --cc=david@kernel.org \
    --cc=farman@linux.ibm.com \
    --cc=freude@linux.ibm.com \
    --cc=mjrosato@linux.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.