From patchwork Thu Sep 5 17:24:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13792748 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAA8CCD6E74 for ; Thu, 5 Sep 2024 17:24:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06A6C6B0082; Thu, 5 Sep 2024 13:24:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F31F86B0096; Thu, 5 Sep 2024 13:24:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E218E6B0098; Thu, 5 Sep 2024 13:24:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C36596B0095 for ; Thu, 5 Sep 2024 13:24:13 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6FC9D12092C for ; Thu, 5 Sep 2024 17:24:13 +0000 (UTC) X-FDA: 82531357986.30.801D78D Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf03.hostedemail.com (Postfix) with ESMTP id E83F92000D for ; Thu, 5 Sep 2024 17:24:11 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ah7zBwWi; spf=pass (imf03.hostedemail.com: domain of sj@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725556954; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=yERm+2GhLD3Gbo5bDIvSxaiRbSuDyO3F04kiEtNueyc=; b=TPofZ946CuAq5tK3S41q3SEERt+c6sTooOMqbiFminHFeJF4qtmI2wdXNU+Yo5DeoI2TrP Q8yWH1W2H8f2EnQMUjGqzjBcAHP52zi2haj1qKlaIuIt86dgqHF5xOavpGvtJlgbSY0wJh BgYpguqxjL6y9hMZAg7dPmH7fkoTNLo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725556954; a=rsa-sha256; cv=none; b=nuxZG+vZ1TUfFbC4Qp/PsAIiOonP295VztcC9Seds+JQGjBkHBvVSxpiMz8DY8XQpPjjpO DjfcvZHeq4AcLI8jro2tQOoI8bzaD8g9XR+6F8wMCFSUQnj5bWPeKxrgumIn9SYghsl6Rv GF5X2rrqga0TyBqBiYyJwpSYu+5coe8= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ah7zBwWi; spf=pass (imf03.hostedemail.com: domain of sj@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id E8205A44E1E; Thu, 5 Sep 2024 17:24:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76494C4CEC6; Thu, 5 Sep 2024 17:24:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725557050; bh=qZcBj0EB8G0ORdOMeKcxDRY4297XFxFpbti5X8aE3Ng=; h=From:To:Cc:Subject:Date:From; b=ah7zBwWiGJ0NIp2n9juU/LpFhavcxTGPFb1NfC5DhDhAoxksbi7YROk8Ak7Kz/ufM XnSJH1Sy8EkaUblI3N/n01OShRkyv5/YMuQSpL2XpG4ziUd/0aXBOkY4j7PPNJ2GKO IMoB38EnGTrZ+ohqsG14MA8fYM9VWTyCf2qChOatOXakanv4fLze6xFl/kMRjx8WDi P8KjGXy6DJ2jO7vOSNsvAN/vm6fTiRRR8E22k4fFJlSe5oc+yDjuW1/YNfqeuF2H3Q 8kYeKdqzHQuquIhh2Srf6GyqIuRw2pYY5/i2HWwai1sDl193xChSbe2ikYsIWZA7my urWo/1Frt/H2Q== From: SeongJae Park To: Cc: SeongJae Park , Guenter Roeck , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH] mm/damon/core: avoid overflow in damon_feed_loop_next_input() Date: Thu, 5 Sep 2024 10:24:05 -0700 Message-Id: <20240905172405.46995-1-sj@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Stat-Signature: zwogtazywrqkip81bdff7phy574bgf5r X-Rspamd-Queue-Id: E83F92000D X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1725557051-537181 X-HE-Meta: U2FsdGVkX1873FByRZ0r2+nLa4KN9ct7CgFDMeO13IG6S4mpd+ZCVwC4Qf/V6/iYhjMGX+3iYNAmYk0ZO28R2dNwfZB6GiB+Tu74+5kOsxdsyKO8Q1o6yzpIJAKPgwlkCsFYlHc9xsPPipm+9CSrT+4YBVoYaQWNWKMkTm7uzl5fSXo3rKiqPOpi7ufKkAXXwKMZ3z+1V/i24pY7vQyFCdVfxPW0FvevPui5nyqHV/wHKXYDqu8agiSxQ4CHuTIzxZ8f0s04gIDPf3bwQ5NHeyPGn+gI/WOioIe4C83Zo3fAXEKqhvsIZUFv62DALcg8SMLHDz05vvuFtOWgZqrJkWY9bGWRJncK9j16VXBVNM+pQXhKCr1YNuxwq6NK9BDmmmDR3UVXH8Op5de1MpC/vATs5vWWXRCDUdXo2dYN5ZtGBUeML42W8xMVNe/FS0wM8/G+4oaCVqCh9r7qzJ8LMgCuMarqlT4emB8urzuZMWelVY31L+En7X/s+Ev5grOngCk888LXzol0xLeE71LLXKR3F//93UCBjiPmojFCc8geV1bEw4Fflo3u7/wC/bnes+TDJ+owUPOe/dFrchY+LBYc510opRSAK61Czo1JgusM8wKtTfAljo04IRM5QG9n5qKgOPwER4YPe42wHajGV7BI7xT6w3KcbCfRQCLUfL7o82DWe74PjRYRy06lPPTi/r+VZ8sn+AmGix1uV2KCIg/ZTZnKr3Ze2o9XtzGuowjIub5/jowaLSvZX81pOhKYtv4DYeEeEXpXGoIlfRcQkSmCmPayMQwZ7/ureqwoR+bN+O/LNluE/JrpGPjG6JMooBtYeig485R5v4rkv6Jp04pcDWgDkI3PcuoCr79nB6qb4jzrt7BHDs16Myqw9Td2JvLAwYWZpqHrGnZKM/xbjNGrtFJrLhm+8ZUnx9xSXLMKjRkWrNwWy8ai4DZmkuXvjAqWx3SEPXvkVWfkYPd K7Eln4lD nKanSjauWOFNj9mENQTKI8h1biyVIQOWe7T05LPTm+hPh6Mpr4xumIMeDC5ubgsbLeImxkc3cjm5cxmcm7zlv9UaDCdisuwoA++urXYzwCSyZEQ8cICMv8qrnXZmOSeW24AKNDMOZWjmQ5Tw6X+pDozVB/gt2sgSPQXZk7ihazEhOWextSJ8WZvHIU4FxNjAlqeSfd0svjsBMcKdVePbLqHLmfQUv0l6nekdKDpl84lnjeu84M5nURSxhQQojbnF3fz6+B7wq7YaJ7YjgyKH6dCh25+6s6oDDT+8+DlJ9cYsGI/21IN+vFDcyaZ0UDfzlyD60ezxacJwoIfU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: damon_feed_loop_next_input() is fragile to overflows. Rewrite code to avoid overflows. This is not yet well tested on 32bit archs. Reported-by: Guenter Roeck Closes: https://lore.kernel.org/944f3d5b-9177-48e7-8ec9-7f1331a3fea3@roeck-us.net Fixes: 9294a037c015 ("mm/damon/core: implement goal-oriented feedback-driven quota auto-tuning") Signed-off-by: SeongJae Park Tested-by: Guenter Roeck Tested-by: Guenter Roeck --- As mentioned on the commit message, this is not yet sufficiently tested on 32bit machines. That's why this is RFC. mm/damon/core.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index 32677f13f437..1d951c2a1d85 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -1494,15 +1494,36 @@ static unsigned long damon_feed_loop_next_input(unsigned long last_input, unsigned long score) { const unsigned long goal = 10000; - unsigned long score_goal_diff = max(goal, score) - min(goal, score); - unsigned long score_goal_diff_bp = score_goal_diff * 10000 / goal; - unsigned long compensation = last_input * score_goal_diff_bp / 10000; /* Set minimum input as 10000 to avoid compensation be zero */ const unsigned long min_input = 10000; + unsigned long score_goal_diff; + unsigned long compensation; + + if (score == goal) + return last_input; + + /* last_input, score <= ULONG_MAX */ + if (score < goal) { + score_goal_diff = goal - score; + } else { + /* if score_goal_diff > goal, will return min_input anyway */ + score_goal_diff = min(score - goal, goal); + } + + if (last_input < ULONG_MAX / score_goal_diff) + compensation = last_input * score_goal_diff / goal; + else + compensation = last_input / goal * score_goal_diff; + + /* compensation <= last_input <= ULONG_MAX */ + + if (goal > score) { + if (last_input < ULONG_MAX - compensation) + return last_input + compensation; + return ULONG_MAX; + } - if (goal > score) - return last_input + compensation; - if (last_input > compensation + min_input) + if (last_input - compensation > min_input) return last_input - compensation; return min_input; }