Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp3621327img; Mon, 25 Mar 2019 14:10:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqxTcsSxwUwuskniQ6FJuar7t/vXc9EG33WyYoCL5Q0akywl8WiGShPPt4cCGOde1HWssC82 X-Received: by 2002:a62:12d0:: with SMTP id 77mr26277391pfs.15.1553548247274; Mon, 25 Mar 2019 14:10:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553548247; cv=none; d=google.com; s=arc-20160816; b=t1hdE6IIu53ogvh1tDbw7SZGpFLAq33IYHz7DW67L6PjCy8iw5uuE9JQiB7NUAYacl q/W/pCOmdvkL/fRxQ4jiI/K/KlgovaavUeQnBzovCaxu/iDcZFxQGKjhnDPzzYNkQKlt s8j+Jx3Swv+87HKKLIyTLIv9eNZjdYVUr9/DnH+UKmc3reNqEa5HUFqe/LOBCvLjjUD8 uof51f3Ua4I6qcmb+Nrgr6n9fv1sL4MwFQ56SsFejacMczk5J6M10D68zBWK99vRLcVc Yu6nv1ZQ7+PPElnqJdQrXRq/IrcotIU2vsg2g/l3Mlvl31xwLQOcyaFmgiplzT/Hx8yk 9q6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=GY0lBb62Z1iVATnV+m0gDyVgNs1C49ML0/XutpXLjs8=; b=MgL517yZ8oZ4qrdYxkZ/VSO9U1GkExsDuyJ5UJKu7nGjYFaF2eVhJNvkNYA/bRlRc7 /W1LU0rfF28AnxxWwI6VABBE9cOi+z9HNRe1C0T+JPrmIgsk7gwFi3C7VVZy76mNW2I+ CUoiNtRzgUz/MXlUaJlrumbyhWAZCLfM82MAiYfY4/oeWSjcFsRGkdDJ2folFOUMbNRY mFpjw/yWfKDE8XFSQllZgK6NWmUIyarIoi1A3A5Ts3M0YgH2hptrAoGx53zi+kelZqiy 6WB4FWCcXOf+0838RGvcy5rfnPvW21YMTqrmpSMjoz6hrBX2PVs+AmtFBg+BBAle1Myo /mqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=mmdTLg9U; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u20si14250946pga.71.2019.03.25.14.10.32; Mon, 25 Mar 2019 14:10:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=mmdTLg9U; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730371AbfCYVH5 (ORCPT + 99 others); Mon, 25 Mar 2019 17:07:57 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:34866 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729283AbfCYVH4 (ORCPT ); Mon, 25 Mar 2019 17:07:56 -0400 Received: by mail-pf1-f193.google.com with SMTP id t21so7035944pfe.2 for ; Mon, 25 Mar 2019 14:07:55 -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:in-reply-to:references; bh=GY0lBb62Z1iVATnV+m0gDyVgNs1C49ML0/XutpXLjs8=; b=mmdTLg9UiX/N1A5ZaqEEgsK4Z+pylWjrHAqTYY0Yz+5sCVRt1c7OLAKMckLP31JHy5 2WMASsVga6fcW7xwLKwo5drPqZQFb7REtIynrATTdtxTChHzOc1k9SUsC7hI1xQY3vgO zE32VGYX+zqRgdY2VA2mrBElNnmoBysvFUXQ+j/vI9nMc6GhTH3yRt3DGoKqqmGRe1K2 8LQyQ5MCQUw8EdGoiQrG527Tr+mikurisn/Ewvnleu6SQP9Cx5fC95BKvCHVQWcqHBWu Dh54vBKWSnbEYxtrQ/CI9qaSCKei7LY7ZKH1yJNmDN+6iFyRV43DsNZAxHL9PyFnYVac rS3g== 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:in-reply-to :references; bh=GY0lBb62Z1iVATnV+m0gDyVgNs1C49ML0/XutpXLjs8=; b=tTw5bfjwj0VeodvoJYUV/62L+MEa6y3WgRNn6tobqRnxKrT8m2DbhYUWUC7r54qZj1 28wS72DXtlw/lO03SmJKOl8nNizFc6w88pLNdzKDkkUFY95Z+7h+/3RHsejCw9mbJIaf QuEgQj11eTgmrzWGV3jFHbHZr3DV+gecYToUXGJjE+fEpwsAEUp3PUjkQN4QprjzZ9O2 aQXLwbGtvASqMfI55hXwf1T7Tqz2GLKcfhylsmZXAV1vvt/jfCyuy25M7NHbQE/cwRre dOtpbH6qUdb5khit6lstJjsbdaJ7P3CcLoqyEWJWNJ5hAQ9pNCf/NwIsUCd+XlZ0UaET /SLw== X-Gm-Message-State: APjAAAW7LVY7CLs6zviDfuXq9YnRhEkHdK5ztWlmcKL7WVuHsMwnAfZ2 LGi5VIHQlOBel4BvgcCmXG4= X-Received: by 2002:a63:4620:: with SMTP id t32mr23519881pga.363.1553548075505; Mon, 25 Mar 2019 14:07:55 -0700 (PDT) Received: from localhost ([169.229.22.206]) by smtp.gmail.com with ESMTPSA id i79sm35719109pfj.28.2019.03.25.14.07.54 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 25 Mar 2019 14:07:54 -0700 (PDT) From: Yury Norov X-Google-Original-From: Yury Norov To: Andrew Morton , Andy Shevchenko , Rasmus Villemoes , Arnd Bergmann , Kees Cook , Matthew Wilcox , Tetsuo Handa Cc: Yury Norov , Yury Norov , linux-kernel@vger.kernel.org Subject: [PATCH 2/6] bitmap_parselist: move non-parser logic to helpers Date: Tue, 26 Mar 2019 00:07:44 +0300 Message-Id: <20190325210748.6571-3-ynorov@marvell.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190325210748.6571-1-ynorov@marvell.com> References: <20190325210748.6571-1-ynorov@marvell.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Move region checking and setting functionality of __bitmap_parselist() to helpers. Signed-off-by: Yury Norov --- lib/bitmap.c | 64 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/lib/bitmap.c b/lib/bitmap.c index ad1fb7e6ad0e..307a1b20bead 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -477,6 +477,42 @@ int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, } EXPORT_SYMBOL(bitmap_print_to_pagebuf); +/* + * Region 9-38:4/10 describes the following bitmap structure: + * 0 9 12 18 38 + * .........****......****......****...... + * ^ ^ ^ ^ + * start off grlen end + */ +struct region { + unsigned int start; + unsigned int off; + unsigned int grlen; + unsigned int end; +}; + +static int bitmap_set_region(const struct region *r, + unsigned long *bitmap, int nbits) +{ + unsigned int start; + + if (r->end >= nbits) + return -ERANGE; + + for (start = r->start; start <= r->end; start += r->grlen) + bitmap_set(bitmap, start, min(r->end - start + 1, r->off)); + + return 0; +} + +static int bitmap_check_region(const struct region *r) +{ + if (r->start > r->end || r->grlen == 0 || r->off > r->grlen) + return -EINVAL; + + return 0; +} + /** * __bitmap_parselist - convert list format ASCII string to bitmap * @buf: read nul-terminated user string from this buffer @@ -507,10 +543,11 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen, int nmaskbits) { unsigned int a, b, old_a, old_b; - unsigned int group_size, used_size, off; + unsigned int group_size, used_size; int c, old_c, totaldigits, ndigits; const char __user __force *ubuf = (const char __user __force *)buf; - int at_start, in_range, in_partial_range; + int at_start, in_range, in_partial_range, ret; + struct region r; totaldigits = c = 0; old_a = old_b = 0; @@ -599,15 +636,20 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen, /* if no digit is after '-', it's wrong*/ if (at_start && in_range) return -EINVAL; - if (!(a <= b) || group_size == 0 || !(used_size <= group_size)) - return -EINVAL; - if (b >= nmaskbits) - return -ERANGE; - while (a <= b) { - off = min(b - a + 1, used_size); - bitmap_set(maskp, a, off); - a += group_size; - } + + r.start = a; + r.off = used_size; + r.grlen = group_size; + r.end = b; + + ret = bitmap_check_region(&r); + if (ret) + return ret; + + ret = bitmap_set_region(&r, maskp, nmaskbits); + if (ret) + return ret; + } while (buflen && c == ','); return 0; } -- 2.17.1