From patchwork Thu Apr 13 09:52:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 9679067 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B15E760326 for ; Thu, 13 Apr 2017 09:52:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ABBEA285C9 for ; Thu, 13 Apr 2017 09:52:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9ED9D28652; Thu, 13 Apr 2017 09:52:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D2BF3285C9 for ; Thu, 13 Apr 2017 09:52:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751503AbdDMJwb (ORCPT ); Thu, 13 Apr 2017 05:52:31 -0400 Received: from frisell.zx2c4.com ([192.95.5.64]:58901 "EHLO frisell.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750765AbdDMJwa (ORCPT ); Thu, 13 Apr 2017 05:52:30 -0400 Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTP id de652762; Thu, 13 Apr 2017 09:45:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=from:to:cc :subject:date:message-id; s=mail; bh=cpJ5a7iNFOTAsTHbW9xEOdG5rnY =; b=GuOf3olbxqEJ+P8ifBhiA+c5m4L3INZaTQlbD9Yzzw0McpYewTXp5Wcla/y l0Cx6B4Xwc4zdeL2UknXR0DLSRdt7ndAu/seFCXq3s1O2BD75YSgnikynYHvLlxf y92V+TWONaoMNFq5LZkmBCpOuyntXOY3urCgRbtTbB41C8CctkEAVf9D9sKcblsr xXVMxTHURMX546odAFq31EL2TmXe83ftWZN6AoyjYzFwu7WVtybPTJSotST+r7tI coFIix8UcgSWBKCuJcz/C+6x6eaBuu5DwWzBMMXplhCETcN1n+JlS7X8nDGDzM+N xl/PA6n4LvkzKpt9reWClvzZ8Bg== Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 4bc86555 (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Thu, 13 Apr 2017 09:45:16 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, steffen.klassert@secunet.com Cc: "Jason A. Donenfeld" Subject: [PATCH] padata: allow caller to control queue length Date: Thu, 13 Apr 2017 11:52:13 +0200 Message-Id: <20170413095213.23513-1-Jason@zx2c4.com> X-Mailer: git-send-email 2.12.2 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Allow users of padata to determine the queue length themselves, via this added helper function, so that we can later remove the hard-coded 1000- job limit. We thus add a helper function, and then move the limiting functionality to pcrypt-proper, since it was the only current consumer relying on the 1000-job limit. We do, however, impose a limit on padata so that the reference count does not have an integer overflow. Signed-off-by: Jason A. Donenfeld --- Documentation/padata.txt | 8 ++++++++ crypto/pcrypt.c | 5 +++++ include/linux/padata.h | 2 ++ kernel/padata.c | 20 +++++++++++++++++--- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Documentation/padata.txt b/Documentation/padata.txt index 7ddfe216a0aa..9347d145bb27 100644 --- a/Documentation/padata.txt +++ b/Documentation/padata.txt @@ -158,3 +158,11 @@ when a padata instance is no longer needed: This function will busy-wait while any remaining tasks are completed, so it might be best not to call it while there is work outstanding. Shutting down the workqueue, if necessary, should be done separately. + +While you cannot have more than 2^31-1 taks submitted at the same time, this +maximum is well above what you might actually want to be submitted. Thus, +callers are encouraged to determine their maximum latency/memory/throughput +constraints, and limit calls to padata_do_parallel() based on the current +queue length, which can be determined with: + + int padata_queue_len(struct padata_instance *pinst); diff --git a/crypto/pcrypt.c b/crypto/pcrypt.c index ee9cfb99fe25..ea321154994b 100644 --- a/crypto/pcrypt.c +++ b/crypto/pcrypt.c @@ -70,6 +70,8 @@ struct pcrypt_aead_ctx { unsigned int cb_cpu; }; +#define MAX_OBJ_NUM 1000 + static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu, struct padata_pcrypt *pcrypt) { @@ -78,6 +80,9 @@ static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu, cpu = *cb_cpu; + if (padata_queue_len(pcrypt->pinst) >= MAX_OBJ_NUM) + return -EBUSY; + rcu_read_lock_bh(); cpumask = rcu_dereference_bh(pcrypt->cb_cpumask); if (cpumask_test_cpu(cpu, cpumask->mask)) diff --git a/include/linux/padata.h b/include/linux/padata.h index 0f9e567d5e15..2482b442f136 100644 --- a/include/linux/padata.h +++ b/include/linux/padata.h @@ -3,6 +3,7 @@ * * Copyright (C) 2008, 2009 secunet Security Networks AG * Copyright (C) 2008, 2009 Steffen Klassert + * Copyright (C) 2016, 2017 Jason A. Donenfeld * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -181,4 +182,5 @@ extern int padata_register_cpumask_notifier(struct padata_instance *pinst, struct notifier_block *nblock); extern int padata_unregister_cpumask_notifier(struct padata_instance *pinst, struct notifier_block *nblock); +extern int padata_queue_len(struct padata_instance *pinst); #endif diff --git a/kernel/padata.c b/kernel/padata.c index ac8f1e524836..6ba2db73413f 100644 --- a/kernel/padata.c +++ b/kernel/padata.c @@ -5,6 +5,7 @@ * * Copyright (C) 2008, 2009 secunet Security Networks AG * Copyright (C) 2008, 2009 Steffen Klassert + * Copyright (C) 2016, 2017 Jason A. Donenfeld * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -32,8 +33,6 @@ #include #include -#define MAX_OBJ_NUM 1000 - static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index) { int cpu, target_cpu; @@ -122,7 +121,7 @@ int padata_do_parallel(struct padata_instance *pinst, if ((pinst->flags & PADATA_RESET)) goto out; - if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM) + if (unlikely(atomic_read(&pd->refcnt) == INT_MAX)) goto out; err = 0; @@ -1021,6 +1020,21 @@ void padata_free(struct padata_instance *pinst) } EXPORT_SYMBOL(padata_free); +/** + * padata_queue_len - retreive the number of in progress jobs + * + * @padata_inst: padata instance from which to read the queue size + */ +int padata_queue_len(struct padata_instance *pinst) +{ + int len; + rcu_read_lock_bh(); + len = atomic_read(&rcu_dereference_bh(pinst->pd)->refcnt); + rcu_read_unlock_bh(); + return len; +} +EXPORT_SYMBOL(padata_queue_len); + #ifdef CONFIG_HOTPLUG_CPU static __init int padata_driver_init(void)