From patchwork Wed Jul 12 23:01:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haitao Huang X-Patchwork-Id: 119424 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp1482598vqm; Wed, 12 Jul 2023 16:39:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlFklh2hiMN2K7PBeOSsJPC06CfBAPBf26fA0TeNdhlV0KltFJTOuhSkL3unf3FHFa2ENl/8 X-Received: by 2002:aa7:8888:0:b0:682:e24c:f4e0 with SMTP id z8-20020aa78888000000b00682e24cf4e0mr178115pfe.11.1689205143187; Wed, 12 Jul 2023 16:39:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689205143; cv=none; d=google.com; s=arc-20160816; b=QM9hlS6Ie3UpiyWxlaxQwKsjY7mGm4XLovpiCLop6/zpZa8G+zDk0vE4sRT2BOVQKH GCBwomprXfK1LD6UJG1gNNyP+jWBmI22M77aRpZaTzhWUHYb7FObxd/w2dF6ZdZmhbjl 0cpMAmJmWWx2InFeGqfYAszZvZeVx6dxg+3ruQXSZpQuy+1qZo9Th+utY4dXVqR/hjAp pkMLRZwCLM79HCS1PQ9/yS4GmqNTxLk3c0xwhIIqm9NquMDec1JD/vM+iWYW5AOibkEp KfMCfHyPjd1n8k+YfEdTllpgXHWt+Q+nNl6K2kbgs+RPzvN9GrhRjmuP+Dd1I8Ttg9if 9MDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=v4ggJB3AVNTsmO+J3q+FuFJ2ApDbBY0FRfH4hKv83PA=; fh=OZr4LIl5No2JYFQArlJ8xitR5GErnb3n5yX+P8GvfUY=; b=ZZyQAJvOc/MYGiF0SZ8Qe611nWtCyGourh6qstp5ecTRLM0feFWk4jsnT1gU/1uxVT KaM0aG7k0RVXtpV8f9iBL0yVYmCf33OBbpGECTToHj8cQH57hFiJKb8Y38AAorf3EsZ8 PzfamkQqNUCEp0F/iVqxfFFTyMhPTDJGqqGvaG7NBAuBu6UduYMAgUcCvS17cdWSjFV8 96YJwOjGN9mAQIN1gwn/kW8xRwtkTo27v2AwJUmSsoHgv86HE2Oj8ZT6TDzcDKGwpcpj ZYtDfFO7/oxna0C1oCc9gloUkoCJKNWeH/LRGUpf5ZzCb1W2gABLzPcwGOtwdh6wIA+e cX8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="LU/fwLB0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g9-20020a056a001a0900b0068035bb7a40si4154238pfv.366.2023.07.12.16.38.50; Wed, 12 Jul 2023 16:39:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="LU/fwLB0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232721AbjGLXDV (ORCPT + 99 others); Wed, 12 Jul 2023 19:03:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232732AbjGLXCS (ORCPT ); Wed, 12 Jul 2023 19:02:18 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FB2418E; Wed, 12 Jul 2023 16:02:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689202937; x=1720738937; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gtPme0IuXCAV4jbp0xb73aBMQFtTiscdTlcYYwDyGg8=; b=LU/fwLB0h47yZrs3fFKLbojxHgRpYrQ6iIvzDpxHqSnhwm5C1Bb8lLs3 ZZ6eclaF+n5kv3WCCBcwpdZ6Bbk/iqdD9EfLa77GTJO+z9mNYI+HXbDjw Y85VOTZTDQegwUXEfgzDNOdIbhgoO/gOSGk9uKinxNZv1aKMEMqUMzaWh hGKMsgb9vB9RX1YA2Nsepz7A34ZIoWfQXbWMU+jLkSuXjDid1mudOwH+y JVcIWZvhc9ffHBl1yLv1/kt7JjkPJ6DpegqdUBLCCM/KmrxWFJNudHqC9 o+PNaBrus11gnGpxuGjD4b4J7cOq/j/q9dX6kGwTfYHJNt7hAV1lC+qxO A==; X-IronPort-AV: E=McAfee;i="6600,9927,10769"; a="428774166" X-IronPort-AV: E=Sophos;i="6.01,200,1684825200"; d="scan'208";a="428774166" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 16:02:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10769"; a="835338649" X-IronPort-AV: E=Sophos;i="6.01,200,1684825200"; d="scan'208";a="835338649" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by fmsmga002.fm.intel.com with ESMTP; 12 Jul 2023 16:02:15 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, cgroups@vger.kernel.org, Zefan Li , Johannes Weiner Cc: kai.huang@intel.com, reinette.chatre@intel.com, zhiquan1.li@intel.com, kristen@linux.intel.com Subject: [PATCH v3 18/28] cgroup/misc: Fix an overflow Date: Wed, 12 Jul 2023 16:01:52 -0700 Message-Id: <20230712230202.47929-19-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230712230202.47929-1-haitao.huang@linux.intel.com> References: <20230712230202.47929-1-haitao.huang@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771259972295516320 X-GMAIL-MSGID: 1771259972295516320 Overflow may happen in misc_cg_try_charge if new_usage becomes above INT_MAX, for example, on platforms with large SGX EPC sizes. Change type of new_usage to long from int and check overflow. Signed-off-by: Haitao Huang --- kernel/cgroup/misc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c index fe3e8a0eb7ed..ff9f900981a3 100644 --- a/kernel/cgroup/misc.c +++ b/kernel/cgroup/misc.c @@ -143,7 +143,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, struct misc_cg *i, *j; int ret; struct misc_res *res; - int new_usage; + long new_usage; if (!(valid_type(type) && cg && READ_ONCE(misc_res_capacity[type]))) return -EINVAL; @@ -153,10 +153,10 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, for (i = cg; i; i = parent_misc(i)) { res = &i->res[type]; - new_usage = atomic_long_add_return(amount, &res->usage); if (new_usage > READ_ONCE(res->max) || - new_usage > READ_ONCE(misc_res_capacity[type])) { + new_usage > READ_ONCE(misc_res_capacity[type]) || + new_usage < 0) { ret = -EBUSY; goto err_charge; }