Re: [PATCH v2 02/11] mm/hmm: use reference counting for HMM struct v2

John Hubbard
2019-03-29T20:07:50Z

On 3/28/19 7:25 PM, Jerome Glisse wrote:
[...]
>> The input value is not the problem. The problem is in the naming.
>>
>> obj = get_obj( various parameters );
>> put_obj(obj);
>>
>>
>> The problem is that the function is named hmm_register() either "gets" a
>> reference to _or_ creates and gets a reference to the hmm object.
>>
>> What John is probably ready to submit is something like.
>>
>> struct hmm *get_create_hmm(struct mm *mm);
>> void put_hmm(struct hmm *hmm);
>>
>>
>> So when you are reading the code you see...
>>
>> foo(...) {
>> struct hmm *hmm = get_create_hmm(mm);
>>
>> if (!hmm)
>> error...
>>
>> do stuff...
>>
>> put_hmm(hmm);
>> }
>>
>> Here I can see a very clear get/put pair. The name also shows that the hmm is
>> created if need be as well as getting a reference.
>>
>
> You only need to create HMM when you either register a mirror or
> register a range. So they two pattern:
>
> average_foo() {
> struct hmm *hmm = mm_get_hmm(mm);
> ...
> hmm_put(hmm);
> }
>
> register_foo() {
> struct hmm *hmm = hmm_register(mm);
> ...
> return 0;
> error:
> ...
> hmm_put(hmm);
> }
>

1. Looking at this fresh this morning, Ira's idea of just a single rename
actually clarifies things a lot more than I expected. I think the following
tiny patch would suffice here (I've updated documentation to match, and added
a missing "@Return:" line too):

iff --git a/mm/hmm.c b/mm/hmm.c
index fd143251b157..37b1c5803f1e 100644
--- a/mm/hmm.c
+++ b/mm/hmm.c
@@ -50,14 +50,17 @@ static inline struct hmm *mm_get_hmm(struct mm_struct *mm)
}

/*
- * hmm_register - register HMM against an mm (HMM internal)
+ * hmm_get_create - returns an HMM object, either by referencing the existing
+ * (per-process) object, or by creating a new one.
*
- * @mm: mm struct to attach to
+ * @mm: the mm_struct to attach to
+ * @Return: a pointer to the HMM object, or NULL upon failure. This pointer must
+ * be released, when done, via hmm_put().
*
- * This is not intended to be used directly by device drivers. It allocates an
- * HMM struct if mm does not have one, and initializes it.
+ * This is an internal HMM function, and is not intended to be used directly by
+ * device drivers.
*/
-static struct hmm *hmm_register(struct mm_struct *mm)
+static struct hmm *hmm_get_create(struct mm_struct *mm)
{
struct hmm *hmm = mm_get_hmm(mm);
bool cleanup = false;
@@ -288,7 +291,7 @@ int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
if (!mm || !mirror || !mirror->ops)
return -EINVAL;

- mirror->hmm = hmm_register(mm);
+ mirror->hmm = hmm_get_create(mm);
if (!mirror->hmm)
return -ENOMEM;

@@ -915,7 +918,7 @@ int hmm_range_register(struct hmm_range *range,
range->start = start;
range->end = end;

- range->hmm = hmm_register(mm);
+ range->hmm = hmm_get_create(mm);
if (!range->hmm)
return -EFAULT;

2. A not directly related point: did you see my minor comment on patch 0001? I think it might have been missed in all the threads yesterday.

thanks,
--
John Hubbard
NVIDIA