Received: by 2002:ab2:1347:0:b0:1f4:ac9d:b246 with SMTP id g7csp425876lqg; Thu, 11 Apr 2024 07:12:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV6ulkT+aYF9zRaIa9J152iRfp1OFcJrcppK58NI2WAzfMcihAQg0ZjpN3TLhhY6H6e08b7G3cDyj+p8gx8wyLVDYMVzWr/P/SRL1I7Xg== X-Google-Smtp-Source: AGHT+IEnnmxMwjUCF47AYKc+KT7f2aI70W71youVGP57FzjqFtpt7u6vldmeSTGucRSA73LaD4/O X-Received: by 2002:a05:6a21:3288:b0:1a3:3260:7555 with SMTP id yt8-20020a056a21328800b001a332607555mr7017543pzb.11.1712844741011; Thu, 11 Apr 2024 07:12:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712844740; cv=pass; d=google.com; s=arc-20160816; b=M1hQa/oIse4dFU95qnWAiKy0XEFi2Qp1kxCIcn2Foyorectkh4EsGUYquID2hkUooI fM3VhB9y3Amb5TfC6UWwTt8ZDfF3DSry9K+f6VFGWB9W0x5eLkrwaMs1XDrC2q8D/12S r9Qnw1Q0d+Jnq2S7zk6ALRaFeqXgka6dQK3jT19OR1/RZvFERVahpXA1yrLP7z7RTU3z acxvUZtsa0QzTWTlbrWcBjxp+syeh6pXxuuX/QJdRO8aUdw4FIdBa2zC3e9bLQOl779S Cnpxio93lCgxxhNlbCI/nFYgAdk4FEl37RPOA12TcRDAsNUamlJeweG5IU/8YdPVOOww 0YSQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=8EmSHNru08Qgpd+bZkkxBxH75B40CEqU79I1xOw9buk=; fh=gmfk4PoPiDC8zIc55sAc/yephYkjEE2WIEquIloK2xI=; b=ZS8JGluuCd6BVNUW3GVHGBcQvRASPyfaVHCjspxzZFXwMhHph9ARHTDZknnoAV+wHP zyJY5e1LNw5WGpnnecWkwTqhGXG4cDKVyjDshWNpv+fp0KvgrcOeXGBvYG7XaNH9lMs4 /6244LPRGiPh7uYZU1jWpfb6xVEEO5/PqGJki5Fd7/u3RVK5yIGDwgZ9PJt81+z8CP50 QXcl6KUT7j9FTti09xLJiWJQ4mhBcWtbfdaSJvdBmKhewgSUsIPKEB5UXPZy/ZZJYrc0 dyNktYSsY21DMHg+Ck6SRF/HBGXD/yFbbu7j5CSHoTgZ+KD13LV/y9Zl6JCPCc6nyrEk srcg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-140565-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-140565-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id gs11-20020a056a004d8b00b006eb3b8c8fcasi1440603pfb.342.2024.04.11.07.12.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:12:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-140565-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-140565-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-140565-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 2D16BB26C32 for ; Thu, 11 Apr 2024 13:49:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DB02E14EC72; Thu, 11 Apr 2024 13:48:58 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1FAA614EC4D for ; Thu, 11 Apr 2024 13:48:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712843338; cv=none; b=dC/zdte+3FlVQFdoDA9IlCuhrNTHBW3XrfOCTXf7s87PLZRMJqM2mePhfNAuZFd/M1p5v/Vtawdz8UMAo+ymtTqUz0qKeuv3hNCqF1SFgJmVQZ4esJ6SMXJjpv/EVTS1VObV4yZHqqrgSS+rUcDtd4hzmB9Z1KH3vyGA0jsoJ3Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712843338; c=relaxed/simple; bh=u17hrof1UMfL/LrYC6HkU+xFZtX4qN2Q6BUksHZyoYo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=b5AwZFCVTOTafx7203jibGW62OhGofz5GDcyPws6jtakoVj8SvISMFhJjrNi5cEMFDFfNXvkJCp7tlZkDFzwbJRxqK8aYPlSKi7o4YH0celfQRSvUMZVhlM+8EKZuidw8KKAkkpoKVHQtoaGlbm4JSvdkWhWvXESHOsFlsE6okc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BA5C9339; Thu, 11 Apr 2024 06:49:25 -0700 (PDT) Received: from [10.1.38.151] (XHFQ2J9959.cambridge.arm.com [10.1.38.151]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D76F83F64C; Thu, 11 Apr 2024 06:48:53 -0700 (PDT) Message-ID: <3cda8e87-7095-4aad-beb1-6a420912df34@arm.com> Date: Thu, 11 Apr 2024 14:48:52 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 1/2] mm/madvise: optimize lazyfreeing with mTHP in madvise_free Content-Language: en-GB To: Lance Yang Cc: akpm@linux-foundation.org, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240408042437.10951-1-ioworker0@gmail.com> <20240408042437.10951-2-ioworker0@gmail.com> <38c4add8-53a2-49ca-9f1b-f62c2ee3e764@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit [...] >>> + >>> + if (!folio_trylock(folio)) >>> + continue; >> >> This is still wrong. This should all be protected by the "if >> (folio_test_swapcache(folio) || folio_test_dirty(folio))" as it was previously >> so that you only call folio_trylock() if that condition is true. You are >> unconditionally locking here, then unlocking, then relocking below if the >> condition is met. Just put everything inside the condition and lock once. > > I'm not sure if it's safe to call folio_mapcount() without holding the > folio lock. > > As mentioned earlier by David in the v2[1] >> What could work for large folios is making sure that #ptes that map the >> folio here correspond to the folio_mapcount(). And folio_mapcount() >> should be called under folio lock, to avoid racing with swapout/migration. > > [1] https://lore.kernel.org/all/5cc05529-eb80-410e-bc26-233b0ba0b21f@redhat.com/ But I'm not suggesting that you should call folio_mapcount() without the lock. I'm proposing this: if (folio_test_swapcache(folio) || folio_test_dirty(folio)) { if (!folio_trylock(folio)) continue; /* - * If folio is shared with others, we mustn't clear - * the folio's dirty flag. + * If we have a large folio at this point, we know it is + * fully mapped so if its mapcount is the same as its + * number of pages, it must be exclusive. */ - if (folio_mapcount(folio) != 1) { + if (folio_mapcount(folio) != folio_nr_pages(folio)) { folio_unlock(folio); continue; } What am I missing?