Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp866387pxa; Sat, 22 Aug 2020 02:58:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwsSKP/ntRCwcGyvJgLDlYT04lMcFP7MQz4GjXAnnjuPw+ygkf02TefamKShfwlv2v27H1k X-Received: by 2002:a17:907:41dc:: with SMTP id og20mr6968184ejb.183.1598090322342; Sat, 22 Aug 2020 02:58:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598090322; cv=none; d=google.com; s=arc-20160816; b=SmDKZQbzsjMqNE0K3MPWK39nGsvS7nKmV9wRog9VaseAYKvx8Bm1IIeLXy9rDjRr0z ClNOXY8etTiQLtt/gDP2O5GTzbI83f5cmDNNcCnWYVhwSUOxqYZCN7F//ElFtIVI7rur /hA0Pw98FwILw7qRjCFkwWcI0EfRUfHmQtLg3KXEBi6ZC2nfcSBZdQWFpH3O7nWGoCEr AgDJnm7Vi8AsNyPa6g5Z68qMKUWK3uJ31Q2Un9WsehxQDuzl3p148jrkFtJS9sVpat3T JS7M1FR8w38DyvPJPQibo2/bDfxKyxrnqNEDS9ene93KOct+B8t+EKZfOEh+fRs9ighE exSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=AA4QvqZ8G3UUgWk56L6siBEVYBlQ7DKtEof4BU/gmnM=; b=sRqF55x7duGmpEnmEDNWRx/r+m3J+IwmNo3oLiLaJbGZ3tnW9pcKF0kszWlgTVKubg nYqZV18X1MPS4y7KmqX6ez20OZzLwwFYbh2SWT4OP/rxou/lp6ITYUrx5fndZ3tLO4wQ MYPuO/CpKwkdfH2n+rgXuQc/t4KuOLw+4mu/MfTBLneBxGF0zLTdawNHNPGlZAf8Zx4D bzhREWQpeEGkLrYy9JVevuRihbZX9kpLrFgFWFhgJAigl8VBUwgYclDQAsARgjxXk9Su zg/v7GMo/PeGXjqvb0MZ05TOdqZQ9s9pXDLhZPbpclIW3GMcJ41tnA44EXo8ziSdA6Wt 8MHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=Hy08vYzb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x8si4161738edl.249.2020.08.22.02.58.19; Sat, 22 Aug 2020 02:58:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=Hy08vYzb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727793AbgHVJxz (ORCPT + 99 others); Sat, 22 Aug 2020 05:53:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725864AbgHVJxx (ORCPT ); Sat, 22 Aug 2020 05:53:53 -0400 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B510C061573 for ; Sat, 22 Aug 2020 02:53:53 -0700 (PDT) Received: by mail-pj1-x1043.google.com with SMTP id q93so1776405pjq.0 for ; Sat, 22 Aug 2020 02:53:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AA4QvqZ8G3UUgWk56L6siBEVYBlQ7DKtEof4BU/gmnM=; b=Hy08vYzbJ2PXLCKeaQ8gMOUe5NEBsJHKUF3pXyca6ilV/ucXow/JiUg6qZKy2iL14I 4gM6qgv9d3Hpktgt722p6TTGFt9m5EXNQWFQafd65aD8q5URNVlW4HuD/16xYbKayopp ppWSbM5gpm2h9jQRzRfPHuYv/ChyvWGH8OfGw2uD3QLz9ZSrPvrSKxrGK12c1/Ve2YEv ydDNukJRr3Fp2rWaQX/jLpgaFtKouvt9pvF/qC7uxQcnpvKiUh0FIuuDU/JY3dBA77Ak +xaQ5ef9Ten+QwSHX+Y9gdq+4py32DSzTve84iV272hi//du1s++RPXnNKIMwLBVwEGa pl2g== 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:mime-version :content-transfer-encoding; bh=AA4QvqZ8G3UUgWk56L6siBEVYBlQ7DKtEof4BU/gmnM=; b=hGpwMT7ov/3tpZnd6DEHVLd9uTGGnrbvYthY6j1CYiLLE9XduIG2VmHBbPCRJNAl65 aPI9Tq9R6btij3SgkzJr0JarFSbce2VIg+6Evi0uHL/Lt0BlUgkmYmOkisVMJn60Idme 8AQk3Tgu/FgZ/Kb31meRpboIDxdNJ74km3LT94FxiNRaUbGIMjzZ71owG3CVvl3pzoY+ rkYVl0FFyJUeN8qpYEQAScLdnN7NZ3magGBP4q3E0zhORALkSDIbk9pwFs29DEQ2DmnP 2/8BaSsgYm4HNVY8+8c25z4JELQEHsKcK8mzSo5H2u41DyoLAf0TN4S+izPsarI4Emd3 AoLA== X-Gm-Message-State: AOAM531/CXxu6ELubkCC3LmwpneAL9X2G8aCVMexWhMo9KRO8WfZyW3M f64Z20wFdt98sqbBQlb/w8nmtw== X-Received: by 2002:a17:902:b714:: with SMTP id d20mr5636180pls.103.1598090031269; Sat, 22 Aug 2020 02:53:51 -0700 (PDT) Received: from localhost.localdomain ([103.136.220.72]) by smtp.gmail.com with ESMTPSA id a13sm4972397pfo.49.2020.08.22.02.53.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 22 Aug 2020 02:53:50 -0700 (PDT) From: Muchun Song To: mike.kravetz@oracle.com, akpm@linux-foundation.org Cc: npiggin@suse.de, agl@us.ibm.com, ak@linux.intel.com, nacc@us.ibm.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH] mm/hugetlb: Fix a race between hugetlb sysctl handlers Date: Sat, 22 Aug 2020 17:53:28 +0800 Message-Id: <20200822095328.61306-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There is a race between the assignment of `table->data` and write value to the pointer of `table->data` in the __do_proc_doulongvec_minmax(). Fix this by duplicating the `table`, and only update the duplicate of it. And introduce a helper of proc_hugetlb_doulongvec_minmax() to simplify the code. The following oops was seen: BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor instruction fetch in kernel mode #PF: error_code(0x0010) - not-present page Code: Bad RIP value. ... Call Trace: ? set_max_huge_pages+0x3da/0x4f0 ? alloc_pool_huge_page+0x150/0x150 ? proc_doulongvec_minmax+0x46/0x60 ? hugetlb_sysctl_handler_common+0x1c7/0x200 ? nr_hugepages_store+0x20/0x20 ? copy_fd_bitmaps+0x170/0x170 ? hugetlb_sysctl_handler+0x1e/0x20 ? proc_sys_call_handler+0x2f1/0x300 ? unregister_sysctl_table+0xb0/0xb0 ? __fd_install+0x78/0x100 ? proc_sys_write+0x14/0x20 ? __vfs_write+0x4d/0x90 ? vfs_write+0xef/0x240 ? ksys_write+0xc0/0x160 ? __ia32_sys_read+0x50/0x50 ? __close_fd+0x129/0x150 ? __x64_sys_write+0x43/0x50 ? do_syscall_64+0x6c/0x200 ? entry_SYSCALL_64_after_hwframe+0x44/0xa9 Fixes: e5ff215941d5 ("hugetlb: multiple hstates for multiple page sizes") Signed-off-by: Muchun Song --- mm/hugetlb.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index a301c2d672bf..818d6125af49 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3454,6 +3454,23 @@ static unsigned int allowed_mems_nr(struct hstate *h) } #ifdef CONFIG_SYSCTL +static int proc_hugetlb_doulongvec_minmax(struct ctl_table *table, int write, + void *buffer, size_t *length, + loff_t *ppos, unsigned long *out) +{ + struct ctl_table dup_table; + + /* + * In order to avoid races with __do_proc_doulongvec_minmax(), we + * can duplicate the @table and alter the duplicate of it. + */ + dup_table = *table; + dup_table.data = out; + dup_table.maxlen = sizeof(unsigned long); + + return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos); +} + static int hugetlb_sysctl_handler_common(bool obey_mempolicy, struct ctl_table *table, int write, void *buffer, size_t *length, loff_t *ppos) @@ -3465,9 +3482,8 @@ static int hugetlb_sysctl_handler_common(bool obey_mempolicy, if (!hugepages_supported()) return -EOPNOTSUPP; - table->data = &tmp; - table->maxlen = sizeof(unsigned long); - ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); + ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, + &tmp); if (ret) goto out; @@ -3510,9 +3526,8 @@ int hugetlb_overcommit_handler(struct ctl_table *table, int write, if (write && hstate_is_gigantic(h)) return -EINVAL; - table->data = &tmp; - table->maxlen = sizeof(unsigned long); - ret = proc_doulongvec_minmax(table, write, buffer, length, ppos); + ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, + &tmp); if (ret) goto out; -- 2.11.0