From patchwork Sun Jun 13 17:40:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xu, Yanfei" X-Patchwork-Id: 12317835 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2937C48BCF for ; Sun, 13 Jun 2021 17:40:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 14FD861245 for ; Sun, 13 Jun 2021 17:40:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 14FD861245 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=windriver.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 630656B0036; Sun, 13 Jun 2021 13:40:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E0D26B006E; Sun, 13 Jun 2021 13:40:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E48D6B0070; Sun, 13 Jun 2021 13:40:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0201.hostedemail.com [216.40.44.201]) by kanga.kvack.org (Postfix) with ESMTP id 0BA3B6B0036 for ; Sun, 13 Jun 2021 13:40:44 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 8DD72181AEF10 for ; Sun, 13 Jun 2021 17:40:44 +0000 (UTC) X-FDA: 78249415608.26.837B8B3 Received: from mx0a-0064b401.pphosted.com (mx0a-0064b401.pphosted.com [205.220.166.238]) by imf30.hostedemail.com (Postfix) with ESMTP id 41664E000263 for ; Sun, 13 Jun 2021 17:40:38 +0000 (UTC) Received: from pps.filterd (m0250810.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 15DHecoR016609; Sun, 13 Jun 2021 10:40:38 -0700 Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2107.outbound.protection.outlook.com [104.47.58.107]) by mx0a-0064b401.pphosted.com with ESMTP id 395g7t057b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 13 Jun 2021 10:40:37 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iG05Dv+0Jci6zOEbN71814fJQYoYyzXCvN1M/YSsrw6B1Zr8quMHshO56IZlLvJQnGYF+ZAV7G/cIRTNzpwlsJUI+9u299TiRYThKumviH+W/Vxdt6jnYUZDJUzlvDHJRKc79xQ6wnRbIfTDI502VpiK4uG6dFJfXUCnNqBtIMhiIfUL1hn5cVTr9pa5/M0qhOWsjdSmnlIi5Fp+Y+H5pAzL2hFUY6Qn5DFZfU01/V7wnWfZQTu8LsAWW/vcbQKw5mQI+WkzWXa9PNjV23YU1DHIHBXx1F5XzYVLjx/ml92qtbrapuoQBvLHdiOgXbB2Uz0uHeGHkF6UBWvm+iSVuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=v9jx5m/5m1vlRBDHHmpgo9Wz+akRHkY2m4IwKGzw6BY=; b=N/cIz7fz0trRByCBEwhXTXyOFXdAzeAJ5/C4iqi7TxHsNcRXXzM6xYwQSt/blMnj2G1i0biVVHLlmVdMVvOJlZ0IqW/AlSwBqOYYmAVEOnHaRiu9NJv90cwZ2BQ10AEEmr3jWl2XsCcUEucpfaJQ0AXfMm6zgyVAGBxyaQ2B/s3OnWEC8fUHe3m+0fZb0Eui1Yr/pFi2NMxxDWzc/AnuxI/d3W1vHUMULR59y4AmNoIRzTwI3U/kwOJpxQ2MfP948a6yLCVGVSJeH2ssVyoBXwSYJqKiVAucDSHoh1lmg784+lvNhXOuGfdTsC1gK0KOBBuTJHnAkL0PcjOrxhz/vA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=windriver.com; dmarc=pass action=none header.from=windriver.com; dkim=pass header.d=windriver.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriversystems.onmicrosoft.com; s=selector2-windriversystems-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=v9jx5m/5m1vlRBDHHmpgo9Wz+akRHkY2m4IwKGzw6BY=; b=G2t40kuJWEUDa5XwFkLfE5Zl1nyuMet4vRs2oh5BP+hM5vx0y8sXOp5eNvCGKqvQCv3vtZ+G8V76ouGuxG59Ps4G0xYmCqTj46E5xZ3kwVmy3dLUvsqqWVgPd9WUJwHbIAVx/plUlmMzJRkT9NPxr8prpeu8VwUXkxhN8hxgLE8= Received: from BY5PR11MB4241.namprd11.prod.outlook.com (52.132.255.77) by SJ0PR11MB5053.namprd11.prod.outlook.com (20.182.106.209) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4219.24; Sun, 13 Jun 2021 17:40:35 +0000 Received: from BY5PR11MB4241.namprd11.prod.outlook.com ([fe80::34b3:17c2:b1ad:286c]) by BY5PR11MB4241.namprd11.prod.outlook.com ([fe80::34b3:17c2:b1ad:286c%5]) with mapi id 15.20.4219.025; Sun, 13 Jun 2021 17:40:35 +0000 From: Yanfei Xu To: catalin.marinas@arm.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm/kmemleak: fix the possible wrong memory scanning period Date: Mon, 14 Jun 2021 01:40:22 +0800 Message-Id: <20210613174022.23044-1-yanfei.xu@windriver.com> X-Mailer: git-send-email 2.27.0 X-Originating-IP: [60.247.85.82] X-ClientProxiedBy: HK2PR03CA0048.apcprd03.prod.outlook.com (2603:1096:202:17::18) To BY5PR11MB4241.namprd11.prod.outlook.com (2603:10b6:a03:1ca::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from pek-lpggp1.wrs.com (60.247.85.82) by HK2PR03CA0048.apcprd03.prod.outlook.com (2603:1096:202:17::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4242.9 via Frontend Transport; Sun, 13 Jun 2021 17:40:33 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8da94ef9-762c-4897-ae97-08d92e92593e X-MS-TrafficTypeDiagnostic: SJ0PR11MB5053: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:457; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PNMS8RCN0Pn60iotAbpDwbhdUSWon4P2feDyRJAmBVsIyxMzJ0l/6qVXy8FY0aJgPO9fwJLoVrlQDEct1hIT8V++TCrXlcB+Ma1ZjSg3/SbxkL07YwwDBygCOzGw0HPSsFq00VPV0swsrxByKBFhYwY0/bTQhtmk0eYxlFNtAec8Gt5AoJ84Ht4HXGTHMXX8SU2AqvjOYeT/3oykgqfaxfpl9M1aXWNRBP6pbDdt5xpez943Xq9VCo0iBD53o+vA8hbPr/RW8q2GsbNhysNVraboXnitBVoaTGKKDmKR34UP9eq2TUGjJzO8AHFBiT2IuxfNolAXKCdJ1axkdtRURKi6MZvil44aRDmFMspfGMzHYiSITrspljBZMAK/b/CTWGQ+GlGlnb0KYXPDy2rZA0h80raHZI9ODWE/dEfefiI8fKb7U+fvHhXkTdt/IgFBoMzUM7vSfneCTyybEiuaeQYBwQPRtN5tVSj2fJ5QS8if5KyKjTrSf5RX1enFqEPiInPrNFlmUCu1qOsGZ1l6i9spqcSR6hIWol78Y8sKHUSp4h/wmOPxgPORAuUbLdYs9wiOVccKM5ZjLQ1Ty2vGrVGUJGZ20H5hQg343zx6zfqNnphvT3Q6ojPP/2/6wapZfgAbe2EUEKzSVC9e6zGDtQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR11MB4241.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(376002)(136003)(39830400003)(396003)(346002)(956004)(2616005)(44832011)(316002)(52116002)(478600001)(2906002)(16526019)(186003)(26005)(6666004)(8936002)(86362001)(4326008)(6506007)(8676002)(1076003)(5660300002)(38100700002)(36756003)(83380400001)(6486002)(66946007)(66476007)(38350700002)(66556008)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2KQt5LGq5KRCzojQgCXQmhc77Ms3GEEiqj4RExLbuXrdB+2khcwQwIVfFAUBIAzDh3BYLf6g4pOZONc4A4w/7wcJdC4Y//y0JZpEeQFSmPwByISnluci7s0LLRL+u5DJ7DBH5Tr1Utr6xMbYB9Cb8CCPhoMHcGitQZWqV9w1HFbSH14JlC+ah7NeQIlJ8yyyiJHgB2JN5eweBBrqbxvlPk+kZRRkuj8tdkyE57oezOei5ioMx91TcQRFePV+PxsZaFytcCzx7ABfGdHN1tm1KPfIzX88lgNTLUj2k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ossTenant-Network-Message-Id: 8da94ef9-762c-4897-ae97-08d92e92593e X-MS-Exchange-CrossTenant-AuthSource: BY5PR11MB4241.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jun 2021 17:40:34.9507 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ddb2873-a1ad-4a18-ae4e-4644631433be X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: NjmkyG42Tclix2rDPdF2BMEvGlxLD45iLIIUq60cjDe7zUlUt2OXm48tndGNAbdfErQbeclrS9YhvYR3Lq1H6A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR11MB5053 X-Proofpoint-GUID: 7kDZMWKGb86CjsStWw3cQI0cbocQQlBn X-Proofpoint-ORIG-GUID: 7kDZMWKGb86CjsStWw3cQI0cbocQQlBn X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.761 definitions=2021-06-13_09:2021-06-11,2021-06-13 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 clxscore=1015 spamscore=0 mlxscore=0 bulkscore=0 malwarescore=0 lowpriorityscore=0 phishscore=0 adultscore=0 suspectscore=0 impostorscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2106130127 Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=windriversystems.onmicrosoft.com header.s=selector2-windriversystems-onmicrosoft-com header.b=G2t40kuJ; dmarc=pass (policy=none) header.from=windriver.com; spf=pass (imf30.hostedemail.com: domain of "prvs=1798d22207=yanfei.xu@windriver.com" designates 205.220.166.238 as permitted sender) smtp.mailfrom="prvs=1798d22207=yanfei.xu@windriver.com" X-Rspamd-Server: rspam02 X-Stat-Signature: m5yg5uu6irh6d1cpr78fgspmmyake5qf X-Rspamd-Queue-Id: 41664E000263 X-HE-Tag: 1623606038-243851 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: This commit contains 3 modifies: 1.Convert the type of jiffies_scan_wait to "unsigned long". 2.Use READ/WRITE_ONCE() for accessing "jiffies_scan_wait". 3.Fix the possible wrong memory scanning period. If you set a large memory scanning period like blow, then the "secs" variable will be non-zero, however the value of "jiffies_scan_wait" will be zero. echo "scan=0x10000000" > /sys/kernel/debug/kmemleak It is because the type of the msecs_to_jiffies()'s parameter is "unsigned int", and the "secs * 1000" is larger than its max value. This in turn leads a unexpected jiffies_scan_wait, maybe zero. We corret it by replacing kstrtoul() with kstrtouint(), and check the msecs to prevent it larger than UINT_MAX. Signed-off-by: Yanfei Xu Acked-by: Catalin Marinas --- mm/kmemleak.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 92a2d4885808..228a2fbe0657 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -219,7 +219,7 @@ static struct task_struct *scan_thread; static unsigned long jiffies_min_age; static unsigned long jiffies_last_scan; /* delay between automatic memory scannings */ -static signed long jiffies_scan_wait; +static unsigned long jiffies_scan_wait; /* enables or disables the task stacks scanning */ static int kmemleak_stack_scan = 1; /* protects the memory scanning, parameters and debug/kmemleak file access */ @@ -1567,7 +1567,7 @@ static int kmemleak_scan_thread(void *arg) } while (!kthread_should_stop()) { - signed long timeout = jiffies_scan_wait; + signed long timeout = READ_ONCE(jiffies_scan_wait); mutex_lock(&scan_mutex); kmemleak_scan(); @@ -1807,14 +1807,20 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, else if (strncmp(buf, "scan=off", 8) == 0) stop_scan_thread(); else if (strncmp(buf, "scan=", 5) == 0) { - unsigned long secs; + unsigned secs; + unsigned long msecs; - ret = kstrtoul(buf + 5, 0, &secs); + ret = kstrtouint(buf + 5, 0, &secs); if (ret < 0) goto out; + + msecs = secs * MSEC_PER_SEC; + if (msecs > UINT_MAX) + msecs = UINT_MAX; + stop_scan_thread(); - if (secs) { - jiffies_scan_wait = msecs_to_jiffies(secs * 1000); + if (msecs) { + WRITE_ONCE(jiffies_scan_wait, msecs_to_jiffies(msecs)); start_scan_thread(); } } else if (strncmp(buf, "scan", 4) == 0)