Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4436225rwb; Mon, 21 Nov 2022 07:36:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf57NJ3jmwNckDDSkNw97geWLaLSF78YfIFsM/e2oq3j/lnDNvV6uPZ6mxvKxW1Qn51JmaT8 X-Received: by 2002:a63:1824:0:b0:46e:baf4:ab7a with SMTP id y36-20020a631824000000b0046ebaf4ab7amr19055506pgl.37.1669044960323; Mon, 21 Nov 2022 07:36:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669044960; cv=none; d=google.com; s=arc-20160816; b=b7mcfWEhJuFQ1G7gjNS0LQI4cwCVMFwTrFctpxssfc/mcGVb2C03t2JxDUAI8zwCNa NSNAlnflF0xWa9GT/6Ck5prl7o6NCuWBnaPqFcP7l/W8MowttTngaP7PimVVpTFmO+hv 17Q9urwhTqkMnHygtFN9aYs9h9DLQAh9m2mnwiGOO7BvX/g6gGoZG3lL5lGCistkKim6 rtXSGasjo0gNT7zEVJrmjiAvsKpJShtsJNDKRYARiYufXYrjwafXWzrkfeVVBo4hJwub Xpd5Oi4nSZQI38PZKJXLAoS2A8MoAGwTZPsbXepVwhyPwSFfyeZawUHi11O46AeOVjkk xkyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:to:content-language:subject:cc:user-agent:mime-version :date:message-id:dkim-signature; bh=eF3fbrL7T17ZRmAQxpB5H4Gx31IIJJ5yHy2ozf6baL8=; b=P95goH9yC6ZEfrTbJuolZxYDlv/SGpJBFiPuwBDqc3qlHUH+/QNs/IUjb3WFROQ2qz eAVZmOi29+TVfhJtCYxeY+ZL5evv6mJElXyxqiTdLYRvbegmszXcvrLUJMENFPjEdiKK c+QwvZE+qDvsfkDzaJcAKppwVNHzDwLL70x99qZMITpU+aoEKutuRslgJAoAcAxSuU9O uMqahQG44SxzJRuqCFn4QujhXMflLtB68RIN8NWvAMeMvxBfVtqqNmAZe1ByvAp8Ge5O e8EAQIw8CNUm1un/mCC9SeTOFtP3jb3b8l0KAksKBfW0G9tpyxX5bmvMercVsYI9f5x2 Q2Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=WTcL3k3D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c7-20020a170903234700b001869b2c5f47si12709220plh.326.2022.11.21.07.35.47; Mon, 21 Nov 2022 07:36:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=WTcL3k3D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231982AbiKUPIH (ORCPT + 92 others); Mon, 21 Nov 2022 10:08:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231946AbiKUPHg (ORCPT ); Mon, 21 Nov 2022 10:07:36 -0500 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF8E0630C for ; Mon, 21 Nov 2022 06:57:16 -0800 (PST) Received: from [192.168.10.9] (unknown [39.45.241.105]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: usama.anjum) by madras.collabora.co.uk (Postfix) with ESMTPSA id 8623666029A5; Mon, 21 Nov 2022 14:57:12 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1669042634; bh=SUxqx2ct+YMAWPyZQcYXUyYo4xyFn2Xq74LW6CRW/UM=; h=Date:Cc:Subject:To:References:From:In-Reply-To:From; b=WTcL3k3D5NZH/YqSRA58tsyvHXmhFUvWvjOma9SFxR7EY66rMcNldGyBxuf7n/zJn 3z+c3pzeNNzVR/KgEanB+kUDKkOxZNT75be7l/tIC+rll7bGtgasBNVsXaqbf27bWv 6ZM77e6euue8FNuIswWMKIhB1p/ZzqPMn0t4qxVeXMIkzmHRZDI5xo52IZh+TOzG3r Fg57qfv7A01YqWa70ND+sgjXuj4i0RKHuuVMLjN0tQri2iNkpSlyepKpp1K6Ap7QnA b7tEqaWkCX/R2scWx8OBUlqvfYYcNQEiInNEKu4Nkw23LUaieEAaUbtSU32jJO2erB zHI6ouppmNb6Q== Message-ID: Date: Mon, 21 Nov 2022 19:57:05 +0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Cc: Muhammad Usama Anjum , David Hildenbrand , Nadav Amit , Andrea Arcangeli , Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v4 1/3] mm/mprotect: Fix soft-dirty check in can_change_pte_writable() Content-Language: en-US To: Peter Xu References: <20220725142048.30450-1-peterx@redhat.com> <20220725142048.30450-2-peterx@redhat.com> From: Muhammad Usama Anjum In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Peter, Thank you so much for replying. On 11/19/22 4:14 AM, Peter Xu wrote: > On Sat, Nov 19, 2022 at 01:16:26AM +0500, Muhammad Usama Anjum wrote: >> Hi Peter and David, > > Hi, Muhammad, > >> >> On 7/25/22 7:20 PM, Peter Xu wrote: >>> 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. >> [...] >>> +static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) >>> +{ >>> + /* >>> + * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty >>> + * enablements, because when without soft-dirty being compiled in, >>> + * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY) >>> + * will be constantly true. >>> + */ >>> + if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)) >>> + return false; >>> + >>> + /* >>> + * Soft-dirty is kind of special: its tracking is enabled when the >>> + * vma flags not set. >>> + */ >>> + return !(vma->vm_flags & VM_SOFTDIRTY); >>> +} >> I'm sorry. I'm unable to understand the inversion here. >>> its tracking is enabled when the vma flags not set. >> VM_SOFTDIRTY is set on the VMA when new VMA is allocated to mark is >> soft-dirty. When we write to clear_refs to clear soft-dirty bit, >> VM_SOFTDIRTY is cleared from the VMA as well. Then why do you say tracking >> is enabled when the vma flags not set? > > Because only when 4>clear_refs happens would VM_SOFTDIRTY be cleared, and > only until then the real tracking starts (by removing write bits on ptes). But even if the VM_SOFTDIRTY is set on the VMA, the individual pages are still marked soft-dirty. Both are independent. It means tracking is enabled all the time in individual pages. Only the soft-dirty bit status in individual page isn't significant if VM_SOFTDIRTY already is set. Right? > >> I'm missing some obvious thing. Maybe the meaning of tracking is to see >> if VM_SOFTDIRTY needs to be set. If VM_SOFTDIRTY is already set, tracking >> isn't needed. Can you give an example here? > > If VM_SOFTDIRTY is set, pagemap will treat all pages as soft-dirty, please > see pagemap_pmd_range(): > > if (vma->vm_flags & VM_SOFTDIRTY) > flags |= PM_SOFT_DIRTY; > > So fundamentally it reports nothing useful when VM_SOFTDIRTY set. That's > also why we need the clear_refs first before we can have anything useful. > > Feel free to reference to the doc page (admin-guide/mm/soft-dirty.rst): > > ---8<--- > The soft-dirty is a bit on a PTE which helps to track which pages a task > writes to. In order to do this tracking one should > > 1. Clear soft-dirty bits from the task's PTEs. > > This is done by writing "4" into the ``/proc/PID/clear_refs`` file of the > task in question. > > 2. Wait some time. > > 3. Read soft-dirty bits from the PTEs. > > This is done by reading from the ``/proc/PID/pagemap``. The bit 55 of the > 64-bit qword is the soft-dirty one. If set, the respective PTE was > written to since step 1. > ---8<--- > > The tracking starts at step 1, where is when the flag is cleared. > > Thanks, > -- BR, Muhammad Usama Anjum