Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3289425yba; Mon, 8 Apr 2019 15:32:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqzrzZIp/j2TNOOY7kHKvvP7Z9dOLj7FXFS7puJDmsUNEOyvvvQFqSQcS14nWKp6nNBrFm0I X-Received: by 2002:a63:3190:: with SMTP id x138mr29946638pgx.273.1554762736016; Mon, 08 Apr 2019 15:32:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554762736; cv=none; d=google.com; s=arc-20160816; b=EIHIsLZ+hvlcwf1ZLYv4eivBbkR8+ubq4Rd8cVUlvva7iOytcEc02y81P00qjj9S/+ R2dgy1ADeot7Y7KSfT+jIPYHA3mdoLBY/8VPoVUR4zTbooIbLNdwePicOkfCnycuaibE mA3sNS3nWXpCA3qML4Gp7DW2V04ob5+i08ZNndpCeU+FSCuons1vTNUvumqIlDziA5Vv gKMmI6tYutTACw4nl/REaA7FBcVAuf4nbTgErzQSctOIcUVyE0j5n4YC/8zd6e9HARFA SjFP7R9RaH1Ws/B7sk8bXaIaWB559OhFexufWozr+BpgtYeABGKkCplr6RRAQXnxJcC4 fDgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:smtp-origin-cluster:cc:to :smtp-origin-hostname:from:smtp-origin-hostprefix:dkim-signature; bh=PeE9Rwo7wGOWQ5q9OAqfNdhcUhps+PHukzw37s+pWWU=; b=LVlO2cdfrsUGIYnsMjS6TL7fFn0XvjLXAte2luHtnsNpLlAhahuJLPwZU51k+h80XN MZqEOUdfA39T3JbHx41Y/k433MS/irjZi4s3eC1odAX00ExB237oTke+xMcry4u7Jm8u KyC9aaf7o2CsUCR2nkGPAdGQkXGE5H9hi2OG6nyPLuQbK8VZbd69AHEj/WIH2yA6/+hn aeeWFmcvN8kh6iFkaaL02JhGePW4F6tSx8HbBZdrBI6wr92QvbF+OThUf63CNgmOG7wu XnJiQ1nTr6IxIu+gVUgEtAs/4HP/ooPyeTmhUZUnTr3x8GfMdLwqS99zfzLYLahn9QMO HY3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=MhNsOP+M; 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=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h97si27744695plb.302.2019.04.08.15.32.01; Mon, 08 Apr 2019 15:32:15 -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=@fb.com header.s=facebook header.b=MhNsOP+M; 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=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727064AbfDHVrL (ORCPT + 99 others); Mon, 8 Apr 2019 17:47:11 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:52624 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726694AbfDHVrK (ORCPT ); Mon, 8 Apr 2019 17:47:10 -0400 Received: from pps.filterd (m0001255.ppops.net [127.0.0.1]) by mx0b-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x38LWlj3004870 for ; Mon, 8 Apr 2019 14:47:09 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=PeE9Rwo7wGOWQ5q9OAqfNdhcUhps+PHukzw37s+pWWU=; b=MhNsOP+MRfkIrNIaqiAHzdp0fdKp6DRltSzGP2He0iJnV9QGWfHatu+4xGZBL6Mi+beM gg6zzRafbnNPUCUg/lfKa3/BvfIYL9UKl91oXdYV9n4uhgMe38zi64c/qqK5GLC/fQ2J B1wPlZ6idY8iKtHEpc/Umtl5ZaZmTdA4nP8= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 2rra0ph85b-10 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 08 Apr 2019 14:47:09 -0700 Received: from mx-out.facebook.com (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::127) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1713.5; Mon, 8 Apr 2019 14:46:07 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 94CAF62E1F66; Mon, 8 Apr 2019 14:46:02 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig006.ftw2.facebook.com To: , CC: , , , , , , Song Liu Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH 3/7] cgroup: introduce cgroup_parse_percentage Date: Mon, 8 Apr 2019 14:45:35 -0700 Message-ID: <20190408214539.2705660-4-songliubraving@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190408214539.2705660-1-songliubraving@fb.com> References: <20190408214539.2705660-1-songliubraving@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-04-08_09:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch introduces a helper to parse percentage string to long integer, with user selected scale: long cgroup_parse_percentage(char *tok, unsigned long base) Valid tok could be integer 0 to 100, decimal 0.00 to 100.00, or "max". A tok of "max"is same as "100". Base is the desire output scale for input "1". Signed-off-by: Song Liu --- include/linux/cgroup.h | 1 + kernel/cgroup/cgroup.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 81f58b4a5418..b28f8a41c970 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -110,6 +110,7 @@ int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts); int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts); int cgroup_rm_cftypes(struct cftype *cfts); void cgroup_file_notify(struct cgroup_file *cfile); +long cgroup_parse_percentage(char *tok, unsigned long base); int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen); int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry); diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index b0df96132476..2e48840ff613 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -3967,6 +3967,43 @@ void cgroup_file_notify(struct cgroup_file *cfile) spin_unlock_irqrestore(&cgroup_file_kn_lock, flags); } +/** + * cgroup_parse_percentage - parse percentage number + * @tok: input string contains the token. Valid values are "00.00" to + * "100.00", or "max" + * @base: number "1" in desired output scale + * + * Returns: + * @base * 100 for "max"; + * @base * <0.00 to 100.00> for valid inputs; + * -EINVAL for invalid input. + */ +long cgroup_parse_percentage(char *tok, unsigned long base) +{ + unsigned long val_int, val_frag; + + if (strcmp(tok, "max") == 0) { + return base * 100; + } else if (sscanf(tok, "%lu.%02lu", &val_int, &val_frag) == 2) { + /* xx.1 yields val_frag = 1, while it should be 10 */ + if (val_frag < 10 && strstr(tok, ".0") == NULL) + val_frag *= 10; + goto calculate_output; + } else if (sscanf(tok, "%lu", &val_int) == 1) { + val_frag = 0; + goto calculate_output; + } else { + return -EINVAL; + } + +calculate_output: + if (val_int > 100 || (val_int == 100 && val_frag > 0)) + return -EINVAL; + + /* round up val_frag by 0.5, to avoid repeated rounding down */ + return (val_int * base) + div_u64((val_frag * 10 + 5) * base, 1000); +} + /** * css_next_child - find the next child of a given css * @pos: the current position (%NULL to initiate traversal) -- 2.17.1