From patchwork Thu Mar 2 12:02:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TWljaGHFgiBLxJlwaWXFhA==?= X-Patchwork-Id: 9600161 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 8FF0F60453 for ; Thu, 2 Mar 2017 12:39:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7D557285AA for ; Thu, 2 Mar 2017 12:39:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6FBEF285B6; Thu, 2 Mar 2017 12:39:45 +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.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, 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 A36C6285AA for ; Thu, 2 Mar 2017 12:39:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751628AbdCBMjo (ORCPT ); Thu, 2 Mar 2017 07:39:44 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:35536 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751274AbdCBMjl (ORCPT ); Thu, 2 Mar 2017 07:39:41 -0500 Received: by mail-lf0-f65.google.com with SMTP id z127so5628811lfa.2 for ; Thu, 02 Mar 2017 04:38:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kempniu.pl; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=v24up40PQpSsTE9FOPcWH7FBCIMYTzTRo769wD0p+os=; b=QAHGjvS2V9DH4ZHZc4pQ/CYVc+/0jjhStX4nFp6EB4TwWQnKVwilaInnMmUk8OPVFT dy+nVh2VKJaAYx7qy7QWMEfANvmQO6j2K2gvDImrpxJVq00BvkSKEcAR6Qps/lEmOHdO u71bd9qMLwWN7afuhY1jLdgqCfbaG7NT98tss= 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:mime-version :content-transfer-encoding; bh=v24up40PQpSsTE9FOPcWH7FBCIMYTzTRo769wD0p+os=; b=W0B5iCHhpBpAUPdvs57/Y0LKMHydlQxJmxcSMnrKGE9cIGp55Av9nulLzNz0Z/qAGF ha6/FdlQnCw0eWngTaTLMFTw61nbIh1KPFSQAKBzfImrFHRNgtk8pNTu7p0xaYyrdIUe Xxlhg5nQfDYz3pWNMVijx14CqF6Wt6H4QsDYSPgmbgpP9B8AOOxI5iVjQCnbIUaB12fH wk3tDm13L8UEVJBvPP8DgLGmh5Enf1Xe5s1Rq6WjuNVrySkN7TwykNBtBly0r7hIfHgi EWeQxalUaBE+UBl6eCMr25R8Nrur5nabE9BpPnT0CYB98FfqFBoJgAvkDI2mWQ0HwpIf z/Pw== X-Gm-Message-State: AMke39lrQtCLtvMBZRuDAE0ceRwHAnQ4mMp9zRVw0uYXqkNyCxRQJMLqApOziiQu306Y2w== X-Received: by 10.46.75.1 with SMTP id y1mr4860321lja.75.1488456168195; Thu, 02 Mar 2017 04:02:48 -0800 (PST) Received: from ozzy.nask.waw.pl ([2001:a10:160:3::3]) by smtp.googlemail.com with ESMTPSA id h23sm1721804lji.34.2017.03.02.04.02.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Mar 2017 04:02:47 -0800 (PST) From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= To: Dmitry Torokhov Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] Input: sparse-keymap - use a managed allocation for keymap copy Date: Thu, 2 Mar 2017 13:02:42 +0100 Message-Id: <20170302120242.6342-1-kernel@kempniu.pl> X-Mailer: git-send-email 2.12.0 MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Some platform drivers use devm_input_allocate_device() together with sparse_keymap_setup() in their .probe callbacks. While using the former simplifies error handling, using the latter necessitates calling sparse_keymap_free() in the error path and upon module unloading to avoid leaking the copy of the keymap allocated by sparse_keymap_setup(). To help prevent such leaks and enable simpler error handling, make sparse_keymap_setup() use devm_kcalloc() to allocate memory for the keymap copy so that it gets automatically freed. This works for both managed and non-managed input devices, although both are handled a bit differently. Specifically, the managed keymap copy is attached to a different struct device: - for managed input devices, as all other managed resources are attached to the device owning the input device, we do the same to ensure freeing the keymap copy is properly slotted in the devres stack, - for non-managed input devices, the managed keymap copy is attached to the struct device embedded inside the input device itself, so that the keymap is freed after the last reference to the input device is dropped, i.e. after input_unregister_device() is called. Note that actions previously taken by sparse_keymap_free(), i.e. taking the input device's mutex and zeroing its keycode and keycodemax fields, are now redundant because the managed keymap will always be freed after the input device is unregistered. Signed-off-by: Michał Kępień --- Changes from v1: - Do not add a new function. Instead, make sparse_keymap_setup() always use a managed allocation, which allows making sparse_keymap_free() a noop and simplifies error handling. - Update subject, commit message and comments accordingly. drivers/input/sparse-keymap.c | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/drivers/input/sparse-keymap.c b/drivers/input/sparse-keymap.c index e7409c45bdd0..fd391c339b4e 100644 --- a/drivers/input/sparse-keymap.c +++ b/drivers/input/sparse-keymap.c @@ -160,12 +160,12 @@ static int sparse_keymap_setkeycode(struct input_dev *dev, * @keymap: Keymap in form of array of &key_entry structures ending * with %KE_END type entry * @setup: Function that can be used to adjust keymap entries - * depending on device's deeds, may be %NULL + * depending on device's needs, may be %NULL * * The function calculates size and allocates copy of the original * keymap after which sets up input device event bits appropriately. - * Before destroying input device allocated keymap should be freed - * with a call to sparse_keymap_free(). + * The allocated copy of the keymap is automatically freed when it + * is no longer needed. */ int sparse_keymap_setup(struct input_dev *dev, const struct key_entry *keymap, @@ -180,7 +180,8 @@ int sparse_keymap_setup(struct input_dev *dev, for (e = keymap; e->type != KE_END; e++) map_size++; - map = kcalloc(map_size, sizeof(struct key_entry), GFP_KERNEL); + map = devm_kcalloc(dev->devres_managed ? dev->dev.parent : &dev->dev, + map_size, sizeof(struct key_entry), GFP_KERNEL); if (!map) return -ENOMEM; @@ -192,7 +193,7 @@ int sparse_keymap_setup(struct input_dev *dev, if (setup) { error = setup(dev, entry); if (error) - goto err_out; + return error; } switch (entry->type) { @@ -221,10 +222,6 @@ int sparse_keymap_setup(struct input_dev *dev, dev->setkeycode = sparse_keymap_setkeycode; return 0; - - err_out: - kfree(map); - return error; } EXPORT_SYMBOL(sparse_keymap_setup); @@ -232,29 +229,13 @@ EXPORT_SYMBOL(sparse_keymap_setup); * sparse_keymap_free - free memory allocated for sparse keymap * @dev: Input device using sparse keymap * - * This function is used to free memory allocated by sparse keymap + * This function used to free memory allocated by sparse keymap * in an input device that was set up by sparse_keymap_setup(). - * NOTE: It is safe to cal this function while input device is - * still registered (however the drivers should care not to try to - * use freed keymap and thus have to shut off interrupts/polling - * before freeing the keymap). + * Since sparse_keymap_setup() now uses a managed allocation for the + * keymap copy, use of this function is deprecated. */ void sparse_keymap_free(struct input_dev *dev) { - unsigned long flags; - - /* - * Take event lock to prevent racing with input_get_keycode() - * and input_set_keycode() if we are called while input device - * is still registered. - */ - spin_lock_irqsave(&dev->event_lock, flags); - - kfree(dev->keycode); - dev->keycode = NULL; - dev->keycodemax = 0; - - spin_unlock_irqrestore(&dev->event_lock, flags); } EXPORT_SYMBOL(sparse_keymap_free);