mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Francis Moreau" <francis.moro@gmail.com>
To: "Roland Dreier" <rdreier@cisco.com>
Cc: "Herbert Xu" <herbert@gondor.apana.org.au>,
	helge.hafting@aitel.hist.no, linux-kernel@vger.kernel.org,
	linux-crypto@vger.kernel.org
Subject: Re: [CRYPTO] is it really optimized ?
Date: Mon, 23 Apr 2007 15:56:28 +0200	[thread overview]
Message-ID: <38b2ab8a0704230656k4863cfb6ne019a443efb22a4b@mail.gmail.com> (raw)
In-Reply-To: <38b2ab8a0704190107x2c151bafx824beca052f97ef6@mail.gmail.com>

Hi

[Sorry for the late answer]

On 4/19/07, Francis Moreau <francis.moro@gmail.com> wrote:
> On 4/17/07, Roland Dreier <rdreier@cisco.com> wrote:
> >  > > It seems trivial to keep the last key you were given and do a quick
> >  > > memcmp in your setkey method to see if it's different from the last
> >  > > key you pushed to hardware, and set a flag if it is.  Then only do
> >  > > your set_key() if you have a new key to pass to hardware.
> >  > >
> >  > > I'm assuming the expense is in the aes_write() calls, and you could
> >  > > avoid them if you know you're not writing something new.
> >
> >  > that's a wrong assumption. aes_write()/aes_read() are both used to
> >  > access to the controller and are slow (no cache involved).
> >
> > Sorry, I wasn't clear.  I meant that the hardware access is what is
> > slow, and that anything you do on the CPU is relatively cheap compared
> > to that.
> >
> > So my suggestion is just to keep a cache (in CPU memory) of what you
> > have already loaded into the HW, and before reloading the HW just
> > check the cache and don't do the actual HW access if you're not going
> > to change the HW contents.  So you avoid any extra aes_write and
> > aes_read calls in the cache hit case.
> >
> > This would have the advantage of making anything that does lots of
> > bulk encryption fast without special casing ecryptfs.
> >
>
> I'm not sure how "memcmp(key, cache, KEY_SIZE)" would impact AES
> performance. I need to give it a test but can't today. I'll do
> tomorrow and give you back the result.
>

OK, I gave it a test and it appears that the cache hit case is
slightly worse than unconditionnal key loading. So it means that
testing that hte key is cached is as long as loading the key into the
controller. Here is what I did in set_key() function:

static void set_key(const char *key)
{
	static u32 my_key[4] __cacheline_aligned;

	u32 key0 = *(const u32 *)(key + 12);
	u32 key1 = *(const u32 *)(key + 8);
	u32 key2 = *(const u32 *)(key + 4);
	u32 key3 = *(const u32 *)(key);
	int timeout = 100;
	u32 miss = 0;

	miss |= key0 ^ my_key[0];
	miss |= key1 ^ my_key[1];
	miss |= key2 ^ my_key[2];
	miss |= key3 ^ my_key[3];
	if (miss == 0)
		return;

	my_key[0] = key0;
	my_key[1] = key1;
	my_key[2] = key2;
	my_key[3] = key3;
	
	aes_write(be32_to_cpu(key0), AES_KEY0);
	aes_write(be32_to_cpu(key1), AES_KEY1);
	aes_write(be32_to_cpu(key2), AES_KEY2);
	aes_write(be32_to_cpu(key3), AES_KEY3);

	/* generate dkey: should take 11 cycles */
	aes_write(aes_read(AES_CR) | CR_DKEYGEN, AES_CR);

	while (aes_read(AES_CR) & CR_DKEYGEN) {
		if (--timeout == 0)
			break;
	}
}

So I was wrong, hardware access is not so expensive as I thought. But
it also means that all instructions executed in the drivers'
encrypt()/decrypt() methods have a real cost and skipping key loadings
is a win.

Using the driver exclusively doesn't seem to be the right solution,
but I don't see another way to do that...
-- 
Francis

      reply	other threads:[~2007-04-23 13:56 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-12 19:39 Francis Moreau
2007-04-13 12:17 ` Helge Hafting
2007-04-13 13:30   ` Francis Moreau
2007-04-14  4:30     ` Herbert Xu
2007-04-14 13:15       ` Francis Moreau
2007-04-14 19:34         ` Herbert Xu
2007-04-14 21:10           ` Francis Moreau
2007-04-15  7:52             ` Herbert Xu
2007-04-16  8:37               ` Francis Moreau
2007-04-17  0:41                 ` Herbert Xu
2007-04-17 12:36                   ` Francis Moreau
2007-04-17 13:04                     ` Evgeniy Polyakov
2007-04-17 13:42                       ` Herbert Xu
2007-04-17 14:01                         ` Francis Moreau
2007-04-17 15:08                           ` Evgeniy Polyakov
2007-04-17 15:34                             ` Francis Moreau
2007-04-17 15:57                               ` Evgeniy Polyakov
2007-04-17 16:18                                 ` Francis Moreau
2007-04-17 17:07                                   ` Evgeniy Polyakov
2007-04-17 13:57                       ` Francis Moreau
2007-04-17 13:40                     ` Herbert Xu
2007-04-17 13:59                       ` Francis Moreau
2007-04-17 14:02                         ` Herbert Xu
2007-04-17 14:41                           ` Francis Moreau
2007-04-17 15:40                             ` Roland Dreier
2007-04-17 16:14                               ` Francis Moreau
2007-04-17 16:33                                 ` Roland Dreier
2007-04-17 17:24                                   ` Francis Moreau
2007-04-17 17:34                                     ` Roland Dreier
2007-04-19  8:07                                       ` Francis Moreau
2007-04-23 13:56                                         ` Francis Moreau [this message]

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=38b2ab8a0704230656k4863cfb6ne019a443efb22a4b@mail.gmail.com \
    --to=francis.moro@gmail.com \
    --cc=helge.hafting@aitel.hist.no \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rdreier@cisco.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®