From patchwork Wed Jul 20 21:53:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12924536 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 1AA92C43334 for ; Wed, 20 Jul 2022 21:53:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4EC0F6B0072; Wed, 20 Jul 2022 17:53:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 499816B0073; Wed, 20 Jul 2022 17:53:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 360FE6B0074; Wed, 20 Jul 2022 17:53:25 -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 22B866B0072 for ; Wed, 20 Jul 2022 17:53:25 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E6982C074D for ; Wed, 20 Jul 2022 21:53:24 +0000 (UTC) X-FDA: 79708829928.14.C043FF1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 6816D18000F for ; Wed, 20 Jul 2022 21:53:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658354003; h=from:from: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; bh=i3Va9g3YkA6BXU/AcK1jCkeA4AOd/o3VNIc0Hlti7UU=; b=QPNMGZ+UuzKR503yII+xhtedlUCzC/4AgrYb/dARcLV18dv1A8jDj7dgkxQgiqjFsacNAJ +tbtot02fOTgH5lM9hlKEJS8wPqV0HN4KdTtAPaEGFWhuQn9YKZWTzzDebLHSraCI7G3rz /O3YnivW32gzU9BOtVfKrSc193dAZcw= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-290-BkleHJxbNBuWWCEstHnecw-1; Wed, 20 Jul 2022 17:53:23 -0400 X-MC-Unique: BkleHJxbNBuWWCEstHnecw-1 Received: by mail-qt1-f199.google.com with SMTP id x16-20020ac85f10000000b0031d3262f264so7883qta.22 for ; Wed, 20 Jul 2022 14:53:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=i3Va9g3YkA6BXU/AcK1jCkeA4AOd/o3VNIc0Hlti7UU=; b=m30aE3FBUxbhiLIaIhcF9mvAVPVJcdPlLdDz9eiC1irlzXPw+euE34FqrcCP7Jk5ux MCHUQXtdBDHKGP9WgaijqAtuiIj0EBLWbNEB+cd5eicNtDStzxeQLqZ92sOEyWq+dKpB SlVrFnBazFHV06dreFFaGKviAsYXI3bpL+/gi8dEOW9nb3YfROUK0VNtary5BDwiQL/s 78FzwV502DG7n79kIgGb8ieDewuuGstSMDM9r51IURXp3uB9tCCLHL+TNcHnrmf338Md H5XCvUH4fJBcg47bHh0OHuASOdnxyskj4dfBSK5LcDE3zG9eQ/9WTcrb73MFceR+lfz/ /nyw== X-Gm-Message-State: AJIora/Xrgd6HW//9HvFFBV5ArXmovc8dMsMpEh8GQerQt1kAEe8PFkh mZZmCOoFYOS5MJOy7fIcr+SXU+diqJsmhSFZWRadzqy9w5E3KerW8F4PWL8JI7/QkDzfWgX3ZWp gfPGhOC58yP6/NI4sbE0ZbpuJMCgUummWd+Bl63A9lV9tMymMExuq797LIPGW X-Received: by 2002:a05:6214:5292:b0:473:3f82:f947 with SMTP id kj18-20020a056214529200b004733f82f947mr31973964qvb.72.1658354002235; Wed, 20 Jul 2022 14:53:22 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v3iNU023iwalNqWReGAA6yDZlG8JijiYsICQZScSUsRWUvYcY4qSe5hJAq2luceAqy0IeOMA== X-Received: by 2002:a05:6214:5292:b0:473:3f82:f947 with SMTP id kj18-20020a056214529200b004733f82f947mr31973941qvb.72.1658354001933; Wed, 20 Jul 2022 14:53:21 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-37-74-12-30-48.dsl.bell.ca. [74.12.30.48]) by smtp.gmail.com with ESMTPSA id x27-20020a05620a0b5b00b006b5e43466ebsm199642qkg.59.2022.07.20.14.53.20 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 20 Jul 2022 14:53:21 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, David Hildenbrand , Nadav Amit , Andrea Arcangeli , Andrew Morton Subject: [PATCH] mm/mprotect: Fix soft-dirty check in can_change_pte_writable() Date: Wed, 20 Jul 2022 17:53:19 -0400 Message-Id: <20220720215319.87839-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QPNMGZ+U; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf24.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658354004; 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: references:dkim-signature; bh=i3Va9g3YkA6BXU/AcK1jCkeA4AOd/o3VNIc0Hlti7UU=; b=0JuGIVYCDrgFAZvOEDC6Lhw/SxVsFAG1tYOwGczN4zmjAebWDIOv8gHajhXLNVJKDY/C5K EgkwuYQMzw52WxbRGmvEOU0+3seP4mnLwG8c6pjQzPQgh4alHi89RgvcphRGIF92fkcxGb HVMdHRwPAOEGtMX8R0WCDindF0pkkVs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658354004; a=rsa-sha256; cv=none; b=BIrWLx0+g729JbXcY+i4UjPKNXkzhMfhdOxTfmGWasXExbq0QzWVNXAIkSOBWl0V0G2/4L KV5JbSZb/pstFrivR9l2sZDzD1+/f4+0ONpJnph4KiMlmHhCPyickazk+3YHwVuY0NRhya RDpwsbumeKLy3G/yb4brYhWBX0Jw4S8= X-Rspamd-Queue-Id: 6816D18000F Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QPNMGZ+U; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf24.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: hxhau3k46x47ek7uiiz1uttr83776pmq X-HE-Tag: 1658354004-333552 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: The check wanted to make sure when soft-dirty tracking is enabled we won't grant write bit by accident, as a page fault is needed for dirty tracking. The intention is correct but we didn't check it right because VM_SOFTDIRTY set actually means soft-dirty tracking disabled. Fix it. It wasn't a bug for a long time because we used to only optimize the write bit settings in change_pte_range() for page caches, and since we've got a higher level check in vma_wants_writenotify(), we will never set the bit MM_CP_TRY_CHANGE_WRITABLE for soft-dirty enabled page caches, hence even if we checked with the wrong value of VM_SOFTDIRTY in change_pte_range() it'll just be an no-op. Functionally it was still correct, even if cpu cycles wasted. However after the recent work of anonymous page optimization on exclusive pages we'll start to make it wrong because anonymous page does not require the check in vma_wants_writenotify() hence it'll suffer from the wrong check here in can_change_pte_writable(). We can easily verify this with any exclusive anonymous page, like program below: =======8<====== unsigned int psize; char *page; uint64_t pagemap_read_vaddr(int fd, void *vaddr) { uint64_t value; int ret; ret = pread(fd, &value, sizeof(uint64_t), ((uint64_t)vaddr >> 12) * sizeof(uint64_t)); assert(ret == sizeof(uint64_t)); return value; } void clear_refs_write(void) { int fd = open("/proc/self/clear_refs", O_RDWR); assert(fd >= 0); write(fd, "4", 2); close(fd); } bool dirty = pagemap_read_vaddr(fd, page) & PM_SOFT_DIRTY; \ if (dirty != expect) { \ printf("ERROR: %s, soft-dirty=%d (expect: %d)\n", str, dirty, expect); \ exit(-1); \ } \ } while (0) int main(void) { int fd = open("/proc/self/pagemap", O_RDONLY); assert(fd >= 0); psize = getpagesize(); page = mmap(NULL, psize, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); assert(page != MAP_FAILED); *page = 1; check_soft_dirty("Just faulted in page", 1); clear_refs_write(); check_soft_dirty("Clear_refs written", 0); mprotect(page, psize, PROT_READ); check_soft_dirty("Marked RO", 0); mprotect(page, psize, PROT_READ|PROT_WRITE); check_soft_dirty("Marked RW", 0); *page = 2; check_soft_dirty("Wrote page again", 1); munmap(page, psize); close(fd); printf("Test passed.\n"); return 0; } =======8<====== So even if commit 64fe24a3e05e kept the old behavior and didn't attempt to change the behavior here, the bug will only be able to be triggered after commit 64fe24a3e05e because only anonymous page will suffer from it. Fixes: 64fe24a3e05e ("mm/mprotect: try avoiding write faults for exclusive anonymous pages when changing protection") Signed-off-by: Peter Xu --- mm/mprotect.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 0420c3ed936c..804807ab14e6 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -48,8 +48,11 @@ static inline bool can_change_pte_writable(struct vm_area_struct *vma, if (pte_protnone(pte) || !pte_dirty(pte)) return false; - /* Do we need write faults for softdirty tracking? */ - if ((vma->vm_flags & VM_SOFTDIRTY) && !pte_soft_dirty(pte)) + /* + * Do we need write faults for softdirty tracking? Note, + * soft-dirty is enabled when !VM_SOFTDIRTY. + */ + if (!(vma->vm_flags & VM_SOFTDIRTY) && !pte_soft_dirty(pte)) return false; /* Do we need write faults for uffd-wp tracking? */