From patchwork Thu Apr 1 18:21:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 12179163 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1563C433B4 for ; Thu, 1 Apr 2021 18:21:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 63BB260240 for ; Thu, 1 Apr 2021 18:21:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 63BB260240 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 08E716B0078; Thu, 1 Apr 2021 14:21:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0659F6B007E; Thu, 1 Apr 2021 14:21:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFA6D6B0080; Thu, 1 Apr 2021 14:21:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0232.hostedemail.com [216.40.44.232]) by kanga.kvack.org (Postfix) with ESMTP id BDB4E6B0078 for ; Thu, 1 Apr 2021 14:21:33 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 80F7EC5AC for ; Thu, 1 Apr 2021 18:21:33 +0000 (UTC) X-FDA: 77984616066.08.2884431 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) by imf06.hostedemail.com (Postfix) with ESMTP id 6E9AAC0007C0 for ; Thu, 1 Apr 2021 18:21:33 +0000 (UTC) Received: by mail-qt1-f201.google.com with SMTP id w2so3606393qts.18 for ; Thu, 01 Apr 2021 11:21:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=7FFTxJfhBub0ermj70tsntXQaF5NxHdv1LLLlAgh3Tg=; b=IvjDKg2QrBzk4nuwOJe7alSzTfjXQP4b7KooEke+eiaaPZYUwJz1yuqQH5tTDFEK98 7Lk7/uotQuA8WyQRHVZilIOzw6ATk5kIoJ1FMiDxVUWxXOo3lA5bkZ4nsXxDQzVFsb6I wQhRBt2hS9Y68Z/VRvYd4JI0ecn1ecAgTx2aT51YYHfrUeGCT4o3Vdi3jgTB0NTGksYH Ag5biBiLOCYWQEf42Fjbyn8pmn5S1XUm0OEFDO56SyluU3/SAdNQKqcz6FZKg12/s5jO hzYsgH8HQUmWqtuOIg96pFQdNKziF+3zTtd959cnOlMDo9R7W+V7/eGk9S+ly3+hVUP8 m61A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=7FFTxJfhBub0ermj70tsntXQaF5NxHdv1LLLlAgh3Tg=; b=L9LjeOUHmDwVSadk1X+SblhUkm2mfEWZI3uYKT7o3GCuVXOMIjT6ELIEDOv7Y/MrRp XDFwEmfupAtb3RsqM93ArpQoIxYRQnIWGFckThREwDqDX1CvedGFz5dC1i3yQM+0ov0C o5q7d4K+CcdI1bj/FGYri+JsD9RYQT2ffbGEwl3Qdc4NM5QJrKBDdfSwZagnqRcljeSN wQPIMdEAe0NCeTJhJr+Nf4yMlQUivfVCpPNJeHb3YsiZEETbJBQyCURVKAW9oCwb20Ha ReCTF8d5MlmwC/YLUXT7YsFH2Temsv/2iuuq6XSd7DnSiDWdu/T5FfjZKC3yL7grtqgU Di3g== X-Gm-Message-State: AOAM5307zgNOroMW06PQ901l/0ad7bkWI5pKbUMU+lYYnLqKQUnEYJMy tYG5RDwH6hRfg5P7J138Vq8wF9c6pt0= X-Google-Smtp-Source: ABdhPJxh3T2CByrPxU0x0VwYvdWMkwXm0bV1SkaFoboTivfYvAc2IT1vS+/1Q743N1AN5Z/g9DCa/yCPEVI= X-Received: from surenb1.mtv.corp.google.com ([2620:15c:211:200:899:1066:21fc:b3c5]) (user=surenb job=sendgmr) by 2002:a0c:fecd:: with SMTP id z13mr9375738qvs.43.1617301292340; Thu, 01 Apr 2021 11:21:32 -0700 (PDT) Date: Thu, 1 Apr 2021 11:21:22 -0700 In-Reply-To: <20210401182125.171484-1-surenb@google.com> Message-Id: <20210401182125.171484-3-surenb@google.com> Mime-Version: 1.0 References: <20210401182125.171484-1-surenb@google.com> X-Mailer: git-send-email 2.31.0.291.g576ba9dcdaf-goog Subject: [PATCH 2/5] mm: do_wp_page() simplification From: Suren Baghdasaryan To: stable@vger.kernel.org Cc: gregkh@linuxfoundation.org, jannh@google.com, ktkhai@virtuozzo.com, torvalds@linux-foundation.org, shli@fb.com, namit@vmware.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com, Peter Xu X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 6E9AAC0007C0 X-Stat-Signature: ebxh5i9bo3oqqsb459p9jmidrpqq3p7f Received-SPF: none (flex--surenb.bounces.google.com>: No applicable sender policy available) receiver=imf06; identity=mailfrom; envelope-from="<3LA9mYAYKCOsfheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com>"; helo=mail-qt1-f201.google.com; client-ip=209.85.160.201 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617301293-469854 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: From: Linus Torvalds How about we just make sure we're the only possible valid user fo the page before we bother to reuse it? Simplify, simplify, simplify. And get rid of the nasty serialization on the page lock at the same time. [peterx: add subject prefix] Signed-off-by: Linus Torvalds Signed-off-by: Peter Xu Signed-off-by: Linus Torvalds --- mm/memory.c | 58 ++++++++++++++++------------------------------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 3874acce1472..d95a4573a273 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2847,49 +2847,25 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) * not dirty accountable. */ if (PageAnon(vmf->page)) { - int total_map_swapcount; - if (PageKsm(vmf->page) && (PageSwapCache(vmf->page) || - page_count(vmf->page) != 1)) + struct page *page = vmf->page; + + /* PageKsm() doesn't necessarily raise the page refcount */ + if (PageKsm(page) || page_count(page) != 1) + goto copy; + if (!trylock_page(page)) + goto copy; + if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) { + unlock_page(page); goto copy; - if (!trylock_page(vmf->page)) { - get_page(vmf->page); - pte_unmap_unlock(vmf->pte, vmf->ptl); - lock_page(vmf->page); - vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, - vmf->address, &vmf->ptl); - if (!pte_same(*vmf->pte, vmf->orig_pte)) { - unlock_page(vmf->page); - pte_unmap_unlock(vmf->pte, vmf->ptl); - put_page(vmf->page); - return 0; - } - put_page(vmf->page); - } - if (PageKsm(vmf->page)) { - bool reused = reuse_ksm_page(vmf->page, vmf->vma, - vmf->address); - unlock_page(vmf->page); - if (!reused) - goto copy; - wp_page_reuse(vmf); - return VM_FAULT_WRITE; - } - if (reuse_swap_page(vmf->page, &total_map_swapcount)) { - if (total_map_swapcount == 1) { - /* - * The page is all ours. Move it to - * our anon_vma so the rmap code will - * not search our parent or siblings. - * Protected against the rmap code by - * the page lock. - */ - page_move_anon_rmap(vmf->page, vma); - } - unlock_page(vmf->page); - wp_page_reuse(vmf); - return VM_FAULT_WRITE; } - unlock_page(vmf->page); + /* + * Ok, we've got the only map reference, and the only + * page count reference, and the page is locked, + * it's dark out, and we're wearing sunglasses. Hit it. + */ + wp_page_reuse(vmf); + unlock_page(page); + return VM_FAULT_WRITE; } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == (VM_WRITE|VM_SHARED))) { return wp_page_shared(vmf);