From patchwork Fri Oct 27 17:04:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: PrasannaKumar Muralidharan X-Patchwork-Id: 10030367 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 0481E6022E for ; Fri, 27 Oct 2017 17:05:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D4D9728E23 for ; Fri, 27 Oct 2017 17:05:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C86C628E2B; Fri, 27 Oct 2017 17:05:11 +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.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 7269B28E23 for ; Fri, 27 Oct 2017 17:05:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751721AbdJ0RFK (ORCPT ); Fri, 27 Oct 2017 13:05:10 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:54421 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751614AbdJ0RFK (ORCPT ); Fri, 27 Oct 2017 13:05:10 -0400 Received: by mail-pf0-f193.google.com with SMTP id n89so5358806pfk.11 for ; Fri, 27 Oct 2017 10:05:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=F0vueFwg1PfD5WdkhrJIHGTDXJdzR6fmAd59U08I5yw=; b=UDSaNy2qrM+mdnUgKgToBMy3SL/uuo+3k0sE9RcEyXyJe1hf8DM3A0Ob6/d4q0mBCP 4n7W79K3xUVUm9Ra30ubuYyHkH87MNiXhJdN3ooL4T9nByNiRTTyBc2rBZw/4sU8Op/s 6RUgtxY4WAJ7t3Kxe1j5XNMedK1hn1HjqvuSvjSozPPFBF2wi13Je8sy7xagy0fRMjBG e5urfxHaBqSzriljJPjH919Jz1V622EHYU6qsp/oa6ZFagxLhKEd7y0OWN/QZnMK0uHZ og6Efecjcmb1uKiEWr1CRUL8sqoGf8iWFSQQIgfzV2N2+uN8ba28BKDSIWCKT39YGotO /E5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=F0vueFwg1PfD5WdkhrJIHGTDXJdzR6fmAd59U08I5yw=; b=L4OFuSv05kHNVT5oGgL5J02IopKkjA2yZthj4htKAtHJ+vC4jz9UAGd/oTqi7dc58E RbTxJylHrO2q+pkhtHR5Zy3TTHjx/dPRfnLBcV5k/tDhYibaWJCbuVj+G88eNOJ41CWs 5H5IwfPTYsDqXS0Xy1Q6M/7CUJi3VLWgu8v+94tZboRdEzLtCF+nLvM3BY3+W4SExGKA UEAf0A89tR5QWu/PY8yJHwQ3O0hcUP+m9MJEmjx4AebjgcJDnXo5cOwCAheue9fBIBmf 35rdX/78xSA+zVMwn64qom1BHcmSvNNiUSFm1NbqtnHHdDYIYSqg9VF73rW6uRg9Jh71 OgXQ== X-Gm-Message-State: AMCzsaVWB+ClqX0KdT5hzhvHLm7DuxNAEOoZcrRLgBNeUBt97KyxnxwL 65dion+bWnC59ytEU22gZVU= X-Google-Smtp-Source: ABhQp+TkMMYuXQwts+hLmsAlMDkIczAUNLgp7rA52fJmaWgYhrl4uMUOdFgywHNO+5d5+NBn5jLzsQ== X-Received: by 10.84.254.79 with SMTP id a15mr865097pln.413.1509123909468; Fri, 27 Oct 2017 10:05:09 -0700 (PDT) Received: from linux.local ([43.224.131.43]) by smtp.gmail.com with ESMTPSA id l22sm16864204pfk.45.2017.10.27.10.05.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 27 Oct 2017 10:05:08 -0700 (PDT) From: PrasannaKumar Muralidharan To: mpm@selenic.com, herbert@gondor.apana.org.au, freude@linux.vnet.ibm.com, linux-crypto@vger.kernel.org Cc: PrasannaKumar Muralidharan Subject: [PATCH] hw_random: core: Reset user selected rng by writing "" to rng_current Date: Fri, 27 Oct 2017 22:34:04 +0530 Message-Id: <20171027170404.21262-1-prasannatsmkumar@gmail.com> X-Mailer: git-send-email 2.10.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 User is able to select a chosen rng by writing its name to rng_current but there is no way to reset it without unbinding the rng. Let user write "" to rng_current and delesect the chosen rng. Signed-off-by: PrasannaKumar Muralidharan reviewed-by: Harald Freudenberger --- drivers/char/hw_random/core.c | 51 +++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c index 9701ac7..be03024 100644 --- a/drivers/char/hw_random/core.c +++ b/drivers/char/hw_random/core.c @@ -292,26 +292,48 @@ static struct miscdevice rng_miscdev = { .groups = rng_dev_groups, }; +static int enable_best_rng(void) +{ + int ret = -ENODEV; + + BUG_ON(!mutex_is_locked(&rng_mutex)); + + /* rng_list is sorted by quality, use the best (=first) one */ + if (!list_empty(&rng_list)) { + struct hwrng *new_rng; + + new_rng = list_entry(rng_list.next, struct hwrng, list); + ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng)); + if (!ret) + cur_rng_set_by_user = 0; + } + + return ret; +} + static ssize_t hwrng_attr_current_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { - int err; + int err = -ENODEV; struct hwrng *rng; err = mutex_lock_interruptible(&rng_mutex); if (err) return -ERESTARTSYS; - err = -ENODEV; - list_for_each_entry(rng, &rng_list, list) { - if (sysfs_streq(rng->name, buf)) { - err = 0; - cur_rng_set_by_user = 1; - if (rng != current_rng) + + if (sysfs_streq(buf, "")) { + err = enable_best_rng(); + } else { + list_for_each_entry(rng, &rng_list, list) { + if (sysfs_streq(rng->name, buf)) { + cur_rng_set_by_user = 1; err = set_current_rng(rng); - break; + break; + } } } + mutex_unlock(&rng_mutex); return err ? : len; @@ -493,17 +515,8 @@ void hwrng_unregister(struct hwrng *rng) mutex_lock(&rng_mutex); list_del(&rng->list); - if (current_rng == rng) { - drop_current_rng(); - cur_rng_set_by_user = 0; - /* rng_list is sorted by quality, use the best (=first) one */ - if (!list_empty(&rng_list)) { - struct hwrng *new_rng; - - new_rng = list_entry(rng_list.next, struct hwrng, list); - set_current_rng(new_rng); - } - } + if (current_rng == rng) + enable_best_rng(); if (list_empty(&rng_list)) { mutex_unlock(&rng_mutex);