Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp3026245rdb; Tue, 13 Feb 2024 04:59:31 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVOEHMuAkRuYAwGCxkFiSdKO/eO8nCoDSoe7Tmf9A7pWWEQEVXBhuANNnoZiv/go35XdJ0N+MqAspCVMLUyTuO9u8BwlQ1BCUlq/NkH4Q== X-Google-Smtp-Source: AGHT+IFOyCC2cxgfY7Jy4z63BFnGUoOqbFGeUeKdMzlBunMhVAB+0mTzo8xW9s9a0RBpGOCyJ233 X-Received: by 2002:ac8:5bd3:0:b0:42c:7a83:141d with SMTP id b19-20020ac85bd3000000b0042c7a83141dmr6235491qtb.5.1707829170926; Tue, 13 Feb 2024 04:59:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707829170; cv=pass; d=google.com; s=arc-20160816; b=fOJB2xAuHnqaDMmMjgiAXVQOVySgWULtNaDWgZw23eBKe7lG+43asLo0/MP8Zp3ZWW 8xAsM18EU/iaJ5NytFr942cOwQPKCO/9EwD9UcsW6aySZv4ZeKVvLP2u64Npv2/4iMzD oCvFutDRjDG0vXvDceyevxR9eqA0QthU6uOL7YEeFy0GDiL1xfxLKjFPyb0JABnCkeWL agxM0XRSa7tdsDyJJWs8y34BrvUcgC3NFXTkdyeq3SaUXjyYUfm5s4I4GcJldNC5M5xd UqirOXBfHl+G0gnqM1w8xkWmQaUR6XNlAKnAb7UiED1px2l7gZ9gZwjBvkRxfZxISrNW dIyg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=Z882ZjxzlMo5zCfCXALkNVq7HPWFHS34A/7S9zGVAME=; fh=w+09xFpe4lwWpyT3dZz92coMwr4gUByRGJex9DEMVAA=; b=kcYcu1/rkUUpw5Z1QKsxVoSP6ZSBSKVHdNVooBfd735J/98tU8Ul9M1ZJl9T2mPGpx Ok7JtyzynnT5yscyW5SFp2QBtln0gxNqFq8wvk1eVC09Po6lH7xp9NoPFFXuIiYzMo5T fGJzk2iOLE5Sm7T5fLyQQB2w6Z76232CVI+Wv9pw70tCc0oJbdfr/c9DjbB9DI0fJlLA /D/Cok0Lm93bj08utPaWFS+WmS+qqNQnaXrV4GbN9+uWldkePMd7jYC+dBWc8gi1tlGJ Uze5HGx3UdCu/p6E9E7Bd4efeJcP6Z0gGmAQ+3AfvAIkDmifGjSfoJQlDDbyRWRwKCnR Ie0w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=F+9muNtm; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-63546-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63546-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVTGc3uWnBsL7gUMKnEL+nBjLlL8RDDi+FVaHOPTGQgRzqphWVgC2tgq2epnPm1o6vndwX0i2omfgsVBW2B4cAHIDMarKav2vJyN/rPDA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id h12-20020ac846cc000000b0042c4d506a06si2615858qto.297.2024.02.13.04.59.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 04:59:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63546-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=F+9muNtm; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-63546-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63546-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A2C4A1C264FF for ; Tue, 13 Feb 2024 12:59:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3B15F487B6; Tue, 13 Feb 2024 12:59:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="F+9muNtm" Received: from mail-ua1-f41.google.com (mail-ua1-f41.google.com [209.85.222.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DED77482C5 for ; Tue, 13 Feb 2024 12:58:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707829141; cv=none; b=PIGZb2BoLzbno/QegriL7gBN8pNugCbr9wZswUAUHM1VfjKeFCl0b1QKNvO2hlkVyfVp23ZPhNNwGFFHYlJSusdBBwTXfxRIGu6u/syX2nH5ARmBe2cs+xdpZAuIRXOVWbY0KV9QiZk/fB66tAtX0UH7s6YucROflnwihspoSQ0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707829141; c=relaxed/simple; bh=HBXn8+mPI60aWGXqXtUgXSjVYMbihc5fwmExHw46ewo=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=qskflMktrWuhU4uBFAdzw9pncWI3e2/rNhYxnqLmL0N4WEP/DqyB54YPBZov31++YnF00roQAs3l0CXtLuRwgnXMyIMQTiAenamtI0K2oq9VWWJZZfbgmvGDafH1Ys0JoaWuOnLDpkDtnU+1c6tqegu+y5bSBYQt+T96aADGeMM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=F+9muNtm; arc=none smtp.client-ip=209.85.222.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-ua1-f41.google.com with SMTP id a1e0cc1a2514c-7ce55932330so1707150241.0 for ; Tue, 13 Feb 2024 04:58:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707829139; x=1708433939; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Z882ZjxzlMo5zCfCXALkNVq7HPWFHS34A/7S9zGVAME=; b=F+9muNtmfxJKvyhik3RT/Nw/ro9svhjuYDIgx4rTpLPzJ3pyqTd9WROVbW59aB36mA /lNorJ/On5vTS2+OR321d/hJK71hGNLkwzlamGd5KWrDLJOluLJ/Q3yR4DPUitb4UabN pjctP3aU8FKfZQEm96b7Z0F1fQS+tYdOEU5URZ/flevkgkPwechaEJEDl5sew88MJ5os fu1rgkb4pZSQNBxF74egfY7ySMPoRRwy9j4UQx0njirxg5S97FS24/2SHGZJIYHU/TAY O7F6+qggzjTDc1hZea0omNyTKXSWRzi8B0VcknYSsy8UDNFJ32Cy2V2JaNVWchxnU5hp gXaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707829139; x=1708433939; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Z882ZjxzlMo5zCfCXALkNVq7HPWFHS34A/7S9zGVAME=; b=HJCnJ/vtQC7spcPD/jvaWo/Wd3KcJE7kKeJCnULMoQ5wsep/4QHFxJPW5rZenar1d9 bpXTsSrRlOzdvICl7J2Szrz3RG53+QbjkmIEKwLRPGTgOyPz7ah6d4epb7gHwNnahDzC 4CrpmfqHhbr4i5b/NxNBOhSPUsH11iFlCKDIk+JLSLU2QQRVt3LLmsRAUTvwg0IVzhzV 612Cy/1g5FVUQiyB5jvTeJELFQxNAuGJcT0P20ptWvZ2kg88hXXI+Lu08FJfpvIYLK8T iEAN/S17MTdpI03nTwsPRK9ni7C4pZDaYmA7hqCC4cuWY8lQuJz6WRbOlwWsXsRag7ZD 1i5g== X-Forwarded-Encrypted: i=1; AJvYcCXqrVjY+WwhNWkFeDvEDfe1t50fS8u178/I3NOwFwPS0weoybYSDT32rGgcjRYruhCcEAX5MSkfoZjdBU3/jputWZ3WB2+MwpXeHMY0 X-Gm-Message-State: AOJu0Yyn6l9Skd5HRnPvORapqXG8JuXWPG5VKS1BqjjWdRE56zT1o/TE JsYhKJhSAUw0AI9YjCDufDSTOKp3NekFR+GYc2t2TYHwSw8C8DYGfriOZT//rxUiMPBWfLPK2r5 hY/0q0qDu51h/FgQbfhXCpQa4Xq8F90IWhTrI X-Received: by 2002:a05:6102:30b8:b0:46d:2b65:aa16 with SMTP id y24-20020a05610230b800b0046d2b65aa16mr4623538vsd.34.1707829138393; Tue, 13 Feb 2024 04:58:58 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240212223029.30769-1-osalvador@suse.de> <20240212223029.30769-3-osalvador@suse.de> <11cb2ac2-102f-4acd-aded-bbfd29f7269a@suse.cz> In-Reply-To: From: Marco Elver Date: Tue, 13 Feb 2024 13:58:20 +0100 Message-ID: Subject: Re: [PATCH v8 2/5] mm,page_owner: Implement the tracking of the stacks count To: Oscar Salvador Cc: Vlastimil Babka , Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Andrey Konovalov , Alexander Potapenko Content-Type: text/plain; charset="UTF-8" On Tue, 13 Feb 2024 at 13:39, Oscar Salvador wrote: > > On Tue, Feb 13, 2024 at 12:34:55PM +0100, Vlastimil Babka wrote: > > On 2/13/24 10:21, Marco Elver wrote: > > > On Tue, 13 Feb 2024 at 10:16, Vlastimil Babka wrote: > > >> Isn't this racy? Shouldn't we use some atomic cmpxchg operation to change > > >> from REFCOUNT_SATURATED to 1? > > > > > > If 2 threads race here, both will want to add it to the list as well > > > and take the lock. So this could just be solved with double-checked > > > locking: > > > > > > if (count == REFCOUNT_SATURATED) { > > > spin_lock(...); > > > > Yeah probably stack_list_lock could be taken here already. But then the > > kmalloc() of struct stack must happen also here, before taking the lock. > > I am thinking what would be a less expensive and safer option here. > Of course, taking the spinlock is easier, but having the allocation > inside is tricky, and having it outside could mean that we might free > the struct once we checked __within__ the lock that the refcount > is no longer REFCOUNT_SATURATED. No big deal, but a bit sub-optimal. > > On the other hand, IIUC, cmpxchg has some memory ordering, like > store_and_release/load_acquire do, so would it be safe to use it > instead of taking the lock? Memory ordering here is secondary because the count is not used to release and later acquire any memory (the list is used for that, you change list head reads/writes to smp_load_acquire/smp_store_release in the later patch). The problem is mutual exclusion. You can do mutual exclusion with something like this as well: > if (refcount_read(&stack->count) == REFCOUNT_SATURATED) { > int old = REFCOUNT_SATURATED; > if (atomic_try_cmpxchg_relaxed(&stack->count.refs, &old, 1)) > add_stack_record_to_list(...); > } > refcount_inc(&stack->count); Probably simpler.