From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B087ECAA25 for ; Thu, 25 Aug 2022 16:39:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242661AbiHYQjD (ORCPT ); Thu, 25 Aug 2022 12:39:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242363AbiHYQiz (ORCPT ); Thu, 25 Aug 2022 12:38:55 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F754BA14F; Thu, 25 Aug 2022 09:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661445535; x=1692981535; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=liGkXhlAm+VXFh8bREExCiBE1UTrJqRblYdv6sZW+iA=; b=Agz2JhW31MrmRDkWVWiNtMxFjltjQTz8+fqte9tu+6VugEgLn5XAUePi dLLMlKeuUsoMgn1TydzNw8nfMR65Ep5ryChU5nNOVcLDtD8Ct52MlmyqR F+d7ZDfrQalVHdreBtKDclnA9lGYG9FM5EDml7gpmh5k3kkihhw2yVXbZ ZrFz3aP0nwQ5P4VO1hHm1WzqKBWjZoudZuKGdOufXgnxTVhRiS4ehswaf z0b2tt6l7vbWRFfibWLx3Vo3OSXbNVCqhwd+++KCDUM8Jjk3ZSNJrZrgF RBCTCnqwAnsbUqDpt+9BoEeOhZtu0/xXhF+YsTgYmqq1npSnLGCC7AcCi g==; X-IronPort-AV: E=McAfee;i="6500,9779,10450"; a="320375166" X-IronPort-AV: E=Sophos;i="5.93,263,1654585200"; d="scan'208";a="320375166" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Aug 2022 09:38:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,263,1654585200"; d="scan'208";a="671070433" Received: from crojewsk-ctrl.igk.intel.com ([10.102.9.28]) by fmsmga008.fm.intel.com with ESMTP; 25 Aug 2022 09:38:51 -0700 From: Cezary Rojewski To: alsa-devel@alsa-project.org, broonie@kernel.org Cc: tiwai@suse.com, perex@perex.cz, amadeuszx.slawinski@linux.intel.com, pierre-louis.bossart@linux.intel.com, hdegoede@redhat.com, lgirdwood@gmail.com, kai.vehmanen@linux.intel.com, peter.ujfalusi@linux.intel.com, ranjani.sridharan@linux.intel.com, yung-chuan.liao@linux.intel.com, viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, andy.shevchenko@gmail.com, Cezary Rojewski Subject: [PATCH v2 1/2] libfs: Introduce tokenize_user_input() Date: Thu, 25 Aug 2022 18:48:32 +0200 Message-Id: <20220825164833.3923454-2-cezary.rojewski@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220825164833.3923454-1-cezary.rojewski@intel.com> References: <20220825164833.3923454-1-cezary.rojewski@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add new helper function to allow for splitting specified user string into a sequence of integers. Internally it makes use of get_options() so the returned sequence contains the integers extracted plus an additional element that begins the sequence and specifies the integers count. Suggested-by: Andy Shevchenko Signed-off-by: Cezary Rojewski --- fs/libfs.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 46 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index 31b0ddf01c31..078b23e26741 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -809,6 +809,51 @@ ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, } EXPORT_SYMBOL(simple_write_to_buffer); +/** + * tokenize_user_input - Split string into a sequence of integers + * @from: The user space buffer to read from + * @ppos: The current position in the buffer + * @count: The maximum number of bytes to read + * @tkns: Returned pointer to sequence of integers + * + * On success @tkns is allocated and initialized with a sequence of + * integers extracted from the @from plus an additional element that + * begins the sequence and specifies the integers count. + * + * Caller takes responsibility for freeing @tkns when it is no longer + * needed. + */ +int tokenize_user_input(const char __user *from, size_t count, int **tkns) +{ + int *ints, nints; + char *buf; + int ret = 0; + + buf = memdup_user_nul(from, count); + if (IS_ERR(buf)) + return PTR_ERR(buf); + + get_options(buf, 0, &nints); + if (!nints) { + ret = -ENOENT; + goto free_buf; + } + + ints = kcalloc(nints + 1, sizeof(*ints), GFP_KERNEL); + if (!ints) { + ret = -ENOMEM; + goto free_buf; + } + + get_options(buf, nints + 1, ints); + *tkns = ints; + +free_buf: + kfree(buf); + return ret; +} +EXPORT_SYMBOL(tokenize_user_input); + /** * memory_read_from_buffer - copy data from the buffer * @to: the kernel space buffer to read to diff --git a/include/linux/fs.h b/include/linux/fs.h index 9eced4cc286e..ab04cc7f9efa 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3345,6 +3345,7 @@ extern ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, const void *from, size_t available); extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, const void __user *from, size_t count); +extern int tokenize_user_input(const char __user *from, size_t count, int **tkns); extern int __generic_file_fsync(struct file *, loff_t, loff_t, int); extern int generic_file_fsync(struct file *, loff_t, loff_t, int); -- 2.25.1