Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp2027091imm; Thu, 24 May 2018 04:45:35 -0700 (PDT) X-Google-Smtp-Source: AB8JxZr3+1zhVi0Ja2lhTu6eQEparFntIGMBkZMdvNNH31/mPQMFp701XUg/aYJkh+6OOAKI4XwM X-Received: by 2002:a17:902:2826:: with SMTP id e35-v6mr7011707plb.348.1527162335122; Thu, 24 May 2018 04:45:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527162335; cv=none; d=google.com; s=arc-20160816; b=YyN82GRqBwjNAMPkcrLjQVgcYdToJAKGKVuTrayPZsA84eeiUEJlRDfChXa6EIZde1 y0jXDUKtiKwMMTBF5wmz+ZdNKbSmZvsPCpdqcKNzfvOq8n/MqrvltrrLBpsgNYBwHFcg Z9IYrNrIbrzDtuo3hH3+TFOnJLfaSsFGJ1qGVgvmDOxElS1/t1B2z/LoF9BQ4lgjmzrP ul4eoUYSYH5p8bH1K68rTcHOi8tFHI6bt45QtQQH5F+cm3JsCuVFRpgmRZRzW0smWGKh 7P353SGhCusF16338iIA8Vii5MxIV0NtSPqphuu3r3paisuKBS8W/sfIwKbT3OBKAGYU C38g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=0gv1KvEttAMhp3x5iMsUAqDgGia06km8bt4LW3UV8mg=; b=YVTqRS8Tv4WoSOMtvRItNjK/aU7okrKEPM0FCxmT31VynbEtLj6lx8wKndfPk4gWrD RfK9AoAB5cew3gDyngkLivbprH/t9+j5UkeSXBFsHHBHbXaQhB5FeE3bpRLezEDRQAd8 cFDoRqDO//TAJPf18+rFsiRjFEUc2Qf7MCQgLFoJ7csOMlBQTg//094TqR/3H4DVWuO+ DZL16+8zKFfQSdn2FBG/jVJ+IwS5qME2rKwpZ0opFEHTrhhhNsNktilHBFYT5q4Pi8/+ hBeHQF+dlhXaOK5/VZ8cbmhNqvI+/ha+icuZWfrAiuSEu60CqK9k09UEmQZOL3DSOhj3 fjNg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c32-v6si15401260plj.177.2018.05.24.04.45.20; Thu, 24 May 2018 04:45:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S969265AbeEXLn6 (ORCPT + 99 others); Thu, 24 May 2018 07:43:58 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33165 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967235AbeEXLnw (ORCPT ); Thu, 24 May 2018 07:43:52 -0400 Received: by mail-wm0-f68.google.com with SMTP id x12-v6so26745783wmc.0; Thu, 24 May 2018 04:43:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0gv1KvEttAMhp3x5iMsUAqDgGia06km8bt4LW3UV8mg=; b=bI/letwMOpbzdDjPEA9sFYVCxIBBmDKlXGWHABDyi+H8aT9WaWBozTQVSnXIIBWX1I 49HpVm/5heB14cEgba4VmzcsmMFZTZjLwjENEoy7TFnYnwxFjQfn5x5OAFIDJRFvS0B/ BqY7gJrCQbSXXA/HYHrbuqaLBkyx2Js69a0/hkHJP3a7hyCKMhrJtbCVIWPxgXx5Ed50 g+bRpfFMMZgH/ipme4v+7hyjXE3VRD1z6+v0REIKKTvJkkYoebq9ccyhyeG5pH5Incwt haLWSTgTo4CiVQLnBanxKNH7ULqJyyE8u0KuUTEnqAE5aEMH4GDzpoJA3l+VC6eMMpuj OGcA== X-Gm-Message-State: ALKqPwedvsYw55M6aS4KE/cW0y2iDwJt5iXNVLO9P7Y55TSxKSgpL6JP aa0tko1KITmr3+DDl3KDZ+U= X-Received: by 2002:a50:83a5:: with SMTP id 34-v6mr11718283edi.276.1527162231211; Thu, 24 May 2018 04:43:51 -0700 (PDT) Received: from tiehlicka.suse.cz (prg-ext-pat.suse.com. [213.151.95.130]) by smtp.gmail.com with ESMTPSA id f16-v6sm11159981edr.32.2018.05.24.04.43.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 May 2018 04:43:50 -0700 (PDT) From: Michal Hocko To: Jonathan Corbet Cc: LKML , , , Michal Hocko , "Darrick J. Wong" , David Sterba Subject: [PATCH] doc: document scope NOFS, NOIO APIs Date: Thu, 24 May 2018 13:43:41 +0200 Message-Id: <20180524114341.1101-1-mhocko@kernel.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180424183536.GF30619@thunk.org> References: <20180424183536.GF30619@thunk.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Michal Hocko Although the api is documented in the source code Ted has pointed out that there is no mention in the core-api Documentation and there are people looking there to find answers how to use a specific API. Cc: "Darrick J. Wong" Cc: David Sterba Requested-by: "Theodore Y. Ts'o" Signed-off-by: Michal Hocko --- Hi Johnatan, Ted has proposed this at LSFMM and then we discussed that briefly on the mailing list [1]. I received some useful feedback from Darrick and Dave which has been (hopefully) integrated. Then the thing fall off my radar rediscovering it now when doing some cleanup. Could you take the patch please? [1] http://lkml.kernel.org/r/20180424183536.GF30619@thunk.org .../core-api/gfp_mask-from-fs-io.rst | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Documentation/core-api/gfp_mask-from-fs-io.rst diff --git a/Documentation/core-api/gfp_mask-from-fs-io.rst b/Documentation/core-api/gfp_mask-from-fs-io.rst new file mode 100644 index 000000000000..e8b2678e959b --- /dev/null +++ b/Documentation/core-api/gfp_mask-from-fs-io.rst @@ -0,0 +1,55 @@ +================================= +GFP masks used from FS/IO context +================================= + +:Date: Mapy, 2018 +:Author: Michal Hocko + +Introduction +============ + +Code paths in the filesystem and IO stacks must be careful when +allocating memory to prevent recursion deadlocks caused by direct +memory reclaim calling back into the FS or IO paths and blocking on +already held resources (e.g. locks - most commonly those used for the +transaction context). + +The traditional way to avoid this deadlock problem is to clear __GFP_FS +resp. __GFP_IO (note the later implies clearing the first as well) in +the gfp mask when calling an allocator. GFP_NOFS resp. GFP_NOIO can be +used as shortcut. It turned out though that above approach has led to +abuses when the restricted gfp mask is used "just in case" without a +deeper consideration which leads to problems because an excessive use +of GFP_NOFS/GFP_NOIO can lead to memory over-reclaim or other memory +reclaim issues. + +New API +======== + +Since 4.12 we do have a generic scope API for both NOFS and NOIO context +``memalloc_nofs_save``, ``memalloc_nofs_restore`` resp. ``memalloc_noio_save``, +``memalloc_noio_restore`` which allow to mark a scope to be a critical +section from the memory reclaim recursion into FS/IO POV. Any allocation +from that scope will inherently drop __GFP_FS resp. __GFP_IO from the given +mask so no memory allocation can recurse back in the FS/IO. + +FS/IO code then simply calls the appropriate save function right at the +layer where a lock taken from the reclaim context (e.g. shrinker) and +the corresponding restore function when the lock is released. All that +ideally along with an explanation what is the reclaim context for easier +maintenance. + +What about __vmalloc(GFP_NOFS) +============================== + +vmalloc doesn't support GFP_NOFS semantic because there are hardcoded +GFP_KERNEL allocations deep inside the allocator which are quite non-trivial +to fix up. That means that calling ``vmalloc`` with GFP_NOFS/GFP_NOIO is +almost always a bug. The good news is that the NOFS/NOIO semantic can be +achieved by the scope api. + +In the ideal world, upper layers should already mark dangerous contexts +and so no special care is required and vmalloc should be called without +any problems. Sometimes if the context is not really clear or there are +layering violations then the recommended way around that is to wrap ``vmalloc`` +by the scope API with a comment explaining the problem. -- 2.17.0