From patchwork Sun Sep 4 08:29:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: PrasannaKumar Muralidharan X-Patchwork-Id: 9312441 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 B67556075E for ; Sun, 4 Sep 2016 08:32:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8F1BD28801 for ; Sun, 4 Sep 2016 08:32:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6F13F2880A; Sun, 4 Sep 2016 08:32:13 +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 1588928801 for ; Sun, 4 Sep 2016 08:32:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752436AbcIDIcM (ORCPT ); Sun, 4 Sep 2016 04:32:12 -0400 Received: from mail-pa0-f66.google.com ([209.85.220.66]:36176 "EHLO mail-pa0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752003AbcIDIcK (ORCPT ); Sun, 4 Sep 2016 04:32:10 -0400 Received: by mail-pa0-f66.google.com with SMTP id ez1so7672878pab.3 for ; Sun, 04 Sep 2016 01:30:30 -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=BVVudvhhyGmq5gKg1tKQiFd9HPxjjQ/YD2BgPcXfC0g=; b=I3E1v2/IgZ6WnV/6iY4BG60HV+DQUra7UMEGoEK8P4BhdhMPYBJarZbKKGFQmENhKa R9ArE1SGkp7c5HyLM2YXpajOvgTTuNcJj0RvZ0BpsGvEoqGJJyr4YoKZi0B+yDpqXRD9 Zd2aWX53qUpTPJNm3ZiNJ4Tmme22Jp2Y2Bl4lfZ4npAok0OVZBk5lLVpNdcTRHPl9aEy CghL0lNjjDaQGBGJ31DukitvUwOKs2QpjJxuuAl8mcb23AsaKEwFACZ/9Z7+pLZK98Cc XQwet3d4Cu3d5SUI2NGfJjFyrQMDv2togIcDQmx/xfFmNZpTPE69h4/YucKd9QJd/aQi 12Lw== 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=BVVudvhhyGmq5gKg1tKQiFd9HPxjjQ/YD2BgPcXfC0g=; b=FR/BzNZLKmT7sv72B7x4xgQxAD5O98E7WkSubMSxR8UNG6YZKPuZpwDQE8k5HvqMYe pYOsfBsFVTPJEI2sAd23JY7aTy0SA6U5vkBKFmdGvK+7kTIZLJxhCe+Leko/YyAXcaOS aoercqI2r1zhWizoLu/T/IlxSJNb4zCNcLSZilTagKAY/ME21PZhdYKRhNi8+3Z8B9tc tz0FMbtR+NJB3WW4rnMqsBlFm54yMVOyvM2C6tW8xOJtZymsvvs8d3WGmgz+IHbqMQVl W9teH+XZNbkg3ZKTcwPUWbOL7SzOIGYzuSTS1y2hW9qyAJwjs2pOLeawqw5jiCbzIsFv fPlQ== X-Gm-Message-State: AE9vXwNoJBUbLbzyoYSTbsjtSZ7G5/hm1JEjEzFgT2xUy3q6InBeWOk94XMXwzWOXjYbPA== X-Received: by 10.66.253.101 with SMTP id zz5mr52204976pac.32.1472977829577; Sun, 04 Sep 2016 01:30:29 -0700 (PDT) Received: from localhost.localdomain ([1.23.11.244]) by smtp.gmail.com with ESMTPSA id l128sm26009362pfl.21.2016.09.04.01.30.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 04 Sep 2016 01:30:29 -0700 (PDT) From: PrasannaKumar Muralidharan To: mpm@selenic.com, herbert@gondor.apana.org.au, jslaby@suse.cz, peter@korsgaard.com, lee.jones@linaro.org, linux-crypto@vger.kernel.org Cc: prasannatsmkumar@gmail.com Subject: [PATCH] hwrng: core - Allocate memory during module init Date: Sun, 4 Sep 2016 13:59:38 +0530 Message-Id: <1472977778-23996-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. Signed-off-by: PrasannaKumar Muralidharan --- drivers/char/hw_random/core.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c index 9203f2d..ec6846b 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,6 +557,17 @@ EXPORT_SYMBOL_GPL(devm_hwrng_unregister); static int __init hwrng_modinit(void) { + /* 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; + } + return register_miscdev(); }