From patchwork Sat Aug 17 06:24:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13767002 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 7A7F4C52D7F for ; Sat, 17 Aug 2024 06:25:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0427C6B040B; Sat, 17 Aug 2024 02:25:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F33326B040C; Sat, 17 Aug 2024 02:25:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD38B6B040D; Sat, 17 Aug 2024 02:25:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BD94D6B040B for ; Sat, 17 Aug 2024 02:25:15 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2F5ECC0504 for ; Sat, 17 Aug 2024 06:25:15 +0000 (UTC) X-FDA: 82460750190.24.38581B3 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf03.hostedemail.com (Postfix) with ESMTP id 4EBC120016 for ; Sat, 17 Aug 2024 06:25:13 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="LDRag/iK"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723875829; 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:in-reply-to:references:references:dkim-signature; bh=xisu3WuwKYF+5Cz14TC0TNfL+QngYLYmdZWec26IjWw=; b=JgQyWcvkngs+LFyXNgpF9vT6vakaQFla9F+hBldomfipzRav4pgWfGIJAR5qwPtIjHntw6 JgPcg+7Zyo+LlbJYwF67ZKHk+p3nNJecKX15gwHTkJ+TFlSRfdFvFOxAGQnOmHSlGlD8lz 6n/tQ30LBFkdnd2e0IV7LSgK2z7owbc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723875829; a=rsa-sha256; cv=none; b=bg/QtE/MiZHZ7KNsUOMMXtcU48fSzUIVnBAEkBZGjB+oAoz16C+ddoIsaPwz7w4mLs6P7n iJHqvIJJfto3yaoyxDGzFix/QILLr0wTQS5DCVBpWqIZ5GwB9rkL29uE+q0zn1dLJo9Kpi qsQ2JJj2WwT4Y1auZe+nW6DUOUkWug0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="LDRag/iK"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=21cnbao@gmail.com Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2021537a8e6so8916145ad.2 for ; Fri, 16 Aug 2024 23:25:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723875912; x=1724480712; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xisu3WuwKYF+5Cz14TC0TNfL+QngYLYmdZWec26IjWw=; b=LDRag/iK/pU9OYlFJsPQ79BQGA+TirDCbr+2rZoRqNKZ7aw9LNxYZkz82Ac0cwlEpx jH9FqZEAUBAcikuiESgMHMcz08TbyfAyMKCnXOOm8fh2Tmqot2/ViwMq4BeQ0wBzqBjd p//0rmmszSIPp7m+0gUn8XAnO8Yza8GozZMHPkpmqRHOd0aMRpabEbak02F0Yap3tFi/ d3/04XS1oazDLJNjo2eK3tJxyIQ5L3yYwl/causcEc3EEcebOHy0GQE0fqJ4ZcxmsA5x VQ8FXe+pCzogP4kBZ/vO7dJQGNlGD2Yz+4T/7c+FAHHf4VRVXmIbuFkNjrPYXySTxqKE KjIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723875912; x=1724480712; h=content-transfer-encoding:mime-version: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=xisu3WuwKYF+5Cz14TC0TNfL+QngYLYmdZWec26IjWw=; b=jg2OtjEfVDrwR3PjKFKZ0gF1rdsrteIBiFXWAKgMfSkGG1fsQVFw64hf2onCODQBqG ePMwnbthHr2kE9rZcn7GSpt38dVy4FVh7mhcMY06o8UUCzCWXQMZ+U+ZkD9rtygmPHjH kc11CrFq9xmGvT1h8jU/d15D0fP9LLeBDKZTOGmW0tim03P+c/ZsitcrTQ6G1xRQnUfg a4wQ3MY+2bWS8T+O4hxAjLqIXetFF+6s5EZlV8fDBQlJfxFvu+mJF0IqtQzyq/Hfejt+ tbqHxhDazqGNPIiHSj+n1u8SsV4t0VsF5sE78OCkOKX1p+kzOcxnaCGW7yUS2Nz7ke22 8UHA== X-Forwarded-Encrypted: i=1; AJvYcCUwA6vJZDlDtIed16xsLM4YbxHBhxsEgV275n82mAJFPIxRK3g/t6TAb9kbkSvzRybEXv7nnDwsPMDYwGMrdowBVdw= X-Gm-Message-State: AOJu0Yy8jR4DT3i/sJLjPIuFJs4jKCd7x4NZWXX0kQKRpi5tgbfEEjKo 2IvFxNaQOdnu8Pvc1PBybADwlQ1ZRN+RXlPu+2ccxnyDP++OvDhczC9yL7P+ X-Google-Smtp-Source: AGHT+IENdPsIQj8xmveRlL6yyjeMypK32/OjrVrt2yi6p6JSCZBnX1qKEm72i6xM5tYhi5eXrnZ5vA== X-Received: by 2002:a17:90b:315:b0:2c7:c788:d34d with SMTP id 98e67ed59e1d1-2d3e0870ccfmr5702366a91.38.1723875911896; Fri, 16 Aug 2024 23:25:11 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:fd84:292a:c6d0:8b67]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac854f3bsm6768404a91.51.2024.08.16.23.25.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 16 Aug 2024 23:25:11 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 42.hyeyoo@gmail.com, cl@linux.com, hailong.liu@oppo.com, hch@infradead.org, iamjoonsoo.kim@lge.com, mhocko@suse.com, penberg@kernel.org, rientjes@google.com, roman.gushchin@linux.dev, torvalds@linux-foundation.org, urezki@gmail.com, v-songbaohua@oppo.com, vbabka@suse.cz, virtualization@lists.linux.dev, Jason Wang , Xie Yongji Subject: [PATCH v3 1/4] vduse: avoid using __GFP_NOFAIL Date: Sat, 17 Aug 2024 18:24:46 +1200 Message-Id: <20240817062449.21164-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240817062449.21164-1-21cnbao@gmail.com> References: <20240817062449.21164-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4EBC120016 X-Stat-Signature: d5f4ps6kjw1jckpfafpxh6aawt4dps5a X-Rspam-User: X-HE-Tag: 1723875913-99729 X-HE-Meta: U2FsdGVkX1+NuERxlTTO46b+uHnsvWfKCFNosBv+QWsqEzVaAk8EwLf1pM9zWtPMmVWJF5dow265t3X3qM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3O5PPhWEf70eVXmHVaYloIbxL+4whqb5uDvlSCLrMRApzEfTPzyPmHeBY4cQ6LH7lBrYzj6Iz6Kahh1I/+lrEznPmiunZfYTfa8KUQn2Oj7A/Mpg49k+/N1+7BTklwUt42dMpfjqPArvZEhJvyth40PozYBSkqM09u7CIg+5oSCTbhbolOdCUpfqY/NRTEj3wKY2vKUKxNIsNAupO9gipRAoxun50IsO7EFtifpg0RhRcwcBCLnlYdKkG986/aLWgYldolLUVGIuQO5nk3rfAZ3FM/kYh7rePV1L0tOp8BB34ejOJ+cG2zV4wJ1QZntEvh6vyPSvT9HYMdDJq1E3xV+7M0w5sdkLlwqOOkR5EufbfaqbLX6oAXMhF2PmCjDgEIqD3j4A5afCRMhKj8hORDBX6IiXFUABEUu4BSHgSdA94MG/VWyTZrkyhPpDmtxwdM+bZjAfih7+Lw7zsIRPilnf 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: From: Jason Wang mm doesn't support non-blockable __GFP_NOFAIL allocation. Because persisting in providing __GFP_NOFAIL services for non-block users who cannot perform direct memory reclaim may only result in an endless busy loop. Therefore, in such cases, the current mm-core may directly return a NULL pointer: static inline struct page * __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, struct alloc_context *ac) { ... /* * Make sure that __GFP_NOFAIL request doesn't leak out and make sure * we always retry */ if (gfp_mask & __GFP_NOFAIL) { /* * All existing users of the __GFP_NOFAIL are blockable, so warn * of any new users that actually require GFP_NOWAIT */ if (WARN_ON_ONCE_GFP(!can_direct_reclaim, gfp_mask)) goto fail; ... } ... fail: warn_alloc(gfp_mask, ac->nodemask, "page allocation failure: order:%u", order); got_pg: return page; } Unfortuantely, vpda does that nofail allocation under non-sleepable lock. A possible way to fix that is to move the pages allocation out of the lock into the caller, but having to allocate a huge number of pages and auxiliary page array seems to be problematic as well per Tetsuon: " You should implement proper error handling instead of using __GFP_NOFAIL if count can become large." So I choose another way, which does not release kernel bounce pages when user tries to register userspace bounce pages. Then we can avoid allocating in paths where failure is not expected.(e.g in the release). We pay this for more memory usage as we don't release kernel bounce pages but further optimizations could be done on top. Fixes: 6c77ed22880d ("vduse: Support using userspace pages as bounce buffer") Reviewed-by: Xie Yongji Tested-by: Xie Yongji Signed-off-by: Jason Wang [v-songbaohua@oppo.com: Refine the changelog] Signed-off-by: Barry Song --- drivers/vdpa/vdpa_user/iova_domain.c | 19 +++++++++++-------- drivers/vdpa/vdpa_user/iova_domain.h | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/iova_domain.c index 791d38d6284c..58116f89d8da 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.c +++ b/drivers/vdpa/vdpa_user/iova_domain.c @@ -162,6 +162,7 @@ static void vduse_domain_bounce(struct vduse_iova_domain *domain, enum dma_data_direction dir) { struct vduse_bounce_map *map; + struct page *page; unsigned int offset; void *addr; size_t sz; @@ -178,7 +179,10 @@ static void vduse_domain_bounce(struct vduse_iova_domain *domain, map->orig_phys == INVALID_PHYS_ADDR)) return; - addr = kmap_local_page(map->bounce_page); + page = domain->user_bounce_pages ? + map->user_bounce_page : map->bounce_page; + + addr = kmap_local_page(page); do_bounce(map->orig_phys + offset, addr + offset, sz, dir); kunmap_local(addr); size -= sz; @@ -270,9 +274,8 @@ int vduse_domain_add_user_bounce_pages(struct vduse_iova_domain *domain, memcpy_to_page(pages[i], 0, page_address(map->bounce_page), PAGE_SIZE); - __free_page(map->bounce_page); } - map->bounce_page = pages[i]; + map->user_bounce_page = pages[i]; get_page(pages[i]); } domain->user_bounce_pages = true; @@ -297,17 +300,17 @@ void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain) struct page *page = NULL; map = &domain->bounce_maps[i]; - if (WARN_ON(!map->bounce_page)) + if (WARN_ON(!map->user_bounce_page)) continue; /* Copy user page to kernel page if it's in use */ if (map->orig_phys != INVALID_PHYS_ADDR) { - page = alloc_page(GFP_ATOMIC | __GFP_NOFAIL); + page = map->bounce_page; memcpy_from_page(page_address(page), - map->bounce_page, 0, PAGE_SIZE); + map->user_bounce_page, 0, PAGE_SIZE); } - put_page(map->bounce_page); - map->bounce_page = page; + put_page(map->user_bounce_page); + map->user_bounce_page = NULL; } domain->user_bounce_pages = false; out: diff --git a/drivers/vdpa/vdpa_user/iova_domain.h b/drivers/vdpa/vdpa_user/iova_domain.h index f92f22a7267d..7f3f0928ec78 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.h +++ b/drivers/vdpa/vdpa_user/iova_domain.h @@ -21,6 +21,7 @@ struct vduse_bounce_map { struct page *bounce_page; + struct page *user_bounce_page; u64 orig_phys; }; From patchwork Sat Aug 17 06:24:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13767003 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 0B64FC531DC for ; Sat, 17 Aug 2024 06:25:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9262C6B0410; Sat, 17 Aug 2024 02:25:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8CA786B040F; Sat, 17 Aug 2024 02:25:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 792046B0410; Sat, 17 Aug 2024 02:25:25 -0400 (EDT) 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 5F4766B040E for ; Sat, 17 Aug 2024 02:25:25 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0DA9914054D for ; Sat, 17 Aug 2024 06:25:25 +0000 (UTC) X-FDA: 82460750610.09.C482145 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf07.hostedemail.com (Postfix) with ESMTP id 31FDD40014 for ; Sat, 17 Aug 2024 06:25:22 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Da+bphgy; spf=pass (imf07.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=21cnbao@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=1723875847; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=friTy/DcHRUPotFFxFEVr/8PnCHtLs9nrIthUQvrKhU=; b=jzmU7ktFxkZskkXdgY8ERc6nUZ/4xP9RysiGt5U93IGyjZWLpHWJwXj9zmFQ+QC2d22Ui5 mff+aHLBBoszYhc/o226+D8vzZDUAXJVAdYBaohX1Xiaxad9qUM8CGwSJz114PNMkVPkwI S3rilVk9g/OigQN9okfrRVBFjJNFL18= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723875847; a=rsa-sha256; cv=none; b=jqP1qVVcwa9V1F0ZOqLMEav5D5DBzc4XTXAfUtPKYohZ2DNGZ5KYaj9GbBbyRvRsGN4Q7H G3welDpMDZ9354wunj0LfaM0qLWDxjRYd8+Xau1CP2F/BKX8H3D55uTxqtW7N/hbfpEvsl bYRpCWBoSU8axPX/SoNC4dJomqCLn68= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Da+bphgy; spf=pass (imf07.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2d3bae081efso1982847a91.1 for ; Fri, 16 Aug 2024 23:25:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723875921; x=1724480721; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=friTy/DcHRUPotFFxFEVr/8PnCHtLs9nrIthUQvrKhU=; b=Da+bphgyFfhSpbYST02f5rlIJKYzKadBsV+U61qp/eB/EpU+2k+kInqPtwPTrGmpWX 0KoIwg447LBLthvmBJHkH+pCbvlH2Z4E0jUw1SldNqHlyVkm/HMFAR0+1I+P3mx0+FoH pjz3XgWtIyldzDbiv5REWieJp4He/VOjNeX0tlf+CbftMWnpPBgiWqyKwQG234DiRpMQ P7kXkUXLV8D1pwa8OxXBnRqaZt05sOxQHgPqAEIC9MDZ5mx1RfOFzSvS/G0/IH3IJ62Q YuO1/BHZTTsWMrwd22YWmdcRlNfHmCaJGs7Govi3DsGzj5z2ceNYhrPFE1p08bgy65ji CUlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723875921; x=1724480721; h=content-transfer-encoding:mime-version: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=friTy/DcHRUPotFFxFEVr/8PnCHtLs9nrIthUQvrKhU=; b=IJA6buLfzD7moPU2JpGtFwBwTj4o9NsL5NknN3lSxuK3a3anljF/u+wq04qlaoYU0I TRSCHcA4jY4f51nFHDEnjs41UeNtl4yd2+shj9opU27hD1HFzOTGxnl1pmtPV9RkjAb7 p/D1s2jTbpd/DGCZv/B2j7ogD0AzbOwurOUSyp6I+HIr4GXYY+A21z909XBhiuCFcl0D ZAyQAKSAfrZzofJHEIAC9XhBGvXWkB2mvvsJO8LKpfIC6hGlSQPaXk5qgUiTzgES04Vv hrywnwDuzj8ztAQJ2t3AkEPnij0OXssseG/bFCjna8TMo1e/gAkukNAJTTOxt3+NcqKN ShGg== X-Forwarded-Encrypted: i=1; AJvYcCUuVKJqbP04gr/S8VEtUFvjXzg0YC8R+kFRP4RgysHaSSBuDuRahzz2UdsDq5UdBqUWVVrxKorNs3e2puQAqiAL8es= X-Gm-Message-State: AOJu0YzIzRPD7eSlINOgrxTd51GrVDYRgfuRcQdcraURNUCWKYslpIbc JDIxws8cyMpdMCiClUbTrqXQwtkfPa/1Vld/F2EbaVTVySd08J90 X-Google-Smtp-Source: AGHT+IGbImbwpzZFycRBvwd+gSXhiD/E6qOTGdZAld/gNp5VVNETMnD4+Zd4mdJ5MJXaEJz/V+vFEQ== X-Received: by 2002:a17:90a:c915:b0:2c9:7e9c:9637 with SMTP id 98e67ed59e1d1-2d40556d433mr2122285a91.13.1723875920984; Fri, 16 Aug 2024 23:25:20 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:fd84:292a:c6d0:8b67]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac854f3bsm6768404a91.51.2024.08.16.23.25.12 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 16 Aug 2024 23:25:20 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 42.hyeyoo@gmail.com, cl@linux.com, hailong.liu@oppo.com, hch@infradead.org, iamjoonsoo.kim@lge.com, mhocko@suse.com, penberg@kernel.org, rientjes@google.com, roman.gushchin@linux.dev, torvalds@linux-foundation.org, urezki@gmail.com, v-songbaohua@oppo.com, vbabka@suse.cz, virtualization@lists.linux.dev, Christoph Hellwig , Davidlohr Bueso , =?utf-8?q?Eugenio_P=C3=A9rez?= , Jason Wang , Kees Cook , Lorenzo Stoakes , Maxime Coquelin , "Michael S. Tsirkin" , Xuan Zhuo Subject: [PATCH v3 2/4] mm: document __GFP_NOFAIL must be blockable Date: Sat, 17 Aug 2024 18:24:47 +1200 Message-Id: <20240817062449.21164-3-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240817062449.21164-1-21cnbao@gmail.com> References: <20240817062449.21164-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: s7m8obm8dgy7g7q55puxkxcta7ynadef X-Rspamd-Queue-Id: 31FDD40014 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1723875922-719090 X-HE-Meta: U2FsdGVkX1+IJ+9M+c1TgPlC9HYS9c7C0OUmMgmeq2Rqw/+hmVoKBbc/tdcrj0GBHvxe0ywBXQ8zPIVcdeKbiqnzWKyMLOAyqAh8Ak35nnGreAg2K1gEAyMPBCNBSRtARtKX7pI6A5Co6k/YosjBKt25xQ4ApIMuq26Q6Kh76g1XBzXCd0RPWhBEIganQKmv5RdU8QFdnL4TahKRq9vbkfE9Uk1GYLeVM5Rdql1GQOMfXFOKCRa8GK30OlNoWB7tCqUG4x3uJiK5NOP/jXPonPJBAcAN812i/gCAWFueWJg5K5CL1uGPt/IPlaHibjCsUjKUrjV5rt9kwM9h9jq7vOi3SoJLrkdEsfq3T4Gv7EaXRiEmw/1KBPWX1/5WaOJPSzYrbU305BGi1t3vXatF4Ag9rVyndA12NOT2gZKTzC9YMBWllilc+aAE6YWATbD/ol7vecgR4bX4nEFo/kU/vSJ/c5rhwru7iEmxJ+7ZaOznKj23qmSgy+mlGvm8y35bPCLZILc6JQKVTOh/lCaiPJR0Tg/R1OAposfbrREAx3t/i13IJex6qlYaA81ZpadHSAck0SM5wK/n4J+onD1EqIDx1bnYGfRTrH6jNUx3P1WhmBYe/If1s7Bb16cwlylxGWtgkakq/ZusOSTUYO8mL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oWl5wMFEwBMi/cLslkBNkxtWeFxZ82++48nX1nEDFYKb6YAe/TpuEe4squ/WPK4RaBxbcu4SsoDLMcrkFYW/YbHWQI/Tb94e9Rv4qBaTJTGwTwbFFQhHkaMZa0DyCPAD7q0FfcNC7RvhQuEXaul8Hja9RXbAeAwRcfu59YDdvia6ZDU92+D+8v2MTK4ecGF8JpYVs5RDTlPzf1uUzscnk9yW0kIoBANMUaQrgxYN0ljs9btALy+uY8Kdf7 zuwTasmy uySDF2QueCqbvG9ya/1JRimOzwcWGHcYy0D+dr6//ik= 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: From: Barry Song Non-blocking allocation with __GFP_NOFAIL is not supported and may still result in NULL pointers (if we don't return NULL, we result in busy-loop within non-sleepable contexts): static inline struct page * __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, struct alloc_context *ac) { ... /* * Make sure that __GFP_NOFAIL request doesn't leak out and make sure * we always retry */ if (gfp_mask & __GFP_NOFAIL) { /* * All existing users of the __GFP_NOFAIL are blockable, so warn * of any new users that actually require GFP_NOWAIT */ if (WARN_ON_ONCE_GFP(!can_direct_reclaim, gfp_mask)) goto fail; ... } ... fail: warn_alloc(gfp_mask, ac->nodemask, "page allocation failure: order:%u", order); got_pg: return page; } Highlight this in the documentation of __GFP_NOFAIL so that non-mm subsystems can reject any illegal usage of __GFP_NOFAIL with GFP_ATOMIC, GFP_NOWAIT, etc. Signed-off-by: Barry Song Acked-by: Michal Hocko Reviewed-by: Christoph Hellwig Acked-by: Vlastimil Babka Acked-by: Davidlohr Bueso Cc: Christoph Lameter Cc: David Rientjes Cc: "Eugenio Pérez" Cc: Hailong.Liu Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Jason Wang Cc: Joonsoo Kim Cc: Kees Cook Cc: Linus Torvalds Cc: Lorenzo Stoakes Cc: Maxime Coquelin Cc: "Michael S. Tsirkin" Cc: Pekka Enberg Cc: Roman Gushchin Cc: Uladzislau Rezki (Sony) Cc: Xuan Zhuo --- include/linux/gfp_types.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/linux/gfp_types.h b/include/linux/gfp_types.h index 313be4ad79fd..4a1fa7706b0c 100644 --- a/include/linux/gfp_types.h +++ b/include/linux/gfp_types.h @@ -215,7 +215,8 @@ enum { * the caller still has to check for failures) while costly requests try to be * not disruptive and back off even without invoking the OOM killer. * The following three modifiers might be used to override some of these - * implicit rules. + * implicit rules. Please note that all of them must be used along with + * %__GFP_DIRECT_RECLAIM flag. * * %__GFP_NORETRY: The VM implementation will try only very lightweight * memory direct reclaim to get some memory under memory pressure (thus @@ -246,6 +247,8 @@ enum { * cannot handle allocation failures. The allocation could block * indefinitely but will never return with failure. Testing for * failure is pointless. + * It _must_ be blockable and used together with __GFP_DIRECT_RECLAIM. + * It should _never_ be used in non-sleepable contexts. * New users should be evaluated carefully (and the flag should be * used only when there is no reasonable failure policy) but it is * definitely preferable to use the flag rather than opencode endless From patchwork Sat Aug 17 06:24:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13767004 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 0F9D7C531DC for ; Sat, 17 Aug 2024 06:25:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60CFC6B0411; Sat, 17 Aug 2024 02:25:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 596706B0412; Sat, 17 Aug 2024 02:25:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 436B16B0413; Sat, 17 Aug 2024 02:25:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 24E986B0411 for ; Sat, 17 Aug 2024 02:25:33 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C4B03804FD for ; Sat, 17 Aug 2024 06:25:32 +0000 (UTC) X-FDA: 82460750904.20.8135F7F Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf17.hostedemail.com (Postfix) with ESMTP id D3AFD40003 for ; Sat, 17 Aug 2024 06:25:30 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KRbCGmj3; spf=pass (imf17.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=21cnbao@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=1723875857; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=s+kupNqvjc4VbaeRrHIrzKXfEz1ha5uSEm43pSCHOkE=; b=2E8yho6iZfHpwuqFnRyjRa4fyeisrcbyfgF9e5Qpi5EQtjM3dhvkON53zhtULtwtRrI+Xx zCDm3GYzb6KW5Ew9pVhBBapxrNBOE03q0LbunZ2WWIeDLiqvxEwNj88Uf8/pls0ZK3u6X1 YfRfbHB7LYwi86hf356qvkPQLR1Mn/I= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723875857; a=rsa-sha256; cv=none; b=yqsEqCmQbe1W4vtMAsUr+IRhq1hVWK6n0h/uK4lBM9rvmStIeK+td0KHMp9Q5VbgViPYWx BN3Q/yTqzsIr67D8DtuMH9+TbNed9wWGBemuo9RVL0N8tfnCpDOaTF2YE/+JVzE9vxnh9b rKfAXB2WyQz5jTE7MfHtLS4u6n0dvII= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KRbCGmj3; spf=pass (imf17.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7a23fbb372dso1870785a12.0 for ; Fri, 16 Aug 2024 23:25:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723875929; x=1724480729; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=s+kupNqvjc4VbaeRrHIrzKXfEz1ha5uSEm43pSCHOkE=; b=KRbCGmj36etsP753QQsMTRHOrR8sUl5ntmIsJ2usUOocpfCPk867Gjzxcl25OIatKY EAFj3X4HGxu8DWUoNSXucKhvZB+0kf8VRjREcJkhrs3INyWdUJUp2ycQLP70oZ5fEKt6 fTRYmtfoR9zi9UJ5zGbM5UGKWWz+Gb7m6EXS1avfKRLy0bDnubqpjhlUhncQ647LXKBW ViGDcvIErvgP7Nwrj8VJxMrEUYBKHJi2wNpIrCT5vu433NhWEf7XEMX4eKM7il8Sf4wf yh70EGP/8x4NjoxPeHUUxMNkMva7CkBzb4Gxnr0zqFMf1P387eHZLK+zKM/ChAA0d37a oaJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723875929; x=1724480729; h=content-transfer-encoding:mime-version: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=s+kupNqvjc4VbaeRrHIrzKXfEz1ha5uSEm43pSCHOkE=; b=FA2lSKq1Q+YmM7lDzmw/kdbCdVEaIqRNqJUo8ma+Nu+dygSgg6K4O7StGbH4XEW5vA LN5cLmSDVHMNkkoIJUua9iD/OWIuHdsNN5wswaP6rQkmGqO0ShUtCr2hkCGJN5qyHK0p SF3RfPSyCBAx6wjk9t9Mg7xVswI0LKIvzZOzsIlFrhrYY0C7ovkvkQw12tw6XGuEL06w /2RFKhVroeA8/sSi0r660mDBsFpyZbL8cUK36AqG1NHkoPdgEDP72LyKdGa9xE5HeJMI 2TVeXuPszx1Vt26JY5koDftyOKbbr2km6FgHZAMRPMvnQHNywZoGwYd/GzwDGtQrxwCQ rllQ== X-Forwarded-Encrypted: i=1; AJvYcCWzBVKPRxXQio65kuAZZRU9toK25ElYKGOp/4Uj7qKRKl+jTNIXPmyKQ95zi4Nf0Z88xQOk0yzxyS1OydU2ETI1lKo= X-Gm-Message-State: AOJu0YzwMs46yMV9oKDn2tbZW3axzcWhE1xhh0M2OzwKiBeKciBiB5bN MfDEEjveo+0pVNFF4X03phpou81jCp3wLa+7CwUH4ubrszl+4+NR X-Google-Smtp-Source: AGHT+IHe4enXKrYqepTLb9GIUtBMG+QjR/xJ8dLQmWYjir/99r3pZcvKGo1qg+vP8DI56Lbbx94MwA== X-Received: by 2002:a05:6a20:9c8a:b0:1c3:ff33:277e with SMTP id adf61e73a8af0-1c904fc2c46mr6039325637.32.1723875929471; Fri, 16 Aug 2024 23:25:29 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:fd84:292a:c6d0:8b67]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac854f3bsm6768404a91.51.2024.08.16.23.25.21 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 16 Aug 2024 23:25:29 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 42.hyeyoo@gmail.com, cl@linux.com, hailong.liu@oppo.com, hch@infradead.org, iamjoonsoo.kim@lge.com, mhocko@suse.com, penberg@kernel.org, rientjes@google.com, roman.gushchin@linux.dev, torvalds@linux-foundation.org, urezki@gmail.com, v-songbaohua@oppo.com, vbabka@suse.cz, virtualization@lists.linux.dev, Christoph Hellwig , Lorenzo Stoakes , Kees Cook , =?utf-8?q?Eugenio_P=C3=A9rez?= , Jason Wang , Maxime Coquelin , "Michael S. Tsirkin" , Xuan Zhuo Subject: [PATCH v3 3/4] mm: BUG_ON to avoid NULL deference while __GFP_NOFAIL fails Date: Sat, 17 Aug 2024 18:24:48 +1200 Message-Id: <20240817062449.21164-4-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240817062449.21164-1-21cnbao@gmail.com> References: <20240817062449.21164-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D3AFD40003 X-Stat-Signature: cm5q61aihcpis5hpaxfz4x7oejna8w5r X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1723875930-25874 X-HE-Meta: U2FsdGVkX1906tR7RFQXvVejebxypbQp6grBbTJ3rsY3Kvbuk3Ws6zztP9oXo/DDl4QTwlLK8HSTwXMC0FrNMgEoMGbDYoN9ojom6waqnkKP6L5oa0GLwxl3Qklfil+TpgEvMMcrWAiCTqtSmbysmBUH+soIbNA1XaMbuuZ1IXFSJNdqeuThAT23cz6OYVd652lZqrL3ewI7fDfwEwfTB4EvXvBmxbeCOUSJAaaNAlLChzZICFGdg63IM8nbIhCuhPX5NTXD+FQrYz7CGjYNfTrbBsAazSKViIn6o27a8zgi7SyrxOMTlFnO18QUG9Vbbv+ktYMCAudS4QBzwK4WHjNFZx9Yd85hYe3IMm6OjcjnEm5Iuq0mBmuQTxoV4FZHDiDhAlgOEzR26isC/wlnC3DPKN8GxpHR+8j4NkCIreOKDbxW0qMd+Z4BHio8z5FG6OjiNXULsk1qzcMU5UAlUGBmgPc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xTVeqath4dP63GG8hb57Gdd6x6LnWO3sOM4p1HqZlYMw1qZ+vCwMimsbBRpK4BiLj6kNzS8c6bH7HQHPTzGPYOgRDDdek8coLFwL/bTE5O/hAYhn9SN0wRgwlHyCCRQhIEeofJBszwI68F17XznS7bNCB/1Z3J9MT3WVYrx9Uzq65VKU9Xy8rbfANzMQZQfme5h3LLuIG+n0rQzxQgF4vk2AzgdvtIseuvvnt7Cx7FGm6/jOAgcv2WR3DzSluMVfXoSE2TO9xwcFOAt7ZglziRcITZJJdxe+nyWJnkY8yymFxvrxGmuQX5povYZyl4SWL1HtjeqbqBrCMdyF9ZztibCTpgL+kJ5XSNqMUDv+Yqgq7T5+5+JnBRXcfn8rIwSK1lbCsEWJBy2RyEdf9NcaDhJWzRrY= 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: From: Barry Song We have cases we still fail though callers might have __GFP_NOFAIL. Since they don't check the return, we are exposed to the security risks for NULL deference. Though BUG_ON() is not encouraged by Linus, this is an unrecoverable situation. Christoph Hellwig: The whole freaking point of __GFP_NOFAIL is that callers don't handle allocation failures. So in fact a straight BUG is the right thing here. Vlastimil Babka: It's just not a recoverable situation (WARN_ON is for recoverable situations). The caller cannot handle allocation failure and at the same time asked for an impossible allocation. BUG_ON() is a guaranteed oops with stracktrace etc. We don't need to hope for the later NULL pointer dereference (which might if really unlucky happen from a different context where it's no longer obvious what lead to the allocation failing). Michal Hocko: Linus tends to be against adding new BUG() calls unless the failure is absolutely unrecoverable (e.g. corrupted data structures etc.). I am not sure how he would look at simply incorrect memory allocator usage to blow up the kernel. Now the argument could be made that those failures could cause subtle memory corruptions or even be exploitable which might be a sufficient reason to stop them early. Signed-off-by: Barry Song Reviewed-by: Christoph Hellwig Acked-by: Vlastimil Babka Acked-by: Michal Hocko Cc: Uladzislau Rezki (Sony) Cc: Lorenzo Stoakes Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Linus Torvalds Cc: Kees Cook Cc: "Eugenio Pérez" Cc: Hailong.Liu Cc: Jason Wang Cc: Maxime Coquelin Cc: "Michael S. Tsirkin" Cc: Xuan Zhuo --- include/linux/slab.h | 4 +++- mm/page_alloc.c | 4 +++- mm/util.c | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index c9cb42203183..4a4d1fdc2afe 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -827,8 +827,10 @@ kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node) { size_t bytes; - if (unlikely(check_mul_overflow(n, size, &bytes))) + if (unlikely(check_mul_overflow(n, size, &bytes))) { + BUG_ON(flags & __GFP_NOFAIL); return NULL; + } return kvmalloc_node_noprof(bytes, flags, node); } diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 60742d057b05..d2c37f8f8d09 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4668,8 +4668,10 @@ struct page *__alloc_pages_noprof(gfp_t gfp, unsigned int order, * There are several places where we assume that the order value is sane * so bail out early if the request is out of bound. */ - if (WARN_ON_ONCE_GFP(order > MAX_PAGE_ORDER, gfp)) + if (WARN_ON_ONCE_GFP(order > MAX_PAGE_ORDER, gfp)) { + BUG_ON(gfp & __GFP_NOFAIL); return NULL; + } gfp &= gfp_allowed_mask; /* diff --git a/mm/util.c b/mm/util.c index ac01925a4179..678c647b778f 100644 --- a/mm/util.c +++ b/mm/util.c @@ -667,6 +667,7 @@ void *__kvmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) /* Don't even allow crazy sizes */ if (unlikely(size > INT_MAX)) { + BUG_ON(flags & __GFP_NOFAIL); WARN_ON_ONCE(!(flags & __GFP_NOWARN)); return NULL; } From patchwork Sat Aug 17 06:24:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13767005 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 B0B33C531DC for ; Sat, 17 Aug 2024 06:25:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 411996B0169; Sat, 17 Aug 2024 02:25:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C1C76B016D; Sat, 17 Aug 2024 02:25:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 263D86B016F; Sat, 17 Aug 2024 02:25:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 03EDE6B0169 for ; Sat, 17 Aug 2024 02:25:41 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7F3F01204E5 for ; Sat, 17 Aug 2024 06:25:41 +0000 (UTC) X-FDA: 82460751282.29.8E0C192 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf19.hostedemail.com (Postfix) with ESMTP id 937801A0002 for ; Sat, 17 Aug 2024 06:25:39 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=EBusAfaJ; spf=pass (imf19.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=21cnbao@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=1723875880; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2X4VWKinXEGDbq8ESQflLvHDAModiFDrwO/8XuMbq2s=; b=PD9ixTW3kKVHKl142CLbWcjMsjZLzoAld67oEKqIbUYa3ULkPbWR7ddhSXrb+XZcNdZRQv UzLFbIFkMVEKZikGyUBzmnHN+X2EC4PNhLW1ZebTRuJ2/E1V57FC5G/6N3enGAOHMffnx/ cTLvuAf5JAkpt/XQtl+AcxF/LADSH2w= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=EBusAfaJ; spf=pass (imf19.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723875880; a=rsa-sha256; cv=none; b=RHwqyT3hGk69Pk20zhzdSCcde/9xnI09UyMqlAmIOKxUqlfCPdKOJMt2XHhPXdACvkG1WG 2d7vOcGJSGsxFsXtxIzO1N6yxctsjnJsXOF32f5T+whsl/GHkY66YJ1CMFN9ew79SSuOlV BRfEqRYdBVKPELgVz8Rkph8e4SC3HOc= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-70d399da0b5so2287748b3a.3 for ; Fri, 16 Aug 2024 23:25:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723875938; x=1724480738; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2X4VWKinXEGDbq8ESQflLvHDAModiFDrwO/8XuMbq2s=; b=EBusAfaJMNXi5uJjOGzdTPIH7vjU4eWHGP7hhVyMkFrMJnhAkql4mSBCkVSQiG+bxw 8CMpYjwVVbViHzKCGCOR4qwvFcw0RZQmLaBvkuCgIl/ELC8e6Y0ZSBiBcLIDytERlHZC JixMrKKcrlJIdPE7LkQ9HmPFOcVZ81HCtztWJ5ClvGTfZBidwMKn88t/6Kq9rWN8EqMl ZtKFijXugcbyna3DeuJdnw73I9+vbQEgMhJj3FlKJjQSa8exnzH+oxardNhJD8vnDVm9 dqzmpi4dCrhhJ/RJvFLWaubFmX7a/XwKKN0zORoiYudOiiAKwsiGmkb3EOa9Q3nP/gPu zRUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723875938; x=1724480738; h=content-transfer-encoding:mime-version: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=2X4VWKinXEGDbq8ESQflLvHDAModiFDrwO/8XuMbq2s=; b=dR1YapnJZEIPbFcvnqmp9AviP+le53AO4s3pmz6efGEzL3h39NZyfbmNI7ITJQYYuu Z3jJAwkpBszgsgZjBMARIdAymL9asKTSgZ+B7su5C22tC9fiqgSiMsxrt/9oUTIOcCtq fewS6pBGYT5WyNGBnPQ+l0uqboE3pVBbsQog6hbE7puCopkT+Kc89rd1iUzVOI4adsys PRfyAfUIQ1ciz4oXtb9sG+sKuFOsu8ZCY4SEMLT8nFgybkicClPb4NXMnJPySmZydn+H ETgDvfc0o9RwbKZ167XQBW87IcZBk4c5Mq0hnk7WkZEV1iaf3xITQkUaAC3w53PXRXMu +I9g== X-Forwarded-Encrypted: i=1; AJvYcCUjCX+H0ES7PoLEIwO3x6NzTqF8OOIyjxZNWnpUrXKGGpwHCVx8tgf/48tkrl3/WpQCLMfU0m0nkvNAj6pB+wXGIbk= X-Gm-Message-State: AOJu0YwGAzoSdMx9MZzVxSAXYwh93G1Rs5yVtMvnPGuKy0RRy+qFQl+M CDz8aFfH2db5T8kIrUwpNIF1ccVGz4u1r81tUiCnQZEzM0C7b/BI X-Google-Smtp-Source: AGHT+IH03FdwAIY4AWVv/BXRCCz7Wo0n+PGDGc1GL3fnHropqxkM8zstDmcU8MGBFc9GOZQKQEX2Iw== X-Received: by 2002:a05:6a21:2d84:b0:1c6:a680:ef3d with SMTP id adf61e73a8af0-1c904fb55c8mr7273628637.28.1723875938269; Fri, 16 Aug 2024 23:25:38 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:fd84:292a:c6d0:8b67]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac854f3bsm6768404a91.51.2024.08.16.23.25.29 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 16 Aug 2024 23:25:37 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 42.hyeyoo@gmail.com, cl@linux.com, hailong.liu@oppo.com, hch@infradead.org, iamjoonsoo.kim@lge.com, mhocko@suse.com, penberg@kernel.org, rientjes@google.com, roman.gushchin@linux.dev, torvalds@linux-foundation.org, urezki@gmail.com, v-songbaohua@oppo.com, vbabka@suse.cz, virtualization@lists.linux.dev, Lorenzo Stoakes , Kees Cook , =?utf-8?q?Eugenio_P=C3=A9rez?= , Jason Wang , Maxime Coquelin , "Michael S. Tsirkin" , Xuan Zhuo Subject: [PATCH v3 4/4] mm: prohibit NULL deference exposed for unsupported non-blockable __GFP_NOFAIL Date: Sat, 17 Aug 2024 18:24:49 +1200 Message-Id: <20240817062449.21164-5-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240817062449.21164-1-21cnbao@gmail.com> References: <20240817062449.21164-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: ktcco8kuo8xbsfjij8zytp37yt44s4j8 X-Rspam-User: X-Rspamd-Queue-Id: 937801A0002 X-Rspamd-Server: rspam02 X-HE-Tag: 1723875939-983273 X-HE-Meta: U2FsdGV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Ey3dawnebr37RfxdkDWaV0IIgLZ/O4O9SzkPKpUB9Q93x+igpiex31F6bAvLrqreEtcrjDsU80ysWWej9o2w5U5z1vC+tbQ1TZ+OyE0cOQlqDZbNOJ0hMxjffHuZqF6JLAmQFqOe+d2QvRDfkchJm0IZSsM1/EZWi1bh9utqePotZBbgMnHjjH+rBPfQEVON86S/g+55IKQyLxsl6qN2MW6bF4FHwFW0FeoFn47TWaxFb/y+QuWKDzKG4YVhU9Ochh1JZI4zh0BYereUl+78Ma/GkR6jUVaF+b9NvFO7CSOj1gfrQu6ferFoKeZ/w5iNC27ZbJZluiFbyKiY6YFLHiYCa9U1FRkGRO/8ZtsnXIG2LBWNbMsRbP98qzU8NxhPzBtaGeoOQglbWmVdFw3To1tgyI9P1SZ2CNNgsmtOQRBZ/h8FxCzfwJfHJR90U/YdfSNOzAxYqATPMBkF/OEDx02aJb4uSLMXGVO4UQagLD/5AJsIvuCVmVtDK5SfLcfGUrPFszl/RD/PQFeWG1OjYS2GeEWIPDyXb/66TZk5pHwzx7moppwzw88RIczH6PBinG8lpEKr8xg/f/VmplcI2iaxGMRDaPdr0UV62CiAStUd3SKa2TdNhc7mp53Vf/szJ44qUX305uphNqzmyNiQCBsXfUOesab1L0UVNevfaYGLTl61jkPUlrZzAPpEKs1fKCXB/9+XFjd/sZk3Z3aBlOq61N+cKUcI4VEu6L+k9CkLN35K0mondY7hOJEUuxhrlQUFsxQ5nLDmEZCen8SqeahvQHrnRjEZzOwvbBdfqfH/Qr4eZE3X6HV+N8bWhRaL4T8ow1o+iDdK5o2/vtFcnVhl4r0HsrUWqz3RhkI55kFhniNcT+OYsQi xQQElxO4 O+2/h389EMWoZlarNxduEpYbhQDfDVqDSJqzFgI9IYo= 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: From: Barry Song When users allocate memory with the __GFP_NOFAIL flag, they might incorrectly use it alongside GFP_ATOMIC, GFP_NOWAIT, etc. This kind of non-blockable __GFP_NOFAIL is not supported and is pointless. If we attempt and still fail to allocate memory for these users, we have two choices: 1. We could busy-loop and hope that some other direct reclamation or kswapd rescues the current process. However, this is unreliable and could ultimately lead to hard or soft lockups, which might not be well supported by some architectures. 2. We could use BUG_ON to trigger a reliable system crash, avoiding exposing NULL dereference. Neither option is ideal, but both are improvements over the existing code. This patch selects the second option because, with the introduction of scoped API and GFP_NOFAIL—capable of enforcing direct reclamation for nofail users(which is in my plan), non-blockable nofail allocations will no longer be possible. Signed-off-by: Barry Song Cc: Michal Hocko Cc: Uladzislau Rezki (Sony) Cc: Christoph Hellwig Cc: Lorenzo Stoakes Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Vlastimil Babka Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Linus Torvalds Cc: Kees Cook Cc: "Eugenio Pérez" Cc: Hailong.Liu Cc: Jason Wang Cc: Maxime Coquelin Cc: "Michael S. Tsirkin" Cc: Xuan Zhuo --- mm/page_alloc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index d2c37f8f8d09..fb5850ecd3ae 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4399,11 +4399,11 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, */ if (gfp_mask & __GFP_NOFAIL) { /* - * All existing users of the __GFP_NOFAIL are blockable, so warn - * of any new users that actually require GFP_NOWAIT + * All existing users of the __GFP_NOFAIL are blockable + * otherwise we introduce a busy loop with inside the page + * allocator from non-sleepable contexts */ - if (WARN_ON_ONCE_GFP(!can_direct_reclaim, gfp_mask)) - goto fail; + BUG_ON(!can_direct_reclaim); /* * PF_MEMALLOC request from this context is rather bizarre @@ -4434,7 +4434,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, cond_resched(); goto retry; } -fail: + warn_alloc(gfp_mask, ac->nodemask, "page allocation failure: order:%u", order); got_pg: