KSPU API + AES offloaded to SPU + testing module
Subsystems (5)
cover KSPU API + AES offloaded to SPU + testing module
This is a complete submission of $subject against current git. Content:
1 - required casting to retrieve private struct out of crypto API 2 - function to retrieve private crypto struct. Herbert queued this
already for 2.6.24.
3 - KSPU doc
4 - KSPU skeleton
5 - exporting required symbols within spufs.ko
6 - allocation of KSPU context
7 - KSPU, PPU side implementation
8 - KSPU, SPE side implementation
9 - AES as KSPU & Crypto user. Providing ECB+CBC block mode
10 - testing module for AES crypto.
Herbert, I've put you on CC to consider patch 1 for inclusion. As I noticed earlier, KSPU is a general purpose interface so it is not clever to use crypto's infrastructure since KSPU may be used for non-crypto related tasks. However, I included a soft limit.
Figure [1] shows performance of aes-spu in ECB mode with a transfer size of 16 KiBb. Generic is crypto/aes.c compiled for SPU, sync means it is there is no queue process just copy data and start the SPU, async is what is provided by the patch series.
Figure [2] shows the performance with different DMA transfer sizes (smaller chunks = more transfers). Async benefits from double buffering and more requests at a time but slower at 16 KiB due to more communication.
The pdfs are 4.6 KiB each but scalable :)
[1] http://download.breakpoint.cc/spu/spu_code_async.pdf
[2] http://download.breakpoint.cc/spu/spu_async_blocksize_aligned.pdf
sleepy Sebastian
PATCH 1/10 t add cast to regain ablkcipher_request from private ctx
This cast allows to regain the struct ablkcipher_request for a request from private data.
include/linux/crypto.h +6 -0
@@ -580,6 +580,12 @@ static inline struct ablkcipher_request return container_of(req, struct ablkcipher_request, base); } +static inline struct ablkcipher_request *ablkcipher_ctx_cast ( +void *ctx) +{ + return container_of(ctx, struct ablkcipher_request, __ctx); +} +static inline struct ablkcipher_request *ablkcipher_request_alloc (struct crypto_ablkcipher *tfm,gfp_t gfp) {
Herbert Xu
maintainerRe: [patch 01/10] t add cast to regain ablkcipher_request from private ctx
Quoted 2 lines · Sebastian Siewior
This cast allows to regain the struct
ablkcipher_requestfor a request from private data.
Hi Sebastian:
I think this function would make more sense as a private function in your driver. That way you can give it an explicit type rather than having it take a void *.
We want to avoid unnecessary casting like this where possible as it's error-prone.
Cheers,
Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txtPATCH 2/10 crypto: retrieve private ctx aligned
In reply to Sebastian SiewiorThis is function does the same thing for ablkcipher that is done for blkcipher by crypto_blkcipher_ctx_aligned(): it returns an aligned address of the private ctx.
include/crypto/algapi.h +5 -0
@@ -160,6 +160,11 @@ static inline void *crypto_ablkcipher_ctreturn crypto_tfm_ctx (&tfm->base); } +static inline void *crypto_ablkcipher_ctx_aligned (struct crypto_ablkcipher *tfm) +{ +return crypto_tfm_ctx_aligned (&tfm->base); +} +static inline struct crypto_blkcipher *crypto_spawn_blkcipher (struct crypto_spawn *spawn) {
PATCH 3/10 spufs: kspu documentation
In reply to Sebastian SiewiorDocumentation how to use kspu from the PPU & SPU side
Documentation/powerpc/kspu.txt new file +243 -0
@@ -0,0 +1,243 @@ + KSPU: Utilization of SPUs for kernel tasks +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +0. KSPU design +============== + +The idea is to offload single time consuming tasks to the SPU. Those tasks are +fed with data that they have to process. +Once the function on the SPU side is invoked, the input data is already +available. After the job is done, the offloaded function must kick off a DMA +transfer, that transfers the result back to the main memory. +On the PPU, the KSPU user queues the job temporary in a linked list and receives +later a callback to put queue the job directly in SPU's ring buffer. The transit +stop is required for two reasons: +- It must be possible to queue work items from softirq context +- All requests must be accepted, even if the ring buffer is full. Waiting (until + a slot becomes available) is not an option. + +The callback (for the enqueue process on the SPU) happens in a kthread context, +so a mutex may be hold. However, there is only one kthread for this job, so +every delay will have global impact. +The user should enqueue only one job item on every enqueue request. The user +may enqueue more than one job item if _really_ necessary. If there are not +enough free slots than the enqueue function will be called as the first enqueue +function once free slots are available again. +After the offloaded function completed the job, the kthread calls the +completion callback (it is the same kthread that is used for enqueue). +Double/multi buffering is performed by KSPU. + +0.5 SPU usage +============= +Currently only one SPU is used and allocated. Allocation occurs during KSPU +module initialization via the spufs interface. Therefore the physical SPU is +considered in the scheduling process (and "shared" with user space). Right +now, it is not "easy" to find out +- how many SPUs may be taken (i.e. not used by user space) +- how many SPUs are useful to be taken (depending of the workload) +The later is (theoretically) an easy accounting approach if there are no +dependencies in processing (and two jobs of the same kind may be processed on +two SPUs in parallel). +A second SPU (context) may be required if the local store memory is used up. +This can be prevented if "overlays" are used. The advantage over several SPU +context: +- less complexity (since there is only one kind of SPU code) +- no tracking which function is in which context. Plus overlay code switches + the binary probably faster than the scheduler does. + +1. Overview of memory layout +============================= + + ------------------ 256 KiB + | RB ENTRY | + ------------------ Ring buffer entries + | ......... | + ------------------ + | RB ENTRY | + ------------------ + | RB state | (consumed + outstanding) + ------------------ + | STACK | Stack growing downwards + | || | + | \/ | + ------------------ + | ....... | unused / reserved :) + ------------------ + | Data | + | DMA Buffers, | + | functions' | + | private data | + ------------------ + | Code | + | offloaded SPU | + | functions | + ------------------ + | multiplexor | spu_main.c + ------------------ 0 + +Type of Ring buffer entry is struct kspu_job. +Number of ring buffer entries is determined by RB_SLOTS. +Number of of DMA buffers is determined by DMA_BUFFERS. +The stack grows uncontrolled. There is no (cheap) way to notice a stack +overflow. After adding a new SPU program, the developer is encourage to check +the stack usage and make sure the stack will never hit the data segment. This +task is not required if recursive functions are used (I hope the suicide part +has been understood). + +1.1 Ring buffer +=============== +The ring buffer has been chosen because the data structure allows exchange of +data (PPU <-> SPU) without any locking. The ring buffer entry consists of two +parts +- Data known by the KSPU (public data). +- Private data is only known by the user (hidden from KSPU) +Public data contains the function parameters of the offloaded SPU program. +Private data is meaningless to the KSPU and may consider algorithm specific +information (like where to put the result). +The number of ring buffer entries (RB_SLOTS) has two constrains (except +LS_SIZE :D): +- it must be power of 2. +- it must be at least DMA_BUFFERS*2 + +1.2 DMA Buffers +=============== +Every DMA buffer is DMA_MAX_TRANS_SIZE bytes in size. The size reflects the +maximum transfer size that may be request by the SPU. Therefore the same +requirements apply here like to the MFC DMA size: it must be a multiple of 16 +and may not by larger than 16KiB. +The only limit for the number of available DMA buffers (DMA_BUFFERS) (besides +the available space) is that "DMA_BUFFERS*2 <= RB_SLOTS" must be true. The +reason for this constraint is that the "multiplexor", once started, requests +DMA_BUFFERS buffers and starts processing. While processing the first batch, +the next DMA_BUFFERS are requested (to get into streaming mode). After +processing DMA_BUFFERS*2 requests, the first point is reached, where the SPU +starts to notify the PPU about done requests and may stop. Therefore the +shortest run is DMA_BUFFERS*2 requests. If there are not enough requests +available, KSPU fills the ring buffer with NOPs to fit. A NOP is a DMA +transfer with the size zero (nop for the MFC) and just a return statement as +the job function. + +2. Offloading a task to SPU +=========================== +Three steps are required to offload a task to SPU: +- PPU code +- SPU code +- Update header files & Makefile + +The example code shows an example how to offload an 'add operation' via spu_add +on the SPU. The complete implementation is in skeleton files. + +2.1 PPU code +============ +1. Init +- Prepare a struct with 'struct kspu_work_item' embedded in it. + struct my_spu_req { + struct kspu_work_item kspu_work; + void *data; + }; + +- get global kspu ctx. + struct kspu_context *kctx = kspu_get_kctx(); + +2. Enqueue a specific request. (struct my_spu_req spe_req) +- Setup enqueue callback. + spe_req.kspu_work.enqueue = my_enqueue_func; + +- Enqueue it in kspu. + kspu_enqueue_work_item(kctx, &spe_req.kspu_work); + +3. Wait for the callback, enqueue it than on the SPU +- Get an empty slot + struct kspu_job *work_item = kspu_get_rb_slot(kctx); + +- fill it + work_item->operation = MY_ADD; + work_item->in = spe_req.data; + work_item->in_size = 16; + +- mark it as ready + kspu_mark_rb_slot_ready(kctx, &spe_req.kspu_work); + +- set the finish callback + spe_req.kspu_work.notify = my_notify_func; + +4. Wait for the "finish" callback. +- job finished. + +2.2 SPU code +============ +- prepare a function that matches the following params: + void spu_my_add(struct kspu_job *kjob, void *buffer, unsigned int buf_num) + + Use init_put_data() to write data back to main memory. It is just a wrapper + around mfc_putf(). Use the supplied buf_num as the tag. + init_put_data(buffer, out, length, buf_num); + +2.3 Update files +================ +- define your private data structures which are visible from your PPU program + and from your SPU program. They become later part of struct kspu_job if you + need them for parameters. Keep them as small as possible. + +- attach the function to SPU_OPS in + include/asm-powerpc/kspu/merged_code.h before TOTAL_SPU_OPS + +- attach the function to spu_funcs[] in arch/powerpc/platforms/cell/spufs/spu_main.c + +2.4 Skeleton files +================= +PPU code in Documentation/powerpc/kspu_ppu_skeleton.c +SPU code in Documentation/powerpc/kspu_spu_skeleton.[ch] + +Merge both into kspu: +--- a/arch/powerpc/platforms/cell/spufs/Makefile ++++ b/arch/powerpc/platforms/cell/spufs/Makefile +@@ -24,6 +24,7 @@ kspu-y += kspu_helper.o kspu_code.o + $(obj)/kspu_code.o: $(obj)/spu_kspu_dump.h + + spu_kspu_code_obj-y += $(obj)/spu_main.o $(obj)/spu_runtime.o ++spu_kspu_code_obj-y += $(obj)/spu_kspu_ppu_skeleton.o + spu_kspu_code_obj-y += $(spu_kspu_code_obj-m) + + $(obj)/spu_kspu: $(spu_kspu_code_obj-y) + +--- a/arch/powerpc/platforms/cell/spufs/spu_main.c ++++ b/arch/powerpc/platforms/cell/spufs/spu_main.c +@@ -13,6 +13,7 @@ + + static spu_operation_t spu_ops[TOTAL_SPU_FUNCS] __attribute__((aligned(16))) = { + [SPU_OP_nop] = spu_nop, ++ [SPU_OP_my_add] = spu_my_add + }; + + static unsigned char kspu_buff[DMA_BUFFERS][DMA_MAX_TRANS_SIZE]; + +--- a/arch/powerpc/platforms/cell/spufs/spu_runtime.h ++++ b/arch/powerpc/platforms/cell/spufs/spu_runtime.h +@@ -25,5 +25,6 @@ void memcpy_aligned(void *dest, const vo + /* exported offloaded functions */ + void spu_nop(struct kspu_job *kjob, void *buffer, + unsigned int buf_num); + ++void spu_my_add(struct kspu_job *kjob, void *buffer, ++ unsigned int buf_num); + + #endif + +--- a/include/asm-powerpc/kspu/merged_code.h ++++ b/include/asm-powerpc/kspu/merged_code.h +@@ -14,6 +14,7 @@ + + enum SPU_OPERATIONS { + SPU_OP_nop, ++ SPU_OP_my_add, + + TOTAL_OP_FUNCS, + }; +@@ -23,6 +24,7 @@ struct kspu_job { + unsigned long long in __attribute__((aligned(16))); + unsigned int in_size __attribute__((aligned(16))); + union { ++ struct my_sum my_sum; + } __attribute__((aligned(16))); + }; +
PATCH 4/10 spufs: kspu doc skeleton
In reply to Sebastian SiewiorSkeleton for PPU & SPU code that the documentation refers to.
Documentation/powerpc/kspu_ppu_skeleton.c new file +98 -0
@@ -0,0 +1,98 @@ +/* +* KSPU skeleton - PPU part +* +*/ + +#include <asm/kspu/kspu.h> +#include <asm/kspu/merged_code.h> + +struct spu_async_req { +struct kspu_context *kctx; +struct kspu_work_item kspu_work; +struct completion mcompletion; +void *n1p; +unsigned char n2; +}; + +static void my_add_finish_callback (struct kspu_work_item *kspu_work) +{ +struct spu_async_req *req =container_of (kspu_work, + struct spu_async_req, kspu_work); + + +complete (&req->mcompletion); +return ; +} + +static int enqueue_on_spu (struct kspu_work_item *kspu_work) +{ +struct spu_async_req *req =container_of (kspu_work, + struct spu_async_req, kspu_work); +struct kspu_job *work_item; +struct my_sum *msum; +unsigned int i; + + work_item =kspu_get_rb_slot (req->kctx); + + my_sum = &work_item->my_sum; + work_item->operation = SPU_OP_my_add; + + work_item->in = (unsigned long int ) req->n1p; + my_sum->out = (unsigned long int ) req->n1p; + +for (i=0 ; i<16 ; i++) + my_sum->num[i] = req->n2; + + kspu_work->notify = my_add_finish_callback; +kspu_mark_rb_slot_ready (req->kctx, kspu_work); +return 1 ; +} + +static void enqueue_request (struct spu_async_req *req) +{ +struct kspu_work_item *work = &asy_d_ctx->kspu_work; + + work->enqueue = enqueue_on_spu; + +kspu_enqueue_work_item (ctx->spe_ctx->ctx, &req->kspu_work); +} + +static unsigned char n1[16] __attribute__((aligned(16))); + +static int __init skeleton_init (void ) +{ +struct async_d_request req; +unsigned int i; + + req.kctx =kspu_get_kctx (); + +for (i=0 ; i<sizeof (n1); i++) + n1[i] = i*i; + + req.n1p = n1; + req.n2 =20 ; + +init_completion (&req.mcompletion); +enqueue_request (req); + +wait_for_completion_interruptible (&req.mcompletion); + +for (i=0 ; i<sizeof (n1); i++) +printk ("0x%02x " , n1[i]); + +printk ("\n" ); + +/* don't load me plz */ +return -ENOMEM ; +} + +static void __exit skeleton_finit(void) +{ +} + +module_init (skeleton_init); +module_exit (skeleton_finit); + +MODULE_DESCRIPTION ("KSPU Skeleton for the PPU part" ); +MODULE_AUTHOR ("John Doe <[email protected]>" ); +MODULE_LICENSE ("GPL" );
Documentation/powerpc/kspu_spu_skeleton.c new file +19 -0
@@ -0,0 +1,19 @@ +/* +* KSPU skeleton - SPU part +* +*/ + +#include <spu_intrinsics.h> +#include <asm/kspu/spu_skeleton.h> +#include <asm/kspu/merged_code.h> + +void spu_my_add (struct kspu_job *kjob,void *buffer,unsigned int buf_num) +{ + vector unsigned char sum1 = (*((vector unsigned char *)(buffer))); +struct my_sum *my_sum = &kjob->my_sum; + vector unsigned char sum2 = (*((vector unsigned char *)(my_sum->num))); + vector unsigned char sum; + + sum =spu_add (sum1, sum2); +init_put_data (&sum, my_sum->out,16 , buf_num); +}
Documentation/powerpc/kspu_spu_skeleton.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef asm_kspu_spu_skeleton_h +#define asm_kspu_spu_skeleton_h + +struct my_sum { +unsigned char num[16 ] __attribute__((aligned (16 ))); +unsigned long long out __attribute__((aligned (16 ))); +}; + +#endif
PATCH 5/10 spufs: kspu add required declarations
In reply to Sebastian Siewiorspu_run_init() and spu_run_fini() need to be used outside of spufs/run.c but within spufs.ko.
arch/powerpc/platforms/cell/spufs/run.c +2 -3
@@ -126,7 +126,7 @@ out:return ret; } -static int spu_run_init (struct spu_context *ctx,u32 *npc) +int spu_run_init (struct spu_context *ctx,u32 *npc) {spuctx_switch_state (ctx,SPU_UTIL_SYSTEM );
@@ -160,8 +160,7 @@ static int spu_run_init(struct spu_contereturn 0 ; } -static int spu_run_fini (struct spu_context *ctx,u32 *npc, -u32 *status) +int spu_run_fini (struct spu_context *ctx,u32 *npc,u32 *status) {int ret =0 ;
arch/powerpc/platforms/cell/spufs/spufs.h +4 -0
@@ -254,6 +254,10 @@ void spu_sched_exit(void);extern char *isolated_loader; +/* sched */ +int spu_run_init (struct spu_context *ctx,u32 *npc); +int spu_run_fini (struct spu_context *ctx,u32 *npc,u32 *status); +/* * spufs_wait * Same as wait_event_interruptible(), except that here
PATCH 6/10 spufs: add kspu_alloc_context()
In reply to Sebastian SiewiorFunction behaves like alloc_spu_context() but instead of current-> init task's informations are used and the problem state bit is removed.
arch/powerpc/platforms/cell/spufs/context.c +34 -2
@@ -32,7 +32,8 @@atomic_t nr_spu_contexts =ATOMIC_INIT (0 ); -struct spu_context *alloc_spu_context (struct spu_gang *gang) +static struct spu_context *__alloc_spu_context (struct spu_gang *gang, +struct mm_struct *mm) {struct spu_context *ctx; ctx =kzalloc (sizeof *ctx,GFP_KERNEL );
@@ -54,7 +55,7 @@ struct spu_context *alloc_spu_context(stinit_waitqueue_head (&ctx->mfc_wq); ctx->state =SPU_STATE_SAVED ; ctx->ops = &spu_backing_ops; - ctx->owner =get_task_mm (current); + ctx->owner = mm;INIT_LIST_HEAD (&ctx->rq);INIT_LIST_HEAD (&ctx->aff_list);if (gang)
@@ -73,6 +74,37 @@ out:return ctx; } +struct spu_context *alloc_spu_context (struct spu_gang *gang) +{ +struct mm_struct *mm; +struct spu_context *ctx; + + mm =get_task_mm (current); + ctx =__alloc_spu_context (gang, mm); +if (!ctx) +mmput (mm); +return ctx; +} + +struct spu_context *kspu_alloc_context (void ) +{ +struct spu_context *ctx; + +/* for priviliged spu context, we borrow all the task specific +* informations from init_task. +*/ +atomic_inc (&init_mm.mm_users); + ctx =__alloc_spu_context (NULL , &init_mm); +if (!ctx) { +mmput (&init_mm); +return ctx; + } + +/* remove problem state bit in order to access kernel memory */ + ctx->csa.priv1.mfc_sr1_RW &= ~MFC_STATE1_PROBLEM_STATE_MASK ; +return ctx; +} +void destroy_spu_context (struct kref *kref) {struct spu_context *ctx;
arch/powerpc/platforms/cell/spufs/spufs.h +1 -0
@@ -232,6 +232,7 @@ static inline void spu_release(struct sp }struct spu_context *alloc_spu_context (struct spu_gang *gang); +struct spu_context *kspu_alloc_context (void );void destroy_spu_context (struct kref *kref);struct spu_context *get_spu_context (struct spu_context *ctx);int put_spu_context (struct spu_context *ctx);
PATCH 7/10 spufs: add kernel support for spu task
In reply to Sebastian SiewiorUtilization of SPUs by the kernel, main implementation. Functions that are offloaded to the SPU must be spitted into two parts:
- SPU part (executing)
- PPU part (prepare/glue)
The SPU part expects a buffer and maybe some other parameters and performs the work on the buffer. After the work/job is done, it requests the transfer back into main memory.
The PPU part needs to split the information into this kind of job. Every job consists of one buffer (16 KiB max) and a few parameters. Once everything is prepared, the request is added to a list. There is soft limit for the number of requests that fit into this list. Once the limit is reached, all request are dropped (unless a flag is passed in order not to). The limit makes sure the user is not trying to process faster than the SPU is capable of. The "queue anyway" flag is necessary because under some circumstances the user may not be able to drop the request or try again later.
A separate thread dequeues the request(s) from the list and calls a user supplied function in order to enqueue this request in a ring buffer which is located on the SPU. This transit stop enables
- enqueuing items if the ring buffer is full (not the list)
- enqueuing items from non-blocking context
After the callback function returns, the SPU starts "immediately" the work. Once the SPU performed the work, KSPU invokes another callback to inform the user, that his request is complete.
The PPU code is responsible for proper alignment & transfer size.
arch/powerpc/platforms/cell/Kconfig +7 -0
@@ -54,6 +54,13 @@ config SPU_BASE bool default n +config KSPU + bool "Support for utilisation of SPU by the kernel" + depends on SPU_FS && EXPERIMENTAL + help + With this option enabled, the kernel is able to utilize the SPUs for its + own tasks. + config CBE_RAS bool "RAS features for bare metal Cell BE" depends on PPC_CELL_NATIVE
arch/powerpc/platforms/cell/spufs/Makefile +1 -0
@@ -3,6 +3,7 @@ obj-y += switch.o fault.o lscsa_alloc.o obj-$(CONFIG_SPU_FS) += spufs.o spufs-y += inode.o file.o context.o syscalls.o coredump.o spufs-y += sched.o backing_ops.o hw_ops.o run.o gang.o +spufs-$(CONFIG_KSPU) += kspu.o # Rules to build switch.o with the help of SPU tool chain SPU_CROSS := spu-
arch/powerpc/platforms/cell/spufs/inode.c +9 -0
@@ -791,10 +791,17 @@ static int __init spufs_init(void)if (ret) goto out_syscalls; + ret =kspu_init (); +if (ret) + goto out_archcoredump; +spufs_init_isolated_loader ();return 0 ; +out_archcoredump: +printk ("kspu_init() failed\n" ); +unregister_arch_coredump_calls (&spufs_coredump_calls); out_syscalls:unregister_spu_syscalls (&spufs_calls); out_fs:
@@ -804,12 +811,14 @@ out_sched: out_cache:kmem_cache_destroy (spufs_inode_cache); out: +printk ("spufs init not performed\n" );return ret; }module_init (spufs_init); static void __exit spufs_exit(void) { +kspu_exit ();spu_sched_exit ();spufs_exit_isolated_loader ();unregister_arch_coredump_calls (&spufs_coredump_calls);
arch/powerpc/platforms/cell/spufs/kspu.c new file +645 -0
645 lines not on this page · raw message
arch/powerpc/platforms/cell/spufs/kspu_util.h new file +30 -0
@@ -0,0 +1,30 @@ +#ifndef KSPU_UTIL_H +#define KSPU_UTIL_H +#include <linux/wait.h> + +struct kspu_code { +const unsigned int *code; +unsigned int code_len; +unsigned int kspu_data_offset; +unsigned int queue_mask; +unsigned int queue_entr_size; +}; + +struct notify_cb_info { +void *notify; +}; + +struct kspu_context { +struct spu_context *spu_ctx; +wait_queue_head_t newitem_wq; +void **notify_cb_info; +unsigned int last_notified; +struct kspu_code *spu_code; +struct task_struct *thread; +/* spinlock protects qlen + work_queue */ +spinlock_t queue_lock; +unsigned int qlen; +struct list_head work_queue; +}; + +#endif
arch/powerpc/platforms/cell/spufs/spufs.h +14 -0
@@ -344,4 +344,18 @@ static inline void spuctx_switch_state(s } } +#ifdef CONFIG_KSPU +int __init kspu_init (void ); +void __exit kspu_exit(void); +#else +static inline int kspu_init (void ) +{ +return 0 ; +} + +static inline void kspu_exit (void ) +{ +} +#endif +#endif
include/asm-powerpc/kspu/kspu.h new file +35 -0
@@ -0,0 +1,35 @@ +#ifndef KSPU_KSPU_H +#define KSPU_KSPU_H +#include <linux/list.h> +#include <asm/kspu/merged_code.h> + +/* +* If the queue is full, the request must be accepted (it can't be droped). +* The user that uses this flag should make sure that further requests arrive +* more slowly +*/ +#define KSPU_MUST_BACKLOG 0x1 + +/* +* Max number of requests that may be in the queue. All following items are +* discared if the KSPU_MUST_BACKLOG is not specified (it seems that the SPE +* is not working fast enough). +*/ +#define KSPU_MAX_QUEUE_LENGTH 400 + +struct kspu_work_item { +struct list_head list; +int (*enqueue)(struct kspu_work_item *); +void (*notify)(struct kspu_work_item *,struct kspu_job *); +}; + +struct kspu_context ; + +struct kspu_job *kspu_get_rb_slot (struct kspu_context *kspu); +void kspu_mark_rb_slot_ready (struct kspu_context *kspu, +struct kspu_work_item *work); +int kspu_enqueue_work_item (struct kspu_context *kctx, +struct kspu_work_item *work,unsigned int flags); +struct kspu_context *kspu_get_kctx (void ); + +#endif
Re: [patch 07/10] spufs: add kernel support for spu task
arch/powerpc/platforms/cell/Kconfig config SPU_BASE · line 62
Quoted 6 lines · Sebastian Siewior
arch/powerpc/platforms/cell/Kconfig +6
-0+config KSPU + bool "Support for utilisation of SPU by the kernel" + depends on SPU_FS && EXPERIMENTAL + help + With this option enabled, the kernel is able to utilize the SPUs for its + own tasks.
It might be better to not have this user-selectable at all, but to autoselect the option when it's used by other code.
arch/powerpc/platforms/cell/spufs/inode.c out_sched: · line 816
Quoted 13 lines
arch/powerpc/platforms/cell/spufs/inode.c +4
-0+out_archcoredump: +printk ("kspu_init() failed\n" ); +unregister_arch_coredump_calls (&spufs_coredump_calls); out_syscalls:unregister_spu_syscalls (&spufs_calls); out_fs:@@ -804,12 +811,14 @@ out_sched: out_cache:kmem_cache_destroy (spufs_inode_cache); out: +printk ("spufs init not performed\n" );return ret; }
The printk lines don't follow the convention of using KERN_* printk levels. I suggest you either remove them or turn them into pr_debug().
arch/powerpc/platforms/cell/spufs/kspu.c line 42
Quoted 9 lines
arch/powerpc/platforms/cell/spufs/kspu.c new file +9
-0+ +#include <asm/spu_priv1.h> +#include <asm/kspu/kspu.h> +#include <asm/kspu/merged_code.h> +#include <linux/kthread.h> +#include <linux/module.h> +#include <linux/init_task.h> +#include <linux/hardirq.h> +#include <linux/kernel.h>
#include lines should be ordered alphabetically, and <asm/ lines come after <linux/ lines.
Quoted 25 lines
arch/powerpc/platforms/cell/spufs/kspu.c new file +25
-0+/* +* based on run.c spufs_run_spu +*/ +static int spufs_run_kernel_spu (void *priv) +{ +struct kspu_context *kctx = (struct kspu_context *) priv; +struct spu_context *ctx = kctx->spu_ctx; +int ret; +u32 status; +unsigned int npc =0 ; +int fastpath; +DEFINE_WAIT (wait_for_stop); +DEFINE_WAIT (wait_for_ibox); +DEFINE_WAIT (wait_for_newitem); + +spu_enable_spu (ctx); + ctx->event_return =0 ; +spu_acquire (ctx); +if (ctx->state ==SPU_STATE_SAVED ) { +__spu_update_sched_info (ctx); + + ret =spu_activate (ctx,0 ); +if (ret) { +spu_release (ctx); + printk(KERN_ERR "could not obtain runnable spu: %d\n",
arch/powerpc/platforms/cell/spufs/kspu.c line 496
Quoted 10 lines
arch/powerpc/platforms/cell/spufs/kspu.c new file +10
-0+ ret); +BUG (); + } + }else { +/* +* We have to update the scheduling priority under active_mutex +* to protect against find_victim(). +*/ +spu_update_sched_info (ctx); + }
The code you have copied this from has recently been changed to also set an initial time slice, you should do the same change here.
Quoted 78 lines
arch/powerpc/platforms/cell/spufs/kspu.c new file +78
-0+ +spu_run_init (ctx, &npc); + do { + fastpath = 0; + prepare_to_wait(&ctx->stop_wq, &wait_for_stop, + TASK_INTERRUPTIBLE); + prepare_to_wait(&ctx->ibox_wq, &wait_for_ibox, + TASK_INTERRUPTIBLE); + prepare_to_wait(&kctx->newitem_wq, &wait_for_newitem, + TASK_INTERRUPTIBLE); + + if (unlikely(test_and_clear_bit(SPU_SCHED_NOTIFY_ACTIVE, + &ctx->sched_flags))) { + + if (!(status & SPU_STATUS_STOPPED_BY_STOP)) { + spu_switch_notify(ctx->spu, ctx); + } + } + + spuctx_switch_state(ctx, SPU_UTIL_SYSTEM); + + pr_debug("going to handle class1\n"); + ret = spufs_handle_class1(ctx); + if (unlikely(ret)) { + /* + * SPE_EVENT_SPE_DATA_STORAGE => refernce invalid memory + */ + printk(KERN_ERR "Invalid memory dereferenced by the" + "spu: %d\n", ret); + BUG(); + } + + /* FIXME BUG: We need a physical SPU to discover + * ctx->spu->class_0_pending. It is not saved on context + * switch. We may lose this on context switch. + */ + status = ctx->ops->status_read(ctx); + if (unlikely((ctx->spu && ctx->spu->class_0_pending) || + status & SPU_STATUS_INVALID_INSTR)) { + printk(KERN_ERR "kspu error, status_register: 0x%08x\n", + status); + printk(KERN_ERR "event return: 0x%08lx, spu's npc: " + "0x%08x\n", kctx->spu_ctx->event_return, + kctx->spu_ctx->ops->npc_read( + kctx->spu_ctx)); + printk(KERN_ERR "class_0_pending: 0x%lx\n", ctx->spu->class_0_pending); + print_kctx_debug(kctx); + BUG(); + } + + if (notify_done_reqs(kctx)) + fastpath = 1; + + if (queue_requests(kctx)) + fastpath = 1; + + if (!(status & SPU_STATUS_RUNNING)) { + /* spu is currently not running */ + pr_debug("SPU not running, last stop code was: %08x\n", + status >> SPU_STOP_STATUS_SHIFT); + if (pending_spu_work(kctx)) { + /* spu should run again */ + pr_debug("Activate SPU\n"); + kspu_fill_dummy_reqs(kctx); + + spu_run_fini(ctx, &npc, &status); + spu_acquire_runnable(ctx, 0); + spu_run_init(ctx, &npc); + } else { + /* spu finished work */ + pr_debug("SPU will remain in stop state\n"); + spu_run_fini(ctx, &npc, &status); + spu_yield(ctx); + spu_acquire(ctx); + } + } else { + pr_debug("SPU is running, switch state to util user\n"); + spuctx_switch_state(ctx, SPU_UTIL_USER);
arch/powerpc/platforms/cell/spufs/kspu.c line 584
Quoted 10 lines
arch/powerpc/platforms/cell/spufs/kspu.c new file +10
-0+ } + +if (fastpath) +continue ; + +spu_release (ctx); +schedule (); +spu_acquire (ctx); + + }while (!kthread_should_stop () || !list_empty (&kctx->work_queue));
The inner loop is rather long, in an already long function. Can you split out parts into separate functions here?
arch/powerpc/platforms/cell/spufs/spufs.h static inline void spuctx_switch_state(s · line 349
Quoted 10 lines
(quoted diff) +1
-0+#endifarch/powerpc/platforms/cell/spufs/spufs.h +3
-0@@ -344,4 +344,18 @@ static inline void spuctx_switch_state(s } } +#ifdef CONFIG_KSPU +int __init kspu_init (void ); +void __exit kspu_exit(void);
The __init and __exit specifiers are not meaningful in the declaration, you only need them in the definition.
Arnd <><
PATCH 8/10 spufs: SPE side implementation of kspu
In reply to Sebastian SiewiorThe SPU part of KSPU which consists of the a multiplexor and one helper function. The multiplexor invokes the offloaded functions and performs multi buffering (DMA_BUFFERS=2 -> double buffering, DMA_BUFFERS= -> triple \ldots). The offloaded function cares only about processing the buffer and arranging the transfer of the result. Waiting for the transfers to complete as well as signaling the completion of functions is taken care of by the multiplexor.
arch/powerpc/platforms/cell/spufs/Makefile +10 -2
@@ -12,13 +12,21 @@ SPU_AS := $(SPU_CROSS)gcc SPU_LD := $(SPU_CROSS)ld SPU_OBJCOPY := $(SPU_CROSS)objcopy SPU_CFLAGS := -O2 -Wall -I$(srctree)/include \ - -I$(objtree)/include2 -D__KERNEL__ + -I$(objtree)/include2 -D__KERNEL__ -ffreestanding SPU_AFLAGS := -c -D__ASSEMBLY__ -I$(srctree)/include \ -I$(objtree)/include2 -D__KERNEL__ SPU_LDFLAGS := -N -Ttext=0x0 $(obj)/switch.o: $(obj)/spu_save_dump.h $(obj)/spu_restore_dump.h -clean-files := spu_save_dump.h spu_restore_dump.h +clean-files := spu_save_dump.h spu_restore_dump.h spu_kspu_dump.h + +$(obj)/kspu.o: $(obj)/spu_kspu_dump.h + +spu_kspu_code_obj-y += $(obj)/spu_main.o $(obj)/spu_runtime.o +spu_kspu_code_obj-y += $(spu_kspu_code_obj-m) + +$(obj)/spu_kspu: $(spu_kspu_code_obj-y) + $(call if_changed,spu_ld) # Compile SPU files cmd_spu_cc = $(SPU_CC) $(SPU_CFLAGS) -c -o $@ $<
arch/powerpc/platforms/cell/spufs/spu_main.c new file +116 -0
116 lines not on this page · raw message
arch/powerpc/platforms/cell/spufs/spu_runtime.c new file +40 -0
@@ -0,0 +1,40 @@ +/* +* Runtime helper functions, which intend to replace libc. They can't be merged +* into spu_main.c because it must be guaranteed that _start() starts at 0x0. +* +* Author: Sebastian Siewior <[email protected]> +* License: GPLv2 +*/ + +#include <spu_intrinsics.h> +#include <asm/kspu/merged_code.h> + +void spu_nop (struct kspu_job *kjob,void *buffer,unsigned int buf_num) +{ +} + +/* +* memcpy_aligned - copy memory +* @src: source of memory +* @dst: destination +* @num: number of bytes +* +* Copies @num bytes from @src to @dst. @src & @dst must be aligned at +* 16byte boundary. If @src or @dst is not properly aligned, wrong data will be +* read and or written. @num must be multiple of 16. If @num is not multiple of +* 16 than the function simply do nothing +*/ +void memcpy_aligned (void *dest,const void *src,unsigned int num) +{ + const vector unsigned char *s = src; + vector unsigned char *d = dest; + +if (num &15 ) +return ; +do { + *d = *s; + s++; + d++; + num -=16 ; + }while (num); +}
arch/powerpc/platforms/cell/spufs/spu_runtime.h new file +29 -0
@@ -0,0 +1,29 @@ +#ifndef SPU_RUNTIME_H +#define SPU_RUNTIME_H +#include <spu_mfcio.h> + +static inline void init_get_data (void *buf,struct kspu_job *job, +unsigned int dma_tag) +{ +mfc_getb (buf, job->in, job->in_size, dma_tag,0 ,0 ); +} + +static inline void init_put_data (void *buf,unsigned long long ea, +unsigned int size,unsigned int dma_tag) +{ +mfc_putf (buf, ea, size, dma_tag,0 ,0 ); +} + +static inline void wait_for_buffer (unsigned int dma_tag) +{ +mfc_write_tag_mask (dma_tag); +spu_mfcstat (MFC_TAG_UPDATE_ALL ); +} + +void memcpy_aligned (void *dest,const void *src,unsigned int n); + +/* exported offloaded functions */ +void spu_nop (struct kspu_job *kjob,void *buffer, +unsigned int buf_num); + +#endif
include/asm-powerpc/kspu/merged_code.h new file +51 -0
51 lines not on this page · raw message
PATCH 9/10 spufs: SPU-AES support (kernel side)
In reply to Sebastian SiewiorThis patch implements the AES cipher algorithm in ECB & CBC blockmode which is executed on the SPU using the crypto async interface & kspu.
CBC has one limitiation: The IV is written back in the notification callback. That means that it is not available for crypto requests that depend on the previous IV (as well as crypto requests >16 KiB). Herbert Xu pointer out, that this is currently not the case. For instance:
- IPsec brings its own IV on with every packet. A packet is usually <= 1500 bytes. The trouble starts with jumbo frames
- EcryptFS changes the IV on page bassis (every enc/dec request is PAGE_SIZE long).
arch/powerpc/platforms/cell/Makefile +2 -1
@@ -24,6 +24,7 @@ obj-$(CONFIG_SPU_BASE) += spu_callback $(spufs-modular-m) \ $(spu-priv1-y) \ $(spu-manage-y) \ - spufs/ + spufs/ \ + crypto/ obj-$(CONFIG_PCI_MSI) += axon_msi.o
arch/powerpc/platforms/cell/crypto/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# +# Crypto, arch specific +# +CFLAGS_aes_vmx_key.o += -O3 -maltivec +aes_spu-objs := aes_spu_glue.o aes_vmx_key.o +obj-$(CONFIG_CRYPTO_AES_SPU) += aes_spu.o
arch/powerpc/platforms/cell/crypto/aes_spu_glue.c new file +462 -0
462 lines not on this page · raw message
arch/powerpc/platforms/cell/crypto/aes_vmx_key.c new file +283 -0
283 lines not on this page · raw message
arch/powerpc/platforms/cell/crypto/aes_vmx_key.h new file +7 -0
@@ -0,0 +1,7 @@ +#ifndef __aes_vmx_addon_h__ +#define __aes_vmx_addon_h__ + +int expand_key (const unsigned char *key,unsigned int keylen, +unsigned char exp_enc_key[15 *4 *4 ], +unsigned char exp_dec_key[15 *4 *4 ]); +#endif
arch/powerpc/platforms/cell/spufs/Makefile +2 -1
@@ -11,7 +11,7 @@ SPU_CC := $(SPU_CROSS)gcc SPU_AS := $(SPU_CROSS)gcc SPU_LD := $(SPU_CROSS)ld SPU_OBJCOPY := $(SPU_CROSS)objcopy -SPU_CFLAGS := -O2 -Wall -I$(srctree)/include \ +SPU_CFLAGS := -O3 -Wall -I$(srctree)/include \ -I$(objtree)/include2 -D__KERNEL__ -ffreestanding SPU_AFLAGS := -c -D__ASSEMBLY__ -I$(srctree)/include \ -I$(objtree)/include2 -D__KERNEL__
@@ -23,6 +23,7 @@ clean-files := spu_save_dump.h spu_resto $(obj)/kspu.o: $(obj)/spu_kspu_dump.h spu_kspu_code_obj-y += $(obj)/spu_main.o $(obj)/spu_runtime.o +spu_kspu_code_obj-$(CONFIG_CRYPTO_AES_SPU) += $(obj)/spu_aes.o spu_kspu_code_obj-y += $(spu_kspu_code_obj-m) $(obj)/spu_kspu: $(spu_kspu_code_obj-y)
arch/powerpc/platforms/cell/spufs/spu_aes.c new file +677 -0
677 lines not on this page · raw message
arch/powerpc/platforms/cell/spufs/spu_main.c +5 -0
@@ -11,6 +11,11 @@ static spu_operation_t spu_ops[TOTAL_SPU_OPS] __attribute__((aligned(16))) = { [SPU_OP_nop] = spu_nop, + [SPU_OP_aes_update_key] = spu_aes_update_key, + [SPU_OP_aes_encrypt_ecb] = spu_aes_encrypt_ecb, + [SPU_OP_aes_decrypt_ecb] = spu_aes_decrypt_ecb, + [SPU_OP_aes_encrypt_cbc] = spu_aes_encrypt_cbc, + [SPU_OP_aes_decrypt_cbc] = spu_aes_decrypt_cbc, };static unsigned char kspu_buff[DMA_BUFFERS ][DMA_MAX_TRANS_SIZE ];
arch/powerpc/platforms/cell/spufs/spu_runtime.h +10 -0
@@ -26,4 +26,14 @@ void memcpy_aligned(void *dest, const vovoid spu_nop (struct kspu_job *kjob,void *buffer,unsigned int buf_num); +void spu_aes_update_key (struct kspu_job *kjob,void *buffer, +unsigned int buf_num); +void spu_aes_encrypt_ecb (struct kspu_job *kjob,void *buffer, +unsigned int buf_num); +void spu_aes_decrypt_ecb (struct kspu_job *kjob,void *buffer, +unsigned int buf_num); +void spu_aes_encrypt_cbc (struct kspu_job *kjob,void *buffer, +unsigned int buf_num); +void spu_aes_decrypt_cbc (struct kspu_job *kjob,void *buffer, +unsigned int buf_num);#endif
drivers/crypto/Kconfig +13 -0
@@ -48,6 +48,19 @@ config CRYPTO_DEV_PADLOCK_SHA source "arch/s390/crypto/Kconfig" +config CRYPTO_AES_SPU + tristate "AES cipher algorithm (SPU support)" + select CRYPTO_ABLKCIPHER + depends on SPU_FS && KSPU + help + AES cipher algorithms (FIPS-197). AES uses the Rijndael + algorithm. + The AES specifies three key sizes: 128, 192 and 256 bits. + See <http://csrc.nist.gov/CryptoToolkit/aes/> for more information. + + This version of AES performs its work on a SPU core and supports + ECB and CBC block mode + config CRYPTO_DEV_GEODE tristate "Support for the Geode LX AES engine" depends on X86_32 && PCI
include/asm-powerpc/kspu/aes.h new file +28 -0
@@ -0,0 +1,28 @@ +#ifndef __SPU_AES_H__ +#define __SPU_AES_H__ + +#define MAX_AES_ROUNDS 15 +#define MAX_AES_KEYSIZE_INT (MAX_AES_ROUNDS * 4) +#define MAX_AES_KEYSIZE_BYTE (MAX_AES_KEYSIZE_INT * 4) +#define SPU_KEY_SLOTS 5 + +struct aes_key_struct { +unsigned char enc[MAX_AES_KEYSIZE_BYTE ] __attribute__((aligned (16 ))); +unsigned char dec[MAX_AES_KEYSIZE_BYTE ] __attribute__((aligned (16 ))); +unsigned int len __attribute__((aligned (16 ))); +}; + +struct aes_update_key { +/* copy key from ea to ls into a specific slot */ +unsigned int keyid __attribute__((aligned (16 ))); +}; + +struct aes_crypt { +/* in */ +unsigned int keyid __attribute__((aligned (16 ))); + +/* out */ +unsigned char iv[16 ] __attribute__((aligned (16 )));/* as well as in */ +unsigned long long out __attribute__((aligned (16 ))); +}; +#endif
include/asm-powerpc/kspu/merged_code.h +9 -0
@@ -1,5 +1,6 @@#ifndef KSPU_MERGED_CODE_H #define KSPU_MERGED_CODE_H +#include <asm/kspu/aes.h> #define KSPU_LS_SIZE 0x40000
@@ -17,6 +18,12 @@*/ enum SPU_OPERATIONS { SPU_OP_nop, + SPU_OP_aes_setkey, + SPU_OP_aes_update_key, + SPU_OP_aes_encrypt_ecb, + SPU_OP_aes_decrypt_ecb, + SPU_OP_aes_encrypt_cbc, + SPU_OP_aes_decrypt_cbc,TOTAL_SPU_OPS , };
@@ -30,6 +37,8 @@ struct kspu_job {* function. */ union { +struct aes_update_key aes_update_key; +struct aes_crypt aes_crypt; } __attribute__((aligned (16 ))); };
A message that is not in the archive (sent off-list or not yet received): [email protected]
Herbert Xu
maintainerRe: [patch 1/1] spufs: SPU-AES support (kspu+ablkcipher user)
Quoted 4 lines · Sebastian Siewior
Herbert, could you please ACK / NACK your bits?
I addedablkcipher_request()in the driver with a proper type as you suggested.
The crypto bits look alright.
Do you plan to use this for anything beyond AES? If so it would be good to come up a way to share some of the crypto code between the main kernel and what runs on the SPUs.
Cheers,
Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txtRe: [patch 1/1] spufs: SPU-AES support (kspu+ablkcipher user)
Quoted 1 line · * Herbert Xu | 2007-08-29 15:15:07 [+0800]:
Do you plan to use this for anything beyond AES? If so it
I probably don't, others maybe.
Quoted 2 lines
would be good to come up a way to share some of the crypto code between the main kernel and what runs on the SPUs.
Yes. You could share almost everything except the setkey() function on the kernel side.
Quoted 1 line
Cheers,
Sebastian
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [Cbe-oss-dev] [patch 1/1] spufs: SPU-AES support (kspu+ablkcipher user)
Quoted 11 lines · * Paul Mackerras | 2007-08-29 09:03:35 [+1000]:
Quoted 8 lines · Sebastian Siewior
CBC has one limitiation: The IV is written back in the notification callback. That means that it is not available for crypto requests that depend on the previous IV (as well as crypto requests >16 KiB). Herbert Xu pointer out, that this is currently not the case. For instance:
- IPsec brings its own IV on with every packet. A packet is usually <= 1500 bytes. Jumbo frames should not exceed 16 KiB.
- EcryptFS changes the IV on page bassis (every enc/dec request isPAGE_SIZElong).
Quoted 1 line
The page size could be 64kB.
Yes, I am aware of this. That's why I mentioned it here. The only way way how I could fix it is by caching the IV the same/similar way I do it for the key. I had no time to implement this so far and it should not break IPsec or EcryptFS if you don't force it :)
Quoted 1 line
Paul.
Sebastian
PATCH 10/10 cryptoapi: async speed test
In reply to Sebastian SiewiorThis was used to test the async AES algo. trcypt is working as well but does enqueue that much :-)
crypto/Kconfig +6 -0
@@ -468,6 +468,12 @@ config CRYPTO_TEST help Quick & dirty crypto test module. +config CRYPTO_LIMI_SPEED_ASYNC + tristate "Crypto algorithm speed test with msec resolution" + help + insmod/modprobe the module, and watch dmesg for results. + Test is for aes only, see modinfo for options + source "drivers/crypto/Kconfig" endif # if CRYPTO
crypto/Makefile +1 -0
@@ -50,6 +50,7 @@ obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += mich obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o +obj-$(CONFIG_CRYPTO_LIMI_SPEED_ASYNC) += limi-speeda.o # # generic algorithms and the async_tx api
crypto/limi-speeda.c new file +191 -0
191 lines not on this page · raw message