From patchwork Wed Sep 7 14:48:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: PrasannaKumar Muralidharan X-Patchwork-Id: 9319399 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 8CCFA601C0 for ; Wed, 7 Sep 2016 14:48:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7DE67293BD for ; Wed, 7 Sep 2016 14:48:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 722D7293BF; Wed, 7 Sep 2016 14:48:41 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 1AD70293BD for ; Wed, 7 Sep 2016 14:48:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938672AbcIGOsW (ORCPT ); Wed, 7 Sep 2016 10:48:22 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:33858 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936548AbcIGOsU (ORCPT ); Wed, 7 Sep 2016 10:48:20 -0400 Received: by mail-pf0-f194.google.com with SMTP id g202so1064884pfb.1 for ; Wed, 07 Sep 2016 07:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=TIQc/vAJJXZSdcPejeZCo2Tr1A9mtKRi3N9GSyMme/Q=; b=uWFYphjYBuf9xeUvJyTqZ4apY566CElNnk0EMFxkMDEgyA6m0dvXRb5WttKTHO37tF puFlv5bGhXTbPFpzNfd4veXhjQHg+xRc+ysavYWloKDzoWeNY0jgpDo57nSJ4UwTp6RE 7C7pazq+m9jV0yWkMxHfnuHHSxr87kiwVjklwsOqItiRtyKI/FNoqHWxenSCO9AvYIDI 0i0LpSqZw3+hEBWoJvWUznY2FsXRgf8dTaVyQ3sNlH8Z2ITf5oEthkGYx+pN+XSnEUr0 3AnHvTFQ2xQ1VNYGzWT7cFoVMeLdxYY8hYY9Waev3NebTmkw1+EFfdtQA8qN+MNvkPKl o+Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=TIQc/vAJJXZSdcPejeZCo2Tr1A9mtKRi3N9GSyMme/Q=; b=aL3fiMUfEb4McJpIpmJxUA6AsyJ9igtlrSOghvOvL0cN54nJOHy9bbM5tXNs022PFW BO+PH+LLDDNwafXA0ABSFcurCW+NutE5BCifZ12/SnZMKCJyY3Qj8N/zCWFxKMeLngOF Bkr06glDWxPStALI6wPySkPrXaLrjEfVRdfPIolRCVGvXvXYlU86DlLVQX4tWa23JoAe 4p8VqkOzReZuIu95DdZhnGpLDB+/ofheFtOStsgy9rESGIGO/ff9NjIroLbD6F/3rHxm mydat8e23OtHDwmgn62tMJ++Ku8YeweSX1lS9CLT0UdkJ1w2U3zhMYeOfkO07e2J9ixK /rWQ== X-Gm-Message-State: AE9vXwMC6Qo9M1Zad8zjvYvb5gfP6y4U4Uak7G/qFV6RkpUyFBDbQlCaUl1Tpob3Zx2Cfw== X-Received: by 10.98.141.219 with SMTP id p88mr50200155pfk.153.1473259694366; Wed, 07 Sep 2016 07:48:14 -0700 (PDT) Received: from localhost.localdomain ([180.151.32.106]) by smtp.gmail.com with ESMTPSA id x126sm49551438pfx.61.2016.09.07.07.48.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 07 Sep 2016 07:48:13 -0700 (PDT) From: PrasannaKumar Muralidharan To: mpm@selenic.com, herbert@gondor.apana.org.au, lee.jones@linaro.org, jslaby@suse.cz, peter@korsgaard.com, linux-crypto@vger.kernel.org Cc: PrasannaKumar Muralidharan Subject: [PATCH v2] hwrng: core - Allocate memory during module init Date: Wed, 7 Sep 2016 20:18:02 +0530 Message-Id: <1473259682-11655-1-git-send-email-prasannatsmkumar@gmail.com> X-Mailer: git-send-email 2.5.0 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 In core rng_buffer and rng_fillbuf is allocated in hwrng_register only once and it is freed during module exit. This patch moves allocating rng_buffer and rng_fillbuf from hwrng_register to rng core's init. This avoids checking whether rng_buffer and rng_fillbuf was allocated from every hwrng_register call. Also moving them to module init makes it explicit that it is freed in module exit. Change in v2: Fix memory leak when register_miscdev fails. Signed-off-by: PrasannaKumar Muralidharan --- drivers/char/hw_random/core.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c index 9203f2d..4827945 100644 --- a/drivers/char/hw_random/core.c +++ b/drivers/char/hw_random/core.c @@ -449,22 +449,6 @@ int hwrng_register(struct hwrng *rng) goto out; mutex_lock(&rng_mutex); - - /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */ - err = -ENOMEM; - if (!rng_buffer) { - rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL); - if (!rng_buffer) - goto out_unlock; - } - if (!rng_fillbuf) { - rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL); - if (!rng_fillbuf) { - kfree(rng_buffer); - goto out_unlock; - } - } - /* Must not register two RNGs with the same name. */ err = -EEXIST; list_for_each_entry(tmp, &rng_list, list) { @@ -573,7 +557,26 @@ EXPORT_SYMBOL_GPL(devm_hwrng_unregister); static int __init hwrng_modinit(void) { - return register_miscdev(); + int ret = -ENOMEM; + + /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */ + rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL); + if (!rng_buffer) + return -ENOMEM; + + rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL); + if (!rng_fillbuf) { + kfree(rng_buffer); + return -ENOMEM; + } + + ret = register_miscdev(); + if (ret) { + kfree(rng_fillbuf); + kfree(rng_buffer); + } + + return ret; } static void __exit hwrng_modexit(void)