From patchwork Tue Sep 10 23:43:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13799474 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 E1741EE01F2 for ; Tue, 10 Sep 2024 23:44:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5F46C8D00CC; Tue, 10 Sep 2024 19:44:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 57D598D0002; Tue, 10 Sep 2024 19:44:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41DDA8D00CC; Tue, 10 Sep 2024 19:44:38 -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 22DB48D0002 for ; Tue, 10 Sep 2024 19:44:38 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D1FF3120DF2 for ; Tue, 10 Sep 2024 23:44:37 +0000 (UTC) X-FDA: 82550460594.14.E702139 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf10.hostedemail.com (Postfix) with ESMTP id 149BAC0002 for ; Tue, 10 Sep 2024 23:44:35 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PNZEK1HN; spf=pass (imf10.hostedemail.com: domain of 349ngZgsKCFg02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=349ngZgsKCFg02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726011772; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sKXYIyjsovK+WV8DiI7P/1dY1v3IRDe3dp7/1EItjd0=; b=7Fm3elZAF9DdWP7rT8pY1wRMipWiuyy8xLTyvQMyNRC1ZCkNX1Mp2q5VwCZGZaCmFUuP/I KX5RGhv1d03heCsdi8t1FsKSLV1kmk3RR9gL2B6DqMfuj/ORi3f8lnGp0p+606CiDfq0rb iYNaxUPpnErNfXZoRol/gfYwjzbwuDE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726011772; a=rsa-sha256; cv=none; b=RH+j1FFsTrJNsgT9T8hDZYBwWFiUXDFAwkGyMUaXtS2ocNNxFiz/jO24gWcZGtBoAS5p53 k5zOIsZ0P8JplVkPVmBr3bbmCNFpkWU4Fl21sDyqhEJkyBG5LDTMcHzntCC/mN5SJFnH1b lqZpHZxKskVglFMMXSIdXz8nMIUrxQo= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PNZEK1HN; spf=pass (imf10.hostedemail.com: domain of 349ngZgsKCFg02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=349ngZgsKCFg02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6d5fccc3548so6661567b3.1 for ; Tue, 10 Sep 2024 16:44:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726011875; x=1726616675; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sKXYIyjsovK+WV8DiI7P/1dY1v3IRDe3dp7/1EItjd0=; b=PNZEK1HNKHaMTyH9NgK5gqNmlpTi+xoWJkYHGMZq0PJxSXqgVlNzsjOrvO3r1Mp1dc Aa0kN1axRkfsDXfkzTPAg+wCa8H6RZhnomFOzVPSTka+aXAZQdFTTmzekDBGHbPgU0dX 1qj6fZEbQv70/Ni/N8T14rUo+/OomaQhXtChOIuRhUUle1trfHUSouh7z8oYnTrLj1Bh 7jX7KZaec3pLcBsVUDbx3LQqhwQonFushhFynRAd84Oy6kVeudx/H7dvusOsSjfkZLU6 CiBLgBMQ38UxIt3oDRyGaqMYNhn2b8WD/tiahf532lt+O0Swt8UnGrXyqBzh0Lnbvo91 7Xxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726011875; x=1726616675; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sKXYIyjsovK+WV8DiI7P/1dY1v3IRDe3dp7/1EItjd0=; b=RlfsJIw31oSULu/IYSMmCqYsHXOfvcHQBoxM3NsLBTsuD+6gjVScPP+wRVi2f5zT2X 57JmPi3mD0tlykRP81TAv+Axv+ZQb/0sDlvRrXPQbtF884RNrrrpiZdfszICfQC9cadB v3oCq/v9dUeroGDIxKCHF6wWSp9xHi2Tk3YwThxMpCR8VM3h7YFKMzImGyZgAQpgnA6q qe/Fai1alI0MrWGTWPVDpnVNGFibCFL3qQXeVd8DHxeSkZTwyV8/wQNOTxbza0PqXq9r 8XV66xi5D1IKQ3q1KM1HRjdr3xNfCp4Ah9IE//aWF9iWvUBYIaGngKUWX0CTGbhAUFIV rh4Q== X-Forwarded-Encrypted: i=1; AJvYcCUOM3HEayobXxzI2eTuA1PzsPItparXQaDL8Doi1EBoXFSAia5obSaXIeFli3+CrcXMoQ5gGuXplg==@kvack.org X-Gm-Message-State: AOJu0Yyh1HIGkOi9RqecjukD8K4yTAQP3CBa645sbx9p39wOVWOgYHZa 18XyS6VJt+OnP27Fdb6SdpvB+/wP3yFap7VQjgVEjag71adxPfgfu9m0B8AClIzWmv8O0XjjJYC S4wlLwvNZ5eiX542gUCWVJQ== X-Google-Smtp-Source: AGHT+IGUU7n7hwsUDrLro4Wgjoj/bFNd+sQ+f4VxxKGmgg0FjsnP+LmFWcl01bi6YDhQwKnjz58ssMXCFPvnGLCBGA== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a05:690c:20a0:b0:6db:7f4d:f79f with SMTP id 00721157ae682-6db951c4d86mr1153687b3.0.1726011875005; Tue, 10 Sep 2024 16:44:35 -0700 (PDT) Date: Tue, 10 Sep 2024 23:43:32 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: Subject: [RFC PATCH 01/39] mm: hugetlb: Simplify logic in dequeue_hugetlb_folio_vma() From: Ackerley Tng To: tabba@google.com, quic_eberman@quicinc.com, roypat@amazon.co.uk, jgg@nvidia.com, peterx@redhat.com, david@redhat.com, rientjes@google.com, fvdl@google.com, jthoughton@google.com, seanjc@google.com, pbonzini@redhat.com, zhiquan1.li@intel.com, fan.du@intel.com, jun.miao@intel.com, isaku.yamahata@intel.com, muchun.song@linux.dev, mike.kravetz@oracle.com Cc: erdemaktas@google.com, vannapurve@google.com, ackerleytng@google.com, qperret@google.com, jhubbard@nvidia.com, willy@infradead.org, shuah@kernel.org, brauner@kernel.org, bfoster@redhat.com, kent.overstreet@linux.dev, pvorel@suse.cz, rppt@kernel.org, richard.weiyang@gmail.com, anup@brainfault.org, haibo1.xu@intel.com, ajones@ventanamicro.com, vkuznets@redhat.com, maciej.wieczor-retman@intel.com, pgonda@google.com, oliver.upton@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-fsdevel@kvack.org X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 149BAC0002 X-Stat-Signature: 6b3cdyfu4tmn9uyaq54ow5nw9xexk69u X-HE-Tag: 1726011875-956836 X-HE-Meta: U2FsdGVkX1/71UODBADkVHCOfeBSVZsLRasoNjboXvn3XH6Pi4cCal5bGYaNDGHIyukRmyUPRpzNw+yC2Bo7QtHDb3ylieNAReip2OLywgSYzFTDm7ninzYtCodjHNPzV2r9coACKU6AzIcG6euHo0szVRHzCVaj9g5OYrw2yERC6MF8musxEKyikDPJP7KYA9TagnUPvMdqCWJDGYeiDq0XmzP96UNXE+p5V9z8DIF+8f0T4K5oFMKMweZ99yd2hteBoG3e5OoEZ0teYYHGUJdibmoDB+4WGhmTY8orHfqiGpmxip5JuwSDqFTT4XXc1HQQRnc7YM5mOSQj3FuAcBGXXLjuNvhYJacOWXc44NlYc0F7EJ0dzWexGj/Iz1UXsXB4+7tpMmmrF5teXSBorworNC9xCOCS7Nyl1aILZhuc082qvTR+T8CeOmFgKC9BpUG0AIw9Cs+VkzaI7y0EhEIqzUvyQxx/+cXxC/u7dbjnmSTh26yqB0w+CIWVDYOrt5BwkaDzkL3z2eYP7XVnXowaL1T0iDUxPTr/qIFrwiRmLWr1JgRHWrIAQ5J6mZGJCf49/L1Pgs9Hsr4MW9iPbK2/n04ibw8Yup7/S50IdVMyqnb+OJadR3NHGsXSyVKDwOL27vgq+GWKyjk6sWaiuP1++2eMxHiGUu0cP1CQTiOTSalamjVXVLvF1aeAKR2vgEjAGSj1XeKUaKEDAkCUmJ97T2x2+IzyP8vw5Fg0kpSJN/oUO+FAZ2WXHJC6ApHcs+w3toPz6oz4BQreIBDjY2gSAEE8NOZj+3kt1jbAPspeeklB/moLV3ZSDaiXZJ5S3Zxp/s/G6OFaEndcavP8TFSUQxkv3N7GY6CMbOQBsNesP7DypcwVRG5jqGYj/NQcJoaEbsDwNz1xOQjHbgxqzUoA1FJrBUkIMCWcGOVRDnuZzDvISnn3nF757A1OWjyFMnnHT1XI3dMBHHYtdUM XM22uvRQ GE1gclt7HNpfP0cE8kEk6xPFX+0uAM69fo1B7Dl3N4289T9m9wj40pImD2FBbBRU4kLrrGMPmosRyx6qlwxAycjuTfkIjHBlTwNnFvZ8oP0YDXp2uEHjz9Lw4+oFPbKjgHTrLYYycSBG6dXhnxI5B2raCSki/xjoxJqgDLE8kUgYG0BKHjmQSCdijjFgDdukwFNFY14yYXjWMx4zWcNU/CBTgM2VAyXtm+8NGvTfoor/e429zsMNhcpto0k24BIe42/BPlOFcU+OvwXL7GHFKkiINqK1Gmg7jtMGMJzRiB5Tlojm87uRs1hOEyCDB/Il1ueaoddx1gF6doJrRVugaSDwitg3vNnC9CO2YZnJAfaEPxKkDexh5phwI+mEQJiIxtHB05Vz981VMlmtXyA6NuOsJL1vAXPWDQEqtJTSjymc98FSyEXYadnxWwkELLL6nCYb5/tdYbSHP+XDHX3gQgkZGohEwtqVzi+PSX7VBfigNmmrkTNH+9nDiEKj7RfEkYWNnIqkEzb9Ha0O9SW3Vg9uoEk4azjWwdLwJPiFLfzO+0/IDNRGqLClUiZ4wjTUWe0KgU2jcfsLG5sLEamhexDBHGJZtkysoKGADAG4h2wgQXHLhf4YncjVxF8bRg1yTpzim1UbvsY7fXicUNr3qNE7eWt8zr3gTIYfHgq+iTDyp5+fi4JSftTxVeTDo8DjB2Cv/FAqBXAyYfb8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000036, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Replace arguments avoid_reserve and chg in dequeue_hugetlb_folio_vma() so dequeue_hugetlb_folio_vma() is more understandable. The new argument, use_hstate_resv, indicates whether the folio to be dequeued should be taken from reservations in hstate. If use_hstate_resv is true, the folio to be dequeued should be taken from reservations in hstate and hence h->resv_huge_pages is decremented, and the folio is marked so that the reservation is restored. If use_hstate_resv is false, then a folio needs to be taken from the pool and hence there must exist available_huge_pages(h), failing which, goto err. The bool use_hstate_resv can be reused within dequeue_hugetlb_folio_vma()'s caller, alloc_hugetlb_folio(). No functional changes are intended. As proof, the original two if conditions !vma_has_reserves(vma, chg) && !available_huge_pages(h) and avoid_reserve && !available_huge_pages(h) can be combined into (avoid_reserve || !vma_has_reserves(vma, chg)) && !available_huge_pages(h). Applying de Morgan's theorem on avoid_reserve || !vma_has_reserves(vma, chg) yields !avoid_reserve && vma_has_reserves(vma, chg), hence the simplification is correct. Signed-off-by: Ackerley Tng --- mm/hugetlb.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index aaf508be0a2b..af5c6bbc9ff0 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1280,8 +1280,9 @@ static bool vma_has_reserves(struct vm_area_struct *vma, long chg) } /* - * Only the process that called mmap() has reserves for - * private mappings. + * Only the process that called mmap() has reserves for private + * mappings. A child process with MAP_PRIVATE mappings created by their + * parent have no page reserves. */ if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { /* @@ -1393,8 +1394,7 @@ static unsigned long available_huge_pages(struct hstate *h) static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h, struct vm_area_struct *vma, - unsigned long address, int avoid_reserve, - long chg) + unsigned long address, bool use_hstate_resv) { struct folio *folio = NULL; struct mempolicy *mpol; @@ -1402,16 +1402,7 @@ static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h, nodemask_t *nodemask; int nid; - /* - * A child process with MAP_PRIVATE mappings created by their parent - * have no page reserves. This check ensures that reservations are - * not "stolen". The child may still get SIGKILLed - */ - if (!vma_has_reserves(vma, chg) && !available_huge_pages(h)) - goto err; - - /* If reserves cannot be used, ensure enough pages are in the pool */ - if (avoid_reserve && !available_huge_pages(h)) + if (!use_hstate_resv && !available_huge_pages(h)) goto err; gfp_mask = htlb_alloc_mask(h); @@ -1429,7 +1420,7 @@ static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h, folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, nid, nodemask); - if (folio && !avoid_reserve && vma_has_reserves(vma, chg)) { + if (folio && use_hstate_resv) { folio_set_hugetlb_restore_reserve(folio); h->resv_huge_pages--; } @@ -3130,6 +3121,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, struct mem_cgroup *memcg; bool deferred_reserve; gfp_t gfp = htlb_alloc_mask(h) | __GFP_RETRY_MAYFAIL; + bool use_hstate_resv; memcg = get_mem_cgroup_from_current(); memcg_charge_ret = mem_cgroup_hugetlb_try_charge(memcg, gfp, nr_pages); @@ -3190,20 +3182,17 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, if (ret) goto out_uncharge_cgroup_reservation; + use_hstate_resv = !avoid_reserve && vma_has_reserves(vma, gbl_chg); + spin_lock_irq(&hugetlb_lock); - /* - * glb_chg is passed to indicate whether or not a page must be taken - * from the global free pool (global change). gbl_chg == 0 indicates - * a reservation exists for the allocation. - */ - folio = dequeue_hugetlb_folio_vma(h, vma, addr, avoid_reserve, gbl_chg); + folio = dequeue_hugetlb_folio_vma(h, vma, addr, use_hstate_resv); if (!folio) { spin_unlock_irq(&hugetlb_lock); folio = alloc_buddy_hugetlb_folio_with_mpol(h, vma, addr); if (!folio) goto out_uncharge_cgroup; spin_lock_irq(&hugetlb_lock); - if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) { + if (use_hstate_resv) { folio_set_hugetlb_restore_reserve(folio); h->resv_huge_pages--; }