Message ID | 20231119194740.94101-2-ryncsn@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp1809542vqn; Sun, 19 Nov 2023 11:48:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IGTvRNbarNDUT3sKNMnceNdMGnhkMDxmmBBqLtSgen9+2I409g1C7p6hBnjMtK1xYi694sj X-Received: by 2002:a05:6a20:d420:b0:187:a581:cc4e with SMTP id il32-20020a056a20d42000b00187a581cc4emr3069084pzb.29.1700423299277; Sun, 19 Nov 2023 11:48:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700423299; cv=none; d=google.com; s=arc-20160816; b=JMwOB1XgUsPT53TIBDM168F+FElzUbQ5vUrHwgytKUuA202asiAbgQcWLSq5xrte0e pEGOGQ8CFiUB+0kkklNm7SlA9BKZ45t1cHSAywJ0r5ZRMXVEBmeQg26fCXh5GrsS+V/a Oj0Nvcf3AuIvqPZ7UbDd16ji4kuE4Y+4qyvlVv8HL3bLNXbplZIAsDP2cIELYs8raxne tUVlkXyCL4hNEb99TzIXfZqX6AnugZod/9WCtP7uouvWxRp/MbEBUWoQz6CqvDzcLQRL Sdx7VePo6/O4jTFl0SFAVvopGT5sT4i5640jejYJjXnClMUWFFAn4gl580ophCuzDvCB dbww== 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:reply-to :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tM0GQjuVnFv7RufWXzCKq9li8NCfwru2okUxp/uvL+E=; fh=4HE/piJoUCKuBTCCBiej4//zvvzywHdOLL9QM/KYjYM=; b=ge+tDEonR5AdmUi1edH2GzuQXyaTl443sZX1gF1v1Zjv5bZWcw2dx8MiQu9i+vuJaG s39lzBSRP8oYQx80el1N/WtomgnYfTFOm/3asQ0G6mZFESn8eZqyiaDMCHGsF43eHRsc Zqwyf53wVBJ+P1g1KoBpoCRNHNeRPqAcTQhrBj08niAABOqrmmudvngUjqFYS6VpvK75 UhiYAVQ5g5ZWfFkRqnFD3qSJx9QgxL391JYc66BiFtlQjLDXvTmT822h9BFYBnJ74Z1O IVJp35200N6tWg9NQUL8D1wd5AjTQUqVCzzphWf0uHlqSUMixkENuIY8VLydunIcuhFV Bu6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bQbQ3YTv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id f13-20020aa79d8d000000b00690d8c05582si6443655pfq.372.2023.11.19.11.48.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Nov 2023 11:48:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bQbQ3YTv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 6592180A5FAB; Sun, 19 Nov 2023 11:48:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229847AbjKSTsK (ORCPT <rfc822;jaysivo@gmail.com> + 29 others); Sun, 19 Nov 2023 14:48:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229665AbjKSTsJ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 19 Nov 2023 14:48:09 -0500 Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E008211A for <linux-kernel@vger.kernel.org>; Sun, 19 Nov 2023 11:48:05 -0800 (PST) Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-357cc880bd8so14138705ab.0 for <linux-kernel@vger.kernel.org>; Sun, 19 Nov 2023 11:48:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700423285; x=1701028085; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=tM0GQjuVnFv7RufWXzCKq9li8NCfwru2okUxp/uvL+E=; b=bQbQ3YTvLIP9LH2t1GMs9zqgejkdaGNPxoDVKKh73N5sqhbbjr8T6V8/JKDnQabOOh /qRdKJtbeBuVKz6qiLkgB1i/i+neW8S75xSXJ3l6Unn82Qj/mkeUawLjWCa6Jae8iE6Q d47bG2IU4g7wCrGZayiNRT+8QdmukBz5hAlQbze6YsyWinY/5XOIImih14QN9GLMcA6N yJpz0WDO4TpISDbkVkKip/X64Q2twM4B/o1Yz7Ep5t13eEoLwJoljNgQBOVvvFBUi2t0 oIzwf5HvzXOT4ZWxSHh+ggG3pSD624M+HRrNDoPEmrogKKVAPm3xTaQ4w6xNohGPbxvV n2LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700423285; x=1701028085; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=tM0GQjuVnFv7RufWXzCKq9li8NCfwru2okUxp/uvL+E=; b=DfHFiiH72e5TOK7Zq8n4zpmve9Q34oPdBKUOZuBIfId2Y2a+HWlXlgDSrWq6ZYnnIV mj9vsl2cYP2B0Miz6hN1eMewBmNXG63IPjuFpbz4UmeE3KmPO5rMiEStgioDox55xdq2 8faPFb7xR14lUllobk+WJGTI3wxmfa3SS/N5E3OzEcJKD62cyItlVh98v3gdvnY2UR00 qxcY1C0W4p3Td4+fdUMTULPBHCcy0uHvcDxsc62+2PskX18UZZFDbBqYtyPvlbYx/zUT rWo0uqzsT8tCfZV5LpcqYUKHK0mpJSmpUHMbTkDrURVCScq2qPxBctMVjPKws/5CfFAx uqfg== X-Gm-Message-State: AOJu0YxsnnAxhFbBWO2h8Rd2pgVIY1p03Tgv+pKpX5nFF7PpL6YR1yM2 RwsPqK7R9cTzGT5EHvaBh7I= X-Received: by 2002:a92:c26d:0:b0:35b:695:c3c8 with SMTP id h13-20020a92c26d000000b0035b0695c3c8mr353889ild.9.1700423285183; Sun, 19 Nov 2023 11:48:05 -0800 (PST) Received: from KASONG-MB2.tencent.com ([115.171.40.79]) by smtp.gmail.com with ESMTPSA id a6-20020aa78646000000b006cb7feae74fsm1237140pfo.164.2023.11.19.11.48.02 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 19 Nov 2023 11:48:04 -0800 (PST) From: Kairui Song <ryncsn@gmail.com> To: linux-mm@kvack.org Cc: Andrew Morton <akpm@linux-foundation.org>, "Huang, Ying" <ying.huang@intel.com>, David Hildenbrand <david@redhat.com>, Hugh Dickins <hughd@google.com>, Johannes Weiner <hannes@cmpxchg.org>, Matthew Wilcox <willy@infradead.org>, Michal Hocko <mhocko@suse.com>, linux-kernel@vger.kernel.org, Kairui Song <kasong@tencent.com> Subject: [PATCH 01/24] mm/swap: fix a potential undefined behavior issue Date: Mon, 20 Nov 2023 03:47:17 +0800 Message-ID: <20231119194740.94101-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231119194740.94101-1-ryncsn@gmail.com> References: <20231119194740.94101-1-ryncsn@gmail.com> Reply-To: Kairui Song <kasong@tencent.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 19 Nov 2023 11:48:18 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783023061325559218 X-GMAIL-MSGID: 1783023061325559218 |
Series |
Swapin path refactor for optimization and bugfix
|
|
Commit Message
Kairui Song
Nov. 19, 2023, 7:47 p.m. UTC
From: Kairui Song <kasong@tencent.com> When folio is NULL, taking the address of its struct member is an undefined behavior, the UB is caused by applying -> operator to a pointer not pointing to any object. Although in practice this won't lead to a real issue, still better to fix it, also makes the code less error-prone, when folio is NULL, page is also NULL, instead of a meanless offset value. Signed-off-by: Kairui Song <kasong@tencent.com> --- mm/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Mon, Nov 20, 2023 at 03:47:17AM +0800, Kairui Song wrote: > From: Kairui Song <kasong@tencent.com> > > When folio is NULL, taking the address of its struct member is an > undefined behavior, the UB is caused by applying -> operator > to a pointer not pointing to any object. Although in practice this > won't lead to a real issue, still better to fix it, also makes the > code less error-prone, when folio is NULL, page is also NULL, > instead of a meanless offset value. Um, &folio->page is NULL if folio is NULL. The offset of 'page' within 'folio' is 0. By definition; and this will never change.
Hi Kairui, On Sun, Nov 19, 2023 at 12:55 PM Matthew Wilcox <willy@infradead.org> wrote: > > On Mon, Nov 20, 2023 at 03:47:17AM +0800, Kairui Song wrote: > > From: Kairui Song <kasong@tencent.com> > > > > When folio is NULL, taking the address of its struct member is an > > undefined behavior, the UB is caused by applying -> operator I think dereferencing the NULL pointer is undefined behavior. There is no dereferencing here. It is just pointer arithmetic of NULL pointers, which is adding offset of page to the NULL pointer, you got NULL. > > won't lead to a real issue, still better to fix it, also makes the > > code less error-prone, when folio is NULL, page is also NULL, > > instead of a meanless offset value. I consider your reasoning is invalid. NULL pointer arithmetic should be legal. This patch is not needed. Chris
Chris Li <chrisl@kernel.org> 于2023年11月20日周一 11:36写道: > > Hi Kairui, > > On Sun, Nov 19, 2023 at 12:55 PM Matthew Wilcox <willy@infradead.org> wrote: > > > > On Mon, Nov 20, 2023 at 03:47:17AM +0800, Kairui Song wrote: > > > From: Kairui Song <kasong@tencent.com> > > > > > > When folio is NULL, taking the address of its struct member is an > > > undefined behavior, the UB is caused by applying -> operator > > I think dereferencing the NULL pointer is undefined behavior. There is > no dereferencing here. It is just pointer arithmetic of NULL pointers, > which is adding offset of page to the NULL pointer, you got NULL. > > > > won't lead to a real issue, still better to fix it, also makes the > > > code less error-prone, when folio is NULL, page is also NULL, > > > instead of a meanless offset value. > > I consider your reasoning is invalid. NULL pointer arithmetic should > be legal. This patch is not needed. > > Chris Hi, Chris and Matthew. Thanks for the comments. Right, it's just a language syntax level thing, since "->" have a higher priority, so in the syntax level it is doing a member access first, then take the address. By C definition member access should not happen if the object is invalid (NULL). Only a hypothesis problem on paper... This is indeed not needed since in reality it's just pointer arithmetic. I'm OK dropping this.
Hi Kairui, On Mon, Nov 20, 2023 at 3:15 AM Kairui Song <ryncsn@gmail.com> wrote: > > Chris > > Hi, Chris and Matthew. > > Thanks for the comments. > > Right, it's just a language syntax level thing, since "->" have a > higher priority, so in the syntax level it is doing a member access > first, then take the address. By C definition member access should > not happen if the object is invalid (NULL). Only a hypothesis problem > on paper... The dereference only shows up in the abstract syntax tree level. According to the C standard there are expansion and evaluation phases after that. At the evaluation phase the dereference will turn into pointer arithmetic. Per my understanding, the dereference never actually happens, due to the evaluation rules, not even in theory. > This is indeed not needed since in reality it's just pointer > arithmetic. I'm OK dropping this. Thanks Chris
diff --git a/mm/memory.c b/mm/memory.c index e27e2e5beb3f..70ffa867b1be 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3861,7 +3861,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) /* skip swapcache */ folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vmf->address, false); - page = &folio->page; if (folio) { __folio_set_locked(folio); __folio_set_swapbacked(folio); @@ -3879,6 +3878,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) workingset_refault(folio, shadow); folio_add_lru(folio); + page = &folio->page; /* To provide entry to swap_readpage() */ folio->swap = entry;