Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp778748imm; Fri, 15 Jun 2018 06:12:10 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ2JS0KUC/gbza4bzg77NDL6RiumOU001JFdk0YkD9VfG3Q9+p47i6bARal8L/xGZsrziIm X-Received: by 2002:a62:d508:: with SMTP id d8-v6mr1930685pfg.128.1529068330876; Fri, 15 Jun 2018 06:12:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529068330; cv=none; d=google.com; s=arc-20160816; b=PdVPpm9NnTkrAkqkdTU0BQH6NWksIRPiIxjPm9yorqYosygRNLSz24aXuI5zxVwlpA 56XkmViMMFtSXkB+Zk4kGAyHvrvji4U/hNrV9jGrQYKlifPHeDVhXh5Wq4tGc5fQqveW M7YmC24L6Dum6j5Z1d/mJH89j3JrmfPNXn3WmVsiFjQtdkyLEvyXP4A49Mrofd10MOtH E429mv58mAefep9eHNSCA0ROQnGZTTNavB51fIIqeBZBFReRKY5uJ4pJ95z8AXG1uu0v a4C73U7Fi19idZdAplcibaFYaO8nHACVoeu1PVHYiIg93DdA79FQ6n3WeThyhMxVQwBi gM+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:subject:message-id:date:from :mime-version:dkim-signature:arc-authentication-results; bh=1LZfApbEyIvPJnPh75xnyCibcYjb+7T5h1LOHYpsamk=; b=JhSn2YzQNwjhyDNOhx4rqk7USqlw+KQG0iJZkht/fd3KpgRx71wVeHS5q3ixcaLtqu yv6BI/S0swwU4qOfceUPLAgLTRn8uEYMOrU5PAAXp2ZSiXLG0CaBcTPEVXeOsyJlHxz/ bN7HTKq5iSUbMwL8I15chIIN9YjjMeYCP+dOCnQsAGs5RT/wdmLs1dP+mrb+k2pfH3b2 IxI8LkcvixBqtgCC42inhksN0dEQ+CW9s3h71pa1/xLUzhH/wrXcPSuiZx6bPWhwbPnZ P2wgf+DvohqSgznD4Ey/2xODekS/E1JNFC4ColdPLLL/3GjLzBWROlrIPK5QyJfILmS8 oj/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=Ha8uTBda; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b18-v6si6037543pgs.417.2018.06.15.06.11.56; Fri, 15 Jun 2018 06:12:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=Ha8uTBda; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965572AbeFONLc (ORCPT + 99 others); Fri, 15 Jun 2018 09:11:32 -0400 Received: from frisell.zx2c4.com ([192.95.5.64]:54517 "EHLO frisell.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936144AbeFONLb (ORCPT ); Fri, 15 Jun 2018 09:11:31 -0400 Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTP id 899d2237 for ; Fri, 15 Jun 2018 13:06:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=mime-version :from:date:message-id:subject:to:content-type; s=mail; bh=AD2nk5 uymHhBT8XbLMXduM0LCNA=; b=Ha8uTBdaNluTBZmGwV8JhrA9D07erFAjjwCg15 oULihI4HIItAuehVw1K4irPvsTzpMZfUcMn2fP08lF5ccUnBdwqotBPrVz2JtUCp XIW71SbPVaLbUkcTB47qF+X4uNJNrQZ27NAYiOEZO+93AAx8Nus2DIh+KK5pWF0G +N9i1qT0zchSiqagT66rBNN9LAb0jvlZo1CIVJzLjSDZ8fEZO3mAh6OCkHto89TW 7HqHq3eo7PR39//uNwp836cdTZdaXoiU5BAxJCgGHOZODCky7AFZs0jz+SD5k8Eu PRKPuLn/Pfx4tKT7PsulZyMtKJubOeNsqlvjpjqTUs9fd3jA== Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 169695b6 (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128:NO) for ; Fri, 15 Jun 2018 13:06:08 +0000 (UTC) Received: by mail-ot0-f175.google.com with SMTP id 101-v6so10927422oth.4 for ; Fri, 15 Jun 2018 06:11:29 -0700 (PDT) X-Gm-Message-State: APt69E235EaU6LpkwjZyjv+OUKRuINq7RDk3yeeYTHlDxxxW/MYdv6Nk ne12WyZEFSzXxAPQTj6R7raT3NkAn6dM6JOJJvQ= X-Received: by 2002:a9d:20e3:: with SMTP id x90-v6mr1010389ota.338.1529068287965; Fri, 15 Jun 2018 06:11:27 -0700 (PDT) MIME-Version: 1.0 From: "Jason A. Donenfeld" Date: Fri, 15 Jun 2018 15:11:15 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Lazy FPU restoration / moving kernel_fpu_end() to context switch To: LKML , X86 ML , Andy Lutomirski Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Andy & folks, Lots of crypto routines look like this: kernel_fpu_begin(); encrypt(); kernel_fpu_end(); If you call such a routine twice, you get: kernel_fpu_begin(); encrypt(); kernel_fpu_end(); kernel_fpu_begin(); encrypt(); kernel_fpu_end(); In a loop this looks like: for (thing) { kernel_fpu_begin(); encrypt(thing); kernel_fpu_end(); } This is obviously very bad, because begin() and end() are slow, so WireGuard does the obvious: kernel_fpu_begin(); for (thing) encrypt(thing); kernel_fpu_end(); This is fine and well, and the crypto API I'm working on will enable this to be done in a clear way, but I do wonder if maybe this is not something that should be happening at the level of the caller, but rather in the fpu functions themselves. Namely, what are your thoughts on modifying kernel_fpu_end() so that it doesn't actually restore the state, but just reenables preemption and marks that on the next context switch, the state should be restored? Then, essentially, kernel_fpu_begin() and end() become free after the first usage of kernel_fpu_begin(). Is this something feasible? I know that performance-wise, I'm really gaining a lot from hoisting those functions out of the loops, and API wise, it'd be slightly simpler to implement if I didn't have to all for that hoisting. Regards, Jason