From patchwork Sun Nov 19 19:47:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13460657 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 BCFFCC5AD4C for ; Sun, 19 Nov 2023 19:48:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 57C406B034A; Sun, 19 Nov 2023 14:48:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 52CBE6B0353; Sun, 19 Nov 2023 14:48:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A6B26B0355; Sun, 19 Nov 2023 14:48:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2042A6B034A for ; Sun, 19 Nov 2023 14:48:09 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id EA505C0857 for ; Sun, 19 Nov 2023 19:48:08 +0000 (UTC) X-FDA: 81475739856.18.F465D79 Received: from mail-il1-f177.google.com (mail-il1-f177.google.com [209.85.166.177]) by imf21.hostedemail.com (Postfix) with ESMTP id 2800F1C0004 for ; Sun, 19 Nov 2023 19:48:06 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l4y9msSL; spf=pass (imf21.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.166.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700423287; h=from:from:sender:reply-to: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:in-reply-to:references:references:dkim-signature; bh=tM0GQjuVnFv7RufWXzCKq9li8NCfwru2okUxp/uvL+E=; b=TqFzOMBbqK6wGGtQzLF2U+nVkYjjuFMYRu/b98yvoQYZWZGS6laHf/LBFiATBJlYeLKRHR GzCOIYfs6dVYART+UhfL+WNGR4jbwB8yAPRiaqCaW+GAeGM1Q4Of2pIdp61ubBn2R6ZpbA 5lO2x8napQflanpzXgHiMCI+BIMhnJY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700423287; a=rsa-sha256; cv=none; b=LOZJXCW0ES0vNmGnTWcDa3opMw1a2ppjfThrRqdTq+3J0LY/qzYUaNiglwzZDdSGv9SoCh GKtkpJJbFP1YeIW65h+9Oli+iZaMvExAxh6shkuyGHIQGvqqWUOc+Xcs9UzBe0bEe1ou2K p+IAZbpj3UmZiPLozNO7eCxJLV5IV5o= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l4y9msSL; spf=pass (imf21.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.166.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-il1-f177.google.com with SMTP id e9e14a558f8ab-3574f99d236so14083275ab.2 for ; Sun, 19 Nov 2023 11:48:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700423285; x=1701028085; darn=kvack.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=l4y9msSLB82T5UtK5v61psg8ykiHtV9zOdeIa9sB7u5d6Q+KzuxArtvg9NP4XfDGhb W/Uj2AX3eD9WEdmbMrAPLLI37vAV35ZSXi3QuDihzSHQjBNvftA/asQnAKsa0F/NVDTa cOxvbk2DI6kkPdqQu5r5vYwquD9mfpOk4Ya1BRWZkTQpy09w+tRt5n3gqAiteGnT6gS/ 2so6bs2a37vmqIcO6yJHIcop1Hu+/6J7+AYOocj2+3w5FqmtoFLLntz91SF7BKZgkpCW pzbN1YWRi7pWV/5q2jGpb5pViaQJ4SeZTUrX+TaGtuJ2hxmIEW5057Yg16GyckK7/yuj HUCw== 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=Exh7Sn5LBfkHse7+fYEVxv3NlkMpw6a90aaDuQ3RgXWLKLM0xht+f34p9rSAh9DWyU aEPrAsSjuu/HJeaHt7MEj72wqwmxdFHMZyG6QuyWFzUgIJGHk4drz+cJb2u4a2pSSXOu fn0TdajmbzAjstLaF2vqHki/hJHcpvyIv4nElBH7rmsqj/ge0VHJCqKyMSfM+qR5wpip uhqUBQCQ1pWZ1OexBVXUVqYxv0lOt+bVzdHTII2cN5EzVAK5efOTYss+2O1+fpTE8hF2 vYgzhFV84/9FWB9Xo3kj3XlMO6ZsxtwJS0Xq7UUMlxO4HYRXmrlxyyqyxBoi1fzo6yyx 9Vcw== X-Gm-Message-State: AOJu0Yzy6wSMZ3E6SbQZu7jtA2I4b0O++d/1UKSk90NZLQ+mtLApTUEN +IW2hBC8WAc61Bla/AEOYabqaQZbKM/I/jD9 X-Google-Smtp-Source: AGHT+IF1UkKRv/ipdxof7nKQAoicjYZ8F2kSET5NL02/aAVKfiSk21ZO3YlPkVn68ZksUna/3gG5lw== 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 To: linux-mm@kvack.org Cc: Andrew Morton , "Huang, Ying" , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , linux-kernel@vger.kernel.org, Kairui Song 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 MIME-Version: 1.0 X-Stat-Signature: 4k3k3n3thrj44b7b7ifjeue177d16zx1 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 2800F1C0004 X-Rspam-User: X-HE-Tag: 1700423286-73558 X-HE-Meta: U2FsdGVkX1/37vj/oNBjFG/S/lVCyW5pKFcv46ayZvEQNnzpjv85+gh8fZOck+g02WHyBaszOGQO9JHMqjAlGowOSWWnL8Jfk+gO+YBL3zanryPYb4ATEVdM6UN2ucTg1zadx7q+Q33wAjxKbQg+xlmtNnBhmW+pgQFtDBLySjnlRkL5A+Tk+sVfephrrektpoQQ7JJARfxLsfm1hh2Gm8520SrvZcQpC6Q4hhFkJPOLEEd6CNqZuSz9y4lULcdXSwxIatzBTDSGJle9CuG/Vu1YtjtKf5HPSs6M2W78fAY1Wdzr+Pi62c/da2a6XbwXUhvI1MIpoGHE5Fi8TSYFx5r88vzWdS7lU7PoW9+nnl22tszGe8Ewvum3PSx26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GVWZ710WBniwFYLD8bytwpZGSPsuGYeV2Yoyopv5kAdYvyKLR1Zwur3DQZ3jb0j1gWtZ2Q9H/A2x7gP/1uSYdxrO9My1P1B2j7f8oxM8KEErRPow7E5rxOBW6AljrFzFUoHr3+h6sVijBTOHAswUHzotXE3BtupPELN2iBTZmQvaiUj+d0PWY4M2SwNIivh X-Bogosity: Ham, tests=bogofilter, spamicity=0.000012, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Kairui Song 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 --- mm/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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;